MHB Memoized Dynamic Programming algorithm

Click For Summary
The discussion clarifies the use of a memoization technique in a dynamic programming algorithm for calculating Fibonacci numbers. It emphasizes that the 'memo' structure is not a set but an associative array, allowing for efficient retrieval of previously computed Fibonacci values by their position. This distinction is crucial since sets do not maintain order and cannot provide a specific Fibonacci number based on its index. The conversation highlights the importance of using a map or dictionary for effective memoization in dynamic programming. Understanding this concept is essential for implementing efficient algorithms.
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)
 
Anthropic announced that an inflection point has been reached where the LLM tools are good enough to help or hinder cybersecurity folks. In the most recent case in September 2025, state hackers used Claude in Agentic mode to break into 30+ high-profile companies, of which 17 or so were actually breached before Anthropic shut it down. They mentioned that Clause hallucinated and told the hackers it was more successful than it was...

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
1K
  • · Replies 36 ·
2
Replies
36
Views
3K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K