Sub topic 5
Recurrence Relations: Substitution and Recursion Trees
This chapter assumes you know O, Omega, and Theta notation, can sum a geometric series, and can read a recursive function. It covers two ways to solve a recurrence: recursion trees, which give a guess, and substitution, which proves it. The next chapter covers the Master theorem, a shortcut for one common recurrence form.
What a recurrence is
A recurrence relation expresses the cost of a recursive algorithm on input size n in terms of its cost on smaller inputs. A recurrence has two parts:
- A recursive case: the cost of splitting the problem, solving the subproblems, and combining the results.
- A base case: the cost when the input is small enough to solve directly, usually T(1) = Θ(1).
Binary search compares the key with the middle element and recurses on one half. Its recurrence is:
T(n) = T(n/2) + O(1)
Merge sort sorts two halves and merges them in linear time:
T(n) = 2T(n/2) + Θ(n)
Writing the recurrence is the first step. Solving it means finding a closed form or a tight asymptotic bound.
Reading a recurrence off code
Count three things: how many recursive calls are made, how large each call's input is, and how much work is done outside the calls.
def example(a, lo, hi):
if hi - lo <= 1:
return 1
mid = (lo + hi) // 2
total = 0
for i in range(lo, hi): # linear work: n steps
total += a[i]
total += example(a, lo, mid) # one call on n/2
total += example(a, mid, hi) # one call on n/2
return total
print(example(list(range(16)), 0, 16))There are two calls on halves and a linear loop, so T(n) = 2T(n/2) + n. Slicing a list in Python, as in a[:mid], also costs time proportional to the slice. Count it as part of the non-recursive work.
Method 1: Recursion trees
A recursion tree draws every call as a node and writes the non-recursive cost of that call inside the node. You then sum the cost level by level and add up the levels.
For T(n) = 2T(n/2) + n:
Work through the levels:
| Level | Number of nodes | Size of each node | Cost of the level |
|---|---|---|---|
| 0 | 1 | n | n |
| 1 | 2 | n/2 | n |
| 2 | 4 | n/4 | n |
| i | 2^i | n/2^i | n |
The recursion stops when the size reaches 1, which happens when n/2^i = 1, so i = log₂ n. The tree has log₂ n + 1 levels. Each level costs n, and the n leaves cost Θ(1) each.
Total: n · log₂ n + Θ(n) = Θ(n log n).
A tree where the levels are not equal
Take T(n) = T(n/3) + T(2n/3) + n. The two branches shrink at different rates, so the tree is unbalanced.
- The shortest path (always taking n/3) has depth log₃ n.
- The longest path (always taking 2n/3) has depth log_{3/2} n.
- Each full level costs at most n.
Every level that is completely filled costs exactly n, and the deeper levels cost less because some branches have already ended. So the total is at most n · log_{3/2} n, which is O(n log n). The total is at least n · log₃ n, so it is also Ω(n log n). Therefore T(n) = Θ(n log n).
Cases where the cost per level changes
Consider T(n) = 2T(n/2) + n². The level costs are:
- Level 0: n²
- Level 1: 2 · (n/2)² = n²/2
- Level 2: 4 · (n/4)² = n²/4
- Level i: n²/2^i
The sum is n² · (1 + 1/2 + 1/4 + …), a decreasing geometric series bounded by 2n². The root dominates, so T(n) = Θ(n²).
Now take T(n) = 4T(n/2) + n. Level i costs 4^i · (n/2^i) = n · 2^i. This series grows, so the leaves dominate. There are 4^(log₂ n) = n² leaves, so T(n) = Θ(n²).
These three outcomes are the same pattern the Master theorem classifies: the cost is spread evenly, dominated by the root, or dominated by the leaves.
Method 2: Substitution
The substitution method has two steps:
- Guess the form of the solution, usually from a recursion tree.
- Prove the guess by induction, which also finds the constants.
Worked example
Prove that T(n) = 2T(n/2) + n is O(n log n). Guess T(n) ≤ c · n · log₂ n for some constant c > 0, and assume it holds for all smaller values, including n/2.
Substitute the hypothesis for T(n/2):
T(n) ≤ 2 · c · (n/2) · log₂(n/2) + n
= c · n · (log₂ n − 1) + n
= c · n · log₂ n − c · n + n
= c · n · log₂ n − (c − 1) · n
This is at most c · n · log₂ n whenever c ≥ 1. The inductive step holds. The base case needs care. At n = 1, log₂ 1 = 0, so the claim T(1) ≤ 0 is false. The fix is to treat n = 2 and n = 3 as base cases, where the bound c · n · log₂ n is positive and a large enough c covers T(2) and T(3). The recurrence for larger n only refers back to those values.
Common mistakes
- Dropping the constant in the hypothesis. Proving T(n) ≤ n log n instead of T(n) ≤ c · n log n can fail, because the leftover term cannot be absorbed.
- Concluding too early. Showing T(n) ≤ c · n · log n + n does not prove T(n) ≤ c · n · log n. You must reach exactly the hypothesis form.
- Ignoring the base case. The induction must start at values where the claim is true.
When the guess is almost right
Try T(n) = 2T(⌊n/2⌋) + 1 with the guess T(n) ≤ c · n. Substituting gives T(n) ≤ 2 · c · ⌊n/2⌋ + 1 ≤ c · n + 1. This exceeds c · n, so the proof fails even though T(n) = Θ(n) is true. Strengthen the hypothesis by subtracting a lower-order term: guess T(n) ≤ c · n − d. Then:
T(n) ≤ 2(c · n/2 − d) + 1 = c · n − 2d + 1
This is at most c · n − d when d ≥ 1. The stronger statement is easier to carry through induction.
Iterating a recurrence by hand
For simple recurrences, expand repeatedly until a pattern appears. For T(n) = T(n − 1) + n with T(1) = 1:
T(n) = n + (n − 1) + (n − 2) + … + 1 = n(n + 1)/2 = Θ(n²)
This form, where the input shrinks by a constant instead of a fraction, is common in GATE questions. The Master theorem does not apply to it. Examples:
| Recurrence | Expansion | Result |
|---|---|---|
| T(n) = T(n − 1) + 1 | n additions of 1 | Θ(n) |
| T(n) = T(n − 1) + n | sum of 1 to n | Θ(n²) |
| T(n) = 2T(n − 1) + 1 | 1 + 2 + 4 + … + 2^(n−1) | Θ(2ⁿ) |
| T(n) = T(n/2) + 1 | log₂ n additions of 1 | Θ(log n) |
| T(n) = T(n/2) + n | n + n/2 + n/4 + … | Θ(n) |
The recurrence T(n) = T(n − 1) + n is also the worst case of quick sort. A bad pivot removes only one element per level, so the partition cost n is paid at n levels.
Checking a solution numerically
You can compute a recurrence directly and compare it with the claimed bound. The ratio T(n) / (n log₂ n) should settle near a constant if the bound is tight.
import math
from functools import lru_cache
@lru_cache(maxsize=None)
def T(n):
if n <= 1:
return 1
return 2 * T(n // 2) + n
for k in range(4, 21, 4):
n = 2 ** k
print(n, T(n), round(T(n) / (n * math.log2(n)), 3))For n = 2^k, T(n) = n · k + n, so the ratio approaches 1 as k grows. A ratio that keeps growing means your bound is too low. A ratio that shrinks toward zero means it is too high.
Which method to use
- Use a recursion tree to find the answer quickly and to see where the cost sits.
- Use substitution when you need a rigorous proof or when the recurrence is not of a standard form.
- Use iteration for recurrences where the size drops by a constant.
- Use the Master theorem, covered next, for recurrences of the form T(n) = aT(n/b) + f(n).
Exercise
Solve T(n) = 3T(n/3) + n with T(1) = 1 using a recursion tree. State the cost of each level, the number of levels, and the final bound.
Show answer
At level i there are 3^i nodes, each of size n/3^i and cost n/3^i, so each level costs 3^i · (n/3^i) = n. The size reaches 1 when i = log₃ n, so there are log₃ n + 1 levels. The total is n · log₃ n + Θ(n), which is Θ(n log n).
Exercise
A function satisfies T(n) = T(n − 1) + log n with T(1) = 1. Find its asymptotic growth.
Show answer
Expanding gives T(n) = 1 + log 2 + log 3 + … + log n = log(n!) + 1. By Stirling's approximation, log(n!) = Θ(n log n), so T(n) = Θ(n log n).
Exercise
Using substitution, a student guesses T(n) ≤ c · n for T(n) = 2T(n/2) + 1 and gets T(n) ≤ c · n + 1, which does not close. What should the student change, and why does it work?
Show answer
Strengthen the hypothesis to T(n) ≤ c · n − d for some d ≥ 1. Substituting gives T(n) ≤ 2(c · n/2 − d) + 1 = c · n − 2d + 1, which is at most c · n − d when d ≥ 1. The extra subtracted term absorbs the +1 added at each step.