Sub topic 5
The Substitution Method: Guess, Then Prove by Induction
A recursion tree gives you a believable answer. The substitution method turns that answer into a proof. You guess the form of the solution, then use mathematical induction to show that the guess holds for every large enough n, with explicit constants.
This chapter assumes you know Big-O, Big-Omega, and Big-Theta. It also assumes you can write a proof by strong induction and can manipulate logarithms such as lg(n/2) = lg n − 1. Here, lg means log base 2.
The method in four steps
- Guess the asymptotic form, usually from a recursion tree or by unrolling. An example guess is T(n) = O(n lg n).
- Write the guess as an inequality with a named constant. For example: there is a c > 0 such that T(n) ≤ c·n lg n for all n ≥ n₀.
- Prove the inductive step. Assume the inequality holds for all smaller arguments. Substitute it into the right side of the recurrence, then show the result is at most the same expression, with the same c.
- Handle the base cases. Pick n₀ and c so that the inequality holds for the smallest values the inductive step depends on.
O, Ω, and Θ do not appear inside the proof. They hide constants, and the proof is about the constants. You prove upper and lower bounds separately and combine them to get Θ.
Worked example: merge sort's recurrence
Take T(n) = 2T(⌊n/2⌋) + n with T(1) = 1. The recursion tree has about lg n levels, and each level does n work, so we guess T(n) = O(n lg n).
Claim. T(n) ≤ c·n lg n for all n ≥ 2, for some constant c.
Inductive step. Let n ≥ 4, so that ⌊n/2⌋ ≥ 2 and the hypothesis applies to ⌊n/2⌋:
- T(n) = 2T(⌊n/2⌋) + n
- ≤ 2c⌊n/2⌋ lg⌊n/2⌋ + n
- ≤ 2c(n/2) lg(n/2) + n, because ⌊n/2⌋ ≤ n/2 and x lg x is increasing
- = cn lg n − cn + n
- ≤ cn lg n, provided c ≥ 1.
The term −cn + n is the slack. When c ≥ 1, it absorbs the additive n.
Base cases. The step needs n ≥ 4, so n = 2 and n = 3 must be checked directly. Their recursive calls reach T(1).
| n | T(n) | c·n lg n | Requirement on c |
|---|---|---|---|
| 2 | 2·1 + 2 = 4 | 2c | c ≥ 2 |
| 3 | 2·1 + 3 = 5 | about 4.755c | c ≥ 1.06 |
With c = 2 and n₀ = 2, all conditions hold. We did not start at n = 1, because 1·lg 1 = 0 and T(1) = 1 > 0. Asymptotic notation only requires the bound for n ≥ n₀, so you are free to skip small n like this.
You can check the constants numerically. A numeric check does not replace the proof, but it catches arithmetic mistakes in your choice of c:
import math
from functools import lru_cache
@lru_cache(maxsize=None)
def T(n):
# T(n) = 2T(floor(n/2)) + n, T(1) = 1
return 1 if n <= 1 else 2 * T(n // 2) + n
c = 2
N = 100_000
violations = [n for n in range(2, N + 1) if T(n) > c * n * math.log2(n)]
print("violations:", violations[:5])
print("max ratio T(n)/(n lg n):", max(T(n) / (n * math.log2(n)) for n in range(2, N + 1)))The output reports no violations and a maximum ratio of 2.0, which occurs at n = 2. That confirms c = 2 is the smallest constant that works with n₀ = 2.
The lower bound
For Θ(n lg n), you also need T(n) ≥ c′·n lg n. Restrict to powers of two for clarity:
- T(n) ≥ 2c′(n/2) lg(n/2) + n = c′n lg n − c′n + n ≥ c′n lg n when c′ ≤ 1.
The base case is T(2) = 4 ≥ 2c′, which holds for c′ = 1. Floors make the general lower bound messier. A later chapter shows why they don't change the asymptotic answer.
When the step almost works: strengthen the hypothesis
Sometimes the guess is correct but the induction fails by a lower-order term. Consider T(n) = 2T(⌊n/2⌋) + 1 with T(1) = 1. A recursion tree suggests O(n).
Try T(n) ≤ cn:
- T(n) ≤ 2c⌊n/2⌋ + 1 ≤ cn + 1.
The result misses the target by 1, and no choice of c fixes that. The remedy seems backwards: prove a stronger statement. Guess T(n) ≤ cn − d:
- T(n) ≤ 2(c⌊n/2⌋ − d) + 1 ≤ cn − 2d + 1 ≤ cn − d, provided d ≥ 1.
The base case T(1) = 1 ≤ c − d holds with d = 1 and c = 2. So T(n) ≤ 2n − 1 for all n ≥ 1. The exact value for powers of two is 2n − 1, so this bound is tight.
A stronger hypothesis works because you also get to assume more in the inductive step. The subtracted term appears twice on the right side, once for each recursive call, but only once on the left. That leftover −d is the slack you need.
Changing variables
Some recurrences look unfamiliar but become familiar after a substitution. Take T(n) = 2T(√n) + lg n, and ignore rounding. Let m = lg n, so n = 2ᵐ and √n = 2^(m/2):
- T(2ᵐ) = 2T(2^(m/2)) + m.
Define S(m) = T(2ᵐ). Then S(m) = 2S(m/2) + m, which is merge sort's recurrence, so S(m) = Θ(m lg m). Converting back gives T(n) = Θ(lg n · lg lg n). To make this rigorous, run the substitution proof on S, or prove T(n) ≤ c·lg n·lg lg n directly. The change of variables gives you the guess.
Common errors and how to avoid them
| Error | Symptom | Fix |
|---|---|---|
| Using O inside the induction | "cn + n = O(n)" ends the proof | Keep explicit constants until the end |
| Base case at n = 1 for n lg n | Bound evaluates to 0 | Start at n₀ = 2 and check every base case the step reaches |
| Forgetting base cases created by floors | Step assumes ⌊n/2⌋ ≥ n₀ | Find the smallest n where the step applies and check every value below it |
| Treating c as something you can change | Constant changes from one line to the next | Fix c before the step and derive conditions on it |
| Proving only the upper bound | Claiming Θ from an O proof | Prove the Ω bound separately |
When to use substitution
Substitution is the most general method in this tutorial. It works for recurrences outside the master theorem's form, including uneven splits, non-polynomial driving functions, and odd base cases. It cannot find the answer for you, though. Use recursion trees or unrolling to get the guess, and use substitution to verify it.
Exercise
Prove that T(n) = 4T(n/2) + n, with T(1) = 1 and n a power of two, is O(n²). First show why the guess T(n) ≤ cn² fails in the inductive step, then repair it.
Show answer
With T(n) ≤ cn², the step gives 4c(n/2)² + n = cn² + n, which exceeds cn². Strengthen to T(n) ≤ cn² − dn. Then T(n) ≤ 4(c(n/2)² − d(n/2)) + n = cn² − 2dn + n ≤ cn² − dn whenever d ≥ 1. Base case: T(1) = 1 ≤ c − d, which holds with d = 1 and c = 2. So T(n) ≤ 2n² − n = O(n²). For powers of two this matches the exact solution, T(n) = 2n² − n.
Exercise
A student proves that T(n) = T(n − 1) + n is O(n) as follows: "Assume T(n − 1) ≤ c(n − 1). Then T(n) ≤ c(n − 1) + n = cn + (n − c) = O(n)." Identify the error and state the correct bound.
Show answer
The step must end with T(n) ≤ cn for the same c, but cn + n − c > cn once n > c, so the induction fails. The guess is too small, and subtracting a lower-order term will not repair it. Unrolling gives T(n) = T(1) + 2 + 3 + … + n = Θ(n²). Prove T(n) ≤ cn² as follows: c(n − 1)² + n = cn² − 2cn + c + n ≤ cn² when c ≥ 1 and n ≥ 1, since −2cn + c + n ≤ 0 in that range.
Exercise
Use a change of variables to find the asymptotic solution of T(n) = T(√n) + 1.
Show answer
Let m = lg n and S(m) = T(2ᵐ). Then S(m) = S(m/2) + 1, which is the binary-search recurrence, so S(m) = Θ(lg m). Therefore T(n) = Θ(lg lg n).