3.1.9Complexity Analysis

Recursion tree method

2,037 words9 min readdifficulty · medium1 backlinks

What is a recursion tree?

The three questions you must answer for any tree:

  1. HOW DEEP is it? (number of levels) — controlled by how fast nn shrinks.
  2. HOW WIDE is each level? (number of nodes at depth ii) — controlled by branching aa.
  3. HOW MUCH work per level? (sum of node values at depth ii).

Then T(n)=levels i(work at level i)T(n) = \sum_{\text{levels } i} (\text{work at level } i).

Figure — Recursion tree method

Deriving the level counts from scratch

Take the canonical divide-and-conquer recurrence: T(n)=aT(n/b)+f(n),a1,  b>1.T(n) = a\,T(n/b) + f(n), \quad a\ge 1,\; b>1.


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

Here a=2, b=2, f(n)=na=2,\ b=2,\ f(n)=n.

Step What we do Why this step?
Depth log2n\log_2 n levels Size halves each level: n/2i=1i=log2nn/2^i = 1 \Rightarrow i=\log_2 n.
Nodes at level ii 2i2^i Branching factor a=2a=2.
Node size at level ii n/2in/2^i Size shrinks by b=2b=2.
Work per level 2in2i=n2^i \cdot \frac{n}{2^i} = n The 2i2^i cancels — every level costs nn.
Total n×(log2n+1)=Θ(nlogn)n \times (\log_2 n + 1) = \Theta(n\log n) Equal work per level × number of levels.

Worked Example 2 — T(n)=3T(n/4)+n2T(n) = 3T(n/4) + n^2

Here a=3, b=4, f(n)=n2a=3,\ b=4,\ f(n)=n^2.

Step Result Why this step?
Work at level ii 3i(n4i)2=n2(316)i3^i\left(\frac{n}{4^i}\right)^2 = n^2\left(\frac{3}{16}\right)^i Plug into aif(n/bi)a^i f(n/b^i); group powers of ii.
Ratio 316<1\frac{3}{16} < 1 Geometric series with ratio <1<1decreasing.
Sum n2i(3/16)in2113/16=Θ(n2)n^2\sum_i (3/16)^i \le n^2 \cdot \frac{1}{1-3/16} = \Theta(n^2) Convergent geometric series → constant factor.
Total Θ(n2)\Theta(n^2) Root dominates — top level work wins.

Worked Example 3 — Unbalanced tree: T(n)=T(n/3)+T(2n/3)+nT(n) = T(n/3) + T(2n/3) + n

Subproblems shrink at different rates, so we can't use bb directly. We bound the height.

Step Result Why this step?
Work per level n\le n (sizes at a level sum to n\le n) n/3+2n/3=nn/3 + 2n/3 = n; cost ff is linear, so each level sums to about nn.
Shortest path log3n\log_3 n (divide by 3) The 1/31/3 branch shrinks fastest.
Longest path log3/2n\log_{3/2} n (multiply by 2/32/3) The 2/32/3 branch shrinks slowest → deepest.
Total between nlog3nn\log_3 n and nlog3/2nn\log_{3/2} nΘ(nlogn)\Theta(n\log n) Levels ×\times per-level work nn, all logs differ by constants.


The 80/20 takeaway


Recall Feynman: explain to a 12-year-old

Imagine a boss with a big job. He splits it into smaller jobs and hands them to helpers. Each helper splits their job again, and so on, until jobs are tiny. Draw this as a family tree of jobs. To find how much total effort the whole company spends, write the effort next to each person and add up everyone — row by row is easiest. Sometimes the boss does the most work (top of the tree), sometimes the tiny workers at the bottom do (because there are SO many of them), and sometimes everyone does an equal share. The recursion tree just helps you see who's doing the heavy lifting.


Flashcards

What three quantities must you find for any recursion tree?
Depth (logbn\log_b n), nodes per level (aia^i), and work per level (aif(n/bi)a^i f(n/b^i)).
Total-work formula from a recursion tree for T(n)=aT(n/b)+f(n)T(n)=aT(n/b)+f(n)?
T(n)=i=0logbnaif(n/bi)T(n)=\sum_{i=0}^{\log_b n} a^i\, f(n/b^i).
How many leaves does the tree have?
alogbn=nlogbaa^{\log_b n} = n^{\log_b a}, each costing Θ(1)\Theta(1).
Per-level work for merge sort 2T(n/2)+n2T(n/2)+n, and why constant?
2i(n/2i)=n2^i \cdot (n/2^i)=n; the branching 2i2^i cancels the shrink, so every level costs nn → total Θ(nlogn)\Theta(n\log n).
For T(n)=3T(n/4)+n2T(n)=3T(n/4)+n^2, what is the level-ii work and the answer?
n2(3/16)in^2(3/16)^i; ratio <1<1 → root dominates → Θ(n2)\Theta(n^2).
When does the root dominate vs the leaves?
Root dominates when per-level work is a decreasing geometric series (a<bda<b^d); leaves dominate when it's increasing (a>bda>b^d).
Why does an unbalanced tree like T(n/3)+T(2n/3)+nT(n/3)+T(2n/3)+n give Θ(nlogn)\Theta(n\log n)?
Each level sums to n\le n and height is between log3n\log_3 n and log3/2n\log_{3/2} n, all Θ(logn)\Theta(\log n).
Common wrong leaf count and the fix?
Saying "nn leaves"; correct is nlogban^{\log_b a} (only equals nn when a=ba=b).

Connections

Concept Map

unfolded by substitution

node value

Q1 how deep

Q2 how wide

sum over levels

level sums shrink

level sums equal

level sums grow

derives

Recurrence T(n)=aT(n/b)+f(n)

Recursion Tree

Non-recursive cost f(m)

Depth = log_b n

Nodes at level i = a^i

Work per level a^i f(n/b^i)

T(n) = sum a^i f(n/b^i)

Leaves cost n^(log_b a)

Root dominates f(n)

Extra log factor

Leaves dominate

Master Theorem

Hinglish (regional understanding)

Intuition Hinglish mein samjho

Recursion tree ka matlab simple hai: jab tum koi recursive function likhte ho, toh wo apne ko baar-baar chhote problems mein todta jaata hai. Har call ko ek node maan lo, aur uss node ke andar likh do "is call ne kitna non-recursive kaam kiya" — yaani f(n)f(n). Phir poore tree ke saare nodes ka kaam jod do, bas wahi tumhara total time T(n)T(n) hai. Itna hi concept hai.

Teen cheezein dhyaan rakhni hain: tree kitna deep hai (kitne levels — yeh logbn\log_b n hota hai kyunki har level pe size bb se divide hoti hai), har level pe kitne nodes hain (aia^i, kyunki har node aa bachche banata hai), aur har level ka total kaam (aif(n/bi)a^i \cdot f(n/b^i)). Merge sort mein magic yeh hai ki har level ka kaam exactly nn aata hai (kyunki 2i2^i aur n/2in/2^i cancel ho jaate hain), aur logn\log n levels hain, isliye answer nlognn\log n.

Sabse important trick: per-level kaam ko dekho — agar wo neeche jaate hue ghat raha hai (geometric series ratio <1<1), toh root jeet jaata hai, answer f(n)f(n). Agar barabar hai toh ek extra log\log factor lag jaata hai. Agar badh raha hai, toh leaves jeet jaate hain, answer nlogban^{\log_b a}. Yeh exactly Master Theorem hai, par ratta maarne ki zaroorat nahi — har baar level sum se nikaal sakte ho.

Galti se bacho: log sochte hain "har level f(n)f(n) ka kaam karta hai" — galat! Sirf merge sort jaise balanced case mein aisa hota hai. Aur leaves ki ginti nn nahi, nlogban^{\log_b a} hoti hai. Yeh do baatein yaad rakh lo toh 80% problems clear.

Go deeper — visual, from zero

Test yourself — Complexity Analysis

Connections