Table of Contents
Fibonacci
1, 1, 2, 3, 5, 8, …
fib(n)
fib(3) = 2
fib(5) = 5
fib(6) = 8
...
Recursion
# Recursion
def fib(n):
if n == 1 or n ==2:
result = 1
else:
result = fib(n-1) + fib(n-2)
return result
Pros: simple to write and simple to understand
Cons: too slow, due to high computation cost
Store (Memoize)
# Memoized Solution
def fib_2(n, memo):
if memo[n] is not None:
return memo[n]
if n == 1 or n == 2:
result = 1
else:
result = fib_2(n-1, memo) + fib_2(n-2, memo)
memo[n] = result
return result
def fib_memo(n):
memo = [None] * (n + 1)
return fib_2(n, memo)
Pros: runs fast
Cons: recursion depth limitation:
fib_memo(1000)
---------------------------------------------------------------------------
RecursionError Traceback (most recent call last)
<ipython-input-9-46b166948401> in <module>()
----> 1 fib_memo(1000)
2 frames
... last 1 frames repeated, from the frame below ...
<ipython-input-5-4df1e59f88e4> in fib_2(n, memo)
6 result = 1
7 else:
----> 8 result = fib_2(n-1, memo) + fib_2(n-2, memo)
9 memo[n] = result
10 return result
RecursionError: maximum recursion depth exceeded in comparison
Bottom-up
# Bottom-up
def fib_bottom_up(n):
if n == 1 or n ==2:
return 1
bottom_up = [None] * (n+1)
bottom_up[1] = 1
bottom_up[2] = 1
for i in range(3, n+1):
bottom_up[i] = bottom_up[i-1] + bottom_up[i-2]
return bottom_up[n]
Pros: fast, and easy to understand
Cons: none
fib_bottom_up(1000)
43466557686937456435688527675040625802564660517371780402481729089536555417949051890403879840079255169295922593080322634775209689623239873322471161642996440906533187938298969649928516003704476137795166849228875