### Fibonacci序列的计算复杂性

``````int Fibonacci(int n)
{
if (n <= 1)
return n;
else
return Fibonacci(n - 1) + Fibonacci(n - 2);
}
``````

Fibonacci序列的计算复杂度是多少以及如何计算？

#### 13 个答案:

`T(n<=1) = O(1)`

`T(n) = T(n-1) + T(n-2) + O(1)`

`)` 等于

`T(n) = T(n-1) + T(n-2) + O(1)` `T(n) = O(2` `n-1` `) + O(2` `n-2` `) + O(1) = O(2`功能 `n`

`Fib(n)`

a n == a （n-1） + a （n-2）

a 2 == a + 1

specific problem over at MIT进行了非常好的讨论。在第5页，他们指出，如果你假设一个加法需要一个计算单位，那么计算Fib（N）所需的时间与Fib（N）的结果密切相关。

``````Fib(N) = (1/sqrt(5)) * 1.618^(N+1) (approximately)
``````

``````O((1/sqrt(5)) * 1.618^(N+1)) = O(1.618^(N+1))
``````

PS：如果你想了解更多信息，可以在维基百科上讨论closed form expression of the Nth Fibonacci number

``````n              *
n-1            **
n-2           ****
...
2           ***********
1       ******************
0    ***************************
``````

``````F(6)                 *  <-- only once
F(5)                 *  <-- only once too
F(4)                 **
F(3)                ****
F(2)              ********
F(1)          ****************           <-- 16
F(0)  ********************************    <-- 32
``````

``````O( F(6) ) = O(2^6)
O( F(n) ) = O(2^n)
``````

``````     T(n) = T(n-1) + T(n-2) <
T(n-1) + T(n-1)

= 2*T(n-1)
= 2*2*T(n-2)
= 2*2*2*T(n-3)
....
= 2^i*T(n-i)
...
==> O(2^n)
``````

``````T(n) = Ω(2^(n/2))  (lower bound)
T(n) = O(2^n)   (upper bound)
T(n) = Θ(Fib(n)) (tight bound)
``````

``````IN | OUT | TOT | LEAF | INT
1 |   1 |   1 |   1  |   0
2 |   1 |   1 |   1  |   0
3 |   2 |   3 |   2  |   1
4 |   3 |   5 |   3  |   2
5 |   5 |   9 |   5  |   4
6 |   8 |  15 |   8  |   7
7 |  13 |  25 |  13  |  12
8 |  21 |  41 |  21  |  20
9 |  34 |  67 |  34  |  33
10 |  55 | 109 |  55  |  54
``````

``````          n
(n-1)      (n-2)
(n-2)(n-3) (n-3)(n-4) ...so on
``````

``````i
0                        n
1            (n-1)                 (n-2)
2        (n-2)    (n-3)      (n-3)     (n-4)
3   (n-3)(n-4) (n-4)(n-5) (n-4)(n-5) (n-5)(n-6)
``````

``````2^0=1                        n
2^1=2            (n-1)                 (n-2)
2^2=4        (n-2)    (n-3)      (n-3)     (n-4)
2^3=8   (n-3)(n-4) (n-4)(n-5) (n-4)(n-5) (n-5)(n-6)    ..so on
2^i for ith level
``````

``````i work
1 2^1
2 2^2
3 2^3..so on
``````

``````1+2+4+.......(n-1)
= 1((2^n)-1)/(2-1)
=2^n -1
``````

`2^n [ O(2^n) ]`的顺序。

F（n）取决于F（n-1）和F（n-2）

F（n-1）再次取决于F（n-2）和F（n-3）

F（n-2）再次取决于F（n-3）和F（n-4）

T（n）= T（n-1）+ T（n-2）+ C，C常数

T（n-1）= T（n-2）+ T（n-3）> T（n-2）然后

T（n）> 2 * T（N-2）

...

T（n）> 2 ^（n / 2）* T（1）= O（2 ^（n / 2））

``````static int fib(int n)
{
/* memory */
int f[] = new int[n+1];
int i;

/* Init */
f[0] = 0;
f[1] = 1;

/* Fill */
for (i = 2; i <= n; i++)
{
f[i] = f[i-1] + f[i-2];
}

return f[n];
}
``````

``````m = log2(n) // your real input size
``````

``````m = log2(n)
2^m = 2^log2(n) = n
``````

``````T(m) = n steps = 2^m steps
``````

``````2 (2 -> 1, 0)

4 (3 -> 2, 1) (2 -> 1, 0)

8 (4 -> 3, 2) (3 -> 2, 1) (2 -> 1, 0)
(2 -> 1, 0)

14 (5 -> 4, 3) (4 -> 3, 2) (3 -> 2, 1) (2 -> 1, 0)
(2 -> 1, 0)

(3 -> 2, 1) (2 -> 1, 0)

22 (6 -> 5, 4)
(5 -> 4, 3) (4 -> 3, 2) (3 -> 2, 1) (2 -> 1, 0)
(2 -> 1, 0)

(3 -> 2, 1) (2 -> 1, 0)

(4 -> 3, 2) (3 -> 2, 1) (2 -> 1, 0)
(2 -> 1, 0)
``````

``````unsigned int Fib(unsigned int n)
{
// first Fibonaci number is Fib(0)
// second one is Fib(1) and so on

// unsigned int m;  // m + current_n = original_n
unsigned int a = 1; // Fib(m)
unsigned int b = 0; // Fib(m-1)
unsigned int c = 0; // Fib(m-2)

while (n--)
{
c = b;
b = a;
a = b+c;
}

return a;
}
``````