Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Computational complexity of Fibonacci Sequence

I understand Big-O notation, but I don't know how to calculate it for many functions. In particular, I've been trying to figure out the computational complexity of the naive version of the Fibonacci sequence:

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

What is the computational complexity of the Fibonacci sequence and how is it calculated?

like image 411
Juliet Avatar asked Dec 11 '08 20:12

Juliet


People also ask

What is the big O of Fibonacci recursive?

Hence the time taken by recursive Fibonacci is O(2^n) or exponential.

What is the time complexity and space complexity for computing nth Fibonacci number using recursion?

My analysis: Time Complexity: O(n 2n) - since for all n values the Fibonacci is calculated (calculating nth Fibonacci is 2n) Space Complexity: O(1) - recursive call are added to stack but then once the execution is completed the value are delete from stack.

How can we reduce the complexity of Fibonacci sequence?

question is there something .... to reduce the time complexity (?) Yes, use a loop. Each clarification of Fibonacci(n) is O(n) complexity. If desired, amend code to simply print Fibonacci(i) at each iteration.


1 Answers

You model the time function to calculate Fib(n) as sum of time to calculate Fib(n-1) plus the time to calculate Fib(n-2) plus the time to add them together (O(1)). This is assuming that repeated evaluations of the same Fib(n) take the same time - i.e. no memoization is used.

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

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

You solve this recurrence relation (using generating functions, for instance) and you'll end up with the answer.

Alternatively, you can draw the recursion tree, which will have depth n and intuitively figure out that this function is asymptotically O(2n). You can then prove your conjecture by induction.

Base: n = 1 is obvious

Assume T(n-1) = O(2n-1), therefore

T(n) = T(n-1) + T(n-2) + O(1) which is equal to

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

However, as noted in a comment, this is not the tight bound. An interesting fact about this function is that the T(n) is asymptotically the same as the value of Fib(n) since both are defined as

f(n) = f(n-1) + f(n-2).

The leaves of the recursion tree will always return 1. The value of Fib(n) is sum of all values returned by the leaves in the recursion tree which is equal to the count of leaves. Since each leaf will take O(1) to compute, T(n) is equal to Fib(n) x O(1). Consequently, the tight bound for this function is the Fibonacci sequence itself (~θ(1.6n)). You can find out this tight bound by using generating functions as I'd mentioned above.

like image 63
12 revs, 3 users 93% Avatar answered Oct 07 '22 01:10

12 revs, 3 users 93%