final 周一边复习一边过一遍这本书--algorithms,中文名叫--算法概论,参考教材是introduction to algorithm CLRS, 也是算法界经典教材,CLRS对各个数据结构和小知识点介绍的更详细。但algorithms很精辟,以后学有余力再去看CLRS。
CH0
0.2 Enter Fibonacci
-
An exponential algorithm
function fib1(n) if n = 0: return 0 if n = 1: return 1 return fib1(n-1)+fib1(n-2)
-
An polynomial algorithm
function fib2(n) if n = 0: return 0 create an array f[0...n] f[0] = 0 f[1] = 1 for i = 2...n: f[i] = f[i-1] + [i-2]
store the intermediate results (F1,F2...) as soon as they become known
conclusion:Fibonacci数列的求法:
- 递归求解,指数级复杂度
- 动态规划,从底至顶
- 矩阵求解(exercise0.4),能求出通项公式
0.3 O notation
uncluttered, machine-independent characterization of an algorithm’s efficiency : express running time by counting the number of basic computer steps,** as a function of the size of the input.**
Let f(n) and g(n) be functions from positive integers to positive reals.** We say f = O(g) (which means that “f grows no faster than g”) if there is a constant c > 0 such that f(n) ≤ c · g(n).**
f = Ω(g) means g = O(f)
f =Θ(g)means f =O(g) and f =Ω(g)
* Multiplicative constants can be omitted
* n^a dominates n^b if a > b, eg:n^2 dominates n
* Any exponential dominates any polynomial: 3n dominates n5
* any polynomial dominates any logarithm: n dominates (log n)^3
Exercise
0.2. Show that, if c is a positive real number, then g(n) = 1 + c + c^2 + · · · + c^n is:
(a) Θ(1) if c < 1.
(b) Θ(n) if c = 1
.(c) Θ(c n ) if c > 1.
The moral: in big-Θ terms, the sum of a geometric series is simply the first term if the series is strictly decreasing, the last term if the series is strictly increasing,or the number of terms if the series is unchanging.
0.3. The Fibonacci numbers F0 , F1 , F2 , . . . , are defined by the ruleF0 = 0, F1 = 1, Fn = F(n−1) + F(n−2) .In this problem we will confirm that this sequence grows exponentially fast and obtain somebounds on its growth.
(a) Use induction to prove that Fn ≥ 2^(0.5*n) for n ≥ 6.
(b) Find a constant c < 1 such that Fn ≤ 2^(c*n) for all n ≥ 0. Show that your answer is correct.
(c) What is the largest c you can find for which Fn = Ω(2^(c*n))?
网友评论