MHB Memoized Dynamic Programming algorithm

AI Thread 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)
 
Thread 'Star maps using Blender'
Blender just recently dropped a new version, 4.5(with 5.0 on the horizon), and within it was a new feature for which I immediately thought of a use for. The new feature was a .csv importer for Geometry nodes. Geometry nodes are a method of modelling that uses a node tree to create 3D models which offers more flexibility than straight modeling does. The .csv importer node allows you to bring in a .csv file and use the data in it to control aspects of your model. So for example, if you...
I tried a web search "the loss of programming ", and found an article saying that all aspects of writing, developing, and testing software programs will one day all be handled through artificial intelligence. One must wonder then, who is responsible. WHO is responsible for any problems, bugs, deficiencies, or whatever malfunctions which the programs make their users endure? Things may work wrong however the "wrong" happens. AI needs to fix the problems for the users. Any way to...

Similar threads

Back
Top