230 likes | 403 Views
Updates. HW#1 has been delayed until next MONDAY . There were two errors in the assignment.
E N D
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.
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)
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
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.
The Substitution Method The most general method: • Guess the form of the solution. • Verify using induction. • Solve for the constants.
Substitution Method: Example 1 • Example: T(n) = 4T(n/2) + 100n • Guess: T(n) = O(n3)
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.
A Tighter Bound? • Example: T(n) = 4T(n/2) + 100n • Guess: T(n) = O(n2)
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).
Subtleties.. • Example: T(n) = 4T(n/2) + 100n • Guess T(n) = O(n2 -n).
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.
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.
Example of recursion tree Solve T(n) = T(n/4) + T(n/2) + n2:
Example of recursion tree Solve T(n) = T(n/4) + T(n/2) + n2: T(n)
T(n/2) T(n/4) Example of recursion tree Solve T(n) = T(n/4) + T(n/2) + n2: n2
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)
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)
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)
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)
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)
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
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)).