1. Gyanastra
  2. Algorithms
  3. Master Theorem and Solving Recurrences: All Cases and Traps
  4. Twelve Fully Worked Recurrences
Free lesson, no account needed 5 min read
Asked in past papers6 times
  • GATE CSE20051999198919881987
  • UGC NET2015

Algorithms · Master Theorem and Solving Recurrences: All Cases and Traps

Twelve Fully Worked Recurrences

Throughout, p=log⁡bap=\log_b a. I compute pp first, then compare.

Example 1: T(n)=2T(n/2)+nT(n) = 2T(n/2) + n (merge sort)

  • a=2a=2, b=2b=2, p=log⁡22=1p=\log_2 2 = 1.
  • f(n)=n=Θ(n1)=Θ(np)f(n)=n=\Theta(n^1)=\Theta(n^p) → Case 2.
  • T(n)=Θ(n1log⁡n)=Θ(nlog⁡n)T(n)=\Theta(n^1\log n)=\boxed{\Theta(n\log n)}.

Example 2: T(n)=4T(n/2)+nT(n) = 4T(n/2) + n

  • a=4a=4, b=2b=2, p=log⁡24=2p=\log_2 4 = 2.
  • f(n)=n=O(n2−ε)f(n)=n=O(n^{2-\varepsilon}) with ε=1\varepsilon=1 → Case 1.
  • T(n)=Θ(n2)=Θ(n2)T(n)=\Theta(n^2)=\boxed{\Theta(n^2)}.

Example 3: T(n)=8T(n/2)+n2T(n) = 8T(n/2) + n^2

  • a=8a=8, b=2b=2, p=log⁡28=3p=\log_2 8 = 3.
  • f(n)=n2=O(n3−ε)f(n)=n^2=O(n^{3-\varepsilon}) with ε=1\varepsilon=1 → Case 1.
  • T(n)=Θ(n3)=Θ(n3)T(n)=\Theta(n^3)=\boxed{\Theta(n^3)}.

Example 5: T(n)=2T(n/2)+nlog⁡nT(n) = 2T(n/2) + n\log n

  • a=2a=2, b=2b=2, p=1p=1.
  • f(n)=nlog⁡n=Θ(n1log⁡1n)f(n)=n\log n=\Theta(n^1\log^1 n) → extended Case 2 with k=1k=1.
  • T(n)=Θ(n1log⁡1+1n)=Θ(nlog⁡2n)T(n)=\Theta(n^1\log^{1+1}n)=\boxed{\Theta(n\log^2 n)}.

Example 6: T(n)=3T(n/4)+nlog⁡nT(n) = 3T(n/4) + n\log n

  • a=3a=3, b=4b=4, p=log⁡43p=\log_4 3. Numerically log⁡43=ln⁡3ln⁡4=1.09861.3863≈0.792\log_4 3=\dfrac{\ln 3}{\ln 4}=\dfrac{1.0986}{1.3863}\approx 0.792.
  • f(n)=nlog⁡nf(n)=n\log n. Compare to n0.792n^{0.792}: ff grows like n1⋅log⁡nn^{1}\cdot\log n, which is Ω(n0.792+ε)\Omega(n^{0.792+\varepsilon}) for e.g. ε=0.2\varepsilon=0.2 (since 1>0.792+0.2=0.9921 > 0.792+0.2=0.992). → Case 3.
  • Regularity: af(n/b)=3⋅n4log⁡n4=34nlog⁡n4≤34nlog⁡n=c f(n)a f(n/b)=3\cdot\frac{n}{4}\log\frac{n}{4}=\frac34 n\log\frac{n}{4}\le \frac34 n\log n = c\,f(n) with c=34<1c=\frac34<1. ✓
  • T(n)=Θ(f(n))=Θ(nlog⁡n)T(n)=\Theta(f(n))=\boxed{\Theta(n\log n)}.

Example 7: T(n)=T(n−1)+nT(n) = T(n-1) + n (decrease by 1 — Master Theorem does NOT apply)

  • This is not divide-and-conquer; bb is effectively 11 (subtract, not divide). Master Theorem inapplicable.
  • Expand: T(n)=T(n−1)+n=T(n−2)+(n−1)+n=⋯=∑i=1ni+T(0)T(n)=T(n-1)+n=T(n-2)+(n-1)+n=\cdots=\sum_{i=1}^{n} i + T(0).
  • ∑i=1ni=n(n+1)2=Θ(n2)\sum_{i=1}^n i=\dfrac{n(n+1)}{2}=\Theta(n^2).
  • T(n)=Θ(n2)\boxed{T(n)=\Theta(n^2)}.

Example 8: T(n)=T(n−1)+log⁡nT(n) = T(n-1) + \log n

  • Expand: T(n)=∑i=1nlog⁡i+T(0)=log⁡(n!)+T(0)T(n)=\sum_{i=1}^{n}\log i + T(0)=\log(n!)+T(0).
  • Stirling: log⁡(n!)=Θ(nlog⁡n)\log(n!)=\Theta(n\log n).
  • T(n)=Θ(nlog⁡n)\boxed{T(n)=\Theta(n\log n)}.

Example 9: T(n)=2T(n−1)+1T(n) = 2T(n-1) + 1

  • Expand: T(n)=2T(n−1)+1=2[2T(n−2)+1]+1=4T(n−2)+2+1T(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)T(n)=2^k T(n-k)+(2^{k-1}+2^{k-2}+\cdots+1)=2^k T(n-k)+(2^k-1).
  • Set k=nk=n: T(n)=2nT(0)+(2n−1)=Θ(2n)T(n)=2^n T(0)+(2^n-1)=\Theta(2^n).
  • T(n)=Θ(2n)\boxed{T(n)=\Theta(2^n)}.

Example 10: T(n)=T(n/3)+T(2n/3)+nT(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)aT(n/b)). Use a recursion tree.

  • Level 0: cost nn.
  • Level 1: n3+2n3=n\frac{n}{3}+\frac{2n}{3}=n.
  • Level 2: n9+2n9+2n9+4n9=n\frac{n}{9}+\frac{2n}{9}+\frac{2n}{9}+\frac{4n}{9}=n (each node splits into 13,23\tfrac13,\tfrac23; sum of all pieces =n=n).
  • Every level costs exactly nn (the fractions always sum to 11).
  • Depth: the longest path follows the 23\tfrac23 branch: (23)kn=1⇒k=log⁡3/2n\left(\tfrac23\right)^k n = 1 \Rightarrow k=\log_{3/2} n. The shortest path follows 13\tfrac13: depth log⁡3n\log_3 n.
  • Total ≈n×(number of levels)=nlog⁡3/2n\approx n \times (\text{number of levels}) = n\log_{3/2} n.
  • T(n)=Θ(nlog⁡n)\boxed{T(n)=\Theta(n\log n)} (base of log irrelevant asymptotically).

Example 11: T(n)=T(n)+1T(n) = T(\sqrt n) + 1 (change of variable)

  • Let n=2mn = 2^m, so m=log⁡2nm=\log_2 n. Then n=2m/2\sqrt n = 2^{m/2}.
  • Define S(m)=T(2m)S(m)=T(2^m). Then S(m)=S(m/2)+1S(m)=S(m/2)+1.
  • Solve SS: a=1,b=2,p=log⁡21=0a=1,b=2,p=\log_2 1=0, f=1=Θ(m0)f=1=\Theta(m^0) → Case 2 → S(m)=Θ(log⁡m)S(m)=\Theta(\log m).
  • Back-substitute: T(n)=S(log⁡2n)=Θ(log⁡log⁡n)T(n)=S(\log_2 n)=\Theta(\log\log n).
  • T(n)=Θ(log⁡log⁡n)\boxed{T(n)=\Theta(\log\log n)}.

Example 12: T(n)=2T(n)+log⁡nT(n) = 2T(\sqrt n) + \log n (change of variable)

  • Let n=2mn=2^m, m=log⁡2nm=\log_2 n. Then n=2m/2\sqrt n=2^{m/2} and log⁡n=m\log n = m.
  • S(m)=2S(m/2)+mS(m)=2S(m/2)+m.
  • Solve: a=2,b=2,p=log⁡22=1a=2,b=2,p=\log_2 2=1, f(m)=m=Θ(m1)f(m)=m=\Theta(m^1) → Case 2 → S(m)=Θ(mlog⁡m)S(m)=\Theta(m\log m).
  • Back-substitute: T(n)=Θ(mlog⁡m)=Θ(log⁡n⋅log⁡log⁡n)T(n)=\Theta(m\log m)=\Theta(\log n\cdot\log\log n).
  • T(n)=Θ(log⁡nlog⁡log⁡n)\boxed{T(n)=\Theta(\log n\log\log n)}.

Example 13 (bonus): T(n)=T(n/2)+T(n/4)+nT(n) = T(n/2) + T(n/4) + n (recursion tree)

Not of Master form (two shrink factors). Recursion tree:

  • Level 0: nn.
  • Level 1: n2+n4=3n4\frac{n}{2}+\frac{n}{4}=\frac{3n}{4}.
  • Level 2: the children of n2\frac n2 are n4,n8\frac n4,\frac n8 and the children of n4\frac n4 are n8,n16\frac n8,\frac n{16}. Sum =4n16+2n16+2n16+n16=9n16=(34)2n=\frac{4n}{16}+\frac{2n}{16}+\frac{2n}{16}+\frac{n}{16}=\frac{9n}{16}=\left(\frac34\right)^2 n. ✓
  • Level kk: (34)kn\left(\frac34\right)^k n. Geometric with ratio 34<1\frac34<1.
  • Total =n∑k≥0(34)k=n⋅11−3/4=n⋅4=Θ(n)=n\sum_{k\ge0}\left(\frac34\right)^k = n\cdot\dfrac{1}{1-3/4}=n\cdot 4=\Theta(n).
  • T(n)=Θ(n)\boxed{T(n)=\Theta(n)}.

Pages 1–2 of 2