The article directories

    • Fibonacci sequence
    • Second, recursive solution of Fibonacci sequence
    • Time complexity analysis

Fibonacci sequence

  • Fibonacci sequence refers to a sequence in which the value of the current term equals the sum of the first two terms:
i i i 0 1 2 3 4 5 6 7 8 9
f ( i ) f(i) f(i) 1 1 2 3 5 8 13 21 34 55
  • The recursion formula of this sequence is as follows:

F (n) (n = 0) = {1, 1 (n = 1) f (n – 1) + f (n – 2) (n > 2) (n) = f \ begin {cases} 1 & (n = 0) \ \ 1 & (n = 1) \ \ f (n – 1) + f (n – 2) & (n > 2) f (n) = {cases} \ end ⎩ ⎪ ⎨ ⎪ ⎧ 11 f (n – 1) + f (n – 2) (n = 0) (n = 1) (n > 2)

Second, recursive solution of Fibonacci sequence

  • C++ code implementation is as follows:
int f(unsigned int n) {
	if(n <= 1) {
		return 1;
	}
	return f(n- 1) + f(n2 -);
}
Copy the code
  • Direct recursive solution according to the formula;

Time complexity analysis

  • The process of graphical solution is as follows:
  • This is a binary tree, the height of the tree is N, so the number of nodes visited in depth-first search is 2n 2^n 2n. However, if you look closely, the height of the leftmost node must be larger than the height of the right node, so it is not a strict complete binary tree. To explore its actual time complexity, let’s change the code:
int f(unsigned int n) {
	++c[n];
	if(n <= 1) {
		return 1;
	}
	return f(n- 1) + f(n2 -);
}
Copy the code
  • I added a line of code++c[n];Let’s see how many times f(n) f(n) f(n) will be called under different n, n, n cases;
  • The table measured is as follows:
n n n f ( n ) f(n) f(n) c [ n ] c[n] c[n]
0 1 1
1 1 1
2 2 3
3 3 5
4 5 9
5 8 15
6 13 25
7 21 41
8 34 67
9 55 109
10 89 177
11 144 287
12 233 465
13 377 753
14 610 1219
15 987 1973
16 1597 3193
17 2584 5167
18 4181 8361
19 6765 13529
20 10946 21891
21 17711 35421
22 28657 57313
23 46368 92735
24 75025 150049
25 121393 242785
26 196418 392835
27 317811 635621
28 514229 1028457
  • Observe the growth trend of C [n] C [n] C [n], first exclude the arithmetic sequence, and then see if it conforms to the arithmetic sequence. Let’s try to calculate the value of C [n]/ C [n−1] C [n]/c[n]. The table is as follows:
n n n f ( n ) f(n) f(n) c [ n ] c[n] c[n] C [n]/ C [N −1] C [n]/ C [n-1] C [n]/c[N −1]
1 1 1 1.000000
2 2 3 3.000000
3 3 5 1.666667
4 5 9 1.800000
5 8 15 1.666667
6 13 25 1.666667
7 21 41 1.640000
8 34 67 1.634146
9 55 109 1.626866
10 89 177 1.623853
11 144 287 1.621469
12 233 465 1.620209
13 377 753 1.619355
14 610 1219 1.618858
15 987 1973 1.618540
16 1597 3193 1.618348
17 2584 5167 1.618227
18 4181 8361 1.618154
19 6765 13529 1.618108
20 10946 21891 1.618080
21 17711 35421 1.618062
22 28657 57313 1.618051
23 46368 92735 1.618045
24 75025 150049 1.618041
25 121393 242785 1.618038
26 196418 392835 1.618037
27 317811 635621 1.618036
28 514229 1028457 1.618035
  • It is observed that as n n n increases, c[n]/c[n−1] c[n]/c[n-1] C [n]/c[n−1] gets closer and closer to a constant, which is: 2 5 −1 ≈1.618034 \frac {2}{\ SQRT 5-1} \approx 1.618034 5 −12≈1.618034
  • When n approaches infinity, the following formula is satisfied: c[n]= 2 5 −1 c[n−1] C [n]= \frac {2}{\ SQRT 5-1} c[n-1] c[n]=5 −12c[n−1]
  • After solving the geometric sequence, we can get: C [n] = 2 5 − 1 c[n − 1] = (2 5 − 1) 2 C [n − 2] =.. = (2 5 − 1) n c[n] = \frac {2}{\ SQRT 5-1} c[n-1] = (\frac {2}{ \sqrt 5 – 1})^2 c[n-2] = … = (\ frac {2} {} \ SQRT 5-1) ^ n c [n] = 5-12 [n – 1) = c (5-12) [n – 2) = 2 c… N = (5-12)
  • So, the time complexity of the recursive solution of the Fibonacci sequence is: O((2 5 −1)n) O((\frac {2}{\ SQRT 5-1})^n) O((5 −12)n)