You are on page 1of 25

Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.

Divide and Conquer


Definition
– Recursion lends itself to a general problem-solving
technique (algorithm design) called divide & conquer
• Divide the problem into 1 or more similar sub-problems
• Conquer each sub-problem, usually using a recursive call
• Combine the results from each sub-problem to form a solution
to the original problem
– Algorithmic Pattern:
DC( problem )
solution = 
if ( problem is small enough )
solution = problem.solve() Divide
else
children = problem.divide() Conquer
for each c in children
solution = solution + c.solve()
return solution

Combine
Applicability
– Use the divide-and-conquer algorithmic pattern when
ALL of the following are true:
• The problem lends itself to division into sub-problems of the
same type

• The sub-problems are relatively independent of one another


(ie, no overlap in effort)

• An acceptable solution to the problem can be constructed


from acceptable solutions to sub-problems
Well-Known Uses
– Searching
• Binary search
– Sorting
• Merge Sort
• Quick Sort
– Mathematics
• Polynomial and matrix multiplication
• Exponentiation
• Large integer manipulation
– Points
• Closest Pair
• Merge Hull
• Quick Hull
MergeSort
Sort a collection of n items into increasing order

mergeSort(A) {
if(A.size() <= 1)
return A;
else {
left = A.subList(0, A.size()/2);
right = A.subList(A.size()/2, A.size());
sLeft = mergeSort(left);
sRight = mergeSort(right);
newA = merge(sLeft, sRight);
return newA;
}
MergeSort
3 8 5 4 1 7 6 2

3 8 5 4 1 7 6 2

3 4 5 8 1 2 6 7

1 2 3 4 5 6 7 8
QuickSort
Sort a collection of n items into increasing order
– Algorithm steps:
• Break the list into 2 pieces based on a pivot
– The pivot is usually the first item in the list
• All items smaller than the pivot go in the left and all items larger go
in the right
• Sort each piece (recursion again)
• Combine the results together
QuickSort
3 8 5 4 2 7 6 1

2 1 3 8 5 4 7 6

1 2 3 4 5 6 7 8

1 2 3 4 5 6 7 8
To Develop a Divide & Conquer
Algorithm
1. Determine how to obtain the solution to an instance
from the solution to one or more smaller instances

2. Determine the terminal conditions that the smaller


instances are approaching

3. Determine the solution in the case of the terminal


conditions
Binary Search
Find the location of an element in a sorted collection of n
items (assuming it exists)

binarySearch( A, low, high, target )


mid = (high + low) / 2
if ( A[mid] == target )
return mid
else if ( A[mid] < target )
return binarySearch(A, mid+1, high, target)
else
return binarySearch(A, low, mid-1, target)
Matrix Multiplication

1 2 3 1 2 3
30 36 42
4 5 6 4 5 6
66 81 96
7 8 9

1x2 + 2x5 + 3x8 = 2 + 10 + 24 = 36

• rows from the first matrix


• columns from the second matrix
Matrix Multiplication
– “inner” dimensions must match
– result is “outer” dimension
– Examples:
• 2  3 X 3  3 = 2x3
• 3  4 X 4  5 = 3x5
• 2  3 X 4  3 = cannot multiply
Matrix Multiplication - Iterative
public static Matrix mult(Matrix m1, Matrix m2) {
Matrix result = new Matrix();

for (int i=0; i<m1.numRow(); i++) {


for (int j=0; j<m2.numCol(); j++) {
double total = 0.0;
for (int k=0; k<m1.numCol(); k++) {
total +=
(m1.m[i][k]*m2.m[k][j]);
}
result.m[i][j] = total;
}
}
return result;
}
Matrix Multiplication
Divide & Conquer:
– Easy to break into subproblems
– Multiplication can be performed blockwise
A B E F AE + BG AF + BH
X×Y= C D
×
G H
=
CE + DG DF + DH

– Divide & Conquer steps…


– Divide each matrix into 4 blocks
– Recursively compute each multiplication
– Combine multiplications with a few additions and
assemble final matrix
– Run-time?
Matrix Multiplication
Algebra Tricks:
A B E F
X×Y= ×
C D G H

P1 = A(F – H)
P5 + P4 – P2 + P6 P1 + P2
P2 = (A + B)H X×Y =
P3 + P4 P1 + P5 – P3 – P7
P3 = (C + D)E
P4 = D(G – E)
P5 = (A + D)(E + H)
P6 = (B – D)(G + H)
P7 = (A – C)(E + F)
Closest-Pair Problem
Find the closest pair of points in a collection of n points in
a given plane (use Euclidean distance)
• Assume n = 2k
• If n = 2, answer is distance between these two points
• Otherwise,
– sort points by x-coordinate
– split sorted points into two piles of equal size (i.e., divide
by vertical line l)
• Three possibilities: closest pair is
– in Left side
– in Right side
– between Left and Right side

Application: Closest pair of airplanes


at a given altitude.
Closest-Pair Problem
• Closest pair is between Left and Right?
– Let d = min(LeftMin, RightMin)
– need to see if there are points in Left and Right that are closer
than d
– only need to check points within 2d of l
– sort points by y-coordinate
– can only be eight points in the 2d x d slice
Convex Hull
Definitions:
• A convex hull of a set of 2D points is the shape taken by a
rubber band stretched around nails pounded into the plane at
each point.
• A convex hull of a set S of points is the smallest polygon P for
which each point of S is either on the boundary or in the
interior of P.
• A convex hull of a set S of points is the smallest set   S | if
x1, x2   and   [0,1] then x = x1 + (1 - )x2  

Applications:
• collision avoidance in robotics;
• mapping the surface of an object (like an asteroid);
• analyzing images of soil particles;
• estimating percentage of lung volume occupied by pneumonia
Convex Hull
QuickHull Algorithm:
• This is actually a half-space hull finder
– You start with the leftmost (A) and rightmost (B) points
and all the points above them
– It finds the half hull to the top side
• One can then find the entire hull by running the same
algorithm on the bottom points
Convex Hull
QuickHull Algorithm:
• Pick the point C that is furthest from the line AB

• Form two lines – AC and CB and place all points above AC into one
list and all points above CB into another list.
Ignore the inner
points
Convex Hull
QuickHull Algorithm:
• Recursively conquer each list of points.

• Return the line AB for each eventual empty list of points


• Combine resulting AB lines into polygon
Convex Hull
MergeHull Algorithm:
• This convex hull algorithm is similar to Merge Sort
(obviously given the name!)
• To make the algorithm as fast as possible we first
need to sort the points from left to right (based on
x-coordinate)
• After that we apply a divide-and-conquer technique
Convex Hull
MergeHull Algorithm
• Split the points into two equal halves

• Conquer each half to get convex hull for that half (stop when
you reach a single point)
Convex Hull
MergeHull Algorithm:
• Combine the 2 small hulls together into 1 large hull
– First find the top and bottom tangent lines to the hulls
» A special “walking” algorithm is used to find the
tangent lines in linear time
– Then use these tangent lines to create the full hull
When to avoid Divide-and-Conquer?

1. An instance of size n is divided into 2 or


more instances each almost of size n
2. An instance of size n is divided into almost
n instance of size n/c, where c is a constant
3. When smaller instances are related

You might also like