Divide-and-Conquer Recurrences
Divide-and-conquer algorithms split a problem of size into subproblems of size , solve them recursively, and combine the results with extra work:
Merge sort (), binary search (), Karatsuba multiplication () and Strassen's matrix multiplication () all fit. Instead of unrolling each one, use a general theorem.
The recursion tree
Draw the recursion as a tree. Level has subproblems of size , doing work in total. There are levels and leaves. So the outcome is a contest between the leaves, , and the top-level work :
- if the work per level shrinks going down, the root dominates: ;
- if it stays equal, every level counts: ;
- if it grows, the leaves dominate: .
The master theorem
Let (the critical exponent). For :
| Case | Condition | Solution |
|---|---|---|
| 1 | ||
| 2 | ||
| 3 |
| Algorithm | , , | Result | |
|---|---|---|---|
| binary search | 1, 2, | 0 | case 2: |
| merge sort | 2, 2, | 1 | case 2: |
| Karatsuba | 3, 2, | case 1: | |
| Strassen | 7, 2, | case 1: | |
| tree traversal | 2, 2, | 1 | case 1: |
| "halve and scan" | 1, 2, | 0 | case 3: |
The Akra–Bazzi theorem generalizes this to unequal splits, , and also shows that floors and ceilings in the recurrence do not change the asymptotic answer.
Checking numerically
Compute exactly and divide by the predicted growth: the ratio should level off at a constant.
import math
from functools import lru_cache
def make(a, b, g):
@lru_cache(None)
def T(n):
return 1 if n <= 1 else a * T(n // b) + g(n)
return T
cases = {
"merge sort 2T(n/2)+n ~ n log n": (make(2, 2, lambda n: n), lambda n: n * math.log2(n)),
"Karatsuba 3T(n/2)+n ~ n^1.585": (make(3, 2, lambda n: n), lambda n: n ** math.log2(3)),
"Strassen 7T(n/2)+n^2 ~ n^2.807": (make(7, 2, lambda n: n * n), lambda n: n ** math.log2(7)),
"halve+scan T(n/2)+n ~ n": (make(1, 2, lambda n: n), lambda n: n),
}
for name, (T, pred) in cases.items():
ratios = [T(2 ** k) / pred(2 ** k) for k in (8, 12, 16, 20)]
print(f"{name}: " + " ".join(f"{r:.3f}" for r in ratios))merge sort 2T(n/2)+n ~ n log n: 1.125 1.083 1.062 1.050
Karatsuba 3T(n/2)+n ~ n^1.585: 2.922 2.985 2.997 2.999
Strassen 7T(n/2)+n^2 ~ n^2.807: 2.318 2.332 2.333 2.333
halve+scan T(n/2)+n ~ n: 1.996 2.000 2.000 2.000
Each row approaches a constant, confirming the predicted order of growth.
Why it matters: Karatsuba
Schoolbook multiplication of two -digit numbers takes digit operations. Karatsuba splits each number into halves, , and computes , and : three half-size products instead of four, since the middle term is . Going from to changes the exponent from 2 to 1.585. CPython uses Karatsuba for large integers.
def karatsuba(x, y):
if x < 10 or y < 10:
return x * y
m = max(len(str(x)), len(str(y))) // 2
B = 10 ** m
x1, x0 = divmod(x, B)
y1, y0 = divmod(y, B)
z2 = karatsuba(x1, y1)
z0 = karatsuba(x0, y0)
z1 = karatsuba(x1 + x0, y1 + y0) - z2 - z0
return z2 * B * B + z1 * B + z0
a, b = 31415926535897932384, 27182818284590452353
print(karatsuba(a, b))
print(karatsuba(a, b) == a * b)853973422267356706510386982199967699552
True
In software
The master theorem is the quick way to read the complexity of recursive code. It also explains design choices: a recursive algorithm that does work per call but makes only one recursive call on half the input (quickselect's average case, binary-search-like scans) is linear overall, while one making two calls on halves with linear work is .
Exercises
Exercise 1: Apply the theorem
Solve , and .
Answer
, , .
Solution
. With : case 1. With : case 2. With : case 3.
Exercise 2: Ternary search
An algorithm splits the input into 3 parts, recurses on one, and does constant extra work. What is its running time?
Answer
.
Solution
: , , , case 2: .
Exercise 3: Count operations (code)
Count the single-digit multiplications Karatsuba performs for two -digit numbers () and compare with and schoolbook .
Answer
Exactly , versus for schoolbook.
Solution
with gives .
M = lambda n: 1 if n == 1 else 3 * M(n // 2)
for k in range(1, 9):
n = 2 ** k
print(n, M(n), 3 ** k, 4 ** k)2 3 3 4
4 9 9 16
8 27 27 64
16 81 81 256
32 243 243 1024
64 729 729 4096
128 2187 2187 16384
256 6561 6561 65536
Going further: The Master Theorem and When the Master Theorem Fails in the Algorithm Theory track continue this topic.
Test yourself
What is the critical exponent for ?
- 2
- 3
- 4
- 8
Merge sort's recurrence falls into which case?
- Case 1 (leaves dominate)
- Case 2 (all levels equal)
- Case 3 (root dominates)
- None
Why is Karatsuba faster than schoolbook multiplication?
- It uses floating point
- It replaces 4 half-size products by 3
- It avoids recursion
- It uses the FFT