Given a directed graph, which may contain cycles, where every edge has weight, the task is to find the minimum cost of any simple path from a given source vertex ‘s’ to a given destination vertex ‘t’. Simple Path is the path from one vertex to another such that no vertex is visited more than once. If there is no simple path possible then return INF(infinite).
The graph is given as adjacency matrix representation where value of graph[i][j] indicates the weight of an edge from vertex i to vertex j and a value INF(infinite) indicates no edge from i to j.
Examples:
Input : V = 5, E = 6
s = 0, t = 2
graph[][] = 0 1 2 3 4
0 INF -1 INF 1 INF
1 INF INF -2 INF INF
2 -3 INF INF INF INF
3 INF INF -1 INF INF
4 INF INF INF 2 INF
Output : -3
Explanation :
The minimum cost simple path between 0 and 2 is given by:
0 -----> 1 ------> 2 whose cost is (-1) + (-2) = (-3).
Input : V = 5, E = 6
s = 0, t = 4
graph[][] = 0 1 2 3 4
0 INF -7 INF -2 INF
1 INF INF -11 INF INF
2 INF INF INF INF INF
3 INF INF INF 3 -4
4 INF INF INF INF INF
Output : -6
Explanation :
The minimum cost simple path between 0 and 2 is given by:
0 -----> 3 ------> 4 whose cost is (-2) + (-4) = (-6).
Approach :
The main idea to solve the above problem is to traverse through all simple paths from s to t using a modified version of Depth First Search and find the minimum cost path amongst them. One important observation about DFS is that it traverses one path at a time, hence we can traverse separate paths independently using DFS by marking the nodes as unvisited before leaving them.
A simple solution is to start from s, go to all adjacent vertices, and follow recursion for further adjacent vertices until we reach the destination. This algorithm will work even when negative weight cycles or self edges are present in the graph.
Below is the implementation of the above-mentioned approach:
// C++ code for printing Minimum Cost // Simple Path between two given nodes // in a directed and weighted graph #include <bits/stdc++.h> using namespace std; // Define number of vertices in // the graph and infinite value #define V 5 #define INF INT_MAX // Function to do DFS through the nodes int minimumCostSimplePath(int u, int destination, bool visited[], int graph[][V]) { // check if we find the destination // then further cost will be 0 if (u == destination) return 0; // marking the current node as visited visited[u] = 1; int ans = INF; // traverse through all // the adjacent nodes for (int i = 0; i < V; i++) { if (graph[u][i] != INF && !visited[i]) { // cost of the further path int curr = minimumCostSimplePath(i, destination, visited, graph); // check if we have reached the destination if (curr < INF) { // Taking the minimum cost path ans = min(ans, graph[u][i] + curr); } } } // unmarking the current node // to make it available for other // simple paths visited[u] = 0; // returning the minimum cost return ans; } // driver code int main() { // initialising the graph int graph[V][V]; for (int i = 0; i < V; i++) { for (int j = 0; j < V; j++) { graph[i][j] = INF; } } // marking all nodes as unvisited bool visited[V] = { 0 }; // initialising the edges; graph[0][1] = -1; graph[0][3] = 1; graph[1][2] = -2; graph[2][0] = -3; graph[3][2] = -1; graph[4][3] = 2; // source and destination int s = 0, t = 2; // marking the source as visited visited[s] = 1; cout << minimumCostSimplePath(s, t, visited, graph); return 0; } |
-3
Recommended Posts:
- Minimum Cost Path in a directed graph via given set of intermediate nodes
- Shortest path with exactly k edges in a directed and weighted graph
- Shortest path with exactly k edges in a directed and weighted graph | Set 2
- Minimum cost to connect weighted nodes represented as array
- Path with minimum XOR sum of edges in a directed graph
- Minimum cost to reverse edges such that there is path between every pair of nodes
- Minimum Cost Path to visit all nodes situated at the Circumference of Circular Road
- Convert the undirected graph into directed graph such that there is no path of length greater than 1
- Shortest Path in a weighted Graph where weight of an edge is 1 or 2
- Find if there is a path between two vertices in a directed graph | Set 2
- Longest Path in a Directed Acyclic Graph | Set 2
- Shortest Path in Directed Acyclic Graph
- Longest Path in a Directed Acyclic Graph
- Find if there is a path between two vertices in a directed graph
- Shortest path in a directed graph by Dijkstra’s algorithm
- Longest path in a directed Acyclic graph | Dynamic Programming
- Maximum weighted edge in path between two nodes in an N-ary tree using binary lifting
- Maximum cost path in an Undirected Graph such that no edge is visited twice in a row
- Minimum edges required to make a Directed Graph Strongly Connected
- Minimum Cost Graph
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


