主定理
主定理适用于求解如下递归式算法的时间复杂度:
\[T(n)=aT({n\over b}) + f(n)
\]
其中:
- \(n\) 是问题规模大小;
- \(a\) 是原问题的子问题个数;
- \({n\over b}\) 是每个子问题的大小,这里假设每个子问题有相同的规模大小;
- \(f(n)\) 是将原问题分解成子问题和将子问题的解合并成原问题的解的时间。
在提高组初赛中,常常会出现考察主定理的选择题。
在设计分治算法时,也经常会用主定理来计算时间复杂度。
对上面的式子进行分析,得到三种情况:
-
如果对于任意常量 \(\epsilon\gt 0,f(n)=O(n^{log_b^{a-\epsilon}})\),那么 \(T(n)=\theta(n^{log_b^a})\)
-
如果对于任意常量 \(k\ge 0,f(n)=\theta(n^{log_b^a}log^kn)\),那么 \(T(n)=\theta(n^{log_b^a}log^{k+1}n)\)
-
如果对于任意常量 \(\epsilon\gt 0,f(n)=\Omega(n^{log_b^{a+\epsilon}})\),那么 \(T(n)=\theta(f(n))\) 当且仅当存在一个常量 \(c\lt 1\),满足对于任意足够大的 \(n,af({n\over b})\le cf(n)\) 均成立。
Akra &. Brazzi 定理
对于式子
\[T(x)=\sum_{i=1}^k a_iT({x\over b_i})
\]
令 \(p\) 满足
\[\sum_{i=1}^k a_ib_i^p=1
\]
则
\[T(x)=\Theta(x^p+x^p\int_i^x{g(u)\over u^{p+1}}du)
\]
例1:\(T(x)=2T(x/2)+x-1\)
解:由式子可得,\(a_1=2,b_1=1/2,k=1\),当 \(p=1\) 时满足 \(a_1(1/2)^1=1\),代入 \(T(x)\) 的积分形式,得
\[\begin{aligned}
T(x) &= \Theta\left(x+x\int_1^x {u-1\over u^2}du\right) \\
&= \Theta\left(x+x\int_1^x\left({1\over u}-{1\over u^2}\right)du\right) \\
&= \Theta\left(x+x\left(ln\ u+{1\over u}\right)|_1^x\right) \\
&= \Theta\left(x+x\left(ln\ x+{1\over x}-1\right)\right) \\
&= \Theta\left(xln\ x\right)
\end{aligned}
\]
例2:
\[T(x)=\left\{
\begin{aligned}
& 2T\left({x\over 2}\right)+{8\over 9}T\left({3x\over 4}\right)+x^2), & x\ge 1 \\
& 0, & x\lt 1
\end{aligned}
\right.
\]
解:由式子可得,\(a_1=2,b_1=2,a_2={8\over 9},b_2={3\over 4}\),当 \(p=2\) 时 \(2\left({1\over 2}\right)^2+{8\over 9}\left({3\over 4}\right)^2=1\)
\[\begin{aligned}
T(x) &= \Theta\left(x^2+x^2\int_1^x{u^2\over u^3}du\right) \\
&= \Theta\left(x^2+x^2\left(ln\ u|_1^x\right)\right) \\
&= \Theta\left(x^2ln\ x\right)
\end{aligned}
\]
那么,如果 \(p\) 不那么容易算怎么办呢?通常可以忽略掉,再看一个例子。
例3:
\[T(x)=3T\left({x\over 3}\right)+4T\left({x\over 4}\right)+x^2
\]
解:由式子可得,\(a_1=3,b_1={1\over 3},a_2=4,b_2={1\over 4}\)。
当 \(p=1\) 时,\(1+1=2\gt 1\),当 \(p=2\) 时,\({1\over 3}+{1\over 4}\lt 1\),所以,\(1\lt p\lt 2\),是一个 \(O(g(x))\) 的式子。
\[\begin{aligned}
T(x) &= \Theta\left(x^p+x^p\int_1^x {u^2\over u^{1+p}}du\right) \\
&= \Theta\left(x^p+x^p\int_1^x u^{1-p}du\right) \\
&= \Theta\left(x^p+x^px^{2-p}\right) \\
&= \Theta\left(x^p+x^2\right) \\
&= \Theta\left(x^2\right)
\end{aligned}
\]
从而得出如下定理:
如果 \(g(x)=\Theta(x^t)\),其中 \(t\ge 0\) 且 \(\sum_{i=1}^k a_ib_i^t\lt 1\),则 \(T(x)=\Theta(g(x))\)。