Sub topic 7
The Key Comparison: f(n) Versus n^(log_b a)
This chapter assumes you know Big-Theta notation, logarithm identities, and geometric series. It also assumes the recursion tree method and the master theorem's statement from the previous chapter. There you met the form T(n) = aT(n/b) + f(n), with a ≥ 1 subproblems, each of size n/b, plus f(n) work outside the recursive calls. Every case of the master theorem rests on one comparison: how f(n) grows relative to n^(log_b a). This chapter explains where that function comes from, how to compute it, and how to compare it with f(n) correctly.
Where n^(log_b a) Comes From
Draw the recursion tree for T(n) = aT(n/b) + f(n). Each node splits into a children, and the subproblem size shrinks by a factor of b at each level. The size reaches a constant after about log_b n levels, so that is the tree's depth.
The number of leaves is a raised to the depth:
a^(logb n) = b^((logb a)(logb n)) = n^(logb a)
The middle step rewrites a as b^(logb a). Each leaf costs Θ(1), so the leaves together cost Θ(n^(logb a)). This number does not depend on f(n) at all. It comes only from the shape of the tree.
The exponent c = log_b a is called the critical exponent. The function n^c is sometimes called the watershed function, because the theorem's cases split according to which side of it f(n) falls.
Computing the Critical Exponent
Compute c = log_b a = ln a / ln b. Compare f(n) with n^c. Do not compare it with n, and do not compare a with b directly.
| Recurrence | a | b | c = log_b a | n^c |
|---|---|---|---|---|
| Merge sort: 2T(n/2) + n | 2 | 2 | 1 | n |
| Binary search: T(n/2) + 1 | 1 | 2 | 0 | 1 |
| Karatsuba: 3T(n/2) + n | 3 | 2 | ≈ 1.585 | n^1.585 |
| Strassen: 7T(n/2) + n² | 7 | 2 | ≈ 2.807 | n^2.807 |
| 4T(n/2) + n² | 4 | 2 | 2 | n² |
| 9T(n/3) + n | 9 | 3 | 2 | n² |
When a = 1, c = 0 and n^c = 1. A single recursive call produces a "tree" that is a path with one leaf.
Why the Comparison Decides Everything
Take f(n) = n^d. Level j of the tree has a^j nodes, and each one costs (n/b^j)^d. The level's total is:
a^j · (n/b^j)^d = n^d · (a / b^d)^j
So the level costs form a geometric series with ratio r = a / b^d. The sign of log_b r = c − d tells you which way the series goes:
- If d < c, then r > 1. Level costs grow toward the leaves, and the bottom level dominates.
- If d = c, then r = 1. Every level costs n^d, and there are about log_b n levels.
- If d > c, then r < 1. Level costs shrink, and the root dominates.
This is why the theorem uses n^(log_b a) as the yardstick. Comparing d with c is the same as asking whether the geometric series grows, stays flat, or shrinks. The next three chapters work out each case.
"Polynomially" Smaller or Larger
The basic theorem requires f(n) to be smaller or larger than n^c by a factor of at least n^ε for some constant ε > 0. Being smaller or larger by some arbitrary amount does not count. A practical test is to look at the ratio f(n) / n^c:
| Ratio f(n) / n^c | Meaning |
|---|---|
| O(n^(−ε)) for some ε > 0 | Polynomially smaller |
| Θ(1) | Same order |
| Ω(n^ε) for some ε > 0 | Polynomially larger |
| Grows or shrinks, but slower than every n^ε (for example log n) | In a gap |
The standard trap is T(n) = 2T(n/2) + n log n. Here c = 1, and the ratio is (n log n) / n = log n. The ratio grows, so f is asymptotically larger than n. But log n grows more slowly than n^ε for every ε > 0. The basic case 3 does not apply. The extended case 2 handles this recurrence, and a later chapter covers it.
You can check numerically that log n eventually loses to even a tiny power of n:
import math
# log2(n) / n^0.1 for n = 2^p: the ratio eventually tends to 0
for p in [10, 20, 50, 100, 200]:
n = 2.0 ** p
print(p, round(math.log2(n) / n ** 0.1, 4))The output is 5.0, 5.0, 1.5625, 0.0977, 0.0002. The ratio stays large for practical input sizes, which is why the gap is easy to miss when you reason from intuition.
A Classifier for Polynomial f(n)
When f(n) = Θ(n^d), the comparison reduces to comparing two numbers. The helper below computes c and the level ratio r = a/b^d and reports which side f(n) falls on. It uses math.isclose because logb a is computed in floating point, so a value such as log2 4 may not come out as exactly 2.0.
import math
def compare(a, b, d):
"""Compare f(n) = n^d with n^(log_b a) for T(n) = a*T(n/b) + n^d."""
if a < 1 or b <= 1:
raise ValueError("requires a >= 1 and b > 1")
c = math.log(a, b) # critical exponent
r = a / b ** d # ratio between consecutive level costs
if math.isclose(d, c):
verdict = "balanced: f(n) = Theta(n^c)"
elif d < c:
verdict = "f(n) polynomially smaller than n^c"
else:
verdict = "f(n) polynomially larger than n^c"
return c, r, verdict
examples = [
("merge sort", 2, 2, 1),
("binary search", 1, 2, 0),
("Karatsuba", 3, 2, 1),
("Strassen", 7, 2, 2),
("2T(n/2) + n^2", 2, 2, 2),
]
for name, a, b, d in examples:
c, r, verdict = compare(a, b, d)
print(f"{name:15} c={c:.3f} r={r:.3f} {verdict}")Output:
merge sort c=1.000 r=1.000 balanced: f(n) = Theta(n^c)
binary search c=0.000 r=1.000 balanced: f(n) = Theta(n^c)
Karatsuba c=1.585 r=1.500 f(n) polynomially smaller than n^c
Strassen c=2.807 r=1.750 f(n) polynomially smaller than n^c
2T(n/2) + n^2 c=1.000 r=0.500 f(n) polynomially larger than n^cIn every row, r > 1 lines up with d < c, r = 1 with d = c, and r < 1 with d > c. This classifier handles only pure polynomials. It reports nothing about log factors, and it does not check the regularity condition that case 3 also requires.
Common Errors
- Comparing f(n) with n. For 4T(n/2) + n^1.5, f looks "bigger than linear," but c = 2, so f is polynomially smaller than n^c.
- Inverting the logarithm. The exponent is logb a, not loga b. For 9T(n/3) + n, c = log_3 9 = 2, not 0.5.
- Comparing a with b. Whether a > b says nothing by itself. For 4T(n/2) + n² and 2T(n/2) + n², a > b in one and a = b in the other, but you only learn the case after you compare f with n^c.
- Calling a log-factor gap polynomial. n log n versus n, or n² / log n versus n², fall in the gaps.
- Reading constants as exponents. In 2T(n/2) + 5n, the 5 does not matter. f(n) = Θ(n), so d = 1.
- Trusting floating-point equality. Use a tolerance, or compute c exactly when a is a power of b.
Exercise
For each recurrence, compute c = log_b a and say whether f(n) is polynomially smaller than, the same order as, or polynomially larger than n^c, or falls in a gap: (a) T(n) = 8T(n/2) + n², (b) T(n) = 3T(n/3) + n, (c) T(n) = 2T(n/4) + n, (d) T(n) = 4T(n/2) + n²/log n.
Show answer
(a) c = 3; n² is polynomially smaller (ratio n^(−1)). (b) c = 1; same order (ratio 1). (c) c = log_4 2 = 0.5; n is polynomially larger (ratio n^0.5). (d) c = 2; the ratio is 1/log n, which tends to 0 more slowly than every n^(−ε), so it falls in a gap of the basic theorem.
Exercise
For T(n) = 3T(n/2) + n, write the total cost of recursion-tree level j and state whether the level costs grow, stay flat, or shrink with depth. Relate your answer to the comparison of f(n) with n^(log_2 3).
Show answer
Level j has 3^j nodes, each costing n/2^j, so the level total is n·(3/2)^j. The ratio 3/2 > 1 means level costs grow toward the leaves. This matches d = 1 < c = log_2 3 ≈ 1.585: f(n) is polynomially smaller than n^c, so the leaves dominate.
Exercise
A classmate says "T(n) = 2T(n/2) + n log n is case 3 because n log n grows faster than n." What is wrong with this reasoning?
Show answer
Case 3 requires f(n) = Ω(n^(c+ε)) for some constant ε > 0. Here c = 1 and f(n)/n = log n, which grows more slowly than n^ε for every ε > 0. So f is larger than n^c, but not polynomially larger, and the basic case 3 does not apply. The extended case 2 gives Θ(n log² n).