Sub topic 3
Solving by Unrolling
This chapter assumes you can read Big-Theta notation, sum arithmetic and geometric series, and use the identity 2^(log2 n) = n. You should also be able to write a recurrence from recursive code, which the previous chapter covered.
Unrolling, also called the iteration method, solves a recurrence by repeatedly substituting it into itself until a pattern appears. You then write the pattern for a general step k and pick the k that reaches the base case. What remains is an ordinary sum.
The Four Steps
- Substitute the recurrence into itself two or three times.
- Write the expression after k substitutions.
- Choose k so the argument hits the base case.
- Evaluate the sum and simplify to a Theta bound.
Example 1: Subtract-One Recurrence
Take T(n) = T(n-1) + n with T(1) = 1. This is the cost of selection sort written recursively.
- T(n) = T(n-1) + n
- = T(n-2) + (n-1) + n
- = T(n-3) + (n-2) + (n-1) + n
After k steps: T(n) = T(n-k) + (n-k+1) + ... + n. Setting k = n-1 reaches T(1):
T(n) = 1 + 2 + ... + n = n(n+1)/2 = Θ(n²).
Example 2: Halving With Constant Work
Binary search gives T(n) = T(n/2) + 1 with T(1) = 1. Assume n is a power of two.
- T(n) = T(n/2) + 1 = T(n/4) + 2 = T(n/8) + 3
After k steps: T(n) = T(n/2^k) + k. The base case needs n/2^k = 1, so k = log2 n. That gives T(n) = 1 + log2 n = Θ(log n).
Example 3: Two Halves With Linear Work
Merge sort gives T(n) = 2T(n/2) + n with T(1) = 1.
- T(n) = 2T(n/2) + n
- = 4T(n/4) + n + n
- = 8T(n/8) + 3n
After k steps: T(n) = 2^k T(n/2^k) + kn. With k = log2 n, the first term is 2^(log2 n) · T(1) = n, so T(n) = n log2 n + n = Θ(n log n).
Checking Your Closed Form
Compare the closed form against the recurrence directly before you trust it:
import math
def T1(n): return 1 if n == 1 else T1(n - 1) + n
def T2(n): return 1 if n == 1 else T2(n // 2) + 1
def T3(n): return 1 if n == 1 else 2 * T3(n // 2) + n
for n in [2, 16, 256, 512]:
lg = int(math.log2(n))
assert T1(n) == n * (n + 1) // 2
assert T2(n) == 1 + lg
assert T3(n) == n * lg + n
print("all closed forms match")Common Errors
| Error | Symptom | Fix |
|---|---|---|
| Dropping the coefficient a | Example 3 comes out as Θ(n) | Multiply every inner term by the accumulated a^k |
| Wrong stopping k | Off by a factor, or a log appears where none belongs | Solve n/b^k = 1 or n - k = base explicitly |
| Ignoring the leftover base term | You miss the n in n log n + n | Keep a^k · T(base) and evaluate it |
| Treating the pattern as proven | The "step k" formula was only guessed | Prove it by induction on k, or use substitution |
Exercise
Solve T(n) = 2T(n-1) + 1 with T(0) = 0 by unrolling. Give the exact closed form and the Theta bound.
Show answer
T(n) = 2T(n-1) + 1 = 4T(n-2) + 2 + 1 = 8T(n-3) + 4 + 2 + 1. After k steps, T(n) = 2^k T(n-k) + (2^k - 1). Setting k = n gives T(n) = 2^n · 0 + 2^n - 1 = 2^n - 1, which is Θ(2^n). This is the Towers of Hanoi move count.
Exercise
Unroll T(n) = T(n/3) + n with T(1) = 1, where n is a power of 3. What is the Theta bound?
Show answer
After k steps, T(n) = T(n/3^k) + n(1 + 1/3 + ... + 1/3^(k-1)). The geometric series is bounded by 3/2, so T(n) ≤ 1 + 3n/2, and T(n) ≥ n. Therefore T(n) = Θ(n).