Download presentation
Presentation is loading. Please wait.
1
The master method The master method applies to recurrences of the form T(n) = a T(n/b) + f (n), where a 1, b > 1, and f is asymptotically positive.
2
f (n/b) Idea of master theorem f (n/b) (1) … Recursion tree: … f (n)f (n) a f (n/b 2 ) … a h = log b n f (n)f (n) a f (n/b) a 2 f (n/b 2 ) … #leaves = a h = a log b n = n log b a n log b a (1)
3
Three common cases Compare f (n) with n log b a : 1. f (n) = O(n log b a – ) for some constant > 0. f (n) grows polynomially slower than n log b a (by an n factor). Solution: T(n) = (n log b a ).
4
f (n/b) Idea of master theorem f (n/b) (1) … Recursion tree: … f (n)f (n) a f (n/b 2 ) … a h = log b n f (n)f (n) a f (n/b) a 2 f (n/b 2 ) … n log b a (1) C ASE 1: The weight increases geometrically from the root to the leaves. The leaves hold a constant fraction of the total weight. (n log b a )
5
Three common cases Compare f (n) with n log b a : 2. f (n) = (n log b a ) f (n) and n log b a grow at similar rates. Solution: T(n) = (n log b a lg n).
6
f (n/b) Idea of master theorem f (n/b) (1) … Recursion tree: … f (n)f (n) a f (n/b 2 ) … a h = log b n f (n)f (n) a f (n/b) a 2 f (n/b 2 ) … n log b a (1) C ASE 2: The weight is approximately the same on each of the log b n levels. (n log b a lg n)
7
Three common cases (cont.) Compare f (n) with n log b a : 3. f (n) = (n log b a + ) for some constant > 0. f (n) grows polynomially faster than n log b a (by an n factor), and f (n) satisfies the regularity condition that a f (n/b) c f (n) for some constant c < 1. Solution: T(n) = ( f (n) ).
8
f (n/b) Idea of master theorem f (n/b) (1) … Recursion tree: … f (n)f (n) a f (n/b 2 ) … a h = log b n f (n)f (n) a f (n/b) a 2 f (n/b 2 ) … n log b a (1) C ASE 3: The weight decreases geometrically from the root to the leaves. The root holds a constant fraction of the total weight. ( f (n))
9
Examples Ex. T(n) = 4T(n/2) + n a = 4, b = 2 n log b a = n 2 ; f (n) = n. C ASE 1: f (n) = O(n 2 – ) for = 1. T(n) = (n 2 ). Ex. T(n) = 4T(n/2) + n 2 a = 4, b = 2 n log b a = n 2 ; f (n) = n 2. C ASE 2: f (n) = (n 2 lg 0 n), that is, k = 0. T(n) = (n 2 lg n).
10
Examples Ex. T(n) = 4T(n/2) + n 3 a = 4, b = 2 n log b a = n 2 ; f (n) = n 3. C ASE 3: f (n) = (n 2 + ) for = 1 and 4(cn/2) 3 cn 3 (reg. cond.) for c = 1/2. T(n) = (n 3 ). Ex. T(n) = 4T(n/2) + n 2 /lg n a = 4, b = 2 n log b a = n 2 ; f (n) = n 2 /lg n. Master method does not apply. In particular, for every constant > 0, we have n (lg n).
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.