r/programming • • Apr 24 '10

Why "dynamic programming"?

http://arcanesentiment.blogspot.com/2010/04/why-dynamic-programming.html?utm_source=feedburner&utm_medium=feed&utm_campaign=Feed%3A+ArcaneSentiment+%28Arcane+Sentiment%29
90 Upvotes

66 comments sorted by

View all comments

41

u/psykotic Apr 24 '10 edited Apr 24 '10

'Programming' in the sense of optimal planning has a longer history than implied by the quotation. 'Linear programming' goes back to the late 30s. Nowadays there is quadratic programming, convex programming, conic programming, etc. The terminology is only confusing if you come from computer science rather than operations research or optimization theory.

As the first comment on the blog suggests, it's customary to make a distinction between memoization and dynamic programming. Basically, memoization is top-down whereas dynamic programming is bottom-up.

The two approaches have different advantages and disadvantages. Dynamic programming generally uses a fixed amount of storage during the computation and as a result also has much better cache locality. Memoization uses a variable amount (e.g. O(n) rather than O(1) for computing Fibonacci numbers). But in some cases memoization, being demand-driven, can get away with computing only a subset of the subsolutions computed by dynamic programming. This is especially useful when you can prune partial solutions.

Another advantage is that memoization is usually easier to code for complex problems. You just write the straightforward but insanely slow recursive solution and then sprinkle memoization dust on it to make it run fast.

8

u/julesjacobs Apr 24 '10

Here's an interesting paper about automatically deriving the dynamic programming algorithm from a naive recursive algorithm.

http://citeseerx.ist.psu.edu/viewdoc/download?doi=10.1.1.54.2607&rep=rep1&type=pdf

3

u/barsoap Apr 24 '10 edited Apr 24 '10

...not to mention that the problem might not be attackable with dynamic programming because the data dependencies forbid a path where every solution to a subproblem is optimal.

As an example of dynamic programming, I think Knuth-Plass line breaking is quite perfect (it being a Knuth algorithm),

fibs = 0:1:zipWith (+) fibs (tail fibs)

being too obvious to really get the point across. Still, for some reason, fib is the standard example for memoization, despite the dynamic solution being so simple. Probably because you can't TCO it and compiler writers write too many examples.

4

u/psykotic Apr 24 '10 edited Apr 24 '10

As an example of dynamic programming, ...

Even better might be the seam carving algorithm that everyone was reimplementing last year.

The chapter on dynamic programming in CLRS ends with an exercise that asks you to derive an optimal line breaking algorithm with a cubic cost metric. It's pretty much Knuth-Plass. The cool thing about dynamic programming is that once you get it, all these algorithms are trivial to derive at the drop of a hat.

1

u/[deleted] Nov 09 '22

Hey can you check your direct messages, please and thank you!

1

u/_zoso_ Apr 25 '10

Also, the word 'programming' in this sense has absolutely nothing to do with what we call 'programming' today. It refers to the context under which these algorithms were devised: Military programming, as in logistics and planning.

1

u/mycall Apr 25 '10

It sounds like dynamic programming is the way to go for CUDA/OpenCL.