Chapter 2: Recurrence Relations
Exercise 2.1
def fib_recursive(n):
return fib_recursive(n - 1) + fib_recursive(n - 2) if n > 1 else n
def fib_iterative(n):
a, b = 0, 1
for _ in range(n):
a, b = b, a + b
return aThe recursive version needlessly recomputes the same values and has an exponential running time. The iterative version eliminates this deficiency. The computation remains efficient for F20=6765, even with the recursive implementation; however, as values exceed 30, performance differences become increasingly apparent.
Solve the Fibonacci Number problem on LeetCode.
Exercise 2.2
The total number of arithmetic operations (additions, subtractions and divisions), including loop increments and index calculations, is
Exercise 2.3
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, recursively calculating values via recurrence (3), as mentioned in the book, has unacceptable performance. By instrumenting the code, we get that the number of function calls is 3N. This can be formally proven by induction on N, using the recurrence rN=1+2∑k=0N−1rk (with r0=1) as the inductive hypothesis. Therefore, the number of arithmetic operations is Θ(3N).
Classifying 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
For efficiently calculating the binomial coefficients, the program employs memoization. Here’s the output:
Analysis of Table 2.5
We’d expect that the average number of compares approach the following asymptotic approximations for sufficiently large N:
Standard quicksort (full-key comparisons): ∼2NlnN≈1.386NlgN
Median-of-three quicksort (full-key comparisons): ∼(12/7)NlnN≈1.188NlgN
Radix exchange sort (bit-inspections): ∼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.
an=pUn+qVn, where Un=Fn−1 and Vn=Fn, thus an=pFn−1+qFn for n>1. Un=Fn−1 because it starts as 1,0,1,1,..., hence behaves like a right shifted Fibonacci sequence.
Exercise 2.7
The homogeneous part remains the same as in the previous exercise. 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=1n−1Fk for n>1. Exercise 3.35 shows how to attain a closed-form solution for the sum of Fibonacci numbers.
Exercise 2.8 🌟
This is a generalization of the previous two exercises.
For f linear, let’s 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.
an=a0Un+a1Vn+f(0,0)k=1∑n−1Vkfor n>1.
Exercise 2.9
This immediately reduces to a product
Exercise 2.10
This immediately reduces to a sum
Exercise 2.11 🌟
This exercise reminds us that sometimes there is a remarkably easy solution once we start thinking outside the box
The solution is posted on the book’s website.
Exercise 2.12
Following the hint from the book, we get
Exercise 2.13
Multiply both sides by (n+1) and iterate
Exercise 2.14 🌟
This is a generalization of Theorem 2.1.
Follow virtually the same steps as in the proof of Theorem 2.1 and stop at at
As a sanity check, shift 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).
Exercise 2.15
After dividing both sides by n we’ve
Notice, that the recurrence in section 2.2 for CN isn’t the same as in section 1.5 (the initial conditions are different). Consequently, the solution based on Theorem 2.1 is exactly as given in the book. The reported errata to change -1 to -3/2 is incorrect, since it assumes the recurrence from section 1.5.
Exercise 2.16 🌟
This exercise perfectly illustrates the generalized Theorem 2.1 (see Exercise 2.14).
After dividing both sides by n, we get a first-order linear recurrence
Use the generalized Theorem 2.1 with t=4. The reduced summation factor sj+1=xj+1xj+2…xn equals to
Substitute back sj+1 into the main recurrence
The identity (6.70) comes from the book Concrete Mathematics: A Foundation for Computer Science (2nd ed.).
Exercise 2.17 🌟
This exercise highlights an important detail not explicitly mentioned in the text; the summation factor must be nonzero.
Simplify the expression for AN to get
Before proceeding, we must resolve the base cases for N≤6, ensuring that the summation factor is nonzero. Using the original recurrence for AN we get A2=0,A3=2,A4=1,A5=9/5,A6=2. Following the same steps from the previous exercise (see also Table 2.2 from the book), we get
Exercise 2.18
From the signed error term bn=an−α, we’ve an−1=bn−1+α. Substitute it back into the main recurrence
Therefore,
As n→∞, an→α, thus, bn→0; see the next exercise why this holds when a0 is between 0 and 1. For small bn−1 we’ve
Notice that α=1+α1⟹1+αα=α2. At any rate, ∣bn∣≈0.4∣bn−1∣, which explains why each iteration increases the number of significant digits available by a constant number of digits (about half a digit).
Exercise 2.19 🌟
The solution is known as the Dottie number and revolves around the concept of a fixed point. 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) on this interval and sin(1)<1 (sine is strictly increasing in the interval [0,1], see Exercise 2.22).
We can also represent the continued fraction 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
Suppose, for the sake of contradiction, that an=0.5(an−1−1/an−1) has a real fixed point for n>0 with a0=0. Since, a0 is a real number, then a1=0.5(a0−1/a0) is also a real number. By induction on n, we can conclude that all an are real numbers for n>0. On the other hand, the fixed point −1=i isn’t a real number. This is a contradiction, so {an}n>0 diverges.
The sequence exhibits chaotic behavior. Let’s use a substitution an=cot(θn). Recall the double-angle identity for cotangent:
This exactly matches our recurrence relation. If a0=cot(θ0), then a1=cot(2θ0). Therefore, an=cot(2nθ0) and the sequence will fluctuate chaotically, governed by the doubling of the angle in the cotangent function. It’ll either terminate or not. If an becomes zero, then the next iteration fails due to division by zero. For example, a0=cot(π/4)=1 will terminate the sequence in the second iteration, since a1=0. It turns out that the sequence terminates if and only if θ0 is a dyadic rational multiple of π. That is
where k and m>0 are integers. Both directions are trivial to prove.
Exercise 2.21 🌟
This exercise introduces the Stolz–Cesàro theorem, which is a discrete version of L'Hôpital’s rule.
We prove the lower bound in a similar fashion, as the book does for the upper bound
Both asymptotic bounds match, hence an=Θ(1/n)⟹limn→∞an=0.
After computing initial terms, it seems that an∼c/n, where c=1. Let’s prove that limn→∞nan=limn→∞1/ann=1, which matches the ∞/∞ case of the Stolz–Cesàro theorem. It 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,
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:
Substitute this expansion into the recurrence an=sin(an−1) to get
If we square both sides of the equation, then we can use the approximation (1−x)2∼1−2x for small x. Thus,
Following the hint from the book to consider change of variable bn=1/an , let’s flip the above equation to get
Therefore, bn=O(n)⟹an=O(1/n).
Exercise 2.23
If f′(α)>1 then α is a repelling fixed point. 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.
Since f′(α)>1 the magnitude of the error term grows instead of shrinks. 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 grow again.
Exercise 2.24
The analysis relies on the approximation of the error term bn from the previous exercise and the precondition stated in the book that a0 is sufficiently close to α. The three cases are:
If the fixed point α is attracting then local convergence is guaranteed. 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.
If the fixed point α is super-attracting, we have guaranteed and fast local convergence. 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≈21∣f′′(α)∣⋅bn−12. Each iteration approximately doubles the number of significant digits available.
If the fixed point α is neutral then convergence isn’t guaranteed. The criteria is more complicated, as described in the excerpt about nonhyperbolic fixed points.
If the fixed point α is repelling then convergence isn’t possible.
The lecture notes by Burbulla nicely recaps the basic theory around fixed points with lots of examples. It contains a case study of a unilaterally attracting/repelling neutral fixed point.
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
The term xd is associated with an−d. Therefore, we can immediately construct our recurrence
The initial values are
Exercise 2.26 🌟
This exercise explains how to solve an inhomogeneous recurrence of the form
an = x1an – 1 + x2an – 2 + ... + xtan – t + r for n ≥ t.
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 (annihilator method) for particular solutions of inhomogeneous linear recurrences. The text covers many recurrence types that occur in practical applications.
At any rate, generating functions handle linear recurrences of this form in a unified manner without worrying about guessing a particular solution or doing any additional searches. Chapter 3 of the book is devoted to this topic.
Exercise 2.27
We know that an=c03n+c12n, as stated in the book. Furthermore, we have
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. Irrespectively of boundary conditions, an must satisfy the recurrence, which isn’t the case here.
Exercise 2.28
The exercise from the book mentions 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 recurrence given in the main text.
By tweaking the initial conditions, we can fundamentally alter the growth rate of the solution to
We use the same approach as in the previous exercise. 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:
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:
The constants are c0=0 and c1=1.
If we change the initial conditions, then we get another system of linear equations:
The constants are c0=1 and c1=0.
Exercise 2.31 🌟
This exercise illustrates how to handle complex roots and transform them into trigonometric form.
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 roots 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 reals, too. Consequently, the constants c0 and c1 must be complex conjugates. We also have the following system of linear equations:
The constants are c0′=0 and c1′=2/3. Thus,
This was an amazing journey from a seemingly straightforward second-order linear recurrence to the trigonometric solution via complex algebra.
Exercise 2.32
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
The other two complex roots are z2,3=21±i23. Using the same transformation as in the previous exercise, we can write down the general solution as
We also have the following system of linear equations:
The constants are c0=4/3, c1=−4/3 and c2=2/3.
Exercise 2.33
Let’s split the problem into two subproblems based on parity of n>1
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
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
To find c0 we need to use the exact general solution
We have the following system of linear equations:
Let’s solve for c0 by running the next Sage script:
It outputs
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
On the right we get
Consequently, (α−β)(α−γ)=3α2−2α−1, which gives us the value of c0 in terms of α
Estimating the Accuracy
We can calculate the exact and approximate value of F20(3) using the following Python script.
It outputs 35890 ~ 35889.999620092305. They are indeed very close.
Exercise 2.35
Let’s define a new sequence bn=n!⋅an⟹an=n!bn. If we substitute this back into the main recurrence, we get
The initial conditions are b1=b0=1, thus bn=Fn+1. Therefore,
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).
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
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 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
and an=2bn 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
For a0=3 the solution is
As the book notes, only the larger of the two roots predominates in this expression—the one with the plus sign.
For a0=4 the solution is
Again, only the larger of the two roots predominates in this expression—the one with the plus sign.
For a0=3/2 the solution is fundamentally different. The roots b0 are complex numbers
We can use the same approach as in Exercise 2.31 to transform these into trigonometric form. Let b0=e±iθ, where θ=arccos(3/4), thus the recurrence becomes
The sequence an fluctuates between -2 and 2, never settling on a single value.
Exercise 2.40 🌟
This exercise introduces the bifurcation theory; as demonstrated in the previous exercise, a fundamentally different behavior occurs around the bifurcation point a0=2.
The accurate approximate answer for 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
For the larger root we have
For the smaller root we have
As n increases, (b0−)2n will rapidly vanish to 0. Consequently, we get an≈(1+ϵ)2n. This shows that even for an infinitesimally small ϵ, the sequence an diverges to infinity at a double-exponential rate.
Exercise 2.41 🌟
This exercise shows a mechanism of transforming generic quadratic recurrences into the register allocation type recurrence, whose solution space is known.
Assume that f=0. Otherwise, we can define g=2 and all sequences an would "turn" into bn=bn−12−2 by using the transformation bn=0⋅an+g.
We expand bn to match parameters
Let’s express an in terms of f and g starting with bn=fan+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.
For an=an−12+1 the parameters are α=1, β=0 and γ=1. Clearly
This shows that it doesn’t 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 the previous exercise 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, hence 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.

Exercise 2.43 🌟
This exercise shows a way to express general classes of “continued fraction” representations as solutions to recurrences.
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.
Distinct Roots
The initial value is b0=(a0−r)/(a0−s)=(1−r)/(1−s). If we substitute b0 into the expression for an, assuming s=1, we get
As a quick check, we should get the closed-form solution for the continued fraction example from the book by using α=0,β=1,γ=1 and δ=1. The roots 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.
The book erroneously states that bn=Fn+1 instead of bn=Fn+2. The correct initial conditions are b0=1 and b1=2.
Repeated Roots
When a Möbius transformation has a single, repeated fixed point r=s, it is classified in dynamical systems as a parabolic transformation. We need to use a different change of variable
Without repeating a similar grueling algebra, the outcome is
Flip the substitution back to get the closed form for an.
Exercise 2.44 🌟
This exercise demonstrates the use of substitution to simplify sequences. Calculating values directly is often difficult and can result in accuracy loss over time. By using an integer sequence, exactness is maintained, allowing any value to be expressed as a rational number at each step.
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
The initial conditions must satisfy
We can choose any starting value for b0.
As a quick 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 🌟
This exercise replaces the bad example in the book and demonstrates how a repertoire table streamlines analysis by allowing reuse of many entries.
We are given a relaxed quicksort recurrence
Let’s 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
Therefore, the solution is
Exercise 2.46
The post on StackExchange for Mathematics explains the basics of the repertoire method and illustrates its application by solving this exercise.
Exercise 2.47 🌟
The solution of this problem illustrates the basic form of the bootstrapping method.
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→∞.
The output shows that we’re on the right track.
If we employ bootstrapping and substitute our guess back into the recurrence, we get
The new version of the script reflects this change.
The results are better aligned as n increases.
After an additional iteration we have
We see that the approximation of an is improved again.
This process may continue depending on a desired accuracy level.
There’s no simple closed-form solution. 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
The key is to convert this discrete recurrence into its continuous form, by approximating the sum with a definite integral. Converting a discrete sum into a definite integral is a good choice for expected (average) values. The integral elegantly smooths out local discrete fluctuations, providing a highly accurate asymptotic approximation without the need for cumbersome error tracking. At any rate, the process can be broken down into three major stages.
Converting the Discrete Recurrence
Let’s simplify the constant multiplier of the sum for large enough N
Let’s set the pivot’s 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
Bootstrapping the Solution
Our initial hypothesis is CN∼αNlnN. After all, we expect to have a familiar asymptotic form Θ(NlnN) as other quicksort variants. Let’s substitute this hypothesis into our recurrence relation
Let’s split this integral into the lnN part and the lnx part.
In the second line, we used the identity ∫01xklnxdx=−(k+1)21.
Determining the Value of α
Bootstrapping confirms the statement from the book that
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 must 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 🌟
This exercise showcases an advanced usage of the bootstrapping method.
With bootstrapping we start with a rough bound and then improve it step by step. The altered recurrence is
where the k=0 term is separated out. When the goal is to establish strict upper bounds, converting a sum to an integral introduces unnecessary complications. We would need to carefully cover the error gaps. It’s far more easier to apply a splitting technique, as explained below.
Iteration 1
Let’s start with the rough guess that an=O(1/n) for n>0. Thus, there exists some constant c>0 such that an≤c/n for sufficiently large n. Note that an>0 for all n≥0. If we substitute this into the recurrence, we have
Iteration 2
We assume that an≤cn2lnn for some constant c>0 and n>0. If we substitute this into the recurrence, we have
To upper bound the sum S=∑1≤k<nk2(n−k)lnk, we split it into two parts. The split typically occurs at the midpoint (e.g., n/2). The approach involves independently constraining each region—either by limiting the growth of an expanding component or by anchoring a diminishing component at its maximal value. This method generally yields two independent asymptotic approximations, which are subsequently combined to bound the entire sum. For convenience assume that n is even.
When 1≤k≤n/2 then n−k≥n/2⟹1/(n−k)≤2/n. We have
The infinite series ∑k=1∞k2lnk converges to a constant.
When n/2<k<n then k2lnk attains its maximum value at the start of the range, bounded by ≈(n/2)2ln(n/2)=O(n2logn). Therefore,
Thus,
Iteration 3
To be rigorous, we should check that this form is "stable"; if we assume O(1/n2), we must 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
We split the summation again into two parts. When 1≤k≤n/2 then n−k1≤n2, hence
The sum ∑k=1∞k21=ζ(2), where ζ is the Riemann zeta function.
When n/2<k<n then k21≤n24. Thus, S2≤n24∑n−k1=O(n2logn). Therefore,
Final Result
From an=O(1/n2) it follows that n2an=O(1).
Exercise 2.50 🌟
We can expect many hardships with the perturbation method. This and the next few exercises demonstrate different obstacles that we may encounter. Here, we must tune the initial growth rate after failing to bound the error rate with the initial guess.
Find the asymptotic growth of the solution to the “perturbed” Fibonacci recurrence
Notice that n>0, otherwise a2 would be missing, since initial values are a0=0 and a1=1.
We can view the recurrence as
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
If we plug Θ(ϕn)ρn into the original recurrence, we won’t be able to bound ρn. Consequently, the solution must grow faster. The book contains a hint as part of Exercise 2.53; let’s add an extra nα multiplier and find the value of α such that ρn=O(1). This gives
with ρ1=1/ϕ. We have
To solve for α we can set αϕ2=ϕ−1−α, which implies that
Thus, the asymptotic growth of the solution is an=O(nϕ+2ϕ−1ϕn).
Exercise 2.51 🌟
Often, we have to employ the perturbation method iteratively. This exercise illustrates the process.
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 the previous exercise. This gives
with ρ1=1/ϕ. We have
To solve for α we can set ϕ2=ϕ+ϕα+2α, which implies that
Thus, the asymptotic growth of the solution is an=O(n!nϕ+21ϕn).
Exercise 2.52
The paper Some Doubly Exponential Sequences by Aho and Sloane elaborates the solution for a more general recurrence. It’s 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 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.
Exercise 2.53
This is a small variation of Exercise 2.50 with α=−ϕ2+1ϕ2=−ϕ+2ϕ+1.
The perturbation factor is (1−1/n)<1, hence, the sequence is asymptotically slightly less than the standard Fibonacci sequence. The polynomial correction must be decay, which explains why α is negative. Of course, we could have forgotten about the polynomial factor, since the upper bound is valid even without it. But it’s assumed that the O-notation expresses a tight upper bound.
Exercise 2.54
In the best 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
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
The number of comparisons is BN=n+1=⌊lg(N+1)⌋ for N≥1.
Exercise 2.55
We always choose the largest subinterval of size ⌈N/3⌉. Therefore, the recurrence is
The number of comparisons is 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/lg3≈1.26>1. Ternary search is less efficient than binary search in terms of comparisons. Even though it 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.
Exercise 2.57
The recurrence (4) from the book may be regarded as a function f:N→N, thus cannot have two closed-form solutions that map the same input to different outputs.
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
Figure 2.5 in the book is wrong. PN<0.5NlgN (just by looking entries of Table 2.3), hence fN=O(N) must be negative; the plot of PN−0.5NlgN cannot be at the top. As a matter of fact, the top and bottom plots must be swapped. Furthermore, P84=252 instead of 215. Finally, the cumulated number of zero bits must include zero itself, otherwise we cannot recreate the plots.
{# 1 bits in numbers less than N} – (NlgN)/2
The function is specified as TN=PN–(NlgN)/2, where
{# 0 bits in numbers less than N} – (NlgN)/2
The function is specified as TN=ZN–(NlgN)/2, where
{# bits in numbers less than N} – NlgN
The function is specified as TN=SN–NlgN, where SN is given in Exercise 2.56.
Exercise 2.59
The recurrences for RN are (for N>1 with R1=R0=0)
Exercise 2.60 🌟
This and the next couple of exercises illustrate how seemingly smooth asymptotic bounds don’t reflect what actually happens under the hood, due to usage of floor and ceiling functions (see Table 2.4 in the book).
The Python 3 script below generates the required plot.

Exercise 2.61
The script from the previous exercise needs to be tweaked to produce the desired plot.

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.

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 ceilings in both recurrences completely smooths out the curve.Exercise 2.63

Exercise 2.64
We’ll 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.
Considering the Leftmost Bit
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 in the leftmost column. 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
The base cases are T1=T0=0.
Considering the Rightmost Bit
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’s exactly the one from Exercise 2.60.
For example, when N=2n, the average simplifies to 2−Nn+2, which tends to 2 as N→∞.
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. Its probability mass function is defined as
The event X=k for 0≤k<N occurs if the first k bits are 1 and the (k+1)-th bit is 0. The expected value is
Exercise 2.66
Based on Exercise 2.65, we have that limn→∞μ=E[X]=1.
The variance is given by
We need to compute
Thus, the variance is 3−1=2.
This problem actually maps perfectly to the geometric distribution. If we let Y be the total number of bits we examine until we see the first 0, Y follows a geometric distribution with probability p=1/2. Our random variable would be defined as X=Y−1. At any rate, both the mean and variance are given.
Exercise 2.67
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 subproblems, we follow the steps from the proof of Theorem 2.5 outlined in the book.
For δ=0 the exact representation of the solution is
If α<βγ⟹γ>logβα then the sum converges and
If α=βγ⟹γ=logβα then each of the terms in the sum is 1 and the solution is simply
For δ=0 the exact representation of the solution is
Note that
As x→∞⟹logx→∞, so for fixed k
If α<βγ⟹γ>logβα then the contribution of terms where k is close to t is negligible and the sum converges. We have
If α=βγ⟹γ=logβα then
This sum can be approximated by an integral. Let t be a continuous variable for k and u=logx−tlogβ, so du=−logβdt. This gives
While the book states c1,c2 depend on α,β,γ, the derivation shows c2 also depends on δ due to the integration of the logarithmic term.
Exercise 2.69 🌟
This exercise reminds us that the asymptotic approximations given by the Master theorem differ in nature from exact solutions.
The recurrence falls under case 2 of the Master theorem. The exact solution takes the form
The function P represents the fluctuations caused by the floor function ⌊N/3⌋. We have

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.

Exercise 2.71 🌟
This exercise shows what happens when the function f(x) grows exponentially fast.
The exact solution takes the form
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 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.
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 3/4 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 just formulated, for example, using induction on N.
We can even determine the leading coefficient by substituting aN∼cNlogN into the recurrence (bootstrapping). This gives aN∼log4−43log3NlogN.
Exercise 2.73 🌟
This exercise shows how the interplay between the Master theorem (or Theorem 2.6 with a more specific function) and bootstrapping may reveal the leading coefficient.
The same remark applies here as in the previous exercise. 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).
We can make one step further and discover the leading coefficient. Assuming that aN∼cN, we can substitute aN into the recurrence and find that aN∼4N.
Exercise 2.74
We must prove that the solution to the recurrence
with the constraint that f(n)+g(n)+h(n)=n, is an=Θ(n). We assume that all index functions are positive. We’ll 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
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
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=af(n)+ag(n)+1 for n>t with an=1 for n≤t, where f(n)+g(n)=n−h(n), describes the number of nodes in a recursion tree of a divide-and-conquer algorithm with a "coarsening effect" (subproblems of size ≤t are handled as base cases). We assume that all index functions are positive:
The +1 represents the current node.
The problem is split into two subproblems of size f(n) and g(n).
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" (blocks of size ≤t), the sum of reductions at every internal node must be equal to the initial size n minus the 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
Roughly speaking n≈an⋅haverage, which implies
So, we just need to ensure that h(n)=ω(1) somehow grows unboundedly as n→∞.
Last updated