13

Using the following recursive Fibonacci algorithm:

def fib(n):
   if n==0:
      return 0
   elif n==1
      return 1
   return (fib(n-1)+fib(n-2))

If I input the number 5 to find fib(5), I know this will output 5 but how do I examine the complexity of this algorithm? How do I calculate the steps involved?

Jake
  • 3,810
  • 21
  • 35
joker
  • 469
  • 1
  • 5
  • 15

4 Answers4

23

Most of the times, you can represent the recursive algorithms using recursive equations. In this case the recursive equation for this algorithm is $T(n) = T(n-1) + T(n-2) + \Theta(1)$. Then you can find the closed form of the equation using the substitution method or the expansion method (or any other method used to solve recurrences). In this case you get $T(n) = \Theta(\phi^n)$, where $\phi$ is the golden ratio ($\phi = \frac{(1 + \sqrt{5})}{2}$).

If you want to find out more about how to solve recurrences I strongly recommend you to read chapter 4 of Introduction to Algorithms.

David Richerby
  • 82,470
  • 26
  • 145
  • 239
ees_cu
  • 346
  • 4
  • 7
0

as an alternative to recurrence relations/ math analysis (but not a substitute) a simple empirical exercise, apparently not taught very often in classes but very informative, is to count the # of executions of the function, and then graph the count for a range of small n inputs, and then curve fit the result. results will generally closely match the theoretical math approach.

good supporting material for this exercise can be found in the free online chapter 3, running time of algorithms/ Foundations of Computer Science, Ullman

vzn
  • 11,162
  • 1
  • 28
  • 52
0

The result of fib (n) is the sum of all recursive calls that returned 1. Therefore there are exactly fib (n) recursive calls evaluating fib (1). So the execution time is Ω(fib (n)); you'd need to show that the calls returning 0 and the other recursive calls don't add significantly to this.

The same reasoning would apply to any recursively defined function that either returns 1, or 0, or the result of another recursive call.

gnasher729
  • 32,238
  • 36
  • 56
0

A lower bound is intuitive: $T(n)=T(n-1)+T(n-2)$ $T(n)>2T(n-2)$ since $T(n-1)>T(n-2)$ Hence $T(n)= \Omega(c^{n})$

wabbit
  • 170
  • 1
  • 8