Sub topic 5
Solving Recurrences with Recursion Trees
This chapter assumes you can trace a recursive function and read Big-O notation. It also assumes you can write a recurrence such as T(n) = 2T(n/2) + n for a divide-and-conquer algorithm, which the previous chapter covered. You will need one piece of algebra: the sum of a geometric series.
A recurrence tells you how the cost of a problem depends on the cost of smaller problems, but it does not give you a closed form. A recursion tree is a drawing of every recursive call, with each call labeled by the work it does outside its own recursive calls. If you add up all the labels, you get the total running time. The method is mechanical. It also shows which part of the algorithm dominates the cost, and the master theorem in the next chapter is built on that idea.
The Method in Four Steps
- Draw the root. Label it with the non-recursive cost at size n, such as the merge step's
n. - Expand children. Each recursive call becomes a child labeled with its own non-recursive cost at its smaller size.
- Sum each level. Add the labels across one depth of the tree. This level sum is usually what makes the pattern visible.
- Count levels and add up the level sums. Find the depth at which subproblems reach the base case, then add the level sums, including the cost of the leaves.
Worked Example: Merge Sort, T(n) = 2T(n/2) + n
Merge sort does n work merging at the top. It then makes two calls of size n/2, each of which does n/2 work merging, and so on.
Level i has 2^i nodes, and each costs n/2^i, so every level sums to n. Subproblem size reaches 1 when n/2^i = 1, which happens at i = log₂ n. That gives log₂ n + 1 levels:
T(n) = n · (log₂ n + 1) = Θ(n log n)
The Three Shapes of Level Sums
The level sums almost always form a geometric series: each level costs some ratio r times the level above it. The value of r decides the answer.
| Recurrence | Level i sum | Ratio r | Dominant part | Result |
|---|---|---|---|---|
| T(n) = 2T(n/2) + n² | n² / 2^i | 1/2 (decreasing) | Root | Θ(n²) |
| T(n) = 2T(n/2) + n | n | 1 (constant) | All levels equally | Θ(n log n) |
| T(n) = T(n/2) + 1 | 1 | 1 (constant) | All levels equally | Θ(log n) |
| T(n) = 3T(n/2) + n | n · (3/2)^i | 3/2 (increasing) | Leaves | Θ(n^log₂3) |
| T(n) = 2T(n/2) + 1 | 2^i | 2 (increasing) | Leaves | Θ(n) |
The three behaviors work as follows:
- Decreasing (r < 1). A geometric series with ratio below 1 sums to a constant times its first term. The root's work dominates, so T(n) = Θ(root cost).
- Constant (r = 1). Every level costs the same, so T(n) = Θ(level cost × number of levels). This is where the log factor comes from.
- Increasing (r > 1). A geometric series with ratio above 1 sums to a constant times its last term. The leaves dominate, so T(n) = Θ(number of leaves).
Counting Leaves
In the increasing case you need the number of leaves. With a children per node and size dividing by b, the depth is logb n and the leaf count is a^(logb n). The identity a^(logb n) = n^(logb a) turns this into a polynomial. For Karatsuba's T(n) = 3T(n/2) + n, there are 3^(log₂ n) = n^(log₂ 3) ≈ n^1.585 leaves. That beats the n² of four-way splitting, because the leaf count grows with the branching factor a.
Unbalanced Trees: T(n) = T(n/3) + T(2n/3) + n
Uneven splits come up in quicksort with a fixed-ratio pivot. In this tree the branches have different lengths. The leftmost path shrinks by 1/3 at each step and ends after log₃ n levels. The rightmost path shrinks by 2/3 and ends after log₃/₂ n levels.
Each full level still sums to n, because n/3 + 2n/3 = n. Below depth log₃ n, some branches have ended, so later levels sum to at most n. That gives two bounds:
- Upper bound: at most log₃/₂ n levels, each costing at most n, gives O(n log n).
- Lower bound: the first log₃ n levels are complete and each costs exactly n, giving Ω(n log n).
The two bounds differ only by a constant factor in the log, so T(n) = Θ(n log n). The master theorem does not cover this recurrence, but the recursion tree handles it without trouble. This is one of the method's real advantages.
Common Errors
- Confusing node count with level cost. A level with 3^i nodes of size n/2^i costs 3^i · (n/2^i), not n/2^i.
- Off-by-one depth. The tree has log_b n + 1 levels, counting the root and the leaves. This changes constants but not the Θ class.
- Ignoring floors and ceilings. Real code splits into ⌊n/2⌋ and ⌈n/2⌉. For asymptotic bounds you can assume n is a power of b. The answer is the same for the usual monotone cost functions.
- Treating a guess as a proof. A recursion tree with sloppy sums produces a guess. To make it rigorous, either do the sums exactly or confirm the guess by induction (the substitution method).
Checking a Tree Result Numerically
You can test a derived bound by evaluating the recurrence directly and dividing by the claimed growth rate. If the bound is right, the ratio settles toward a constant.
from functools import lru_cache
import math
@lru_cache(maxsize=None)
def t_merge(n):
# T(n) = 2T(n/2) + n, T(1) = 1
if n <= 1:
return 1
return 2 * t_merge(n // 2) + n
@lru_cache(maxsize=None)
def t_kara(n):
# T(n) = 3T(n/2) + n, T(1) = 1
if n <= 1:
return 1
return 3 * t_kara(n // 2) + n
for k in range(4, 21, 4):
n = 2 ** k
r1 = t_merge(n) / (n * math.log2(n))
r2 = t_kara(n) / n ** math.log2(3)
print(f"{n:>8} {r1:.4f} {r2:.4f}")Output:
16 1.2500 2.6049
256 1.1250 2.9220
4096 1.0833 2.9846
65536 1.0625 2.9970
1048576 1.0500 2.9994The first ratio approaches 1, because the exact solution is n log₂ n + n. The second approaches 3, because the exact solution is 3n^(log₂3) − 2n. Both ratios level off at a constant, which matches the bounds from the trees. If you divide by the wrong function, such as n log n for the Karatsuba recurrence, the ratio keeps growing.
Exercises
Exercise
Use a recursion tree to solve T(n) = 4T(n/2) + n. State the level sum, the ratio between levels, and the final bound.
Show answer
Level i has 4^i nodes of size n/2^i, so the level sum is 4^i · n/2^i = n · 2^i. The ratio is 2, which is increasing, so the leaves dominate. There are 4^(log₂ n) = n² leaves, so T(n) = Θ(n²).
Exercise
Solve T(n) = 2T(n/2) + n log₂ n with a recursion tree. Why is the answer not Θ(n log n)?
Show answer
Level i has 2^i nodes of size n/2^i, each costing (n/2^i) log₂(n/2^i). The level sum is n(log₂ n − i). Adding over i = 0 to log₂ n gives n(log₂ n + (log₂ n − 1) + ... + 0) = n · Θ(log² n), so T(n) = Θ(n log² n). The level sums shrink only by subtraction, not by a constant ratio, so no single level dominates. All log n levels contribute roughly n log n each, and that adds an extra log factor.
Exercise
Quicksort with a pivot that always splits 1-to-9 gives T(n) = T(n/10) + T(9n/10) + n. What are the shortest and longest root-to-leaf depths, and what is T(n)?
Show answer
The shortest path has depth log₁₀ n and the longest has depth log₁₀/₉ n. Every full level costs n, and no level costs more than n. The bounds are Ω(n log₁₀ n) and O(n log₁₀/₉ n), which differ only by a constant factor, so T(n) = Θ(n log n).