r/learnprogramming • • Apr 05 '22

Name one programming/comp sci concept you never understood and if you understand it, try to explain it to them

Name a programming concept such as virtual functions, pointers, certain algorithms, etc that you always had a hard time understanding and why.

Someone else who reads yours and does understand it, try to explain it to them to help them learn

1.6k Upvotes

738 comments sorted by

View all comments

255

u/ICantPCGood Apr 05 '22

Dynamic Programming, it’s just recursion with notes about what you’ve done already?

We covered it in a class with the only teacher I’ve ever truly called “bad” and then every teacher after just assumed we all knew it 🙃. Could never really grok it on my own or figure out how to apply it.

292

u/procrastinatingcoder Apr 05 '22

It generally refers to keeping a table of previous results, and re-using those for your next calculations. It's nothing special, and I find the term "dynamic" in dynamic programming to be the most misleading thing ever personally.

157

u/[deleted] Apr 05 '22

It was deliberately misleadingly named so the researcher could continue funding or something like that.

It’s a shame as the basics are simple to grasp.

14

u/tv_head__ Apr 05 '22

Got any source ? (if you don't mind my asking)

115

u/TheAtro Apr 05 '22

I spent the Fall quarter (of 1950) at RAND. My first task was to find a name for multistage decision processes. An interesting question is, "Where did the name, dynamic programming, come from?" The 1950s were not good years for mathematical research. We had a very interesting gentleman in Washington named Wilson. He was Secretary of Defense, and he actually had a pathological fear and hatred of the word "research". I’m not using the term lightly; I’m using it precisely. His face would suffuse, he would turn red, and he would get violent if people used the term research in his presence. You can imagine how he felt, then, about the term mathematical. The RAND Corporation was employed by the Air Force, and the Air Force had Wilson as its boss, essentially. Hence, I felt I had to do something to shield Wilson and the Air Force from the fact that I was really doing mathematics inside the RAND Corporation. What title, what name, could I choose? In the first place I was interested in planning, in decision making, in thinking. But planning, is not a good word for various reasons. I decided therefore to use the word "programming". I wanted to get across the idea that this was dynamic, this was multistage, this was time-varying. I thought, let's kill two birds with one stone. Let's take a word that has an absolutely precise meaning, namely dynamic, in the classical physical sense. It also has a very interesting property as an adjective, and that is it's impossible to use the word dynamic in a pejorative sense. Try thinking of some combination that will possibly give it a pejorative meaning. It's impossible. Thus, I thought dynamic programming was a good name. It was something not even a Congressman could object to. So I used it as an umbrella for my activities.

— Richard Bellman, Eye of the Hurricane: An Autobiography (1984, page 159)

https://en.wikipedia.org/wiki/Dynamic_programming

16

u/sageknight Apr 06 '22 edited Apr 06 '22

Wanna get funding from Air Force? Name something that rhymes with dynamite.

→ More replies (1)

6

u/KuntaStillSingle Apr 06 '22

You can hardly object to someone for getting angry at the type of research RAND was up to in that time lol.

9

u/[deleted] Apr 05 '22

https://youtu.be/OQ5jsbhAv_M in the first couple of minutes.

5

u/tv_head__ Apr 05 '22

Lol nice I also love how they still use big ass chalkboards.

14

u/ICantPCGood Apr 05 '22

Oh, that actually sounds similar to how I would describe it. I guess I’ve just been over complicating it in my head. Thanks!

9

u/Select-Sir9364 Apr 05 '22

Is this table of previous results similar to memoization?

11

u/Drifts Apr 06 '22

my limited understanding is that it is exactly memoization

5

u/Unsounded Apr 06 '22

They’re actually two different concepts, dynamic programming is a bit more nuanced, and is defined as a problem being able to be solved by breaking down a larger problem set into a smaller one and the micro decisions being optimal in the sense that you can solve the sub problem and it doesn’t require context from outside the sub problem.

Memoization can help, and is used for some dynamic programming problems but it isn’t the same.

→ More replies (6)

88

u/AlSweigart Author: ATBS Apr 05 '22

(Heh, I just got done writing an entire book on recursion.)

YEP. Famously, the name "dynamic programming" is meaningless. The guy behind DP named it that because he wanted to do math research for RAND but the Secretary of Defense hated "research" projects and wouldn't fund them. Though other people say this story isn't actually true.

Dynamic programming falls into two categories. The first is, as you noted, just recursion but specifically the kind of recursive problems that have overlapping subproblems. For example, recursive Fibonacci has a problem where you keep solving the same thing over and over again: fib(6) is equal to fib(5) + fib(4), which is equal to (fib(4) + fib(3)) + (fib(3) + fib(2)), and so on until the base case where fib(1) and fib(2) are 1. But notice you're solving fib(3) multiple times. These are the overlapping subproblems. The return value never changes, so why not just use a little memory to remember the calculation after doing it once and just re-use that result? This form of caching is specifically called memoization (not memorization). Dynamic programming is recursive algorithms that use memoization to handle their overlapping subproblems.

Merge sort and quicksort are recursive algorithms, but you don't have any repeat recursive calls there so there are no overlapping subproblems.

The other part of DP is tabulation, which is where you effectively start from the base cases and build up to the big problem you want to solve, storing results in a table in memory to help you solve things. For fibonacci, you're starting from the first two numbers 1 and 1 and building up the the nth number.

These are called "top-down dynamic programming" and "bottom-up dynamic programming". Note that there's no such thing as "top-down recursion" and "bottom-up recursion": all recursion is top-down and "bottom-up" recursive algorithms don't actually have recursive function calls and aren't recursive.

4

u/ICantPCGood Apr 05 '22

Thanks! That’s super helpful. I could recall that there was two approaches but had a hard time distinguishing between them. Also the naming history is good to know because I was definitely trying to read in to the name and figure out what was dynamic about Dynamic Programming which lead to a lot of frustration.

→ More replies (1)

30

u/MyHomeworkAteMyDog Apr 05 '22 edited Apr 05 '22

I think of DP as a bottom up approach while recursion is top down. The typical example is the computation tree of a Fibonacci sequence, Fib(n)=fib(n-1) + fib(n-2). Calling fib(n) this way requires 2n fib() calls, many of which are repeated computations. But with DP, you start bottom up, computing and storing fib(0), fib (1), fib(2), …, fib(n-2), fib(n-1), fib(n) in a table. Each next fib call is solved in constant time, using the values previously stored in your table. This lets us avoid recursively recomputing down to the base case for each fib call. With DP, you start with the base case of the recursion and generate the values up to the starting value. This way you can get an answer to Fibonacci(n) in linear time rather then exponential.

7

u/kupiakos Apr 05 '22

Recursion with memoization would get you the same answer with the same number of fib calls, it's just a different approach

7

u/faceplanted Apr 05 '22

You're missing one detail though, using dynamic programming prevents you blowing the stack trying to get to the very first lead in the tree to memoize it, dynamic programming, what with the way it works upwards and uses iteration doesn't have that problem.

→ More replies (1)

35

u/Lynx2161 Apr 05 '22

Its recursion with a hash map of previous results nothing fancy

→ More replies (12)

21

u/the_last_ordinal Apr 05 '22

I usually describe it as "Recursion plus memoization" and I think that's really all it is.

Memoization is a fancy term for notes about what you've done already

6

u/faceplanted Apr 05 '22

The name is meaningless like everyone has said, but the idea is to optimise recursive problems where parts of the recursion tree repeat themselves, and to start from the leaves of the tree rather than the root. So if you know there's only like 9 possibilities for the base case repeated a trillion times you can just work out those 9, and then work out the parents from those, until you get to the top of the tree, basically shaving off 99% of the tree by precalculating the repeated parts.

The reason it's not "recursion with memoization" like some people are saying is that it's specifically designed to remove the risks of recursion, if you use recursion with memoization, you can blow the stack before you ever get to a leaf to memoize it, with dynamic programming you start at the bottom and use iteration so you don't have that problem, and since as I mentioned there might be only like 9 leaf nodes you can limit your memory usage to a similar number of entries.

4

u/rashnull Apr 05 '22

Yes! This! These meaningless buzz words like “memoization” and “dynamic programming” should be taken to the backyard and shot with a bazooka!

3

u/perpetualeye Apr 05 '22

Yes but the important part is identifying optimal substructure and overlapping subproblems. Once you get that then DP is just that, recording stuff.

5

u/pigeon768 Apr 06 '22

Dynamic Programming, it’s just recursion with notes about what you’ve done already?

Yes. That's literally all it is. It's hard if you overthink it. It's easy if you don't think about it too much.

Honestly it's a lot easier if you delete the term "dynamic programming" from your memory banks and just think "that thing where you turn a recursive definition of a thing into a non-recursive algorithm to do the thing".

Consider the Fibbonaci sequence. It's got this nice recursive definition: F(n) = F(n-1) + F(n-2); F(0) = 0; F(1) = 1.

So you write the code to do that:

int fib(int n) {
  if (n <= 1)
    return n;
  else
    return fib(n-1) + fib(n-2);
}

the trouble is that if you run this for even moderate values of n, it takes for freaking ever to run. (go ahead, try it) The trouble is that fibonacci does not have a "simple" nested structure. As you go downwards recursively, it branches outwards.

Dynamic programming is the pile of all the different methods you might use to improve the runtime. For instance, if you do this:

#include <map>

int fib(int n) {
  if (n <= 1)
    return n;
  static std::map<int, int> s;
  if (const auto it = s.find(n); it != s.end())
    return it->second;
  return s[n] = fib(n-1) + fib(n-2);
}

...you've done dynamic programming.

If you do this:

int fib(int n) {
  int a = 0;
  int b = 1;
  for (int i = 0; i < n; i++) {
    int tmp = a + b;
    b = a;
    a = tmp;
  }
  return a;
}

...you've done dynamic programming.

Here's the important bit: Dynamic programming is an idea, not an algorithm. There is no set of steps to start with a recursive definition and end up with a realizable algorithm. I've just given you a recursive definition of the Fibonnaci sequence and shown you two totally different algorithms that might fall out of applying "dynamic programming" to the recursive definition.

→ More replies (4)