[Follow here] for daily coding insights.
Advanced Recursion : The Method of Expansion — Solving Recurrences by Unrolling
In the previous post, you learned how recursion trees reveal the structure and total work of recursive algorithms. In this post, we go one level deeper: the Method of Expansion (also called Unrolling or Iterative Substitution).
This technique is fundamental for deriving tight mathematical bounds on recursive runtimes without relying directly on the Master Theorem.
It’s especially useful when:
-
The recurrence doesn’t fit Master Theorem form
-
The problem has irregular subproblem sizes
-
You want to see how work evolves across recursion depth
-
You need exact closed-form solutions, not just asymptotic behavior
Let’s break it down systematically.
1. What Is the Method of Expansion?
Given a recurrence like:
T(n) = a · T(n/b) + f(n)
the method expands the recurrence step-by-step:
T(n) = a(a · T(n/b²) + f(n/b)) + f(n)
= a² · T(n/b²) + a · f(n/b) + f(n)
You keep expanding until the input reduces to the base case.
This gives you a series representing the total work.
The pattern reveals:
-
growth or shrinkage of coefficients
-
how much work happens at each depth
-
when a term becomes dominant
This pattern recognition is the core skill of advanced recursion analysis.
2. Warm-up Example
Recurrence:
T(n) = 2T(n/2) + n
Step-by-step expansion
Level 0
T(n) = 2T(n/2) + n
Level 1
T(n) = 2[2T(n/4) + n/2] + n
= 4T(n/4) + 2(n/2) + n
Level 2
T(n) = 8T(n/8) + 4(n/4) + 2(n/2) + n
You now see a pattern:
T(n) = 2^k · T(n / 2^k) + k·n
Stop when n / 2^k = 1 → k = log₂ n.
So:
T(n) = n · T(1) + n log₂ n
Final result:
T(n) = Θ(n log n)
You solved it without Master Theorem — purely by expansion.
3. Technique #1: Identify the Structure of Terms
Every unrolled recurrence has two types of terms:
(1) The “Homogeneous” term: aᵏ · T(n/bᵏ)
This tells you how many subproblems appear at the leaf of the recursion tree.
Example:
2ᵏ subproblems of size n / 2ᵏ appears for divide-and-conquer.
(2) The “Particular” or additive terms:
f(n),
a·f(n/b),
a²·f(n/b²), ...
These combine to form a series which is often geometric or arithmetic.
Understanding which part dominates is key for tight bounds.
4. When f(n) Is Polynomial
These are the most common.
Example:
T(n) = T(n/2) + n²
Expansion:
= T(n/4) + (n/2)² + n²
= T(n/8) + (n/4)² + (n/2)² + n²
Pattern:
T(n) = T(n/2^k) + Σ (n/2^i)² for i = 0..k-1
Series:
Σ n² / 4^i = n² * (1 + 1/4 + 1/16 + ...)
This is a geometric series → converges.
Final:
T(n) = Θ(n²)
5. When f(n) Decreases Slowly (Logarithmic Cases)
Example:
T(n) = 2T(n/2) + log n
Expansion:
= 4T(n/4) + 2 log(n/2) + log n
= 8T(n/8) + 4 log(n/4) + 2 log(n/2) + log n
Series is:
Σ 2^i · log(n / 2^i)
This is not standard geometric because of the log(n/2^i) term.
Technique: split log term
log(n/2^i) = log n – i
Then handle separately:
Σ 2^i log n and Σ –i · 2^i
The first term grows like n log n
The second grows like n
Final:
T(n) = Θ(n log n)
This ability—to break apart and analyze non-uniform series—is what advanced recursion problems require.
6. When f(n) Is Itself Exponential
Example:
T(n) = T(n/2) + 2^n
The expansion shows:
2^n dominates everything
Even summing many smaller exponential terms doesn't change the asymptotic:
T(n) = Θ(2^n)
Unrolling instantly exposes exponential dominance.
7. Irregular Recurrences (Very Important)
Many interview or research problems violate the Master Theorem’s structure.
Example:
T(n) = T(n - 1) + log n
Expansion:
T(n) = log n + log(n-1) + log(n-2) + ... + log 1
That sum is:
log(n!) = Θ(n log n)
This is a classic trick:
convert a series into a well-known formula.
8. General Strategy (What Professionals Use)
When facing a new recurrence:
Step 1: Unroll until you see the pattern
Write 3–4 levels manually.
Step 2: Identify coefficient and argument patterns
What does aᵏ look like?
What does n/bᵏ become?
Step 3: Express the total work as a series
Write:
Σ a^i · f(n / b^i)
Step 4: Compare series growth
Is it geometric?
Arithmetic-geometric?
Telescoping?
Factorable (e.g., log terms)?
Step 5: Evaluate using known series behavior
Important patterns:
-
geometric series
-
harmonic series
-
log factorial
-
polynomial decay
-
exponential dominance
Step 6: Stop exactly at base case
Solve for k such that:
n / b^k = 1
9. Practice Problems
You can include these at the end of the blog.
1. Solve by expansion
T(n) = 3T(n/3) + n log n
2. Solve
T(n) = T(n/2) + n / log n
3. Solve
T(n) = 2T(n/4) + √n
4. Solve
T(n) = T(n-1) + 1/n
5. Solve
T(n) = T(n/2) + T(n/4) + n
Conclusion
The Method of Expansion is the mathematical backbone behind understanding recursion without relying on shortcuts.
It forces you to see:
-
how work accumulates
-
how terms dominate
-
how recursion depth shapes complexity
In the next post, a powerful topic:
“Akra–Bazzi Method — The Real Master Theorem for Professionals”
ALSO CHECK
ALSO CHECK
.png)
Comments
Post a Comment