deffib_recursive(n):returnfib_recursive(n -1)+fib_recursive(n -2)if n >1else ndeffib_iterative(n): a, b =0,1for _ inrange(1, n +1): a, b = b, a + breturn a
The recursive version needlessly recomputes the same values and has an exponential running time. The iterative version eliminates this deficiency. Otherwise, both correctly return F20=6765.
Exercise 2.2
The total number of arithmetic operations (additions, subtractions and divisions), including loop increments and index calculations, is
def c(n):
return (1 + 1 / n) * c(n - 1) + 2 if n > 1 else 2
It uses only 5(Nmax−1) arithmetic operations to compute the same value as Program 2.1.
Exercise 2.4
A non-recursive program would require Θ(N) time to compute values using recurrence (2) and Θ(N2) for recurrence (3), as shown in Exercise 2.2.
A recursive program would also require Θ(N) time to compute values using recurrence (2). Despite an exponentially growing number of subproblems, their sizes are also exponentially dropping. On the other hand, calculating values with recurrence (3), as mentioned in the book, has unacceptable performance. By instrumenting the code and counting the number of function calls (excluding the base cases) we get an interesting sequence of numbers for N∈[1,10]. It is (1,3,9,27,81,243,729,2187,6561,19683), where the elements are powers of 3. Therefore, the number of operations is Θ(3N). As a side note, we can formally prove this via induction on N, using the recurrence describing the number of calls rN=1+2∑k=0N−1rk (with r0=0) as the inductive hypothesis.
Qualifying variants with tight asymptotic bounds is convenient here. For example, we don’t need more details to understand that Θ(3N) is prohibitive.
Exercise 2.5
We can’t directly implement the recurrence for the radix exchange sort using the technique demonstrated in Program 2.1. To calculate CN we need to know CN. Let’s transform it
Based on closed form formulas for quicksort variants as well as analysis of radix exchange sort, we’d expect that the average number of compares approaches the following asymptotic approximations for sufficiently large N:
Standard quicksort: ∼2NlnN≈1.386NlgN
Median-of-three quicksort: ∼(12/7)NlnN≈1.188NlgN
Radix exchange sort: ∼NlgN
Nonetheless, for smaller values of N we can’t ignore lower-order terms, as the data in Table 2.5 shows. This exercise serves as a perfect example of why relying only on the leading asymptotic term can lead to the wrong conclusion for practical input sizes. The book highlights this fact many times. Gradual asymptotic expansion allows fine-tuning the model to reflect the desired accuracy in relation to input size ranges.
Exercise 2.6
Denote by Un and Vn solutions to homogeneous recurrences un and vn, respectively, where f(x,y)=x+y.
un=f(un−1,un−2) for n>1 with u0=1 and u1=0,vn=f(vn−1,vn−2) for n>1 with v0=0 and v1=1.
According to the book, an=pUn+qVn, where Un=Fn−1 and Vn=Fn, thus an=pFn−1+qFn for n>1. The reason why Un=Fn−1 is because it starts as 1,0,1,1,..., hence behaves like the Fibonacci sequence from the second element.
Exercise 2.7
The homogeneous part remains the same as in Exercise 2.6. The constant term r "obeys" the same Fibonacci like growth pattern, with a difference, that in each iteration we also add one additional instance of r. Imagine always starting a new Fibonacci sequence and adding it to the existing count. We get an=pFn−1+qFn+r∑k=0n−1Fk for n>1.
🌟 Exercise 2.8
This is a generalization of the previous two exercises. For f linear, we can express the solution to the recurrence
an=f(an−1,an−2) for n>1
in terms of a0, a1, f(0,0), Un and Vn, where Un and Vn are solutions to homogeneous recurrences un and vn, respectively.
un=f(un−1,un−2)−f(0,0) for n>1 with u0=1 and u1=0,vn=f(vn−1,vn−2)−f(0,0) for n>1 with v0=0 and v1=1.
Thus, an=a0Un+a1Vn+f(0,0)∑k=0n−1Vk for n>1.
in terms of the x’s, the y’s and the initial value at. Following through the same steps as in the proof of Theorem 2.1 and stopping at at, we get
an=yn+at(xnxn−1…xt+1)+t+1≤j<n∑yjxj+1xj+2…xn for n>t.
As a sanity check, try shifting the recurrence in Exercise 2.13 and compute values as described here. For example, use t=2 and at=2 to get a7=9/2 in both ways, via the original recurrence and this synthesized formula. When xn=n/(n+1) then xnxn−1…xt+1=(t+1)/(n+1).
Before proceeding we must resolve the base cases for N≤6; we must ensure that the summation factor is non-zero. Using the recurrence for AN we get A2=0,A3=2,A4=1,A5=9/5,A6=2. Following the same steps from Exercise 2.16 (see also Table 2.2 from the book), we get
AN=72(N+1) for N>6.
🌟 Exercise 2.18
From the error term bn=an−α we have an−1=bn−1+α. We can substitute it back into the main recurrence
The solution is known as the Dottie number. We must show that all conditions are satisfied for applying the Banach fixed point theorem; it then guarantees that the function cos(x) converges to a unique fixed point for any a0∈[0,1].
Let f(x)=cos(x) and x∈[0,1]. We know that f:[0,1]↦[0,1] and is differentiable on this whole interval. Thus, by the mean value theorem, for any distinct x,y∈[0,1], there’s a point c∈(x,y) such that f(x)−f(y)=f′(c)(x−y). Consequently, we can always express the distance between f(x) and f(y) in terms of the distance between x and y. If ∣f′(c)∣<1 for all c∈(0,1), then f has a Lipschitz constant less than 1, therefore f is a contraction mapping on [0,1]. This is indeed the case, since, f′(x)=sin(x) and sin(1)<1.
We can also represent the continued fraction (mentioned in the book) as f(x)=1/(1+x) for x∈(0,1]. I’s easy to show that all conditions hold for applying the Banach theorem.
Exercise 2.20
The function f(x)=0.5(x−1/x) has a singularity at zero. As ∣x∣→1, ∣f(x)∣→0 and as ∣x∣→0, ∣f(x)∣→∞. The sequence can’t converge; as soon as it starts to approach ±1, it’ll move to zero, from zero to large values and back again. These wild oscillations will continue forever. Of course, ∣x∣=1 would immediately "push" f into singularity.
🌟 Exercise 2.21
We prove the lower bound in a similar fashion, as the book does for the upper bound
an1=an−11(1−an−11)=an−11+1−an−11<an−11+2…<2(n+1)≤3n∴an≥31⋅n1=Ω(1/n).(for n>0)(1−an−1>1/2 for sufficiently large n)(telescoping sum)(for all n>1)
Both asymptotic bounds match, hence an=Θ(1/n). After computing initial terms, it seems that an∼c/n, where c=1. Let’s prove that limn→∞nan=1/ann=1. According to the book, we know that an→0 as n→∞, so 1/an→∞. We can apply the Stolz–Cesàro theorem, which is a discrete version of L'Hôpital’s rule. The theorem states that if the limit of the differences 1/an−1/an−1n−(n−1) exists, then limn→∞1/ann is equal to that same limit. The numerator is 1, and the denominator is 1/(1−an−1) (see the derivation of the lower bound). Thus,
n→∞lim1/(1−an−1)1=n→∞lim(1−an−1)=1.
By the Stolz–Cesàro theorem, since the limit of the differences is 1, the limit of the original sequence is also 1.
🌟 Exercise 2.22
First we prove that limn→∞an=0. For x∈(0,1] we have that 0<sin(x)<x; the geometric proof relies on the definition of radian. Consequently, an is a strictly decreasing sequence bounded from below by zero. According to the monotone convergence theorem, it converges to the infimum, which is zero.
Since an→0, we can use the Taylor series for sin(x) around x=0:
sin(x)=x−3!x3+5!x5−⋯=x−6x3+O(x5).
We can substitute this expansion into the recurrence an=sin(an−1) to get
an≈an−1−6an−13=an−1(1−6an−12).
If we square both sides of the equation, then we can use the approximation (1−x)2≈1−2x for small x. Thus,
an2≈an−12(1−6an−12)2=an−12(1−3an−12).
Following the hint from the book, let’s flip the above equation to get
an21≈an−12(1−3an−12)1≈an−121(1+3an−12)=an−121+31.(1−x1≈1+x for small x)
Now we can use the substitution bn=1/an to make a telescoping sum
bn2≈bn−12+31=⋯=1+3n.
Therefore, bn=O(n)⟹an=O(1/n).
🌟 Exercise 2.23
If ∣f′(α)∣>1, then α is a repelling fixed point of a differentiable function f. The sequence an=f(an−1) starting at any point a0 near α (but not exactly α) will diverge from the fixed point α. To develop an intuition why, let’s use the familiar error term from the book bn=an−α and monitor how it changes in each iteration.
anα+bn=f(an−1)=f(α+bn−1)≈f(α)+f′(α)⋅bn−1∴bn≈f′(α)⋅bn−1(by Taylor expansion near α, f(α+bn−1)≈f(α)+f′(α)⋅bn−1)(α=f(α)).
Since f′(α)>1 the error term grows over time instead of shrinking. In general, what happens in later iterations highly depends on the global properties of the function. The point is, once the error term becomes small, the above approximation for bn becomes relevant and it’ll start growing again.
🌟 Exercise 2.24
The analysis relies on the approximation of the error term bn from Exercise 2.23 and the precondition stated in the book that a0 is sufficiently close to α. The three cases are:
When 0<∣f′(α)∣<1 (simple convergence) the fixed point α is attracting and the sequence an is guaranteed to converge to α. The error drops by a constant factor ∣f′(α)∣ at each step, hence the number of significant digits increases by a fixed amount in each iteration.
When f′(α)=0 (quadratic convergence) the fixed point α is super-attracting. Convergence is guaranteed and is extremely fast. Here, the error term must be more precisely described using a higher-order derivative, assuming f is at least twice continuously differentiable, for example, bn≈2f′′(α)⋅bn−12. The error at each step is proportional to the square of the previous error. Each iteration approximately doubles the number of significant digits available.
When ∣f′(α)∣=1 ("slow" convergence) the fixed point α is neutral. Convergence isn’t guaranteed by this criterion alone and depends on the higher-order terms of the function f. If the sequence does converge, then the speed is sublinear. The shrinkage of the error term isn’t driven by a constant factor, but rather it decreases "harmonically" (see Exercise 2.21 and Exercise 2.22).
Testing Convergence Using Only a0
Since α is unknown (it is usually the value we are trying to find), we cannot check f′(α) directly. Instead, we must rely on theorems that predict convergence based on the starting region I=[a0−δ,a0+δ], for some constant δ>0, that contains α (see Exercise 2.19).
For practical purposes, we should check the derivative ∣f′(a0)∣. We have three major cases:
If ∣f′(a0)∣<1, then the sequence will likely converge. However, we must ensure that the derivative stays less than 1 as the sequence moves. If it grows larger than 1 between a0 and α, the sequence may diverge.
If ∣f′(a0)∣>1, then the sequence will locally diverge (move away from a0). It might jump to a different basin of attraction where the derivative is smaller, or it might diverge to infinity.
If ∣f′(a0)∣=1, then we may anticipate a very slow convergence, although we cannot be sure.
At any rate, one viable approach is to monitor the step size during the iterative process. Assuming that f is a contraction mapping with a Lipschitz constant 0≤k<1, we have
We want to estimate the distance between the current point an and the fixed point α. Since the sequence converges to α, we can express α as the limit of the sequence an+m as m→∞. We can expand the distance ∣an−α∣ as the sum of all future steps using the triangle inequality:
∣an−α∣=∣an−an+1+an+1−an+2+…∣≤∣an−an+1∣+∣an+1−an+2∣+∣an+2−an+3∣+…≤(k∣an−an−1∣)+(k2∣an−an−1∣)+(k3∣an−an−1∣)+…=∣an−an−1∣(k+k2+k3+…)=∣an−an−1∣⋅1−kk(iterating the distances)(∑i=1∞ki=1−kk).
We’ve a clear stopping criteria, since we can check whether the current step size satisfies the inequality, given the desired accuracy ∣an−α∣=ϵ>0. Usually, this should be attained in some predefined number of steps (maximum number of iterations) that could be specified as a parameter. The step sizes should strictly decrease, hence the ratio of subsequent values should be less than one. Of course, this happens when the sequence converges.
Exercise 2.25
The book mentions that restructuring the terms of a recurrence corresponds to factoring the polynomial whose degree equals the order of the recurrence. There are two worked-out examples:
For an=2n−1=2n−1n the "characteristic polynomial" is (1−2x)(1−x) in factored form.
For an=3n−2n the "characteristic polynomial" is (1−3x)(1−2x) in factored form.
In our case, we need to work with a cubic polynomial
(1−4x)(1−3x)(1−2x)=1x0−9x1+26x2−24x3.
The term xd is associated with an−d. Therefore, we can immediately construct our recurrence
an=9an−1−26an−2+24an−3 for n>2.
The initial values are
a0a1a2=40−30+20=1=41−31+21=3=42−32+22=11.
In practice, it’s more common to work with a characteristic equation whose roots correspond to terms in a solution. This is also employed later in the book.
🌟 Exercise 2.26
The main idea is the same as in Exercise 2.8. The notes for recitation 12, from the course 6.042/18.062J Mathematics for Computer Science (2005), summarize the methodology of solving inhomogeneous linear recurrences. It contains additional examples for each recurrence type.
The paper Some Techniques for Solving Recurrences by GL is a superb tutorial on solving recurrences with advanced material on systematic search for particular solutions of inhomogeneous linear recurrences. The text covers many recurrence types that occur in practical applications.
Exercise 2.27
We know that an=c03n+c12n, as stated in the book. Furthermore, we have
a0a1=c0+c1=3c0+2c1.
We need to work backwards to find initial conditions resulting in c0=0 and c1=1. We immediately get a0=1 and a1=2. There are no initial conditions for which the solution is an=2n−1.
🌟 Exercise 2.28
The book refers to recurrence an=2an−1−an−2+2an−3 for n>2. The characteristic equation has two complex roots, so the general solution is an=c02n+c1in+c2(−i)n. However, there is no way to set initial conditions for an to be constant, except trivially being zero, which is only a special case. So, we assume that this exercise is about the one given in the main text.
By tweaking initial conditions, we can fundamentally change the growth rate of the solution to
an=2an−1+an−2−2an−3 for n>2.
We use the same approach as in Exercise 2.27. Here are the initial conditions leading to each desired growth rate of an:
Constant is achieved when a0=a1=a2=c0. For example, a0=a1=a2=2 makes an=2.
Exponential is achieved when a0=c2, a1=2c2 and a2=4c2. For example, a0=1, a1=2 and a2=4 makes an=2n.
Fluctuating in sign is achieved when a0=c1, a1=−c1 and a2=c1. For example, a0=1, a1=−1 and a2=1 makes an=(−1)n.
Exercise 2.29
The characteristic equation is z2−2z−4=0 whose roots are z1=1+5 and z2=1−5. Therefore, the general solution is an=c0(1+5)n+c1(1−5)n for n>1. We also have the following system of linear equations:
a0a1=1=c0+c1=2=c0(1+5)+c1(1−5).
The constants are c0=255+1 and c1=255−1.
Exercise 2.30
The characteristic equation is z2−2z+1=0 whose double root is z1=1. Therefore, the general solution is an=c0+c1n for n>1. We also have the following system of linear equations:
a0a1=0=c0=1=c0+c1.
The constants are c0=0 and c1=1.
If we change the initial conditions, then we get another system of linear equations:
a0a1=1=c0=1=c0+c1.
The constants are c0=1 and c1=0.
🌟 Exercise 2.31
We should solve the recurrence an=an−1−an−2 for n>1 with a0=0 and a1=1. The characteristic equation is z2−z+1=0 whose complex roots are z1=(1+3i)/2 and z2=(1−3i)/2. We could continue as usual, but the expression for an would be awkward. Let’s use Euler’s formula to convert complex numbers into trigonometric form. The magnitudes of the roots are 1. They can be represented as e±i3π=cos(3π)±isin(3π). Thankfully to Euler’s formula exponentiation becomes trivial. The general solution is
Since the sequence an is real (it starts with real numbers a0,a1 and has real coefficients), the constants c0 and c1 must be complex conjugates. Consequently, both constants c0′ and c1′ are also reals. We also have the following system of linear equations:
This was an amazing journey from a seemingly straightforward second-order linear recurrence to the trigonometric solution via complex algebra.
🌟 Exercise 2.32
We should solve the recurrence 2an=3an−1−3an−2+an−3 for n>2 with a0=0, a1=1 and a2=2. The characteristic equation is 2z3−3z2+3z−1=0. Let’s first search for a rational root. If we can find one, then by dividing the original polynomial with the corresponding factor, we get a quadratic equation. The latter is then directly solvable. The rational root theorem enumerates all rational candidates. It turns out that ½ is a root, thus (2z−1) is a factor.
Using a classical polynomial division algorithm, we get that the characteristic equation in factored form is
(z2−z+1)(2z−1)=0.
The other two complex roots are z2,3=21±i23. Using the same transformation as in Exercise 2.31, we can write down the general solution as
an=c0(21)n+c1cos(3nπ)+c2sin(3nπ).
We also have the following system of linear equations:
We need to find a recurrence describing a sequence for which the order of growth decreases exponentially for odd-numbered terms, but increases exponentially for even-numbered terms. Let’s split the problem into two subproblems based on parity of n>1
an=⎩⎨⎧2an−221an−2if n is even,if n is odd.
We can combine both cases into a single recurrence an=2(−1)nan−2 for n>1. The initial conditions are a0=a1=1.
Exercise 2.34
The sequence is known as Tribonacci numbers. The characteristic equation is z3−z2−z−1=0, whose single real root is the Tribonacci constant α≈1.839286755. The other two complex roots β and γ have magnitudes less than one, hence they tend to zero for large N. Therefore, the approximate general solution is
FN(3)≈c0αN.
Tools are useful in finding roots of the characteristic polynomial. For example, issuing x^3-x^2-x-1=0 in WolframAlpha gives three solutions (one real and two complex). Switching to trigonometric form reveals the magnitudes of complex roots.
Finding the Unknown Constant c0
To find the constant c0 we need to use the exact general solution
The matrix on the left is a Vandermonde matrix. For any set of initial conditions F0,F1,F2, the "standard formula" for the constant c0 is
c0=(α−β)(α−γ)F2−F1(β+γ)+F0(βγ)=(α−β)(α−γ)1.
The characteristic polynomial is P(z)=z3−z2−z−1=(z−α)(z−β)(z−γ). Let’s take the derivative of P(z) on both sides and make them equal for α. On the left we have
The initial conditions are b1=b0=1, thus bn=Fn+1. Therefore,
an=n!Fn+1.
Exercise 2.36
The following Python script implements the algorithm for checking the validity of a monomial; covers both types of monomials (pure and mixed). The running time is Θ(plgp+qlgq).
def is_valid_monomial(s, t, n):
"""
This function checks if a given monomial, represented by two
lists s and t, appears in the expansion of a[n], for n > 1.
The function returns True if the monomial is valid, otherwise False.
"""
assert(n > 1)
# Insert sentinel values to avoid index errors.
s.append(0)
t.append(0)
i = len(s) - 1
j = len(t) - 1
s.sort()
t.sort()
while n > 1:
# Simple reduction rules:
# 1. If the largest element is in s,
# then the sub-monomial comes from the previous iteration.
# 2. If the largest element is in t,
# then the sub-monomial comes from two iterations back.
# The premise is that the current monomial is valid
# iff built from a valid sub-monomial. Easily provable by induction.
if s[i] > t[j]:
if s[i] != n - 1:
return False
i -= 1
n -= 1
else:
if t[j] != n - 2:
return False
j -= 1
n -= 2
return n > 0 and i == j == 0
In the expansion of an, for n>1, we have Fn−1−21−(−1)n mixed monomials (consisted of both symbols si and tj). The total number of such monomials, across all iterations, is
In the derivation, we’ve used the expression for the sum of Fibonacci numbers. For example, when n=6 this formula evaluates to 12, which matches the number of mixed monomials listed in the book.
If we count all monomials, including initial values, then the formula for their total number is much simpler; it is Fn+2−1.
Exercise 2.37
Solving bn proceeds along the same steps as in Exercise 2.29 and won’t be repeated here. The exact solution is
bn=32(1−(−21)n) for n>1.
Therefore,
an=232(1−(−21)n) for n>1.
Exercise 2.38
Let’s square both sides of the initial recurrence to get an2=1+an−12 for n>0. If we introduce a substitution bn=an2 then the recurrence transforms into bn=1+bn−1, thus bn=n. Therefore, an=n for n>0.
Exercise 2.39
We need to solve the register allocation recurrence an=an−12−2 for n>0 and various initial values.
Case 1: a0=3
The solution is
an=(23+5)2n+(23−5)2n.
As the book notes, only the larger of the two roots predominates in this expression—the one with the plus sign.
Case 2: a0=4
The solution is
an=(2+3)2n+(2−3)2n.
Again, only the larger of the two roots predominates in this expression—the one with the plus sign.
Case 3: a0=23
This case is fundamentally different compared to the previous two. The roots b0 are complex numbers
b0=21(23±−7/4)=43±i47.
We can use the same approach as in Exercise 2.31 to transform these into trigonometric form. The magnitude of each root is one. Let b0=e±iθ, thus the recurrence becomes
an=(eiθ)2n+(e−iθ)2n=ei(2nθ)+e−i(2nθ)=2cos(2nθ)(eix+e−ix=2cos(x) because sin(−x)=−sin(x)).
The sequence an will therefore fluctuate chaotically between -2 and 2, never settling on a single value.
🌟 Exercise 2.40
This exercise builds upon the so-called bifurcation theory, since according to Exercise 2.39 we perceive a fundamentally different behavior around the bifurcation point a0=2.
The accurate approximate answer for very large ϵ and sufficiently large n is an≈a02n. Usually, in expressions, like a0=2+ϵ, ϵ is a small positive constant, so the subsequent analysis assumes this situation. The critical term is
As n increases, (b0−)2n will rapidly vanish to 0. Consequently, we get an≈(1+ϵ)2n.
All in all, this shows that even for an infinitesimally small ϵ, the sequence an will diverge to infinity at a double-exponential rate. Recall from the book that it has a nice solution when ϵ=0.
🌟 Exercise 2.41
We need to find all values of the parameters α, β and γ such that an=αan−12+βan−1+γ reduces to bn=bn−12−2 by a linear transformation bn=f(α,β,γ)an+g(α,β,γ). In particular, we must also show that an=an−12+1 does not reduce to this form.
This exercise lacks an explicit assumption that f=0. Otherwise, we can define g=2 and all sequences an would "turn" into bn=bn−12−2 using the transformation bn=0⋅an+g.
We now match the coefficients of this result with the given recurrence an=αan−12+βan−1+γ and derive the necessary and sufficient condition for transforming an into bn=bn−12−2
α=f
β=2g⟹g=β/2
γ=fg2−g−2=αβ2/4−β/2−2⟹4αγ=β2−2β−8.
Analysis of an=an−12+1
The parameters are α=1, β=0 and γ=1. Clearly
4αγ=4=β2−2β−8=−8.
This shows that an=an−12+1 does not reduce to the target form.
🌟 Exercise 2.42
We need to solve the recurrence an=2an−11−an−12 for n>0 and different initial values. Let’s square both sides and use the substitution bn=an2. We get
The idea is to leverage the result from Exercise 2.41 and transform bn into cn=cn−12−2. The parameters are α=−4=f, β=4=2g and γ=0. Since 4⋅(−4)⋅0=42−2⋅4−8, we can apply the transformation, thus cn=−4bn+2.
If a0=1/2, then b0=1/4, thus c0=1. We already know the solution from the book for this special case, namely cn=−1. Therefore, bn=3/4 which implies that an=3/2 for n>0.
If a0=1/3, then b0=1/9, thus c0=14/9. We hit the third case in Exercise 2.39, so the solution is cn=2cos(2nθ), where θ=arccos(7/9). Therefore, bn=(1−cos(2nθ))/2, which implies that an=(1−cos(2nθ))/2.
Here’s the Python script to plot a6 as a function of a0.
from math import sqrt
import numpy as np
import matplotlib.pyplot as plt
def a(a0):
x = a0
for _ in range(6):
x = 2 * x * sqrt(1 - x * x)
return x
def plot_a(num_points=1000, save_path="a_plot.png",
label_fontsize=18, tick_fontsize=14, title_fontsize=20):
xs = np.linspace(0.0, 1.0, num_points)
ys = [a(float(x)) for x in xs]
plt.figure(figsize=(8, 5))
plt.plot(xs, ys, marker='o', linestyle='-', markersize=4)
plt.grid(True)
plt.xlabel(r"$a_0$", fontsize=label_fontsize)
plt.ylabel(r"$a_6$", fontsize=label_fontsize)
plt.title(r"Values of $a_6$ for $a_0$ on the interval $[0,1]$",
fontsize=title_fontsize)
plt.xticks(fontsize=tick_fontsize)
plt.yticks(fontsize=tick_fontsize)
plt.tight_layout()
plt.savefig(save_path, dpi=150)
plt.show()
if __name__ == "__main__":
plot_a()
The plot shows chaotic dynamics, where the system’s state after only 6 steps is almost random and highly unpredictable; looks like a wildly oscillating wave. As a0 approaches 1 the oscillations become extremely rapid.
🌟 Exercise 2.43
Our starting recurrence is a generalized "continued fraction" representation
an=γan−1+δαan−1+β for n>0.
Let’s represent the recurrence as a function f(x)=(αx+β)/(γx+δ), where γ=0 (otherwise, we won’t have a continued fraction). This function defines a Möbius transformation. We first need to determine the fixed points denoted by r and s. Afterward we can leverage a change of variable to linearize the original recurrence
bn=an−san−r=f(an−1)−sf(an−1)−r=γan−1+δαan−1+β−sγan−1+δαan−1+β−r=γan−1+δαan−1+β−sγan−1−sδγan−1+δαan−1+β−rγan−1−rδ=αan−1+β−sγan−1−sδαan−1+β−rγan−1−rδ=α1−bn−1r−sbn−1+β−sγ1−bn−1r−sbn−1−sδα1−bn−1r−sbn−1+β−rγ1−bn−1r−sbn−1−rδ=1−bn−1αr−αsbn−1+β−βbn−1−srγ+s2γbn−1−sδ+sδbn−11−bn−1αr−αsbn−1+β−βbn−1−r2γ+rγsbn−1−rδ+rδbn−1=αr−αsbn−1+β−βbn−1−srγ+s2γbn−1−sδ+sδbn−1αr−αsbn−1+β−βbn−1−r2γ+rγsbn−1−rδ+rδbn−1=rα+β−srγ−sδ+(s2γ+sδ−sα−β)bn−1(−sα−β+rsγ+rδ)bn−1−(r2γ+rδ−rα−β)=rα+β−srγ−sδ−sα−β+rsγ+rδ⋅bn−1=γr+δγs+δ⋅bn−1=(γr+δγs+δ)nb0(assuming γan−1+δ=0)(an−1=1−bn−1r−sbn−1)(assuming bn−1=1)(r and s are roots of the quadratics)(−sα−β=−s2γ−sδ and rα+β=r2γ+rδ)(iterating).
The initial value is b0=(a0−r)/(a0−s)=(1−r)/(1−s). If we substitute b0 into the expression for an we get
an=(1−s)−(1−r)(γr+δγs+δ)nr(1−s)−s(1−r)(γr+δγs+δ)n for n>0.
As a quick check, we should get the closed-form solution for the continuted fraction example from the book by using α=0,β=1,γ=1 and δ=1. The rroots of the equation x2+x−1=0 are r=−ϕ and s=−ϕ^. If we substitute these values into the formula for an, and use the properties of the golden ratio, then we get an=Fn+1/Fn+2 for n≥0.
🌟 Exercise 2.44
We need to express the recurrence
an=sn+tnan−11 for n>0,
where sn and tn are arbitrary sequences, as the ratio of two successive terms in a sequence defined by a linear recurrence.
Let an=bn/bn+1 for n≥0. We need to specify a linear recurrence relation on bn such that an also satisfies the original non-linear recurrence. We have
As a qucik check, we should get the closed-form solution for the continued fraction example from the book by using b0=1,sn=1 and tn=1. Indeed, bn=Fn+1 implying an=Fn+1/Fn+2 for n≥0.
🌟 Exercise 2.45
We are given a relaxed quicksort recurrence
an=f(n)+n21≤j≤n∑aj−1 for n>0 with a0=0.
We need to solve it when f(n)=n3. The repertoire table from the book has many reusable entries except for the n3 term. If we set an=n3 then an−(2/n)∑j=1naj−1=n3/2+n2−n/2. We need to combine entries from the expanded table to get f(n)=n3, thus we have
The post on StackExchange for Mathematics explains the basics of the repertoire method and illustrates its application by solving this exercise.
🌟 Exercise 2.47
Let’s use the next Python script to calculate numerical values of an, alongside our guess that an∼2/n. This hypothesis is based on the observation that an−1→0 as n→∞.
a = 1
for n in range(1, 11):
a = 2/(n+a)
print(f"a({n})={a}, â({n})={2/n}")
This process continues depending on a desired accuracy level.
There’s no simple closed-form solution to an. The standard method to "solve" it is based on the technique from Exercise 2.44. In this case, the substitution should be an=2bn−1/bn for n≥1, that yields bn=snbn−1+2tnbn−2, where sn=n and tn=1. The initial values are b0=1 and b1=2.
🌟 Exercise 2.48
The book assumes (without stating it explicitly) that we need to find the approximation of the average number of compares used by median-of-three quicksort. For the sake of completeness, it’s repeated here
CN=N+1+(3N)2k=1∑N(k−1)(N−k)Ck−1.
The key is to first convert this discrete recurrence into its continuous (integral) form, which is what we need for bootstrapping when working with recurrences containing summation over a full history. The process can be broken down into three major stages.
1
Converting the Discrete Recurrence
Let’s simplify the constant multiplier of the sum for large enough N
(3N)2=6N(N−1)(N−2)2≈N3/62=N312.
Let’s set the pivot rank k relative to the size N; for this we use the continuous variable x=k/N⟹k=xN⟹dk=Ndx. Finally, we treat k−1≈k and N+1≈N. If we substitute these changes into our original expression and replace the summation with an integral, we have
CN≈N+12∫01(x−x2)CxNdx.
2
Bootstrapping the Solution
Our initial hypothesis is CN∼αNlnN. After all, we expect to have a familiar asymptotic form Θ(NlnN) as other mergesort variants. Let’s substitute this hypothesis into our recurrence relation
If we want to retain only the leading factor, then we should choose α=12/7 to "eradicate" the O(N) summand.
To be rigorous, we should check that this form is "stable"; if we assume αNlnN+O(N), we get αNlnN+O(N) back. This is indeed true, since the definite integral
N+12∫01[α(xN)ln(xN)+βN](x−x2)dx
resolves again to the same form. β denotes the hidden constant inside the O(N) term.
🌟 Exercise 2.49
With bootstrapping we start with a rough bound and then improve it step by step. The recurrence is
an=n10≤k<n∑n−kak=n21+n11≤k<n∑n−kak.
We separated the k=0 term, which is n(n−0)a0=n21.
Iteration 1: From O(1/n) to O(n2lnn)
Let’s start with the rough guess that an=O(1/n) for n>0. This means there exists some constant c>0 such that an≤c/n. Note that an>0 for all n≥0. If we substitute this into the recurrence, we have
We assume that an≤cn2lnn for some constant c>0 and n>0. If we substitute this into the recurrence, we have
an≤n21+nc1≤k<n∑k2(n−k)lnk.
To bound the sum S=∑1≤k<nk2(n−k)lnk, we split it into two parts: the first half of the range and the second half. For convenience assume that n is even.
To be rigorous, we should check that this form is "stable"; if we assume O(1/n2), we get O(1/n2) back. So, we take that an≤c/n2 for some constant c>0 and n>0. If we substitute this into the recurrence, we have
an≤n21+nc1≤k<n∑k2(n−k)1.
We split the summation again into two parts. When 1≤k≤n/2 then n−k1≤n2, hence
When n/2<k<n then k21≤n24. Thus, S2≤n24∑n−k1=O(n2lnn). Therefore,
an≤n21+nc⋅(O(1/n)+O(n2lnn))=O(1/n2).
Final Result
From an=O(1/n2) it follows that n2an=O(1).
🌟 Exercise 2.50
The recurrence is
an+1=(1+n1)an+(1−n1)an−1 for n>0.
The book states that it applies for n>1, but in that case a2 would be missing, since initial values are a0=0 and a1=1. We can view the recurrence as
Standard Fibonaccian+1−an−an−1=Perturbation Termn1(an−an−1).
The LHS essentially produces values of the standard Fibonacci sequence, since an+1≈an+an−1 if we ignore perturbations. Thus, the solution to a simpler recurrence bn+1=bn+bn−1 for n>0 with b0=0 and b1=1 is bn=Fn=Θ(ϕn), where ϕ is the golden ratio.
Following the steps from the book, we need to compare the two recurrences by forming the ratio
ρn=bnan=Θ(ϕn)an.
If we plug Θ(ϕn)ρn into the original recurrence, we won’t be able to bound ρn. The perturbation term is approximately
n1(ϕn−ϕn−1)≈n1ϕn(ϕ−1),
and the Θ(ϕn) growth rate itself is not stable against the 1/n perturbations in the original recurrence. In our case, this instability means the solution must grow faster. Therefore, we should add an extra nα multiplier and find the value of α to achieve ρn=O(1). Let’s use the "error" ratio
ρn∼nαϕnan for n>1,
with ρ1=1/ϕ. We have
(n+1)αϕn+1ρn+1ϕ2nα(n+1)αρn+1ϕ2(1+n1)αρn+1ϕ2(1+n1)αρn+1ϕ2(1+nα)ρn+1(ϕ2+nαϕ2)ρn+1(ϕ2+nαϕ2)ρn+1(ϕ2+nαϕ2)ρn+1=(1+n1)nαϕnρn+(1−n1)(n−1)αϕn−1ρn−1=ϕ(1+n1)ρn+(1−n1)nα(n−1)αρn−1=ϕ(1+n1)ρn+(1−n1)(1−n1)αρn−1≤[ϕ(1+n1)+(1−n1)(1−n1)α]ρn≤[ϕ(1+n1)+(1−n1)(1−nα)]ρn≤[ϕ+nϕ+(1−nα−n1+O(n21))]ρn≤[(ϕ+1)+nϕ−1−α]ρn≤[ϕ2+nϕ−1−α]ρn.(assume ρn is non-decreasing)(Taylor expansion (1+x)α≈1+αx)(ignore O(1/n2)
To solve for α we can set αϕ2=ϕ−1−α, which implies that
α=ϕ2+1ϕ−1.
Thus, the asymptotic growth of the solution is an=O(nαϕn).
Exercise 2.51
The coefficient n on an−1 suggests an=ω(n!). Let’s employ a change of variable bn=an/n! to find a solution to a simpler recurrence
As n→∞, n−11→0. Therefore, applying the perturbation method again, we end up with another simpler recurrence cn=cn−1+cn−2 for n>1 with c0=0 and c1=1. Clearly, cn=Fn∼ϕn, where ϕ is the golden ratio.
To account for the perturbation factor, we reuse the approach from Exercise 2.50. Let’s use the "error" ratio
ρn∼nαϕnbn for n>1,
with ρ1=1/ϕ. We have
nαϕnρnϕ2ρnϕ2ρnϕ2ρn(1−n1)ϕ2ρn(ϕ2−nϕ2)ρn(ϕ2−nϕ2)ρn(ϕ2−nϕ2)ρn(ϕ2−nϕ2)ρn=(n−1)αϕn−1ρn−1+n−1n(n−2)αϕn−2ρn−2=(1−n1)αϕρn−1+n−1n(1−n2)αρn−2=(1−nα)ϕρn−1+n−1n(1−n2α)ρn−2≤ρn−1[(1−nα)ϕ+n−1n(1−n2α)]≤ρn−1[(1−n1)(1−nα)ϕ+(1−n2α)]≤ρn−1[ϕ(1−n1−nα+O(n21))+(1−n2α)]≤ρn−1[ϕ(1−n1−nα)+(1−n2α)]≤ρn−1[ϕ+1−n1(ϕ+ϕα+2α)]≤ρn−1[ϕ2−n1(ϕ+ϕα+2α)].(Taylor expansion (1+x)α≈1+αx)(assume ρn is non-decreasing)(ignore O(1/n2)
To solve for α we can set ϕ2=ϕ+ϕα+2α, which implies that
ϕ2−ϕ1=α(ϕ+2)=α(ϕ+2)∴α=ϕ+21.
Thus, the asymptotic growth of the solution is an=O(n!nαϕn).
Exercise 2.52
The paper Some Doubly Exponential Sequences by Aho and Sloane presents the solution for a more general recurrence. It is also instructive to read about how variants of the generic form appear in practical problems.
The recurrence from the book, where gn=1, generates a sequence A003095 from OEIS shifted by one (the first element should be skipped, since in our case a0=1). Using the relationship to the constant c=1.225902443528748…, our α corresponds to c2 (due to the previously mentioned index shift). The digits of α are listed in A077496 from OEIS.
Exercise 2.53
This is a small variation of Exercise 2.50; the steps are exactly the same. The result is α=−ϕ2+1ϕ2.
The perturbation factor is (1−1/n)<1. This means at every step, the sequence is being reduced slightly compared to the standard Fibonacci sequence. Therefore, the polynomial correction must be decay, so α must be negative.
Exercise 2.54
In this case, we always choose the smaller subinterval of size ⌊(N−1)/2⌋ (see the proof of Theorem 2.3 in the book). Therefore, the recurrence is
BN=B⌊(N−1)/2⌋+1 for N>1 with B1=1.
After one iteration, we get BN=B⌊(N−3)/4⌋+2 leveraging a useful property of floor and ceiling functions that ⌊⌊x⌋/2⌋=⌊x/2⌋. At the end, we must have
2kN−(2k−1)≥1⟹k=⌊lg(N+1)⌋−1.
The number of comparisons is BN=k+1=⌊lg(N+1)⌋ for N>1.
Exercise 2.55
We always choose the largest subinterval of size ⌈N/3⌉. Therefore, the recurrence is
BN=B⌈N/3⌉+2 for N>1 with B1=1 and B0=0.
Similarly, as in Exercise 2.54, we can characterize the number of comparisons as BN∼2log3N for N>1. If we convert the formula of ternary search to use logarithm of base two, then we can see that the leading term is 2/log23≈1.26>1. Ternary search is less efficient than binary search in terms of comparisons. Even though ternary search reduces the problem size faster (dividing by 3 instead of 2), the "cost" of doing so (2 comparisons instead of 1) is too high to be worth it.
🌟 Exercise 2.56
The solution is part of the course material about recurrences (slides 36-37) by RS. One reservation is that zero is thought to have no bits, a point that could be argued.
Exercise 2.57
Exercise 1.5 contains an inductive proof for the RHS of an identity mentioned in this exercise. The LHS is the Theorem 2.4 from the book. Both are closed-form solutions to the same recurrence, so they must resolve to the same value for all N>0.
Exercise 2.58
All subproblems below build upon the combinatorial correspondence demonstrated in Exercise 2.56.
{# 1 bits in numbers less than N} – (NlgN)/2
This function is specified as TN=PN–(NlgN)/2, where
PN=P⌈N/2⌉+P⌊N/2⌋+⌊N/2⌋ for N>1 with P1=0 (see the book).
We substitute PN=TN+(NlgN)/2 into the above recurrence relation
ΔN=⌊N/2⌋+21(⌈N/2⌉lg⌈N/2⌉+⌊N/2⌋lg⌊N/2⌋−NlgN)=⌊N/2⌋+21(⌈N/2⌉(lg⌈N/2⌉−lgN)+⌊N/2⌋(lg⌊N/2⌋−lgN))=⌊N/2⌋+21(⌈N/2⌉lg(N⌈N/2⌉)+⌊N/2⌋lg(N⌊N/2⌋))=⎩⎨⎧021(⌈2N⌉lg(N2⌈N/2⌉)+⌊2N⌋lg(N2⌊N/2⌋)−1)if N is evenif N is odd.(−NlgN=−⌈N/2⌉lgN−⌊N/2⌋lgN)
The final simplification follows the ideas from Exercise 1.5 together with an identity lg(M/N)=lg(2⋅M/N)−1.
{# 0 bits in numbers less than N} – (NlgN)/2
Without repeating the previous derivation, we’ll just dump the final results for this and the next subproblem. The starting points is TN=ZN–(NlgN)/2, where
ZN=Z⌈N/2⌉+Z⌊N/2⌋+⌈N/2⌉−1.
TN=T⌈N/2⌉+T⌊N/2⌋+ΔN, where ΔN is
ΔN=⎩⎨⎧−121(⌈2N⌉lg(N2⌈N/2⌉)+⌊2N⌋lg(N2⌊N/2⌋)−1)if N is evenif N is odd.
{# bits in numbers less than N} – NlgN
The starting point is TN=SN–NlgN. The recurrence for SN is from Exercise 2.56.
TN=T⌈N/2⌉+T⌊N/2⌋+ΔN, where ΔN is
ΔN=⎩⎨⎧−1⌈2N⌉lg(N2⌈N/2⌉)+⌊2N⌋lg(N2⌊N/2⌋)if N is evenif N is odd.
Exercise 2.59
We again reuse the combinatorial correspondence demonstrated in Exercise 2.56. The recurrences for RN are (when N>1 with R1=R0=0)
RN=RN−2⌊lgN⌋+R2⌊lgN⌋−1+⌊lgN⌋=R⌊N/2⌋+⌊N/2⌋(considering the leftmost bit)(considering the rightmost bit).
🌟 Exercise 2.60
The Python 3 script below generates the required plot.
from functools import lru_cache
from math import floor, log2
import matplotlib.pyplot as plt
@lru_cache(maxsize=None)
def f(n):
return f(n//2) + f((n + 1)//2) + floor(log2(n)) if n > 1 else 0
def plot(save_path="exercise_2_60.png", label_fontsize=18, tick_fontsize=14, title_fontsize=20):
xs = list(range(1, 513))
ys = [f(n) for n in xs]
# Theoretical comparison curve (scaled to match roughly) 𝛩(N).
zs = [2 * n for n in xs]
plt.figure(figsize=(12, 9))
plt.plot(xs, ys, label="$A_N$", linestyle='-', color='blue', linewidth=1)
plt.plot(xs, zs, label="$\\Theta(N)$", color='red', linestyle='--', alpha=0.3)
plt.grid(True)
plt.legend(fontsize=16)
plt.xlabel(r"$N$", fontsize=label_fontsize)
plt.ylabel(r"$A_N$", fontsize=label_fontsize)
plt.title(r"$A_N = A_{\lfloor N/2 \rfloor} + A_{\lceil N/2 \rceil} + \lfloor \lg N \rfloor$",
fontsize=title_fontsize)
plt.xticks(fontsize=tick_fontsize)
plt.yticks(fontsize=tick_fontsize)
plt.tight_layout()
plt.savefig(save_path, dpi=150)
plt.show()
if __name__ == "__main__":
plot()
Apparently, the function is continuous and periodic. It runs slightly below the line y=2N.
Exercise 2.61
The script from Exercise 2.60 needs to be tweaked a little to produce the desired plot.
This time the function is extremely "spiky." Large jumps occur just after powers of 2.
Exercise 2.62
The following Python 3 script creates the required plot and could be used to test out the variants, as mentioned in the book.
We see a similar figure as in Exercise 2.61. If N isn’t a power of 2, then many recursive calls happen on larger indices due to the usage of the ceil function. These effects accumulate and produce large jumps right after these border values. When only floors are used, jumps happen on transitioning to powers of 2. Balancing floors and ceils in both recurrences completely smooths out the curve.
🌟 Exercise 2.63
The function wildly oscillates and is periodic. Let m=lgN. Values of g(N) for N∈[2m,2m+1) can be generated from previously computed values in the interval [2m−1,2m) by adding 2m depending on parity of N. Therefore, every subsequent period is a scaled version of its predecessor.
Exercise 2.64
We develop two equivalent recurrence relations, although they appear to be completely different. As the book hinted in Chapter 1, it’s often easier to derive the total amount TN and calculate the average as TN/N; we do include zero in the range of numbers less than N. In both cases below, we follow this tactic.
Considering the Leftmost Bit
The constituent parts of the recursive formula for the cumulative number of leading 1s TN in numbers less than N.
Let’s focus on the leftmost column and define m=⌊lgN⌋. There are 2m zeros, so we should add N−2m 1s to our count. This is represented as (1) in the image. There are two major sections demarcated by a horizontal line between the last 0 and first 1. The upper region has m+T2m−1 1s, where (2) denotes those m 1s. The total number of 1s in the bottom section depends whether it contains the row (3). If it doesn’t include (3), then the count is simply zero. Otherwise, it is TN−2m−T2m−1. After combining all these ingredients into a single formula, we get
If N is a power of two, then TN=RN due to symmetry. Otherwise, if we focus on the rightmost bit, we see that the range can be split onto numbers ending with 1 and those ending with 0. Suppose we count how many leading 1s are there in those two subintervals. What we’re only missing is the ⌊lgN⌋ 1s associated with the number 2⌊lgN⌋−1∈[1,N). Thus, we need to add it to the total. Therefore, the recurrence becomes TN=T⌊N/2⌋+T⌈N/2⌉+⌊lgN⌋ for N>1 with T1=0. Notice, that it is exactly the one from Exercise 2.60.
🌟 Exercise 2.65
A bitstring of length N has the form b1b2…bN, where each bi∈0,1. The initial run of 1s ends at the first 0 (or continues to the end if all bits are 1). Let X be a random variable denoting the length of the initial string of 1s in a random bitstring of length N. The probability mass function is defined as
The total number of carries made when a binary counter increments N times, from 0 to N, matches RN, as defined in the book (see also Exercise 2.59).
🌟 Exercise 2.68
In both subproblem, we follow the steps from the proof of Theorem 2.5 outlined in the book.
σ=0
The exact representation of the solution is
a(x)=xγ(1+(βγα)+(βγα)2+…(βγα)t), where t=⌊logβx⌋.
If α<βγ⟹γ>logβα then the sum converges and
a(x)∼xγj≥0∑(βγα)j=c1βγ−αβγxγ.
If α=βγ⟹γ=logβα then each of the terms in the sum is 1 and the solution is simply
a(x)∼xγlogβx∼c21⋅xγlogx.
σ=0
The exact representation of the solution is
a(x)=xγ((lgx)σ+(βγα)(lgβx)σ+(βγα)2(lgβ2x)σ+…(βγα)t(lgβtx)σ), where t=⌊logβx⌋.
Note that
(lgβkx)σ=(lgx−klgβ)σ=(lgx)σ(1−lgxklgβ)σ.
As x→∞⟹lgx→∞, for fixed k
(1−lgxklgβ)σ→1.
Furthermore, because the series converges rapidly, since (α/βγ)<1, the contribution of terms where k is close to t is negligible.
If α<βγ⟹γ>logβα then the sum converges and
a(x)∼xγ(lgx)σj≥0∑(βγα)j=c1βγ−αβγxγ(lgx)σ.
If α=βγ⟹γ=logβα then each of the terms in the sum is 1 and the solution is simply
a(x)∼xγlogβx(lgx)σ∼c21⋅xγ(logx)σ+1.
🌟 Exercise 2.69
The recurrence falls under case 2 of the Master theorem. The exact solution takes the form
aN=Nlog3N+N⋅P(N).
The function P represents the fluctuations caused by the floor function ⌊N/3⌋. We have
P(N)=NaN−log3N for N>0.
The plot of the periodic part shows a fractal image, where jumps occur at values of N=4⋅3k for k>0.
Exercise 2.70
Here is the Python script for trying out different configurations. Currently, it is setup to draw a variant where floors are replaced by ceils.
from functools import lru_cache
from math import log
import matplotlib.pyplot as plt
@lru_cache(maxsize=None)
def a(n):
return 3 * a((n+1)//3) + n if n > 3 else 1
def plot(save_path="exercise_2_70.png", label_fontsize=18, tick_fontsize=14, title_fontsize=20):
xs = list(range(1, 973))
ys = [a(n) / n - log(n, 3) for n in xs]
plt.figure(figsize=(12, 9))
plt.plot(xs, ys, linestyle='-', color='blue', linewidth=1)
plt.grid(True, which='both', linestyle='--', alpha=0.4)
plt.xlabel(r"$N$", fontsize=label_fontsize)
plt.ylabel(r"$\frac{a_N}{N} - \log_3 N$", fontsize=label_fontsize)
plt.title(r"Periodic Part of $a_N = 3a_{\lceil N/3 \rceil} + N$",
fontsize=title_fontsize)
plt.xticks(fontsize=tick_fontsize)
plt.yticks(fontsize=tick_fontsize)
plt.tight_layout()
plt.savefig(save_path, dpi=150)
plt.show()
if __name__ == "__main__":
plot()
This is the plot where 2 floors are replaced by ceils. Apparently, the jumps aren’t so steep as in Exercise 2.69.
Exercise 2.71
The exact solution takes the form
a(x)=2x+α2x/β+α22x/β2+⋯+αt2x/βt=k=0∑tαk2x/βk=2x(1+k=1∑tαk2x⋅βk1−βk), where t=⌊logβx⌋.
Since β>1, we have βk1−βk<0 for k≥1. The 2c⋅x term grows faster than any polynomial αx, hence ax/2c⋅x→0 as x→∞, for any positive constant c.
Therefore, we can conclude that a(x)=Θ(2x).
Exercise 2.72
There are many ways to give an asymptotic solution to the recurrence. One approach is to use the more general Akra-Bazzi method. Another one is to draw a recursion tree and discover patterns. There’s a superb tool called VisuAlgo to see recursive trees in action. It has lots of prebuilt recurrences, but it’s also possible to define a custom recurrence by selecting the Custom Code option from the drop-down list. The following snippet contains the definition for this exercise.
if (n <= 3) /* base case */
return 1;
else /* recursive case */
return f(3*n/4) + f(n/4) + n;
After entering a value for N and pressing the Run button, the tool visualizes the whole tree. In general, the cost per level is N. The largest subproblem size decreases by a factor of 4/3 at each step, so the depth is ∼log4/3N. Therefore, the recurrence satisfies aN=Θ(NlgN).
If we want to be rigorous, then we must prove the hypothesis formulated using a recursion tree, for example, using induction on N.
Exercise 2.73
The same remark applies here as in Exercise 2.72. By drawing a recursion tree, we can see that the depth is ∼lgN and the amount of work at each step drops by a factor of 3/4. In other words, the cost at level k is (3/4)kN. After summing up costs, we can conclude that the recurrence satisfies aN=Θ(N).
🌟 Exercise 2.74
We assume that all index functions are positive. We prove the lower and upper bounds separately.
Suppose an=O(n)⟹an≤c2n−d2 for some positive constants c2 and d2 as well as sufficiently large n. Substituting this hypothesis into our recurrence, we get
an=af(n)+ag(n)+ah(n)+1≤c2(f(n)+g(n)+h(n))−3d2+1=c2n−d2−(2d2−1)≤c2n−d2∴an=O(n)✓(by the inductive hypothesis)(if we make 2d2−1≥0⟹d2≥1/2)
The lower bound is even easier. Suppose an=Ω(n)⟹an≥c1n for some positive constant c1 and sufficiently large n. Substituting this hypothesis into our recurrence, we get
an=af(n)+ag(n)+ah(n)+1≥c1(f(n)+g(n)+h(n))+1=c1n+1≥c1n∴an=Ω(n)✓(by the inductive hypothesis)
Since both bounds are the same, we can conclude that an=Θ(n). Observe that we can choose suitable values for our constants to satisfy the base cases, too. For example, c1=1/t,c2=3/2 and d2=1/2 configuration works.
🌟 Exercise 2.75
The recurrence an is the standard formula for counting the number of nodes (internal nodes + leaves) in a binary tree with a "coarsening effect" (subtrees of size ≤t are counted as 1).
The +1 represents the current node.
af(n) is the number of nodes in the left child's subtree.
ag(n) is the number of nodes in the right child's subtree.
Since f(n)≥1 and g(n)≥1 all non-leaf nodes have exactly two children, which is the definition of a full binary tree. A key property of non-empty full binary trees is that the number of leaf nodes is one more than the number of internal nodes. Consequently, the number of internal nodes is roughly an/2.
We are also given the condition f(n)+g(n)=n−h(n). This equation tells us how the "size" of the problem decreases at each step. At any internal node with size n, the combined size of its children is n−h(n). This means that at every step of the recursion, a size drops by h(n). Since the recursion starts with n, and ends when reaching the leaves, the sum of reductions at every internal node must be approximately equal to the initial size n (minus the small residuals at the leaves). This can be expressed as
Let’s assume that h(n)=O(1)⟹h(n)<=c for some positive constant c and sufficiently large n. Substituting this into the above equation, we get
n≤i=1∑anc=can⟹an/n>0 as n→∞.
We must have h(n)=ω(1). Thus, in the summation, the terms h(sizei) become larger on average as n grows. Roughly speaking n≈an⋅haverage, which implies
nan≈haverage1→0.
So, we just need to ensure that h(n) grows unboundedly (arbitrarily slowly) as n→∞.