Given a weighted, directed graph G, an array V consisting of vertices, the task is to find the Minimum Cost Path passing through all the vertices of the set V, from a given source S to a destination D.
To solve the problem, the idea is to use Breadth-First-Search traversal. BFS is generally used to find the Shortest Paths in the graph and the minimum distance of all nodes from Source, intermediate nodes, and the Destination can be calculated by the BFS from these nodes.
Follow the steps below to solve the problem:
- Initialize minSum to INT_MAX.
- Traverse the graph from the source node S using BFS.
- Mark each neighbouring node of the source as the new source and perform BFS from that node.
- Once the destination node D is encountered, then check if all the intermediate nodes are visited or not.
- If all the intermediate nodes are visited, then update the minSum and return minimum value.
- If all the intermediate nodes are not visited, then return minSum.
- Mark the source as unvisited.
- Print the final value of minSum obtained.
Below is the implementation of the above approach:
Time Complexity: O(N+M)
Auxiliary Space: O(N+M)
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Minimum cost path from source node to destination node via an intermediate node
- Minimum Cost of Simple Path between two nodes in a Directed and Weighted Graph
- Convert the undirected graph into directed graph such that there is no path of length greater than 1
- Path with minimum XOR sum of edges in a directed graph
- Longest Path in a Directed Acyclic Graph | Set 2
- Find if there is a path between two vertices in a directed graph | Set 2
- Shortest path with exactly k edges in a directed and weighted graph | Set 2
- Find if there is a path between two vertices in a directed graph
- Shortest Path in Directed Acyclic Graph
- Longest Path in a Directed Acyclic Graph
- Shortest path with exactly k edges in a directed and weighted graph
- Longest path in a directed Acyclic graph | Dynamic Programming
- Shortest path in a directed graph by Dijkstra’s algorithm
- Check whether the cost of going from any node to any other node via all possible paths is same
- Convert undirected connected graph to strongly connected directed graph
- Minimum edges to be added in a directed graph so that any node can be reachable from a given node
- Minimum time taken by each job to be completed given by a Directed Acyclic Graph
- Minimum cost to reverse edges such that there is path between every pair of nodes
- Check if a given directed graph is strongly connected | Set 2 (Kosaraju using BFS)
- Minimum edges required to make a Directed Graph Strongly Connected
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. 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.
Improved By : ahmednadeem1