Time complexity of recursive Fibonacci program

The Fibonacci numbers are the numbers in the following integer sequence 0, 1, 1, 2, 3, 5, 8, 13…
Mathematically Fibonacci numbers can be written by the following recursive formula.

For seed values F(0) = 0 and F(1) = 1
F(n) = F(n-1) + F(n-2)

Before proceeding with this article make sure you are familiar with the recursive approach discussed in Program for Fibonacci numbers

Analysis of the recursive Fibonacci program:
We know that the recursive equation for Fibonacci is T(n)=T(n-1)+T(n-2)+O(1).
What this means is, the time taken to calculate fib(n) is equal to the sum of time taken to calculate fib(n-1) and fib(n-2). This also includes the constant time to perform the previous addition.



On solving the above recursive equation we get the upper bound of Fibonacci as O(2^n) but this is not the tight upper bound. The fact that Fibonacci can be mathematically represented as a linear recursive function can be used to find the tight upper bound.
Now Fibonacci is defined as

F(n) = F(n-1)+F(n-2)

The characteristic equation for this function will be
x^2 = x+1
x^2x1 = 0

Solving this by quadratic formula we can get the roots as
x = (1+\sqrt{5})/2 and x=(1\sqrt{5})/2

Now we know that solution of a linear recursive function is given as
F(n) = ($\alpha_1)^n + ($\alpha_2)^n

where $\alpha_1 and $\alpha_2 are the roots of the characteristic equation.
So for our Fibonacci function F(n) = F(n-1)+F(n-2) the solution will be

F(n) = ((1+$\sqrt{5})/2)^n+((1-\sqrt{5})/2)^n
Clearly T(n) and F(n) are asymptotically the same as both functions are representing the same thing.
Hence it can be said that
T(n) = O(((1+$\sqrt{5})/2)^n+((1-\sqrt{5})/2)^n)
or we can write below (using the property of Big O notation that we can drop lower order terms)
T(n) = O(((1+$\sqrt{5})/2)^n
T(n) = O(1.6180)^n
This is the tight upper bound of fibonacci.\

Fun Fact:
1.6180 is also called the golden ratio. You can read more about golden ratio here: Golden Ratio in Maths

This article is contributed by Vineet Joshi. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.



My Personal Notes arrow_drop_up


Article Tags :
Practice Tags :


3


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.