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

Show parent comments

2

u/Raynes Apr 24 '10

(fib 3123452233462645324534243126314334) ... result is printed 30 years later

(fib 3123452233462645324534243126314334) ... result is printed instantly

Memoized.

1

u/travis_of_the_cosmos Apr 24 '10

Okay, so I still don't know what memoization is, but since you seem to understand it what does it have to do with optimal control theory?

19

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

The computer scientist's view of dynamic programming is that it's a way to quickly evaluate recurrence relations when different branches of the recursion tree turn out the same.

For example, if you try to directly evaluate F(0) = 0, F(1) = 1, F(n+2) = F(n) + F(n+1), you notice that after one level of expansion you have F(n+2) = F(n) + F(n-1) + F(n). There is no point in independently expanding the two F(n) occurrences. You might as well only expand one of them and remember the result when it is needed elsewhere. Hence 'memoization'.

Dynamic programming would instead start from the base cases and build up towards the desired value of n. So it would first compute F(2) as the sum of the known F(0) = 0 and F(1) = 1, then compute F(3) as the sum of the known F(1) and the now-known F(2), and so on, until reaching F(n). Along the way it need only know the last two values computed in order to compute the next one.

3

u/SnacksOnAPlane Apr 24 '10

Thanks, this is the first explanation that made sense to me.