Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Solving Recursion in fibonacci numbers

I'm unaware of the mathematics in this algorithm and could use some help.

Algorithm:

if n<2 then

  return n

else return fibonacci(n-1) + fibonacci(n-2)

Statement

n < 2 is O(1)
Time n >=2 is O(1)
Return n is O(1) Time n>=2 is - Return fib(n-1) + fib(n-2) is -

and time n>=2 is T(n-1) + T(n-2) +O(1)

Total: O(1) T(n-1) + T(n-2) + O(1)
T(n) = O(1) if n < 2
T(n) = T(n-1) + T(n-2) + O(1) if n>=2

like image 902
noviceneedshelp Avatar asked Mar 18 '26 14:03

noviceneedshelp


1 Answers

I think you're supposed to notice that the recurrence relation for this function is awfully familiar. You can learn exactly how fast this awfully familiar recurrence grows by looking it up by name.

However, if you fail to make the intuitive leap, you can try to bound the runtime using a simplified problem. Essentially, you modify the algorithm in ways guaranteed to increase the runtime while making it simpler. Then you figure out the runtime of the new algorithm, which gives you an upper bound.

For example, this algorithm must take longer and is simpler to analyze:

F(n): if n<2 then return n else return F(n-1) + F(n-1)
like image 77
Craig Gidney Avatar answered Mar 20 '26 18:03

Craig Gidney



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!