As we discussed in Set 1, following are the two main properties of a problem that suggest that the given problem can be solved using Dynamic programming:
1) Overlapping Subproblems
2) Optimal Substructure
We have already discussed Overlapping Subproblem property in the Set 1. Let us discuss Optimal Substructure property here.
2) Optimal Substructure: A given problems has Optimal Substructure Property if optimal solution of the given problem can be obtained by using optimal solutions of its subproblems.
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. The standard All Pair Shortest Path algorithms like Floyd–Warshall and Bellman–Ford are typical examples of Dynamic Programming.
On the other hand, the Longest Path problem doesn’t have the Optimal Substructure property. Here by Longest Path we mean longest simple path (path without cycle) between two nodes. Consider the following unweighted graph given in the CLRS book. There are two longest paths from q to t: q→r→t and q→s→t. Unlike shortest paths, these longest paths do not have the optimal substructure property. 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 and the longest path from r to t is r→q→s→t.
We will be covering some example problems in future posts on Dynamic Programming.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.
- Optimal Strategy for the Divisor game using Dynamic Programming
- Overlapping Subproblems Property in Dynamic Programming | DP-1
- Dynamic Programming on Trees | Set-1
- Bitmasking and Dynamic Programming | Set-2 (TSP)
- Dynamic Programming on Trees | Set 2
- How to solve a Dynamic Programming Problem ?
- Greedy approach vs Dynamic programming
- Double Knapsack | Dynamic Programming
- Dynamic Programming vs Divide-and-Conquer
- Dynamic Programming | Building Bridges
- Top 20 Dynamic Programming Interview Questions
- Convert N to M with given operations using dynamic programming
- Number of Unique BST with a given key | Dynamic Programming
- Ackermann's function using Dynamic programming
- Python | Implementing Dynamic programming using Dictionary
- Compute nCr % p | Set 1 (Introduction and Dynamic Programming Solution)
- Longest subsequence with a given OR value : Dynamic Programming Approach
- Minimum time required to rot all oranges | Dynamic Programming
- Distinct palindromic sub-strings of the given string using Dynamic Programming
- Travelling Salesman Problem | Set 1 (Naive and Dynamic Programming)
Improved By : ASAJ RAWAT