Sub topic 7
The Key Comparison: f(n) Versus n^(log_b a)
This chapter assumes you can read the recurrence T(n) = aT(n/b) + f(n), identify a, b, and f(n), and use the identity a^(logb n) = n^(logb a). Every master theorem case comes from one comparison, between f(n) and the function n^(log_b a). This chapter explains what that function measures and how to make the comparison correctly.
What n^(log_b a) Counts
Picture the recursion tree. Each node has a children, and the problem size shrinks by a factor of b at each level. The tree therefore has depth log_b n, and the number of leaves is:
a^(logb n) = n^(logb a)
Each leaf does constant work, so n^(logb a) is the total work at the bottom of the tree. The root does f(n) work. The comparison asks which end of the tree carries more work. The exponent logb a is called the critical exponent, and n^(log_b a) is often called the watershed function.
Worked Comparisons
| Recurrence | a | b | log_b a | f(n) | f(n) relative to n^(log_b a) |
|---|---|---|---|---|---|
| Merge sort: 2T(n/2) + n | 2 | 2 | 1 | n | same order |
| Binary search: T(n/2) + 1 | 1 | 2 | 0 | 1 | same order |
| Karatsuba: 3T(n/2) + n | 3 | 2 | ≈ 1.585 | n | polynomially smaller |
| 2T(n/2) + n² | 2 | 2 | 1 | n² | polynomially larger |
When f(n) = Θ(n^d), the comparison reduces to comparing the exponent d with log_b a:
import math
def compare(a, b, d):
c = math.log(a, b) # critical exponent log_b(a)
if math.isclose(d, c):
return c, "same order"
return c, "polynomially smaller" if d < c else "polynomially larger"
cases = [
("merge sort", 2, 2, 1),
("binary search", 1, 2, 0),
("Karatsuba", 3, 2, 1),
("2T(n/2) + n^2", 2, 2, 2),
]
for name, a, b, d in cases:
c, rel = compare(a, b, d)
print(f"{name}: log_b a = {c:.3f}, f(n) = n^{d} is {rel}")The Common Error: "Larger" Must Mean Polynomially Larger
Cases 1 and 3 require a gap of a factor n^ε for some constant ε > 0. Being asymptotically larger or smaller is not enough. Consider T(n) = 2T(n/2) + n log n. Here n^(log_b a) = n, and n log n is larger than n. It is not polynomially larger, though, because log n grows more slowly than n^ε for every ε > 0. That rules out case 3. The recurrence belongs to the extended case 2, which a later chapter covers.
A reliable test is to look at the ratio f(n) / n^(log_b a):
- If the ratio is Θ(1), the two functions have the same order.
- If the ratio is O(n^(-ε)) or Ω(n^ε), the gap is polynomial.
- If the ratio is a power of log n, or anything else between those, plain cases 1 to 3 do not apply.
Exercise
For T(n) = 4T(n/2) + n² log n, compute n^(logb a), form the ratio f(n) / n^(logb a), and say whether the gap is polynomial.
Show answer
log2 4 = 2, so n^(logb a) = n². The ratio is (n² log n) / n² = log n. That is not Ω(n^ε) for any ε > 0, so the gap is not polynomial. Cases 1 and 3 do not apply. This is an extended case 2 recurrence.
Exercise
For T(n) = 7T(n/2) + n², is f(n) polynomially smaller, of the same order, or polynomially larger than n^(log_b a)?
Show answer
log2 7 ≈ 2.807, so n^(logb a) ≈ n^2.807. The ratio n² / n^2.807 = n^(-0.807), which is O(n^(-ε)) with ε = 0.8. So f(n) is polynomially smaller.