Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dynamic Programming : Why the need for optimal sub structure

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.

like image 819
OneMoreError Avatar asked Jan 04 '15 17:01

OneMoreError


People also ask

Is dynamic programming optimal substructure?

Uses. Dynamic programming can be used to solve a problem with optimal substructure and overlapping subproblems. Greedy algorithms may be used to solve a problem with optimal substructure.

What is the reason dynamic programming can provide an optimal solution?

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.

What is optimal in dynamic programming?

The principle of optimality is the basic principle of dynamic programming, which was developed by Richard Bellman: that an optimal path has the property that whatever the initial conditions and control variables (choices) over some initial period, the control (or decision variables) chosen over the remaining period ...

Do we need optimal substructure for divide and conquer solutions?

Divide and conquer is applicable when your problem exhibits optimal substructure, but not overlapping subproblems.


2 Answers

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
like image 67
Umesh Mishra Avatar answered Oct 25 '22 17:10

Umesh Mishra


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!

like image 21
Gene Avatar answered Oct 25 '22 18:10

Gene