Recurrences and the Master Theorem
Turn a divide and conquer algorithm into a recurrence, then solve it with a recursion tree or the master theorem.
Lessons in this course
Foundations
01Growth Rates and Big O- 02Recurrences and the Master Theorem
Going further
03Amortized Analysis
Where recurrences come from
A loop's cost is a sum. A recursive function's cost is a recurrence, an equation that defines in terms of on smaller inputs. Divide and conquer algorithms produce recurrences of one shape. They split a problem of size into subproblems of size , solve each recursively, and spend on splitting and combining:
Merge sort is the standard example.
def merge_sort(items):
if len(items) <= 1:
return list(items)
mid = len(items) // 2
left = merge_sort(items[:mid])
right = merge_sort(items[mid:])
return merge(left, right)
def merge(left, right):
merged = []
i = j = 0
while i < len(left) and j < len(right):
if left[i] <= right[j]:
merged.append(left[i])
i += 1
else:
merged.append(right[j])
j += 1
merged.extend(left[i:])
merged.extend(right[j:])
return mergedThe two highlighted calls each receive half the list, so and . Slicing and merge both touch every element once, so . Writing for that linear work, the recurrence is
The recursion tree
Draw the calls as a tree and add up the work level by level. Assume is a power of two.
| Level | Subproblems | Size of each | Work on this level |
|---|---|---|---|
Every level costs and there are levels, so
The tree also shows what can happen in general. Going down the tree, the work per level can shrink (the root dominates), stay equal (every level matters), or grow (the leaves dominate). The master theorem is that observation made precise.
The master theorem
Let and be constants, let be asymptotically positive, and let , where may be rounded up or down. Compare with , which is the number of leaves in the recursion tree.
- Leaves dominate. If for some constant , then .
- Balanced. If , then .
- Root dominates. If for some constant , and if for some constant and all sufficiently large , then .
The extra requirement in case 3 is the regularity condition. It says the work really does shrink by a constant factor from each level to the next. Ordinary polynomials satisfy it, but it has to be checked.
Worked examples
| Algorithm | Recurrence | Case | Result | |
|---|---|---|---|---|
| Binary search | 2 | |||
| Merge sort | 2 | |||
| Karatsuba multiplication | 1 | |||
| (no name) | 3 |
Binary search. and , so . The driving function is of the same order. Case 2 gives .
Karatsuba. Three half-size multiplications plus linear work. Since with, say, , case 1 applies and the leaves dominate. This is how the algorithm beats the schoolbook method.
The last row. Here with . Regularity: , so works. Case 3 gives .
When it does not apply
Consider . Here , and is larger, but only by a factor of , which grows more slowly than any . Case 3 fails, case 2 fails, and the theorem is silent.
The recursion tree still works. Level has subproblems of size , so the level costs . Summing over the levels :
The leaves add only more, so .
The theorem also says nothing about recurrences that are not of its shape: subproblems of unequal size, such as , or "subtract and conquer" recurrences such as . Use a tree or unrolling for those.
Exercises
Solve each recurrence, naming the case if the master theorem applies.
1.
Show the solution
, and with . Case 1: .
2.
Show the solution
, and with . Regularity: , so . Case 3: .
3. , with
Show the solution
The subproblem shrinks by subtraction, not division, so the master theorem does not apply. Unroll it instead:
This is the recurrence for selection sort, and for quicksort when every pivot happens to be the smallest element.