Algorithm Pattern - DivideAndConquerCMP422
Algorithm Pattern - DivideAndConquerCMP422
▪ Divide-and-conquer is a general
algorithm design paradigm to find an
optimal solution to a problem. Its basic idea
is to decompose a given problem into two or
more similar, but simpler, subproblems, to
solve them in turn, and to compose their
solutions to solve the given :
▪ Divide/Break: divide the input data
Divide-and- S in two or more disjoint subsets S1,
S2, …
Conquer ▪ Conquer/Solve: solve the
subproblems recursively
▪ Combine/Merge: combine the
solutions for S1, S2, …, into a
solution for S
▪The base case for the recursion
are subproblems of constant size
▪Analysis can be done using
recurrence equations
Divide-and-Conquer
6
7 29 4 → 2 4 7 9
72 → 2 7 94 → 4 9
Divide-and-
Conquer 7→7 2→2 9→9 4→4
Divide-and-Conquer
Divide &
Conquer
Algorithm
Application of
& Conquer
Binary Search Merge Sort Quick Sort
Strassen's
Karatsuba
Matrix
Algorithm
multiplication
13
Merge-Sort Review
▪ Merge-sort on an input
sequence S with n elements
Algorithm mergeSort(S)
consists of three steps:
Input sequence S with n
▪ Divide: partition S into two elements
sequences S1 and S2 of about
n/2 elements each Output sequence S sorted
▪ Conquer: recursively sort S1 according to C
and S2 if S.size() > 1
▪ Combine: merge S1 and S2 into (S1, S2) partition(S, n/2)
a unique sorted sequence mergeSort(S1)
mergeSort(S2)
S merge(S1, S2)
Divide-and-Conquer
14
b if n 2
T (n) =
2T (n / 2) + bn if n 2
Recurrence tree
Master Theorem
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
= 2(2T (n / 22 )) + b(n / 2)) + bn
= 22 T (n / 22 ) + 2bn
= 23 T (n / 23 ) + 3bn
= 24 T (n / 24 ) + 4bn
= ...
= 2i T (n / 2i ) + ibn
Note that base, T(n)=b, case occurs when 2i=n. That is, i = log n.
So, T (n) = bn + bn log n
Thus, T(n) is O(n log n).
© 2014 Goodrich, Tamassia, Goldwasser Divide-and-Conquer 22