You are on page 1of 2

Master Method

Definition
T (n): upper-bound of function at level n
Recurrance: expression of T (n) in terms of runtime of recursive calls.
Base case: T (1) k
n > 1 : T (n) (recursive work) + (current work)

For larger, non-base case sizes of n:


n
T (n) aT
+ O(nd )
(1)
b
There are a recursive calls on inputs of size n/b, where the work done in
the combination step of the algorithm is of the magnitude nd .
This leads to the generic Master Method for divide-and-conquer algorithms:

O(nd log n) : a = bd

T (n) =
O(nd )
: a < bd

O(nlogb a )
: a > bd

(2)

Note: In case 1, the base of the logarithm doesnt matter because the
difference is a constant factor.

Proof

Given the following recurrence:


T (n) = aT

n
b

+ cnd

T (1) = f

(3)
(4)

expanding it out a few iterations for T (n) reveals the pattern.


n
T (n) = aT
+ cnd
b
  
 n d 
n
+ cnd
= a aT 2 + c
b
b
   
 n d 
 n d 
n
= a a aT 3 + c 2
+c
+ cnd
b
b
b
n
 n d
= a3 T 3 + a2 c 2 + cnd
b
b
1

(5)
(6)
(7)
(8)

Master Method
Let k = logb n be the number of iterations in the recurrence before T
reaches its base case.

T (n) = a f +

k1
X

ai c

i=0

= ak f + cnd
= ak f + cnd

 n d

(9)

bi

k1 i
X
a
bid
i=0

(10)

k1  
X
a i

bd
i=0
 k
a

(11)

= ak f + cnd ba

1
d
b log n

a b
nd 1

= nlogb a f + cnd
a

1
bd
= nlogb a f +

calogb n cnd
a
1
bd

c
cnd
T (n) = a
+ f nlogb a a
1
1
bd
bd

(12)

(13)

(14)

(15)

Note that the geometric sum formula is not valid when a/bd = 1 so the
final formula doesnt work for that case, but it is a special case that makes
the original summation trivial to solve. The full definition of the Master
Theorem, accounting for all 3 possible cases, is as follows:



(nlogb a ) a > bd

cnd
nlogb a d
=
f + dc
(nd )
a < bd
T (n) =
ab 1
ab 1
d
n (f + c logb n) = (nd logb n)
a = bd
(16)
Superposition If the work done in each recursive call isnt perfectly
modeled by cnd because there are extra terms, use the theorem on each part
of the recurrence letting f = 0, add the solutions together, and then finally
add f nlogb a .
2

You might also like