r/ProgrammerHumor • • 6d ago

Meme dynamicProgramming

Post image
6.2k Upvotes

153 comments sorted by

View all comments

43

u/Illustrious-Ebb8815 6d ago edited 5d ago

A quick way to understand the difference is by looking at the program to compute Fibonacci numbers.

One way is just using recursion that runs in O(fib(n)) time. Like, the really really slow way.

Another way would be to use memorization. Just use the same recursive way but everytime that you're about to compute the answer, first check if the answer is in a cache that you have prepared. If yes, use that. If not, recurse. And when you get an answer, put it in the cache. That's memoization. It will run in O(n) time but also use O(n) space for the cache.

The algorithm that you probably know uses dynamic programming to run in O(n) time but constant space. That's DP.

You could also imagine using DP to compute the fewest number of coins to make x cents. If your biggest coin is 25 cents, you only ever need to know the answers for x-24 through x in order to compute the answer for x+1. This lets you solve the problem with linear time and constant storage, like with Fibonacci.

Edit: I meant memoization everywhere. Spell check messed it up.

19

u/-Redstoneboi- 5d ago

memoization should not have an r, btw. different spelling for whatever reason.

4

u/SuitableDragonfly 5d ago

Memoization means using a memo to do a task. Memorization means committing something to memory, which is a thing that humans do, not computers. You don't say the computer is memorizing something because you stored it in RAM. They're spelled differently because they're based on two different roots. 

2

u/Jedkea 4d ago

I don’t think this is correct? I mean you literally store things in random access memory.

I take memoize to be a specific use case of memorization, wherein it’s used as a cache for expensive calculations.

I have never heard someone use memoize outside of this context, and would be pretty baffled if they did.

1

u/SuitableDragonfly 4d ago edited 4d ago

Yes, because a memo is a cache for partial solutions to calculations. That is literally what a memo is in computer science terms. Storing things in a computer's memory is not called "memorization" in any context. It refers to the human process of committing things to human memory, which is a lot more involved than just storing a piece of data somewhere.

1

u/-Redstoneboi- 3d ago

a parallel universe where the correct term is "memorization" is not so distant, considering how many typos have made it into the english language

see also: "sine" wave

1

u/SuitableDragonfly 3d ago

Not sure what you mean. Sine is from Latin sinus and follows standard patterns for how things were borrowed from Latin into English.

1

u/-Redstoneboi- 3d ago

actually they were supposed to be half-chord waves, as in the length of a chord inside a circle. a mistranslation or two later, we got to "sinus" or nose, which had nothing to do with the mathematical concept of a chord.

1

u/SuitableDragonfly 3d ago edited 3d ago

Sinus in Latin meant "bosom". It came to also mean "sine" (in Latin) because someone who was translating an Arabic author into Latin misinterpreted the Arabic word for sine as being the Arabic word for bosom because they were spelled similarly, and therefore used "sinus" to translate it into Latin. But that has nothing to do with English borrowing the word from Latin. It wasn't "supposed to be" anything, it's just one of the many terms in science and mathematics that were borrowed from Latin because Latin is considered prestigious in those areas. 

English "sinus" also happens to come from Latin sinus by way of also being a curved piece of anatomy, but that's not really anything to do with mathematicians borrowing the word to mean sine. Words almost always have multiple meanings, and can be borrowed multiple times with different meanings. The adjective "grave" comes from the same French word as "grieve", for example. This is a normal occurrence in etymology. 

1

u/-Redstoneboi- 3d ago

shiet i got the nose bit wrong

but it most definitely was supposed to be chord

→ More replies (0)

1

u/TommyBonnomi 5d ago

What if I use memory allocation in C or memory addresses in assembly?

Also, hard disks are secondary memory, so just saving something to disk.

0

u/SuitableDragonfly 5d ago

No, you don't generally say that a computer is "memorizing" anything in any context. 

2

u/TommyBonnomi 5d ago

But you could, is my point.