Given a connected, directional graph. Each node is connected to exactly two other nodes. There is weight associated with each edge denoting the cost to reverse its direction. The task is to find the minimum cost to reverse some edges of the graph such that it is possible to go from each node to every other node.
Input: 5 1 2 7 5 1 8 5 4 5 3 4 1 3 2 10 Output: 15 Input: 6 1 5 4 5 3 8 2 4 15 1 6 16 2 3 23 4 6 42 Output: 39
- In order to reach from each node to every other node, the graph must form a ring i.e Direct all edges on it in one of 2 directions either clockwise or anti-clockwise. Let us denote the cost of redirecting all the clockwise edges to anticlockwise direction as cost1 and vice versa as cost2. The answer is clearly the minimum of these two costs.
- Maintain two boolean arrays start and end. The start and end arrays denote whether there is an edge starting from or ending at a given node. Whenever we encounter an edge going from node a to node b, we first check the condition if there is an edge already starting from node a or ending at node b. If there is an edge that satisfying the condition, the edge is in the opposite direction to the edge already present. In this case, we update cost2 and store the edge is the opposite direction. Otherwise, we update the cost1. This way we are able to maintain the costs of both orientations. Finally, print the minimum cost.
Below is the implementation of above approach:
Time Complexity: O(N) where N is number of edges
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 edges to reverse to make path from a source to a destination
- Minimum Cost of Simple Path between two nodes in a Directed and Weighted Graph
- Minimum Cost Path in a directed graph via given set of intermediate nodes
- Ways to Remove Edges from a Complete Graph to make Odd Edges
- Convert the undirected graph into directed graph such that there is no path of length greater than 1
- Dijkstra's shortest path with minimum edges
- Path with minimum XOR sum of edges in a directed graph
- Longest subsequence such that absolute difference between every pair is atmost 1
- Find if there is a path between two vertices in a directed graph
- Find whether there is path between two cells in matrix
- Find if there is a path between two vertices in a directed graph | Set 2
- Find if there is a path between two vertices in an undirected graph
- Minimum nodes to be colored in a Graph such that every node has a colored neighbour
- Minimum Cost using Dijkstra by reducing cost of an Edge
- Minimum cost to empty Array where cost of removing an element is 2^(removed_count) * arr[i]
- Maximum cost path in an Undirected Graph such that no edge is visited twice in a row
- Minimum colors required such that edges forming cycle do not have same color
- Minimum number of Nodes to be removed such that no subtree has more than K nodes
- Minimum labelled node to be removed from undirected Graph such that there is no cycle
- Minimum Cost Path with Left, Right, Bottom and Up moves allowed
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.