Sub topic 1
What Divide and Conquer Means: Divide, Conquer, Combine
Divide and conquer is a way to design algorithms. You split a problem into smaller problems of the same kind, solve those recursively, and then build the full answer from their answers. Most of the algorithms in this tutorial follow this pattern, so learning to spot its three steps is the first skill to build.
Prerequisites
This chapter assumes you can:
- Read and write basic Python
- Trace a recursive function by hand
- Use Big-O notation to describe running time
- Work with lists, including slicing and indexing
Proof by induction is used in later chapters. You do not need it here.
The Three Steps
Every divide-and-conquer algorithm has three parts:
- Divide. Split the input into smaller subproblems. Usually there are two halves, but the number and size can vary.
- Conquer. Solve each subproblem by calling the same algorithm on it. When a subproblem is small enough, solve it directly. That direct case is called the base case.
- Combine. Merge the subproblem answers into the answer for the original input.
A Worked Example: Maximum of a List
Here is a small problem that shows each step clearly: find the largest element in a non-empty list.
def max_dc(a, lo, hi):
# Largest element of the non-empty slice a[lo:hi]
if hi - lo == 1: # base case: one element
return a[lo]
mid = (lo + hi) // 2 # divide
left_max = max_dc(a, lo, mid) # conquer left half
right_max = max_dc(a, mid, hi) # conquer right half
return max(left_max, right_max) # combine
nums = [4, 17, 3, 9, 12, 8]
print(max_dc(nums, 0, len(nums))) # 17The divide step is cheap because it only computes mid. The combine step compares two numbers, which takes constant time. Passing lo and hi as indices avoids copying slices.
Where the Work Happens
Algorithms differ in which step does the real work:
| Algorithm | Divide cost | Combine cost |
|---|---|---|
| Max of a list (above) | O(1) | O(1) |
| Merge sort | O(1) | O(n) merge |
| Quicksort | O(n) partition | O(1) |
| Binary search | O(1) | none, one half is discarded |
Ask yourself where the effort goes when you read a new algorithm. The answer tells you how to analyze it, which later chapters do with recurrences.
Common Errors
- Missing or wrong base case. If the base case is wrong, the recursion may never stop, or it may index an empty range.
- Subproblems that do not shrink. If
midis computed so that one half equals the whole range, the recursion loops forever. - Forgetting cross-boundary answers. Some problems have answers that span both halves. The combine step must account for them, as the maximum subarray chapter shows.
Exercise
In the function max_dc, name the divide, conquer, and combine steps, and state the cost of the divide and combine steps.
Show answer
Divide is computing mid = (lo + hi) // 2, which costs O(1). Conquer is the two recursive calls on a[lo:mid] and a[mid:hi]. Combine is max(leftmax, rightmax), which costs O(1).
Exercise
Rewrite maxdc as sumdc so it returns the sum of a non-empty slice a[lo:hi]. Which lines change?
Show answer
Only the base case and the combine step change in meaning. The base case still returns a[lo], and the final line becomes return leftsum + rightsum, where leftsum and rightsum are the results of the two recursive calls.