前言
这个笔记旨在于复习和准备算法分析的基本知识,使用的课程是NYU courant的foundation of Algorithm. 本笔记会根据老师的进度实时更新。
第一章 基本数学知识和渐进分析(Basic mathematical knowledge and asymptotic analysis)
渐进分析符号(asymptotic symbol)
1. O ( g ( n ) ) = { f ( n ) : ∃ constants c , n 0 > 0 s.t. ∀ n ≥ n 0 : f ( n ) ≤ c ⋅ g ( n ) } O(g(n))=\left\{f(n): \exists \text { constants } c, n_{0}>0 \text { s.t. } \forall n \geq n_{0}: f(n) \leq c \cdot g(n)\right\} O(g(n))={ f(n):∃ constants c,n0>0 s.t. ∀n≥n0:f(n)≤c⋅g(n)}
— O ( g ) O(g) O(g)表示的是函数f(n)的上界(或者说上限),而严格意义的上界则表示为 o ( g ) o(g) o(g)(任意常数c,都可以是的上面的表达式成立)
注:当两个函数增长率(growth rate)一样的时候,这两个函数可以互为上界
2. Ω ( g ( n ) ) = { f ( n ) : ∃ \Omega(g(n))=\left\{f(n): \exists\right. Ω(g(n))={
f(n):∃ constants c , n 0 > 0 c, n_{0}>0 c,n0>0 s.t. ∀ n ≥ n 0 : f ( n ) ≥ c ⋅ g ( n ) } \left.\forall n \geq n_{0}: f(n) \geq c \cdot g(n)\right\} ∀n≥n0:f(n)≥c⋅g(n)}
– Ω ( g ) \Omega(g) Ω(g)表示的是函数f(n)的下界(或者说下限),而严格意义上的下界则表示为 ω ( g ) \omega(g) ω(g)
3. Θ ( g ( n ) ) = { f ( n ) : ∃ \Theta(g(n))=\left\{f(n): \exists\right. Θ(g(n))={
f(n):∃ constants c 1 , c 2 , n 0 > 0 c_{1}, c_{2}, n_{0}>0 c1,c2,n0>0
s.t. ∀ n ≥ n 0 : c 1 ⋅ g ( n ) ≤ f ( n ) ≤ c 2 ⋅ g ( n ) } \left.\forall n \geq n_{0}: c_{1} \cdot g(n) \leq f(n) \leq c_{2} \cdot g(n)\right\} ∀n≥n0:c1⋅g(n)≤f(n)≤c2⋅g(n)}
– Θ ( g ) \Theta(g) Θ(g)表示的是当上下限相等的时候的算法开销
4.当 f ( n ) = Θ ( g ) f(n)=\Theta(g) f(n)=Θ(g) 时 g = Θ ( f ) g=\Theta(f) g=Θ(f),且此时上下界对应的是相同的
限制测试(limits test) 和增长率(growth rate)
lim n → ∞ f ( n ) g ( n ) = c \lim _{n \rightarrow \infty} \frac{f(n)}{g(n)}=c n→∞limg(n)f(n)=c
- If c = 0 c=0 c=0, then f = o ( g ) f=o(g) f=o(g)
- If c = ∞ c=\infty c=∞, then f = ω ( g ) f=\omega(g) f=ω(g)
- If 0 < c < ∞ 0<c<\infty 0<c<∞, i.e., a finite positive constant, then f = Θ ( g ) f=\Theta(g) f=Θ(g)
循环不变性(loop invariant)
这个方法的作用是是为了确保在一组在循环体内、每次迭代均保持为真的性质,通常被用来证明程序或伪码的正确性。具体可以分为三步(for example: bubble sort):
- Base case: When i=1,trivially true because empty array
- Maintenance: Assume that the inner loop iterates from n to i+1 and finds that smallest elements among it. And also that true before iteration i. Then A[i] contains the smallest elements from A[i,…,n], thanks to inner loop
- Terminates: Algorithm Terminates Trivially and at i=n-1. So A[1,…,n-1] is sorted and A[n] is the largest element, and therefore fully sorted.
数学知识小结
1. lim h → ∞ n b a n = 0 ⇒ n b = 0 ( a n ) \lim _{h \rightarrow \infty} \frac{n^{b}}{a^{n}}=0 \quad \Rightarrow \quad n^{b}=0\left(a^{n}\right) limh→∞annb=0⇒nb=0(an)
2. 2 π n ( n e ) n ⩽ n ! ⩽ 2 π n ( n e ) n + 1 12 n \sqrt{2 \pi n}\left(\frac{n}{e}\right)^{n} \leqslant n ! \leqslant \sqrt{2 \pi n}\left(\frac{n}{e}\right)^{n+\frac{1}{12 n}} 2πn(en)n⩽n!⩽