![]() |
VOOZH | about |
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.
Examples:
Input: V = {7}, S = 0, D = 6
Output: 11
Explanation:
Minimum path 0->7->5->6.
Therefore, the cost of the path = 3 + 6 + 2 = 11Input: V = {7, 4}, S = 0, D = 6
Output: 12
Explanation:
Minimum path 0->7->4->6.
Therefore the cost of the path = 3 + 5 + 4 = 12
Approach:
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 Destination can be calculated by the BFS from these nodes.
Follow the steps below to solve the problem:
Below is the implementation of the above approach:
12
Time Complexity: O(N*M)
Auxiliary Space: O(N+M)