跳到正文

算法·理论

主定理

_skytown约 1 分钟
原文
文章信息与版本

文章信息

发布
2025年9月17日
收录
2025年12月12日
更新
2026年11月16日
篇幅
约 1 分钟

版本

  1. v1当前

主定理(master theorem):

定理

当有递归算法时间表示为: T(n)=a×T(nb)+ndT(n)=a\times T(\frac n b)+n^d 时,

其时间复杂度:

当 log⁡ba>d\log_b a>d 时,为 O(nlog⁡ba)\Omicron(n^{\log_b a}) ;

当 log⁡ba=d\log_b a=d 时,为 O(nlog⁡ba×log2n)\Omicron(n^{\log_b a}\times log_2 n)

当 log⁡ba<d\log_b a<d 时,为 O(nd)\Omicron(n^d).

简单不?

实例

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

假设某算法的计算时间表示为递推关系式: T(n)=2T(n4)+nT(n)=2T(\frac n 4)+\sqrt n

T(1)=1

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

A.O(n)\Omicron(n)

B.O(n)\Omicron(\sqrt n)

C.O(nlogn)\Omicron(\sqrt nlog n)

D.O(n2)\Omicron(n^2)

分析

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

d=12.d=\frac 1 2.

而 log⁡ba\log_b a 呢?

即 log⁡42\log_4 2 。因为 412=24^\frac 1 2=2,所以式子=12\frac 1 2 .

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


好用不?

原文
存档评论1 条抓取于