基础复习-算法设计基础 | 复杂度计算
一. 算法时间复杂度比较符号
Definition of Asymptotic Signature
一般讨论的 “计算算法的(时间)复杂度” 是人们为了衡量算法性能创造的一种评估方法,我们引入O,Ω,Θ符号来表示算法的复杂度比较关系,在实际分析中我们默认:f,g are monotonic functions and maps from positive to positive.
"O" signature:
-
- We say f(n) = O(g(n)) if g(n) eventually domains f(n).
Or:
-
- If there exists a constant c such thta for all sufficient large n: f(n) ≤ c*g(n).
"Ω" signature:
-
- We say f(n) = Ω(g(n)) if f(n) eventually domains g(n).
Or:
-
- If there exists a constant c such that for all sufficient large n: f(n) ≥ c*g(n).
"Θ" signature:
-We say f(n) = Θ(g(n)) if f(n) = O(g(n)) and f(n) = Ω(g(n))
Note here in the definition of big o and big sigma, the constant c locate at the same position at the formular, but we can understand it like this:
- f(n) ≥ c*g(n)
- 1/c*f(n) ≥ g(n)
- a*f(n) ≥ g(n)
so we can think we are searching for a big constant c(a) to magnitude the domianing function in both asymptotic relation.
Approach of Sorting Functions in Asymptotic Order
Basically we can classify functions into three types: logarithmic, polinomial, exponencial function.
This is because functions belong to these three types are strictly in ascending asymptotic order:
证明对数函数增长慢于多项式函数:
-
- 令f(n) = n, 则 f(n) = O(P(n))
- 现证log(n) = O(f(n)):
令Φ(n) = c*f(n) - log(n)
令Φ'(n) = c - 1/n = 0, 得c = 1/n
故Φ(n)在 n = 1/c处取最小值为Φ(1/c) = 1 + log(c)
得到:当1 + log(c)>0 时,c*f(n) > log(n) 恒成立
证明指数函数增长快于多项式函数:
The properties of logarithic function:
- 和差
- 换底
- 指系
- 还原
- 互换
- 倒数
- 链式
Practice:
sorting functions below in ascending asymptotic order:
- log(nn), n2, nlog(n), nlog(log(n)), 2log(n), log2(n), n2^1/2
- n2.5, (2n)1/2, n+10, 10n, 100n, n2logn
- 2(logn)1/2, 2n, n4/3, nlogn, 22^n, 2n^2