Sub topic 5
Solving Recurrences with Recursion Trees
This chapter assumes you can write a recurrence such as T(n) = 2T(n/2) + cn, as covered in the previous chapter. You should also be comfortable with logarithms and with summing a geometric series.
A recursion tree draws a recurrence as the calls it makes. Each node stands for one call and is labeled with the work that call does outside its recursive calls. You add up the cost of each level, then add up all the levels. This turns a recurrence into a sum you can evaluate.
Building the Tree for Merge Sort
Take T(n) = 2T(n/2) + cn. The root does cn work and has two children, each of size n/2. Each child does c(n/2) work and has two children of size n/4, and the pattern continues down the tree.
At depth i there are 2^i nodes, and each costs cn/2^i, so every level costs exactly cn. The subproblem size reaches 1 at depth log₂ n, which gives log₂ n + 1 levels. The total is cn(log₂ n + 1) = Θ(n log n).
The General Procedure
- Find the number of nodes at depth i. For T(n) = aT(n/b) + f(n), it is a^i.
- Find the subproblem size at depth i, which is n/b^i, and the cost per node, which is f(n/b^i).
- Multiply to get the level cost, a^i · f(n/b^i).
- Find the depth where the size reaches 1, which is log_b n.
- Sum the level costs, counting the leaves separately. There are a^(logb n) = n^(logb a) leaves.
The level costs usually follow one of three patterns.
| Level costs | Example | Result |
|---|---|---|
| Equal at every level | 2T(n/2) + n | cost per level × number of levels, Θ(n log n) |
| Shrinking geometrically | 2T(n/2) + n² | the root dominates, Θ(n²) |
| Growing geometrically | 3T(n/2) + n | the leaves dominate, Θ(n^log₂3) |
The next chapter turns these three patterns into the master theorem.
Checking the Level Sums with Code
This function lists the cost of each level for a given n, which lets you check your algebra on small inputs.
def level_costs(n, a, b, f):
costs = []
size, nodes = n, 1
while size >= 1:
costs.append(nodes * f(size))
nodes *= a
size //= b
return costs
print(level_costs(16, 2, 2, lambda m: m)) # [16, 16, 16, 16, 16]
print(level_costs(16, 3, 2, lambda m: m)) # [16, 24, 36, 54, 81]The first output has five equal levels, and 16 × 5 = 16(log₂16 + 1). In the second output the costs grow by a factor of 3/2 per level, and the last level, 81 = 3⁴ = 16^(log₂3), is the largest.
Common Errors
- Off-by-one depth. The tree has logb n + 1 levels, not logb n. This does not change Θ, but it does change exact counts.
- Assuming every level costs the same. That holds only when a = b^k for f(n) = n^k. Always compute a^i · f(n/b^i).
- Forgetting the leaves. When a > b, there are n^(log_b a) leaves, and they can outweigh everything above them.
- Summing a geometric series wrong. In a decreasing series the first term dominates. In an increasing series the last term dominates.
Exercise
Use a recursion tree to solve T(n) = 4T(n/2) + n.
Show answer
Depth i has 4^i nodes, each costing n/2^i, so level i costs n·2^i. The costs double at each level, so the last level dominates. At depth log₂ n the cost is n · 2^(log₂ n) = n², which matches the n^(log₂4) = n² leaves. T(n) = Θ(n²).
Exercise
Solve T(n) = 2T(n/2) + n² and say which part of the tree dominates.
Show answer
Level i costs 2^i · (n/2^i)² = n²/2^i. This geometric series decreases with ratio 1/2 and sums to less than 2n². The root dominates, so T(n) = Θ(n²).