The Story That Explains the Master Theorem
At every level of this hierarchy somebody still has to do paperwork — divide the order, then later combine the boxes. That paperwork cost is f(n).
Here is the only question that ever matters for the total bill: where does most of the money go? Is it eaten up by the army of tiny cooks at the bottom, or by the paperwork at the top, or is it spread evenly across every level? Answer that, and you've solved the recurrence. That single question is the Master Theorem.
Most divide-and-conquer algorithms — merge sort, binary search, Karatsuba multiplication, Strassen's matrix product — describe their running time with a recurrence of the same shape. The Master Theorem (Chapter 4 of Cormen, Leiserson, Rivest & Stein, Introduction to Algorithms) is a recipe that reads that shape and hands you a tight Θ-bound without any algebra.
Compare two quantities: the work piled up at the leaves of the recursion tree, captured by nlogba, against the per-call divide-and-combine work f(n). Whichever grows faster decides the answer. They tie? Then every level costs the same and you pay a logarithmic number of times.
Anatomy of a Recurrence
The Master Theorem applies to recurrences of exactly this form, where a ≥ 1
and b > 1 are constants and f(n) is asymptotically positive:
The whole theorem hinges on one derived quantity: nlogba. CLRS calls this the watershed function — it measures the total work done by all the leaves. Computing logba is always your first move.
Three Ways to Solve a Recurrence
CLRS gives three tools. The Master Theorem is the fastest, but it grew out of the recursion-tree picture — understanding that picture is what makes the theorem stop feeling like memorisation.
aT(n/b)+f(n) shape.
The Recursion Tree — Where the Theorem Comes From
Picture merge sort: T(n) = 2T(n/2) + cn. Each node does cn work
to merge, then splits into two half-sized children. Watch what the cost does at each level.
The animated dot below traces the running total as we sweep down the tree.
Every level costs exactly cn. There are lg n + 1 levels. Multiply: Θ(n lg n). This perfectly balanced tree is the signature of Case 2.
The tree has height logbn, and each node has a children, so the
bottom level holds alogbn = nlogba
leaves. That algebraic identity is the heart of the watershed function — it's
literally a leaf count.
The Master Theorem — The Three Cases
Let p = logba. Compare f(n) with the watershed
np. Exactly one of three things is true, and each gives a tight bound.
The same three cases, viewed as a contest between two stacks of work:
The glowing bar is the winner that sets the asymptotic cost. In Case 2 the bars match, so the log factor breaks the tie.
A 4-Step Recipe for Applying the Theorem
Worked Example 1 — Merge Sort (Case 2)
Worked Example 2 — T(n) = 9T(n/3) + n (Case 1)
This is the textbook Case-1 example from CLRS.
Worked Example 3 — T(n) = 3T(n/4) + n log n (Case 3)
Case 3 is the only one with a second hoop: the regularity condition. Here is the full check, also straight from CLRS.
It is tempting to declare Case 3 the moment f(n) looks bigger. But the bound Θ(f(n)) only holds if the work shrinks geometrically as you go down the tree — that's exactly what a·f(n/b) ≤ c·f(n) guarantees. For the polynomial and polynomial-times-log f(n) you meet in practice, it almost always holds; pathological f(n) can fail it.
A Cheat Sheet of Common Recurrences
| Algorithm / Recurrence | a | b | nlogba | f(n) | Case | T(n) |
|---|---|---|---|---|---|---|
| Binary search — T(n/2)+Θ(1) | 1 | 2 | n0=1 | 1 | 2 | Θ(log n) |
| Tree traversal — 2T(n/2)+Θ(1) | 2 | 2 | n | 1 | 1 | Θ(n) |
| Merge sort — 2T(n/2)+Θ(n) | 2 | 2 | n | n | 2 | Θ(n log n) |
| Karatsuba — 3T(n/2)+Θ(n) | 3 | 2 | n1.585 | n | 1 | Θ(n1.585) |
| CLRS Case 1 — 9T(n/3)+n | 9 | 3 | n2 | n | 1 | Θ(n2) |
| Strassen — 7T(n/2)+Θ(n2) | 7 | 2 | n2.807 | n2 | 1 | Θ(n2.807) |
| 4T(n/2)+n2 | 4 | 2 | n2 | n2 | 2 | Θ(n2 log n) |
| CLRS Case 3 — 3T(n/4)+n log n | 3 | 4 | n0.793 | n log n | 3 | Θ(n log n) |
Python — A Master Theorem Solver
Here is a compact solver that classifies the case and reports the Θ-bound for any recurrence whose f(n) has the form ne·(log n)k.
from math import log, isclose
def master_theorem(a, b, f_exp, f_log=0):
"""Solve T(n) = a*T(n/b) + f(n), where f(n) = n^f_exp * (log n)^f_log."""
assert a >= 1 and b > 1, "Master Theorem needs a >= 1 and b > 1"
crit = log(a) / log(b) # watershed exponent p = log_b(a)
if f_exp < crit: # f(n) smaller -> leaves dominate
return f"Case 1 -> Theta(n^{crit:.4g})"
elif isclose(f_exp, crit): # tie -> one extra log factor
k = f_log + 1
return f"Case 2 -> Theta(n^{crit:.4g} * log^{k} n)"
else: # f(n) larger -> root dominates
return f"Case 3 -> Theta(f(n)) [verify regularity!]"
# (name, a, b, exponent of n in f(n), power of log in f(n))
tests = [
("Merge Sort", 2, 2, 1, 0),
("Binary Search", 1, 2, 0, 0),
("Case 1 (CLRS)", 9, 3, 1, 0),
("Strassen", 7, 2, 2, 0),
("Case 3 (CLRS)", 3, 4, 1, 1),
]
for name, a, b, fe, fl in tests:
print(f"{name:14s}: {master_theorem(a, b, fe, fl)}")
Proving Case 2 by Summing the Tree
This second snippet makes the recursion-tree picture concrete: it prints the work at every level for merge sort. Watch how every line lands on the same number — that's Case 2.
from math import log
def level_costs(a, b, n, f):
"""Print the work done at each level of the recursion tree."""
depth = int(log(n, b)) # tree height = log_b(n)
total = 0
for i in range(depth + 1):
nodes = a ** i # a^i nodes at level i
size = n / (b ** i) # each subproblem has size n / b^i
cost = nodes * f(size) # work at this level
total += cost
print(f" level {i:2d}: {nodes:>6} nodes x f(n/{b**i:<4}) = {cost:,.0f}")
print(f" TOTAL work across the tree ~ {total:,.0f}")
return total
# Merge sort: T(n) = 2T(n/2) + n, with n = 1024
level_costs(2, 2, 1024, lambda x: x)
Eleven identical levels of 1,024 each. That's n × (lg n + 1) with n = 1024 and lg 1024 = 10. The flat profile is the experimental fingerprint of Case 2, exactly as the animated tree predicted.
When the Master Theorem Fails
The theorem is powerful but not universal. CLRS is explicit that gaps exist between the cases, and that some recurrences don't fit the form at all.
T(n) = 2T(n/2) + n log n. Here p = 1 and f(n) = n log n is larger than n,
but only by a log factor — not by nε. No case applies.
(The recursion tree gives Θ(n log2n).)
T(n) = T(n−1) + n (worst-case quicksort) shrinks by subtraction, not
division. The Master Theorem doesn't apply — reach for the recursion tree or
substitution instead.
The Three Methods Compared
| Method | How it works | Best for | Limitation |
|---|---|---|---|
| Substitution | Guess a bound, prove it by induction | Confirming a bound rigorously | Needs a good initial guess |
| Recursion tree | Draw the tree, sum cost per level, add levels | Intuition + generating a guess | Informal; arithmetic slips are easy |
| Master method | Match to one of three cases, read the answer | Clean aT(n/b)+f(n) forms | Fails on gaps, uneven / subtractive splits |
Golden Rules
T(n) = aT(n/b)+f(n).
Subtractive recurrences like T(n−1)+f(n) or uneven splits need the recursion
tree or substitution method.