- Gyanastra
- Algorithms
- Master Theorem and Solving Recurrences: All Cases and Traps
- Twelve Fully Worked Recurrences
Asked in past papers6 times- GATE CSE20051999198919881987
- UGC NET2015
Throughout, p=logba. I compute p first, then compare.
Example 1: T(n)=2T(n/2)+n (merge sort)
- a=2, b=2, p=log22=1.
- f(n)=n=Θ(n1)=Θ(np) → Case 2.
- T(n)=Θ(n1logn)=Θ(nlogn).
Example 2: T(n)=4T(n/2)+n
- a=4, b=2, p=log24=2.
- f(n)=n=O(n2−ε) with ε=1 → Case 1.
- T(n)=Θ(n2)=Θ(n2).
Example 3: T(n)=8T(n/2)+n2
- a=8, b=2, p=log28=3.
- f(n)=n2=O(n3−ε) with ε=1 → Case 1.
- T(n)=Θ(n3)=Θ(n3).
Example 4: T(n)=T(n/2)+1 (binary search)
- a=1, b=2, p=log21=0 (since 20=1).
- f(n)=1=Θ(n0)=Θ(np) → Case 2.
- T(n)=Θ(n0logn)=Θ(logn).
Example 5: T(n)=2T(n/2)+nlogn
- a=2, b=2, p=1.
- f(n)=nlogn=Θ(n1log1n) → extended Case 2 with k=1.
- T(n)=Θ(n1log1+1n)=Θ(nlog2n).
Example 6: T(n)=3T(n/4)+nlogn
- a=3, b=4, p=log43. Numerically log43=ln4ln3=1.38631.0986≈0.792.
- f(n)=nlogn. Compare to n0.792: f grows like n1⋅logn, which is Ω(n0.792+ε) for e.g. ε=0.2 (since 1>0.792+0.2=0.992). → Case 3.
- Regularity: af(n/b)=3⋅4nlog4n=43nlog4n≤43nlogn=cf(n) with c=43<1. ✓
- T(n)=Θ(f(n))=Θ(nlogn).
Example 7: T(n)=T(n−1)+n (decrease by 1 — Master Theorem does NOT apply)
- This is not divide-and-conquer; b is effectively 1 (subtract, not divide). Master Theorem inapplicable.
- Expand: T(n)=T(n−1)+n=T(n−2)+(n−1)+n=⋯=∑i=1ni+T(0).
- ∑i=1ni=2n(n+1)=Θ(n2).
- T(n)=Θ(n2).
Example 8: T(n)=T(n−1)+logn
- Expand: T(n)=∑i=1nlogi+T(0)=log(n!)+T(0).
- Stirling: log(n!)=Θ(nlogn).
- T(n)=Θ(nlogn).
Example 9: T(n)=2T(n−1)+1
- Expand: T(n)=2T(n−1)+1=2[2T(n−2)+1]+1=4T(n−2)+2+1.
- Pattern: T(n)=2kT(n−k)+(2k−1+2k−2+⋯+1)=2kT(n−k)+(2k−1).
- Set k=n: T(n)=2nT(0)+(2n−1)=Θ(2n).
- T(n)=Θ(2n).
Example 10: T(n)=T(n/3)+T(2n/3)+n (recursion tree)
Master Theorem does not apply (two different shrink factors — it is not of the form aT(n/b)). Use a recursion tree.
- Level 0: cost n.
- Level 1: 3n+32n=n.
- Level 2: 9n+92n+92n+94n=n (each node splits into 31,32; sum of all pieces =n).
- Every level costs exactly n (the fractions always sum to 1).
- Depth: the longest path follows the 32 branch: (32)kn=1⇒k=log3/2n. The shortest path follows 31: depth log3n.
- Total ≈n×(number of levels)=nlog3/2n.
- T(n)=Θ(nlogn) (base of log irrelevant asymptotically).
Example 11: T(n)=T(n)+1 (change of variable)
- Let n=2m, so m=log2n. Then n=2m/2.
- Define S(m)=T(2m). Then S(m)=S(m/2)+1.
- Solve S: a=1,b=2,p=log21=0, f=1=Θ(m0) → Case 2 → S(m)=Θ(logm).
- Back-substitute: T(n)=S(log2n)=Θ(loglogn).
- T(n)=Θ(loglogn).
Example 12: T(n)=2T(n)+logn (change of variable)
- Let n=2m, m=log2n. Then n=2m/2 and logn=m.
- S(m)=2S(m/2)+m.
- Solve: a=2,b=2,p=log22=1, f(m)=m=Θ(m1) → Case 2 → S(m)=Θ(mlogm).
- Back-substitute: T(n)=Θ(mlogm)=Θ(logn⋅loglogn).
- T(n)=Θ(lognloglogn).
Example 13 (bonus): T(n)=T(n/2)+T(n/4)+n (recursion tree)
Not of Master form (two shrink factors). Recursion tree:
- Level 0: n.
- Level 1: 2n+4n=43n.
- Level 2: the children of 2n are 4n,8n and the children of 4n are 8n,16n. Sum =164n+162n+162n+16n=169n=(43)2n. ✓
- Level k: (43)kn. Geometric with ratio 43<1.
- Total =n∑k≥0(43)k=n⋅1−3/41=n⋅4=Θ(n).
- T(n)=Θ(n).
Pages 1–2 of 2