Divide-and-conquer algorithms like merge sort, binary search, and quick sort all lead to a recurrence of the same shape:
T(n) = 2T(n/2) + n
You could solve it by expanding it by hand or drawing a recursion tree, but for this family of recurrences there's a much faster path.
The Master Theorem is a formula that reads the Big O of most divide-and-conquer recurrences straight off their structure, with no expansion or induction.
The Master Theorem applies to recurrences of the form:
T(n) = aT(n/b) + f(n)
Where:
a: number of subproblems each call splits intob: factor the input shrinks by, so each subproblem has size n/bf(n): cost of the work done outside the recursive calls (dividing, merging, or combining)The whole point is to find the asymptotic complexity directly, without expanding the recurrence by hand.
Consider a problem of size n.
Each recursive call breaks it into a subproblems of size n/b, and spends some extra effort f(n) outside recursion (like merging or partitioning).
The total time is the sum of all the work done across every level of recursion, which splits into two parts: the work inside the subproblems, and the work outside recursion.
The Master Theorem simply compares which of those two parts dominates: the recursive work or the combining work. Whichever wins decides the answer.
We analyze recurrences of the form:
Everything hinges on one quantity, the watershed:
This is the total work done at the bottom of the recursion tree. A tree that branches a ways and is log_b n levels deep has a^(log_b n) leaves, which equals n^(log_b a), and each leaf does constant work. So n^(log_b a) measures the recursive side of the cost, and the three cases all come down to comparing the combine work f(n) against it: is f(n) smaller, equal, or larger?
Condition:
In words, the combine work f(n) grows polynomially slower than the recursive work n^(log_b a). The ε > 0 is what makes "slower" mean slower by a factor of some power of n, not just by a constant or a log.
Result:
Intuition: almost all the time is spent inside the recursion. The combine work is negligible by comparison, so the leaf work n^(log_b a) sets the complexity.
Example:
Here: a = 8, b = 2 → log₂8 = 3
Condition
The recursive work and the combine work grow at the same rate.
Result:
Intuition: every level of the recursion tree does roughly the same amount of work, and there are log n levels, so the total is that per-level work times log n.
Example:
Here:
If
and the regularity condition a × f(n/b) ≤ c × f(n) holds for some constant c < 1 and all sufficiently large n,
then:
Intuition: the combine step f(n) does so much work that the recursive calls become insignificant, so f(n) sets the complexity. The regularity condition rules out oddly behaved f(n) and guarantees the top-level cost keeps dominating all the way down.
Example:
Here:
The watershed is:
So f(n) = Θ(1) = Θ(n⁰), which matches the watershed → Case 2: T(n) = Θ(log n)
The watershed is:
So f(n) = Θ(n¹), which matches the watershed → Case 2: T(n) = Θ(n log n)
This isn't in Master Theorem form: the subproblem size is n−1, not n/b. The input shrinks by subtraction, not division, so there's no b to work with and the theorem doesn't apply.
Expanding it by hand instead gives T(n) = 1 + 2 + 4 + ... + 2^(n−1), which is Θ(2ⁿ).
Here:
This is how Strassen’s algorithm beats the classic O(n³) approach.
Even though the combine step is linear, the recursion spawns so many subproblems that the leaf work n² swamps it.
The master theorem is widely applicable but not universal. It assumes a specific shape: equal-sized subproblems, a division-based split, and an f(n) that is polynomially comparable to the watershed n^(log_b a). It breaks down when any of those assumptions is violated.
You cannot use it when:
T(n) = T(n/2) + T(n/3) + n. The single a and b no longer describe the recursion. (The Akra-Bazzi method handles these.)T(n) = T(n−1) + n. There's no n/b to plug in, so you fall back to expansion.T(n) = 2T(n/2) − n. The theorem assumes a non-negative combine cost.n^(log_b a), like T(n) = 2T(n/2) + n log n. Here the watershed is n, and n log n beats it by only a log factor, not by a factor of n^ε, so none of the three cases apply. (This is different from T(n) = 2T(n/2) + log n, which the theorem does solve: log n is polynomially smaller than n, so Case 1 gives Θ(n).)In these cases, fall back to the recursion tree or substitution methods from the previous chapter.
For most divide-and-conquer recurrences, the Master Theorem lets you read off the complexity in one step, without drawing a recursion tree or running induction. For the cases where it does not apply, the substitution and recursion-tree methods from the previous chapter fill the gap.
Together with Big O notation, best/worst/average case analysis, amortized analysis, and recurrence relations, this completes the foundation for reasoning about how any algorithm scales as input grows.
10 quizzes