0% found this document useful (0 votes)
45 views

11 Answers: Time-Complexitybig-Ocomplexity-Theoryfibonacci Edited Aug 10 '17 at 15:43

The computational complexity of the naive recursive Fibonacci sequence is exponential, or O(φ^n), where φ is the golden ratio (approximately 1.618). This is because the number of operations needed to calculate each term F(n) is equal to the number needed for F(n-1) plus F(n-2), matching the exponential growth of the golden ratio. While an initial analysis may estimate O(2^n) time, more precise analysis shows the complexity matches the exponential growth of the Fibonacci numbers themselves.

Uploaded by

minal
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
45 views

11 Answers: Time-Complexitybig-Ocomplexity-Theoryfibonacci Edited Aug 10 '17 at 15:43

The computational complexity of the naive recursive Fibonacci sequence is exponential, or O(φ^n), where φ is the golden ratio (approximately 1.618). This is because the number of operations needed to calculate each term F(n) is equal to the number needed for F(n-1) plus F(n-2), matching the exponential growth of the golden ratio. While an initial analysis may estimate O(2^n) time, more precise analysis shows the complexity matches the exponential growth of the Fibonacci numbers themselves.

Uploaded by

minal
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

Asked 11 years, 10 months ago

Active 9 months ago


Viewed 317k times
337
183
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?
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)

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.
share  improve this answer  follow 
edited Jan 30 '19 at 8:48
community wiki

11 revs, 2 users 95%


Mehrdad Afshari
 Itake your StackOverflowException as a joke. The exponential time is perceivable quite easily
with rather small values for n. – David Rodríguez - dribeas Dec 11 '08 at 23:08
 @MehrdadAfshari can you explain why u take T(n-1) = O(2^n-1). T(n-1) should be (n^2),
because Fibonacci have calls T(n-1)+T(n-2) so after summing all the cost (2*2*2....) should be
2^n. – Devendra Jul 17 '14 at 6:22 
 Is the proof by induction really correct? It seems like you could equally just assume T(n) =
O(n) and then you would have T(n) = O(n-1) + O(n-2) + O(1) = O(n) so T(n) = O(n) but that is
obviously not true? If it is correct someone please explain.. – Richard Fung Apr 28 '16 at 5:33
 @RichardFung The logic used here is not precise, the induction hypothesis is too weak,
because it already includes the big-O inside it. The correct hypothesis is to say that T(n) <= c*2^n for
some fixed c, and then from the conclusion of the inductive proof, you can infer that T(n) =
O(2^n) – amnn Jul 3 '16 at 10:36
 1
"Alternatively, you can draw the recursion tree, which will have depth n and intuitively figure out that
this function is asymptotically O(2n)." - This is completely false. The time complexity is
O(golden_ratio^n). It never comes close to O(2^n). If you could reach out toward infinity it would get
close to O(golden_ratio^n). That is what an asymptote is, the distance between the two lines must
approach 0. – bob Dec 20 '19 at 22:32 
show 1 more comment
139
Just ask yourself how many statements need to execute for F(n) to complete.

For F(1), the answer is 1 (the first part of the conditional).

For F(n), the answer is F(n-1) + F(n-2).

So what function satisfies these rules? Try an (a > 1):

an == a(n-1) + a(n-2)

Divide through by a(n-2):

a2 == a + 1

Solve for a and you get (1+sqrt(5))/2 = 1.6180339887, otherwise known as the golden ratio.

So it takes exponential time.

You might also like