Sub topic 9
Case 2: When Every Level Does Equal Work
This chapter assumes you can read the master theorem form T(n) = aT(n/b) + f(n) and pick out a, b, and f(n). It also assumes you know how to compute the critical exponent logb a and compare f(n) against n^(logb a). You should be comfortable with Big-Theta notation and with summing a recursion tree level by level.
Case 2 covers the recurrences where f(n) and n^(log_b a) grow at the same rate. When that happens, no single part of the recursion tree dominates. Every level contributes about the same amount of work, and the answer picks up one extra logarithmic factor.
The Statement
Let a ≥ 1 and b > 1 be constants, and write c = log_b a for the critical exponent.
If f(n) = Θ(n^c), then T(n) = Θ(n^c log n).
The extra log n counts the levels of the recursion tree. Each level costs about n^c, and there are about log_b n levels, so the total is n^c times log n.
Why Every Level Costs the Same
Look at level j of the recursion tree, where the root is level 0. That level has a^j subproblems, and each has size n/b^j. If f(n) is exactly n^c, the work on level j is:
a^j · (n/b^j)^c = n^c · (a / b^c)^j
Since c = log_b a, we have b^c = a, so the ratio a / b^c equals 1. Every level therefore costs exactly n^c.
This ratio is what separates the three cases. The level sums form a geometric series with ratio r = a / b^c', where c' is the exponent of f(n):
| Ratio r | Level sums | Who dominates | Result |
|---|---|---|---|
| r > 1 | grow toward the leaves | leaves | case 1 |
| r = 1 | constant | nobody, all levels tie | case 2 |
| r < 1 | shrink toward the leaves | root | case 3 |
In case 2 the series has ratio 1, so its sum is just the number of terms times the common value. The tree has logb n + 1 levels: the root, then one level each time the size divides by b, down to size 1. The bottom level has a^(logb n) = n^c leaves, each with constant cost, so the leaves also cost Θ(n^c), the same as every other level.
When f(n) is only Θ(n^c), not exactly n^c, the same argument goes through with constants. There are constants k1 and k2 with k1·m^c ≤ f(m) ≤ k2·m^c for all large m. Each level's work then lies between k1·n^c and k2·n^c, apart from the bottom few levels where subproblems are smaller than the threshold. Those levels cost Θ(n^c) in total. Summing gives a total between constant multiples of n^c log n. A later chapter covers the full proof sketch.
Seeing It Numerically
The code below builds the per-level work for T(n) = 4T(n/2) + n². Here a = 4, b = 2, and c = log_2 4 = 2, so f(n) = n² matches n^c exactly. Every level should report the same number.
import math
def level_work(a, b, f, n):
"""Total work at each level of the tree for T(n) = a*T(n/b) + f(n)."""
levels = []
nodes, size = 1, n
while size >= 1:
levels.append(nodes * f(size))
nodes *= a
size //= b
return levels
def T(n):
# T(1) = 1, T(n) = 4 T(n/2) + n^2
return 1 if n <= 1 else 4 * T(n // 2) + n * n
n = 1024
levels = level_work(4, 2, lambda m: m * m, n)
print(levels) # eleven copies of 1048576
print(sum(levels), T(n)) # 11534336 11534336
for p in range(4, 21, 4):
m = 2 ** p
print(m, T(m), round(T(m) / (m * m * math.log2(m)), 4))For n = 2^p the exact solution is T(n) = n²(log2 n + 1), so the printed ratio is 1 + 1/log2 n. It falls toward 1 as n grows: 1.25, 1.125, 1.0833, 1.0625, 1.05. That is the Θ(n² log n) bound showing up in the numbers.
Worked Examples
Each example uses the same steps. Identify a, b, and f(n). Compute c = log_b a. Confirm that f(n) = Θ(n^c). Then write down Θ(n^c log n).
| Recurrence | a | b | c = log_b a | f(n) | Solution |
|---|---|---|---|---|---|
| T(n) = 2T(n/2) + Θ(n) (merge sort) | 2 | 2 | 1 | n | Θ(n log n) |
| T(n) = T(n/2) + Θ(1) (binary search) | 1 | 2 | 0 | 1 = n^0 | Θ(log n) |
| T(n) = 4T(n/2) + n² | 4 | 2 | 2 | n² | Θ(n² log n) |
| T(n) = 9T(n/3) + 5n² + 3n | 9 | 3 | 2 | Θ(n²) | Θ(n² log n) |
| T(n) = 2T(n/4) + √n | 2 | 4 | 1/2 | n^(1/2) | Θ(√n log n) |
| T(n) = T(2n/3) + 1 | 1 | 3/2 | 0 | 1 | Θ(log n) |
Some details are worth pointing out:
- Binary search is the case 2 instance with c = 0. Since n^0 = 1, the formula Θ(n^0 log n) simplifies to Θ(log n). The tree is a single path of log_2 n nodes, each doing constant work.
- Lower-order terms in f(n) do not matter. In 5n² + 3n, the 3n term and the constant 5 vanish inside Θ(n²).
- b need not be an integer. In T(n) = T(2n/3) + 1, each call shrinks the size by a factor of 3/2, so b = 3/2. Because a = 1, c = 0 for any b.
- The base of the logarithm does not matter in the answer. logb n = ln n / ln b, and the 1/ln b is a constant that Θ absorbs. The base matters only inside the exponent logb a, where it changes the polynomial degree.
Common Errors
Dropping the log factor
The most frequent mistake is to notice that f(n) matches n^c and answer Θ(n^c). That is the case 1 answer. In case 2 the root alone already costs Θ(n^c), and there are Θ(log n) levels each costing the same, so the log n factor is required.
Miscomputing the critical exponent
For T(n) = 9T(n/3) + n², the exponent is log_3 9 = 2, not 3 and not 9/3. Compute c from a and b together, never from a alone. A wrong c sends you to the wrong case.
Treating "close" as equal
T(n) = 2T(n/2) + n log n has c = 1, but n log n is not Θ(n). It grows faster than n by a log factor and slower than any n^(1+ε). Basic case 2 does not apply. The extended case 2 from the next chapter handles it and gives Θ(n log² n). In the same way, f(n) = n / log n is not Θ(n) either.
Applying it to uneven splits
T(n) = T(n/3) + T(2n/3) + n has two subproblems of different sizes, so it does not have the form aT(n/b) + f(n). A recursion tree shows each full level still sums to n, and the answer is Θ(n log n). That answer comes from the tree or from Akra-Bazzi, not from the master theorem.
Confusing the recurrence's cost with the algorithm's output
Case 2 tells you the asymptotic running time. It says nothing about constants, so two Θ(n log n) algorithms can differ a lot in speed in practice.
Exercises
Exercise
Solve T(n) = 8T(n/2) + 7n³ + n² log n using the master theorem. Show a, b, the critical exponent, and why the case applies.
Show answer
a = 8, b = 2, so c = log_2 8 = 3. The function f(n) = 7n³ + n² log n is Θ(n³), because n² log n grows slower than n³. Since f(n) = Θ(n^c), case 2 applies and T(n) = Θ(n³ log n).
Exercise
A recursive algorithm splits its input into 3 parts of size n/9 each, recurses on all three, and spends Θ(√n) time combining. Write its recurrence and solve it.
Show answer
T(n) = 3T(n/9) + Θ(√n). Here c = log_9 3 = 1/2, and f(n) = Θ(n^(1/2)) matches. Case 2 gives T(n) = Θ(√n log n).
Exercise
For T(n) = 4T(n/2) + n² with T(1) = 1 and n a power of 2, compute the work on level j of the recursion tree and use it to find T(n) exactly.
Show answer
Level j has 4^j nodes of size n/2^j, each costing (n/2^j)² = n²/4^j, so the level costs n². The levels run from j = 0 to j = log2 n, which is log2 n + 1 levels, and the leaf level (4^(log2 n) = n² leaves of cost 1) also costs n². So T(n) = n²(log2 n + 1), which is Θ(n² log n).
Exercise
A student solves T(n) = 2T(n/2) + n log n by saying "f(n) is about n, so case 2 gives Θ(n log n)." What is wrong, and what does a recursion tree suggest instead?
Show answer
n log n is not Θ(n), so basic case 2 does not apply. On level j, there are 2^j nodes of size n/2^j, each costing (n/2^j) log(n/2^j), so the level costs n(log n − j). Summing over j from 0 to log n gives about n·(log n)²/2, which is Θ(n log² n). The extended case 2 gives the same result, so the student's answer is too small by a log factor.