Sub topic 6
The Master Theorem for T(n) = aT(n/b) + f(n)
This chapter assumes you can use Big-O, Big-Omega, and Big-Theta notation, and that you know the identity log_b a = ln a / ln b. It also builds on the recursion trees from the earlier chapter. Here you will learn the theorem's statement and how to read its three parameters from a recurrence. Later chapters cover each case in detail.
The form the theorem handles
The master theorem solves recurrences of this form:
T(n) = aT(n/b) + f(n)
Each symbol describes part of a divide-and-conquer algorithm:
| Symbol | Meaning | Requirement |
|---|---|---|
| a | Number of recursive calls made by one invocation | a ≥ 1, a constant |
| b | Factor by which the input shrinks in each call | b > 1, a constant |
| f(n) | Work done outside the recursive calls (splitting, combining) | asymptotically positive |
Every subproblem must have the same size, n/b. If a recurrence splits into unequal pieces or shrinks by subtraction, as in T(n − 1), it does not have this form.
The statement
Let n^(logb a) be the critical exponent term. It counts the leaves of the recursion tree, because the tree has depth logb n and a^(logb n) = n^(logb a). Compare f(n) against it:
- Case 1. If f(n) = O(n^(logb a − ε)) for some constant ε > 0, then T(n) = Θ(n^(logb a)).
- Case 2. If f(n) = Θ(n^(logb a)), then T(n) = Θ(n^(logb a) log n).
- Case 3. If f(n) = Ω(n^(log_b a + ε)) for some constant ε > 0, and a·f(n/b) ≤ c·f(n) for some constant c < 1 and all large n, then T(n) = Θ(f(n)).
In case 1, f(n) must be smaller than the critical term by a polynomial factor n^ε. In case 3, it must be larger by a polynomial factor. Case 3 also needs the extra inequality, called the regularity condition.
Reading off a, b, and f(n)
The first step is always to identify the parameters and compute log_b a:
| Recurrence | a | b | f(n) | log_b a |
|---|---|---|---|---|
| Merge sort: 2T(n/2) + n | 2 | 2 | n | 1 |
| Binary search: T(n/2) + 1 | 1 | 2 | 1 | 0 |
| Karatsuba: 3T(n/2) + n | 3 | 2 | n | ≈ 1.585 |
| Strassen: 7T(n/2) + n² | 7 | 2 | n² | ≈ 2.807 |
This script computes the critical exponents:
import math
examples = {
"merge sort": (2, 2),
"binary search": (1, 2),
"Karatsuba": (3, 2),
"Strassen": (7, 2),
}
for name, (a, b) in examples.items():
print(f"{name}: a={a}, b={b}, log_b a = {math.log(a, b):.3f}")Exercise
For T(n) = 4T(n/2) + n², identify a, b, f(n), and log_b a. Which case appears to apply, and what bound does it give?
Show answer
a = 4, b = 2, f(n) = n², and log_2 4 = 2. Then f(n) = Θ(n^2) matches the critical term exactly, so case 2 applies and T(n) = Θ(n² log n).
Exercise
Can the master theorem be applied directly to T(n) = T(n − 1) + n? Explain why or why not.
Show answer
No. The subproblem has size n − 1. The theorem needs size n/b for a constant b > 1, so this recurrence is not in the required form. Unrolling it gives Θ(n²).