1 / 23

Recurrences

Recurrences. Solving Recurrences. A recurrence is an equation or inequality that describes a function in terms of itself by using smaller inputs The expression: is a recurrence. Solving Recurrences. Examples: T ( n ) = 2 T ( n /2) + ( n ) T ( n ) = ( n lg n )

tehya
Download Presentation

Recurrences

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. Recurrences

  2. Solving Recurrences • A recurrence is an equation or inequality that describes a function in terms of itself by using smaller inputs • The expression: • is a recurrence.

  3. Solving Recurrences • Examples: • T(n) = 2 T(n/2) + (n) • T(n) = (n lg n) • T(n) = 2T(n/2) + n • T(n) = (n lg n) • T(n) = 2(T(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) = 2T(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. Substitution Method • Consider • Simplify it by letting m = lg n n = 2m • Rename S(m) = T(2m) • S(m) = 2 S(m/2) + m = O(m lg m) • Changing back from S(m) to T(n), we obtain • T(n) = T(2m) = S(m) = O(m lg m) = O(lg n lg lg n)

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

  9. 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)

  10. 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

  11. 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

  12. 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  k = lg n • T(n) = 2lgnT(n/2lgn) + (2lgn - 1)c = nT(n/n) + (n - 1)c = nT(1) + (n-1)c = nc + (n-1)c = nc + nc – c = 2cn – c=cn – c/2 cn = O(n) for all n ½

  13. T(n) = aT(n/b) + cn 1 = a(aT(n/b/b) + cn/b) + cn 2 = a2T(n/b2) + cna/b + cn = a2T(n/b2) + cn(a/b + 1) = a2(aT(n/b2/b) + cn/b2) + cn(a/b + 1) 3 = a3T(n/b3) + cn(a2/b2) + cn(a/b + 1) = a3T(n/b3) + cn(a2/b2 + a/b + 1) = … = akT(n/bk) + cn(ak-1/bk-1 + ak-2/bk-2 + … + a2/b2 + a/b + 1) k

  14. So we have • T(n) = akT(n/bk) + cn(ak-1/bk-1 + ... + a2/b2 +a/b+1) • To stop the recursion, we should have • n/bk = 1  n = bk  k = logbn • T(n) = akT(1) + cn(ak-1/bk-1 +...+ a2/b2 + a/b+1) = akc + cn(ak-1/bk-1 + ... + a2/b2 + a/b + 1) = cak + cn(ak-1/bk-1 + ... + a2/b2 + a/b + 1) = cnak/bk + cn(ak-1/bk-1 + ... +a2/b2 +a/b+1) = cn(ak/bk+ ... + a2/b2 + a/b + 1)

  15. So with k = logbn • T(n) = cn(ak/bk+ ... + a2/b2 + a/b + 1) • What if a = b? • T(n) = cn(1+ ... + 1+ 1 + 1) // k+1 times = cn(k + 1) = cn(logbn + 1) = (n logb n)

  16. So with k = logbn • T(n) = cn(ak/bk+ ... + a2/b2 + a/b + 1) • What if ab? • Recall that • (xk + xk-1 + … + x + 1) = (xk+1 -1)/(x-1) • So: • T(n) = cn · (1) = (n)

  17. So with k = logbn • T(n) = cn(ak/bk+ ... + a2/b2 + a/b + 1) • What if a  b? • T(n) = cn · (ak / bk) = cn · (alogbn / blogbn) = cn· (alogbn / n) recall logarithm fact: alogbn = nlogba = cn· (nlogba / n) = (cn· nlogba / n) = (nlogba)

  18. So…

  19. The Master Theorem • Given: a divide and conquer algorithm • An algorithm that divides the problem of size n into a subproblems, each of size n/b • Let the cost of each stage (i.e., the work to divide the problem + combine solved subproblems) be described by the function f(n) • Then, the Master Theorem gives us a cookbook for the algorithm’s running time:

  20. The Master Theorem • if T(n) = aT(n/b) + f(n) where a≥ 1 & b > 1 • then

  21. Using The Master MethodCase 1 • T(n) = 9T(n/3) + n • a = 9, b = 3, f(n) = n • nlogb a = nlog39 = n2 • since f(n) = O(nlog39 - ) = O(n2-0.5) = O(n1.5) • where  = 0.5 • case 1 applies: • Thus the solution is • T(n) = (n2)

  22. Using The Master Method Case 2 • T(n) = T(2n/3) + 1 • a = 1, b = 3/2, f(n) = 1 • nlogba = nlog3/21 = n0 = 1 • since f(n) = (nlogba) = (1) • case 2 applies: • Thus the solution is • T(n) = (lg n)

  23. Using The Master Method Case 3 • T(n) = 3T(n/4) + n lg n • a = 3, b = 4, f(n) = n lg n • nlogba = nlog43 = n0.793 = n0.8 • Since f(n) = W(nlog43+) = W(n0.8+0.2) = W(n) • where   0.2, and for sufficiently large n, • a.f(n/b) = 3(n/4) lg(n/4)  (3/4) n lg n for c = 3/4 • case 3 applies: • Thus the solution is • T(n) = (n lg n)

More Related