The Master Theorem
Most divide-and-conquer recurrences have the same shape, , and their recursion trees always come out in one of three pictures: the levels shrink, stay level, or grow. The master theorem records the answer for each picture once and for all, so that for a large family of recurrences solving them takes a few seconds of comparison. This lesson states it in a general form, explains why it holds and works through examples, each checked numerically.
The Divide-and-Conquer Recurrences lesson gives the simple version for . The version here also handles logarithmic factors and spells out the conditions that the simple version hides.
The watershed function
Fix constants and , and a function that is positive for large . In the recursion tree of
the bottom level has leaves. The function is called the watershed function: it is the total cost of the leaves, and the whole theorem is a comparison between , the cost of the root, and this watershed. The exponent is the critical exponent.
| Recurrence | watershed | ||
|---|---|---|---|
| 2 | 2 | ||
| 9 | 3 | ||
| 1 | |||
| 7 | 2 |
The statement
Master theorem. Let and be constants, let be asymptotically positive, and let for large , with read as either or and constant on small inputs. Then:
- If for some constant , then .
- If for some constant , then .
- If for some constant , and for some constant and all large , then .
In terms of the tree:
- Case 1, leaves win. is polynomially smaller than the watershed: the level costs grow geometrically going down, and the leaves dominate.
- Case 2, a tie. matches the watershed up to a factor . All about levels contribute roughly equally, which adds one more factor of . With this is the familiar .
- Case 3, root wins. is polynomially larger, and the extra regularity condition says the level costs really do shrink geometrically. Then the root dominates.
The word "polynomially" matters. In case 1, must be smaller by a factor , not merely smaller; a factor of is not enough. The gaps this leaves are the subject of When the Master Theorem Fails.
Proof sketch (exact powers of b)
For the recursion tree gives exactly
the leaves plus the internal levels. Call the sum .
- Case 1. With , the term is at most , an increasing geometric series with ratio . Its sum is of its last term, which is .
- Case 2. Each term is , and over levels is (it is a sum of -th powers of , up to the constant factor ).
- Case 3. Regularity gives , a decreasing geometric series, so . Also and dominates the leaves.
Handling floors and ceilings adds technical work but changes nothing.
Using it: a recipe
- Read off , and , and compute the critical exponent .
- Compare with . Is the ratio a negative power of , a power of , or a positive power of ?
- In case 3, check regularity. For with it always holds: , and exactly because .
The program applies the recipe to functions of the form , which cover most algorithms in practice.
import math
def term(p, k):
"""Write n^p lg^k n the way a person would."""
parts = []
if abs(p) > 1e-12:
parts.append("n" if abs(p - 1) < 1e-12 else f"n^{p:.3g}")
if k:
parts.append("lg n" if k == 1 else f"lg^{k} n")
return " ".join(parts) or "1"
def master(a, b, p, k=0):
"""Solve T(n) = a T(n/b) + n^p lg^k n with k >= 0."""
e = math.log(a, b) # critical exponent log_b a
if abs(p - e) < 1e-12:
return "case 2: Theta(" + term(e, k + 1) + ")"
if p < e:
return "case 1: Theta(" + term(e, 0) + ")"
return "case 3: Theta(" + term(p, k) + ")" # regularity holds for these f
examples = [
("binary search T(n/2) + 1", 1, 2, 0, 0),
("merge sort 2T(n/2) + n", 2, 2, 1, 0),
(" 9T(n/3) + n", 9, 3, 1, 0),
(" T(2n/3) + 1", 1, 1.5, 0, 0),
(" 2T(n/4) + sqrt n", 2, 4, 0.5, 0),
(" 2T(n/2) + n lg n", 2, 2, 1, 1),
(" 16T(n/4) + n^2", 16, 4, 2, 0),
(" 4T(n/2) + n^3", 4, 2, 3, 0),
("Karatsuba 3T(n/2) + n", 3, 2, 1, 0),
("Strassen 7T(n/2) + n^2", 7, 2, 2, 0),
]
for name, a, b, p, k in examples:
print(f"{name:34} -> {master(a, b, p, k)}")binary search T(n/2) + 1 -> case 2: Theta(lg n)
merge sort 2T(n/2) + n -> case 2: Theta(n lg n)
9T(n/3) + n -> case 1: Theta(n^2)
T(2n/3) + 1 -> case 2: Theta(lg n)
2T(n/4) + sqrt n -> case 2: Theta(n^0.5 lg n)
2T(n/2) + n lg n -> case 2: Theta(n lg^2 n)
16T(n/4) + n^2 -> case 2: Theta(n^2 lg n)
4T(n/2) + n^3 -> case 3: Theta(n^3)
Karatsuba 3T(n/2) + n -> case 1: Theta(n^1.58)
Strassen 7T(n/2) + n^2 -> case 1: Theta(n^2.81)
Read a few lines carefully:
- : the watershed is , and , so case 1 with : .
- : here and , so the watershed is . Case 2 with gives , like binary search. Nothing requires to be an integer.
- : the watershed is , a tie, so .
- : the watershed is , and , case 2 with : . The simple version of the theorem, which only knows , cannot do this one.
- : the watershed is , and . Regularity: , so works. Case 3: .
Checking the answers numerically
A theorem is only as useful as your confidence in applying it. Compute each recurrence exactly at with and divide by the predicted order of growth. If the prediction is right, the ratio settles towards a positive constant.
import math
def T(a, b, f, n):
return 1 if n == 1 else a * T(a, b, f, n // b) + f(n)
lg = lambda n: n.bit_length() - 1 # exact lg for powers of 2
cases = [
("9T(n/3) + n", 9, 3, lambda n: n, lambda n: n**2),
("2T(n/4) + sqrt n", 2, 4, lambda n: math.isqrt(n), lambda n: math.sqrt(n) * lg(n)),
("2T(n/2) + n lg n", 2, 2, lambda n: n * lg(n), lambda n: n * lg(n)**2),
("16T(n/4) + n^2", 16, 4, lambda n: n * n, lambda n: n * n * lg(n)),
("4T(n/2) + n^3", 4, 2, lambda n: n**3, lambda n: n**3),
]
print("recurrence ratio T(n) / prediction at n = b^4, b^8, b^12, b^16")
for name, a, b, f, g in cases:
print(f"{name:18}", " ".join(f"{T(a, b, f, b**m) / g(b**m):.4f}" for m in (4, 8, 12, 16)))recurrence ratio T(n) / prediction at n = b^4, b^8, b^12, b^16
9T(n/3) + n 1.4938 1.4999 1.5000 1.5000
2T(n/4) + sqrt n 0.6250 0.5625 0.5417 0.5312
2T(n/2) + n lg n 0.6875 0.5781 0.5486 0.5352
16T(n/4) + n^2 0.6250 0.5625 0.5417 0.5312
4T(n/2) + n^3 1.9375 1.9961 1.9998 2.0000
Every row levels off. Two converge quickly (to and to ); the three case-2 rows creep towards , because their exact solutions carry a lower-order term: for example solves to exactly . That slow drift is typical of case 2, and it is a reminder that says nothing about how fast the ratio settles.
In practice: the master theorem only needs up to . So "the merge costs " is all the information required to conclude for merge sort. The exact constants (14 comparisons for 7 elements, and so on) need the exact recurrence, as in Merge Sort.
Test yourself
What is the watershed function of , and which case applies?
- , case 2
- , case 1
- , case 1
- , case 3
Solve with the master theorem.
- The theorem does not apply
In case 3 with and , why does the regularity condition always hold?
- Because and
- Because is increasing
- Because
- It does not always hold for polynomials
Which recurrence has the solution ?