OCR H446, section 2.2.2 (d)
Divide and conquer is a method that solves a problem by:
- Dividing it into smaller sub-problems of the same type.
- Conquering: solving each sub-problem, usually by calling the same method again (recursion). When a sub-problem is small enough (the base case), it's solved directly.
- Combining the sub-solutions into the answer to the original problem.
It differs from simple decomposition (2.2.2 (c)), where the parts are different tasks: in divide and conquer, the parts are smaller versions of the same problem.
Why it works well
- Big problems become many small ones, and small ones are easy.
- It often gives much faster algorithms: halving the problem each time gives logarithmic growth.
- The sub-problems are independent, so they can be solved concurrently (2.1.5).
- It maps naturally onto recursion (2.2.1 (b)).
Example 1: binary search
Search a sorted list by halving the part still to search each time.
def binary_search(items, target, low, high):
if low > high: # base case: not found
return -1
mid = (low + high) // 2
if items[mid] == target: # base case: found
return mid
elif target < items[mid]: # search the left half
return binary_search(items, target, low, mid - 1)
else: # search the right half
return binary_search(items, target, mid + 1, high)
- Divide: choose the middle item.
- Conquer: search one half (the other can be ignored).
- Combine: nothing to do, since the answer is passed back.
A list of 1,000 items needs at most 10 comparisons (), whereas a linear search could need 1,000. The time grows as .
Example 2: merge sort
def merge_sort(items):
if len(items) <= 1: # base case
return items
mid = len(items) // 2
left = merge_sort(items[:mid]) # divide and conquer
right = merge_sort(items[mid:])
return merge(left, right) # combine
def merge(a, b):
out = []
i = j = 0
while i < len(a) and j < len(b):
if a[i] <= b[j]:
out.append(a[i])
i += 1
else:
out.append(b[j])
j += 1
return out + a[i:] + b[j:]
Here the hard work is in combining. Merge sort takes time.
Other examples
- Quick sort: divide by a pivot, sort each side.
- Tree traversal: do the same on the left and right sub-trees.
- Towers of Hanoi.
- Fast exponentiation and fast multiplication.
- Image processing: split into blocks and process each separately.
Comparing the three methods
| Decomposition | Divide and conquer | Iteration | |
|---|---|---|---|
| Sub-problems | Different tasks | Same type, smaller | Repeated steps |
| Typical form | Several subroutines | Recursive subroutine | A loop |
Costs
- Recursion uses stack memory for every call.
- The combine step may take extra time and memory (merge sort needs extra space for the merged lists).
- For small problems, simpler methods can be faster because of the call overhead.
Worked example
Task: use divide and conquer to find the largest value in a list.
- Divide: split the list into two halves.
- Conquer: find the largest of each half by the same method.
- Combine: the answer is the larger of the two results.
- Base case: a list of one item, whose largest is that item.
def largest(items):
if len(items) == 1:
return items[0]
mid = len(items) // 2
a = largest(items[:mid])
b = largest(items[mid:])
return a if a > b else b
print(largest([4, 9, 2, 7, 5])) # 9
Trace: [4, 9, 2, 7, 5] splits into [4, 9] and [2, 7, 5]. [4, 9] gives 9. [2, 7, 5] splits into [2] and [7, 5], which give 2 and 7, so 7. The final comparison is 9 against 7, giving 9.
A simple loop would be just as good here. This shows the method, but divide and conquer pays off on problems such as sorting and searching.
Exam tip: describe the three stages (divide, conquer, combine), and state the base case. For binary search, say that half of the data is discarded each time. For merge sort, say that the hard part is the merge.
Check yourself
What are the three stages of divide and conquer?
Divide the problem into smaller sub-problems of the same type, conquer each one (usually recursively), then combine the results.
How does binary search use divide and conquer?
It compares with the middle item, then discards half the list and repeats the search on the other half, until the item is found or no items are left.
How does divide and conquer differ from ordinary decomposition?
In divide and conquer the sub-problems are smaller versions of the same problem, solved by the same method. In decomposition the parts can be completely different tasks.
Give one cost of divide and conquer
Recursion uses stack memory, and the combine step can take extra time and space.