Previous Section
 < Day Day Up > 
Next Section


4.2 The recursion-tree method

Although the substitution method can provide a succinct proof that a solution to a recurrence is correct, it is sometimes difficult to come 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, is 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 all the per-level costs to determine the total cost of all levels of the recursion. Recursion trees are particularly useful when the recurrence describes the running time of a divide-and-conquer algorithm.

A recursion tree is best used to generate a good guess, which is then verified by the substitution method. When using a recursion tree to generate a good guess, you can often 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.4, 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 T (n) = 3T (n/4) + Θ(n2). We start by focusing on finding an upper bound for the solution. Because we know that floors and ceilings are usually insubstantial in solving recurrences (here's an example of sloppiness that we can tolerate), we create a recursion tree for the recurrence T (n) = 3T(n/4) + cn2, having written out the implied constant coefficient c > 0.

Figure 4.1 shows the derivation of the recursion tree for T (n) = 3T (n/4) + cn2. For convenience, we assume that n is an exact power of 4 (another example of tolerable sloppiness). Part (a) of the figure shows T (n), which is expanded in part (b) into an equivalent tree representing the recurrence. The cn2 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)2. We continue expanding each node in the tree by breaking it into its constituent parts as determined by the recurrence.

Click To expand
Figure 4.1: The construction of a recursion tree for the recurrence T(n) = 3T(n/4) + cn2. Part (a) shows T(n), which is progressively expanded in (b)-(d) to form the recursion tree. The fully expanded tree in part (d) has height log4 n (it has log4 n + 1 levels).

Because subproblem sizes decrease as we get further from the root, 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 n/4i. Thus, the subproblem size hits n = 1 when n/4i = 1 or, equivalently, when i = log4 n. Thus, the tree has log 4n + 1 levels (0, 1, 2,..., log4 n).

Next we determine the cost at each level of the tree. Each level has three times more nodes than the level above, and so the number of nodes at depth i is 3i. Because subproblem sizes reduce by a factor of 4 for each level we go down from the root, each node at depth i, for i = 0, 1, 2,..., log4 n - 1, has a cost of c(n/4i)2. Multiplying, we see that the total cost over all nodes at depth i, for i = 0, 1, 2,..., log4 n - 1, is 3i c(n/4i)2 = (3/16)i cn2. The last level, at depth log4 n, has nodes, each contributing cost T (1), for a total cost of , which is .

Now we add up the costs over all levels to determine the cost for the entire tree:

Click To expand

This last formula looks somewhat messy until we realize that we can again take advantage of small amounts of sloppiness and use an infinite decreasing geometric series as an upper bound. Backing up one step and applying equation (A.6), we have

Thus, we have derived a guess of T (n) = O(n2) for our original recurrence T (n) = 3T (n/4) + Θ(n2). In this example, the coefficients of cn2 form a decreasing geometric series and, by equation (A.6), the sum of these coefficients is bounded from above by the constant 16/13. Since the root's contribution to the total cost is cn2, the root contributes a constant fraction of the total cost. In other words, the total cost of the tree is dominated by the cost of the root.

In fact, if O(n2) is indeed an upper bound for the recurrence (as we shall verify in a moment), then it must be a tight bound. Why? The first recursive call contributes a cost of Θ(n2), and so (n2) must be a lower bound for the recurrence.

Now we can use the substitution method to verify that our guess was correct, that is, T (n) = O(n2) is an upper bound for the recurrence T (n) = 3T (n/4)+Θ(n2). We want to show that T (n) dn2 for some constant d > 0. Using the same constant c > 0 as before, we have

T(n)

3T(n/4) + cn2

 

3dn/42 + cn2

 

3d(n/4)2 + cn2

 

=

3/16 dn2 + cn2

 

dn2,

where the last step holds as long as d (16/13)c.

As another, more intricate example, Figure 4.2 shows the recursion tree for T (n) = T(n/3) + T(2n/3) + O(n).


Figure 4.2: A recursion tree for the recurrence T(n) = T (n/3) + T (2n/3) + cn.

(Again, we omit floor and ceiling functions for simplicity.) As before, we let c represent the constant factor in the O(n) term. When we add the values across the levels of the recursion tree, we get a value of cn for every level. The longest path from the root to a leaf is n (2/3)n (2/3)2n ··· 1. Since (2/3)kn = 1 when k = log3/2 n, the height of the tree is log3/2 n.

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 log3/2 n) = O(n lg n). The total cost is evenly distributed throughout the levels of the recursion tree. There is a complication here: we have yet to consider the cost of the leaves. If this recursion tree were a complete binary tree of height log3/2 n, there would be leaves. Since the cost of each leaf is a constant, the total cost of all leaves would then be , which is ω(n lg n). This recursion tree is not a complete binary tree, however, and so it has fewer than leaves. Moreover, as we go down from the root, more and more internal nodes are absent. Consequently, not all levels contribute a cost of exactly cn; levels toward the bottom contribute less. We could work out an accurate 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 lg n) for the upper bound is correct.

Indeed, we can use the substitution method to verify that O(n lg n) is an upper bound for the solution to the recurrence. We show that T (n) dn lg n, where d is a suitable positive constant. We have

T(n)

T(n/3) + T(2n/3) + cn

 

d(n/3)lg(n/3) + d(2n/3)lg(2n/3) + cn

 

=

(d(n/3)lgn - d(n/3)lg 3) + (d(2n/3) lg n - d(2n/3)lg(3/2)) + cn

 

=

dn lg n - d((n/3) lg 3 + (2n/3) lg(3/2)) + cn

 

=

dn lg n - d((n/3) lg 3 + (2n/3) lg 3 - (2n/3)lg 2) + cn

 

=

dn lg n - dn(lg 3 - 2/3) + cn

 

dn lg n,

as long as d c/(lg 3 - (2/3)). Thus, we did not have to perform a more accurate accounting of costs in the recursion tree.

Exercises 4.2-1
Start example

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.

End example
Exercises 4.2-2
Start example

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 a recursion tree.

End example
Exercises 4.2-3
Start example

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 bound by the substitution method.

End example
Exercises 4.2-4
Start example

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.

End example
Exercises 4.2-5
Start example

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.

End example


Previous Section
 < Day Day Up > 
Next Section