Open In App

Time complexity of recursive Fibonacci program

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Share
Report issue
Report

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-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(2n) 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 x2 = x+x2-x-1 = 0.
  • Solving this by quadratic formula we can get the roots as x = (1+√5)/2 and x = (1-√5)/2 .
  • Now we know that solution of a linear recursive function is given as F(n)

F(n) = (α1)n + (α2)n where α1 and α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+√5)/2 )n + ( (1-√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+√5)/2 )n + ( (1-√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+√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


Last Updated : 22 Feb, 2024
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads