KonkurStudy
همه کارت‌ها

Solving Recurrences & the Master Theorem

Model a recursive algorithm's cost as a recurrence, then read its asymptotic order off the Master Theorem — or, when the shape doesn't fit, via variable substitution, Akra–Bazzi, or a recursion tree.

کارت قضیه کنکور ارشد مهندسی کامپیوتر

اهمیت
۰/۵

A recurrence expresses an algorithm's cost T(n)T(n) in terms of its cost on smaller inputs. Every divide-and-conquer algorithm that breaks a size-nn problem into aa subproblems of size n/bn/b and spends f(n)f(n) extra work to split and combine obeys

T(n)=a T ⁣(nb)+f(n),a≥1,  b>1.T(n) = a\,T\!\left(\frac{n}{b}\right) + f(n),\qquad a\ge 1,\; b>1.

Solving it means finding the asymptotic order of T(n)T(n).

The Master Theorem

Compare f(n)f(n) against the watershed function nlog⁡ban^{\log_b a} (the number of leaves in the recursion tree). Let c∗=log⁡bac^{*}=\log_b a.

T(n)={Θ ⁣(nc∗)if f(n)=O ⁣(nc∗−ϵ)  (leaves win),Θ ⁣(nc∗log⁡k+1n)if f(n)=Θ ⁣(nc∗log⁡kn), k≥0,Θ ⁣(f(n))if f(n)=Ω ⁣(nc∗+ϵ)  (root wins).T(n)= \begin{cases} \Theta\!\left(n^{c^{*}}\right) & \text{if } f(n)=O\!\left(n^{c^{*}-\epsilon}\right)\ \ (\text{leaves win}),\\ \Theta\!\left(n^{c^{*}}\log^{k+1} n\right) & \text{if } f(n)=\Theta\!\left(n^{c^{*}}\log^{k} n\right),\ k\ge 0,\\ \Theta\!\left(f(n)\right) & \text{if } f(n)=\Omega\!\left(n^{c^{*}+\epsilon}\right)\ \ (\text{root wins}). \end{cases}

Case 3 additionally needs the regularity condition a f(n/b)≤c f(n)a\,f(n/b)\le c\,f(n) for some constant c<1c<1. The workhorse is Case 2 with k=0k=0: a T(n/b)+Θ ⁣(nc∗)=Θ ⁣(nc∗log⁡n)a\,T(n/b)+\Theta\!\left(n^{c^{*}}\right)=\Theta\!\left(n^{c^{*}}\log n\right) — exactly merge sort, 2T(n/2)+n=Θ(nlog⁡n)2T(n/2)+n=\Theta(n\log n).

Recipe

  1. Read off aa, bb, and f(n)f(n).
  2. Compute c∗=log⁡bac^{*}=\log_b a and the watershed nc∗n^{c^{*}}.
  3. Decide whether ff is polynomially smaller (Case 1), the same order up to logs (Case 2), or polynomially larger (Case 3).

Mini-example. T(n)=3T(n/2)+nT(n)=3T(n/2)+n: here c∗=log⁡23≈1.58c^{*}=\log_2 3\approx 1.58 and f(n)=n=O ⁣(n 1.58−ϵ)f(n)=n=O\!\left(n^{\,1.58-\epsilon}\right), so Case 1 gives T(n)=Θ ⁣(nlog⁡23)T(n)=\Theta\!\left(n^{\log_2 3}\right).

When the Master Theorem doesn't apply

Situation Tool
Non-polynomial gap (e.g. f=n/log⁡nf=n/\log n) recursion tree / substitution
Argument is n\sqrt{n} variable substitution n=2mn=2^{m}
Unequal subproblem sizes T(αn)+T(βn)+fT(\alpha n)+T(\beta n)+f Akra–Bazzi
Subtract-and-conquer T(n)=a T(n−b)+fT(n)=a\,T(n-b)+f unroll the sum directly

Square-root recurrences. For T(n)=a T(n)+f(n)T(n)=a\,T(\sqrt{n})+f(n), substitute n=2mn=2^{m} (so m=log⁡nm=\log n) and set S(m)=T(2m)S(m)=T(2^{m}); then n=2m/2\sqrt{n}=2^{m/2} turns the square root into a halving:

T(n)=2T(n)+log⁡n  ⟹  S(m)=2S ⁣(m2)+m=Θ(mlog⁡m),T(n)=2T(\sqrt{n})+\log n \;\Longrightarrow\; S(m)=2S\!\left(\tfrac{m}{2}\right)+m=\Theta(m\log m),

and back-substituting m=log⁡nm=\log n gives T(n)=Θ(log⁡n log⁡log⁡n)T(n)=\Theta(\log n\,\log\log n). A floor such as ⌊n⌋\lfloor\sqrt{n}\rfloor never changes the asymptotics.

Subtract-and-conquer & halving. T(n)=T(n−1)+Θ(n)=Θ(n2)T(n)=T(n-1)+\Theta(n)=\Theta(n^{2}) (insertion/selection-sort worst case); T(n)=2T(n−1)+Θ(1)=Θ(2n)T(n)=2T(n-1)+\Theta(1)=\Theta(2^{n}); and the pure halving search T(n)=T(n/2)+Θ(1)=Θ(log⁡n)T(n)=T(n/2)+\Theta(1)=\Theta(\log n) — e.g. shrinking a region of 1616 down to 11 by comparing halves takes log⁡216=4\log_2 16=4 steps.

Setting up the recurrence is half the battle. Naming the true subproblem sizes matters more than the algebra: a quicksort whose pivot is the median of a Θ(n)\Theta(\sqrt{n}) sample guarantees at least n\sqrt{n} elements on each side, so its worst-case split is the lopsided T(n)+T(n−n)+Θ(n)T(\sqrt{n})+T(n-\sqrt{n})+\Theta(n) — not the balanced 2T(n/2)2T(n/2) that the word "median" might suggest.

Exam trick: Reach for the Master Theorem only once the recurrence already has the exact shape a T(n/b)+f(n)a\,T(n/b)+f(n). A n\sqrt{n} argument means substitute n=2mn=2^{m}; two different subproblem sizes mean Akra–Bazzi or a recursion tree; an n−bn-b argument (not n/bn/b) means subtract-and-conquer, so unroll the sum instead.

فرمول‌های کلیدی

Divide-and-conquer recurrence
T(n)=a T ⁣(nb)+f(n)T(n) = a\,T\!\left(\dfrac{n}{b}\right) + f(n)

a subproblems of size n/b, plus f(n) work to split and combine (a >= 1, b > 1).

Master Theorem
T(n)={Θ ⁣(nlog⁡ba)f(n)=O ⁣(nlog⁡ba−ϵ)Θ ⁣(nlog⁡balog⁡k+1n)f(n)=Θ ⁣(nlog⁡balog⁡kn)Θ ⁣(f(n))f(n)=Ω ⁣(nlog⁡ba+ϵ)T(n)=\begin{cases}\Theta\!\left(n^{\log_b a}\right) & f(n)=O\!\left(n^{\log_b a-\epsilon}\right)\\ \Theta\!\left(n^{\log_b a}\log^{k+1} n\right) & f(n)=\Theta\!\left(n^{\log_b a}\log^{k} n\right)\\ \Theta\!\left(f(n)\right) & f(n)=\Omega\!\left(n^{\log_b a+\epsilon}\right)\end{cases}

Compare f(n) with the watershed n^{log_b a}: leaves win (Case 1), tie up to logs (Case 2), or root wins (Case 3, which also needs the regularity condition).

Square-root substitution
n=2m,S(m)=T ⁣(2m)n=2^{m},\quad S(m)=T\!\left(2^{m}\right)

Substitute n = 2^m to turn T(n) = a·T(sqrt(n)) + f into a halving recurrence in m = log n, then translate the answer back.

#recurrence#master-theorem#asymptotic-analysis#divide-and-conquer#variable-substitution#square-root-recurrence#akra-bazzi#recursion-tree#merge-sort#big-theta

سوالات تمرینی