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?
You model the time function to calculate
Fib(n)as sum of time to calculateFib(n-1)plus the time to calculateFib(n-2)plus the time to add them together (O(1)). This is assuming that repeated evaluations of the sameFib(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
nand intuitively figure out that this function is asymptoticallyO(2n). You can then prove your conjecture by induction.Base:
n = 1is obviousAssume
T(n-1) = O(2n-1), thereforeT(n) = T(n-1) + T(n-2) + O(1)which is equal toT(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 asf(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 toFib(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.