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?
Hence the time taken by recursive Fibonacci is O(2^n) or exponential.
My analysis: Time Complexity: O(n 2n) - since for all n values the Fibonacci is calculated (calculating nth Fibonacci is 2n) Space Complexity: O(1) - recursive call are added to stack but then once the execution is completed the value are delete from stack.
question is there something .... to reduce the time complexity (?) Yes, use a loop. Each clarification of Fibonacci(n) is O(n) complexity. If desired, amend code to simply print Fibonacci(i) at each iteration.
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 used.
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(2
n
)
. You can then prove your conjecture by induction.
Base: n = 1
is obvious
Assume T(n-1) = O(2
n-1
)
, therefore
T(n) = T(n-1) + T(n-2) + O(1)
which is equal to
T(n) = O(2
n-1
) + O(2
n-2
) + O(1) = O(2
n
)
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.6
n
)
). You can find out this tight bound by using generating functions as I'd mentioned above.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With