[CSN212] Study Group 3
Recursions
Solutions
1 Recursion tree method and substitution method
n
Solve the recursion T (n) = 2T (n/2) + log n .
1. Use the recursion tree method to solve it.
2. Use the substitution method to verify your answer.
n 2.n/2 n 4.n/4
Solution. First level: log n , Second level log(n/2) = log n−1 ,
Third level log(n/4) = log nn−2 , last level n1 .
Plog n 1 Pk
Summing up the levels of the recursion tree produces n i=1 n = Θ(n log log n) because i=1 n1 = log k.
Substitution method: T (n) = O(n log log n). The induction assumption is T (n/2) ≤ c(n/2) log log(n/2).
Substituting to the recurrence:
n
T (n) ≤ cn log log(n/2) +
log n
Now we need to show that there is a constant c such that for all n sufficiently large we have:
n
cn log log(n/2) + ≤ cn log log n
log n
Solving for c:
1
c≥
(log log n − log log(n/2)) log n
It turns out that the denominator approaches log(e) as n approaches infinity, where e is Napier’s constant
2.718... (all logarithms in this solution are assumed to be in base 2). To see why, let’s first rewrite the
denominator into a more manageable form:
log n
log(n)(log log n − log log(n/2)) = log(n) log( )
log n − 1
Substitute log n = x:
x 1 x 1 x−1 1
= x log( ) = log((1 + ) ) = log((1 + ) (1 + ))
x−1 x−1 x−1 x−1
Using the fact that limx→∞ (1 + 1/x)x = e we get that our experssion approaches:
1 1
log(e(1 + )) = log e + log(1 + ) → log e
x−1 x−1
Therefore, selecting any c larger than 1/ log e gives T (n) = O(n log log n). Similarly, any c smaller than
1/ log e gives T (n) = Ω(n log log n). Therefore T (n) = Θ(n log log n).
2 Substitution method with floor funtion
The substitution method for solving T (n) = 2T (n/2) + n is restricted to n being a power of two.
Extend the proof to all values of n using the recurrence T (n) = 2T (⌊n/2⌋) + n.
1. Prove first that T (n) = O(n log n).
2. Then prove that T (n) = Ω(n log n) thus showing that T (n) = Θ(n log n).
Solution. For the upper bound, we guess T (n) ≤ cn log n and make the induction assumption
T (⌊n/2⌋) ≤ c⌊n/2⌋ log⌊n/2⌋ ≤ c(n/2) log(n/2)
The rest of the proof is as in the lecture notes.
For the lower bound, we guess T (n) ≥ cn log n when n ≥ 2 and make the induction assumption using the
hint:
n−1 n n−1
T (⌊n/2⌋) ≥ c⌊n/2⌋ log⌊n/2⌋ ≥ c log = c (log n − 2)
2 4 2
Then we check:
T (n) = 2T (⌊n/2⌋) + n ≥ c(n − 1)(log n − 2) + n = cn log n + (1 − 2c)n − c log n + 2c ≥ cn log n
when 1 − 2c ≥ c, so we choose c = 1/3. The base case: T (2) = 2T (1) + 2 = 4 ≥ (1/3)2 log 2.
3 Master method.
Use the master method to give tight asymptotic bounds for the following recurrences.
1. T (n) = T (n/2) + Θ(1)
2. T (n) = 2T (n/2) + n log n
3. T (n) = 3T (n/2) + n2/3
4. T (n) = 2T (n/4) + n7/3
Solution.
1. a = 1, b = 2, α = log2 1 = 0, f (n) = 1 = nα . Thus the middle case applies and T (n) = Θ(n0 log n) =
Θ(log n).
2. a = 2, b = 2, α = log2 2 = 1, f (n) = n log n. None of the cases applies and the recurrence cannot be
solved using the Master method.
3. a = 3, b = 2, α = log2 3 ≈ 1.585, f (n) = n2/3 . Then f (n) = O(nα−ϵ ) for ϵ = 0.1 and the first case
applies: T (n) = Θ(nα ) = Θ(nlog2 3 ).
4. a = 2, b = 4, α = log4 2 = 1/2, f (n) = n7/3 . Then f (n) = Ω(nα+ϵ ) for ϵ = 0.1 and the third case
applies if the regularity condition is satisfied:
af (n/b) = 2(n/4)7/3 = 21−14/3 n7/3 ≤ cn7/3 = cf (n)
when c ≥ 21−14/3 . Thus T (n) = Θ(n7/3 ).
4 Substitution method, lower order term correction.
Solve the recurrence T (n) = 4T (n/2) + n using the substitution method.
1. Start with the guess T (n) = cn2 . What goes wrong?
2. Try to modify the guess to correct the problem.
Solution. Guess: T (n) = cn2 . Induction assumption: T (n/2) = c(n/2)2 = (c/4)n2 . Check:
T (n) = 4(c/4)n2 + n = cn2 + n ̸≤ cn2 .
The problem is the lower order term +n.
A possible correction is to subtract the lower order term from the guess. The new guess is T (n) = cn2 −dn.
Induction assumption: T (n/2) = (c/4)n2 − (d/2)n. Check:
T (n) = 4 (c/4)n2 − (d/2)n + n = cn2 + (1 − 2d)n = cn2 − dn
when d = 1. To satisfy the base case, we choose c = 2: T (1) = c − d = 1.
5 Change of Variables approach
√
Solve the recurrence T (n) = 2T ( n) + log n. Your solution should be asymptotically tight. Do not worry
about whether values are integral. For the base case, assume T (n) = 0 for n ≤ 2.
√
Hint. Define S(x) = T (2x ). Then express T ( n) as a function of S() to obtain a recurrence for S(x). Solve
that using e.g. Master method. Replace back to get a bound for T (n).
Solution. We can define x = log n, which implies n = 2x . Replacing this into the definition of T we obtain:
√
T (n) = 2T ( n) + log n
T (2x ) = 2T (2x/2 ) + x
S(x) = 2S(x/2) + x
The solution to this recurrence is S(x) = Θ(x log x), therefore T (n) = Θ(log n log log n).