4.
4 The recursion-tree method for solving recurrences
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 subproblem size for a node at depth i is n/2i .
Thus, the tree has lg n + 1 levels and 3lg n = nlg 3 leaves.
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
= 2[(3/2)lg n − 1]n + Θ(nlg 3 )
= 2[nlg(3/2) − 1]n + Θ(nlg 3 )
= 2[nlg 3−lg 2 − 1]n + Θ(nlg 3 )
= 2[nlg 3−1+1 − n] + Θ(nlg 3 )
= O(nlg 3 ).
We guess T (n) ≤ cnlg 3 − dn,
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,
where the last step holds for d ≥ 2.
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 subproblem size for a node at depth i is n/2i .
Thus, the tree has lg n + 1 levels and 1lg n = 1 leaf.
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 ).
We guess T (n) ≤ cn2 ,
T (n) ≤ c(n/2)2 + n2
= cn2 /4 + n2
= (c/4 + 1)n2
≤ cn2 ,
where the last step holds for c ≥ 4/3.
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.
The subproblem size for a node at depth i is n/2i .
Thus, the tree has lg n + 1 levels and 4lg n = n2 leaves.
The total cost over all nodes at depth i, for i = 0, 1, 2, … , lg n − 1, is 4i (n/2i + 2) = 2i n + 2 ⋅ 4i .
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 ).
We guess T (n) ≤ c(n2 − dn),
T (n) = 4T (n/2 + 2) + n
≤ 4c[(n/2 + 2)2 − d(n/2 + 2)] + n
= 4c(n2 /4 + 2n + 4 − dn/2 − 2d) + n
= cn2 + 8cn + 16c − 2cdn − 8cd + n
2
= cn − cdn + 8cn + 16c − cdn − 8cd + n
= c(n2 − dn) − (cd − 8c − 1)n − (d − 2) ⋅ 8c
≤ c(n2 − dn),
where the last step holds for cd − 8c − 1 ≥ 0.
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 subproblem size for a node at depth i is n − i.
Thus, the tree has n + 1 levels (i = 0, 1, 2, … , n) and 2n leaves.
The total cost over all nodes at depth i, for i = 0, 1, 2, … , n − 1, is 2i .
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 ).
We guess T (n) ≤ c2n − d,
T (n) ≤ 2(c2n−1 − d) + 1
= c2n − 2d + 1
≤ c2 − d n
Where the last step holds for d ≥ 1. Thus T (n) = O(2n ).
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.
We guess T (n) ≤ c2n − 4n,
T (n) ≤ c2n−1 − 4(n − 1) + c2n/2 − 4n/2 + n
= c(2n−1 + 2n/2 ) − 5n + 4 (n ≥ 1/4)
n−1 n/2
≤ c(2 +2 ) − 4n (n ≥ 2)
= c(2n−1 + 2n−1 ) − 4n
≤ c2n − 4n
= O(2n ).
We guess T (n) ≥ cn2 ,
T (n) ≥ c(n − 1)2 + c(n/2)2 + n
= cn2 − 2cn + c + cn2 /4 + n
= (5/4)cn2 + (1 − 2c)n + c
≥ cn2 + (1 − 2c)n + c (c ≤ 1/2)
≥ 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 subproblem size for a node at depth i is n/2i .
Thus, the tree has lg n + 1 levels and 4lg n = nlg 4 = n2 leaves.
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 ).
For O(n2 ), we guess T (n) ≤ dn2 − cn,
T (n) ≤ 4d(n/2)2 − 4c(n/2) + cn
= dn2 − cn.
For Ω(n2 ), we guess T (n) ≥ dn2 − cn,
T (n) ≥ 4d(n/2)2 − 4c(n/2) + cn
= dn2 − cn.
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 tree has n/a + 1 levels.
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 ).
For O(n2 ), we guess T (n) ≤ cn2 ,
T (n) ≤ c(n − a)2 + ca + cn
≤ cn2 − 2can + ca + cn
≤ cn2 − c(2an − a − n) (a > 1/2, n > 2a)
≤ cn2 − cn
≤ cn2
= Θ(n2 ).
For Ω(n2 ), we guess T (n) ≥ cn2 ,
T (n) ≥ c(n − a)2 + ca + cn
≥ cn2 − 2acn + ca + cn
≥ cn2 − c(2an − a − n) (a < 1/2, n > 2a)
≥ cn2 + cn
≥ 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).
We can also show T (n) = Θ(n lg n) by substitution.
To prove the upper 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
= dn lg n + dn(α lg α + (1 − α) lg(1 − α)) + cn
≤ dn lg n,
where the last step holds when d ≥ −c .
α lg α+(1−α) lg(1−α)
We can achieve this result by solving the inequality
dn lg n + dn(α lg α + (1 − α) lg(1 − α)) + cn ≤ dn lg n
⟹ dn(α lg α + (1 − α) lg(1 − α)) + cn ≤ 0
⟹ d(α lg α + (1 − α) lg(1 − α)) ≤ −c
−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
= dn lg n + dn(α lg α + (1 − α) lg(1 − α)) + cn
≥ dn lg n,
−c
where the last step holds when 0 < d ≤ α lg α+(1−α) lg(1−α) .
We can achieve this result by solving the inequality
dn lg n + dn(α lg α + (1 − α) lg(1 − α)) + cn ≥ dn lg n
⟹ dn(α lg α + (1 − α) lg(1 − α)) + cn ≥ 0
⟹ d(α lg α + (1 − α) lg(1 − α)) ≥ −c
−c
⟹ 0<d≤ ,
α lg α + (1 − α) lg(1 − α)
Therefore, T (n) = Θ(n lg n).