Sub topic 3
Solving by Unrolling
Unrolling, also called the iteration method, solves a recurrence by repeatedly substituting it into itself until a pattern appears. You then write that pattern as a sum, stop it at the base case, and evaluate the sum. The method needs only algebra and a few standard series. It is the most direct way to get an exact closed form, and it is the basis for the recursion tree method in the next chapter.
This chapter assumes you can read Big-Theta notation, use logarithm identities such as $a^{\logb n} = n^{\logb a}$, and sum arithmetic and geometric series.
The Four-Step Procedure
Every unrolling follows the same steps:
- Expand the recurrence two or three times by substituting it into its own right-hand side.
- Generalize to the form after $k$ expansions, written as one term involving $T(\cdot)$ plus a sum.
- Stop by choosing $k$ so that the argument of $T$ equals the base case.
- Simplify the sum into a closed form, then read off the asymptotic bound.
Step 2 is where most errors happen. A pattern that looks right after two expansions can still be wrong by a coefficient or an off-by-one in the index. That is why the procedure ends with a numeric check.
Example 1: Subtract-One Recurrences
Take $T(n) = T(n-1) + n$ with $T(1) = 1$. This is the running time of selection sort written recursively. Each call scans $n$ elements and then recurses on $n - 1$ of them.
Expand:
$$ T(n) = T(n-1) + n = T(n-2) + (n-1) + n = T(n-3) + (n-2) + (n-1) + n $$
Generalize: after $k$ expansions,
$$ T(n) = T(n-k) + \sum_{i=0}^{k-1} (n - i) $$
Stop: the base case is $T(1)$, so set $n - k = 1$, which gives $k = n - 1$. The sum then runs over the values $n, n-1, \dots, 2$.
Simplify:
$$ T(n) = 1 + \sum{j=2}^{n} j = \sum{j=1}^{n} j = \frac{n(n+1)}{2} = \Theta(n^2) $$
Now a recurrence that branches: $T(n) = 2T(n-1) + 1$ with $T(1) = 1$, the Towers of Hanoi move count. The coefficient 2 multiplies everything that comes after it, so watch it build up:
$$ T(n) = 2T(n-1) + 1 = 4T(n-2) + 2 + 1 = 8T(n-3) + 4 + 2 + 1 $$
After $k$ steps the form is $2^k T(n-k) + \sum_{i=0}^{k-1} 2^i = 2^k T(n-k) + 2^k - 1$. Setting $k = n - 1$ gives $2^{n-1} + 2^{n-1} - 1 = 2^n - 1 = \Theta(2^n)$.
Example 2: Divide-by-Constant Recurrences
When the argument shrinks by a factor instead of by a fixed amount, the number of expansions is logarithmic. To keep the arguments whole numbers, assume $n$ is a power of the divisor. A later chapter shows why this assumption is harmless for asymptotic bounds.
Take $T(n) = T(n/2) + 1$ with $T(1) = 1$, the recurrence for binary search.
$$ T(n) = T(n/2) + 1 = T(n/4) + 2 = T(n/8) + 3 $$
After $k$ steps the form is $T(n/2^k) + k$. Set $n/2^k = 1$, so $k = \log2 n$, which gives $T(n) = \log2 n + 1 = \Theta(\log n)$.
A Case Where the Sum Matters
The recurrence $T(n) = 3T(n/2) + n$ with $T(1) = 1$ shows the method's full machinery. It describes an algorithm that makes three half-size calls and does linear extra work, which is the shape of Karatsuba multiplication.
Expand:
$$ \begin{aligned} T(n) &= 3T(n/2) + n \\ &= 3\left[3T(n/4) + n/2\right] + n = 9T(n/4) + \tfrac{3}{2}n + n \\ &= 27T(n/8) + \tfrac{9}{4}n + \tfrac{3}{2}n + n \end{aligned} $$
Generalize:
$$ T(n) = 3^k \, T\!\left(\frac{n}{2^k}\right) + n \sum_{i=0}^{k-1} \left(\frac{3}{2}\right)^i $$
Stop: set $k = \log2 n$. The first term becomes $3^{\log2 n} \cdot T(1) = n^{\log2 3}$, using the identity $a^{\logb n} = n^{\log_b a}$.
Simplify: the sum is geometric with ratio $3/2$:
$$ n \cdot \frac{(3/2)^{\log2 n} - 1}{3/2 - 1} = 2n\left(\frac{n^{\log2 3}}{n} - 1\right) = 2n^{\log_2 3} - 2n $$
Adding the two parts:
$$ T(n) = 3n^{\log2 3} - 2n = \Theta(n^{\log2 3}) \approx \Theta(n^{1.585}) $$
Check with $n = 4$. The recurrence gives $T(2) = 3 + 2 = 5$ and $T(4) = 15 + 4 = 19$. The formula gives $3 \cdot 9 - 8 = 19$, which matches.
The geometric sum is what decides the answer. Its ratio is greater than 1, so the last term dominates and the work near the bottom of the recursion wins. If the ratio were less than 1, the first term would dominate. If it were exactly 1, every term would be equal and you would get a factor of $\log n$. This three-way split comes back later as the three cases of the master theorem.
Checking Closed Forms in Code
Algebra mistakes are easy to make, so test every closed form against the recurrence itself. The script below evaluates each recurrence directly and compares it with the derived formula. For the divide-by-2 recurrences it uses $n = 2^p$, so that $n^{\log_2 3} = 3^p$ stays an exact integer.
def check(name, rec, closed, ns):
for n in ns:
got, want = rec(n), closed(n)
assert got == want, f"{name}: n={n}, recurrence={got}, formula={want}"
print(f"{name}: ok for n in {list(ns)}")
# T(n) = T(n-1) + n, T(1) = 1 -> n(n+1)/2
def t_sub(n):
return 1 if n == 1 else t_sub(n - 1) + n
# T(n) = 2T(n-1) + 1, T(1) = 1 -> 2^n - 1
def t_hanoi(n):
return 1 if n == 1 else 2 * t_hanoi(n - 1) + 1
# T(n) = T(n/2) + 1, T(1) = 1 -> log2(n) + 1
def t_bsearch(n):
return 1 if n == 1 else t_bsearch(n // 2) + 1
# T(n) = 3T(n/2) + n, T(1) = 1 -> 3 n^(log2 3) - 2n
def t_three(n):
return 1 if n == 1 else 3 * t_three(n // 2) + n
def log2_exact(n):
return n.bit_length() - 1 # exact for powers of two
powers = [2 ** p for p in range(0, 16)]
check("T(n-1)+n", t_sub, lambda n: n * (n + 1) // 2, range(1, 200))
check("2T(n-1)+1", t_hanoi, lambda n: 2 ** n - 1, range(1, 60))
check("T(n/2)+1", t_bsearch, lambda n: log2_exact(n) + 1, powers)
check("3T(n/2)+n", t_three, lambda n: 3 * 3 ** log2_exact(n) - 2 * n, powers)If any formula is wrong, the assert reports the first n where the recurrence and the formula disagree. Usually that points straight at an off-by-one in the stopping index.
Common Errors
| Error | Symptom | Fix |
|---|---|---|
| Dropping the multiplier $a^k$ on the $T$ term | Answer is too small for branching recurrences | Keep the coefficient attached through every expansion |
| Forgetting to scale $f$ inside each expansion | Sum terms all equal $n$ instead of $n/2^i$ | Substitute the full argument, e.g. $f(n/2) = n/2$ |
| Wrong number of levels | Off by one term, e.g. $\log_2 n$ vs. $\log_2 n + 1$ | Solve $n/b^k = $ base explicitly; test small $n$ |
| Stopping at $T(0)$ when the base is $T(1)$ | Constant or additive error in the closed form | Use the base case the recurrence actually defines |
| Summing $\Theta(\cdot)$ terms | Hidden constants grow with the number of terms | Replace $\Theta(f(n))$ with $c\,f(n)$ first |
| Trusting the pattern after one expansion | Wrong general form | Expand at least three times before generalizing |
When Unrolling Stops Being Practical
Unrolling works best when each call makes one recursive call or several calls of the same size, because then the general form keeps one $T$ term. With unequal subproblems, as in $T(n) = T(n/3) + T(2n/3) + n$, each expansion doubles the number of distinct $T$ terms, and no clean general form appears. In that situation, draw the expansion as a recursion tree and sum it level by level.
Exercise
Unroll T(n) = T(n-1) + 2 with T(1) = 3. Give the general form after k steps, the closed form, and the Theta bound.
Show answer
After k steps, T(n) = T(n-k) + 2k. Setting k = n - 1 gives T(n) = 3 + 2(n-1) = 2n + 1, which is Theta(n). Check: T(2) = 5 = 2(2) + 1.
Exercise
Unroll T(n) = 4T(n/2) + n with T(1) = 1, assuming n is a power of 2. Find the exact closed form.
Show answer
After k steps, T(n) = 4^k T(n/2^k) + n * sum_{i=0}^{k-1} 2^i = 4^k T(n/2^k) + n(2^k - 1). With k = log2 n, 4^k = n^2, so T(n) = n^2 + n(n - 1) = 2n^2 - n = Theta(n^2). Check: T(2) = 4 + 2 = 6 = 2(4) - 2.
Exercise
A student unrolls T(n) = 2T(n/2) + n and writes the general form as 2T(n/2^k) + kn. Find the error and give the correct closed form when T(1) = 1.
Show answer
The coefficient should be 2^k, not 2, since each expansion doubles the number of calls. The correct form is 2^k T(n/2^k) + kn. With k = log2 n this gives T(n) = n + n log2 n = Theta(n log n).
Exercise
Unroll T(n) = T(n/2) + n with T(1) = 1, assuming n is a power of 2. Which term of the sum dominates?
Show answer
After k steps, T(n) = T(n/2^k) + n * sum_{i=0}^{k-1} (1/2)^i. With k = log2 n, the sum equals 2(1 - 1/n), so T(n) = 1 + 2n - 2 = 2n - 1 = Theta(n). The ratio is below 1, so the first term, n (the top-level work), dominates.