3.1.8Complexity Analysis

Substitution method for recurrences

1,863 words8 min readdifficulty · medium1 backlinks

What is it?

WHAT we are really doing: picking a candidate bound f(n)f(n) and proving the inductive invariant "T(k)cf(k)T(k) \le c\,f(k) for all k<nk < n" forces "T(n)cf(n)T(n) \le c\,f(n)".


HOW: the recipe (derivation from first principles)

We never dump a formula. Here is the engine, step by step.

Suppose T(n)=aT(n/b)+h(n)T(n) = a\,T(n/b) + h(n).

  1. Guess T(n)cf(n)T(n) \le c\,f(n) — call this the inductive hypothesis (IH).
  2. Assume IH holds for all smaller values, in particular for n/bn/b: T(n/b)cf(n/b).T(n/b) \le c\,f(n/b). Why this step? Induction lets us assume the claim for inputs smaller than nn; n/b<nn/b < n qualifies.
  3. Substitute into the recurrence: T(n)=aT(n/b)+h(n)acf(n/b)+h(n).T(n) = a\,T(n/b) + h(n) \le a\,c\,f(n/b) + h(n). Why this step? Replacing T(n/b)T(n/b) by its (assumed) upper bound only increases or preserves the right side — legal for an upper bound.
  4. Massage the right side until it is cf(n)\le c\,f(n). The leftover terms must be absorbed. If you can choose cc so the residual is 0\le 0, you win.
  5. Base case: check small nn (e.g. n=1n=1) so the induction is grounded.


Worked Example 1 — T(n)=2T(n/2)+nT(n) = 2T(n/2) + n


Worked Example 2 — the subtle constant trap


Worked Example 3 — the subtract-a-lower-order-term trick




Recall Feynman: explain to a 12-year-old

Imagine a magic box that, to answer a big question, asks two smaller copies of itself and adds a little work of its own. You want to know how long the whole thing takes. You make a smart guess ("I bet it takes about nlognn\log n seconds"). Then you play a game: pretend the smaller boxes already obey your guess, plug that in, and check whether the big box still obeys it. If the leftover work disappears (becomes zero or negative), your guess was right! If a stubborn piece won't go away, your guess was wrong and you try a bigger one. You also check the tiniest box by hand so the whole chain of "pretends" stands on solid ground.


Active Recall

What are the two steps of the substitution method?
(1) Guess the form of the solution; (2) prove it by induction, finding the constants.
Why is the method named "substitution"?
You substitute the inductive hypothesis (the guess for smaller inputs) back into the recurrence to verify the bound.
In proving T(n)cf(n)T(n)\le c\,f(n), what must happen to the residual/leftover terms?
They must be 0\le 0 for a single fixed constant cc (i.e. fully absorbed).
For T(n)=2T(n/2)+nT(n)=2T(n/2)+n, what is the tight bound and why isn't it O(n)O(n)?
Θ(nlogn)\Theta(n\log n); assuming O(n)O(n) gives (c+1)n(c+1)n, an extra +n+n that no fixed cc can absorb.
Why can't the constant grow from cc to c+1c+1 during the inductive step?
Induction must reproduce the SAME cc; a per-level growth accumulates over the logn\log n levels, blowing up the bound.
What is the "strengthen the hypothesis" trick?
Guess T(n)cf(n)dT(n)\le c\,f(n)-d (subtract a lower-order term) to gain slack that absorbs stubborn residuals.
Why must you check a base case?
Without grounding the induction, any false claim could be "proved"; also it fixes cc for small nn.
Why pick n0=2n_0=2 rather than 11 for an nlognn\log n bound?
log1=0\log 1 = 0 makes cnlogn=0c\,n\log n = 0 at n=1n=1, which can't bound a positive T(1)T(1).

Connections

Concept Map

has shape induction loves

enables

first do

then do

relies on

applied to smaller input

plug into recurrence

simplify

choose c so residual le 0

grounded by

verified in

needs n0 = 2

Recurrence T of n hides runtime

Proof by induction

Substitution method

Step 1 Guess closed form

Step 2 Prove by induction

Inductive hypothesis T of k le c f k

Assume IH for n over b

Substitute into recurrence

Massage right side

Absorb residual le 0

Base case at n0

Example 2T of n over 2 plus n = O of n log n

Hinglish (regional understanding)

Intuition Hinglish mein samjho

Substitution method ka idea simple hai: recurrence (jaise T(n)=2T(n/2)+nT(n)=2T(n/2)+n) apne answer ko apne andar chhupa ke rakhta hai. Hum pehle ek smart guess maarte hain — "bhai, ye toh O(nlogn)O(n\log n) lag raha hai" — aur phir induction se prove karte hain. Isko "substitution" isliye kehte hain kyunki hum apna guess (smaller input ke liye) wapas recurrence mein substitute karke check karte hain ki inequality survive karti hai ya nahi.

Recipe yaad rakho — G-A-S-A-B: Guess karo, smaller nn ke liye Assume karo, recurrence mein Substitute karo, jo leftover bachta hai usko Absorb karo (woh 0\le 0 hona chahiye SAME constant cc ke saath), aur Base case check karo. Agar leftover term ((c1)n-(c-1)n wala) negative ho gaya, matlab guess sahi hai.

Sabse bada trap: agar tumhe (c+1)n(c+1)n mila aur tum khush ho gaye ki "linear hi toh hai", toh galat! Induction mein wahi cc wapas aana chahiye, badhna nahi chahiye. Constant har level pe badhta jaaye toh logn\log n levels mil ke pura nlognn\log n ban jaata hai. Isiliye T(n)=2T(n/2)+nT(n)=2T(n/2)+n ka actual answer Θ(nlogn)\Theta(n\log n) hai, O(n)O(n) nahi.

Aur ek pro-tip: kabhi-kabhi seedha guess fail hota hai chhote se leftover ke wajah se. Tab hypothesis ko strong karoT(n)cndT(n)\le cn - d guess karo. Ulta lagta hai, par stronger assumption se inductive step mein zyada cheez maan-ne ko milti hai, jisse proof aasaan ho jaata hai. Base case kabhi mat bhoolna, aur nlognn\log n ke liye n0=2n_0=2 se shuru karo kyunki log1=0\log 1=0 hota hai.

Go deeper — visual, from zero

Test yourself — Complexity Analysis

Connections