1 算法分析技术
1.1 假设 f f f 和 g g g 是定义在自然数集合上的函数, 若对某个其他函数 h h h 有 f = O ( h ) f = O(h) f=O(h)和 g = O ( h ) g = O(h) g=O(h) 成立, 那么证明 f + g = O ( h ) f + g = O(h) f+g=O(h)
证明:
根据已知条件 f = O ( h ) f = O(h) f=O(h),存在 c 1 > 0 c_{1}>0 c1>0 和正整数 n 1 n_{1} n1 使得当使得当 n ⩾ n 1 n \geqslant n_{1} n⩾n1 时, 有 f ( n ) ⩽ c 1 h ( n ) f(n) \leqslant c_{1} h(n) f(n)⩽c1h(n) 。
同理根据已知条件 g = O ( h ) g = O(h) g=O(h),存在 c 2 > 0 c_{2}>0 c2>0 和正整数 n 2 n_{2} n2 使得当使得当 n ⩾ n 2 n \geqslant n_{2} n⩾n2 时, 有 g ( n ) ⩽ c 2 h ( n ) g(n) \leqslant c_{2} h(n) g(n)⩽c2h(n) 。
取常数
c
0
=
max
{
2
c
1
,
2
c
2
}
c_{0}=\max \left\{2 c_{1}\right. , \left.2 c_{2}\right\}
c0=max{2c1,2c2},
n
0
=
max
{
n
1
,
n
2
}
n_{0}=\max \left\{n_{1}, n_{2}\right\}
n0=max{n1,n2}, 那么当
n
⩾
n
0
n \geqslant n_{0}
n⩾n0 时,可以得到
f
(
n
)
+
g
(
n
)
⩽
c
1
h
(
n
)
+
c
2
h
(
n
)
⩽
c
h
(
n
)
f(n)+g(n) \leqslant c_{1} h(n)+c_{2} h(n) \leqslant c h(n)
f(n)+g(n)⩽c1h(n)+c2h(n)⩽ch(n)
由此可证
f
+
g
=
O
(
h
)
f + g = O(h)
f+g=O(h)。
1.2 设 n , a , b n, a, b n,a,b 为正整数, 证明下述性质:
⌈ ⌈ n a ⌉ b ⌉ = ⌈ n a b ⌉ , ⌊ ⌊ n a ⌋ b ⌋ = ⌊ n a b ⌋ \left\lceil\frac{\left\lceil\frac{n}{a}\right\rceil}{b}\right\rceil=\left\lceil\frac{n}{a b}\right\rceil,\left\lfloor\frac{\left\lfloor\frac{n}{a}\right\rfloor}{b}\right\rfloor=\left\lfloor\frac{n}{a b}\right\rfloor ⌈b⌈an⌉⌉=⌈abn⌉,⌊b⌊an⌋⌋=⌊abn⌋
证明:
当 n n n为 a b ab ab的整数倍时,设 n = p a b n=pab n=pab,命题1和命题2的等式左右两边化简后均为 p p p,命题显然成立。
当 n n n不是 a b ab ab的整数倍时,设 n = p a b + r ( 0 < r < a b ) n=pab+r(0<r<ab) n=pab+r(0<r<ab),
命题1等式左边进行如下化简:
⌈
⌈
n
a
⌉
b
⌉
=
⌈
⌈
p
a
b
+
r
a
⌉
b
⌉
=
⌈
p
b
+
⌈
r
a
⌉
b
⌉
=
⌈
p
+
⌈
r
a
⌉
b
⌉
=
p
+
⌈
⌈
r
a
⌉
b
⌉
=
p
+
1
\left\lceil\frac{\left\lceil\frac{n}{a}\right\rceil}{b}\right\rceil=\left\lceil\frac{\left\lceil\frac{p a b+r}{a}\right\rceil}{b}\right\rceil=\left\lceil\frac{p b+\left\lceil\frac{r}{a}\right\rceil}{b}\right\rceil=\left\lceil{p+\frac{\left\lceil\frac{r}{a}\right\rceil}{b}}\right\rceil=p+\left\lceil\frac{\left\lceil \frac{r}{a}\right\rceil}{b}\right\rceil=p+1
⌈b⌈an⌉⌉=
b⌈apab+r⌉
=⌈bpb+⌈ar⌉⌉=⌈p+b⌈ar⌉⌉=p+⌈b⌈ar⌉⌉=p+1
命题1等式右边进行如下化简:
⌈
n
a
b
⌉
=
⌈
p
a
b
+
r
a
b
⌉
=
⌈
p
+
r
a
b
⌉
=
p
+
⌈
r
a
b
⌉
=
p
+
1
\left\lceil\frac{n}{a b}\right\rceil=\left\lceil\frac{pab+r}{a b}\right\rceil=\left\lceil{p+\frac{r}{a b}}\right\rceil=p+\left\lceil{\frac{r}{a b}}\right\rceil=p+1
⌈abn⌉=⌈abpab+r⌉=⌈p+abr⌉=p+⌈abr⌉=p+1
由此可证
⌈
⌈
n
a
⌉
b
⌉
=
⌈
n
a
b
⌉
\left\lceil\frac{\left\lceil\frac{n}{a}\right\rceil}{b}\right\rceil=\left\lceil\frac{n}{a b}\right\rceil
⌈b⌈an⌉⌉=⌈abn⌉
同理对命题2等式左边进行化简:
⌊
⌊
n
a
⌋
b
⌋
=
⌊
⌊
p
a
b
+
r
a
⌋
b
⌋
=
⌊
p
b
+
⌊
r
a
⌋
b
⌋
=
⌊
p
+
⌊
r
a
⌋
b
⌋
=
p
+
⌊
⌊
r
a
⌋
b
⌋
=
p
\left\lfloor\frac{\left\lfloor\frac{n}{a}\right\rfloor}{b}\right\rfloor=\left\lfloor\frac{\left\lfloor\frac{p a b+r}{a}\right\rfloor}{b}\right\rfloor=\left\lfloor\frac{p b+\left\lfloor\frac{r}{a}\right\rfloor}{b}\right\rfloor=\left\lfloor{p+\frac{\left\lfloor\frac{r}{a}\right\rfloor}{b}}\right\rfloor=p+\left\lfloor\frac{\left\lfloor \frac{r}{a}\right\rfloor}{b}\right\rfloor=p
⌊b⌊an⌋⌋=
b⌊apab+r⌋
=⌊bpb+⌊ar⌋⌋=⌊p+b⌊ar⌋⌋=p+⌊b⌊ar⌋⌋=p
对命题2等式右边进行化简:
⌊
n
a
b
⌋
=
⌊
p
a
b
+
r
a
b
⌋
=
⌊
p
+
r
a
b
⌋
=
p
+
⌊
r
a
b
⌋
=
p
\left\lfloor\frac{n}{a b}\right\rfloor=\left\lfloor\frac{pab+r}{a b}\right\rfloor=\left\lfloor{p+\frac{r}{a b}}\right\rfloor=p+\left\lfloor{\frac{r}{a b}}\right\rfloor=p
⌊abn⌋=⌊abpab+r⌋=⌊p+abr⌋=p+⌊abr⌋=p
由此可证
⌊
⌊
n
a
⌋
b
⌋
=
⌊
n
a
b
⌋
\left\lfloor\frac{\left\lfloor\frac{n}{a}\right\rfloor}{b}\right\rfloor=\left\lfloor\frac{n}{a b}\right\rfloor
⌊b⌊an⌋⌋=⌊abn⌋
1.3 对于下面每个函数 f ( n ) f(n) f(n), 用 Θ 符号表示成 f ( n ) = Θ ( g ( n ) ) f(n) = Θ(g(n)) f(n)=Θ(g(n)) 的形式, 其中 g ( n ) g(n) g(n) 要尽可能简洁. 比如 f ( n ) = n 2 + 2 n + 3 f(n) = n^2 + 2n + 3 f(n)=n2+2n+3 可以写成 f ( n ) = Θ ( n 2 ) f(n) = Θ(n^2 ) f(n)=Θ(n2). 然后按照阶递增的顺序将这些函数进行排列:
( n − 2 ) ! , 5 log ( n + 100 ) 10 , 2 2 n , 0.001 n 4 + 3 n 3 + 1 , ( ln n ) 2 n 1 / 3 + log n , 3 n , log ( n ! ) , log ( n n + 1 ) , 1 + 1 2 + ⋯ + 1 n \begin{array}{c} (n-2) !, 5 \log (n+100)^{10}, 2^{2 n}, 0.001 n^{4}+3 n^{3}+1,(\ln n)^{2} \\ n^{1 / 3}+\log n, 3^{n}, \log (n !), \log \left(n^{n+1}\right), 1+\frac{1}{2}+\cdots+\frac{1}{n} \end{array} (n−2)!,5log(n+100)10,22n,0.001n4+3n3+1,(lnn)2n1/3+logn,3n,log(n!),log(nn+1),1+21+⋯+n1
解:
下表格从上到下按照函数的阶递增的顺序排列:
Θ表达式 | 函数 |
---|---|
Θ ( log n ) \Theta(\log n) Θ(logn) | 1 + 1 2 + ⋯ + 1 n , 5 log ( n + 100 ) 10 1+\frac{1}{2}+\cdots+\frac{1}{n}, 5 \log (n+100)^{10} 1+21+⋯+n1,5log(n+100)10 |
Θ ( log 2 n ) \Theta\left(\log ^{2} n\right) Θ(log2n) | ln n ) 2 \ln n)^{2} lnn)2 |
Θ ( n 3 ) \Theta(\sqrt[3]{n}) Θ(3n) | n 1 / 3 + log n , 3 n n^{1 / 3}+\log n, 3^{n} n1/3+logn,3n |
Θ ( n log n ) \Theta(n \log n) Θ(nlogn) | log ( n n + 1 ) , log ( n ! ) \log \left(n^{n+1}\right),\log (n !) log(nn+1),log(n!) |
Θ ( n 4 ) \Theta\left(n^{4}\right) Θ(n4) | 0.001 n 4 + 3 n 3 + 1 0.001 n^{4}+3 n^{3}+1 0.001n4+3n3+1 |
Θ ( 3 n ) \Theta\left(3^{n}\right) Θ(3n) | 3 n 3^{n} 3n |
Θ ( 4 n ) \Theta\left(4^{n}\right) Θ(4n) | 2 2 n 2^{2 n} 22n |
Θ ( ( n − 2 ) ! ) \Theta((n-2) !) Θ((n−2)!) | ( n − 2 ) ! (n-2) ! (n−2)! |
1.4 求解以下递推方程:
{ T ( n ) = T ( n − 1 ) + n 2 T ( 1 ) = 1 \left\{\begin{array}{c} T(n)=T(n-1)+n^{2} \\ T(1)=1 \end{array}\right. {T(n)=T(n−1)+n2T(1)=1
解:
由递推公式
T
(
n
)
=
T
(
n
−
1
)
+
n
2
T(n)=T(n-1)+n^{2}
T(n)=T(n−1)+n2不断迭代可得
T
(
n
)
=
n
2
+
(
n
−
1
)
2
+
⋯
+
2
2
+
T
(
1
)
=
1
+
2
2
+
⋯
+
n
2
=
n
(
n
+
1
)
(
2
n
+
1
)
6
=
Θ
(
n
3
)
T(n) =n^{2}+(n-1)^{2}+\cdots+2^{2}+T(1)\\=1+2^{2}+\cdots+n^{2}=\frac{n(n+1)(2 n+1)}{6}=\Theta\left(n^{3}\right)
T(n)=n2+(n−1)2+⋯+22+T(1)=1+22+⋯+n2=6n(n+1)(2n+1)=Θ(n3)
1.5 求解以下递推方程:
{ T ( n ) = 9 T ( n / 3 ) + n T ( 1 ) = 1 \left\{\begin{array}{c} T(n)=9 T(n / 3)+n \\ T(1)=1 \end{array}\right. {T(n)=9T(n/3)+nT(1)=1
解:
已知
a
=
9
,
b
=
3
,
f
(
n
)
=
n
a=9,b=3,f(n)=n
a=9,b=3,f(n)=n,运用主定理可得
T
(
n
)
=
Θ
(
n
log
3
9
)
=
Θ
(
n
2
)
T(n)=\Theta\left(n^{\log _{3} 9}\right)=\Theta\left(n^{2}\right)
T(n)=Θ(nlog39)=Θ(n2)
1.6 求解以下递推方程:
{ T ( n ) = T ( n 2 ) + T ( n 4 ) + c n , c 为常数 T ( 1 ) = 1 \left\{\begin{array}{c} T(n)=T\left(\frac{n}{2}\right)+T\left(\frac{n}{4}\right)+c n, c \text { 为常数 } \\ T(1)=1 \end{array}\right. {T(n)=T(2n)+T(4n)+cn,c 为常数 T(1)=1
解:
对该递推方程构建递归树求解,可得第x个递归层复杂度总和为 ( 3 4 ) x − 1 c n \left(\frac{3}{4}\right)^{x-1}cn (43)x−1cn
由此可得
T
(
n
)
=
T
(
n
2
)
+
T
(
n
4
)
+
c
n
=
T
(
n
4
)
+
2
T
(
n
8
)
+
T
(
n
16
)
+
3
c
n
4
+
c
n
=
c
n
+
3
c
n
4
+
(
3
4
)
2
c
n
+
⋯
=
[
1
+
3
4
+
(
3
4
)
2
+
⋯
]
c
n
=
Θ
(
n
)
T(n)=T\left(\frac{n}{2}\right)+T\left(\frac{n}{4}\right)+c n=T\left(\frac{n}{4}\right)+2T\left(\frac{n}{8}\right)+T\left(\frac{n}{16}\right)+\frac{3 c n}{4}+c n\\=c n+\frac{3 c n}{4}+\left(\frac{3}{4}\right)^{2} c n+\cdots=\left[1+\frac{3}{4}+\left(\frac{3}{4}\right)^{2}+\cdots\right] c n=\Theta(n)
T(n)=T(2n)+T(4n)+cn=T(4n)+2T(8n)+T(16n)+43cn+cn=cn+43cn+(43)2cn+⋯=[1+43+(43)2+⋯]cn=Θ(n)