• 沒有找到結果。

The recursion-tree method for solving recurrences

在文檔中 ALGORITHMS INTRODUCTION TO (頁 109-114)

Third Edition

4.4 The recursion-tree method for solving recurrences

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 all 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 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.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 T .n/ D 3T .bn=4c/ C ‚.n2/. 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/ D 3T .n=4/ C cn2, having written out the implied constant coefficient c > 0.

Figure 4.5 shows how we derive the recursion tree for T .n/ D 3T .n=4/ C cn2. 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 .n/, which we expand in part (b) into an equivalent tree representing the recurrence. The cn2term 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.

Figure 4.5 Constructing a recursion tree for the recurrence T .n/ D 3T .n=4/ C cn2. Part (a) shows T .n/, which progressively expands in (b)–(d) to form the recursion tree. The fully expanded tree in part (d) has height log4n (it has log4n C 1 levels).

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 n=4i. Thus, the subproblem size hits n D 1 when n=4i D 1 or, equivalently, when i D log4n.

Thus, the tree has log4n C 1 levels (at depths 0; 1; 2; : : : ; log4n).

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 D 0; 1; 2; : : : ; log4n  1, has a cost of c.n=4i/2. Multiplying, we see that the total cost over all nodes at depth i , for i D 0; 1; 2; : : : ; log4n  1, is 3ic.n=4i/2 D .3=16/icn2. The bottom level, at depth log4n, has 3log4n D nlog43 nodes, each contributing cost T .1/, for a total cost of nlog43T .1/, which is ‚.nlog43/, 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 .n/ D cn2C 3

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/ D O.n2/ for our original recurrence T .n/ D 3T .bn=4c/ C ‚.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 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 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 cor-rect, that is, T .n/ D O.n2/ is an upper bound for the recurrence T .n/ D 3T .bn=4c/ C ‚.n2/. We want to show that T .n/  d n2for some constant d > 0.

Using the same constant c > 0 as before, we have T .n/  3T .bn=4c/ C cn2

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/ D T .n=3/ C T .2n=3/ C O.n/ :

(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 shown in the figure, we get a value of cn for every level.

The longest simple path from the root to a leaf is n ! .2=3/n ! .2=3/2n !

   ! 1. Since .2=3/kn D 1 when k D log3=2n, the height of the tree is log3=2n.

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=2n/ D O.n lg n/. Figure 4.6 shows only the top levels of the recursion tree, however, and not every level in the tree contributes a cost of cn. Consider the cost of the leaves. If this recursion tree were a complete binary tree of height log3=2n, there would be 2log3=2nD nlog3=22 leaves. Since the cost of each leaf is a constant, the total cost of all leaves would then be ‚.nlog3=22/ which, since log3=22 is a constant strictly greater than 1, is !.n lg n/. This recursion tree is not a complete binary tree, however, and so it has fewer than nlog3=22 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 cn 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 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/  d n lg n, where d is a suitable positive constant. We have

T .n/  T .n=3/ C T .2n=3/ C cn

 d.n=3/ lg.n=3/ C d.2n=3/ lg.2n=3/ C cn D .d.n=3/ lg n  d.n=3/ lg 3/

C .d.2n=3/ lg n  d.2n=3/ lg.3=2// C cn D d n lg n  d..n=3/ lg 3 C .2n=3/ lg.3=2// C cn

D d n lg n  d..n=3/ lg 3 C .2n=3/ lg 3  .2n=3/ lg 2/ C cn D d n lg n  d n.lg 3  2=3/ C cn

 d n lg n ;

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

Exercises

4.4-1

Use a recursion tree to determine a good asymptotic upper bound on the recurrence T .n/ D 3T .bn=2c/ C n. Use the substitution method to verify your answer.

4.4-2

Use a recursion tree to determine a good asymptotic upper bound on the recurrence T .n/D T .n=2/ C n2. Use the substitution method to verify your answer.

4.4-3

Use a recursion tree to determine a good asymptotic upper bound on the recurrence T .n/ D 4T .n=2 C 2/ C n. Use the substitution method to verify your answer.

4.4-4

Use a recursion tree to determine a good asymptotic upper bound on the recurrence T .n/ D 2T .n  1/ C 1. Use the substitution method to verify your answer.

4.4-5

Use a recursion tree to determine a good asymptotic upper bound on the recurrence T .n/ D T .n1/CT .n=2/Cn. Use the substitution method to verify your answer.

4.4-6

Argue that the solution to the recurrence T .n/ D T .n=3/CT .2n=3/Ccn, where c is a constant, is .n lg n/ by appealing to a recursion tree.

4.4-7

Draw the recursion tree for T .n/ D 4T .bn=2c/ C cn, where c is a constant, and provide a tight asymptotic bound on its solution. Verify your bound by the substi-tution method.

4.4-8

Use a recursion tree to give an asymptotically tight solution to the recurrence T .n/ D T .n  a/ C T .a/ C cn, where a  1 and c > 0 are constants.

4.4-9

Use a recursion tree to give an asymptotically tight solution to the recurrence T .n/ D T .˛ n/ C T ..1  ˛/n/ C cn, where ˛ is a constant in the range 0 < ˛ < 1 and c > 0 is also a constant.

在文檔中 ALGORITHMS INTRODUCTION TO (頁 109-114)