160 likes | 664 Views
Using The Master Method Case 1. T(n) = 9T(n/3) + n a=9, b=3, f(n) = n n log b a = n log 3 9 = (n 2 ) Since f(n) = O( n log 3 9 - ) = O(n 2-0.5 ) = O(n 1.5 ) where =0.5 case 1 applies: Thus the solution is T(n) = (n 2 ). Using The Master Method Case 2. T(n) = T(2n/3) + 1
E N D
Using The Master MethodCase 1 • T(n) = 9T(n/3) + n • a=9, b=3, f(n) = n • nlogb a = nlog3 9 = (n2) • Since f(n) = O(nlog3 9 - ) = O(n2-0.5) = O(n1.5) • where =0.5 • case 1 applies: • Thus the solution is • T(n) = (n2)
Using The Master MethodCase 2 • T(n) = T(2n/3) + 1 • a=1, b=3/2, f(n) = 1 • nlogba = nlog3/21 = n0 = 1 • Since f(n) = (nlogba) = (1) • case 2 applies: • Thus the solution is • T(n) = (lg n)
Using The Master MethodCase 3 • T(n) = 3T(n/4) + n lg n • a=3, b=4, f(n) = n lg n • nlogba = nlog43 = n0.793 = n0.8 • Since f(n) = W(nlog43+) = W(n0.8+0.2)= W(n) • where 0.2, and for sufficiently large n, • a . f(n/b) = 3(n/4) lg(n/4) < (3/4) n lg n for c = 3/4 • case 3 applies: • Thus the solution is • T(n) = (n lg n)
When the Master Method does not apply to recurrence • T(n) = 2T(n/2) + n lg n • a = 2, b = 2, f(n) = n lg n, and nlogba = n • Note that f(n) = n lg n is asymptotically larger than nlogba = n …. It might seem that case 3 should apply. • The problem is that it is not polynomially larger. • The ratio f(n) / nlogba = (n lg n) / n = lg n is asymptotically less than nε for any positive constant ε. • The recurrence falls into the gap between case 2 and case 3.
f(n) a f(n/b) f(n/b) … f(n/b) a a a … … … f(n/b2) f(n/b2) f(n/b2) f(n/b2) f(n/b2) f(n/b2) f(n/b2) f(n/b2) f(n/b2) a a a a a a … … … … … … Θ(1) Θ(1) Θ(1) Θ(1) Θ(1) Θ(1) Θ(1) Θ(1) Θ(1) Θ(1) Θ(1) Θ(1) Θ(1) Θ(1) Recursion tree view f(n) af(n/b) a2f(n/b2)
Examples • Ex. T(n) = 4T(n/2) + n • a = 4, b = 2, f (n) = n • nlogba= n2 f • Since f (n) = O(n2–) for = 1 • Case 1 applies • T(n) = Θ(n2). • Ex. T(n) = 4T(n/2) + n2 • a = 4, b = 2, f(n) = n2 • nlogba= n2 • Since f (n) = Θ(n2). • Case 2 applies • T(n) = Θ(n2lgn).
Examples • Ex. T(n) = 4T(n/2) + n3 • a = 4, b = 2, f (n) = n3 • nlogba= n2 • case 3: f (n) = Ω(n2 + ) for = 1 • and 4(n/2)3 ≤ cn3 for c = 1/2. • T(n) = Θ(n3). • Ex. T(n) = 4T(n/2) + n2/lgn • a = 4, b = 2, f (n) = n2/lgn • nlogba= n2 • Master method does not apply.
Master Method – Examples • T(n) = 16T(n/4)+n • a = 16, b = 4, nlogba = nlog416 = n2. • f(n) = n = O(nlogba-) = O(n2- ), where = 1 Case 1. • Hence, T(n) = (nlogba ) = (n2). • T(n) = T(3n/7)+ 1 • a = 1, b=7/3, and nlogba = nlog7/31 = n0 = 1 • f(n) = 1 = (nlogba) Case 2. • Therefore, T(n) = (nlogba lg n) = (lg n)
Simplified Master Theorem Let a 1 and b >1 be constants and let T(n) be the recurrence T(n) = a T(n/b) + c nk defined for n 0. 1. If a > bk, then T(n) = ( nlogba ). 2. Ifa = bk, then T(n) = ( nk lg n ). 3. If a < bk, then T(n) = ( nk ).
Examples • T(n) = 16T(n/4) + n • a = 16, b = 4, k=1 • 16>41 a > bk • T(n) = ( nlogba ) = ( nlog416 ) • T(n) =(n2) • T(n) = T(3n/7) + 1 • a = 1, b=7/3, k = 0 • 1 = (7/3)0 1 = 1 a = bk • T(n) = ( nk lg n ) = ( n0 lg n ) • T(n) = ( lg n ).