Thursday, December 26, 2024
Google search engine
HomeData Modelling & AIOptimal Substructure Property in Dynamic Programming | DP-2

Optimal Substructure Property in Dynamic Programming | DP-2

 

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. 

In Dynamic  programming, the ideal base property alludes to the way that an ideal answer for an issue can be built from ideal answers for subproblems. This property is utilized to plan dynamic programming calculations that tackle streamlining issues by separating them into more modest subproblems and afterward consolidating the answers for those subproblems to get an answer for the first issue.
For instance, think about the issue of tracking down the most brief way between two focuses in a diagram. 

On the off chance that we apply dynamic programming to this issue, we can characterize the subproblems as the briefest ways between halfway focuses on the chart, and afterward utilize the answers for those subproblems to build an answer for the first issue.
To show this thought all the more officially, we should assume we disapprove of an ideal arrangement S* and a bunch of subproblems S1, S2, …, Sn. On the off chance that the ideal answer for the issue can be developed from the ideal answers for the subproblems, then the issue displays the ideal base property.
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. 
Example: 

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.

Article 

Practice Problem

1

Longest Common Subsequence 

solve

2

Count ways to reach the n’th stair 

solve

3

Coin Change 

solve

4

Edit Distance | DP-5 – neveropen

solve

5

Cutting a Rod

solve

6

Program for Fibonacci numbers – neveropen

solve

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.

Pseudo code: 

for x in V:
 for y in V:
   dp[x][y] = INF

dp[u][u] = 0

for x in V:
 for y in V:
   for z in V:
     if (x, y) is an edge in E:
       dp[x][y] = min(dp[x][y], dp[x][z] + dp[z][y])

return dp[u][v]
 

C++




#include <iostream>
#include <algorithm>
#include <limits>
 
using namespace std;
 
const int N = 100010;
 
// The number of vertices in the graph
int n;
 
// The adjacency matrix representation of the graph
int adj[N][N];
 
// The array for storing the shortest distances between the vertices
int dist[N][N];
 
void floyd() {
  // Initialize the distances with the weights of the edges
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) {
      dist[i][j] = adj[i][j];
    }
  }
 
  // Solve the subproblems using the optimal substructure property
  for (int k = 0; k < n; k++) {
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < n; j++) {
        // Relax the edge (i, j) using the vertex k as the intermediate vertex
        dist[i][j] = min(dist[i][j], dist[i][k] + dist[k][j]);
      }
    }
  }
}
 
int main() {
  // Read the input
  cin >> n;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) {
      cin >> adj[i][j];
      if (adj[i][j] == 0) {
        // There is no edge between the vertices
        adj[i][j] = numeric_limits<int>::max();
      }
    }
  }
 
  // Solve the shortest path problem
  floyd();
 
  // Print the shortest distances between the vertices
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) {
      cout << i << " " << j << " " << dist[i][j] << endl;
    }
  }
 
  return 0;
}


Java




import java.util.Arrays;
import java.util.List;
import java.util.Map;
 
class ShortestPath {
  static final int INF = Integer.MAX_VALUE;
 
  static int shortestPath(int[][] graph, int u, int v) {
    int n = graph.length;
    int[][] dp = new int[n][n];
    for (int[] row : dp) {
      Arrays.fill(row, INF);
    }
 
    dp[u][u] = 0;
 
    for (int x = 0; x < n; x++) {
      for (int y = 0; y < n; y++) {
        for (int z = 0; z < n; z++) {
          if (graph[x][y] != INF) {
            dp[x][y] = Math.min(dp[x][y], dp[x][z] + dp[z][y]);
          }
        }
      }
    }
 
    return dp[u][v];
  }
 
  public static void main(String[] args) {
    // Example graph
    int[][] graph = {
      {0,   5,   INF, 10},
      {INF, 0,   3,   INF},
      {INF, INF, 0,   1},
      {INF, INF, INF, 0}
    };
 
    int shortestPath = shortestPath(graph, 0, 3);
    System.out.println("Shortest path from 0 to 3: " + shortestPath);
  }
}


Python3




# The number of vertices in the graph
N = 100010
 
# The adjacency matrix representation of the graph
adj = [[0 for j in range(N)] for i in range(N)]
 
# The array for storing the shortest distances between the vertices
dist = [[0 for j in range(N)] for i in range(N)]
 
def floyd():
    global N, adj, dist
    # Initialize the distances with the weights of the edges
    for i in range(N):
        for j in range(N):
            dist[i][j] = adj[i][j]
 
    # Solve the subproblems using the optimal substructure property
    for k in range(N):
        for i in range(N):
            for j in range(N):
                # Relax the edge (i, j) using the vertex k as the intermediate vertex
                dist[i][j] = min(dist[i][j], dist[i][k] + dist[k][j])
 
if __name__ == "__main__":
    # Read the input
    n = int(input())
    for i in range(n):
        line = input().strip().split()
        for j in range(n):
            adj[i][j] = int(line[j])
            if adj[i][j] == 0:
                # There is no edge between the vertices
                adj[i][j] = sys.maxsize
 
    # Solve the shortest path problem
    floyd()
 
    # Print the shortest distances between the vertices
    for i in range(n):
        for j in range(n):
            print(i, j, dist[i][j])


C#




using System;
using System.Collections.Generic;
 
class ShortestPath {
    static readonly int INF = int.MaxValue;
 
    static int shortestPath(int[,] graph, int u, int v) {
        int n = graph.GetLength(0);
        int[,] dp = new int[n, n];
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < n; j++) {
                dp[i, j] = INF;
            }
        }
 
        dp[u, u] = 0;
 
        for (int x = 0; x < n; x++) {
            for (int y = 0; y < n; y++) {
                for (int z = 0; z < n; z++) {
                    if (graph[x, y] != INF) {
                        dp[x, y] = Math.Min(dp[x, y], dp[x, z] + dp[z, y]);
                    }
                }
            }
        }
 
        return dp[u, v];
    }
 
    public static void Main(string[] args) {
        // Example graph
        int[,] graph = {
            {0,   5,   INF, 10},
            {INF, 0,   3,   INF},
            {INF, INF, 0,   1},
            {INF, INF, INF, 0}
        };
 
        int shortestpath = shortestPath(graph, 0, 3);
        Console.WriteLine("Shortest path from 0 to 3: " + shortestpath);
    }
}


Javascript




// Initialize the required libraries
// and declare the class 'ShortestPath'
const Arrays = require('java.util.Arrays');
const List = require('java.util.List');
const Map = require('java.util.Map');
 
class ShortestPath {
// Declare the constant value for maximum integer
static const INF = Number.MAX_VALUE;
 
// Declare the static method for finding the shortest path
static shortestPath(graph, u, v) {
// Get the number of vertices in the graph
const n = graph.length;
// Initialize the 2D array for dynamic programming
const dp = new Array(n).fill().map(() => new Array(n).fill(INF));
 
// Fill the diagonal elements with 0
dp[u][u] = 0;
 
// Iterate through all the vertices
for (let x = 0; x < n; x++) {
  for (let y = 0; y < n; y++) {
    for (let z = 0; z < n; z++) {
      // Check if there is an edge between the vertices
      if (graph[x][y] !== INF) {
        dp[x][y] = Math.min(dp[x][y], dp[x][z] + dp[z][y]);
      }
    }
  }
}
 
// Return the shortest path from u to v
return dp[u][v];
}
 
// Declare the main function for testing
static main(args) {
// Declare the example graph
const graph = [
[0, 5, INF, 10],
[INF, 0, 3, INF],
[INF, INF, 0, 1],
[INF, INF, INF, 0]
];
// Find the shortest path from vertex 0 to vertex 3
const shortestPath = ShortestPath.shortestPath(graph, 0, 3);
 
// Print the result
console.log(`Shortest path from 0 to 3: ${shortestPath}`);
}
}
 
// Call the main function for testing
ShortestPath.main();


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. 
References: 
http://en.wikipedia.org/wiki/Optimal_substructure 
CLRS book
 

Feeling lost in the world of random DSA topics, wasting time without progress? It’s time for a change! Join our DSA course, where we’ll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!

RELATED ARTICLES

Most Popular

Recent Comments