1 / 11

Introduction to Algorithms

Introduction to Algorithms. Chapter 4: Recurrences. Solving Recurrences. A recurrence is an equation that describes a function in terms of itself by using smaller inputs The expression: Describes the running time for a function contains recursion. Solving Recurrences. Examples:

keaira
Download Presentation

Introduction to Algorithms

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. Introduction to Algorithms Chapter 4: Recurrences

  2. Solving Recurrences • A recurrence is an equation that describes a function in terms of itself by using smaller inputs • The expression: • Describes the running time for a function contains recursion.

  3. Solving Recurrences • Examples: • T(n) = 2T(n/2) + (n)  T(n) = (n lg n) • T(n) = 2T(n/2) + n  T(n) = (n lg n) • T(n) = 2T(n/2) + 17 + n  T(n) = (n lg n) • Three methods for solving recurrences • Substitution method • Iteration method • Master method

  4. Recurrence Examples

  5. Substitution Method • The substitution method • “making a good guess method” • Guess the form of the answer, then • use induction to find the constants and show that solution works • Our goal: show that T(n) = 2T(n/2) + n = O(n lg n)

  6. Substitution MethodT(n) = 2T(n/2) + n = O(n lg n) • Thus, we need to show that T(n)  c n lg n with an appropriate choice of c • Inductive hypothesis: assume T(n/2)  c (n/2) lg (n/2) • Substitute back into recurrence to show thatT(n)  c n lg n follows, when c  1 • T(n) = 2 T(n/2) + n  2 (c (n/2) lg (n/2)) + n = cn lg(n/2) + n = cn lg n – cn lg 2 + n = cn lg n – cn + n  cn lg n for c  1 = O(n lg n) for c  1

  7. Iteration Method • Iteration method: • Expand the recurrence k times • Work some algebra to express as a summation • Evaluate the summation

  8. T(n) = c + T(n-1) = c + c + T(n-2) = 2c + T(n-2) = 2c + c + T(n-3) = 3c + T(n-3) … kc + T(n-k) = ck + T(n-k) • So far for nk we have • T(n) = ck + T(n-k) • To stop the recursion, we should have • n - k = 0  k = n • T(n) = cn + T(0) = cn • Thus in general T(n) = O(n)

  9. T(n) = n + T(n-1) = n + n-1 + T(n-2) = n + n-1 + n-2 + T(n-3) = n + n-1 + n-2 + n-3 + T(n-4) = … = n + n-1 + n-2 + n-3 + … + (n-k+1) + T(n-k) = for nk • To stop the recursion, we should have n - k = 0  k = n

  10. T(n) = 2 T(n/2) + c 1 = 2(2 T(n/2/2) + c) + c 2 = 22 T(n/22) + 2c + c = 22(2 T(n/22/2) + c) + (22-1)c 3 = 23 T(n/23) + 4c + 3c = 23 T(n/23) + (23-1)c = 23(2 T(n/23/2) + c) + 7c 4 = 24 T(n/24) + (24-1)c = … = 2kT(n/2k) + (2k - 1)ck

  11. So far for nk we have • T(n) = 2k T(n/2k) + (2k - 1)c • To stop the recursion, we should have • n/2k = 1  n = 2k k = lg n • T(n) = n T(n/n) + (n - 1)c = nT(1) + (n-1)c = nc + (n-1)c = nc + nc – c = 2cn – c T(n) = 2cn – c = O(n)

More Related