Sub topic 11
Extended Case 2: Recurrences with Log Factors
This chapter assumes you already know the basic master theorem: how to read off a, b, and f(n) from T(n) = aT(n/b) + f(n), how to compute the critical exponent c = log_b a, and how the three basic cases compare f(n) with n^c. It also assumes you can sum an arithmetic series and are comfortable treating log n as a slowly growing function.
The basic case 2 covers only f(n) = Θ(n^c). Many real recurrences have a driving function that matches n^c up to a log factor, such as f(n) = n log n when c = 1. These fall between the basic cases. They grow too fast for case 2 as usually stated, and too slowly for case 3. The extended case 2 handles them.
The statement
Let c = log_b a. Suppose
f(n) = Θ(n^c · log^k n) for some constant k ≥ 0.
Then
T(n) = Θ(n^c · log^(k+1) n).
The rule is short: when f(n) matches the watershed function n^c up to a polylogarithmic factor, the solution keeps that factor and gains one more log. The basic case 2 is the special case k = 0, where f(n) = Θ(n^c) gives T(n) = Θ(n^c log n).
The theorem also extends below zero. Some textbooks, including the fourth edition of CLRS, state only k ≥ 0 or k > −1, so check which version your source uses. The full picture is:
| Exponent k in f(n) = Θ(n^c log^k n) | Solution T(n) | Which part of the tree dominates |
|---|---|---|
| k > −1 | Θ(n^c log^(k+1) n) | All levels contribute, top-heavy for k > 0 |
| k = −1 | Θ(n^c log log n) | Levels sum like a harmonic series |
| k < −1 | Θ(n^c) | Leaves dominate, as in case 1 |
Why one extra log appears
The recursion tree gives the reason. At depth j there are a^j subproblems, each of size n/b^j. With f(m) = m^c log^k m, the work at depth j is
a^j · (n/b^j)^c · log^k(n/b^j).
Because b^c = a, the factor a^j / b^(jc) equals 1, so the polynomial part is n^c at every level. Only the log factor changes from level to level. Use logs base b and write L = logb n. Then logb(n/b^j) = L − j, and the total internal work is
n^c · Σ{j=0}^{L−1} (L − j)^k = n^c · Σ{i=1}^{L} i^k.
The whole question reduces to how fast Σ i^k grows:
- For k > −1, the sum is Θ(L^(k+1)). For integer k this is the familiar fact that 1 + 2 + … + L is about L²/2. In general, compare the sum with the integral of x^k from 1 to L.
- For k = −1, the sum is the harmonic number H_L = Θ(log L) = Θ(log log n).
- For k < −1, the sum converges to a constant, so the internal levels contribute only Θ(n^c). The tree also has n^c leaves, so the total is Θ(n^c).
Changing the log base only multiplies by a constant, so log_b and log₂ give the same Θ bound.
Worked examples
T(n) = 2T(n/2) + n log n
Here a = 2, b = 2, c = 1, and f(n) = n log n = n^1 log^1 n, so k = 1. Extended case 2 gives T(n) = Θ(n log² n). This recurrence appears, for example, in divide-and-conquer algorithms that sort their subproblem inside each merge step.
T(n) = 4T(n/2) + n² log n
Here c = log₂ 4 = 2 and f(n) = n² log n, so k = 1. The answer is T(n) = Θ(n² log² n).
T(n) = T(n/2) + log n
Here a = 1, so c = 0 and n^c = 1. f(n) = log n = n^0 log^1 n, so k = 1. The answer is T(n) = Θ(log² n). This is binary search with logarithmic work per step instead of constant work.
T(n) = 2T(n/2) + n / log n
Here c = 1 and k = −1. The answer is T(n) = Θ(n log log n). Many older texts list this recurrence as one the master theorem cannot solve. That is true for the three basic cases, but the k = −1 branch solves it.
T(n) = 3T(n/3) + n / log² n
Here c = 1 and k = −2. The level sums converge, and T(n) = Θ(n).
Checking the answers numerically
The script below evaluates two recurrences exactly for powers of two, using T(1) = 1. For n = 2^p, log₂ n = p, which you can compute exactly as n.bit_length() - 1.
import math
def T_k1(n):
# T(n) = 2T(n/2) + n log2 n, with T(1) = 1 (k = 1)
if n <= 1:
return 1
p = n.bit_length() - 1
return 2 * T_k1(n // 2) + n * p
def T_kminus1(n):
# T(n) = 2T(n/2) + n / log2 n, with T(1) = 1 (k = -1)
if n <= 1:
return 1
p = n.bit_length() - 1
return 2 * T_kminus1(n // 2) + n / p
print("p T_k1/(n p^2) T_kminus1/(n ln p)")
for p in range(4, 21, 4):
n = 2 ** p
r1 = T_k1(n) / (n * p * p)
r2 = T_kminus1(n) / (n * math.log(p))
print(f"{p:<3} {r1:<14.4f} {r2:.4f}")Unrolling the first recurrence gives T(2^p) = n(1 + p(p+1)/2) exactly. The first ratio therefore tends to 1/2, a constant, which confirms Θ(n log² n). The second unrolls to n(1 + Hp), where Hp is the p-th harmonic number. Its ratio to n ln p also tends to a constant, namely 1, but it gets there slowly: about 2.22 at p = 4 and about 1.53 at p = 20. The additive 1 and Euler's constant inside H_p fade only as fast as 1/ln p. Slow convergence like this is normal for log log terms, so a numerical check of a log log bound needs very large n before the ratio settles.
Common errors
Treating n log n as case 3
A frequent mistake is to say that n log n grows faster than n, so T(n) = 2T(n/2) + n log n is case 3 with answer Θ(n log n). Case 3 needs f(n) = Ω(n^(c+ε)) for some constant ε > 0. No ε works here, because log n = o(n^ε) for every ε > 0. The function n log n beats n by a log factor, which is less than any polynomial factor. The correct answer is Θ(n log² n), larger than the wrong answer by a full log factor.
Forgetting to add one to k
The solution has log^(k+1), not log^k. If you keep k unchanged, you are effectively counting only the root's work. The extra log comes from summing over Θ(log n) levels whose work shrinks only polynomially in the level index, not geometrically.
Misreading the polynomial part
Extended case 2 requires the polynomial exponent in f(n) to equal c exactly. For T(n) = 4T(n/2) + n log n, c = 2 and f(n) = n log n = O(n^(2−ε)) with ε = 1/2. That is case 1, and T(n) = Θ(n²). The log factor does not matter once the polynomial exponents differ.
Applying it to a factor like log log n
The theorem covers f(n) = n^c log^k n only. A driving function such as n log log n with c = 1 does not have that form. You need a recursion tree or Akra-Bazzi for it. A later chapter covers Akra-Bazzi.
Exercise
Solve T(n) = 9T(n/3) + n² log³ n.
Show answer
c = log₃ 9 = 2, and f(n) = n² log³ n has k = 3. Extended case 2 gives T(n) = Θ(n² log⁴ n).
Exercise
Solve T(n) = 2T(n/4) + sqrt(n) · log n.
Show answer
c = log₄ 2 = 1/2, so n^c = sqrt(n). f(n) = n^(1/2) log¹ n, so k = 1, and T(n) = Θ(sqrt(n) · log² n).
Exercise
A student solves T(n) = 8T(n/2) + n³ / log n and gets Θ(n³) by case 3. What is wrong, and what is the correct bound?
Show answer
c = log₂ 8 = 3, so f(n) = n³ log^(−1) n is smaller than n³ by a log factor. That rules out case 3, which needs f(n) to be polynomially larger than n³. With k = −1, the recurrence solves to Θ(n³ log log n).
Exercise
Use the level sum n^c · Σ_{i=1}^{L} i^k to explain why T(n) = 2T(n/2) + n / log² n is Θ(n) and not Θ(n log log n).
Show answer
Here k = −2, so the level sum is Σ 1/i², which converges to π²/6 no matter how large L gets. The internal levels contribute Θ(n) in total, and the n leaves contribute Θ(n), so T(n) = Θ(n). The log log n term appears only when k = −1, where the sum is the harmonic series, and that series diverges.