Sub topic 9
Case 2: When Every Level Does Equal Work
This chapter assumes you know Big-Theta notation and logarithm identities, and that you have seen how a recursion tree adds up work level by level. It also assumes you have read the previous chapter on comparing f(n) with the watershed function n^(log_b a).
The statement
For a recurrence T(n) = aT(n/b) + f(n) with a ≥ 1 and b > 1, case 2 applies when
- f(n) = Θ(n^(log_b a)).
When it does, the solution is
- T(n) = Θ(n^(log_b a) · log n).
The non-recursive work f(n) grows at exactly the same rate as the number of leaves. Neither the root nor the leaves wins, so the total is the per-level work multiplied by the number of levels.
Why the log factor appears
Level i of the recursion tree has a^i subproblems, and each one has size n/b^i. If f(n) = c · n^(log_b a), the work at level i is
a^i · c · (n/b^i)^(logb a) = c · n^(logb a) · (a / b^(logb a))^i = c · n^(logb a),
because b^(logb a) = a. Every level costs the same. The tree has about logb n + 1 levels, so the total is Θ(n^(log_b a) log n). The base of the logarithm only changes the result by a constant factor, so we write log n.
Worked examples
| Recurrence | a | b | n^(log_b a) | f(n) | Result |
|---|---|---|---|---|---|
| Merge sort: 2T(n/2) + n | 2 | 2 | n | n | Θ(n log n) |
| Binary search: T(n/2) + 1 | 1 | 2 | n^0 = 1 | 1 | Θ(log n) |
| 4T(n/2) + n² | 4 | 2 | n² | n² | Θ(n² log n) |
| 3T(n/3) + 5n | 3 | 3 | n | 5n | Θ(n log n) |
Binary search shows that case 2 also covers constant f(n). When a = 1, the watershed function is n^0 = 1, so constant work per call matches it.
Checking it numerically
For T(n) = 4T(n/2) + n² with T(1) = 1 and n = 2^p, every level contributes n², which gives T(n) = n²(log2 n + 1). The ratio T(n) / (n² log2 n) should therefore approach 1.
import math
def T(n):
# T(n) = 4T(n/2) + n^2, T(1) = 1
return 1 if n <= 1 else 4 * T(n // 2) + n * n
for p in range(4, 21, 4):
n = 2 ** p
print(n, round(T(n) / (n * n * math.log2(n)), 4))The printed ratios are 1.25, 1.125, 1.0833, 1.0625, and 1.05, which equal 1 + 1/p.
Common errors
- Matching only an upper bound. Case 2 needs f(n) = Θ(n^(logb a)). If you only know f(n) = O(n^(logb a)), you cannot tell case 1 from case 2.
- Ignoring a log factor in f(n). For T(n) = 2T(n/2) + n log n, f(n) is not Θ(n). This recurrence falls under extended case 2, which comes in the next chapter.
- Forgetting the log. Answering Θ(n^(log_b a)) leaves out the multiplication by the number of levels.
- Counting constants as a mismatch. 5n and n are both Θ(n), so constant factors never affect which case applies.
Exercise
Solve T(n) = 9T(n/3) + 7n² and T(n) = 8T(n/2) + n³. Then explain why T(n) = 2T(n/2) + n log n is not a plain case 2 recurrence.
Show answer
For the first, log3 9 = 2 and f(n) = Θ(n²), so T(n) = Θ(n² log n). For the second, log2 8 = 3 and f(n) = n³, so T(n) = Θ(n³ log n). For the third, n^(log_2 2) = n, but n log n is not Θ(n) because it grows faster by a log factor. So the plain case 2 condition fails. Extended case 2 gives Θ(n log² n).