0 ratings0% found this document useful (0 votes) 247 views6 pagesRecurrence Tree
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content,
claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
88
Chaprer 4 Divide-and-Conguer
439
Solve the recurrence T(n) = 31) + logn by making a change of variables,
Your solution should be asymptotically tight. Do not worry about whether values
ave integral
method for
Although you can use the substitution method to provide a succinct proof that
a solution to a recurrence is correct, you might have trouble coming up with a
good guess. Drawing out a recursion tree, as we did in our analysis of the merge
sort recurrence in Section 2.3.2, serves as a straightforward way to devise a good
guess. In a recursion tree, each node represents the cost of a single subproblem
somewhere in the set of recursive function invocations. We sum the costs within
each level of the tree to obtain a set of per-level costs, and then we sum alll the
‘per-level costs to determine the total cost of all levels of the recursion.
A recursion tree is best used to generate a good guess, which you can then verify
by the substitution method. When using a recursion tree to generate a good guess,
you can ofien tolerate a small amount of “sloppiness,” since you will be verifying
your guess later on. If you are very careful when drawing out a recursion tree and
summing the costs, however, you can use a recursion tree as a direct proof of a
solution to a recurrence. In this section, we will use recursion trees to generate
good guesses, and in Section 4.6, we will use recursion trees directly to prove the
theorem that forms the basis of the master method.
For example, let us see how a recursion tree would provide a good guess for
the recurrence Tn) = 37(|n/4|) | @(a2). We start by focusing on finding an
upper bound for the solution. Because we know that floors and ceilings usually do
not matter when solving recurrences (here’s an example of sloppiness that we can
tolerate), We create a recursion tree for the recurrence T(n) = 31 (n/4) | en,
having written out the implied constant coefficient ¢ > 0.
Figure 4.5 shows how we derive the recursion tree for T(n) = 37(n/4) ~ en.
For convenience, we assume that n is an exact power of 4 (another example of
tolerable sloppiness) so that all subproblem sizes are integers. Part (a) of the figure
shows T(mn), which we expand in part (b) into an equivalent tree representing the
recurrence. The cn? term at the root represents the cost at the top level of recursion,
and the three subtrees of the root represent the costs incurred by the subproblems
of size n/4, Part (c) shows this process carried one step further by expanding each
node with cost T(n/4) from part (b). The cost for each of the three children of the
root is c(n/4)*. We continue expanding each node in the tree by breaking it into
its constituent parts as determined by the recurrence.44 The recursion-iree method for solving recurrences 9
Tin) en
/\\
Tie) THE) TR)
cay cay
J... LS
T(z) Te) Tae) Th
©) Tie Tie)
@ © ©
cay
en?
Ta) TA) TU) TE) TO) TA) TA) TAY TU) TU) TAY TA) TA) O(n)
@ Toul: O10")
Figure 4.5 Constructing a recursion tree for the recurrence T(n) = 3T(n/4) = en®. Part (a)
shows 7 (), which progressively expands in (b}~14) to form the recursion tee. The fully expanded
tree in part () has height logy m (it has logy n ~ 1 levels.90
Chaprer 4 Divide-and-Conguer
Because subproblem sizes decrease by a factor of 4 each time we go down one
level, we eventually must reach a boundary condition. How far from the root do
we reach one? The subproblem size for a node at depth i is m/4'. Thus, the
‘subproblem size hits m = I when n/4! = 1 or, equivalently, when i = loggn.
Thus, the tree has log, 1" + I levels (at depths 0, 1,2,... .log,m).
Next we determine the cost at each level of the tree. Each level has three times,
‘more nodes thun the evel above, and so dhe numer of nodes at depth 7 is 3
se subproblem sizes reduce by a factor of 4 for each level we go down
from the root, each node at depth i, for? = 0,1,2,..2,logym — I, has a cost
‘of e(n/4')®, Multiplying, we see that the fotal cost over all nodes at depth i, for
i = 0,1.2,...,logen — 1, is 8e(n/4')? = (B/16)'en. The bottom level, at
depth log, n, has 3°" — n> nodes, each contributing cost T(1), for a'total
cost of n=" 7 (1), which is @(n"*"), since we assume that T(1) is a constant.
Now we add up the costs over all levels to determine the cost for the entire tree:
T 2 3 on (ZY ent 3 eyghee®
0) = or a eo (B) em anne (Bf a 0m
=F Ayer em
7 On) (by caution
Gem
“ay—T*
This lst formula looks somewhat messy until we realize that
advantage of small amounts of sloppiness and use an infinite decreasing geometric
ies as an upper bound. Backing up one step and applying equation (A.6), we
have
‘an again take
3)
Ta = (=) en? + O(n'=3)
3 (2) en? Ines3
< Lz) en + (nh)
- 1 une
= oe on)
16 2 nie?
= Fer oui)
= O(n)
‘Thus, we have derived a guess of T(n) = O(n?) for our original recurrence
T(n) = 31 (|n/4)) + O(n). In this example, the coefficients of en? form a
decreasing geometric series and, by equation (A.6), the sum of these coefficients4A The recursion-tree method for solving recurrences or
oN :
ea) oF
/ \ / \
ee) Ge
Total: O(n
Figure 4.6 A recursion tee forthe recurrence Tan) = T(n/3) + TQn/3) en.
is bounded from above by the constant 16/13. Since the root's contribution to the
total cost is cn?, the root contributes a constant fraction of the total cost. In other
words, the cost of the root dominates the total cost of the tree.
In fact, if O(n2) is indeed an upper bound for the recurrence (as we shall verify in
momen, then it must be a tight bound. Why? The first I contributes
a cost of @(n7), and so ©)(7;2) must be a lower bound for the recurrence.
Now we ean use the substitution method to verify that our guess was cor
‘rect, that is, T(m) = O(n?) is an upper bound for the recurrence T(n) =
“1 (0). We want to show that T(n) = dn? for some constant d > 0,
Using the same constant ¢ > 0 as before, we have
Tin) = 37(\n/4)) +en?
3d [n/4)? + en?
= 3d(n/4)? + en?
3
= Sdn? en
16
= dn?,
where the last step holds as long as d > (16/13)c
In another, more intricate, example, Figure 4.6 shows the recursion tree for
T(n) =T(n/3) + TQn/3) ~ O(n)
(Again, we omit floor and ceiling functions for simplicity.) As before, we let ¢
represent the constant factor in the O(7) term. When we add the values across the
levels of the recursion tree shown in the figure, we get a value of en for every level.2
Chaprer 4 Divide-and-Conguer
The longest simple path from the root to a leaf ism + (2/3) > (2/3)'m
» 1. Since (2/3)kn = 1 when k = logsy2., the height of the tree is logs,
Intuitively, we expect the solution to the recurrence to be at most the number
of levels times the cost of each level, or O(cn logan) = O(n ign). Figure 4.6
shows only the top levels of the recursion tree, however, and not every level in the
tree contributes a cost of ext. Consider the cost of the leaves. If this recursion tree
were a complete binary tree of height logaj27, there would be 2659/2" = n'e/2?
leaves. Since the cost of each leaf is a constant, the total cost of all leaves would
then be @(n'*327) which, since log3/22 is a constant strictly greater than 1,
is w(n Ign). This recursion tree is not a complete binary tree, however, and so
it has fewer than n=? leaves. Moreover, as we go down from the root, more
and more internal nodes are absent. Consequently, levels toward the bottom of the
recursion tree contribute less than cv to the total cost. We could work out an accu
rate accounting of all costs, but remember that we are just trying to come up with a
guess to use in the substitution method. Let us tolerate the sloppiness and attempt
to show that a guess of O(n Ig) for the upper bound is correct.
Indeed, we can use the substitution method to verify that O(r Ig.) is an upper
bound for the solution to the recurrence. We show that T(n) < dn ign, where d is
a suitable positive constant. We have
Tin) = T(n/3) 4 TQn/3) -en
= d(n/3)1g(x/3) + d2n/3) 1g(2n/3) + en
= (d(n/3)lgn —d(n/3) 1g 3)
+ (dQn/3) 1g — d(2n/3)1g(3/2)) 4 en
= dnign —d((n/3)1g3 + 2n/3)1g(3/2)) ~ en
dn ign —d((n/3)1g3 | (2n/3)1g3 —2n/3)1g2) en
= dnign—dn(lg3—2/3) +n
dnign,
aslongas d »
accounting o!
¢/(lg3—(2/3)). Thus, we did not need to perform a more accurate
ts in the recursion tree.
Exercises.
44-1
Use a recursion tree to determine a good asymptotic upper bound on the recurrence
T(n) = 3T (\n/2|) 4 n. Use the substitution method to verify your answer,
442
Use a recursion tree to determine a good asymptotic upper bound on the recurrence
T(n) = T(n/2) +12. Use the substitution method to verily your answer.45° The master method for solving recurrences 93
43
Use a recursion tree to determine a good asymptotic upper bound on the recurrence
T(n) =4T(n/2 4 2) 4 n. Use the substitution method to verify your answer.
444
Use a recursion tree to determine a good asymptotic upper bound on the recurrence
T(n) = 21 (n = 1) + L. Use the substitution method to verify your answer.
Use a recursion tree to determine a good asymptotic upper bound on the recurrence
T(n) = T(n=1) +7 (n/2)-+n. Use the substitution method to verify your answer.
46
Argue that the solution to the recurrence T(n) = T(n/3) +T(Qn/3)-+en, where c
is a constant, is (0 lg) by appealing to a recursion tree.
447
Draw the recursion tree for T(n) = 47(|n/2|) + en, where ¢ is a constant, and
provide a tight asymptotic bound on its solution. Verify your bound by the substi-
tution method.
448
Use a recursion tree to give an asymptotically tight solution to the recurrence
T(n) =T(n —a) + T(a) + en, where a > 1 and ¢ > 0 are constants
449
Use a recursion tree to give an asymptotically tight solution to the recurrence
T(n) = Tan) ~T (0 —ain)-+ en, where «isa constant in the range () = @ = |
and ¢ > Os also a constant.
4.5. The master method for solving recurrences
‘The master method provides a “cookbook” method for solving recurrences of the
form
T(n) =aT(n/b) + fin). (4.20)
where @ > 1 and b > 1 are constants and f(n) is an asymptotically positive
function. To use the master method, you will need to memorize th . but
then you will be able to solve many recurrences quite easily, often without pencil
and paper.