TExcelTAD
Home › Foundations of Algorithm Analysis › The Substitution Method

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

  1. Guess the form of the solution, with unknown constants: "T(n)≤c nlg⁡nT(n) \le c\,n \lg n for all n≥n0n \ge n_0".
  2. Prove it by strong induction on nn. Assume the bound for every size smaller than nn, substitute it into the recurrence's right-hand side, and show that the bound follows for nn. Then check the base cases and pick constants cc and n0n_0 that make every step true.

The constants are chosen during the proof. The inductive step usually says "this works if c≥c \ge something", and the base cases say "this works if c≥c \ge something else"; take the larger.

A full example: merge sort's recurrence

Let

T(n)=2T(⌊n/2⌋)+nfor n≥2,T(1)=1.T(n) = 2T(\lfloor n/2 \rfloor) + n \quad \text{for } n \ge 2, \qquad T(1) = 1.

Guess: T(n)=O(nlg⁡n)T(n) = O(n \lg n), that is, T(n)≤c nlg⁡nT(n) \le c\,n \lg n for all n≥n0n \ge n_0.

Inductive step. Take n≥4n \ge 4 and assume the bound for every kk with 2≤k<n2 \le k < n. Since ⌊n/2⌋≥2\lfloor n/2 \rfloor \ge 2, the hypothesis applies to it:

T(n)=2T(⌊n/2⌋)+n≤2c⌊n/2⌋lg⁡⌊n/2⌋+ninduction hypothesis≤2c (n/2)lg⁡(n/2)+n⌊n/2⌋≤n/2=c nlg⁡n−c n+n≤c nlg⁡nif c≥1.\begin{aligned} T(n) &= 2T(\lfloor n/2 \rfloor) + n \\ &\le 2c \lfloor n/2 \rfloor \lg \lfloor n/2 \rfloor + n && \text{induction hypothesis} \\ &\le 2c \, (n/2) \lg (n/2) + n && \lfloor n/2 \rfloor \le n/2 \\ &= c\,n \lg n - c\,n + n \\ &\le c\,n \lg n && \text{if } c \ge 1. \end{aligned}

Base cases. The bound cannot hold at n=1n = 1, because c⋅1⋅lg⁡1=0<1=T(1)c \cdot 1 \cdot \lg 1 = 0 < 1 = T(1). That is fine: asymptotic notation only asks for the bound from some n0n_0 on, so start at n0=2n_0 = 2. The inductive step needs ⌊n/2⌋≥2\lfloor n/2 \rfloor \ge 2, which holds from n=4n = 4, so n=2n = 2 and n=3n = 3 are base cases, the two sizes whose recursion lands on T(1)T(1). Now T(2)=2⋅1+2=4T(2) = 2 \cdot 1 + 2 = 4 and T(3)=2⋅1+3=5T(3) = 2 \cdot 1 + 3 = 5. The bound needs 4≤2c4 \le 2c and 5≤3clg⁡3≈4.75c5 \le 3c \lg 3 \approx 4.75c, so c≥2c \ge 2 covers both.

With c=2c = 2 and n0=2n_0 = 2 every step holds, so T(n)≤2nlg⁡nT(n) \le 2n \lg n for all n≥2n \ge 2. A computer cannot replace the proof, but it can catch an arithmetic slip in it:

python · try it yourself
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 n=2n = 2, which is the reason cc had to be 2 rather than 1. The smallest ratio stays well above 0, a hint that T(n)=Ω(nlg⁡n)T(n) = \Omega(n \lg n) as well; a lower-bound proof by substitution runs the same way with the inequalities reversed, so T(n)=Θ(nlg⁡n)T(n) = \Theta(n \lg n).

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:

T(n)=T(⌈n/2⌉)+T(⌊n/2⌋)+1,T(1)=1.T(n) = T(\lceil n/2 \rceil) + T(\lfloor n/2 \rfloor) + 1, \qquad T(1) = 1.

The guess T(n)=O(n)T(n) = O(n) is right, but try to prove T(n)≤cnT(n) \le cn:

T(n)≤c⌈n/2⌉+c⌊n/2⌋+1=cn+1.T(n) \le c \lceil n/2 \rceil + c \lfloor n/2 \rfloor + 1 = cn + 1.

That is not ≤cn\le cn for any cc. The bound is off by a constant, and no choice of cc absorbs it. The fix looks backwards at first: prove something stronger, T(n)≤cn−dT(n) \le cn - d with a constant d≥0d \ge 0.

T(n)≤(c⌈n/2⌉−d)+(c⌊n/2⌋−d)+1=cn−2d+1≤cn−dif d≥1.T(n) \le (c \lceil n/2 \rceil - d) + (c \lfloor n/2 \rfloor - d) + 1 = cn - 2d + 1 \le cn - d \quad \text{if } d \ge 1.

The base case needs T(1)=1≤c−dT(1) = 1 \le c - d, so d=1d = 1, c=2c = 2 works, and in fact T(n)=2n−1T(n) = 2n - 1 exactly. The stronger statement succeeds because the induction hypothesis is stronger too: each of the two calls gives back a dd, and together they pay for the +1+1.

python · try it yourself
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 −1-1 for every nn: the bound T(n)≤2nT(n) \le 2n holds with room exactly d=1d = 1 to spare, which is what the strengthened induction exploits.

A classic false proof

Here is a "proof" that T(n)=2T(⌊n/2⌋)+nT(n) = 2T(\lfloor n/2 \rfloor) + n is O(n)O(n). Guess T(n)≤cnT(n) \le cn. Then

T(n)≤2c⌊n/2⌋+n≤cn+n=O(n).(wrong!)T(n) \le 2c \lfloor n/2 \rfloor + n \le cn + n = O(n). \quad \text{(wrong!)}

The error: the claim to prove was T(n)≤cnT(n) \le cn with the same constant cc as in the hypothesis, and the computation produced (c+1)n(c + 1)n. Concluding "=O(n)= O(n)" swaps the precise statement for a vague one, and each level of the induction quietly increases the constant. After lg⁡n\lg n levels the "constant" has grown by about lg⁡n\lg n, which is exactly the factor the false proof lost. The first example already shows it: T(n)/(nlg⁡n)T(n)/(n \lg n) stays between constants, so T(n)/nT(n)/n grows like lg⁡n\lg n and is not bounded.

Rule: in a substitution proof, never write OO, Ω\Omega or Θ\Theta 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

T(n)=2T(⌊n⌋)+lg⁡n.T(n) = 2T(\lfloor \sqrt{n} \rfloor) + \lg n.

The square root is unusual, but write n=2mn = 2^m, so that n=2m/2\sqrt{n} = 2^{m/2} and lg⁡n=m\lg n = m, and ignore the rounding for now. Defining S(m)=T(2m)S(m) = T(2^m) turns the recurrence into

S(m)=2S(m/2)+m,S(m) = 2S(m/2) + m,

which is merge sort's recurrence, so S(m)=Θ(mlg⁡m)S(m) = \Theta(m \lg m). Translating back with m=lg⁡nm = \lg n:

T(n)=Θ(lg⁡nlg⁡lg⁡n).T(n) = \Theta(\lg n \lg \lg n).

A numerical check on the values n=22kn = 2^{2^k}, where the square roots are exact (Python's integers are unbounded, so n=21024n = 2^{1024} is no problem):

python · try it yourself
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 m=2km = 2^k and base S(1)=T(2)=1S(1) = T(2) = 1, the exact solution is S(m)=mlg⁡m+mS(m) = m \lg m + m, so the ratio is 1+1/lg⁡m=1+1/k1 + 1/\lg m = 1 + 1/k, 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 SS, with care for the rounding) makes it a theorem.

Test yourself

A substitution proof of T(n)≤c nlg⁡nT(n) \le c\,n \lg n for T(n)=2T(⌊n/2⌋)+nT(n) = 2T(\lfloor n/2 \rfloor) + n, T(1)=1T(1) = 1, cannot use n=1n = 1 as its base case. Why?

  1. Because ⌊1/2⌋=0\lfloor 1/2 \rfloor = 0
  2. Because c⋅1⋅lg⁡1=0c \cdot 1 \cdot \lg 1 = 0, which is less than T(1)=1T(1) = 1
  3. Because the recurrence is undefined at 1
  4. Because cc must be at least nn

Trying to prove T(n)≤cnT(n) \le cn for T(n)=2T(n/2)+1T(n) = 2T(n/2) + 1 leads to T(n)≤cn+1T(n) \le cn + 1. What is the standard fix?

  1. Guess T(n)≤cn2T(n) \le cn^2 instead
  2. Prove the stronger bound T(n)≤cn−dT(n) \le cn - d for a suitable constant dd
  3. Conclude T(n)=O(n)T(n) = O(n) anyway, since cn+1=O(n)cn + 1 = O(n)
  4. Change variables with n=2mn = 2^m

What is wrong with the step T(n)≤2c(n/2)+n=cn+n=O(n)T(n) \le 2c(n/2) + n = cn + n = O(n) in a proof that T(n)=O(n)T(n) = O(n)?

  1. Nothing, it is a valid proof
  2. The inductive step must end with T(n)≤cnT(n) \le cn for the same cc, but it gives (c+1)n(c + 1)n
  3. The base case is missing, otherwise it is fine
  4. cn+ncn + n is not O(n)O(n)

After substituting n=2mn = 2^m and S(m)=T(2m)S(m) = T(2^m), the recurrence T(n)=2T(n)+lg⁡nT(n) = 2T(\sqrt{n}) + \lg n becomes S(m)=2S(m/2)+mS(m) = 2S(m/2) + m. What is T(n)T(n)?

  1. Θ(lg⁡n)\Theta(\lg n)
  2. Θ(lg⁡nlg⁡lg⁡n)\Theta(\lg n \lg \lg n)
  3. Θ(nlg⁡n)\Theta(n \lg n)
  4. Θ(n)\Theta(\sqrt{n})