1、
The Fibonacci number sequence {F_N} is defined as: F_0=0, F_1=1, F_N=F_N−1+F_N−2, N=2, 3, .... The time complexity of the function which calculates F_N recursively is Θ(N!).
T()
F()
说实话我还没法一眼看出来……姑且写一下这个函数:
#include <stdio.h>
int Fib (int n);
int main () {
int n;
scanf ("%d", &n);
printf ("%d", Fib (n));
return 0;
}
int Fib (int n) {
if (n == 0)
return 0;
else if (n == 1)
return 1;
else
return Fib (n - 1) + Fib (n - 2);
}
分析该函数可知时间复杂度为 O(2^N),分析过程如下:
Fib(N) = Fib(N-1) + Fib(N-2) = [ Fib(N-2) + Fib(N-3) ] + [ Fib(N-3) + Fib(N-4) ] = ... ...
可见括号内参数每减1,整体项数就会乘2,故当出现Fib(0)时,共有2^N项
故答案为F
PS:该函数的空间复杂度为O(N),因为有N层
2、
Let n be a non-negative integer representing the size of input. The time complexity of the following piece of code is:

文章探讨了递归计算Fibonacci数列的时间复杂度(O(2^N)),并分析了两个程序P1和P2的时间复杂性,P1为O(logN),P2为O(N)。同时提及了空间复杂度的相关讨论。
最低0.47元/天 解锁文章
346

被折叠的 条评论
为什么被折叠?



