# 对主定理(Master Theorem)的理解

### 介绍

def solve(problem):
solve_without_recursion()
for subProblem in problem:
solve(subProblem)


$T(n)=a\,T\Big(\frac nb\Big)+f(n)$

$T(n)=\Theta(n^{\log_ba})+\sum_{i=0}^{\log_bn-1}a^if\Big(\frac{n}{b^i}\Big)$

1. $\exist \varepsilon>0\ s.t.\ f(n)=O(n^{\log_ba-\varepsilon})$。此时$T(n)=\Theta(n^{\log_ba})$
2. $f(n)=\Theta(n^{\log_ba})$。此时$T(n)=\Theta(n^{\log_ba}\lg n)$
3. $\exist \varepsilon>0\ s.t.\ f(n)=\Omega(n^{\log_ba+\varepsilon})$，且$\exist c<1$，当$n$足够大时，有$a\, f(\frac{n}{b})\le c\, f(n)$。此时$T(n)=\Theta(f(n))$

$f(n)$$\log$的情况类似，待补充。

### 证明

#### Case 1

$g(n)=\sum_{i=0}^{\log_bn-1}a^if(\frac{n}{b^i})$，由$f(n)=O(n^{\log_ba-\varepsilon})$，得：

$g(n)=O\Big(\sum_{i=0}^{\log_bn-1}a^i\Big(\frac{n}{b^i}\Big)^{\log_ba-\varepsilon}\Big)$

\begin{aligned} \sum_{i=0}^{\log_bn-1}a^i\Big(\frac{n}{b^i}\Big)^{\log_ba-\varepsilon} &= n^{\log_ba-\varepsilon}\sum_{i=0}^{\log_bn-1}\Big(\frac{ab^\varepsilon}{b^{\log_ba}}\Big)^i\\ &= n^{\log_ba-\varepsilon}\sum_{i=0}^{\log_bn-1}(b^\varepsilon)^i\\ &= n^{\log_ba-\varepsilon}\Big(\frac{(b^\varepsilon)^{\log_bn}-1}{b^\varepsilon-1}\Big)^i\\ &= n^{\log_ba-\varepsilon}\Big(\frac{n^\varepsilon-1}{b^\varepsilon-1}\Big)^i \end{aligned}

$T(n)=\Theta(n^{\log_ba})+O(n^{\log_ba})=\Theta(n^{\log_ba})$

#### Case 2

$g(n)=\Theta\Big(\sum_{i=0}^{\log_bn-1}a^i\Big(\frac{n}{b^i}\Big)^{\log_ba}\Big)$

\begin{aligned} \sum_{i=0}^{\log_bn-1}a^i\Big(\frac{n}{b^i}\Big)^{\log_ba} &= n^{\log_ba}\sum_{i=0}^{\log_bn-1}\Big(\frac{a}{b^{\log_ba}}\Big)^i\\ &= n^{\log_ba}\log_bn \end{aligned}

$T(n)= \Theta(n^{\log_ba})+\Theta(n^{\log_ba}\lg n)=\Theta(n^{\log_ba}\lg n)$

#### Case 3

\begin{aligned} a\, f\Big(\frac{n}{b}\Big) &\le c\, f(n)\\ \Rightarrow f\Big(\frac{n}{b}\Big) &\le \frac{c}{a}f(n)\\ \Rightarrow f\Big(\frac{n}{b^2}\Big) &\le \frac{c}{a}f\Big(\frac nb\Big)\le\Big(\frac{c}{a}\Big)^2f(n)\\ &\vdots\\ f\Big(\frac{n}{b^i}\Big) &\le\Big(\frac{c}{a}\Big)^if(n)\\ \Rightarrow a^i\, f\Big(\frac{n}{b^i}\Big) &\le c^i\, f(n)\\ \end{aligned}

\begin{aligned} \sum_{i=0}^{\log_bn-1}a^i\Big(\frac{n}{b^i}\Big)^{\log_ba} &\le \sum_{i=0}^{\log_bn-1}c^i\,f(n)+O(1)\\ &\le f(n)\sum_{i=0}c^i+O(1)\\ &=f(n)\Big(\frac{1}{1-c}\Big)+O(1)\\ &=f(n) \end{aligned}

$g(n)=O(f(n))$。又因为$g(n)=\sum_{i=0}^{\log_bn-1}a^if(\frac{n}{b^i})\ge f(n)$，得$g(n)=\Omega(f(n))$。因此$g(n)=\Theta(f(n))$

$T(n)=\Theta(n^{\log_ba})+\Theta(f(n))=\Theta(f(n))$

### 应用

$T(n)=2T\Big(\frac{n}{2}\Big)+O(1),\ T(n)=O(n)$

BFPRT(Median of Medians)

$T(n)\le T\Big(\frac{n}{5}\Big)+\Big(\frac{7n}{10}\Big)+O(n),\ T(n)=O(n)$

$T(n)=2T\Big(\frac{n}{2}\Big)+O(n),\ T(n)=O(\lg n)$

posted @ 2020-02-01 20:59  qqktr  阅读(664)  评论(0编辑  收藏