Memoized Dynamic Programming algorithm

Click For Summary
SUMMARY

The discussion centers on the implementation of a Memoized Dynamic Programming algorithm for calculating Fibonacci numbers. The key point clarified is that the variable memo is an associative array (or dictionary) rather than a set, allowing for efficient lookups of Fibonacci numbers by their position n. The algorithm checks if n exists in memo to avoid redundant calculations, ensuring optimal performance by storing previously computed values.

PREREQUISITES
  • Understanding of Dynamic Programming concepts
  • Familiarity with associative arrays (dictionaries) in programming
  • Basic knowledge of Fibonacci sequence and its properties
  • Experience with a programming language that supports memoization (e.g., Python, JavaScript)
NEXT STEPS
  • Implement a Memoized Fibonacci algorithm in Python
  • Explore the differences between memoization and tabulation in Dynamic Programming
  • Learn about time complexity analysis of recursive algorithms
  • Investigate other applications of memoization beyond Fibonacci calculations
USEFUL FOR

Software developers, computer science students, and anyone interested in optimizing recursive algorithms using memoization techniques.

evinda
Gold Member
MHB
Messages
3,741
Reaction score
0
Hello! (Wave)

I am looking at the following example of Memoized Dynamic Programming algorithm.

Code:
memo={}
Fib(n):
  if n in memo return memo[n]
  if n<=2 F[n]=1
  else F[n]=F[n-1]+F[n-2]
  memo[n]=F[n]
  return F[n]

Could you explain me why we check if $n$ is in memo although memo is a set that contains the values of $F[n]$ and not of $n$? (Thinking)
 
Technology news on Phys.org
memo is not a set. It would not be very useful to have a set that contains various Fibonacci numbers, since there would be no way to look up a Fibonacci number by its position in the Fibonacci sequence (remember sets have no notion of order; if I give you a set that contains the first 100 Fibonacci numbers and then ask you to give me the 71st Fibonacci number, the set doesn't help a whole lot).

Here memo is an associative array (also known as a map or a dictionary) which associates any integer $n$ to the corresponding Fibonacci number $F_n$, if it has been computed so far (if not, then $n$ is not inside the array and memo[n] is undefined).
 
Last edited:
Bacterius said:
memo is not a set. It would not be very useful to have a set that contains various Fibonacci numbers, since there would be no way to look up a Fibonacci number by its position in the Fibonacci sequence (remember sets have no notion of order; if I give you a set that contains the first 100 Fibonacci numbers and then ask you to give me the 71st Fibonacci number, the set doesn't help a whole lot).

Here memo is an associative array (also known as a map or a dictionary) which associates any integer $n$ to the corresponding Fibonacci number $F_n$, if it has been computed so far (if not, then $n$ is not inside the array and memo[n] is undefined).

I see... Thank you! (Smirk)
 

Similar threads

Replies
9
Views
3K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 17 ·
Replies
17
Views
4K
  • · Replies 7 ·
Replies
7
Views
5K
  • · Replies 89 ·
3
Replies
89
Views
6K
  • · Replies 17 ·
Replies
17
Views
2K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 36 ·
2
Replies
36
Views
3K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K