Sub topic 5
The Substitution Method: Guess, Then Prove by Induction
This chapter assumes you know Big-O and Big-Theta notation, proof by mathematical induction, and the basic log identities such as log(n/2) = log n − 1 for base-2 logs. A recursion tree gives you a believable guess. The substitution method turns that guess into a proof.
The method has two steps:
- Guess the form of the solution, for example T(n) = O(n log n).
- Use induction to prove that the guess holds for an explicit constant c.
A Worked Proof: Merge Sort
Merge sort's recurrence is T(n) = 2T(n/2) + n with T(1) = 1. The recursion tree suggests O(n log n), so we try to prove T(n) ≤ c·n log n for some constant c > 0. Assume n is a power of two, and use log base 2.
Inductive hypothesis. Assume T(m) ≤ c·m log m for all m < n, and in particular for m = n/2.
Inductive step. Substitute the hypothesis into the recurrence:
- T(n) ≤ 2·c(n/2) log(n/2) + n
- = cn(log n − 1) + n
- = cn log n − cn + n
- ≤ cn log n, provided c ≥ 1.
The step works because the term −cn absorbs the +n.
Base case. At n = 1 the bound gives c·1·log 1 = 0, but T(1) = 1, so the base fails there. Asymptotic bounds only need to hold for n ≥ n₀, so start the induction at n = 2 instead. T(2) = 4 ≤ c·2·1 holds when c ≥ 2. Choosing c = 2 satisfies both the base case and the step.
You can check the constant numerically before you write the proof. This script also uses floors, so it tests inputs that are not powers of two:
import math
from functools import lru_cache
@lru_cache(maxsize=None)
def T(n):
return 1 if n <= 1 else 2 * T(n // 2) + n
c = 2
bad = [n for n in range(2, 100_000) if T(n) > c * n * math.log2(n)]
print("violations:", bad[:5]) # violations: []Common Errors
Changing the constant partway through
Suppose you try to prove T(n) = O(n) for merge sort. The step gives T(n) ≤ 2c(n/2) + n = cn + n, and someone writes "= O(n), done." That proof is wrong. The hypothesis was T(n) ≤ cn with the same c, and cn + n is larger than cn. The inequality has to close exactly, with the same constant on both sides.
Giving up when a lower-order term gets in the way
Take T(n) = 2T(n/2) + 1. The true answer is Θ(n). Trying T(n) ≤ cn gives cn + 1, which fails by exactly 1. Don't switch to a bigger guess. Strengthen the hypothesis instead, to T(n) ≤ cn − d:
- T(n) ≤ 2(c·n/2 − d) + 1 = cn − 2d + 1 ≤ cn − d, provided d ≥ 1.
A stronger hypothesis is also a stronger assumption to use in the step, so this kind of proof often goes through where the weaker one fails.
Forgetting the base case
The inductive step alone proves nothing. As the merge sort example showed, the base case is often what forces the value of c or the starting point n₀.
Exercise
Use substitution to prove that T(n) = T(n/2) + 1 with T(1) = 1 is O(log n). Use the hypothesis T(n) ≤ c log n for n ≥ 2.
Show answer
Inductive step: T(n) ≤ c log(n/2) + 1 = c log n − c + 1 ≤ c log n when c ≥ 1. Base case: T(2) = T(1) + 1 = 2 ≤ c·log 2 = c, so c ≥ 2. Choosing c = 2 and n₀ = 2 works.
Exercise
A student claims T(n) = 4T(n/2) + n is O(n²). With the hypothesis T(n) ≤ cn², the step gives cn² + n. How do you fix the proof?
Show answer
Strengthen the hypothesis to T(n) ≤ cn² − dn. Then T(n) ≤ 4(c(n/2)² − d(n/2)) + n = cn² − 2dn + n ≤ cn² − dn when d ≥ 1. Then pick c large enough for the base case.