Divide and Conquer
Divide and Conquer
Divide and Conquer
7 29 4 2 4 7 9 72 2 7 77 22 94 4 9 99 44
Integer Multiplication
Divide-and-Conquer
Divide-and-Conquer
Divide-and-Conquer
Divide-and conquer is a general algorithm design paradigm:
Divide: divide the input data S in two or more disjoint subsets S1, S2, Recur: solve the subproblems recursively Conquer: combine the solutions for S1, S2, , into a solution for S
Merge-Sort Review
Merge-sort on an input sequence S with n elements consists of three steps:
Divide: partition S into two sequences S1 and S2 of about n/2 elements each Recur: recursively sort S1 and S2 Conquer: merge S1 and S2 into a unique sorted sequence Algorithm mergeSort(S, C) Input sequence S with n elements, comparator C Output sequence S sorted according to C if S.size() > 1 (S1, S2) partition(S, n/2) mergeSort(S1, C) mergeSort(S2, C) S merge(S1, S2)
The base case for the recursion are subproblems of constant size Analysis can be done using recurrence equations
Divide-and-Conquer
Divide-and-Conquer
Iterative Substitution
In the iterative substitution, or plug-and-chug, technique, we iteratively apply the recurrence equation to itself and see if we can find a pattern: T ( n ) = 2T ( n / 2) + bn
b T (n) = 2T ( n / 2) + bn
if n < 2 if n 2
We can therefore analyze the running time of merge-sort by finding a closed form solution to the above equation.
That is, a solution that has T(n) only on the left-hand side.
Divide-and-Conquer
Divide-and-Conquer
Guess-and-Test Method
In the guess-and-test method, we guess a closed form solution and then try to prove it is true by induction:
b T (n) = 2T (n / 2) + bn log n if n < 2 if n 2
depth 0 1 i
Ts 1 2 2i
time bn bn bn
Divide-and-Conquer
Master Method
Many divide-and-conquer recurrence equations have the form:
T (n) = 2T (n / 2) + bn log n = 2(c(n / 2) log 2 (n / 2)) + bn log n = cn(log n log 2) 2 + bn log n = cn log n 2cn log n + cn + bn log n
2
c T (n ) = aT (n / b) + f ( n )
if n < d
if n d
1. if f (n) is O(n logb a ), then T (n) is (n logb a ) 2. if f (n) is (n logb a log k n), then T (n) is (n logb a log k +1 n) 3. if f (n) is (n logb a + ), then T (n) is ( f (n)), provided af (n / b) f (n) for some < 1.
9 Divide-and-Conquer 10
if c > b.
cn log 2 n
So, T(n) is O(n log2 n). In general, to use this method, you need to have a good guess and you need to be good at induction proofs.
Divide-and-Conquer
log
k +1
n)
2. if f ( n) is (n logb a log k n), then T (n) is (n logb a log k +1 n) 3. if f (n) is (n logb a + ), then T (n) is ( f (n)), provided af (n / b) f (n) for some < 1.
3. if f (n) is (n logb a + ), then T (n) is ( f (n)), provided af (n / b) f (n) for some < 1.
Example:
T ( n) = 4T ( n / 2) + n
Example:
T ( n) = 2T (n / 2) + n log n
Example:
T ( n) = T (n / 3) + n log n
Example:
T (n) = 8T (n / 2) + n 2
Example:
T ( n) = 9T (n / 3) + n 3
O(n3).
15
Example:
T ( n ) = T ( n / 2) + 1
(binary search)
c aT ( n / b) + f ( n )
if n < d if n d
log b a
), then T (n) is (n
k
)
log b a
2. if f ( n) is (n
log
k +1
n)
3. if f (n) is (n logb a + ), then T (n) is ( f (n)), provided af (n / b) f (n) for some < 1.
a
i =0 i=0
f (n / bi ) f (n / b i )
Example:
T (n) = 2T (n / 2) + log n
(heap construction)
= n logb aT (1) +
(log b n ) 1 i
18
Integer Multiplication
Algorithm: Multiply two n-bit integers I and J.
Divide step: Split I and J into high-order and low-order bits
I = I h 2n / 2 + I l J = J h 2n / 2 + J l
We can then define I*J by multiplying the parts and adding:
I = I h 2n / 2 + I l
J = J h 2n / 2 + J l
I * J = ( I h 2n / 2 + I l ) * ( J h 2n / 2 + J l ) = I h J h 2n + I h J l 2n / 2 + I l J h 2n / 2 + I l J l
So, T(n) = 4T(n/2) + n, which implies T(n) is O(n2). But that is no better than the algorithm we learned in grade school.
Divide-and-Conquer 19
I * J = I h J h 2 n + [( I h I l )( J l J h ) + I h J h + I l J l ]2 n / 2 + I l J l = I h J h 2 n + [( I h J l I l J l I h J h + I l J h ) + I h J h + I l J l ]2 n / 2 + I l J l = I h J h 2 n + ( I h J l + I l J h )2 n / 2 + I l J l
So, T(n) = 3T(n/2) + n, which implies T(n) is O(nlog23), by the Master Theorem. Thus, T(n) is O(n1.585).
Divide-and-Conquer 20