Sub topic 11
Extended Case 2: Recurrences with Log Factors
This chapter assumes you know the three basic master theorem cases and can sum a series like 1 + 2 + ... + L. It covers one common recurrence the basic theorem misses, and the extension that solves it.
The Gap in the Basic Theorem
Consider T(n) = 2T(n/2) + n log n. Here a = 2 and b = 2, so n^(log_b a) = n. The driving function f(n) = n log n is larger than n, but only by a log factor. Case 3 needs f(n) to be polynomially larger, meaning Ω(n^(1+ε)) for some ε > 0. No such ε works, because log n grows more slowly than every n^ε. Case 2 needs f(n) = Θ(n), which is also false. The basic theorem gives no answer.
The Extended Statement
Let c = log_b a. Suppose f(n) = Θ(n^c log^k n). Then:
| Value of k | Solution |
|---|---|
| k > -1 | T(n) = Θ(n^c log^(k+1) n) |
| k = -1 | T(n) = Θ(n^c log log n) |
| k < -1 | T(n) = Θ(n^c) |
Basic case 2 is the row with k = 0. Most textbook problems use k ≥ 0, and the rule there is simple: multiply f(n) by one extra log n.
Why the Extra Log Appears
Use the recursion tree. Level j has a^j subproblems of size n/b^j. Because a^j = (b^j)^c, the work on level j is
a^j · (n/b^j)^c · log^k(n/b^j) = n^c · log^k(n/b^j).
Measure logs in base b and write L = log_b n. Then log(n/b^j) = L − j, and the total work is
n^c · Σ (L − j)^k for j from 0 to L − 1, which is n^c · Σ i^k for i from 1 to L.
For k > -1 the sum is Θ(L^(k+1)). That gives Θ(n^c log^(k+1) n). For k = -1 the sum is the harmonic series, Θ(log L) = Θ(log log n). For k < -1 the sum converges to a constant, so only the leaves count.
Checking It Numerically
The code below evaluates T(n) = 2T(n/2) + n log₂ n exactly for powers of two and divides by n log² n. The prediction is Θ(n log² n).
import math
def T(n):
# T(n) = 2T(n/2) + n log2 n, with T(1) = 1
return 1 if n <= 1 else 2 * T(n // 2) + n * math.log2(n)
for p in range(4, 21, 4):
n = 2 ** p
print(n, round(T(n) / (n * math.log2(n) ** 2), 4))The output is:
16 0.6875
256 0.5781
4096 0.5486
65536 0.5352
1048576 0.5275The ratio settles near 0.5. That fits the tree sum: Σ i for i from 1 to p is p(p+1)/2, which is about p²/2. The constant is 1/2 and the growth rate is n log² n.
Common Errors
- Applying basic case 3 to f(n) = n^c log n. That f(n) is not polynomially larger than n^c, so case 3 does not apply.
- Adding one log in every case. When k = -1 the answer is log log n, and when k < -1 no log appears at all.
- Treating log² n as 2 log n. log² n means (log n)², while log(n²) = 2 log n.
Exercise
Solve T(n) = 2T(n/2) + n log³ n.
Show answer
c = log₂ 2 = 1, so f(n) = n^1 log³ n gives k = 3 > -1. Then T(n) = Θ(n log⁴ n).
Exercise
Solve T(n) = 2T(n/2) + n / log n.
Show answer
c = 1 and f(n) = n log^(-1) n, so k = -1. Level j contributes about n/(L − j). Summing over the levels gives n times a harmonic series, so T(n) = Θ(n log log n).
Exercise
Solve T(n) = 8T(n/2) + n³ log n.
Show answer
c = log₂ 8 = 3, so f(n) = n³ log n gives k = 1. Then T(n) = Θ(n³ log² n).