4 ms·
I think if you find those algorithms obvious and you're able to write memoized solutions, then you already know most of what you need to know about dynamic prog
by svat 8y ago
I think if you find those algorithms obvious and you're able to write memoized solutions, then you already know most of what you need to know about dynamic programming. For example, Dijkstra's algorithm for shortest paths can be seen as DP.
The main thing is to write down a formulation for the answer for n in terms of answers for smaller n, or the answer for (n, k) in terms of answers for smaller (n, k). (And don't worry about how you'd compute them, just focus on getting a correct expression in terms of smaller ones. http://okasaki.blogspot.com/2008/10/score-one-for-induction.html http://okasaki.blogspot.com/2008/10/score-one-for-induction....)
It can be tricky to formulate exactly what you're counting / measuring (e.g. what's "k" and what's the quantity you're optimizing for (n, k)), but once you have that, and once you have the recurrence, you can consider it a separate (and independent) step to figure out in which order to compute the values, so that you have each value by the time you need it (i.e. the bottom-up formulation that you said is the tricky part for you).
Beyond that I guess lots of practice with problems… e.g. about a decade ago there used to be weekly(?) TopCoder contests with editorials written later explaining the solutions (and the problems were graded by difficulty and even how many people solved it), and the DPs could get quite tricky. I believe those are still happening, and now there are other resources like LeetCode or whatnot.
Do you have an example of a problem that you struggled with, to see what's missing?
- jefferickson 8y ago"Dijkstra's algorithm for shortest paths can be seen as DP" Really? Sure, if the graph is a dag, but then Dijkstra is overkill. Hmm. I'll have to think about this one. +1 for the Okasaki shoutout.
- svat 8y agoHmm, I wrote that from vague memory, and perhaps that's debatable! (There seem to be different communities that use “dynamic programming” slightly differently.) But my main point was: if one can look at the shortest-path problem and formulate something like distance(v) = min_{u->v} (distance(u) + edge_length(u, v)) then one probably has a handle on whatever it is that's hard about dynamic programming. Actually Wikipedia cites some references that consider Dijkstra's algorithm a case of DP: https://en.wikipedia.org/w/index.php?title=Dijkstra%27s_algorithm&oldid=876084651#Dynamic_programming_perspective https://en.wikipedia.org/w/index.php?title=Dijkstra%27s_algo... and the same text (Wikipedia doesn't care about “self-plagiarism”) is also on the DP page where Dijkstra's algorithm is given as the first example: https://en.wikipedia.org/w/index.php?title=Dynamic_programming&oldid=876525808#Dijkstra's_algorithm_for_the_shortest_path_problem https://en.wikipedia.org/w/index.php?title=Dynamic_programmi...