Given a directed tree with V vertices and V-1 edges, we need to choose such a root (from given nodes from where we can reach to every other node) with a minimum number of edge reversal.
In above tree, if we choose node 3 as our root then we need to reverse minimum number of 3 edges to reach every other node, changed tree is shown on the right side.
We can solve this problem using DFS. we start dfs at any random node of given tree and at each node we store its distance from starting node assuming all edges as undirected and we also store number of edges which need to be reversed in the path from starting node to current node, let’s denote such edges as back edges so back edges are those which point towards the node in a path. With this dfs, we also calculate total number of edge reversals in the tree. After this computation, at each node we can calculate ‘number of edge reversal to reach every other node’ as follows,
Let total number of reversals in tree when some node is chosen as starting node for dfs is R then if we want to reach every other node from node i we need to reverse all back edges from path node i to starting node and we also need to reverse all other back edges other than node i to starting node path. First part will be (distance of node i from starting node – back edges count at node i) because we want to reverse edges in path from node i to starting node it will be total edges (i.e. distance) minus back edges from starting node to node i (i.e. back edge count at node i). The second part will be (total edge reversal or total back edges of tree R – back edge count of node i). After calculating this value at each node we will choose minimum of them as our result.
In below code, in the given edge direction weight 0 is added and in reverse direction weight 1 is added which is used to count reversal edges in dfs method.
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.
Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.
- Maximum edge removal from tree to make even forest
- Minimum number of reversals to reach node 0 from every other node
- Product of minimum edge weight between all pairs of a Tree
- Remove all outgoing edges except edge with minimum weight
- Find the root of the sub-tree whose weighted sum XOR with X is minimum
- Find the root of the sub-tree whose weighted sum is minimum
- Minimum Operations to make value of all vertices of the tree Zero
- Minimum number of pairs required to make two strings same
- Minimum edges required to add to make Euler Circuit
- Minimum edges to reverse to make path from a source to a destination
- Minimum number of Water to Land conversion to make two islands connected in a Grid
- Edge Coloring of a Graph
- Delete Edge to minimize subtree sum difference
- Program to Calculate the Edge Cover of a Graph
- Check if removing a given edge disconnects a graph
- Maximize number of nodes which are not part of any edge in a Graph
- Tree, Back, Edge and Cross Edges in DFS of Graph
- Paths to travel each nodes using each edge (Seven Bridges of Königsberg)
- Find weight of MST in a complete graph with edge-weights either 0 or 1
- Shortest Path in a weighted Graph where weight of an edge is 1 or 2