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)
 
Dear Peeps I have posted a few questions about programing on this sectio of the PF forum. I want to ask you veterans how you folks learn program in assembly and about computer architecture for the x86 family. In addition to finish learning C, I am also reading the book From bits to Gates to C and Beyond. In the book, it uses the mini LC3 assembly language. I also have books on assembly programming and computer architecture. The few famous ones i have are Computer Organization and...
I had a Microsoft Technical interview this past Friday, the question I was asked was this : How do you find the middle value for a dataset that is too big to fit in RAM? I was not able to figure this out during the interview, but I have been look in this all weekend and I read something online that said it can be done at O(N) using something called the counting sort histogram algorithm ( I did not learn that in my advanced data structures and algorithms class). I have watched some youtube...

Similar threads

Back
Top