Recursion Tree Method
Recursion Tree Method
4.4-1
Use a recursion tree to determine a good asymptotic upper bound on the recurrence T (n) = 3T (⌊n/2⌋) + n. Use the substitution method to verify your answer.
The total cost over all nodes at depth i, for i = 0, 1, 2, … , lg n − 1, is 3i (n/2i ) = (3/2)i n.
3 3 2 3 lg n−1
T (n) = n + n + ( ) n + ⋯ + ( ) n + Θ(nlg 3 )
2 2 2
lg n−1
3 i
= ∑ ( ) n + Θ(nlg 3 )
2
i=0
(3/2)lg n − 1
= n + Θ(nlg 3 )
(3/2) − 1
T (n) = 3T (⌊n/2⌋) + n
≤ 3 ⋅ (c(n/2)lg 3 − d(n/2)) + n
= (3/2lg 3 )cnlg 3 − (3d/2)n + n
= cnlg 3 + (1 − 3d/2)n,
4.4-2
Use a recursion tree to determine a good asymptotic upper bound on the recurrence T (n) = T (n/2) + n2 . Use the substitution method to verify your answer.
The total cost over all nodes at depth i, for i = 0, 1, 2, … , lg n − 1, is 1i (n/2i )2 = (1/4)i n2 .
lg n−1
1 i
T (n) = ∑ ( ) n2 + Θ(1)
4
i=0
∞
1 i
< ∑ ( ) n2 + Θ(1)
4
i=0
1
= n2 + Θ(1)
1 − (1/4)
= Θ(n2 ).
T (n) ≤ c(n/2)2 + n2
= cn2 /4 + n2
= (c/4 + 1)n2
≤ cn2 ,
4.4-3
Use a recursion tree to determine a good asymptotic upper bound on the recurrence T (n) = 4T (n/2 + 2) + n. Use the substitution method to verify your answer.
lg n−1
T (n) = ∑ (2i n + 2 ⋅ 4i ) + Θ(n2 )
i=0
lg n−1 lg n−1
= ∑ 2i n + ∑ 2 ⋅ 4i + Θ(n2 )
i=0 i=0
lg n
2 −1 4lg n − 1
= n+2⋅ + Θ(n2 )
2−1 4−1
2
= (2lg n − 1)n + (4lg n − 1) + Θ(n2 )
3
2
= (n − 1)n + (n2 − 1) + Θ(n2 )
3
= Θ(n2 ).
2
= cn − cdn + 8cn + 16c − cdn − 8cd + n
= c(n2 − dn) − (cd − 8c − 1)n − (d − 2) ⋅ 8c
≤ c(n2 − dn),
4.4-4
Use a recursion tree to determine a good asymptotic upper bound on the recurrence T (n) = 2T (n − 1) + 1. Use the substitution method to verify your answer.
The n-th level has 2n leaves each with cost Θ(1), so the total cost of the n-th level is Θ(2n ).
Adding the costs of all the levels of the recursion tree we get the following:
n−1
T (n) = ∑ 2i + Θ(2n )
i=0
2n −
1
= + Θ(2n )
2−1
= 2n − 1 + Θ(2n )
= Θ(2n ).
T (n) ≤ 2(c2n−1 − d) + 1
= c2n − 2d + 1
≤ c2 − d n
4.4-5
Use a recursion tree to determine a good asymptotic upper bound on the recurrence T (n) = T (n − 1) + T (n/2) + n. Use the substitution method to verify your answer.
This is a curious one. The tree makes it look like it is exponential in the worst case. The tree is not full (not a complete binary tree of height n), but it is not polynomial either. It's easy to show O(2n )
and Ω(n2 ).
To justify that this is a pretty tight upper bound, we'll show that we can't have any other choice. If we have that T (n) ≤ cnk , when we substitue into the recurrence, the new coefficient for nk can be
1
as high as c(1 + 2k )
which is bigger than c regardless of how we choose the value c.
≤ c2n − 4n
= O(2n ).
≥ cn2
= Ω(n2 ).
4.4-6
Argue that the solution to the recurrence T (n) = T (n/3) + T (2n/3) + cn, where c is a constant, is Ω(n lg n) by appealing to the recursion tree.
We know that the cost at each level of the tree is cn by examining the tree in figure 4.6. To find a lower bound on the cost of the algorithm, we need a lower bound on the height of the tree.
The shortest simple path from root to leaf is found by following the leftest child at each node. Since we divide by 3 at each step, we see that this path has length log3 n. Therefore, the cost of the
algorithm is
c
cn(log3 n + 1) ≥ cn log3 n = n log n = Ω(n log n).
log3
4.4-7
Draw the recursion tree for T (n) = 4T (⌊n/2⌋) + cn, where c is a constant, and provide a tight asymptotic bound on its solution. Verify your answer with the substitution method.
The total cost over all nodes at depth i, for i = 0, 1, 2, … , lg n − 1, is 4i (cn/2i ) = 2i cn.
lg n−1
T (n) = ∑ 2i cn + Θ(n2 )
i=0
−12lg n
cn + Θ(n2 )
=
2−1
= Θ(n2 ).
4.4-8
Use a recursion tree to give an asymptotically tight solution to the recurrence T (n) = T (n − a) + T (a) + cn, where a ≥ 1 and c > 0 are constants.
The total cost over all nodes at depth i, for i = 0, 1, 2, … , n/a − 1, is c(n − ia).
n/a
T (n) = ∑ c(n − ia) + (n/a)ca
i=0
n/a n/a
= ∑ cn − ∑ cia + (n/a)ca
i=0 i=0
2
= cn /a − Θ(n) + Θ(n)
= Θ(n2 ).
≤ cn2
= Θ(n2 ).
≥ cn2
= Θ(n2 ).
4.4-9
Use a recursion tree to give an asymptotically tight solution to the recurrence T (n) = T (αn) + T ((1 − α)n) + cn, where α is a constant in the range 0 < α < 1, and c > 0 is also a constant.
We can assume that 0 < α ≤ 1/2, since otherwise we can let β = 1 − α and solve it for β .
Thus, the depth of the tree is log1/α n and each level costs cn. And let's guess that the leaves are Θ(n),
log1/α n
T (n) = ∑ cn + Θ(n)
i=0
= cn log1/α n + Θ(n)
= Θ(n lg n).
To prove the upper bound, we guess that T (n) ≤ dn lg n for a constant d > 0,
−c
⟹ d≥ ,
α lg α + (1 − α) lg(1 − α)
To prove the lower bound, we guess that T (n) ≥ dn lg n for a constant d > 0,
T (n) = T (αn) + T ((1 − α)n) + cn
≥ dαn lg(αn) + d(1 − α)n lg((1 − α)n) + cn
= dαn lg α + dαn lg n + d(1 − α)n lg(1 − α) + d(1 − α)n lg n + cn
−c
where the last step holds when 0 < d ≤ α lg α+(1−α) lg(1−α) .
−c
⟹ 0<d≤ ,
α lg α + (1 − α) lg(1 − α)