Sub topic 3
Proving Divide-and-Conquer Algorithms Correct
This chapter assumes you can trace recursive functions, read Big-O notation, and have seen proof by mathematical induction. It also assumes you have read the earlier chapters on the divide, conquer, combine steps and on base cases.
Why Induction Fits Recursion
A divide-and-conquer algorithm trusts its recursive calls to return correct answers on smaller inputs. Strong induction on input size makes that trust formal. With strong induction, you may assume the claim holds for every size smaller than n, and then you prove it for size n.
A proof has four parts:
- Claim. State exactly what the function returns for every valid input of size n.
- Base case. Show the claim holds for the sizes the code handles directly.
- Inductive hypothesis. Assume the claim holds for all valid inputs of size k < n.
- Inductive step. Show every recursive call gets a valid input that is strictly smaller, then show the combine step turns correct sub-results into a correct result.
A Worked Proof
This function finds the largest element in a non-empty slice by splitting it in half.
def range_max(a, lo, hi):
# Returns max(a[lo:hi]); requires hi - lo >= 1
if hi - lo == 1:
return a[lo]
mid = (lo + hi) // 2
left = range_max(a, lo, mid)
right = range_max(a, mid, hi)
return max(left, right)
print(range_max([4, 9, 2, 7, 5], 0, 5)) # 9Claim. For every n = hi − lo ≥ 1, range_max(a, lo, hi) returns the largest value in a[lo:hi].
Base case. When n = 1, the slice holds only a[lo], and the function returns it.
Inductive hypothesis. For every k with 1 ≤ k < n, the function returns the correct maximum of any slice of size k.
Inductive step. Let n ≥ 2. Then mid = (lo + hi) // 2 satisfies lo < mid < hi, so both halves have sizes between 1 and n − 1. Both halves are valid inputs and both are strictly smaller. By the hypothesis, left and right are the maxima of the two halves. Every element of the slice lies in exactly one half, so the overall maximum is the larger of the two half-maxima. That is what max(left, right) returns.
Common Errors
| Error | What goes wrong |
|---|---|
| Vague claim | "It works" cannot be checked. Name the exact return value and the input precondition. |
| Missing size check | If a split can produce a subproblem of size n, the hypothesis does not apply and the recursion may never end. |
| Invalid subproblem | A call on an empty slice breaks the precondition, even if the size shrinks. |
| Skipping the combine step | Correct sub-results do not guarantee a correct whole. The combine logic needs its own argument. |
Exercise
Suppose range_max used the base case if hi - lo <= 2: return max(a[lo:hi]). Which part of the proof must change, and does the inductive step still need lo < mid < hi?
Show answer
The base case must now cover sizes 1 and 2, which holds because Python's max returns the largest element of a non-empty slice. The inductive step then starts at n ≥ 3, where mid still satisfies lo < mid < hi, so both halves are non-empty and strictly smaller. The rest of the argument is unchanged.
Exercise
A student splits with mid = lo + (hi - lo) // 3 and recurses on a[lo:mid] and a[mid:hi]. For which sizes n ≥ 2 does the inductive step fail?
Show answer
It fails at n = 2, where (hi - lo) // 3 = 0 gives mid = lo. The left half is then empty, which breaks the precondition, and the right half has size n, so it is not smaller. For n ≥ 3, mid = lo + n // 3 with 1 ≤ n // 3 < n, so both halves are non-empty and strictly smaller.