The Master Theorem: Three Cases, Worked Examples and Limits
The master theorem is a shortcut for solving one family of recurrences, the kind produced by divide-and-conquer algorithms that split a problem into equal parts. Given the recurrence, you compare two numbers and read off a Theta bound, with no tree drawing and no induction. This guide states the theorem in its common polynomial form, works six examples, explains why some answers are written with a logarithm in the exponent, and lists the recurrences it cannot handle.
The form it applies to
The theorem applies to recurrences of exactly this shape:
T(n) = a T(n/b) + Theta(n^d), with constants a >= 1, b > 1 and d >= 0.
- a is the number of recursive calls;
- n/b is the size of each call, the same for all of them;
- n^d is the work done outside the recursive calls (splitting the input and combining the answers).
a, b and d must be constants, not functions of n. The halves may really be floor(n/b) or ceil(n/b); that rounding does not change the answer.
The three cases
Compute log_b a, the exponent that describes how fast the number of subproblems grows relative to their size, and compare it with d:
| Case | Condition | Answer | Where the work is |
|---|---|---|---|
| 1 | log_b a > d | Theta(n^(log_b a)) | at the leaves of the recursion tree |
| 2 | log_b a = d | Theta(n^d log n) | spread evenly over the log n levels |
| 3 | log_b a < d | Theta(n^d) | at the root |
The reason comes from the recursion tree: level L does n^d x (a / b^d)^L work, a geometric series that grows, stays flat or shrinks depending on whether a / b^d is above, equal to or below 1, which is the same as comparing log_b a with d. The recurrence relations guide works those trees level by level, and the Recursion Tree Calculator prints the level sums for any a, b, d and n.
Textbooks such as "Introduction to Algorithms" by Cormen, Leiserson, Rivest and Stein state the theorem for a general f(n) in place of n^d. Case 3 there carries an extra "regularity condition", a f(n/b) <= k f(n) for some constant k < 1. When f(n) = n^d it always holds in Case 3, because a (n/b)^d = (a / b^d) n^d and a / b^d < 1.
Six worked examples
The quoted answers are the exact output of the Master Theorem Calculator, which takes a, b and d and compares log_b a with d.
Binary search: a = 1, b = 2, d = 0
One call on half the items, constant work. log_2 1 = 0 = d, so Case 2: Θ(log n). With d = 0, n^d log n is just log n.
Merge sort: a = 2, b = 2, d = 1
Two calls on halves, linear merge. log_2 2 = 1 = d, so Case 2: Θ(n log n).
Three half-size multiplications: a = 3, b = 2, d = 1
Multiplying two n-digit numbers by splitting each into halves naively needs four half-size products. Karatsuba's method gets by with three, plus a linear amount of addition and shifting. log_2 3 is about 1.585, which is greater than d = 1, so Case 1: Θ(n^log2(3)) ≈ Θ(n^1.58). That beats the Theta(n^2) of schoolbook multiplication.
Eight half-size matrix products: a = 8, b = 2, d = 2
Multiplying two n by n matrices by splitting each into four n/2 by n/2 blocks takes eight block products and Theta(n^2) work to add them up. log_2 8 = 3 > 2, so Case 1: Θ(n^3), no better than three nested loops. Strassen's method uses seven block products instead, and (7, 2, 2) gives "Θ(n^log2(7)) ≈ Θ(n^2.81)".
Root-heavy: a = 2, b = 2, d = 2
Two half-size calls, but quadratic work at each node. log_2 2 = 1 < 2, so Case 3: Θ(n^2). The top-level work is so large that the recursion adds only a constant factor.
Balanced at a higher power: a = 4, b = 2, d = 2
log_2 4 = 2 = d, so Case 2, and the calculator prints "Θ(n^2 log n)".
Why the exponent is written exactly
The calculator prints "Θ(n^log2(3))" and puts "≈ Θ(n^1.58)" beside it, rather than giving only the decimal. That is deliberate: n^1.58 and n^log2(3) = n^1.58496... are different growth classes. Any difference in the exponent, however small, eventually produces an unbounded ratio. Examples use Python 3.10 or later and only the standard library. This script computes T(n) = 3T(n/2) + n exactly for powers of 2 and divides by both candidates.
import math
from functools import cache
@cache
def T(n):
"""T(n) = 3 T(n/2) + n with T(1) = 1, for n a power of 2."""
if n == 1:
return 1
return 3 * T(n // 2) + n
exact = math.log2(3)
for k in [4, 8, 12, 16, 20]:
n = 2 ** k
print(f"n=2^{k:<2} T(n)={T(n):<14} "
f"T/n^log2(3)={T(n) / n ** exact:.4f} T/n^1.58={T(n) / n ** 1.58:.4f}")
Output:
n=2^4 T(n)=211 T/n^log2(3)=2.6049 T/n^1.58=2.6410
n=2^8 T(n)=19171 T/n^log2(3)=2.9220 T/n^1.58=3.0035
n=2^12 T(n)=1586131 T/n^log2(3)=2.9846 T/n^1.58=3.1104
n=2^16 T(n)=129009091 T/n^log2(3)=2.9970 T/n^1.58=3.1665
n=2^20 T(n)=10458256051 T/n^log2(3)=2.9994 T/n^1.58=3.2130
Divided by n^log2(3), the ratio settles toward 3 (in fact T(n) = 3n^(log2 3) - 2n exactly). Divided by n^1.58, it keeps growing, slowly but without limit. So "Θ(n^1.58)" would be false; "O(n^1.59)" would be a true but loose upper bound.
What the theorem does not cover
- Subproblems of unequal size. T(n) = T(n/3) + T(2n/3) + n has no single b. A recursion tree still works (each full level sums to n and there are Theta(log n) levels, giving Theta(n log n)), and a generalization known as the Akra-Bazzi method handles such recurrences.
- Subtract-by-a-constant recurrences. T(n) = T(n - 1) + n shrinks n by 1, not by a factor. Unrolling gives n + (n - 1) + ... + 1 = Theta(n^2). Naive Fibonacci, T(n) = T(n - 1) + T(n - 2) + O(1), is also outside the theorem.
- Non-constant a or b. A recurrence like T(n) = sqrt(n) T(sqrt(n)) + n has a number of subproblems that depends on n, so the theorem does not apply.
- f(n) that is not a polynomial. Take T(n) = 2T(n/2) + n log n. Here log_b a = 1, and n log n is larger than n^1 but smaller than n^(1 + e) for every e > 0, so it fits neither Case 2 (which needs f exactly Theta(n)) nor Case 3 (which needs f polynomially larger). It falls in the gap between them. An extended version of the theorem covers it: if f(n) = Theta(n^(log_b a) x (log n)^k) for some k >= 0, then T(n) = Theta(n^(log_b a) x (log n)^(k + 1)). With k = 1 this gives Theta(n log^2 n).
The calculator takes only polynomial f(n) = n^d, so it cannot accept n log n; it also rejects a < 1 and b <= 1 with a message.
A checklist for using it
- Write the recurrence from the code (count calls, their size and the outside work).
- Check the form: equal sizes n/b, constant a >= 1 and b > 1, f(n) polynomial (or polylog times polynomial for the extended Case 2).
- Compute log_b a and compare with d.
- Write the answer with the exact exponent.
- If the form does not fit, draw the recursion tree instead.
For the algorithms these recurrences come from, see divide and conquer.
Check yourself
- T(n) = 9T(n/3) + n^2? (log_3 9 = 2 = d, Case 2, Theta(n^2 log n).)
- T(n) = 3T(n/4) + n? (log_4 3 is about 0.79 < 1, Case 3, Theta(n).)
- T(n) = 2T(n - 1) + 1? (Not in the theorem's form; unrolling gives Theta(2^n).)