Learning Resources "Algorithms", author S.dasgupta,c.h.papadimitriou,and U.v.vazirani.
The electronic version is available to download http://download.csdn.net/detail/segen_jaa/7900765 in the repository.
1, problem description
Fibonacci Series Presumably everyone is more familiar with, after a digit is the first two and.
0,1,1,2,3,5,8,13,21,34
The corresponding formula is as follows.
What is the number of Fibonacci for the nth bit?
2. Iterative algorithm
Implementation language: C language.
Win32 program, calculate the number of Fibonacci.
45-bit: 1134903170
46-bit: 1836311903
47-bit: 1323752223
The calculation to the 47th long type has not been supported, so the test program is agreed to within 50 bits.
#include "stdafx.h" #define Max_num 50long fib1 (int n) {if (0 = = N) return 0;if (1 = = N) return 1;return fib1 (n-1) +fib1 (n-2) ;} int _tmain (int argc, _tchar* argv[]) {while (true) {int input = 0;printf ("Please input Fibonacci number:"); scanf ("%d", & Input), if (input>max_num) {printf ("Please input number less than%d", max_num); continue;} if (input<0) {break;} int RESULT1 = FIB1 (input);p rintf ("Fib1 result:%d\n", RESULT1);} return 0;}
3. Iterative algorithm Analysis
Three questions
is the ① algorithm correct?
What is the complexity of the ② algorithm?
Can the ③ algorithm be improved?
Corresponding Answer:
① implementation is completely in accordance with the definition formula, correctness is not questioned.
② see recursion, we should all know, this algorithm time complexity is a bit high.
The complexity of the one-time plus-minus operation is 1, then T (n) =t (n-1) +t (n-2) +3.
Calculated t (n) value ≈
The exponential calculation method, the algorithm can be declared to fail. This means that the performance of the computer is increased by 1.6 times times, and only one more forward is calculated.
Under this algorithm, you can try the calculation on your own machine for how long. Native CPU i7 test, reckoning 50th bit near 1 hour time. If the same time to calculate the 51st place, only wait until i8, I9.
③ to improve the algorithm, first analyze why the complexity is so high.
The procedure for recursive invocation is as follows.
You can see that the same number has been repeatedly computed multiple times. In the case of F (n-3), it was calculated three times in. Then our idea of improvement starts here, and the calculated numbers are cached.
4. Improved algorithm
#include "stdafx.h" #define Max_num 50long fib2 (int n) {long array[max_num];array[0] = 0;array[1] = 1;for (int i=2; i<=n; ++i) {Array[i] = array[i-1]+array[i-2];} return array[n];} int _tmain (int argc, _tchar* argv[]) {while (true) {int input = 0;printf ("Please input Fibonacci number:"); scanf ("%d", & Input), if (input>max_num) {printf ("Please input number less than%d", max_num); continue;} if (input<0) {break;} int result2 = FIB2 (input);p rintf ("Fib2 result:%d\n", RESULT2);} return 0;}
the same problem
is the ① algorithm correct?
Answer: Correct.
What is the complexity of the ② algorithm?
Answer: Complexity o (n).
Can the ③ algorithm be improved?
Answer: Performance is linear and can no longer be improved.
Algorithmic Learning-The No. 0 chapter begins with Fibonacci