11 Answers: Time-Complexitybig-Ocomplexity-Theoryfibonacci Edited Aug 10 '17 at 15:43
11 Answers: Time-Complexitybig-Ocomplexity-Theoryfibonacci Edited Aug 10 '17 at 15:43
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?
time-complexitybig-ocomplexity-theoryfibonacci
share improve this question follow
edited Aug 10 '17 at 15:43
Tony Tannous
10.4k66 gold badges3232 silver badges6666 bronze badges
asked Dec 11 '08 at 20:20
Juliet
75.3k4343 gold badges190190 silver badges224224 bronze badges
7
Good answers here. – Greg Dec 11 '08 at 20:26
3
See the matrix form section here: en.wikipedia.org/wiki/Fibonacci_number . by doing this matrix ^ n (in a
clever way) you can compute Fib(n) in O(lg n). The trick is in doing the power function. Theres a very
good lecture on iTunesU about this exact problem and how to solve in O(lg n). The course is intro to
algorithms from MIT lecture 3 (its absolutley free so check it out if you're interested) – Aly Feb 11 '11 at
16:50
1
Neither of the above comments address the question, which is about the computational complexity of
the naive version (in posted code), not about smarter versions like matrix form or non-recursive
computation. – Josh Milthorpe May 2 '18 at 5:21
A very nice video here which talks about both lower bound complexity(2^n/2) and upper bound
complexity(2^n) of recursive implementation. – RBT Jan 29 '19 at 0:08
1
A side-note query: Should the naive implementation of Fibonacci series
be iterative or recursive? – RBT Jan 29 '19 at 0:12
add a comment
11 Answers
Active OldestVotes
386
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 use.
T(n<=1) = 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.
Assume T(n-1) = O(2n-1), therefore
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
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.
share improve this answer follow
edited Jan 30 '19 at 8:48
community wiki
a2 == a + 1