0% found this document useful (0 votes)
64 views2 pages

Divide and Conquer

This document discusses divide-and-conquer recurrences, which model algorithms that divide a problem into smaller subproblems, solve those independently, and combine the solutions. It analyzes the recurrence T(n)=aT(n/b)+n, showing it has runtime Θ(nlogban) if a>b, Θ(nlogn) if a=b, and Θ(n) if a<b. This generalizes to T(n)=aT(n/b)+cnd, with runtimes of Θ(nlogban), Θ(nlogn), Θ(nd) depending on the values of a and b.

Uploaded by

Maria Kusuma
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
64 views2 pages

Divide and Conquer

This document discusses divide-and-conquer recurrences, which model algorithms that divide a problem into smaller subproblems, solve those independently, and combine the solutions. It analyzes the recurrence T(n)=aT(n/b)+n, showing it has runtime Θ(nlogban) if a>b, Θ(nlogn) if a=b, and Θ(n) if a<b. This generalizes to T(n)=aT(n/b)+cnd, with runtimes of Θ(nlogban), Θ(nlogn), Θ(nd) depending on the values of a and b.

Uploaded by

Maria Kusuma
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

Divide-and-Conquer Recurrences (Draft)

(Updated May 8, 2013)


We will now discuss a completely different type of recurrences. These recurrences come up very often in
the analysis of algorithms based on the divide-and-conquer method. In this method, the original instance is
divided into b smaller pieces, the solutions are computed independently for these b pieces and then combined
together to obtain the solution of the original instance. Such divide-and-conquer algorithms include: binary
search, merge-sort, quick-sort, algorithms for integer multiplication (minimizing the number of bit operations),
matrix multiplication, and many other.
Assumption: For simplicity, we will assume that n, the input size, is of the form n = bk , where b 2 is an
integer. In most cases b = 2, but in some applications other values of b will be used. This way, each time we
divide n by b, the result will be integer.
Note: For divide-and-conquer recurrences, we typically do not need the exact solution, but only the asymptotic solution. (Since these recurrences represent the running time, asymptotic solutions are sufficient. Plus,
the exact solutions are often difficult to determine.)
Merge-sort. In Merge-Sort, we divide the sequence into equal halves, sort them recursively, and then merge
them together. Merging two sorted sequences of length n/2 takes n comparisons. So the recurrence is:
T (n) =

2T (n/2) + n,

and, say, for n = 1 assume T (1) = 1.


As before, let n be a power of 2, and applying the recurrence to T (n/2), we have
T (n) = 2T (n/2) + n
= 2[2T (n/4) + n/2] + n
= 4T (n/4) + 2n.
We can repeat this substitution again, and again, up to log n times:
T (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).

A more general case. Lets solve a more general recurrence:


T (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

a[aT (n/b2 ) + n/b] + n

a2 T (n/b2 ) + (a/b)n + n

...
=

aj T (n/bj ) + n[(a/b)j1 + ... + (a/b)2 + (a/b) + 1]

...
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

You might also like