1 / 14

CS200: Algorithm Analysis

CS200: Algorithm Analysis. SOLVING RECURRENCES. Three methods : iteration (recurrence tree), substitution, and the master method . All methods require a final inductive proof, but this step is typically trivial and not required in this class. We assume the input size N is a positive integer.

Télécharger la présentation

CS200: Algorithm Analysis

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. CS200: Algorithm Analysis

  2. SOLVING RECURRENCES • Three methods : iteration (recurrence tree), substitution, and the master method. All methods require a final inductive proof, but this step is typically trivial and not required in this class. • We assume the input size N is a positive integer. • We assume that the input size (N) is a power of 2 in order to simplify the analysis. • These assumptions allow us to ignore floors and ceilings in run-time equations. • ie.Tmergesort(n) = T(n/2) + T(n/2) + (n)

  3. ELIMINATION OF FLOORS AND CEILINGS The following is an argument by example and not a formal proof. Already discussed (worst case, always search the larger partition): • T(n)binarysearch = (1) if n = 1 = T( (n/2) ) + (1) if n > 1 • If n is a power of 2, then we have shown previously that BinarySearch has a run time of T(n) = log n + 1 = Q( log n)

  4. To show that this is correct we can substitute lg n + 1 for T in the recurrence and note that the resulting equations are true when n = 2k. Use the log identity below: n = 2k and taking the lg of both sides gives us lg n = lg(2k) = k lg(2) = k => lg n = k Substituting lg n + 1 in recurrence, T(n)binarysearch = (1) if n = 1 = T( (n/2) ) + (1) if n > 1 lg n + 1 = 1 if n = 1 = (lg(n/2)+ 1) + 1 if n > 1

  5. Show that equations hold for powers of 2: nlog n + 1(log (n/2)+ 1) + 1 1 1 2 2 2 4 3 3 ................................ This doesn't actually prove that T(n) = Q(log n) but it does show that T(n) <= T(2 log n) = log n + 1 = Q( log n) This means that floors and ceilings usually may be ignored, technically one should be careful as in the text.

  6. BOUNDARY CONDITIONS We must show that T(n) holds for boundary conditions on n. • assume that T(n) = Q(1) for small n if solution is polynomially bounded. • but there are cases where boundary conditions do affect the analysis. Ex 1. T(n) = (T(n/2))2 if T(1) = 2 then T(2) = T(1)2 = 22 then T(4) = T(2)2 = 24 then T(n) = Q( 2n) and

  7. Ex 2. T(n) = (T(n/2))2 if T(1) = 3 then T(2) = T(1)2 = 32 then T(4) = T(2)2 = 34 then T(n) = Q( 3n) and 2n is not Q( 3n).

  8. SOLVING RECURRENCES 1. SUBSTITUTION- most general method. Guess the form of the solution Verify by induction and solve for constants • Note : we will not use this method as developing a guess requires a strong background in analysis and recurrences. Examples are only to give you a flavor of the technique.

  9. Note – following is an asymptotic sol. not an exact one. ex. T(n) = 4T(n/2) + Q(n) Guess: O(n3) • Induction: (using strong induction; assume true for all k < n) • Note : Typically we use weak induction and assume true for n-1 to prove for n. So a little different. Base case : assume T(k) ≤ ck3 for all k < n. Prove: T(n) ≤ cn3 by induction. T(n) = 4T(n/2) + n ≤ 4c(n/2)3 + n T(n) = c/2 (n3) + n Can we show c/2 (n3) + n ≤ cn3? NO!

  10. But = c/2 (n3) + n = [cn3] - ((c/2)n3 – n) So, T(n) = 4T(n/2) + n <= cn3 - ((c/2)n3 - n) • this last step is a trick for substitution method –> to prove inductive step write expression as [answer you want ] - [something > 0]. To finish we must show that cn3 - ((c/2)n3 - n) is O(n3). 0 <= cn3 - ((c/2)n3 - n) <= cn3 for c = ? and n0 = ? This is not a tight bound. We can show that T(n) = O(n2)

  11. We can show that T(n) = O(n2) A fallacious argument: Guess T(n) = 4T(n/2) + n = O(n2) Induction: Base case : assume T(k) <= ck2 for all k < n. Prove: T(n) <= cn2 by induction. T(n) = 4T(n/2) + n <= 4c(n/2)2 + n = cn2 + n But we can’t prove 0 <= cn2 + n <= cn2 for c = ? and n0 = ?

  12. A valid argument: Guess T(n) = 4T(n/2) + n = O(n2) Induction: Base case : assume T(k) ≤ ck2 - ck for all k < n. Prove: T(n) ≤ cn2 by induction. T(n) = 4T(n/2) + n ≤ 4(c(n/2)2 - c (n/2)) + n and 4(c(n/2)2 - c (n/2)) + n = cn2 - 2c  n + n and cn2 - 2c  n + n = cn2 - c  n - (c  n - n) in form we want cn2 - c  n - (c  n - n) ≤ cn2 - c  n if c  ≥ 1 and 0 ≤ cn2 - c  n ≤ cn2 for c= ? , c  = ? and n0 = ?

  13. This technique can also be used to determine lower bounds on runtime and thus Theta bounds.

  14. Summary Solving Recurrences • We can eliminate the use of floors/ceilings • We usually ignore boundary conditions – by assumingT(1) = 1 if T(n) is polynomially bounded. • Substitution method is “nasty” but is the most general technique and can be applied to any recurrence. We need a more algorithmic (procedural) technique to apply to certain common recurrences.

More Related