1 / 23

Updates

Updates. HW#1 has been delayed until next MONDAY . There were two errors in the assignment.

tamah
Download Presentation

Updates

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. Updates • HW#1 has been delayed until next MONDAY. There were two errors in the assignment. • 2-1. Merge sort runs in Θ(n log n). Insertion sort runs in Θ(n2). Consider a modification to Merge sort where n/k sublists of length k are sorted using Insertion sort and then merged by Merge sort.

  2. Updates • A) Show each n/k sublist can be sorted by Insertion Sort in Θ(n/k) worst case time. • B) Show that the sublists can be merged in Θ(n lg(n/k)) worst case time. • C) Skip. • D) How should k be chosen in practice? (hint: philosophical)

  3. Lecture 3: Solving Recurrences

  4. Recurrences • Recurrence: An equation that describes a function in terms of its value on smaller inputs. • Example (MergeSort):T(n) = Θ(n) if n = 1, 2T(n/2) + Θ(n) if n>1

  5. Recurrences • We will study three techniques: • The substitution method • The recursion-tree method • The master method • Sometimes more than one works well, sometimes none work well.

  6. The Substitution Method The most general method: • Guess the form of the solution. • Verify using induction. • Solve for the constants.

  7. Substitution Method: Example 1 • Example: T(n) = 4T(n/2) + 100n • Guess: T(n) = O(n3)

  8. Substitution Method: Example 1 • Example: T(n) = 4T(n/2) + 100n • Proof: • Assume that T(1) = Θ(1). • Guess T(n) = O(n3). We want to prove T(n) < cn3 for some c>0. • Verify by substituting into the recurrence.We assume this holds for T(n/2) < c (n/2)3, thusT(n) < 4c(n/2)3 + 100n = cn3/2 + 100n= (c/2)n3 + 100n= cn3 - (c/2)n3 + 100n= cn3 - ((c/2)n3 - 100n) “residual”< cn3when (c/2)n3 - 100n > 0, that is solving for constants, when c > 200 and n > 1.Always verify the base case(s) as well. Here Θ(1) < cn3 for sufficiently large c.

  9. A Tighter Bound? • Example: T(n) = 4T(n/2) + 100n • Guess: T(n) = O(n2)

  10. A Tighter Bound? • Example: T(n) = 4T(n/2) + 100n • Proof: • Assume that T(1) = Θ(1). • Guess T(n) = O(n2). We want to prove T(n) < cn2 for some c>0. • Verify by substituting into the recurrence.We assume this holds for T(n/2) < c (n/2)2, thusT(n) < 4c(n/2)2 + 100n = cn2 + 100n= cn2 – (-100n) residual?< cn2when -100n > 0. No valid assignment of c and n > 0 can be made! We have made a bad guess.We MUST prove the EXACT form of the inductive hypothesis (our guess).

  11. Subtleties.. • Example: T(n) = 4T(n/2) + 100n • Guess T(n) = O(n2 -n).

  12. Subtleties.. • Example: T(n) = 4T(n/2) + 100n • Proof: • Assume that T(1) = Θ(1). • Guess T(n) = O(n2 -n). We want to prove T(n) < c1n2 -c2 for some c1>0 and c2>0. • Verify by substituting into the recurrence.We assume this holds for T(n/2) < c1(n/2)2-c2n/2, thusT(n) < 4c1(n/2)2 - 4c2n/2 + 100n = c1n2 - 2c2n + 100n= c1n2 - c2n - (c2n - 100n) residual< c1n2 - c2n when c2n-100n > 0, that is, c2>100.

  13. Recursion Tree Method • The Recursion Tree Method doesn’t necc. build a proof, but a good guess. You might use this method with the substitution method. • The idea is to draw the tree and do the “accounting” the way we did with MergeSort last time. • This can be unreliable, but it provides good intuition.

  14. Example of recursion tree Solve T(n) = T(n/4) + T(n/2) + n2:

  15. Example of recursion tree Solve T(n) = T(n/4) + T(n/2) + n2: T(n)

  16. T(n/2) T(n/4) Example of recursion tree Solve T(n) = T(n/4) + T(n/2) + n2: n2

  17. Example of recursion tree Solve T(n) = T(n/4) + T(n/2) + n2: n2 (n/2)2 (n/4)2 T(n/8) T(n/4) T(n/16) T(n/8)

  18. Example of recursion tree Solve T(n) = T(n/4) + T(n/2) + n2: n2 (n/2)2 (n/4)2 (n/8)2 (n/4)2 (n/16)2 (n/8)2 … Q(1)

  19. Example of recursion tree Solve T(n) = T(n/4) + T(n/2) + n2: n2 (n/2)2 (n/4)2 (n/8)2 (n/4)2 (n/16)2 (n/8)2 … Q(1)

  20. Example of recursion tree Solve T(n) = T(n/4) + T(n/2) + n2: n2 (n/2)2 (n/4)2 (n/8)2 (n/4)2 (n/16)2 (n/8)2 … Q(1)

  21. Example of recursion tree Solve T(n) = T(n/4) + T(n/2) + n2: n2 (n/2)2 (n/4)2 (n/8)2 (n/4)2 (n/16)2 (n/8)2 … … Q(1)

  22. Example of recursion tree Solve T(n) = T(n/4) + T(n/2) + n2: n2 (n/2)2 (n/4)2 (n/8)2 (n/4)2 (n/16)2 (n/8)2 … … Q(1) Total = = Q(n2) geometric series

  23. The Master Method • The Master Theorem: Let a > 1 and b > 1 be constants. Let f(n) be a function and T(n) be defined on the non-negative integers as,T(n) = a T(n/b) + f(n). • T(n) can be asymptotically bounded as follows: • If f(n) = O(nlogba-ε), then T(n) = Θ(nlogba) • If f(n) = Θ(nlogba), then T(n) = Θ(nlogbalg n) • If f(n) = Ω(nlogba+ ε) and if af(n/b)<cf(n) for c < 1 and all sufficiently large n, then T(n) = Θ(f(n)).

More Related