Skip to content
Related Articles

Related Articles

Optimal Substructure Property in Dynamic Programming | DP-2

Improve Article
Save Article
  • Difficulty Level : Easy
  • Last Updated : 28 Nov, 2022
Improve Article
Save Article

As we discussed in Set 1, the 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 the Overlapping Subproblem property in Set 1. Let us discuss the Optimal Substructure property here. 


2) Optimal Substructure:

A given problem is said to have Optimal Substructure Property if the optimal solution of the given problem can be obtained by using the optimal solution to its subproblems instead of trying every possible way to solve the subproblems. 


The Shortest Path problem has the following optimal substructure property: 

If 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 a combination of the shortest path from U to X and the shortest path from X to V. The standard All Pair Shortest Path algorithm like Floyd–Warshall and Single Source Shortest path algorithm for negative weight edges like 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 the 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 the longest path from q to r and the 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. 

Some Standard problems having optimal substructure are:

S. No.


Practice Problem


Longest Common Subsequence 



Count ways to reach the n’th stair 



Coin Change 



Edit Distance | DP-5 – GeeksforGeeks



Cutting a Rod



Program for Fibonacci numbers – GeeksforGeeks


The above problems can be solved optimally using Dynamic programming as each of these problems have an optimal substructure, On the other hand, there are some problems that need to be solved by trying all possible solutions one such problem is Rat in a Maze problem. In these types of problems, the optimal solution for subproblems may not surely give the solution to the entire problem. In Rat in a Maze problem, all paths need to be explored to find out the final path from the source that leads to the destination. Thus in these problems, Recursion and Backtracking are the way to go.

We will be covering some example problems in future posts on Dynamic Programming
Please write comments if you find anything incorrect, or if you want to share more information about the topic discussed above. 

CLRS book

My Personal Notes arrow_drop_up
Related Articles

Start Your Coding Journey Now!