主定理

· · 算法·理论

主定理(master theorem):

定理

当有递归算法时间表示为:

其时间复杂度: 当 $\log_b a>d$ 时,为 $\Omicron(n^{\log_b a})$ ; 当 $\log_b a=d$ 时,为 $\Omicron(n^{\log_b a}\times log_2 n)

当 \log_b a<d 时,为 \Omicron(n^d).

简单不?

实例

(取自NOIP2016提高组初赛第十四题)

假设某算法的计算时间表示为递推关系式:

T(n)=2T(\frac n 4)+\sqrt n

T(1)=1

则算法的时间复杂度为()。

A.\Omicron(n)

B.\Omicron(\sqrt n)

C.\Omicron(\sqrt nlog n)

D.\Omicron(n^2)

分析

在这里,a=2,b=4,根据 a^\frac n m=(m\sqrt a)^n(m为根号上的,打不上去而已)能得到:

d=\frac 1 2.

而 \log_b a 呢?

即 \log_4 2 。因为 4^\frac 1 2=2,所以式子=\frac 1 2 .

对应第二种情况,复杂度即为 \Omicron(n^\frac 1 2logn)。选 C .

好用不?