Salta al contenuto
Note per Studenti Master Theorem Report

Master Theorem

In questa pagina 5

The Master Theorem (MT) gives the order of growth of an important class of recurrences without solving them exactly. It is a restricted consequence of the general formulaThe recurrence tree has the shape of the call tree of the worst instance and T(n) is the sum of the works of its nodes. For the family T(n) = s(n) T(f(n)) + w(n) with base T0 for n <= n0, counting nodes per level with the iterates of f and the level f*(n,n0) gives the closed formula T(n) = sum over levels of [product of s] w + [product of s] T0, proved by induction and applied to merge sort, Hanoi and square-root recurrences.Recurrence Relations - Recursion Tree and General Formula →.

Statement

Let a,T0>0a,T_0>0, an integer b>1b>1 and w:Z+→Z+w:\mathbb Z^+\to\mathbb Z^+ non-decreasing. For n=bkn=b^k, T(n)={T0n=1a T(n/b)+w(n)n>1.(∗)T(n)=\begin{cases}T_0&n=1\\a\,T(n/b)+w(n)&n>1.\end{cases}\tag{$\ast$} This is the general formula with s=as=a, f=n/bf=n/b, n0=1n_0=1. The threshold function is nlog⁡ban^{\log_ba}.

Theorem (Master Theorem).

  • Case 1: if ∃ε>0:w(n)=O(nlog⁡ba−ε)\exists\varepsilon>0: w(n)=O(n^{\log_ba-\varepsilon}) then T(n)=Θ(nlog⁡ba)T(n)=\Theta(n^{\log_ba}).
  • Case 2: if w(n)=Θ(nlog⁡ba)w(n)=\Theta(n^{\log_ba}) then T(n)=Θ(nlog⁡balog⁡n)T(n)=\Theta(n^{\log_ba}\log n).
  • Case 3: if ∃ε>0:w(n)=Ω(nlog⁡ba+ε)\exists\varepsilon>0: w(n)=\Omega(n^{\log_ba+\varepsilon}) and (regularity) ∃c∈(0,1):∀n  a w(n/b)≤c w(n)\exists c\in(0,1): \forall n\ \ a\,w(n/b)\le c\,w(n), then T(n)=Θ(w(n))T(n)=\Theta(w(n)).

The MT is not exhaustive. Example: a=1a=1, b=2b=2, w=log⁡2nw=\log_2n: threshold n0=1n^0=1, and ww is larger, but log⁡2n/nε→0\log_2n/n^\varepsilon\to0 for every ε>0\varepsilon>0, so w≠Ω(nε)w\ne\Omega(n^\varepsilon) and Case 3 fails. The gap from the threshold must be a polynomial factor nεn^\varepsilon, not a logarithmic one.

Proof

Preliminary. For positive integer-domain functions, OO and Ω\Omega can be taken valid for all n>0n>0: if w(n)≤c g(n)w(n)\le c\,g(n) for n>nˉn>\bar n, take cˉ=max⁡{c,w(1)/g(1),…,w(nˉ)/g(nˉ)}\bar c=\max\{c,w(1)/g(1),\dots,w(\bar n)/g(\bar n)\}; for Ω\Omega use the minimum.

Starting point. The general formula with f∗(n,1)=log⁡bn−1f^\ast(n,1)=\log_bn-1, aℓa^\ell nodes at level ℓ\ell and alog⁡bn=nlog⁡baa^{\log_bn}=n^{\log_ba} gives T(n)=∑ℓ=0log⁡bn−1aℓ w ⁣(nbℓ)+nlog⁡ba T0.(∗∗)T(n)=\sum_{\ell=0}^{\log_bn-1}a^\ell\,w\!\Bigl(\frac n{b^\ell}\Bigr)+n^{\log_ba}\,T_0.\tag{$\ast\ast$} The last term is Θ(nlog⁡ba)\Theta(n^{\log_ba}) since T0>0T_0>0. Study the sum.

Case 1. w(n)≤cˉ nlog⁡ba−εw(n)\le\bar c\,n^{\log_ba-\varepsilon}. With (bℓ)log⁡ba=aℓ(b^\ell)^{\log_ba}=a^\ell: ∑ℓaℓw ⁣(nbℓ)≤cˉ nlog⁡ba−ε∑ℓ=0log⁡bn−1(bε)ℓ=cˉ nlog⁡ba−εnε−1bε−1≤cˉbε−1nlog⁡ba.\sum_{\ell}a^\ell w\!\Bigl(\tfrac n{b^\ell}\Bigr)\le\bar c\,n^{\log_ba-\varepsilon}\sum_{\ell=0}^{\log_bn-1}(b^\varepsilon)^\ell=\bar c\,n^{\log_ba-\varepsilon}\frac{n^\varepsilon-1}{b^\varepsilon-1}\le\frac{\bar c}{b^\varepsilon-1}n^{\log_ba}. So the sum is O(nlog⁡ba)O(n^{\log_ba}) and T=Θ(nlog⁡ba)T=\Theta(n^{\log_ba}) (if T0=0T_0=0, only OO). The leaves dominate.

Case 2. c^ nlog⁡ba≤w(n)≤cˉ nlog⁡ba\hat c\,n^{\log_ba}\le w(n)\le\bar c\,n^{\log_ba}. The term aℓ(n/bℓ)log⁡ba=nlog⁡baa^\ell(n/b^\ell)^{\log_ba}=n^{\log_ba} does not depend on ℓ\ell: every level costs the same, there are log⁡bn\log_bn levels, so the sum is Θ(nlog⁡balog⁡n)\Theta(n^{\log_ba}\log n) and so is TT.

Case 3. Regularity implies aℓw(n/bℓ)≤cℓw(n)a^\ell w(n/b^\ell)\le c^\ell w(n) (induction on ℓ\ell: aℓw(n/bℓ)=aℓ−1⋅a w(n/bℓ−1b)≤aℓ−1c w(n/bℓ−1)≤cℓw(n)a^\ell w(n/b^\ell)=a^{\ell-1}\cdot a\,w\bigl(\frac{n/b^{\ell-1}}b\bigr)\le a^{\ell-1}c\,w(n/b^{\ell-1})\le c^\ell w(n)). Then ∑ℓ=0log⁡bn−1aℓw ⁣(nbℓ)<w(n)∑ℓ=0∞cℓ=w(n)1−c.\sum_{\ell=0}^{\log_bn-1}a^\ell w\!\Bigl(\tfrac n{b^\ell}\Bigr)<w(n)\sum_{\ell=0}^{\infty}c^\ell=\frac{w(n)}{1-c}. The threshold term is dominated by w=Ω(nlog⁡ba+ε)w=\Omega(n^{\log_ba+\varepsilon}), so T=O(w)T=O(w); and T(n)≥w(n)T(n)\ge w(n), so T=Ω(w)T=\Omega(w). □\square

Meaning for design. Case 1: the leaves dominate (reduce the number of subinstances, as Strassen does). Case 3: the root work dominates (reduce it). Case 2: balanced, every level costs the same (a different substructure is usually needed to improve).

Extended version and use

The three cases hold identically for T(n)=aT(⌊n/b⌋)+w(n)T(n)=aT(\lfloor n/b\rfloor)+w(n) and aT(⌈n/b⌉)+w(n)aT(\lceil n/b\rceil)+w(n) for n>n0n>n_0, T(n)=T0T(n)=T_0 otherwise, for arbitrary n,n0n,n_0 (proof omitted: stopping at n0>1n_0>1 replaces constant-depth subtrees by a constant). Hence, when applying the MT only, one writes T(n)=aT(n/b)+w(n)T(n)=aT(n/b)+w(n) with no rounding; elsewhere this notation is meaningless.

How to apply. (1) read aa, bb, ww; (2) threshold nlog⁡ban^{\log_ba}; (3) compare exponents if w=Θ(nk)w=\Theta(n^k): k<log⁡bak<\log_ba Case 1, k=log⁡bak=\log_ba Case 2, k>log⁡bak>\log_ba Case 3; (4) in Case 3 find c<1c<1 with a w(n/b)≤c w(n)a\,w(n/b)\le c\,w(n).

Regularity is automatic for polynomials. If w=nkw=n^k with k≥log⁡ba+εk\ge\log_ba+\varepsilon: a w(n/b)=abknk≤ablog⁡ba+εw(n)=b−εw(n),a\,w(n/b)=\frac a{b^k}n^k\le\frac a{b^{\log_ba+\varepsilon}}w(n)=b^{-\varepsilon}w(n), so c=b−ε<1c=b^{-\varepsilon}<1.

Applications

Recurrence aa, bb threshold comparison case result
2T(n/2)+12T(n/2)+1 (MAX) 2, 2 nn 1=O(n1−ε)1=O(n^{1-\varepsilon}), ε=1\varepsilon=1 1 Θ(n)\Theta(n)
2T(n/2)+n−12T(n/2)+n-1 (merge sort) 2, 2 nn n−1=Θ(n)n-1=\Theta(n) 2 Θ(nlog⁡n)\Theta(n\log n)
T(n/2)+1T(n/2)+1 (binary search) 1, 2 11 1=Θ(1)1=\Theta(1) 2 Θ(log⁡n)\Theta(\log n)
3T(n/2)+3n3T(n/2)+3n 3, 2 nlog⁡23n^{\log_23} n=O(nlog⁡23−ε)n=O(n^{\log_23-\varepsilon}) 1 Θ(nlog⁡23)\Theta(n^{\log_23})
7T(n/2)+n27T(n/2)+n^2 (Strassen type) 7, 2 nlog⁡27n^{\log_27} n2=O(nlog⁡27−ε)n^2=O(n^{\log_27-\varepsilon}) 1 Θ(nlog⁡27)\Theta(n^{\log_27})
4T(n/2)+n24T(n/2)+n^2 4, 2 n2n^2 n2=Θ(n2)n^2=\Theta(n^2) 2 Θ(n2log⁡n)\Theta(n^2\log n)
T(n/2)+nT(n/2)+n 1, 2 11 n=Ω(n0+ε)n=\Omega(n^{0+\varepsilon}), c=12c=\frac12 3 Θ(n)\Theta(n)
3T(n/4)+n23T(n/4)+n^2 3, 4 nlog⁡43≈n0.79n^{\log_43}\approx n^{0.79} n2=Ω(nlog⁡43+ε)n^2=\Omega(n^{\log_43+\varepsilon}), c=316c=\frac3{16} 3 Θ(n2)\Theta(n^2)
8T(n/2)+n28T(n/2)+n^2 8, 2 n3n^3 n2=O(n3−ε)n^2=O(n^{3-\varepsilon}) 1 Θ(n3)\Theta(n^3)

Regularity for the Case 3 rows: T(n/2)+nT(n/2)+n: 1⋅n2=12n1\cdot\frac n2=\frac12n; 3T(n/4)+n23T(n/4)+n^2: 3(n4)2=316n23(\frac n4)^2=\frac3{16}n^2.

Misuse of the MT

Take T(n)=2T(n/2)+nlog⁡2nT(n)=2T(n/2)+n\log_2n, T(1)=1T(1)=1. Threshold nn; w=ω(n)w=\omega(n), so Cases 1 and 2 fail. Case 3 would need nlog⁡n=Ω(n1+ε)n\log n=\Omega(n^{1+\varepsilon}), false since log⁡n/nε→0\log n/n^\varepsilon\to0: the MT does not apply. Applying Case 3 anyway would give Θ(nlog⁡n)\Theta(n\log n), which is wrong. Unfolding with the general formula (n=2kn=2^k): T(n)=∑ℓ=0log⁡2n−12ℓn2ℓlog⁡2n2ℓ+n=n∑ℓ′=1log⁡2nℓ′+n=nlog⁡2n(log⁡2n+1)2+n=Θ(nlog⁡2n),T(n)=\sum_{\ell=0}^{\log_2n-1}2^\ell\frac n{2^\ell}\log_2\frac n{2^\ell}+n=n\sum_{\ell'=1}^{\log_2n}\ell'+n=n\frac{\log_2n(\log_2n+1)}2+n=\Theta(n\log^2n), using ℓ′=log⁡2n−ℓ\ell'=\log_2n-\ell and Gauss' sum. Check: T(2)=4T(2)=4, T(4)=16T(4)=16.

Versione ripasso

  • Recurrence: T(1)=T0>0T(1)=T_0>0, T(n)=a T(n/b)+w(n)T(n)=a\,T(n/b)+w(n), b>1b>1 integer, ww non-decreasing, n=bkn=b^k; threshold nlog⁡ban^{\log_ba} (from T(n)=∑ℓ<log⁡bnaℓw(n/bℓ)+nlog⁡baT0T(n)=\sum_{\ell<\log_bn}a^\ell w(n/b^\ell)+n^{\log_ba}T_0, see Recurrence Relations - Recursion Tree and General FormulaThe recurrence tree has the shape of the call tree of the worst instance and T(n) is the sum of the works of its nodes. For the family T(n) = s(n) T(f(n)) + w(n) with base T0 for n <= n0, counting nodes per level with the iterates of f and the level f*(n,n0) gives the closed formula T(n) = sum over levels of [product of s] w + [product of s] T0, proved by induction and applied to merge sort, Hanoi and square-root recurrences.Recurrence Relations - Recursion Tree and General Formula →).
  • Case 1: w=O(nlog⁡ba−ε)⇒T=Θ(nlog⁡ba)w=O(n^{\log_ba-\varepsilon})\Rightarrow T=\Theta(n^{\log_ba}) (leaves dominate; sum ≤cˉbε−1nlog⁡ba\le\frac{\bar c}{b^\varepsilon-1}n^{\log_ba}). Case 2: w=Θ(nlog⁡ba)⇒T=Θ(nlog⁡balog⁡n)w=\Theta(n^{\log_ba})\Rightarrow T=\Theta(n^{\log_ba}\log n) (each level costs nlog⁡ban^{\log_ba}). Case 3: w=Ω(nlog⁡ba+ε)w=\Omega(n^{\log_ba+\varepsilon}) and regularity a w(n/b)≤c w(n)a\,w(n/b)\le c\,w(n), c<1c<1 ⇒T=Θ(w(n))\Rightarrow T=\Theta(w(n)) (regularity gives aℓw(n/bℓ)≤cℓw(n)a^\ell w(n/b^\ell)\le c^\ell w(n), sum <w(n)1−c<\frac{w(n)}{1-c}; T≥wT\ge w).
  • Proof tools: OO/Ω\Omega extended to all n>0n>0 via cˉ=max⁡\bar c=\max, c^=min⁡\hat c=\min of the initial ratios; (bℓ)log⁡ba=aℓ(b^\ell)^{\log_ba}=a^\ell. If T0=0T_0=0, Case 1 gives only OO.
  • Extension: valid with ⌊n/b⌋\lfloor n/b\rfloor, ⌈n/b⌉\lceil n/b\rceil, arbitrary n,n0n,n_0; write T(n)=aT(n/b)+w(n)T(n)=aT(n/b)+w(n) only for the MT.
  • Not exhaustive: a=1,b=2,w=log⁡na=1,b=2,w=\log n fits no case (gap must be a power nεn^\varepsilon). For w=nkw=n^k regularity holds with c=b−εc=b^{-\varepsilon}.
  • How to apply: read a,b,wa,b,w; threshold nlog⁡ban^{\log_ba}; compare exponents for w=Θ(nk)w=\Theta(n^k): k<log⁡bak<\log_ba Case 1, == Case 2, >> Case 3; Case 3: find c<1c<1.
  • Examples: 2T(n/2)+1→Θ(n)2T(n/2)+1\to\Theta(n); 2T(n/2)+n−1→Θ(nlog⁡n)2T(n/2)+n-1\to\Theta(n\log n); T(n/2)+1→Θ(log⁡n)T(n/2)+1\to\Theta(\log n); 3T(n/2)+3n→Θ(nlog⁡23)3T(n/2)+3n\to\Theta(n^{\log_23}); 7T(n/2)+n2→Θ(nlog⁡27)7T(n/2)+n^2\to\Theta(n^{\log_27}); 4T(n/2)+n2→Θ(n2log⁡n)4T(n/2)+n^2\to\Theta(n^2\log n); 8T(n/2)+n2→Θ(n3)8T(n/2)+n^2\to\Theta(n^3); T(n/2)+n→Θ(n)T(n/2)+n\to\Theta(n) (c=12c=\frac12); 3T(n/4)+n2→Θ(n2)3T(n/4)+n^2\to\Theta(n^2) (c=316c=\frac3{16}).
  • Meaning: Case 1 leaves dominate (cut subinstances, e.g. Strassen); Case 3 root dominates; Case 2 all levels equal (seek another substructure).
  • Misuse: 2T(n/2)+nlog⁡n2T(n/2)+n\log n: no case applies; unfolding gives T=nlog⁡2n(log⁡2n+1)2+n=Θ(nlog⁡2n)T=n\frac{\log_2n(\log_2n+1)}2+n=\Theta(n\log^2n) (Case 3 would wrongly give Θ(nlog⁡n)\Theta(n\log n)).
  • Common mistakes: Case 3 with a merely logarithmic gap; skipping regularity; log⁡ba\log_ba confused with log⁡ab\log_ab; MT with subinstances of different sizes (use the general formula or an upper bound with equal sizes); rounding-free notation outside the MT.

Teoria collegata