ADE Lec12 Master Theorem
ADE Lec12 Master Theorem
http://www.cs.nott.ac.uk/~pszajp/
COMP2009-ACE
The “Master Theorem”
Master Theorem (MT)
Consider recurrence relations of the form
T(n) = a T(n/b) + f(n)
• Designed for “divide and conquer” in which
problems are divided into ‘a’ instances of a
problem of size n/b.
• Aim is to be able to quickly express the “big-Oh
family” behavior of T(n) for various cases of the
values of a and b, and the scaling behavior of
f(n).
It does not cover all cases, but does cover many
useful cases.
2
Master Theorem
• No proof needed in this module.
• Just learn it, and how to apply it!
• Suggest to generate and try many examples
3
Motivations
• Consider the special case that f(n) = 0
• T(n) = a T(n/b) with T(1) = 1
• T(b) = a
• T(b2) = a2
• T(b3) = a3
• So T(bk) = ak
• Exercise (offline): prove by induction
4
Motivations
• Consider the special case that f(n) = 0
• T(n) = a T(n/b) with T(1) = 1
• Gives T(bk) = ak
• But now suppose f(n) = nc for some c
• We can ask which term dominates; the recurrence or
the f(n)?
• Put n = bk then compare
• (for clarity write “logb” as “log_b” )
• “Recurrence term”: ak = ( blog_b(a) )k.
• Note: used the identity that a=b log_b(a)
• “f term” n c = ( bk )c = ( bc )k
• So need to compare c and logb(a)
5
Master Theorem (MT): Cases
T(n) = a T(n/b) + f(n)
Results are split into 3 cases, according to
comparing the growth rate of f(n) to 𝑛^(log 𝑏 (𝑎))
7
MT: Case 1: Example
T(n) = 2 T(n/2) + d
8
MT: Case 2
T(n) = a T(n/b) + f(n)
if
f(n) is Θ(𝑛𝑐 (log 𝑛)^𝑘)
with c = logb a and 𝑘 ≥ 0
(Note: it is “c =“ and Big-Theta)
9
MT: Case 2: Example
T(n) = 2 T(n/2) + 3 n + 5
11
MT: Case 3 : Example
T(n) = 2 T(n/2) + n2
Then T(n) is Θ 𝑛2
12
Regularity Condition (case 3)
• a f(n/b) <= k f(n) for some k < 1
• Suppose f(n) = d n^c then we need
• a d (n/b)^c <= k d n^c for some k < 1
• a / b^c <= k for some k < 1
• a / b^c < 1
• a < b^c
• Now take log_b of both sides
• Need log_b(a) < c
• Which is already satisfied for case 3 to apply.
• So is not a new condition in this case (needed for more
complex cases)
• (Note: this is included for completeness; usually not relevant)
13
MT Example
• T(n) = 4 T(n/2) + d n with T(1)=1
• a=4, b=2
• so logb a = log2 4 = log2 22 = 2
• Hence is Θ( 𝑛2 )
• Matches the exact solution in previous lecture.
14
Expectations
15