430 likes | 677 Views
Chapter 4: Solution of recurrence relationships Techniques: Substitution: Guess answer, prove by induction Tree analysis: Good way to develop a guess Master theorem: Recipe when T(n) = aT(nb) + f(n) with a > 1, b >1 and f(n) a specified function.
E N D
Chapter 4: Solution of recurrence relationships Techniques: Substitution: Guess answer, prove by induction Tree analysis: Good way to develop a guess Master theorem: Recipe when T(n) = aT(n\b) + f(n) with a >1, b >1 and f(n) a specified function Example: use substitution method to find run time for merge sort
When n is a power of 2 The recurrence for merge sort (worst case) is T(n) = 2 n=2 T(n) = 2T(n/2) +n n=2k k>1 Guess the solution T(n) = nlgn Prove by induction as in HW1
Worst case merge sort for any n T(n) = constant n=1 T(n) = T(floor(n/2)) + T(ceiling(n/2)) + Q(n) n>1 Look for order of growth instead of analytic solution We can ignore floor and ceiling and replace Q(n) by cn with c > 0 Recurrence becomes T(n) = 2T(n/2) + cn Prove T(n) = Q(nlgn) by substitution method
Based on Theorem 3.1 and definitions of O and W -dn + cn = 0 if d = c > 0; therefore, there exist d > 0 such that 0 <T(n) < d(nlgn)
Assume T(n/2)= W((n/2)lg(n/2)) -dn + cn = 0 if d = c > 0; therefore, there exist d > 0 such that 0 < d(nlgn) <T(n)
Sometimes, even with correct guess, substitution does not work To prove Big O in these cases, try subtracting a lower-order term in your assumption Example: T(n) = 8T(n/2) + Q(n2) Show T(n) =O(n3)
if assume T(n/2) < d[(n/2)3 – (n/2)2] most show T(n) < d[n3-n2] if d = c > 0
To prove Big W in these cases, try adding a lower-order term in your assumption
Sometimes a change of variable helps Example: T(n) = 2T(n1/2) + lg(n)
Solution of recurrence by tree analysis Example: T(n) = 2T(n/2) + Q(n)
Tree analysis At each node write overhead per recurrence usually a function of level index Cost of leaves = number of leaves Calculate istop Sum level cost for i = 0 to istop -1 Add cost of leaves If tree analysis just yields guess, verify by substitution Example: T(n) = 3T(|_n/4_|) + Q(n2) ~ 3T(n/4) + cn2
T(n)=3T(n/4)+cn2 cost of level i istop = ? How many leaves?
As indicated in previous slide, levels are indexed from zero ISTOP is the index of leaves At ISTOP divide and conquer has bottomed out with subgroups of size n0 Could evaluate cost of level using finite geometric sum Bounding by infinite series easier
Test guess from tree analysis by substitution Guess is upper bound only Did not evaluate sum over levels = 1 > 0 therefore, there exist d > 0 such that 0 < T(n) < dn2 all n>0 hence, T(n) = O(n2)
Quiz Friday, 2-14-14 Tree analysis size of tree (istop) cost of leaves (number of leaves) cost of ith level total cost of levels total cost order of growth Substitution method set up (remove floors, ceiling, asymptotic notation) assumption implementation of assumption constant and n0
CptS 450 Spring 2014 • [All problems are from Cormen et al, 3rd Edition] • Homework Assignment 4: due 2/24/14 • 1. ex 4.4-7 on page 93 (tree analysis and substitution) • 2. problems 4-1a, c and e on page 107 by master method • 3. problem 4-3f on page 108 by tree analysis and substitution
Binary tree with complicated level cost T(n) = 2T(n/2 +17) +n Expect T(n) = nlgn Why?
Note: sum cannot be bounded by geometric series. Why?
Imbalanced tree analysis continued Test guess by substitution = 0 = all n > 0
Skinny tree leaf = no2 no2 no2 See text p1147 + Q(n3) no2
substitution = 0 In this case, c(6n2-12n+8)-n2 = 0 leads to c = f(n),which is not acceptable
Case 1: is polynomially larger than f(n) at large n Conclusion: T(n) = Q( ) is still an upper bound on f(n) We can subtract e > 0 from logb(a) and Master Theorem: (statement on p94) “cook-book” method to solve T(n) = aT(n/b) + f(n) when a > 1, b > 1, and f(n) asymptotically positive Ignore floors and ceiling do not effect validity (section 4.6.2 p103) At large n, compare f(n) to
Case 2: is asymptotically the same as f(n) f(n) = Q( T(n) = Q( ) lgn) Conclusion: = Q(f(n) lgn) Case 3: f(n) is polynomially larger than We can add e > 0 to logb(a) and is still a lower bound on f(n) f(n) is “regular” af(n/b) < cf(n) for some c < 1 and n > n0 Conclusion: T(n) = Q(f(n))
When is Master Theorem not applicable 1. T(n) does not have the correct form 2. f(n) falls between cases 1 and 2 (cannot find e > 0 required by case 1) 3. f(n) falls between cases 2 and 3 (cannot find e > 0 required by case 3) 4. Regularity condition fails in case 3
Given f(n) = nk and f(n) = W( ) Prove that, in case 3, polynomial f(n) is regular
Proof of Master Theorem MT applies to T(n) = aT(n/b) + f(n) with a>1 and b>1 • Analyze master recurrence for n = bk 1. tree analysis → solution as a sum 2. bound sum → 3 cases B. Extend solution to any n > 0 Apply techniques related to floors and ceilings
Tree analysis master theorem T(n) = aT(n/b) + f(n)
T(n) = aT(n/b) + f(n) Case 1: cost of leaves dominates Case 2: cost of levels is same; sum can be evaluated Case 3: cost of root dominates: T(n) = Q(f(n))
T(n) = aT(n/b) + f(n) When n is not a power of b recurrence becomes T(n) = constant n=1 T(n) = T(floor(n/2)) + T(ceiling(n/2)) + Q(n) n>1 Section 4.6.2 show that floors and ceiling do not affect the tree analysis
Quiz Friday, 2-28-14 Master Theorem Which case applies Relationship between f(n) and nlogb(a) in asymptotic notation Value of e, if applicable Regularity of f(n), if applicable Substitution method set up (remove floors, ceiling, asymptotic notation) assumption implementation of assumption condition that determines constant and n0 use of constant and n0 in definition of O or W
Conditions that determine constant and n0 term = 0 factor = 1 term > 0 term < 0 factor > 1 factor < 1 If term = 0 or factor = 1 leads to constant = f(n) try a different condition. Example: T(n) = T(n/2-n1/2) + n
Iteration on recurrence T(n)=4T(n/2)+cn T(n) = cn + 4T(n/2) T(n) = cn + 4(cn/2 + 4T(n/4)) T(n) = cn +2cn +16(cn/4 + 4T(n/8)) T(n) = cn +2cn + 4cn +64(cn/8+4T(n/16)) T(n) = cn +2cn + 4cn + 8cn +256(cn/16 + 4T(n/32)) Level = 0 1 2 3 … Cost of level k is 2kcn Tree analysis can be performed without drawing tree istop and cost of leaves evident from recurrence Cost of levels sum over k