2. 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()
else
children = problem.divide()
for each c in children
solution = solution + c.solve()
return solution
Divide
Conquer
Combine
3. 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
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
4. 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;
}
5. 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
6. 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)
7. Matrix Multiplication
1x2 + 2x5 + 3x8 = 2 + 10 + 24 = 36
1 2 3
4 5 6
1 2 3
4 5 6
7 8 9
30 36 42
66 81 96
• rows from the first matrix
• columns from the second matrix
8. 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;
}
9. Matrix Multiplication
Divide & Conquer:
– Easy to break into subproblems
– Multiplication can be performed blockwise
X × Y =
– 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?
A B
×
E F
=
AE + BG AF + BH
C D G H CE + DG DF + DH
10. 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.
11. 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
12. 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
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
13. 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
14. Conclusion
Divide & Conquer is a fundamental algorithmic paradigm
that breaks complex problems into smaller, manageable
subproblems, solves them recursively, and combines their
solutions for the final result. It forms the backbone of many
efficient algorithms like Merge Sort, Quick Sort, and
Binary Search, offering enhanced performance and
modularity. Despite its potential challenges, such as
recursion overhead and unbalanced divisions, Divide &
Conquer remains an essential strategy in computer science,
powering various real-world applications from data
processing to numerical simulations.