Given a directed graph and a source node and destination node, we need to find how many edges we need to reverse in order to make at least 1 path from source node to destination node.
In above graph there were two paths from node 0 to node 6, 0 -> 1 -> 2 -> 3 -> 6 0 -> 1 -> 5 -> 4 -> 6 But for first path only two edges need to be reversed, so answer will be 2 only.
This problem can be solved assuming a different version of the given graph. In this version we make a reverse edge corresponding to every edge and we assign that a weight 1 and assign a weight 0 to original edge. After this modification above graph looks something like below,
Now we can see that we have modified the graph in such a way that, if we move towards original edge, no cost is incurred, but if we move toward reverse edge 1 cost is added. So if we apply Dijkstra’s shortest path on this modified graph from given source, then that will give us minimum cost to reach from source to destination i.e. minimum edge reversal from source to destination.
Below is the code based on above concept.
This article is contributed by Utkarsh Trivedi. 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 write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
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.
- Shortest Path with even number of Edges from Source to Destination
- Shortest path in a graph from a source S to destination D with exactly K edges for multiple Queries
- Count all possible walks from a source to a destination with exactly k edges
- Minimum cost to reverse edges such that there is path between every pair of nodes
- Shortest path from source to destination such that edge weights along path are alternatively increasing and decreasing
- Minimum cost path from source node to destination node via an intermediate node
- Dijkstra's shortest path with minimum edges
- Path with minimum XOR sum of edges in a directed graph
- Minimum edges required to add to make Euler Circuit
- Minimum edges required to make a Directed Graph Strongly Connected
- Ways to Remove Edges from a Complete Graph to make Odd Edges
- Number of Walks from source to destination
- Print all paths from a given source to a destination using BFS
- Print all paths from a given source to a destination
- Print all shortest paths between given source and destination in an undirected graph
- Sum of shortest distance on source to destination and back having at least a common vertex
- Count total ways to reach destination from source in an undirected Graph
- Find if there is a path of more than k length from a source
- Single source shortest path between two cities
- Path with smallest product of edges with weight >= 1