Divide and Conquer
Divide and Conquer
2T (n/2) + n,
2T (n/2) + n
4T (n/4) + 2n
8T (n/8) + 3n
...
=
2j T (n/2j ) + jn
...
=
nT (1) + n log n
(n log n).
aT (n/b) + n,
where a 1, b 2 are some integers, and we assume that the initial condition is T (1) = 1. (This does not
matter for the asymptotic solution.)
Again, we do repeated substitutions:
T (n)
aT (n/b) + n
a2 T (n/b2 ) + (a/b)n + n
...
=
...
logb n1
logb n
T (1) + n
(a/b)i
i=0
logb n1
nlogb a + n
(a/b)i
i=0
To estimate the second term and the whole expression, we have three cases.
Case 1: a = b. The first term is n. In the summation, we have logb n terms and they are all equal a/b = 1,
so the second term is n logb n. Thus we get T (n) = (n log n).
Case 2: a < b. The second term is now a geometric series with the ratio smaller than 1, so
(1). The first term is nlogb a with logb a < 1, so we get T (n) = (n).
Plogb n1
i=0
(a/b)i =
Case 3: a > b. Summing the geometric series in the second term, we get
logb n1
X
i=0
(a/b)i
(a/b)logb n 1
=
(a/b) 1
b
logb n logb n
/b
ab (a
1) =
b
logb a
/n
ab (n
1)
So
T (n) = nlogb a +
b
logb a
ab (n
n) = (nlogb a ).
This gives us the solution to the recurrence T (n) = aT (n/b) + n. The same proof works if we replace n by
cn, for some constant n. In fact, it can be generalized further, by allowing some additive term cnd , for some
d 0. Thus we get:
Theorem 1 ( Master Theorem ) Let a 1, b > 1, c > 0 and d 0. If T (n) satisfies the recurrence
T (n) = aT (n/b) + cnd ,
then
T (n) =
log a
(n b )
d
(n log n)
(nd )
for a > bd
for a = bd
for a < bd