10

I was revisiting my notes on Dynamic Programming. Its basically a memoized recursion technique, which stores away the solutions to smaller subproblems for later reuse in computing solutions to relatively larger sub problems.

The question I have is that in order to apply DP to a recursive problem, it must have an optimal substructure. This basically necessitates that an optimal solution to a problem contains optimal solution to subproblems.

Is it possible otherwise ? I mean have you ever seen a case where optimal solution to a problem does not contain an optimal solution to subproblems.

Please share some examples, if you know to deepen my understanding.

Community
  • 1
  • 1
OneMoreError
  • 7,518
  • 20
  • 73
  • 112
  • 2
    "a case where optimal solution to a problem does not contain an optimal solution to subproblems" This is the default state of things. Usually it's necessary to choose the definition of subproblem optimality to get a DP to work -- as a result, I'm not sure that your other questions really are answerable in a satisfactory manner. – David Eisenstat Jan 10 '15 at 13:12

6 Answers6

18

In dynamic programming a given problems has Optimal Substructure Property if optimal solution of the given problem can be obtained by using optimal solutions of its sub problems.

For example the shortest path problem has following optimal substructure property: If a node X lies in the shortest path from a source node U to destination node V then the shortest path from U to V is combination of shortest path from U to X and shortest path from X to V.

But Longest path problem doesn’t have the Optimal Substructure property. i.e the longest path between two nodes doesn't have to be the longest path between the in between nodes.

For example, the longest path q->r->t is not a combination of longest path from q to r and longest path from r to t, because the longest path from q to r is q->s->t->r.

enter image description here

So here: optimal solution to a problem does not contain an optimal solution to the sub problems.

For more details you can read

  1. Longest path problem from wikipedia
  2. Optimal substructure from wikipedia
Community
  • 1
  • 1
Umesh Mishra
  • 749
  • 4
  • 11
  • 1
    Shortest vs. longest path is an excellent example. As others have observed, though, it depends on how subproblems are defined: the shortest path problem gives us optimal substructure if we choose to decompose it into subproblems of the form "What is the shortest path between any two given nodes u and v?", while longest path doesn't. But if we instead choose to ask "What is the longest path between two given nodes u and v *that uses only nodes from the given set X*?", this decomposition *will* give us optimal substructure -- but it requires an exponential number of subproblems. – j_random_hacker Jan 17 '15 at 15:44
  • Specifically, here is a DP-ready recursion for finding the longest path between u and v that uses only nodes from X: `L(u, v, X) = the max, over all x in X, of (the max, over all subsets Y of X that contain both u and x, of (L(u, x, Y) + L(y, v, X-Y+{x})))`. – j_random_hacker Jan 17 '15 at 15:56
  • yes very thoughtful indeed necessarily including a node z(lets just take node rather then set) between u to v will convert this problem into optimal substructure when the problem will divide into finding longest path from u to z and z to v but still that path doesn't have to be longest between the u to v. Hence to introduce optimal substructure efficiently in this problem we need to make a node compulsory. – Umesh Mishra Jan 17 '15 at 18:25
  • http://www.geeksforgeeks.org/dynamic-programming-set-2-optimal-substructure-property/ – berkay Nov 02 '15 at 06:37
  • @UmeshMishra Shouldn't you give reference for the example you used? :D – stillanoob Mar 03 '16 at 15:36
5

To my understanding, this 'optimal substructure' property is necessary not only for Dynamic Programming, but to obtain a recursive formulation of the solution in the first place. Note that in addition to the Wikipedia article on Dynamic Programming, there is a separate article on the optimal substructure property. To make things even more involved, there is also an article about the Bellman equation.

Codor
  • 17,447
  • 9
  • 29
  • 56
  • I do not think this statement is correct. Recursion can be applied on many other problems than finding optimal solution. Reverse statement is right, Any problem solves using dynamic programming can be solved using recursion, but complexity may be high – kamoor Jan 17 '15 at 04:06
  • I agree in part, however this is debatable. Some people consider evaluation of the Fibonacci sequence or computation of a binomial coefficient not dynamic programming, as they are not optimization problems. I don't share that view, though. – Codor Jan 17 '15 at 08:32
5

You're perfectly right that the definitions are imprecise. DP is a technique for getting algorithmic speedups rather than an algorithm in itself. The term "optimal substructure" is a vague concept. (You're right again here!) To wit, every loop can be expressed as a recursive function: each iteration solves a subproblem for the successive one. Is every algorithm with a loop a DP? Clearly not.

What people actually mean by "optimal substructure" and "overlapping subproblems" is that subproblem results are used often enough to decrease the asymptotic complexity of solutions. In other words, the memoization is useful! In most cases the subtle implication is a decrease from exponential to polynomial time, O(n^k) to O(n^p), p<k or similar.

Ex: There is an exponential number of paths between two nodes in a dense graph. DP finds the shortest path looking at only a polynomial number of them because the memos are extremely useful in this case.

On the other hand, Traveling salesman can be expressed as a memoized function (e.g. see this discussion), where the memos cause a factor of O( (1/2)^n ) time to be saved. But, the number of TS paths through n cities, is O(n!). This is so much bigger that the asymptotic run time is still super-exponential: O(n!)/O(2^n) = O(n!). Such an algorithm is generally not called a Dynamic Program even though it's following very much the same pattern as the DP for shortest paths. Apparently it's only a DP if it gives a nice result!

Community
  • 1
  • 1
Gene
  • 46,253
  • 4
  • 58
  • 96
3

You could solve the Traveling Salesman Problem, choosing the nearest city at each step, but it's wrong method.

Mark Shevchenko
  • 7,937
  • 1
  • 25
  • 29
  • 1
    The TSP actually has an 'optimal substructure' : Let G(V,E) be a (complete) graph and S ∈ V. TSP(G,S) = min(TSP(G', S')) where S' ∈ V, S' ≠ S and G' = G - S). The problem is that to store every "internal variable", you need n! space :) – Rerito Jan 12 '15 at 07:35
  • 1
    To be more precise, this depends on the approach. To my understanding, the optimal substructure is not really intrinsic in the problem, but depends on the formulation. Apparently the main problem is to find a suitable modelling of the problem which has an optimal substructure. – Codor Jan 12 '15 at 07:41
  • @greybeard How so? Plus, the fact that every computation has to be done once means n! time complexity as well (that's not a surprise). – Rerito Jan 12 '15 at 09:16
  • @Rerito In my opinion the optimal substructure has the property that new data doesn't cancel previous calucaltions. Say when calculate Levenshtein distance, new characters at the end of strings doesn't cancel results of calcuation at the start of strings. In the TSP new edge can completely cancel the previous shortest route. Otherwise the TSP would be solved by Dynamic Programming. – Mark Shevchenko Jan 12 '15 at 09:34
  • @Codor I agree. An optimal substructure may not be obvious. – Mark Shevchenko Jan 12 '15 at 10:28
0

The whole idea is to narrow down the problem into the relatively small set of the candidates for optimal solution and use "brute force" to solve it.

So it better be that solutions of the smaller sub-problem should be sufficient to solve the bigger problem.

This is expressed via a recursion as function of the optimal solution of smaller sub-problems.

answering this question:

Is it possible otherwise ? I mean have you ever seen a case where optimal solution to a problem does not contain an optimal solution to subproblems.

no it is not possible, and can even be proven.

user2649908
  • 557
  • 7
  • 15
0

You can try to implement dynamic programming on any recursive problem but you will not get any better result if it doesn't have optimal substructure property. In other words dynamic programming methodology is not useful to implement on a problem which doesn't have optimal substructure property.

kamoor
  • 2,909
  • 2
  • 19
  • 34