Sub topic 10
Solving recurrences: unrolling and the Master Theorem
This chapter assumes you can write a recurrence for a recursive function, such as T(n) = 2T(n/2) + n for merge sort. It also assumes you are comfortable with exponents and logarithms. Here you learn to turn a recurrence into a Big O bound.
Unrolling: expand until a pattern appears
Unrolling (also called the substitution or recursion-tree method) means writing out the recurrence several times and spotting the pattern. Take merge sort, T(n) = 2T(n/2) + cn, with T(1) = c.
- T(n) = 2T(n/2) + cn
- T(n) = 2[2T(n/4) + c(n/2)] + cn = 4T(n/4) + 2cn
- T(n) = 4[2T(n/8) + c(n/4)] + cn = 8T(n/8) + 3cn
- After k levels: T(n) = 2^k · T(n/2^k) + k·cn
The recursion stops when n/2^k = 1, so k = log2 n. Substituting gives n·T(1) + cn·log2 n. The cn·log2 n term dominates, so T(n) = O(n log n).
The tree shows why the answer is what it is. Each level costs cn in total, and there are about log2 n levels. You can check the unrolled formula by running the recurrence.
import math
def T(n):
return 1 if n <= 1 else 2 * T(n // 2) + n
for k in (1, 4, 10, 16):
n = 2 ** k
print(n, T(n), n * math.log2(n) + n) # the last two columns matchFor powers of two, T(n) equals n·log2 n + n exactly, which matches the unrolled result.
The Master Theorem
Unrolling works for any recurrence, but it takes effort. Many divide-and-conquer algorithms fit the form
T(n) = a·T(n/b) + Θ(n^d), with a ≥ 1, b > 1, d ≥ 0
Here a is the number of subproblems, b is the factor by which the input shrinks, and n^d is the cost of splitting and combining. Compare a with b^d:
| Case | Condition | Result | Where the work is |
|---|---|---|---|
| 1 | a < b^d | Θ(n^d) | At the root |
| 2 | a = b^d | Θ(n^d · log n) | Spread evenly across levels |
| 3 | a > b^d | Θ(n^(log_b a)) | At the leaves |
Some examples:
- Merge sort: a = 2, b = 2, d = 1. Since 2 = 2^1, case 2 gives Θ(n log n).
- Binary search: T(n) = T(n/2) + 1, so a = 1, b = 2, d = 0. Since 1 = 2^0, case 2 gives Θ(log n).
- Karatsuba multiplication: T(n) = 3T(n/2) + n, so a = 3, b = 2, d = 1. Since 3 > 2, case 3 gives Θ(n^(log2 3)), about n^1.585.
- T(n) = T(n/2) + n: a = 1 < 2^1, so case 1 gives Θ(n). The geometric series n + n/2 + n/4 + … sums to less than 2n.
Common mistakes
- Forgetting that the recursion depth is log_b n when the input shrinks by division, but n when it shrinks by subtraction.
- Comparing a with b instead of with b^d.
- Using the theorem when the combine cost is not polynomial, such as n log n. Such recurrences need the extended version of the theorem or unrolling.
- Ignoring the base case. It matters in case 3, where the leaf count drives the answer.
Exercise
Solve T(n) = 4T(n/2) + n using the Master Theorem. Then use unrolling to explain which level of the recursion tree dominates the cost.
Show answer
Here a = 4, b = 2, d = 1, so b^d = 2 and a > b^d. Case 3 applies, giving Θ(n^(log2 4)) = Θ(n^2). In the tree, level k has 4^k subproblems, each costing n/2^k, so the level costs n·2^k. This sum doubles with each level, so the deepest level (the leaves, at k = log2 n) dominates. It costs about n·2^(log2 n) = n^2.