计算理论 复杂度预备知识

计算理论 复杂度预备知识

符号

f ( n ) = o ( g ( n ) ) f(n)=o(g(n)) f(n)=o(g(n)) ∃ c \exists c c ,当 n n n 足够大时, f ( n ) < c g ( n ) f(n)\lt cg(n) f(n)<cg(n) ∑ n → ∞ f ( n ) g ( n ) = 0 \sum\limits_{n\to\infty} \frac{f(n)}{g(n)}=0 ng(n)f(n)=0
f ( n ) = O ( g ( n ) ) f(n)=O(g(n)) f(n)=O(g(n)) ∃ c \exists c c ,当 n n n 足够大时, f ( n ) ≤ c g ( n ) f(n)\le cg(n) f(n)cg(n)
f ( n ) = θ ( g ( n ) ) f(n)=\theta(g(n)) f(n)=θ(g(n)) ∃ c 1 , c 2 \exists c_{1}, c_{2} c1,c2 ,当 n n n 足够大时, c 1 g ( n ) < f ( n ) < c 2 g ( n ) c_{1}g(n)\lt f(n) \lt c_{2}g(n) c1g(n)<f(n)<c2g(n)
f ( n ) = ω ( g ( n ) ) f(n)=\omega(g(n)) f(n)=ω(g(n)) ∃ c \exists c c ,当 n n n 足够大时, f ( n ) > c g ( n ) f(n)\gt cg(n) f(n)>cg(n)
f ( n ) = Ω ( g ( n ) ) f(n)=\Omega(g(n)) f(n)=Ω(g(n)) ∃ c \exists c c ,当 n n n 足够大时, f ( n ) ≥ c g ( n ) f(n)\ge cg(n) f(n)cg(n)

递归表达式求解通项公式

T ( n ) = 4 T ( n 2 ) + n T(n)=4T\left( \frac{n}{2} \right)+n T(n)=4T(2n)+n :就直接展开,找到规律
T ( n ) = 4 T ( n 2 ) + n = 4 ( 4 ( T ( n 2 2 ) + n 2 ) + n = 4 T ( n 2 2 ) + 2 n + n = 4 k T ( n 2 k ) + n ( 2 0 + 2 1 + ⋯ + 2 k − 1 ) \begin{array}{l} \quad T(n) \\ =4T(\frac{n}{2})+n \\ =4(4(T(\frac{n}{2^{2}})+\frac{n}{2})+n \\ =4T(\frac{n}{2^{2}})+2n+n \\ =4^{k}T(\frac{n}{2^{k}})+n(2^0+2^1+\dots+2^{k-1}) \end{array} T(n)=4T(2n)+n=4(4(T(22n)+2n)+n=4T(22n)+2n+n=4kT(2kn)+n(20+21++2k1)
假设 n n n 是 2 的幂,则最后 k k k 应该等于 log ⁡ 2 ( n ) \log_{2}(n) log2(n) ,故:
T ( n ) = T ( 1 ) n 2 + n ∑ i = 1 log ⁡ 2 ( n ) 2 i − 1 = T ( 1 ) n 2 + n ( n − 1 ) = O ( n 2 ) T(n)=T(1)n^{2}+n\sum\limits_{i=1}^{\log_{2}(n)}2^{i-1}=T(1)n^{2}+n(n-1)=O(n^{2}) T(n)=T(1)n2+ni=1log2(n)2i1=T(1)n2+n(n1)=O(n2)
T ( n ) = 2 T ( n 2 ) + n = n T ( 1 ) + n log ⁡ 2 ( n ) = O ( n log ⁡ n ) T(n)=2T(\frac{n}{2})+n=nT(1)+n\log_{2}(n)=O(n\log{n}) T(n)=2T(2n)+n=nT(1)+nlog2(n)=O(nlogn)
T ( n ) = 4 T ( n 2 ) + n 2 = n 2 T ( 1 ) + n 2 log ⁡ 2 n = O ( n 2 log ⁡ n ) T(n)=4T(\frac{n}{2})+n^{2}=n^{2}T(1)+n^{2}\log_{2}{n}=O(n^{2}\log{n}) T(n)=4T(2n)+n2=n2T(1)+n2log2n=O(n2logn)
T ( n ) = 2 T ( n 2 ) + n 2 = n T ( 1 ) + 2 n 2 = O ( n 2 ) T(n)=2T(\frac{n}{2})+n^2=nT(1)+2n^{2}=O(n^{2}) T(n)=2T(2n)+n2=nT(1)+2n2=O(n2)
T ( n ) = 4 T ( n 2 ) + n 2 log ⁡ n T(n)=4T(\frac{n}{2})+\frac{n^{2}}{\log{n}} T(n)=4T(2n)+lognn2
T ( n ) = 4 T ( n 2 ) + n 2 log ⁡ n = n 2 T ( 1 ) + n 2 log ⁡ n + n 2 log ⁡ n − 1 + ⋯ + n 2 log ⁡ 1 = θ ( n 2 log ⁡ log ⁡ n ) \begin{array}{l} \quad T(n) \\ =4T(\frac{n}{2})+\frac{n^{2}}{\log{n}} \\ =n^{2}T(1)+\frac{n^{2}}{\log{n}}+\frac{n^{2}}{\log{n-1}}+\dots+\frac{n^{2}}{\log{1}} \\ =\theta(n^{2}\log{\log{n}}) \end{array} T(n)=4T(2n)+lognn2=n2T(1)+lognn2+logn1n2++log1n2=θ(n2loglogn)
因为这个级数可以看成积分:
1 x + 1 x − 1 + 1 x − 2 + ⋯ = ∫ 1 x   d x = ln ⁡ x \frac{1}{x}+\frac{1}{x-1}+\frac{1}{x-2}+\dots=\int_{1}^x \, dx=\ln{x} x1+x11+x21+=1xdx=lnx

主方法

Th:设 a ≥ 1 a\geq 1 a1 b ≥ 1 b\geq 1 b1 f ( n ) f(n) f(n) 为一定义在非负整数上的函数, T ( n ) = a T ( n b ) + f ( n ) T(n)=aT(\frac{n}{b})+f(n) T(n)=aT(bn)+f(n) (当 n b \frac{n}{b} bn 不为整数时代表 ⌈ n b ⌉ \lceil \frac{n}{b} \rceil bn ⌊ n b ⌋ \lfloor \frac{n}{b} \rfloor bn ),则:

  1. ∃ ε > 0 \exists \varepsilon>0 ε>0 ,使得 f ( n ) = O ( n log ⁡ b a − ε ) f(n)=O(n^{\log_{b}{a}-\varepsilon}) f(n)=O(nlogbaε) ,则 T ( n ) = Θ ( n log ⁡ b a ) T(n)=\Theta(n^{\log_{b}{a}}) T(n)=Θ(nlogba)
  2. ∃ k ≥ 0 \exists k\geq 0 k0 ,使得 f ( n ) = Θ ( n log ⁡ b a   l g k n ) f(n)=\Theta(n^{\log_{b}{a}\,lg^k{n}}) f(n)=Θ(nlogbalgkn) ,则 T ( n ) = Θ ( n log ⁡ b a   l g k + 1 n ) T(n)=\Theta(n^{\log_{b}{a}}\,lg^{k+1}{n}) T(n)=Θ(nlogbalgk+1n)
  3. ∃ ε > 0 \exists \varepsilon>0 ε>0 ,使得 f ( n ) = Ω ( n log ⁡ b a + ε ) f(n)=\Omega(n^{\log_{b}{a}+\varepsilon}) f(n)=Ω(nlogba+ε) ,且存在 0 < c < 1 0<c<1 0<c<1 以及正整数 N 0 N_{0} N0 ,使得当 n > N 0 n>N_{0} n>N0 时,有 a f ( n b ) ≤ c f ( n ) af(\frac{n}{b})\leq cf(n) af(bn)cf(n) ,则 T ( n ) = Θ ( f ( n ) ) T(n)=\Theta(f(n)) T(n)=Θ(f(n))
    证明:首先展开,得到:

T ( n ) = a T ( n b ) + f ( n ) = a ( a T ( n b 2 ) + f ( n b ) ) + f ( n ) a 2 T ( n b 2 ) + a f ( n b ) + f ( n ) = … = a log ⁡ b n T ( 1 ) + ∑ i = 0 log ⁡ b n − 1 a i f ( n b i ) \begin{array}{l} \quad T(n) \\ =aT(\frac{n}{b})+f(n) \\ =a(aT(\frac{n}{b^2})+f(\frac{n}{b}))+f(n) \\ a^2T(\frac{n}{b^{2}})+af(\frac{n}{b})+f(n) \\ =\dots \\ =a^{\log_{b}{n}}T(1)+\sum\limits_{i=0}^{\log_{b}{n}-1}a^if(\frac{n}{b^i}) \end{array} T(n)=aT(bn)+f(n)=a(aT(b2n)+f(bn))+f(n)a2T(b2n)+af(bn)+f(n)==alogbnT(1)+i=0logbn1aif(bin)

其中 a log ⁡ b n = b log ⁡ b a log ⁡ b n = n log ⁡ b a a^{\log_{b}{n}}=b^{\log_{b}a\log_{b}n}=n^{\log_{b}a} alogbn=blogbalogbn=nlogba ;如果把 f ( n ) f(n) f(n) 看成多项式的话,只需要比较 f ( n ) f(n) f(n) 的次数与 log ⁡ b a \log_{b}a logba 的相对大小;
① 若 ∃ ε > 0 \exists \varepsilon>0 ε>0 ,使得 f ( n ) = O ( n log ⁡ b a − ε ) f(n)=O(n^{\log_{b}{a}-\varepsilon}) f(n)=O(nlogbaε) ,则 T ( n ) = Θ ( n log ⁡ b a ) T(n)=\Theta(n^{\log_{b}{a}}) T(n)=Θ(nlogba)
∑ i = 0 log ⁡ b n − 1 a i f ( n b i ) = ∑ a i ( n b ) log ⁡ b a − ε = ∑ n log ⁡ b a − ε − b i ε a i = n log ⁡ b a − ε ∑ b i ε = n log ⁡ b a − ε 1 − n ε 1 − b ε = O ( n log ⁡ b a ) \begin{array}{l} \quad\sum\limits_{i=0}^{\log_{b}{n}-1}a^if\left( \frac{n}{b^i} \right) \\ =\sum\limits a^i\left( \frac{n}{b} \right)^{\log_{b}a-\varepsilon} \\ =\sum\limits \frac{n^{\log_{b}a-\varepsilon}-b^{i\varepsilon}}{a^i} \\ =n^{\log_{b}a-\varepsilon}\sum\limits b^{i\varepsilon} \\ =n^{\log_{b}a-\varepsilon} \frac{1-n^{\varepsilon}}{1-b^{\varepsilon}} \\ =O(n^{\log_{b}a}) \end{array} i=0logbn1aif(bin)=ai(bn)logbaε=ainlogbaεbiε=nlogbaεbiε=nlogbaε1bε1nε=O(nlogba)
② 若 ∃ k ≥ 0 \exists k\geq 0 k0 ,使得 f ( n ) = Θ ( n log ⁡ b a   l g k n ) f(n)=\Theta(n^{\log_{b}{a}\,lg^k{n}}) f(n)=Θ(nlogbalgkn) ,则 T ( n ) = Θ ( n log ⁡ b a   l g k + 1 n ) T(n)=\Theta(n^{\log_{b}{a}}\,lg^{k+1}{n}) T(n)=Θ(nlogbalgk+1n)
我们就假设 f ( n ) = n log ⁡ b a lg ⁡ k n f(n)=n^{\log_{b}a}\lg^k{n} f(n)=nlogbalgkn ,则:
∑ i = 0 log ⁡ b n − 1 a i f ( n b i ) = ∑ a i ( n b i ) log ⁡ b a lg ⁡ k ( n b i ) = ∑ a i n log ⁡ b a a i ( lg ⁡ n − i lg ⁡ b ) k \begin{array}{l} \quad\sum\limits_{i=0}^{\log_{b}{n}-1}a^if\left( \frac{n}{b^i} \right) \\ =\sum\limits a^{i}(\frac{n}{b^{i}})^{\log_{b}{a}}\lg^k{(\frac{n}{b^i})} \\ =\sum\limits a^{i}\frac{n^{\log_{b}a}}{a^{i}}(\lg{n}-i\lg{b})^k \end{array} i=0logbn1aif(bin)=ai(bin)logbalgk(bin)=aiainlogba(lgnilgb)k
这个 ( lg ⁡ n − i lg ⁡ b ) k (\lg{n}-i\lg{b})^k (lgnilgb)k 二项展开的话会发现,次数肯定是 lg ⁡ k n \lg^k{n} lgkn 决定的,则:
≈ ∑ n l o g b a lg ⁡ k n = log ⁡ b n ⋅ n l o g b a lg ⁡ k n = n log ⁡ b a   l g k + 1 n \begin{array}{l} \approx \sum\limits n^{\\log_{b}a}\lg^k{n} \\ =\log_{b}{n} \cdot n^{\\log_{b}a}\lg^k{n} \\ =n^{\log_{b}{a}}\,lg^{k+1}{n} \end{array} nlogbalgkn=logbnnlogbalgkn=nlogbalgk+1n
③ 若 ∃ ε > 0 \exists \varepsilon>0 ε>0 ,使得 f ( n ) = Ω ( n log ⁡ b a + ε ) f(n)=\Omega(n^{\log_{b}{a}+\varepsilon}) f(n)=Ω(nlogba+ε) ,且存在 0 < c < 1 0<c<1 0<c<1 以及正整数 N 0 N_{0} N0 ,使得当 n > N 0 n>N_{0} n>N0 时,有 a f ( n b ≤ c f ( n ) ) af(\frac{n}{b}\leq cf(n)) af(bncf(n)) ,则 T ( n ) = Θ ( f ( n ) ) T(n)=\Theta(f(n)) T(n)=Θ(f(n))
当满足条件时,有 c f ( n ) ≥ a f ( n b ) cf(n)\geq af(\frac{n}{b}) cf(n)af(bn) ,得到:
f ( n ) ≥ a c f ( n b ) ≥ a 2 c 2 f ( n b 2 ) ≥ ⋯ ≥ a i c i f ( n b i ) f(n)\geq \frac{a}{c}f(\frac{n}{b}) \geq \frac{a^{2}}{c^{2}}f(\frac{n}{b^{2}})\geq\dots\geq\frac{a^{i}}{c^{i}}f(\frac{n}{b^{i}}) f(n)caf(bn)c2a2f(b2n)ciaif(bin)
f ( n ) ≥ a i c i f ( n b i ) f(n)\geq\frac{a^{i}}{c^{i}}f(\frac{n}{b^{i}}) f(n)ciaif(bin) ;需要满足所有大于等于的条件,发现只需要最后一个大于等于满足即可,即 n b i > N 0    ⟹    i < log ⁡ b n − log ⁡ b N 0 + 1 \frac{n}{b^{i}}\gt N_{0}\implies i< \log_{b}n-\log_{b}{N_{0}}+1 bin>N0i<logbnlogbN0+1
∑ i = 0 log ⁡ b n − 1 a i f ( n b i ) = ∑ i = 0 log ⁡ b n − log ⁡ b N 0 a i f ( n b i ) + ∑ i = log ⁡ b n − log ⁡ b N 0 + 1 log ⁡ b n − 1 a i f ( n b i ) \begin{array}{l} \quad\sum\limits_{i=0}^{\log_{b}{n}-1}a^if\left( \frac{n}{b^i} \right) \\ =\sum\limits_{i=0}^{\log_{b}^n-\log_{b}^{N_{0}}} a^if\left( \frac{n}{b^i} \right)+\sum\limits_{i=\log_{b}^n-\log_{b}^{N_{0}}+1}^{\log_{b}n-1}a^if\left( \frac{n}{b^i} \right) \end{array} i=0logbn1aif(bin)=i=0logbnlogbN0aif(bin)+i=logbnlogbN0+1logbn1aif(bin)
左项是满足 i < log ⁡ b n − log ⁡ b N 0 + 1 i< \log_{b}n-\log_{b}{N_{0}}+1 i<logbnlogbN0+1 的条件,有:
≤ ∑ i = 0 log ⁡ b n − log ⁡ b N 0 c i f ( n ) = O ( f ( n ) ) \leq \sum\limits_{i=0}^{\log_{b}^n-\log_{b}^{N_{0}}}c^if(n)=O(f(n)) i=0logbnlogbN0cif(n)=O(f(n))
右项是 O ( 1 ) O(1) O(1) ,展开也看不出来(直接把 f ( n ) f(n) f(n) 看作 O ( n log ⁡ b a + ε ) O(n^{\log_{b}a+{\varepsilon}}) O(nlogba+ε)):
= ∑ a i ( n b i ) log ⁡ b a + ε = ∑ a i n log ⁡ b a + ε a i ⋅ b i ε = n log ⁡ b a + ε ∑ 1 b i ε = 1 − ( 1 b ε ) log ⁡ b N 0 − 1 1 − 1 b ε ⋅ 1 b ε ( log ⁡ b n − log ⁡ b N 0 + 1 ) ⋅ n log ⁡ b a + ε \begin{array}{l} =\sum\limits a^i\left( \frac{n}{b^i} \right)^{\log_{b}a+\varepsilon} \\ =\sum\limits a^i\frac{n^{\log_{b}a+\varepsilon}}{a^i\cdot b^{i\varepsilon}} \\ =n^{\log_{b}a+\varepsilon}\sum\limits\frac{1}{b^{i\varepsilon}} \\ =\frac{1-(\frac{1}b^{\varepsilon})^{\log_{b}N_{0}-1}}{1-\frac{1}{b^{\varepsilon}}}\cdot \frac{1}{b^{\varepsilon(\log_{b}n-\log_{b}N_{0}+1)}} \cdot n^{\log_{b}a+\varepsilon} \end{array} =ai(bin)logba+ε=aiaibiεnlogba+ε=nlogba+εbiε1=1bε11(b1ε)logbN01bε(logbnlogbN0+1)1nlogba+ε
第一项是常数,后边两项乘起来看着像个常数

Akra-Bazzi 定理

Th:设 g ( x ) g(x) g(x) 为一非负函数, T ( x ) = { Θ ( 1 ) , 1 ≤ x ≤ X 0 ∑ i = 1 k a i T ( x b i ) + g ( x ) , n > X 0 T(x)=\left\{\begin{array}{ll}\Theta(1) & ,1\leq x \leq X_{0} \\ \sum\limits_{i=1}^k a_{i}T(\frac{x}{b_{i}})+g(x)& ,n>X_{0} \end{array}\right. T(x)= Θ(1)i=1kaiT(bix)+g(x),1xX0,n>X0 (其中 k ≥ 1 k\geq 1 k1 a i > 0 a_{i}>0 ai>0 b i > 1 b_{i}>1 bi>1 X 0 X_{0} X0 满足对任意 1 ≤ i ≤ k 1\leq i\leq k 1ik X 0 > b i X_{0}>b_{i} X0>bi X 0 > b i b i − 1 X_{0}>\frac{b_{i}}{b_{i}-1} X0>bi1bi ),若 g ( x ) g(x) g(x) 满足多项式增长条件, p p p 为方程 ∑ i = 1 k a i b i p = 1 \sum\limits_{i=1}^k\frac{a_{i}}{b_{i}^p}=1 i=1kbipai=1 的实数解,则:
T ( x ) = Θ ( x p ( 1 + ∫ 1 x g ( x ) x p + 1 d x ) ) T(x)=\Theta\left( x^p\left( 1+\int_{1}^x \frac{g(x)}{x^{p+1}}dx \right) \right) T(x)=Θ(xp(1+1xxp+1g(x)dx))
T ( n ) = 2 T ( n 4 + 3 T ( n 6 ) + n lg ⁡ n ) T(n)=2T(\frac{n}{4}+3T(\frac{n}{6})+n\lg{n}) T(n)=2T(4n+3T(6n)+nlgn)
2 4 p + 3 6 p = 1 \frac{2}{4^p}+\frac{3}{6^p}=1 4p2+6p3=1 p = 1 p=1 p=1
T ( n ) = Θ ( n ( 1 + ∫ 1 n x lg ⁡ x x 2 d x ) ) = Θ ( n ( 1 + 1 2 lg ⁡ 2 n ) ) = Θ ( n lg ⁡ 2 n ) \begin{array}{l} \quad T(n) \\ =\Theta(n(1+\int_{1}^n \frac{x\lg{x}}{x^{2}} dx)) \\ =\Theta(n(1+\frac{1}{2}\lg^2{n})) \\ =\Theta(n\lg^2{n}) \end{array} T(n)=Θ(n(1+1nx2xlgxdx))=Θ(n(1+21lg2n))=Θ(nlg2n)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Air浩瀚

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值