Chapter 4 - Recursion Tree Document last modified: 01/31/2013 05:24:07 Uses: Use recursion tree to determine a good asymptotic bound on the recurrence T(n) = … Sum the costs within each level of the tree to obtain a set of per-level costs. Sum all the per-level costs to determine the total cost of all levels of recursion. Best if used to generate a good guess for the closed form bounds of the recurrence T(n). Guess is verified by using Substitution Method or Master Method. Note: the bound sought will be one of the following: “asymptotic upper bound” means you’re looking for Big-O “tight asymptotic bound” means you’re looking for Θ “asymptotic lower bound” means you’re looking for Ω Steps: Draw the tree based on the recurrence 1. From the tree determine: # of levels in the tree a. cost per level b. # of nodes in the last level c. cost of the last level (which is based on the number found in 2c) d. 2. Write down the summation using ∑ notation – this summation sums up the cost of all the levels in the recursion tree 3. Recognize the sum or look for a closed form solution for the summation created in 3). Use Appendix A. 4. Apply that closed form solution to your summation coming up with your “guess” in terms of Big-O, or Θ, or Ω (depending on which type of asymptotic bound is being sought). 5. Then use Substitution Method or Master Method to prove that the bound is correct. 6. Example - Show Factorial is O(n) For the recursive n! algorithm: int F(int n) { if (n == 1) return 1; return F (n-1) * n; } n! is defined by the recurrence: F(1) = 1 for n=1 F(n) = F(n-1)*n for n>1 The run time cost, T(n), is defined by the recurrence: T(1) = 1 for n=1 T(n) = T(n-1) + 1 for all n>1 where 1 is the running time for each execution of the factorial function. Backward Substitution method for exact closed-end equation - T(n) = n T(n) = T(n-1) + 1 = [ T(n-2)+1 ] + 1 Substitute T(n-2)+1 for T(n-1) = T(n-2) + 2 = [ T(n-3)+1 ] + 2 Substitute T(n-3)+1 for T(n-2) = T(n-3) + 3 = [ T(n-4)+1 ] + 3 Substitute T(n-4)+1 for T(n-3) = T(n-4) + 4 After i substitutions: T(n) = T(n-i) + T(n-i+1) + ... + 1 + 1 Chapter 4 - Recursion Tree http://homepages.ius.edu/RWISMAN/C455/html/notes/Chapter4/Recursio... 1 of 14 7/2/2013 8:47 PM
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Chapter 4 - Recursion TreeDocument last modified: 01/31/2013 05:24:07
Uses:
Use recursion tree to determine a good asymptotic bound on the recurrence T(n) = …
Sum the costs within each level of the tree to obtain a set of per-level costs.
Sum all the per-level costs to determine the total cost of all levels of recursion.
Best if used to generate a good guess for the closed form bounds of the recurrence T(n).
Guess is verified by using Substitution Method or Master Method.
Note: the bound sought will be one of the following:
“asymptotic upper bound” means you’re looking for Big-O“tight asymptotic bound” means you’re looking for Θ“asymptotic lower bound” means you’re looking for Ω
Steps:
Draw the tree based on the recurrence1.
From the tree determine:
# of levels in the treea.
cost per levelb.
# of nodes in the last levelc.
cost of the last level (which is based on the number found in 2c)d.
2.
Write down the summation using ∑ notation – this summation sums up the cost of all the levels in the recursion tree3.
Recognize the sum or look for a closed form solution for the summation created in 3). Use Appendix A.4.
Apply that closed form solution to your summation coming up with your “guess” in terms of Big-O, or Θ, or Ω (depending on which type ofasymptotic bound is being sought).
5.
Then use Substitution Method or Master Method to prove that the bound is correct.6.
Example - Show Factorial is O(n)
For the recursive n! algorithm:
int F(int n) {
if (n == 1) return 1;
return F (n-1) * n;}
n! is defined by the recurrence:
F(1) = 1 for n=1
F(n) = F(n-1)*n for n>1
The run time cost, T(n), is defined by the recurrence:
T(1) = 1 for n=1
T(n) = T(n-1) + 1 for all n>1
where 1 is the running time for each execution of the factorial function.
Backward Substitution method for exact closed-end equation - T(n) = n
By summing across each level, the recursion tree shows the cost at each level of recursion (minus the costs of recursive calls, whichappear in subtrees):
There are log3n full levels,
after log3/2n levels, the problem size is down to 1;
log3n ≤ log3/2n
Height log3/2n
Recall that dividing n = 2i size problem into 1/2 size problems has a tree height:
lg n = log2/1n = log2/1(2/1)i=log22i = i
T(2n/3) means that the n size problem is reduced to 2/3 the size but double the size of T(n/3). T(2n/3) path is the longest from root to leaf. A leaf is a problem of size 1.
The longest path is then: n → (2/3)n → (2/3)2n → ... → 1
(2/3)kn=1 when k=log3/2 n the height is log3/2 n
Solve for k in (2/3)kn=1
n = (3/2)k
log3/2n=log3/23/2k=k
Cost ≤ cn log3/2nExpect cost to be at most: # levels * cost at each level = cn log3/2n when total cost evenly distributed across all levels.Because some internal leaves are absent (more absent nearer bottom), some levels contributes ≤ cn.
A complete binary tree of height log2n has 2log2n = nlog
22 = n leaves (at bottom).
The complete binary tree leaves are: 2log3/2
n = nlog3/2
2
Upper bound guess: dn log3/2 n = O(n lg n) for some positive constant d.Note: log3/2 n/log2 n= (log2 n*log3/2 2)/ log2 n = log3/2 2 = 0.585n log3/2 n / n lg n = c or about 0.585 suggesting dn log3/2 n = O(n lg n)
O upper bound: rewrite as T(n) ≤ T(n/3) + T(2n/3) + cn
Guess: T(n) = O(n lg n)
IH: T(n/3) ≤ d(n/3) lg(n/3)
T(2n/3) ≤ d(2n/3) lg(2n/3)
Chapter 4 - Recursion Tree http://homepages.ius.edu/RWISMAN/C455/html/notes/Chapter4/Recursio...
12 of 14 7/2/2013 8:47 PM
Substitution: Recall: log a/b = log a - log b
T(n) ≤ T(n/3) + T(2n/3) + cn
≤ d(n/3) lg(n/3) + d(2n/3) lg(2n/3) + cn IH
= (d(n/3) lg n − 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
Determining a tight upper-bound requires finding a closed form representation of the summation of the recursion tree calls
Chapter 4 - Recursion Tree http://homepages.ius.edu/RWISMAN/C455/html/notes/Chapter4/Recursio...
13 of 14 7/2/2013 8:47 PM
defined by:
There is an on-line encyclopedia of integer sequences, that can be searched by providing it with a few terms of a sequence. Forexample, we can search 1, 4, 15, 64, 325 (terms 1 through 5 of the sequence).
According to the site, the sequence is the number of permutations of non-empty subsets of {1,2,3,...,n}. If we define the sequence a(n) to be the expression given, we find that it obeys the recursion:
a(n) = n + n*a(n-1).
It also has the closed formula a(n) = [e*n! – 1], where the symbol [x] is the nearest integer to x, and e is the usual constant2.718...
The upper-bound is then: O(e*n!) = O(n!)
See http://www.research.att.com/~njas/sequences/index.html for the site's main page.
Chapter 4 - Recursion Tree http://homepages.ius.edu/RWISMAN/C455/html/notes/Chapter4/Recursio...