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.
Fib(n) being used for DP has always been funny example to me. The naive recursive method is worse than the DP method sure... but if you calculate it the way a *human would on paper* it's the non-recursive O(n) time O(1) space solution.
It's even funnier for me because analytic solutions exist. We expect people to have complete mastery of courses in in discrete math and algorithms but then pretend they don't know how to use a generating series for a linear recurrence.
To be honest I wouldn't expect anybody who isn't a specialist to be able to do that off the top of their head. The last time I heard the words "a generating series for a linear recurrence" was in college 10 years ago, and it hasn't been relevant to my software eng career since, including in interviews. I've had to dust off other parts of the math toolkit occasionally, but it's hard to justify analytical solutions in an environment where requirements are expected to change after your solution is live.
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.
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.
Doesn't this ignore cache update and lookup times? I guess O(n_max) (n->infinity makes that hard) would have plain array indexed with n, but otherwise you end with at least multiples of n. And there is a loop or tail recursion that has O(n) time and constant memory use anyway. Point of memoization is that after first time, its cache fetch complexity rather than original complexity, O(1) if we have enough memory and trivial index, and you can do that with the loop and tail recursion as well.
the memoized recursive function is still dynamic programming; it's just top-down whereas the obvious loop implementation (that everyone writes before they even learned what recursion is) is the so-called "bottom up" approach which just shows more how "dynamic programming" is such a stupid nothing burger name
View all comments
39
u/Illustrious-Ebb8815 1d ago edited 1d 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.