计算理论 复杂度预备知识
符号
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
n→∞∑g(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+⋯+2k−1)
假设
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=1∑log2(n)2i−1=T(1)n2+n(n−1)=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+logn−1n2+⋯+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+x−11+x−21+⋯=∫1xdx=lnx
主方法
Th:设 a ≥ 1 a\geq 1 a≥1 , b ≥ 1 b\geq 1 b≥1 , 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⌋ ),则:
- 若 ∃ ε > 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)
- 若 ∃ k ≥ 0 \exists k\geq 0 ∃k≥0 ,使得 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)
- 若
∃
ε
>
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=0∑logbn−1aif(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=0∑logbn−1aif(bin)=∑ai(bn)logba−ε=∑ainlogba−ε−biε=nlogba−ε∑biε=nlogba−ε1−bε1−nε=O(nlogba)
② 若
∃
k
≥
0
\exists k\geq 0
∃k≥0 ,使得
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=0∑logbn−1aif(bin)=∑ai(bin)logbalgk(bin)=∑aiainlogba(lgn−ilgb)k
这个
(
lg
n
−
i
lg
b
)
k
(\lg{n}-i\lg{b})^k
(lgn−ilgb)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=logbn⋅nlogbalgkn=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(bn≤cf(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>N0⟹i<logbn−logbN0+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=0∑logbn−1aif(bin)=i=0∑logbn−logbN0aif(bin)+i=logbn−logbN0+1∑logbn−1aif(bin)
左项是满足
i
<
log
b
n
−
log
b
N
0
+
1
i< \log_{b}n-\log_{b}{N_{0}}+1
i<logbn−logbN0+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=0∑logbn−logbN0cif(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+ε=∑aiai⋅biεnlogba+ε=nlogba+ε∑biε1=1−bε11−(b1ε)logbN0−1⋅bε(logbn−logbN0+1)1⋅nlogba+ε
第一项是常数,后边两项乘起来看着像个常数
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=1∑kaiT(bix)+g(x),1≤x≤X0,n>X0 (其中
k
≥
1
k\geq 1
k≥1 ,
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
1≤i≤k 有
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>bi−1bi ),若
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=1∑kbipai=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)