Sub topic 13
Why the Three Cases Arise: A Proof Sketch
This chapter assumes you know the recursion tree method, the statement of the master theorem, and how to sum a geometric series. To keep the algebra simple, we take n to be an exact power of b. The chapter on floors and ceilings explains why this assumption doesn't change the result.
The recursion tree for T(n) = aT(n/b) + f(n)
Expand the recurrence into a tree. The root costs f(n) and has a children. Each child is a subproblem of size n/b, so each child costs f(n/b), and the pattern repeats down the tree.
At depth j there are a^j nodes, and each has size n/b^j. The tree stops when the size reaches 1, which happens at depth log_b n. The number of leaves is therefore
a^(logb n) = n^(logb a).
Write c = log_b a for this critical exponent. Each leaf costs Θ(1), so the total is
T(n) = Θ(n^c) + Σ{j=0}^{logb n − 1} a^j f(n/b^j).
Every case of the theorem comes from asking one question about this sum: do the level totals grow, stay flat, or shrink as j increases?
Case 2: flat levels
Suppose f(n) = Θ(n^c). Then level j costs
a^j (n/b^j)^c = n^c · (a/b^c)^j = n^c,
because b^c = a. Every level costs the same n^c, and there are log_b n levels. The sum is Θ(n^c log n), and the leaves add only another n^c.
Case 1: growing levels
Suppose f(n) = O(n^(c−ε)) for some ε > 0. Then level j costs at most
a^j (n/b^j)^(c−ε) = n^(c−ε) · (a/b^(c−ε))^j = n^(c−ε) · b^(εj).
This is an increasing geometric series. A geometric series is within a constant factor of its largest term, which here is the last one:
n^(c−ε) · b^(ε log_b n) = n^(c−ε) · n^ε = n^c.
The internal levels add up to O(n^c), so the leaves dominate and T(n) = Θ(n^c).
Case 3: shrinking levels and the regularity condition
Suppose a·f(n/b) ≤ k·f(n) for some constant k < 1. Applying this condition j times gives a^j f(n/b^j) ≤ k^j f(n). The level totals now form a decreasing geometric series:
Σ k^j f(n) ≤ f(n) / (1 − k) = O(f(n)).
The root alone contributes f(n), so the sum is Θ(f(n)). The leaves cost n^c, which is O(f(n)) because f(n) = Ω(n^(c+ε)). The result is T(n) = Θ(f(n)).
Seeing the three shapes numerically
The code below prints the cost of each level, from the root down to the leaves.
def level_work(a, b, f, n):
sums = []
size, nodes = n, 1
while size > 1:
sums.append(nodes * f(size))
size //= b
nodes *= a
sums.append(nodes) # leaves, cost 1 each
return sums
n = 2 ** 10
print(level_work(4, 2, lambda m: m, n)) # case 1: costs double each level
print(level_work(2, 2, lambda m: m, n)) # case 2: every level costs 1024
print(level_work(2, 2, lambda m: m * m, n)) # case 3: costs halve each level| Case | Level costs | Dominant part | Result |
|---|---|---|---|
| 1 | Increasing geometric | Leaves | Θ(n^c) |
| 2 | Constant | All levels equally | Θ(n^c log n) |
| 3 | Decreasing geometric | Root | Θ(f(n)) |
Exercise
For T(n) = 3T(n/3) + n, compute the cost of level j of the recursion tree and use it to find T(n).
Show answer
Level j has 3^j nodes of size n/3^j, so it costs 3^j · (n/3^j) = n. There are log_3 n levels, each costing n, so T(n) = Θ(n log n). This is case 2.
Exercise
For T(n) = 2T(n/2) + n^2, find a constant k < 1 that satisfies the regularity condition, and give T(n).
Show answer
a·f(n/b) = 2(n/2)^2 = n^2/2, so k = 1/2 works. The level costs shrink by half at each depth, the root dominates, and T(n) = Θ(n^2).