Sub topic 6
The Master Theorem and Its Three Cases
This chapter assumes you can read Big-O, Big-Omega, and Big-Theta notation, write a recurrence for a recursive algorithm, and sketch a recursion tree as in the previous chapter. The master theorem gives the answer a recursion tree would give for a common family of recurrences, without drawing the tree each time.
The Form the Theorem Accepts
The master theorem solves recurrences of this shape:
T(n) = a · T(n/b) + f(n)
The symbols mean:
| Symbol | Meaning | Constraint |
|---|---|---|
| a | number of recursive calls | a ≥ 1 |
| b | factor by which the input shrinks | b > 1 |
| f(n) | work done outside the recursive calls (divide plus combine) | asymptotically positive |
Every subproblem must have the same size n/b. Floors and ceilings such as ⌈n/2⌉ do not change the result, so you can ignore them.
The Critical Exponent
Everything turns on one number, the critical exponent:
c* = log_b a
The recursion tree has a^(logb n) leaves, and a^(logb n) = n^(log_b a) = n^(c). So n^(c) is the total cost of the leaves. The theorem compares f(n), the work at the root, against n^(c*), the work at the leaves. Whichever grows polynomially faster dominates. If they match, every level does about the same work and you pay a log factor for the number of levels.
You can see this in the tree. Level i does a^i · f(n/b^i) work. When f(n) = n^d, that equals n^d · (a/b^d)^i, which is a geometric series with ratio r = a/b^d:
- r > 1 (d < c*): the per-level cost grows toward the leaves, and the last level dominates.
- r = 1 (d = c*): every level costs the same, and there are about log_b n levels.
- r < 1 (d > c*): the per-level cost shrinks, and the root dominates.
The Three Cases
Case 1: the leaves dominate. If f(n) = O(n^(c − ε)) for some constant ε > 0, then T(n) = Θ(n^(c)).
Case 2: the levels balance. If f(n) = Θ(n^(c) · log^k n) for some constant k ≥ 0, then T(n) = Θ(n^(c) · log^(k+1) n). The most common version has k = 0: f(n) = Θ(n^(c)) gives T(n) = Θ(n^(c) log n).
Case 3: the root dominates. If f(n) = Ω(n^(c* + ε)) for some ε > 0, and the regularity condition a · f(n/b) ≤ c · f(n) holds for some constant c < 1 and all large n, then T(n) = Θ(f(n)).
Applying It to Algorithms in This Tutorial
| Algorithm | Recurrence | c* | Case | Result |
|---|---|---|---|---|
| Binary search | T(n/2) + Θ(1) | 0 | 2 (k = 0) | Θ(log n) |
| Fast exponentiation | T(n/2) + Θ(1) | 0 | 2 (k = 0) | Θ(log n) |
| Merge sort | 2T(n/2) + Θ(n) | 1 | 2 (k = 0) | Θ(n log n) |
| Naive split multiplication | 4T(n/2) + Θ(n) | 2 | 1 | Θ(n²) |
| Karatsuba | 3T(n/2) + Θ(n) | ≈ 1.585 | 1 | Θ(n^1.585) |
| Strassen | 7T(n/2) + Θ(n²) | ≈ 2.807 | 1 | Θ(n^2.807) |
| Quadratic combine | 2T(n/2) + Θ(n²) | 1 | 3 | Θ(n²) |
Compare the naive multiplication row with the Karatsuba row. The only change is that a drops from 4 to 3, and that change alone moves c* from 2 to about 1.585. In Case 1 the number of subproblems sets the running time, and the combine work does not matter. Karatsuba and Strassen both exploit this.
A Solver for Polynomial-Times-Log Driving Functions
Most recurrences you meet have f(n) = n^d · log^k n. For that family the regularity condition in Case 3 holds automatically: a · f(n/b) ≤ (a/b^d) · f(n), and a/b^d < 1 whenever d > c*. The function below encodes the theorem for this family.
import math
def fmt(p, k):
parts = []
if not math.isclose(p, 0, abs_tol=1e-9):
parts.append("n" if math.isclose(p, 1) else f"n^{p:.4g}")
if k == 1:
parts.append("log n")
elif k > 1:
parts.append(f"log^{k} n")
return "Theta(" + (" ".join(parts) or "1") + ")"
def master(a, b, d, k=0):
"""Solve T(n) = a*T(n/b) + Theta(n^d * log(n)^k), a >= 1, b > 1, d >= 0, k >= 0."""
if a < 1 or b <= 1 or d < 0 or k < 0:
raise ValueError("need a >= 1, b > 1, d >= 0, k >= 0")
crit = math.log(a, b)
if math.isclose(d, crit, abs_tol=1e-9):
return "Case 2: " + fmt(crit, k + 1)
if d < crit:
return "Case 1: " + fmt(crit, 0)
return "Case 3: " + fmt(d, k)
print(master(1, 2, 0)) # Case 2: Theta(log n)
print(master(2, 2, 1)) # Case 2: Theta(n log n)
print(master(3, 2, 1)) # Case 1: Theta(n^1.585)
print(master(7, 2, 2)) # Case 1: Theta(n^2.807)
print(master(2, 2, 2)) # Case 3: Theta(n^2)
print(master(2, 2, 1, k=1)) # Case 2: Theta(n log^2 n)The tolerance in math.isclose matters. math.log(8, 2) can return a value a tiny bit off from 3.0, and an exact == test would then put a Case 2 recurrence in Case 1 or Case 3.
Common Errors and Where the Theorem Fails
These mistakes come up often:
- Unequal splits. T(n) = T(n/3) + T(2n/3) + n has two subproblem sizes, so no single b exists. Use a recursion tree, which gives Θ(n log n), or the Akra–Bazzi method.
- Subtractive recurrences. T(n) = T(n − 1) + n does not shrink by a factor, so the theorem says nothing about it. Its solution is Θ(n²).
- Non-constant a. T(n) = n · T(n/2) + n fails because a must be a constant.
- Skipping regularity. Case 3 also needs the regularity check. f(n) = n² is safe. A driving function that oscillates, such as n² · (2 + sin n), can break it.
- Swapping a and b. In merge sort, a counts the calls (2) and b is the shrink factor (2). Because they are equal there, the swap goes unnoticed. In T(n) = 3T(n/4) + n log n the swap gives a wrong answer. With the correct values, c* = log_4 3 ≈ 0.79 < 1, so Case 3 gives Θ(n log n).
Exercise
Solve T(n) = 9T(n/3) + n using the master theorem, and name the case.
Show answer
a = 9 and b = 3, so c* = log_3 9 = 2. f(n) = n = O(n^(2 − ε)) with ε = 1, so Case 1 applies and T(n) = Θ(n²).
Exercise
Solve T(n) = 4T(n/2) + n² log n.
Show answer
c* = log_2 4 = 2, and f(n) = Θ(n² log^1 n), so Case 2 applies with k = 1. T(n) = Θ(n² log² n).
Exercise
An algorithm splits its input into 8 subproblems of size n/2 and combines them in Θ(n³) time. Someone suggests cutting the combine step to Θ(n²). Does that change the asymptotic running time? Explain.
Show answer
c* = log_2 8 = 3. With f(n) = n³ you are in Case 2, so T(n) = Θ(n³ log n). With f(n) = n² you are in Case 1, so T(n) = Θ(n³). The faster combine step removes only the log factor. To go below n³ you need fewer subproblems, which is the idea behind Strassen's algorithm.
Exercise
Why can't the master theorem solve T(n) = 2T(n/2) + n log n using only Cases 1 and 3, and what is the answer?
Show answer
c* = 1, and n log n is larger than n by only a log factor, not by n^ε, so Case 3 does not apply. Case 1 needs f to be smaller, which it is not. The extended Case 2 with k = 1 covers it: T(n) = Θ(n log² n).