The Substitution Method
The substitution method is the most general way to solve a recurrence, and the most honest one: you guess the form of the answer and then prove the guess by induction, with real constants. It needs a good guess, which the recursion trees of the next lesson are good at producing, but once the proof goes through there is nothing left to doubt. Other methods, including the master theorem, are at bottom substitution proofs done once and for all.
The induction here is the same tool as in Induction and Strong Induction. What is new is the bookkeeping with constants, which is where most mistakes happen.
The method in two steps
- Guess the form of the solution, with unknown constants: " for all ".
- Prove it by strong induction on . Assume the bound for every size smaller than , substitute it into the recurrence's right-hand side, and show that the bound follows for . Then check the base cases and pick constants and that make every step true.
The constants are chosen during the proof. The inductive step usually says "this works if something", and the base cases say "this works if something else"; take the larger.
A full example: merge sort's recurrence
Let
Guess: , that is, for all .
Inductive step. Take and assume the bound for every with . Since , the hypothesis applies to it:
Base cases. The bound cannot hold at , because . That is fine: asymptotic notation only asks for the bound from some on, so start at . The inductive step needs , which holds from , so and are base cases, the two sizes whose recursion lands on . Now and . The bound needs and , so covers both.
With and every step holds, so for all . A computer cannot replace the proof, but it can catch an arithmetic slip in it:
import math
N = 100_000
T = [0] * (N + 1)
T[1] = 1
for n in range(2, N + 1):
T[n] = 2 * T[n // 2] + n
ratios = [(T[n] / (n * math.log2(n)), n) for n in range(2, N + 1)]
print("T(n) <= 2 n lg n for all 2 <= n <= 100000:", all(T[n] <= 2 * n * math.log2(n) for n in range(2, N + 1)))
print("largest T(n) / (n lg n) = %.4f at n = %d" % max(ratios))
print("smallest T(n) / (n lg n) = %.4f at n = %d" % min(ratios))T(n) <= 2 n lg n for all 2 <= n <= 100000: True
largest T(n) / (n lg n) = 2.0000 at n = 2
smallest T(n) / (n lg n) = 0.8361 at n = 15
The largest ratio is exactly at the base case , which is the reason had to be 2 rather than 1. The smallest ratio stays well above 0, a hint that as well; a lower-bound proof by substitution runs the same way with the inequalities reversed, so .
When the induction almost works: subtract a lower-order term
Sometimes the right guess fails the inductive step by a hair. Take the recursive maximum's comparison count, written with one comparison per call:
The guess is right, but try to prove :
That is not for any . The bound is off by a constant, and no choice of absorbs it. The fix looks backwards at first: prove something stronger, with a constant .
The base case needs , so , works, and in fact exactly. The stronger statement succeeds because the induction hypothesis is stronger too: each of the two calls gives back a , and together they pay for the .
from functools import lru_cache
@lru_cache(None)
def T(n):
return 1 if n == 1 else T((n + 1) // 2) + T(n // 2) + 1
print(" n T(n) 2n-1 T(n) - c n with c = 2")
for n in [1, 2, 3, 5, 8, 13, 100, 1000]:
print(f"{n:4} {T(n):4} {2 * n - 1:4} {T(n) - 2 * n:5}") n T(n) 2n-1 T(n) - c n with c = 2
1 1 1 -1
2 3 3 -1
3 5 5 -1
5 9 9 -1
8 15 15 -1
13 25 25 -1
100 199 199 -1
1000 1999 1999 -1
The last column is for every : the bound holds with room exactly to spare, which is what the strengthened induction exploits.
A classic false proof
Here is a "proof" that is . Guess . Then
The error: the claim to prove was with the same constant as in the hypothesis, and the computation produced . Concluding "" swaps the precise statement for a vague one, and each level of the induction quietly increases the constant. After levels the "constant" has grown by about , which is exactly the factor the false proof lost. The first example already shows it: stays between constants, so grows like and is not bounded.
Rule: in a substitution proof, never write , or inside the induction. Write explicit constants, and end the inductive step with exactly the inequality you assumed.
Changing variables
Some recurrences become familiar after a substitution in the argument. Consider
The square root is unusual, but write , so that and , and ignore the rounding for now. Defining turns the recurrence into
which is merge sort's recurrence, so . Translating back with :
A numerical check on the values , where the square roots are exact (Python's integers are unbounded, so is no problem):
import math
from functools import lru_cache
@lru_cache(None)
def T(n):
if n <= 2:
return 1
return 2 * T(math.isqrt(n)) + (n.bit_length() - 1) # bit_length - 1 = floor(lg n)
print(" k lg n T(n) T(n) / (lg n lg lg n)")
for k in range(1, 11):
n = 2 ** (2 ** k)
lg = 2 ** k
print(f"{k:2} {lg:5} {T(n):5} {T(n) / (lg * math.log2(lg)):.4f}") k lg n T(n) T(n) / (lg n lg lg n)
1 2 4 2.0000
2 4 12 1.5000
3 8 32 1.3333
4 16 80 1.2500
5 32 192 1.2000
6 64 448 1.1667
7 128 1024 1.1429
8 256 2304 1.1250
9 512 5120 1.1111
10 1024 11264 1.1000
With and base , the exact solution is , so the ratio is , slowly approaching 1. It is exactly what the table shows. A change of variables gives the guess; a substitution proof on the original recurrence (or on , with care for the rounding) makes it a theorem.
Test yourself
A substitution proof of for , , cannot use as its base case. Why?
- Because
- Because , which is less than
- Because the recurrence is undefined at 1
- Because must be at least
Trying to prove for leads to . What is the standard fix?
- Guess instead
- Prove the stronger bound for a suitable constant
- Conclude anyway, since
- Change variables with
What is wrong with the step in a proof that ?
- Nothing, it is a valid proof
- The inductive step must end with for the same , but it gives
- The base case is missing, otherwise it is fine
- is not
After substituting and , the recurrence becomes . What is ?