Dt Structures nd Algorithms CMPSC 465 LECTURE 10 Solving recurrences Mster theorem Adm Smith S. Rskhodnikov nd A. Smith; bsed on slides by E. Demine nd C. Leiserson
Review questions Guess the solution to the recurrence: T(n)=2T(n/3)+n 3/2. (Answer: Θ(n 3/2 ).) Drw the recursion tree for this recurrence.. Wht is its height? b. Wht is the number of leves in the tree? (Answer: h=log 3 n.) (Answer: n (1/log 3).)
The mster method The mster method pplies to recurrences of the form T(n) = T(n/b) +, where 1, b > 1, nd f is symptoticlly positive, tht is >0 for ll n > n 0.
Three common cses Compre with n log b : 1. = O(n log b ε ) for some constnt ε > 0. grows polynomilly slower thn n log b (by n n ε fctor). Solution: T(n) = Θ(n log b ).
Three common cses Compre with n log b : 1. = O(n log b ε ) for some constnt ε > 0. grows polynomilly slower thn n log b (by n n ε fctor). Solution: T(n) = Θ(n log b ). 2. = Θ(n log b lg k n) for some constnt k 0. nd n log b grow t similr rtes. Solution: T(n) = Θ(n log b lg k+1 n).
Three common cses (cont.) Compre with n log b : 3. = Ω(n log b + ε ) for some constnt ε > 0. grows polynomilly fster thn n log b (by n n ε fctor), nd stisfies the regulrity condition tht f (n/b) c for some constnt c < 1. Solution: T(n) = Θ( ).
Ide of mster theorem Recursion tree: f (n/b) f (n/b) f (n/b 2 ) f (n/b 2 ) f (n/b 2 ) f (n/b)
Ide of mster theorem Recursion tree: f (n/b) f (n/b) f (n/b 2 ) f (n/b 2 ) f (n/b 2 ) f (n/b) f (n/b) 2 f (n/b 2 )
Ide of mster theorem h = log b n Recursion tree: f (n/b) f (n/b) f (n/b 2 ) f (n/b 2 ) f (n/b 2 ) f (n/b) f (n/b) 2 f (n/b 2 )
Ide of mster theorem h = log b n Recursion tree: f (n/b) f (n/b) f (n/b 2 ) f (n/b 2 ) f (n/b 2 ) f (n/b) f (n/b) 2 f (n/b 2 ) #leves = h = log bn = n log b n log b
Ide of mster theorem h = log b n Recursion tree: f (n/b) f (n/b) f (n/b 2 ) f (n/b 2 ) f (n/b 2 ) f (n/b) f (n/b) 2 f (n/b 2 ) CASE 1: The weight increses geometriclly from the root to the leves. The leves hold constnt frction of the totl weight. n log b Θ(n log b )
Ide of mster theorem h = log b n Recursion tree: f (n/b) f (n/b) f (n/b 2 ) f (n/b 2 ) f (n/b 2 ) f (n/b) f (n/b) 2 f (n/b 2 ) CASE 2: (k = 0) The weight is pproximtely the sme on ech of the log b n levels. n log b Θ(n log b lg n)
Ide of mster theorem h = log b n Recursion tree: f (n/b) f (n/b) f (n/b 2 ) f (n/b 2 ) f (n/b 2 ) f (n/b) f (n/b) 2 f (n/b 2 ) CASE 3: The weight decreses geometriclly from the root to the leves. The root holds constnt frction of the totl weight. n log b Θ( )
Exmples EX. T(n) = 4T(n/2) + n = 4, b = 2 n log b = n 2 ; = n. CASE 1: = O(n 2 ε ) for ε = 1. T(n) = Θ(n 2 ).
Exmples EX. T(n) = 4T(n/2) + n = 4, b = 2 n log b = n 2 ; = n. CASE 1: = O(n 2 ε ) for ε = 1. T(n) = Θ(n 2 ). EX. T(n) = 4T(n/2) + n 2 = 4, b = 2 n log b = n 2 ; = n 2. CASE 2: = Θ(n 2 lg 0 n), tht is, k = 0. T(n) = Θ(n 2 lg n).
Exmples EX. T(n) = 4T(n/2) + n 3 = 4, b = 2 n log b = n 2 ; = n 3. CASE 3: = Ω(n 2 + ε ) for ε = 1 nd 4(n/2) 3 cn 3 (reg. cond.) for c = 1/2. T(n) = Θ(n 3 ).
Exmples EX. T(n) = 4T(n/2) + n 3 = 4, b = 2 n log b = n 2 ; = n 3. CASE 3: = Ω(n 2 + ε ) for ε = 1 nd 4(n/2) 3 cn 3 (reg. cond.) for c = 1/2. T(n) = Θ(n 3 ). EX. T(n) = 4T(n/2) + n 2 /lg n = 4, b = 2 n log b = n 2 ; = n 2 /lg n. Mster method does not pply. In prticulr, for every constnt ε > 0, we hve n ε = ω(lg n).