算法复杂度计算

Master theorem

Let T ( n ) T(n) T(n) be defined by T ( n ) = a T ( n b ) + O ( n d ) T(n) = aT(\frac{n}{b}) + O(n^d) T(n)=aT(bn)+O(nd) for a > 1 ,   b > 1 a>1, \ b>1 a>1, b>1 and d > 0 d>0 d>0, then T ( n ) T(n) T(n) can be bounded by:

  • if d < l o g b a dd<logba, then T ( n ) = O ( n l o g b a ) T(n) = O(n^{log_ba}) T(n)=O(nlogba);
  • if d = l o g b a d = log_ba d=logba, then T ( n ) = O ( n l o g b a l o g n ) T(n) = O(n^{log_ba}logn) T(n)=O(nlogbalogn);
  • if d > l o g b a d>log_ba d>logba, then T ( n ) = O ( n d ) T(n) = O(n^d) T(n)=O(nd);

看到上面三个时间复杂度计算的前提条件,发现少了一项,就是 d = 0 d=0 d=0 时要怎么计算,所以下面将对这四种情况进行推导说明。

1. d=0

d = 0 d=0 d=0时,可以适用于只分不 merge 的情况,比如查找二叉树某结点深度等。
在下面计算时会用到换底公式
a l o g b c = c l o g b a a^{log_b^c} = c^{log_b^a} alogbc=clogba

T ( n ) = a T ( n b ) + c = a 2 T ( n b 2 ) + 2 c = a 3 T ( n b 3 ) + 3 c ⋯ = a l o g b n T ( n b l o g b n ) + c ⋅ l o g b n = a l o g b n T ( n n ) + c ⋅ l o g b n = n l o g b a + O ( l o g n ) \begin{aligned} T(n) &= aT(\frac{n}{b})+c \\ &= a^2T(\frac{n}{b^2}) +2c \\ &= a^3T(\frac{n}{b^3}) +3c \\ &\cdots \\ &= a^{log_bn}T(\frac{n}{b^{log_bn}}) +c\cdot log_bn \\ &= a^{log_bn}T(\frac{n}{n}) + c\cdot log_bn \\ &= n^{log_ba}+O(logn) \end{aligned} T(n)=aT(bn)+c=a2T(b2n)+2c=a3T(b3n)+3c=alogbnT(blogbnn)+clogbn=alogbnT(nn)+clogbn=nlogba+O(logn)

先判定一个边界条件,即 a l o g b n = l o g b n a^{log_bn} = log_bn alogbn=logbn,此时 a = 1 a=1 a=1,所以我们有:

  • 当 a > 1 时, T ( n ) = O ( n l o g b a ) T(n)=O(n^{log_ba}) T(n)=O(nlogba)
  • 当 a ≤ 1 时, T ( n ) = O ( l o g n ) T(n)=O(logn) T(n)=O(logn)

2. d ≠ 0

T ( n ) = a T ( n b ) + O ( n d ) ≤ a T ( n b ) + c ⋅ n d ≤ a ( a T ( n b 2 ) + c ( n b ) d ) + c ⋅ n d ≤ a ( a ( a T ( n b 3 ) + c ( n b 2 ) d ) + c ( n b ) d ) + c ⋅ n d ≤ ⋯ ≤ c n d ( 1 + ( a b d ) + ( a b d ) 2 + ⋯ + ( a b d ) l o g b n − 1 ) + a l o g b n = c n d ⋅ 1 − ( a b d ) l o g b n 1 − a b d + a l o g b n \begin{aligned} T(n) &= aT(\frac{n}{b}) + O(n^d) \\ &\leq aT(\frac{n}{b}) + c\cdot n^d \\ &\leq a(aT(\frac{n}{b^2}) + c(\frac{n}{b})^d)+c\cdot n^d \\ &\leq a(a(aT(\frac{n}{b^3}) + c(\frac{n}{b^2})^d)+c(\frac{n}{b})^d)+c\cdot n^d \\ &\leq \cdots \\ &\leq cn^d(1 + (\frac{a}{b^d}) + (\frac{a}{b^d})^2 + \cdots + (\frac{a}{b^d})^{log_bn-1}) + a^{log_bn} \\ &= cn^d \cdot \frac{1-(\frac{a}{b^d})^{log_bn}}{1-\frac{a}{b^d}} + a^{log_bn} \end{aligned} T(n)=aT(bn)+O(nd)aT(bn)+cnda(aT(b2n)+c(bn)d)+cnda(a(aT(b3n)+c(b2n)d)+c(bn)d)+cndcnd(1+(bda)+(bda)2++(bda)logbn1)+alogbn=cnd1bda1(bda)logbn+alogbn

2.1 if d < l o g b a dd<logba b d < a b^dbd<a

上式子可化为

T ( n ) = c n d ⋅ ( a b d ) l o g b n + a l o g b n = c n d ⋅ a l o g b n b d l o g b n + n l o g b a = c n d ⋅ n l o g b a n d + n l o g b a = O ( n l o g b a ) \begin{aligned} T(n) &= cn^d \cdot (\frac{a}{b^d})^{log_bn} + alog_bn \\ &= cn^d \cdot \frac{a^{log_bn}}{b^{dlog_bn}} + n^{log_ba} \\ &= cn^d \cdot \frac{n^{log_ba}}{n^d} + n^{log_ba} \\ &= O(n^{log_ba}) \end{aligned} T(n)=cnd(bda)logbn+alogbn=cndbdlogbnalogbn+nlogba=cndndnlogba+nlogba=O(nlogba)

2.2 if d = l o g b a d=log_ba d=logba b d = a b^d=a bd=a

上式子可化为

T ( n ) = c n d ⋅ ( 1 + 1 + ⋯ + 1 ) + n l o g b a = c n l o g b a ⋅ l o g n + n l o g b a = O ( n l o g b a l o g n ) \begin{aligned} T(n) &= cn^d \cdot (1+1+\cdots +1) + n^{log_ba} \\ &= cn^{log_ba}\cdot logn + n^{log_ba} \\ &= O(n^{log_ba}logn) \end{aligned} T(n)=cnd(1+1++1)+nlogba=cnlogbalogn+nlogba=O(nlogbalogn)

2.3 if d > l o g b a d>log_ba d>logba b d > a b^d>a bd>a

由于 a b d < 1 \frac{a}{b^d} < 1 bda<1 等比数列和近似为1,上式子可化为

T ( n ) = c n d ⋅ 1 + n l o g b a = O ( n d ) \begin{aligned} T(n) &= cn^d \cdot 1 + n^{log_ba} \\ &= O(n^d) \end{aligned} T(n)=cnd1+nlogba=O(nd)

综上

3. Example

举两个例子熟悉一下。

  • Example1: T ( n ) = 3 T ( n 2 ) + O ( n ) = O ( n l o g 2 3 ) = O ( n 1.585 ) T(n)=3T(\frac{n}{2})+O(n) = O(n^{log_23}) = O(n^{1.585}) T(n)=3T(2n)+O(n)=O(nlog23)=O(n1.585)
  • Example2: T ( n ) = 2 T ( n 2 ) + O ( n 2 ) = O ( n 2 ) T(n)=2T(\frac{n}{2})+O(n^2)=O(n^2) T(n)=2T(2n)+O(n2)=O(n2)

Example1

算法复杂度计算_第1张图片

3 l o g 2 n 3^{log_2n} 3log2n 是指的 T(n) 一次分三个,一共有 l o g 2 n log_2n log2n 次。

T ( n ) = 3 T ( n 2 ) + O ( n ) = c n ( 1 + ( 3 2 ) + ( 3 2 ) 2 + ⋯ + ( 3 2 ) l o g 2 n − 1 ) + 3 l o g 2 n = c n ⋅ 1 − ( 3 2 ) l o g 2 n 1 − 3 2 + 3 l o g 3 n = c n ⋅ 3 l o g 2 n 2 l o g 2 n + 3 l o g 2 n = O ( 3 l o g 2 n ) = O ( n 1.585 ) \begin{aligned} T(n) &=3T(\frac{n}{2})+O(n) \\ &= cn(1 + (\frac{3}{2}) + (\frac{3}{2})^2 + \cdots + (\frac{3}{2})^{log_2n-1}) + 3^{log_2n} \\ &= cn\cdot \frac{1-(\frac{3}{2})^{log_2n}}{1-\frac{3}{2}} +3^{log_3n} \\ &= cn \cdot \frac{3^{log_2n}}{2^{log_2n}} + 3^{log_2n} \\ &= O(3^{log_2n}) \\ &= O(n^{1.585}) \end{aligned} T(n)=3T(2n)+O(n)=cn(1+(23)+(23)2++(23)log2n1)+3log2n=cn1231(23)log2n+3log3n=cn2log2n3log2n+3log2n=O(3log2n)=O(n1.585)

Example2
算法复杂度计算_第2张图片

T ( n ) = 2 T ( n 2 ) + O ( n 2 ) = c n 2 ( 1 + ( 1 2 ) + ( 1 2 ) 2 + ⋯ + ( 1 2 ) l o g 2 n − 1 ) + 2 l o g 2 n = c n 2 ⋅ 1 − ( 1 2 ) l o g 2 n 1 − 1 2 + n = c n 2 + n = O ( n 2 ) \begin{aligned} T(n) &=2T(\frac{n}{2})+O(n^2)\\ &= cn^2(1 + (\frac{1}{2}) + (\frac{1}{2})^2 + \cdots + (\frac{1}{2})^{log_2n-1}) + 2^{log_2n} \\ &= cn^2\cdot \frac{1-(\frac{1}{2})^{log_2n}}{1-\frac{1}{2}} +n \\ &= cn^2 + n \\ &= O(n^2) \\ \end{aligned} T(n)=2T(2n)+O(n2)=cn2(1+(21)+(21)2++(21)log2n1)+2log2n=cn21211(21)log2n+n=cn2+n=O(n2)

你可能感兴趣的:(算法,算法)