KonkurStudy
همه کارت‌ها

Master Theorem Case 2 (balanced recurrence, the merge-sort case)

When the work to split/combine, f(n)f(n), exactly matches the leaf cost nlog⁡ban^{\log_b a}, the recurrence T(n)=aT(n/b)+f(n)T(n)=aT(n/b)+f(n) solves to Θ ⁣(nlog⁡balog⁡n)\Theta\!\left(n^{\log_b a}\log n\right) — every level costs the same, and there are Θ(log⁡n)\Theta(\log n) levels.

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

اهمیت
۰/۵

The Master Theorem

Many divide-and-conquer algorithms split a problem of size nn into aa subproblems of size n/bn/b, then spend f(n)f(n) extra work splitting and combining. This gives the recurrence

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.

The outcome is decided by comparing f(n)f(n) against the watershed function nlog⁡ban^{\log_b a} — the total cost of the leaves of the recursion tree.

Case Condition on f(n)f(n) Solution
1 f(n)=O ⁣(nlog⁡ba−ε)f(n)=O\!\left(n^{\log_b a-\varepsilon}\right) (leaves dominate) Θ ⁣(nlog⁡ba)\Theta\!\left(n^{\log_b a}\right)
2 f(n)=Θ ⁣(nlog⁡ba)f(n)=\Theta\!\left(n^{\log_b a}\right) (balanced) Θ ⁣(nlog⁡balog⁡n)\Theta\!\left(n^{\log_b a}\log n\right)
3 f(n)=Ω ⁣(nlog⁡ba+ε)f(n)=\Omega\!\left(n^{\log_b a+\varepsilon}\right) + regularity (root dominates) Θ ⁣(f(n))\Theta\!\left(f(n)\right)

Case 2 — the balanced case

Case 2. If f(n)=Θ ⁣(nlog⁡ba)f(n)=\Theta\!\left(n^{\log_b a}\right), then T(n)=Θ ⁣(nlog⁡balog⁡n)T(n)=\Theta\!\left(n^{\log_b a}\log n\right).

Intuition from the recursion tree: at depth ii there are aia^{i} nodes, each doing f(n/bi)f(n/b^{i}) work. When f(n)=Θ ⁣(nlog⁡ba)f(n)=\Theta\!\left(n^{\log_b a}\right), every level does the same total amount of work, Θ ⁣(nlog⁡ba)\Theta\!\left(n^{\log_b a}\right). The tree has height log⁡bn=Θ(log⁡n)\log_b n=\Theta(\log n), so summing identical work over all levels multiplies by a log⁡n\log n factor:

T(n)=Θ ⁣(nlog⁡ba)⏟per level×Θ(log⁡n)⏟levels=Θ ⁣(nlog⁡balog⁡n).T(n)=\underbrace{\Theta\!\left(n^{\log_b a}\right)}_{\text{per level}}\times\underbrace{\Theta(\log n)}_{\text{levels}}=\Theta\!\left(n^{\log_b a}\log n\right).

The extra log⁡n\log n is the signature of Case 2 — it is what separates it from Cases 1 and 3, where one end of the tree dominates and no log appears.

How to apply (recipe)

  1. Read off aa (number of recursive calls), bb (shrink factor), and f(n)f(n) (non-recursive work per call).
  2. Compute the watershed exponent log⁡ba\log_b a and form nlog⁡ban^{\log_b a}.
  3. Compare f(n)f(n) to nlog⁡ban^{\log_b a}. If they are the same order (Θ\Theta of each other), you are in Case 2.
  4. Answer: Θ ⁣(nlog⁡balog⁡n)\Theta\!\left(n^{\log_b a}\log n\right).

Mini-example — merge sort and friends

Merge sort: T(n)=2T(n/2)+Θ(n)T(n)=2T(n/2)+\Theta(n). Here a=2a=2, b=2b=2, so nlog⁡22=n1=nn^{\log_2 2}=n^{1}=n. Since f(n)=Θ(n)f(n)=\Theta(n) matches, Case 2 gives the famous

T(n)=Θ(nlog⁡n).T(n)=\Theta(n\log n).

The konkur question with f2(n) = f2(n/2) + f1(n) + f2(n/2) is exactly this shape: two half-size calls plus Θ(n)\Theta(n) work from f1, i.e. T(n)=2T(n/2)+Θ(n)⇒Θ(nlog⁡n)T(n)=2T(n/2)+\Theta(n)\Rightarrow\Theta(n\log n).

Binary search of a different flavor: T(n)=2T(n/2)+Θ(1)T(n)=2T(n/2)+\Theta(1) is not Case 2 — here f(n)=Θ(1)f(n)=\Theta(1) but nlog⁡22=nn^{\log_2 2}=n, so leaves dominate (Case 1) and T(n)=Θ(n)T(n)=\Theta(n). Matching f(n)f(n) to nlog⁡ban^{\log_b a} is the whole game.

Exam trick: Case 2 is the only case that produces a log⁡n\log n factor. The instant you see two half-size calls plus linear work — 2T(n/2)+Θ(n)2T(n/2)+\Theta(n) — answer Θ(nlog⁡n)\Theta(n\log n) on sight. Do not be fooled when the linear term is hidden inside a helper function (like an O(n)O(n) loop): it still counts as f(n)=Θ(n)f(n)=\Theta(n).

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

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

General divide-and-conquer recurrence: a subproblems of size n/b plus f(n) split/combine work.

Watershed (leaf-cost) function
nlog⁡ban^{\log_b a}

The benchmark you compare f(n) against; equals the total cost of the leaves of the recursion tree.

Case 2 condition and result
f(n)=Θ ⁣(nlog⁡ba)  ⟹  T(n)=Θ ⁣(nlog⁡balog⁡n)f(n)=\Theta\!\left(n^{\log_b a}\right)\;\Longrightarrow\; T(n)=\Theta\!\left(n^{\log_b a}\log n\right)

When split/combine work balances the leaf cost, every level costs the same and the answer gains a log n factor.

Merge-sort instance
T(n)=2T ⁣(n2)+Θ(n)=Θ(nlog⁡n)T(n)=2T\!\left(\frac{n}{2}\right)+\Theta(n)=\Theta(n\log n)

The canonical Case 2 example: a=2, b=2, log_b a = 1, f(n)=Theta(n).

#master-theorem#case-2#divide-and-conquer#recurrence#merge-sort#n-log-n#recursion-tree#asymptotics

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