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

Show parent comments

2

u/Sexual_tomato Apr 06 '22

From my understanding, dynamic programming is the act of caching results of a function call or operation for simple subsets of a problem that overlaps finding an optimal solution to a harder overarching problem, like finding the shortest path between two nodes in a weighted graph. My personal interaction with it comes from the class I took in control theory when I got my engineering degree, but I just called it "don't waste time computing stuff I've already computed otherwise the Matlab code takes all night to run."

That being said, thank you for your really detailed explanation, and I appreciate your willingness to outline all the inaccuracies I glossed over.

I'm aware of all the points you made, and I realize that it painted my understanding in a bit of a dubious light. But, given that I typed all that out on my phone, it was time to get off the toilet, and this is a thread regarding the total lack of basic understanding, I figured the extra nuance would be too much noise. Imo, you adding the extra context in a separate comment is a better way to read it anyway :)

1

u/procrastinatingcoder Apr 06 '22

There's a distinction between memoization and dynamic programming. As a "short but obvious" comparison. I really "glossed over" that distinction in my post, which was meant to just be as simple as can be, and someone else already explained it somewhat. But here goes another simple non-detailed explanation with an example.

Memoization:

  • Caching a function's result.

Here's an "example" undirected weighted graph.

A--5--B--7--C
|           |
3           |
|           |
D-----------5

The shortest path from A->C has value 12, therefore if you see the method: graph.findShortestPath(A, C), you know the answer is 12 and you don't actually need to run that function.

HOWEVER

You can't do something like:

shortest_path = graph.findShortestPath(A, C) + graph.findShortestPath(C, D)

In other words, you can't re-use the result from A to C to compute A to D.

As an extra note: You could use memoization to compute the shortest path from A to D passing through C.

Dynamic Programming:

  • Caching the result of sub-problems in order to solve the bigger problem

With the classic example of fibonacci (adding the two previous number of the sequence together makes the next number. the sequence starts with 0, 1, so it follows like this:

fibonacci = 0, 1, 1, (1+1), (1+(1+1), ((1+1) + (1+(1+1))), ...

fibonacci = 0, 1, 1, 2, 3, 5, 8, 13, 21, ...

If you have the Nth and the (N-1)th values, you don't need to recompute everything that came before to get the (N+1)th value. So Dynamic programming can be applied. Dynamic programming is particularly useful in pattern matching, and is a very common way to apply some distances like the Levenshtein one. An example of the fibonacci function would be:

class Foo:
    def __init__(self):
        self.values = [0, 1]

def fibonacci(int: i):
    if len(self.values) >= i:
        return self.values[i]
    ...calculate fibonnaci up to i here, and store the results...

It basically requires the bigger problem's solution to be built from the smaller problem's solution.

Long story short, dynamic programming is usually terrible for most graph-related things, it's in fact one of the reasons why things like graph coloring, etc. Are such a pain. You can't know for sure that the solution to a smaller version of the graph will be part of the solution of a bigger graph that includes that one.

What you're using to doing is thus memoization, and not dynamic programming.

As an added note, you could say that memoization is part of dynamic programming, but not all memoization tends to work with dynamic programming.

I already gave up to write anything longer than a text message on my phone, all good though!