1 / 45

Algorithms Analysis & Methods | Lecture Summary

Explore recursion tree, substitution, & iteration methods in this computer algorithms lecture. Learn about The Master Theorem & examples of application. Understand how to solve equations using The Master Method.

oliviaa
Download Presentation

Algorithms Analysis & Methods | Lecture Summary

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. CS 575Design and Analysis ofComputer AlgorithmsProfessor Michal CutlerLecture 5September 13, 2005

  2. This class • Recursion tree method • Substitution method • Iteration • The master theorem • Application examples of Master theorem

  3. Recursion trees • The tree is expanded one depth at a time. • In the result tree: • Each “recursive call” is represented by a node containing the “non recursive” number of operations done during the call (t(size)) • Each base case is represented by a leaf containing the count for the base case • The number of operations is summed for each depth, and then over the depths

  4. T(n)=2T(n/2)+n2 n2 T(n/2) T(n/2)

  5. T(n)=2T(n/2)+n2 n2 (n/2)2 (n/2)2 T(n/4) T(n/4) T(n/4) T(n/4)

  6. The recursion tree for T(n) n2 n2 0 1 (n/2)2 (n/2)2 (1/2) n2 2 (n/4)2 (n/4)2 (n/4)2 (n/4)2 (1/4) n2 . . . . . . lgn

  7. Recursion trees

  8. Recursion trees

  9. The substitution method • We guess the form of the solution (from experience mostly). • We then verify the solution and solve for constants. • Not possible to guess correctly in every case. • T(n) = 4T(n/2) + n • We guess O(n3) and show T(n) £cn3 for all n³n0

  10. Example (cont.) • We assume that T(k) £ ck3 for k < n and prove T(n) £ cn3 by induction. T(n)=4T(n/2) + n £ 4c(n/2)3 + n = = (c/2)n3 + n = cn3 - ((c/2)n3 - n) £cn3 provided (c/2)n3 - n ³0 • So (c/2)n3 ³ n and c ³ 2/n2 for all n³1 • c³2 and n³1

  11. Initial Condition • Assume that T(1)=3 • 3=T(1)c13=c . So cmax{3, 2}=3.

  12. A tighter bound • Improve bound to T(n)=O(n2) • We assume that T(k) £ ck2 for k < n and prove T(n) £ cn2. T(n)=4T(n/2) + n£4c(n/2)2 + n = = cn2 + n. • Bad guess! Try T(n)£c1n2-c2n

  13. Subtract a lower term • We assume that T(k) £ c1k2-c2k for k < n and prove T(n) £ c1n2-c2n. T(n)=4T(n/2) + n£4(c1(n/2)2 - c2n/2)+ n = (c1n2 - c2n) - (c2n - n) £(c1n2 - c2n) only if c2n- n³0 and c2³1 for n³1 • Since T(1)=3 £ c112 - c21 we pick c1=4 and c2=1

  14. The iteration method • We expand the recursion, then get a summation, then compute the summation using algebraic methods.

  15. The iteration method • We have to know rules for summations to use this method. • We often use this method to guess the solution for the substitution method.

  16. Example • T(n) = n + 4T(n/2) = n + 4(n/2 + 4T(n/4))= = n + 2n + 16(n/4 + 4T(n/8))= = n + 2n + 4n + 64(n/8 + 4T(n/16))= = n + 2n + 22n + 23n + …+2k-1n + 4kT(n/2k) • Let n=2k and k=lgn • We know 4lg n=nlg4 =n2 (because algb = blga)

  17. Example (cont)

  18. The Master method • Used to solve equations of the form T(n) = aT(n/b) + f(n) where a ³1, b > 1, and f(n) is an asymptotically nonnegative function. Note: Version more general than one in text

  19. Master theorem • More general version than book

  20. Master theorem • A different version of previous slide

  21. The recursion tree (integer a) f(n) f(n) af(n/b) f(n/b) f(n/b) f(n/b) a2f(n/b2) f(n/b2) f(n/b2) f(n/b2) k Q(1) Q(1) Q(1) Q(1) Q(1) Q(1) Q(1) Q(1) Q(1) ak Q(1) n=bk, k=logbn, T(n/bk )=T(1)= Q (1)

  22. The Master method • Let n=bk and k=logbn. • The number of leaves in the recursion tree is alogbn = nlogba.

  23. Master method examples • T(n) = 2T(n/2) + Q(n) • a = b =2 • f(n)= Q(n) • nlog22 = n, • Q(n)/n = Q(1) = Q(lg0n) • Case 2 and T(n)= Q(nlgn)

  24. Master method examples • T(n) = 7T(n/2) + Q(n2). • a =7, b = 2 • nlogba = nlog27 • Q(n2)/nlog27 = Q(n2-lg 7) = Q(n-0.8) • Case 1 and T(n) = Q(nlog27)

  25. Master method examples • T(n) = T(n/2) + Q(1) Since nlog21 = n0 = 1, Q(1)/1 = Q(log0n), we are dealing with Case 2 and T(n) = Q(log n)

  26. Master method examples • T(n) = 4T(n/2) + n3 Since n3/nlg4 = n, we are dealing with Case 3. • Is af(n/b) <= cf(n) for some c<1 and all sufficiently large n? yes 4(n/2)3 =n3/2 so c=1/2 • So T(n) = Q(n3) • The master method does not solve all recurrences of the given form.

  27. Master method fails • T(n) = 4T(n/2) + n2/lgn (n2/lgn)/n2 = 1/lgn and no case applies

  28. Master method fails • The solution is T(n) = Q(n2lglgn) and can be verified by the substitution method.

  29. The Three Cases

  30. Case 1: nlogba is polynomially larger than f(n). • f(n)= O(nlogba-e), or • f(n)/nlogba = O(n-e) , or • nlogba/ f(n) = W(ne) for some constant e > 0 • We bound the sum within the O-notation by factoring the terms, the remaining sum is an increasing geometric series

  31. g(n) for case 1

  32. g(n) for Case 1

  33. g(n) for Case 1

  34. Case 1: nlogba is polynomially larger than f(n). • Total weight is (constant *number leaves).

  35. Case 2: nlogba and f(n) are within a polylogarithmic factor. • f(n)/ nlogba = Q(lgkn) for some constant k³0. • T(n) = Q(nlogba) + Q(nlogba lgk+1n)= = Q(nlogba lgk+1n)

  36. g(n) for Case 2

  37. g(n) for Case 2

  38. g(n) for Case 2

  39. g(n) for Case 2

  40. Case 3:nlogba is polynomially smaller than f(n). • f(n)/ nlogba = W (ne) for some constant e > 0. • Total weight is (constant times weight of root). • T(n) = Q(nlogba) + Q(f(n))= Q(f(n))

  41. Case 3:nlogba is polynomially smaller than f(n). • We will show that 1. g(n)= W(f(n)), and 2. g(n)=O(f(n)) and therefore g(n)= Q(f(n))

  42. Case 3:nlogba is polynomially smaller than f(n). 1. We know that a³1 and that f(n)³0 for all large enough n • So all terms of g(n) are nonnegative for large enough n. • The first term of g(n) is f(n), so g(n)³f(n) for all large enough n and g(n)=W(f(n))

  43. Case 3

  44. Case 3

  45. Next class • Quicksort

More Related