Sub topic 6
The Master Theorem and Its Three Cases
This chapter assumes you can write a recurrence for a divide-and-conquer algorithm and that you have used a recursion tree to sum the work level by level. The master theorem turns that level-by-level summation into a lookup. Most recurrences in this tutorial can be solved with it in a few seconds.
The Form It Applies To
The theorem covers recurrences of this shape:
T(n) = a·T(n/b) + Θ(n^d), with a ≥ 1, b > 1, d ≥ 0
- a is the number of subproblems.
- b is the factor by which the input shrinks.
- n^d is the cost of dividing and combining at the top level.
The key quantity is the critical exponent log_b a. In a recursion tree, level k has a^k nodes, and each node has size n/b^k. The work at level k is therefore n^d · (a / b^d)^k. That ratio a / b^d is the only thing that matters. Either the work shrinks as you go down the tree, stays the same at every level, or grows.
The Three Cases
| Case | Condition | Which levels dominate | Result |
|---|---|---|---|
| 1 | d > log_b a | The root (work shrinks going down) | Θ(n^d) |
| 2 | d = log_b a | All levels equally | Θ(n^d log n) |
| 3 | d < log_b a | The leaves (work grows going down) | Θ(n^(log_b a)) |
Applying It in Code
The comparison is mechanical, so you can check your answers with a few lines of Python:
import math
def master(a, b, d):
c = math.log(a, b)
if math.isclose(d, c):
return f"Theta(n^{d} log n)"
if d > c:
return f"Theta(n^{d})"
return f"Theta(n^{c:.3f})"
print(master(2, 2, 1)) # merge sort: Theta(n^1 log n)
print(master(1, 2, 0)) # binary search: Theta(n^0 log n) = Theta(log n)
print(master(3, 2, 1)) # Karatsuba: Theta(n^1.585)
print(master(7, 2, 2)) # Strassen: Theta(n^2.807)
print(master(2, 2, 2)) # quadratic combine: Theta(n^2)Binary search and merge sort both fall into case 2. Karatsuba and Strassen fall into case 3. In case 3, removing even one subproblem lowers the exponent, and this is the reason those algorithms work as well as they do.
Common Errors
- Unequal splits. The theorem does not cover T(n) = T(n/3) + T(2n/3) + n. Use a recursion tree for that recurrence instead.
- Subtract-style recurrences. The theorem does not cover T(n) = T(n − 1) + n either, because the input does not shrink by a constant factor. That recurrence solves to Θ(n²).
- Non-polynomial combine costs. T(n) = 2T(n/2) + n log n does not fit the simplified form. The answer, Θ(n log² n), comes from an extended version of case 2.
- Forgetting regularity. The general version of case 3 requires a·f(n/b) ≤ k·f(n) for some k < 1. Polynomial costs always satisfy this, but other combine costs may not.
- Confusing a and b. In T(n) = 4T(n/2) + n, a is 4 and b is 2, so log_b a = 2 and the result is Θ(n²). It is not Θ(n log n).
Exercise
Solve T(n) = 4T(n/2) + n² and T(n) = 8T(n/2) + n² with the master theorem. Which case applies to each?
Show answer
For the first, log2 4 = 2 = d, so case 2 applies and T(n) = Θ(n² log n). For the second, log2 8 = 3 > 2 = d, so case 3 applies and T(n) = Θ(n³).
Exercise
Why can't the master theorem solve T(n) = 2T(n − 1) + 1?
Show answer
The subproblem size is n − 1, which is not n/b for any constant b > 1. Unrolling the recurrence shows that it grows as Θ(2^n).