1 / 18

Recurrence Equations

Recurrence Equations. The running time analysis of recursive functions leads naturally to recurrence equations A recurrence equation defines the value of an integer function at input n in terms of values of the function on previous values

kenna
Download Presentation

Recurrence Equations

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. Recurrence Equations • The running time analysis of recursive functions leads naturally to recurrence equations • A recurrence equation defines the value of an integer function at input n in terms of values of the function on previous values • The specification must also indicate the specific values at small inputs • Example 1: • f(1) = f(2) = 1 (initial conditions) • f(n) = f(n-1) + f(n-2) for all n > 2 • Of course, the above recurrence equation defines the sequence of Fibonacci numbers

  2. Recurrence Equations: Example 2 • Example 2 • Find an exact solution to the following recurrence equation: • Initial condition: c(0) = 0 • Recurrence: c(n) = c(n-1) + n for all n > 0 • To solve by iteration, we repeatedly replace terms with their recurrence expression until the solution is obvious • In principle we should then prove by induction that the solution is valid • c(n) = c(n-1) + n = c(n-2) + n-1 + n = c(n-3) + n-2 + (n-1) + n  = 1 + 2 +  + (n-1) + n = n(n+1)/2

  3. Recurrence Equations: Example 2 • Example 2 • Find an exact solution to the following recurrence equation: • Initial condition: c(0) = 0 • Recurrence: c(n) = c(n-1) + n for all n > 0 • Claim: c(n) = n(n+1)/2 • Proof by induction on n: • Basis (n = 0): c(0) = 0 = 0(0+1)/2 • Inductive Step: Assume that c(k) = k(k+1)/2 and prove c(k+1) = (k+1)((k+1)+1)/2 • c(k+1) = c(k) + k+1 = k(k+1)/2 + (k+1) inductive hypothesis = (k/2 + 1)(k+1) = (k/2+2/2)(k+1) = ((k+2)/2)(k+1) = (k+1)(k+2)/2 = (k+1)((k+1)+1)/2 done

  4. Recurrence Equations: Example 3 • Let c(n) denote the number of times the instruction x = x+1 is executed by the following algorithm: • example(n) { if (n == 1) return for i = 1 to n x = x+ 1 example(n/2) } • Initial condition: c(1) = 0, since x = x+1 is never executed • Recurrence: c(n) = c(n/2) + n for n > 1. • Problem: how to deal with the n/2 • Answer: first prove that c is a nondecreasing function • Then solve for n a power of 2 • Then use the fact that the function is increasing to solve for other values Execution count: n Execution count: c(n/2) // Note: n/2 means integer division, n/2

  5. Recurrence Equations: Example 3 • Initial condition: c(1) = 0 • Recurrence: c(n) = c(n/2) + n for i > 1 • Claim: c(n)  0 for all n  1 The proof is Strong Mathematical Induction on n. The basis case is trivial: c(1) = 0 We must now show that for n > 1, if c(i)  0 for all positive integers i < n, then c(n)  0. But this is also obvious: c(n) = c(n/2) + n  0 + n > 0. Inductive Hypothesis Inductive Conclusion

  6. Recurrence Equations: Example 3 • Initial condition: c(1) = 0 • Recurrence: c(n) = c(n/2) + n for i > 1 • Claim: if 1  i  j, then c(i)  c(j) Let P(n) be the following statement: if 1  i  j  n, then c(i)  c(j) We will prove the P(n) is true for all n  1 by induction on n. Basis step n = 1: immediate, since i = j = n = 1. Inductive Step: assume n > 1 and P(n-1) is true. That is, assumeif 1  i  j  n-1, then c(i)  c(j). We must prove that P(n) is true. If j  n, then j  n-1 and the statement follows by the inductive hypothesis P(n-1). Thus we must show that if 1  i  n, then c(i)  c(n). If i = 1, then c(1) = 0  c(n) for all n  1. Thus assume i  2. Then 1  i/2  n/2  n-1, so c(i/2)  c(n/2) and hence c(i) = c(i/2) + i  c(n/2) + n = c(n).

  7. Recurrence Equations: Example 3 • Initial condition: c(1) = 0 • Recurrence: c(n) = c(n/2) + n • Looking at n = 2k for some k: • c(2k) = c(2k-1) + 2k = c(2k-2) + 2k-1 + 2k … = c(1)+ 21 +  + 2k-1 + 2k = 0 + 21 +  + 2k-1 + 2k = 2k+1 – 2 = 2(2k )– 2 = 2n – 2 • Thus for n a power of 2, c(n) = 2n-2 and hence is (n) • A formal proof of the statement c(n) = 2n-2 for n a power of 2 can be done easily by induction and is assigned as an exercise for the curious.

  8. Recurrence Equations • Initial condition: c(1) = 0 • Recurrence: c(n) = c(n/2) + n • For n a power of 2, c(n) = 2n-2 • Now, for integers n not equal to a power of 2, let k-1 = log2n • Then k-1  log2n < k and thus 2k-1 n < 2k • Then c(2k-1)  c(n) < c(2k) = 2k+1 – 2 = 4(2k-1)– 2 < 4n • Thus c(n) is O(n) • Since n  3, n/2  n-2  2k – 2 = c(2k-1)  c(n) • Thus c(n) is (n) • It now follows that c(n) is (n) • Whew! What a lot of work. • Don’t worry, there is a theorem on the way to help.

  9. The Master Theorem for Recurrences • Divide and conquer algorithms do the following: • break a problem up into a  2 subproblems of smaller size • recursively solve the subproblems • combine these solutions to solve the intial problem • Often the subproblems all have essentially the same size, say n/b or n/b, where n is the size of the original problem and b  2 is an integer • Such algorithms give rise to recurrences of the formT(n) = aT(n/b) + f(n)where f(n) is the time needed by the combination phase and n/b is understood to be either  n/b or  n/b  • There are also situations where we do not have equality, but rather  or  relations • The Master Theorem provides asymptotic estimates in these situations and, as it happens, the presence of floor or ceiling does not matter.

  10. Example • Mergesort sorts an array of 2 or more elements by recursively sorting the first half, recursively sorting the second half then merging the two sorted subarrays. • The first half of the array has size n/2 and the second half has size n/2. • Moreover, the merge routine for the two subarrays has running time (n) • Thus the recurrence for the running time T(n) of mergesort is T(n) = T(n/2 ) + T(n/2) + n • Since the Master Theorem treats n/2 and n/2 as simply n/2, the recurrence becomesT(n) = 2T(n/2 ) + n

  11. The Master Theorem for Recurrences • Let a, b be integers and k a real number with a  1, b  2 and k  0. • We will use n/b to denote either n/b or n/b • Upper Bound: If T(n)  aT(n/b) + f(n) and f(n) is O(nk), then O(nk) if a < bkT(n) = O(nk lg n) if a = bkO( ) if a > bk

  12. The Master Theorem for Recurrences • Let a, b, k be integers with a  1, b  2 and k  0. • We will use n/b to denote either n/b or n/b • Lower Bound: If T(n)  aT(n/b) + f(n) and f(n) is (nk), then (nk) if a < bkT(n) = (nk lg n) if a = bk ( ) if a > bk

  13. The Master Theorem for Recurrences • Let a, b, k be integers with a  1, b  2 and k  0. • We will use n/b to denote either n/b or n/b • Tight Bound: If T(n) = aT(n/b) + f(n) and f(n) is (nk), then (nk) if a < bkT(n) = (nk lg n) if a = bk ( ) if a > bk

  14. The Master Theorem for Recurrences • Example 1: Mergesort’s recurrence is T(n) = 2T(n/2) + n • In the setting of the Master Theorem, we have a = 2, b = 2 and k = 1 • Since a = bk, the Master Theorem implies Mergesort’s running time is (n1 lg n) = (nlg n)

  15. Example Recurrence Solutions • Example 2: c(n) = c(n/2) + n • The parameters in this case are: a = 1, b = 2, f(n) = n and thus k = 1 • Since a = 1 < bk = 21 = 2, the first case applies. • Since the recurrence is an equality, we conclude that c(n) is (nk) = (n1) = (n)

  16. Example Recurrence Solutions • Example 3: T(n) = 2T(n/2) + n • Parameters: a = 2, b = 2, k = 1 • a = 2 = bk = 21, so second case applies • Since the recurrence is an equality, we conclude that T(n) is (nk lg n) = (n1 lg n) = (n lg n)

  17. Example Recurrence Solutions • Example 4: T(n) = 7T(n/4) + 1 • Parameters: a = 7, b = 4, k = 0 (since 1 = n0) • a = 7 > bk = 40, so third case applies • Since the recurrence is an equality, we conclude thatT(n) is  (n 1.4037 )

  18. Homework • Pages 66-68, problems 14, 15, 17, 32, 33, 35 and 36

More Related