I have used the technique of dynamic programming multiple times however today a friend asked me how I go about defining my sub-problems, I realized I had no way of providing an objective formal answer. How do you formally define a sub-problem for a problem that you would solve using dynamic programming?
-
It looks like none of the existing answers (as of April, 2019) are good enough, especially for beginners. I would recommend tutorials on other sites. – John L. Apr 10 '19 at 01:27
3 Answers
The principle of dynamic programming is to think top-down (i.e recursively) but solve bottom up. So a good strategy for designing a DP is to formulate the problem recursively and generate sub-problems that way.

- 5,387
- 2
- 27
- 31
-
15
-
2@JeffE Yes, I read and used your notes when teaching my algorithm classes and it was effective. Quote: "Dynamic is not about filling in tables. It's about smart recursion!" – Dai Mar 22 '12 at 14:06
-
2My understanding of how to teach DPs is STRONGLY influenced by @JeffE's notes :) – Suresh Mar 22 '12 at 16:47
-
3It's also possible to automatically turn a top-down recursive procedure into a dynamic programming algorithm. When you are about to return, store the answer in a hash table. On the start of each call, check if the answer is already in the hash table, and if so, return it immediately. Many algorithms become easy with this idea. For example with such a table, recursive algorithms on tries automatically work on DAWGs. By storing a sentinel value in the table at the start of a call, the same algorithms can work even on DFAs. Algorithms on BDDs become very easy to specify recursively. – Jules Mar 23 '12 at 19:21
-
1Last but not least, this can have huge performance advantages. For example the traditional bottom up subset sum algorithm can compute tons of unneeded table entries. With this method only the necessary table entries will be computed. – Jules Mar 23 '12 at 19:26
-
Here is a Python implementation: http://pastebin.com/AugVDvRA You just put @memo in front of your recursive algorithm and it will magically become a dynamic programming algorithm. – Jules Mar 23 '12 at 19:35
-
Sorry for spamming this thread so much, but DFA minimization using this technique is just too lovely. Not many people realize that this is just dynamic programming, like many other algorithms. Here is a program for computing the equivalent states of a DFA in essentially 2 lines: http://pastebin.com/1cBtDdXz The same technique can also be used to handle left recursion in recursive descent parsers. And Hindley-Milner type inference of mutually recursive functions. And a Datalog interpreter. Etc, etc. Newton's method and infmemo pretty much make 95% of algorithms trivial. – Jules Mar 23 '12 at 20:19
-
1Strategy is clear, but how do you go about finding a recurrence? Is there a technique or just study lots of DP problems. – mrk Jun 14 '14 at 17:12
As @Suresh pointed out, once you know that your problem can be solved by DP (i.e. it exhibits optimal substructure and overlapping subproblems), you may think of a divide and conquer recursive solution.
Of course, divide and conquer will be highly ineffecient because every subproblem encountered in the associated recursion tree will be solved again even if it has already been found and solved. This is where DP differs: each time you encounter a subproblem, you solve it and store its solution in a table. Later, when you encounter again that subproblem, you access in $\mathcal{O}(1)$ time its solution instead of solving it again. Since the number of overlapping subproblems is typically bounded by a polynomial, and the time required to solve one subproblem is polynomial as well (otherwise DP can not provide a cost-efficient solution), you achieve in general a polynomial solution.
Therefore, thinking about a divide and conquer solution will provide you with the insight about what a subproblem may be for your particular problem.

- 72,336
- 29
- 179
- 389

- 4,247
- 18
- 27
-
1"optimal substructure" (whatever that means) is probably not a sufficient condition for DP-solvability. "Overlapping subproblems" is certainly not a necessary one. – Raphael Mar 22 '12 at 11:08
-
1Optimal substructure and overlapping supproblems are both exhibited by problems that can be efficiently solved by DP. Of course optimal substructure alone is not enough for DP solvability. However, if you do not have overlapping subproblems, then you can solve the problem by ordinary divide and conquer with the same cost: indeed, the advantage of DP over divide an conquer is that each subproblem is solved exactly once when encountered in the recursion tree. – Massimo Cafaro Mar 22 '12 at 12:16
-
Somewhat true, but your formulation suggests that these two properties characterise the class of problems that can be solved by DP. – Raphael Mar 22 '12 at 12:21
-
1It's not my formulation: you will find it on "Introduction to algorithms" by Cormen, Leiserson, Rivest and Stein and on many other textbooks on algorithms. – Massimo Cafaro Mar 22 '12 at 15:00
-
1Jup, and most are at least partially wrong. I am happy to elaborate if you post a suitable question. – Raphael Mar 22 '12 at 15:35
-
1I am not sure I understand correctly your last comment. To show that this kind of characterization is wrong (it can't be partially wrong: either is correct or it is wrong), you can simply show as a counterexample, a problem which does not exhibit both optimal substructure and overlapping subproblems, yet is amenable to a polynomial DP solution. But note that, in this context, that means a solution which is provably better than ordinary divide and conquer. – Massimo Cafaro Mar 22 '12 at 16:23
-
This is too long here. If you want to discuss it further, please come into the chat or ask a new question regarding the typical "definition" of dynamic programming. – Raphael Mar 22 '12 at 16:26
My experience is finding out a way to "cut down redundant enumerating with help of storing useful value already enumerated". By the way, Dynamic Programming is really popular in ICPC(International Collegiate Programming Contest. Anyone can have his own feeling about DP after practice several ICPC problems.

- 143
- 5