Dynamic Programming: Memo and Table

Fibonacci without the waste: remember answers in a dict (top down), or fill a table from the bottom up.

Python · intermediate · 9 min · 53 steps · free

What you'll learn

fibonacci_dp.py

def fib_memo(n, memo):
    if n in memo:
        return memo[n]
    if n <= 1:
        return n
    memo[n] = fib_memo(n - 1, memo) + fib_memo(n - 2, memo)
    return memo[n]

def fib_table(n):
    table = [0, 1]
    for i in range(2, n + 1):
        table.append(table[i - 1] + table[i - 2])
    return table[n]

print(fib_memo(5, {}), fib_table(5))

Step by step

  1. Python defines the function fib_memo(). Its body does not run yet: it runs each time the function is called.
  2. Python defines the function fib_table(). Its body does not run yet: it runs each time the function is called.
  3. Python calls fib_memo(), with n = 5 and memo = {}. It gets its own names.
  4. n in memo is false, so the block under it is skipped.
  5. n <= 1 is false, so the block under it is skipped.
  6. fib(5) needs fib(4) and fib(3). Without a memo, fib(3) would be worked out twice, fib(2) three times…
  7. n in memo is false, so the block under it is skipped.
  8. n <= 1 is false, so the block under it is skipped.
  9. Python calls fib_memo() #3, with n = 3 and memo = {}. It gets its own names.
  10. n in memo is false, so the block under it is skipped.
  11. n <= 1 is false, so the block under it is skipped.
  12. Python calls fib_memo() #4, with n = 2 and memo = {}. It gets its own names.
  13. n in memo is false, so the block under it is skipped.
  14. n <= 1 is false, so the block under it is skipped.
  15. Python calls fib_memo() #5, with n = 1 and memo = {}. It gets its own names.
  16. n in memo is false, so the block under it is skipped.
  17. n <= 1 is true, so the block under it runs.
  18. The function returns n.
  19. fib_memo() #5 has finished. Python calls fib_memo() #5, with n = 0 and memo = {}. It gets its own names.
  20. n in memo is false, so the block under it is skipped.
  21. n <= 1 is true, so the block under it runs.
  22. The function returns n.
  23. fib(2) = 1 is worked out and saved in memo.
  24. The function returns memo[n].
  25. fib_memo() #4 has finished. Python calls fib_memo() #4, with n = 1 and memo = {2: 1}. It gets its own names.
  26. n in memo is false, so the block under it is skipped.
  27. n <= 1 is true, so the block under it runs.
  28. The function returns n.
  29. fib_memo() #4 has finished, so its names are gone. A new key 3 with the value 2 is added to the dict that memo points to.
  30. The function returns memo[n].
  31. fib_memo() #3 has finished. Python calls fib_memo() #3, with n = 2 and memo = {2: 1, 3: 2}. It gets its own names.
  32. fib(2) is already in memo: return it straight away, no more recursion.
  33. The function returns memo[n].
  34. fib_memo() #3 has finished, so its names are gone. A new key 4 with the value 3 is added to the dict that memo points to.
  35. The function returns memo[n].
  36. fib_memo() #2 has finished. Python calls fib_memo() #2, with n = 3 and memo = {2: 1, 3: 2, 4: 3}. It gets its own names.
  37. n in memo is true, so the block under it runs.
  38. The function returns memo[n].
  39. fib_memo() #2 has finished, so its names are gone. A new key 5 with the value 5 is added to the dict that memo points to.
  40. The function returns memo[n].
  41. Python calls fib_table(), with n = 5. It gets its own names. fib_memo() has finished, so its names are gone.
  42. Bottom-up: start from the two known answers, 0 and 1.
  43. i is created with the value 2.
  44. Each new value is the sum of the previous two: no recursion at all.
  45. i changes from 2 to 3.
  46. 2 is added to the end of the list that table points to.
  47. i changes from 3 to 4.
  48. 3 is added to the end of the list that table points to.
  49. i changes from 4 to 5.
  50. 5 is added to the end of the list that table points to.
  51. Python moves to the next item of the loop: for i in range(2, n + 1).
  52. The function returns table[n].
  53. Both give 5.

Common mistakes

Best practices

Key takeaways

Check yourself

Plain recursive fib(30) makes about?

Answer: 1.6 million calls

Which needs no recursion?

Answer: The table

·

More in Backtracking and Dynamic Programming