Sub topic 10
Solving Recurrences: Unrolling and the Master Theorem
This chapter assumes you can write a recurrence for a recursive function, as in the previous chapter on merge sort. You should also be comfortable with logarithms, exponents, and the sum 1 + 2 + ... + n = n(n+1)/2. Here you will turn recurrences such as T(n) = 2T(n/2) + n into closed-form bounds like Θ(n log n).
What "solving" a recurrence means
A recurrence defines the cost of a problem of size n in terms of the cost of smaller problems. Solving it means finding a bound on T(n) that has no T on the right-hand side. There are three standard methods:
- Unrolling (also called iteration or the recursion tree): expand the recurrence level by level and add up the work.
- The Master Theorem: a lookup rule for recurrences of one common shape.
- Substitution: guess the answer and prove it by induction. It is the most general method, but you need a good guess, which unrolling usually supplies.
This chapter covers unrolling and the Master Theorem in detail.
Unrolling
Take merge sort. Split the work into a constant c per element for merging, and assume n is a power of 2:
T(n) = 2T(n/2) + cn, with T(1) = c.
Expand it one level at a time:
- Level 0: one problem of size n, cost cn.
- Level 1: two problems of size n/2, each costing c·n/2. Total cn.
- Level 2: four problems of size n/4, each costing c·n/4. Total cn.
- Level k: 2^k problems of size n/2^k. Total cn.
The recursion stops when the size reaches 1, which happens at level log2 n. Every level costs cn, and there are log2 n + 1 levels, so T(n) = cn(log2 n + 1) = Θ(n log n).
You can check the algebra by running the recurrence. With T(1) = 1 and T(n) = 2T(n/2) + n, unrolling gives exactly T(2^k) = 2^k(k + 1) = n·log2(n) + n.
def T(n):
if n == 1:
return 1
return 2 * T(n // 2) + n
for k in (4, 10, 16):
n = 2 ** k
print(n, T(n), n * k + n)The output shows the two columns agreeing:
16 80 80
1024 11264 11264
65536 1114112 1114112Two more unrollings
T(n) = T(n/2) + 1 (binary search). Each level costs 1 and there are log2 n levels, so T(n) = Θ(log n).
T(n) = T(n-1) + n. This shrinks by subtraction, not division. Unrolling gives n + (n-1) + ... + 1 = n(n+1)/2, so T(n) = Θ(n²). This is the cost pattern of selection sort.
A tree with unequal branches
Unrolling also works when the subproblems have different sizes, which the Master Theorem cannot handle. Consider T(n) = T(n/3) + T(2n/3) + n. Each level's sizes add up to at most n, so each level costs at most n. The longest path shrinks by a factor of 2/3 per step, so the depth is log base 3/2 of n. The total is O(n log n).
The Master Theorem
Many divide-and-conquer algorithms have the form
T(n) = a·T(n/b) + f(n), with a ≥ 1 and b > 1.
Here a is the number of subproblems, b is the factor by which the size shrinks, and f(n) is the work to split and combine. Unrolling this shape gives a tree with logb n levels. The number of leaves is a^(logb n) = n^(logb a). The value n^(logb a), called the critical exponent term, measures the total work at the leaves. The theorem compares it with f(n), the work at the root.
| Case | Condition | Where the work concentrates | Result |
|---|---|---|---|
| 1 | f(n) = O(n^c) for some c < log_b a | Leaves | Θ(n^(log_b a)) |
| 2 | f(n) = Θ(n^(log_b a)) | Evenly across all levels | Θ(n^(log_b a) · log n) |
| 3 | f(n) = Ω(n^c) for some c > log_b a, and a·f(n/b) ≤ k·f(n) for some k < 1 | Root | Θ(f(n)) |
The condition in case 3 on a·f(n/b) is called the regularity condition. It holds automatically for polynomial f(n) such as n² or n³. Case 2 has a more general form with extra log factors, but the plain version covers most practical recurrences.
Worked examples
| Recurrence | a, b | n^(log_b a) | f(n) | Case | Result |
|---|---|---|---|---|---|
| 2T(n/2) + n | 2, 2 | n | n | 2 | Θ(n log n) |
| T(n/2) + 1 | 1, 2 | n^0 = 1 | 1 | 2 | Θ(log n) |
| 8T(n/2) + n² | 8, 2 | n³ | n² | 1 | Θ(n³) |
| 7T(n/2) + n² | 7, 2 | n^2.807 | n² | 1 | Θ(n^2.807) |
| 2T(n/2) + n² | 2, 2 | n | n² | 3 | Θ(n²) |
The fourth row is the recurrence for Strassen's matrix multiplication. Seven recursive multiplications instead of eight reduce the exponent from 3 to about 2.807.
The following function classifies recurrences of the form T(n) = aT(n/b) + Θ(n^d). For a polynomial f, the regularity condition always holds in case 3, so the exponents are all you need.
import math
def master(a, b, d):
"""Classify T(n) = a*T(n/b) + Theta(n^d)."""
crit = math.log(a, b)
if math.isclose(d, crit):
return f"Theta(n^{d} log n)"
if d < crit:
return f"Theta(n^{crit:.3f})"
return f"Theta(n^{d})"
for a, b, d in [(2, 2, 1), (1, 2, 0), (8, 2, 2), (7, 2, 2), (2, 2, 2)]:
, b={b}, d={d}: {master(a, b, d)}")a=2, b=2, d=1: Theta(n^1 log n)
a=1, b=2, d=0: Theta(n^0 log n)
a=8, b=2, d=2: Theta(n^3.000)
a=7, b=2, d=2: Theta(n^2.807)
a=2, b=2, d=2: Theta(n^2)When the Master Theorem does not apply
The theorem has a fixed shape, and several common recurrences fall outside it.
- Subtraction instead of division. T(n) = T(n-1) + n and T(n) = 2T(n-1) + 1 do not shrink by a constant factor. Unroll them instead. The first gives Θ(n²). The second doubles at every level, so it gives Θ(2^n). Naive Fibonacci has this exponential behavior.
- Unequal subproblem sizes. T(n) = T(n/3) + T(2n/3) + n needs a recursion tree or the more general Akra–Bazzi method.
- A gap between f(n) and the leaf work. f(n) must differ from n^(log_b a) by a polynomial factor in case 1 or 3. For T(n) = 2T(n/2) + n/log n, the ratio is only logarithmic, so no case matches.
- A non-constant number of subproblems. The theorem requires a and b to be constants.
Choosing a method
- If the recurrence is aT(n/b) + f(n) with constants a and b, apply the Master Theorem. It takes seconds.
- If the shape differs, draw the recursion tree and sum the cost per level. This gives you a guess.
- If you need a rigorous proof, confirm the guess by induction using the substitution method.
Common errors
- Reporting O(n log n) for a recurrence you only checked in one case. Check which case applies, and state Θ when the theorem gives a tight bound.
- Forgetting the base case. The unrolling must stop at a constant-size problem, and the level count comes from where it stops.
- Counting levels as n instead of log n. Halving the size each step gives log2 n levels. Subtracting 1 each step gives n levels.
- Applying case 3 without checking regularity when f(n) is not a plain polynomial, for example one with oscillating terms.
Exercise
Solve T(n) = 4T(n/2) + n² with the Master Theorem. Then solve T(n) = 3T(n/2) + n and name the case.
Show answer
For the first, a = 4, b = 2, so n^(log2 4) = n². Since f(n) = n² matches it, case 2 applies and T(n) = Θ(n² log n). For the second, n^(log2 3) ≈ n^1.585. Since f(n) = n is polynomially smaller, case 1 applies and T(n) = Θ(n^1.585).
Exercise
Unroll T(n) = T(n-1) + 1 with T(1) = 1, and explain why the Master Theorem cannot be used. Then say what T(n) = 2T(n-1) + 1 grows like.
Show answer
Each step adds 1 and there are n steps, so T(n) = n = Θ(n). The Master Theorem needs the problem size to be divided by a constant b > 1, but here it only drops by 1. For T(n) = 2T(n-1) + 1 the number of calls doubles at every level over n levels, so T(n) = 2^n - 1 = Θ(2^n).