Outline • • • • Recurrence, Sloppiness Recitation 2: Recurrence, sloppiness 1 Master Method • 2. 3. If f (n) = O(nlogba – ε) for ε > 0, then T(n) = Θ(nlogba) . If f (n) = Θ(nlogba lgkn) for some constant k ≥ 0, then T(n) = Θ(nlogba lgk+1n) . If f (n) = Ω(nlogba + ε) for some constant ε > 0 and a f (n/b) ≤ c f (n) for some constant c < 1, then T(n) = Θ( f (n) ) . Recitation 2: Recurrence, sloppiness Recitation 2: Recurrence, sloppiness 3 • For each of the following recurrences, give the asymptotic solution using the Master Theorem (state which case), or else state that the Master Theorem does not apply • T(n) = 4T(n/2) + n Recitation 2: Recurrence, sloppiness • T(n) = 4T(n/2) + n2 • T(n) = 4T(n/2) + n2 lg n • T(n) = 4T(n/2) + n3 • T(n) = T(n-2) + n • T(n) = 3T(n/4) + n lg n • T(n) = 5T(n/2) + n2 lg n Recitation 2: Recurrence, sloppiness 2 Solving Recurrence (Practice) Let a ≥ 1, b > 1 and T(n) = aT(n/b) + f(n), where n/b means either ⎣n/b⎦ or ⎡n/b⎤. Then 1. Master Method review Sloppiness Solving recurrence (practice) Solving recurrence (tricks) 5 Recitation 2: Recurrence, sloppiness 4 6 1 • T(n) = 4T(n/4) + n/lg n • T(n) = 2T(n/4) + √n • T(n) = T(n/4) + lg n • T(n) = 8 T((n - √n)/4) + n2 • T(n) = 2T(n/4) + lg n • T(n) = 2T(n/4) + n0.51 Recitation 2: Recurrence, sloppiness 7 Recitation 2: Recurrence, sloppiness • T(n) = 4T(n/2) + n/lg n • T(n) = T(n/2) + lg(n!) • T(n) = 8T(n/3) + 2n • T(n) = T(n/2) + n! • T(n) = 7T(n/3) + n2 • T(n) =16T(n/4) + n2 log 2 n Recitation 2: Recurrence, sloppiness 9 Change of Variable 10 Drop Lower Order Terms • An unfamiliar looking recurrence can be sometimes be made into a familiar one by a change of variable • Example: T(n) = T(√n) + lg n • Let m = lg n, so n = 2m and √n = 2m/2 • So T(2m) = T(2m/2) + m • Define S(m) = T(2m), then • For recurrences like T(n) = 2T(n/2 + √n) + Θ(1), note that n dominates √n. • Guess that solution is the same as S(n) = 2S(n/2) + Θ(1) = Θ(n) • Solution must be verified by the substitution method – S(m) = S(m/2) + m • So S(m) = Θ(m) • Substituting back: T(n) = Θ(lg n) Recitation 2: Recurrence, sloppiness Recitation 2: Recurrence, sloppiness 8 11 Recitation 2: Recurrence, sloppiness 12 2 Theorem (Sloppiness): • Suppose that: Sloppiness 1. 2. 3. • What about the merge sort recurrence? – T(n) = T(⎣n/2⎦ ) + T(⎡n/2⎤) + Θ(n) – Not strictly covered by Master Theorem – In the case where n is a power of 2, no problem: • T(n) = 2T(n/2)+Θ(n) • By case 2 of Master Theorem, T(n) = Θ(n log n) – Under what conditions can we say that recurrences such as the merge sort recurrence has the same solution as the simplified version that works for powers of 2?? Recitation 2: Recurrence, sloppiness 13 T(n) and f(n) are monotonically increasing T(bi) ≤ f(bi) for all i > 0 f(n) = O(f(n/b)) - polynomial growth • Then T(n) = O(f(n)) Proof: • T(n) ≤ T(b ⎡logbn⎤) by (1) • ≤ f(b ⎡logbn⎤) by (2) • ≤ cf(b ⎡logbn⎤-1) by (3) • ≤ c(f(n)) by (1) Recitation 2: Recurrence, sloppiness 14 Summary • Master Theorem – Memorize - can do most recurrences in your head • Sloppiness – Recurrences in algorithms often not defined cleanly – Only need to be defined cleanly for powers of b • Tricks for solving recurrences – Change of variable – Drop lower order terms • Akra-Bazzi – Useful in some cases not covered by Master Theorem • If these methods don’t work, use your ingenuity, then verify with substitution method! Recitation 2: Recurrence, sloppiness 18 3