The complexity of a naive recursive fibonacci is indeed 2โฟ.
T(n) = T(n-1) + T(n-2) = T(n-2) + T(n-3) + T(n-3) + T(n-4) =
= T(n-3) + T(n-4) + T(n-4) + T(n-5) + T(n-4) + T(n-5) + T(n-5) + T(n-6) = ...
In each step you call T twice, thus will provide eventual asymptotic barrier of:
T(n) = 2โ
2โ
...โ
2 = 2โฟ
bonus: The best theoretical implementation to fibonacci is actually a close formula, using the golden ratio:
Fib(n) = (ฯโฟ โ (โฯ)โปโฟ)/sqrt(5) [where ฯ is the golden ratio]
(However, it suffers from precision errors in real life due to floating point arithmetics, which are not exact)
Answer from amit on Stack OverflowSo, of course, the typical version of the recursive fibonacci suquence looks like:
function fib(n):
if n < 2:
return n
return fib(n - 1) + fib(n - 2);And its complexity is O( 2n )
Now, in this instance Cache[] is an array with n elements. Since the base cases are known, we can say Cache[0] = 0 and Cache[1] = 1. The rest of the Cache array is set equal to some placeholder value, say -1 in this instance, so we get:
Cache[0,1,2,...,n] = [0,1,-1,...,-1]
function fib(n):
if n < 2:
return n
n_1 = Cache[n - 1]
if n_1 == -1:
n_1 = fib(n - 1, Cache)
Cache[n - 1] = n_1
n_2 = Cache[n - 2]
return n_1 + n_2
Now, I get to thinking, the complexity of this would be O(n) because the base case is O(1), the only branch that is ever calculated is the initial n - 1 branch, and by that time ever n - 2 branch has already been calculated, going back to the base case. Is my thinking correct here?
This isn't homework or anything. Just a thought experiment.
algorithm - Why is the complexity of computing the Fibonacci series 2^n and not n^2? - Stack Overflow
What is the expression for time complexity for finding Fibonacci number using recursion?
a. T ( n ) = T ( n - 1 ) + O ( n )
b. T ( n ) = T ( n - 1 ) + T ( n / 2 )
c. T ( n ) = 2 ^ { * } T ( n / 2 ) + O ( 1 )
d. T ( n ) = T ( n - 1 ) + T ( n - 2 )
Can Fibonacci numbers be calculated using recursion in O(N) without memo?
Time Complexity of Recursive Functions
The complexity of a naive recursive fibonacci is indeed 2โฟ.
T(n) = T(n-1) + T(n-2) = T(n-2) + T(n-3) + T(n-3) + T(n-4) =
= T(n-3) + T(n-4) + T(n-4) + T(n-5) + T(n-4) + T(n-5) + T(n-5) + T(n-6) = ...
In each step you call T twice, thus will provide eventual asymptotic barrier of:
T(n) = 2โ
2โ
...โ
2 = 2โฟ
bonus: The best theoretical implementation to fibonacci is actually a close formula, using the golden ratio:
Fib(n) = (ฯโฟ โ (โฯ)โปโฟ)/sqrt(5) [where ฯ is the golden ratio]
(However, it suffers from precision errors in real life due to floating point arithmetics, which are not exact)
The recursion tree for fib(n) would be something like :
n
/ \
n-1 n-2 --------- maximum 2^1 additions
/ \ / \
n-2 n-3 n-3 n-4 -------- maximum 2^2 additions
/ \
n-3 n-4 -------- maximum 2^3 additions
........
-------- maximum 2^(n-1) additions
- Using n-1 in 2^(n-1) since for fib(5) we will eventually go down to fib(1)
- Number of internal nodes = Number of leaves - 1 = 2^(n-1) - 1
- Number of additions = Number of internal nodes + Number of leaves = (2^1 + 2^2 + 2^3 + ...) + 2^(n-1)
- We can replace the number of internal nodes to 2^(n-1) - 1 because it will always be less than this value : = 2^(n-1) - 1 + 2^(n-1) ~ 2^n