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.

Examples:

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.

`// C++ program to find min edge reversal to ` `// make every node reachable from root ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// method to dfs in tree and populates disRev values ` `int` `dfs(vector< pair<` `int` `, ` `int` `> > g[], ` ` ` `pair<` `int` `, ` `int` `> disRev[], ` `bool` `visit[], ` `int` `u) ` `{ ` ` ` `// visit current node ` ` ` `visit[u] = ` `true` `; ` ` ` `int` `totalRev = 0; ` ` ` ` ` `// looping over all neighbors ` ` ` `for` `(` `int` `i = 0; i < g[u].size(); i++) ` ` ` `{ ` ` ` `int` `v = g[u][i].first; ` ` ` `if` `(!visit[v]) ` ` ` `{ ` ` ` `// distance of v will be one more than distance of u ` ` ` `disRev[v].first = disRev[u].first + 1; ` ` ` ` ` `// initialize back edge count same as ` ` ` `// parent node's count ` ` ` `disRev[v].second = disRev[u].second; ` ` ` ` ` `// if there is a reverse edge from u to i, ` ` ` `// then only update ` ` ` `if` `(g[u][i].second) ` ` ` `{ ` ` ` `disRev[v].second = disRev[u].second + 1; ` ` ` `totalRev++; ` ` ` `} ` ` ` `totalRev += dfs(g, disRev, visit, v); ` ` ` `} ` ` ` `} ` ` ` ` ` `// return total reversal in subtree rooted at u ` ` ` `return` `totalRev; ` `} ` ` ` `// method prints root and minimum number of edge reversal ` `void` `printMinEdgeReverseForRootNode(` `int` `edges[][2], ` `int` `e) ` `{ ` ` ` `// number of nodes are one more than number of edges ` ` ` `int` `V = e + 1; ` ` ` ` ` `// data structure to store directed tree ` ` ` `vector< pair<` `int` `, ` `int` `> > g[V]; ` ` ` ` ` `// disRev stores two values - distance and back ` ` ` `// edge count from root node ` ` ` `pair<` `int` `, ` `int` `> disRev[V]; ` ` ` ` ` `bool` `visit[V]; ` ` ` ` ` `int` `u, v; ` ` ` `for` `(` `int` `i = 0; i < e; i++) ` ` ` `{ ` ` ` `u = edges[i][0]; ` ` ` `v = edges[i][1]; ` ` ` ` ` `// add 0 weight in direction of u to v ` ` ` `g[u].push_back(make_pair(v, 0)); ` ` ` ` ` `// add 1 weight in reverse direction ` ` ` `g[v].push_back(make_pair(u, 1)); ` ` ` `} ` ` ` ` ` `// initialize all variables ` ` ` `for` `(` `int` `i = 0; i < V; i++) ` ` ` `{ ` ` ` `visit[i] = ` `false` `; ` ` ` `disRev[i].first = disRev[i].second = 0; ` ` ` `} ` ` ` ` ` `int` `root = 0; ` ` ` ` ` `// dfs populates disRev data structure and ` ` ` `// store total reverse edge counts ` ` ` `int` `totalRev = dfs(g, disRev, visit, root); ` ` ` ` ` `// UnComment below lines to print each node's ` ` ` `// distance and edge reversal count from root node ` ` ` `/* ` ` ` `for (int i = 0; i < V; i++) ` ` ` `{ ` ` ` `cout << i << " : " << disRev[i].first ` ` ` `<< " " << disRev[i].second << endl; ` ` ` `} ` ` ` `*/` ` ` ` ` `int` `res = INT_MAX; ` ` ` ` ` `// loop over all nodes to choose minimum edge reversal ` ` ` `for` `(` `int` `i = 0; i < V; i++) ` ` ` `{ ` ` ` `// (reversal in path to i) + (reversal ` ` ` `// in all other tree parts) ` ` ` `int` `edgesToRev = (totalRev - disRev[i].second) + ` ` ` `(disRev[i].first - disRev[i].second); ` ` ` ` ` `// choose minimum among all values ` ` ` `if` `(edgesToRev < res) ` ` ` `{ ` ` ` `res = edgesToRev; ` ` ` `root = i; ` ` ` `} ` ` ` `} ` ` ` ` ` `// print the designated root and total ` ` ` `// edge reversal made ` ` ` `cout << root << ` `" "` `<< res << endl; ` `} ` ` ` `// Driver code to test above methods ` `int` `main() ` `{ ` ` ` `int` `edges[][2] = ` ` ` `{ ` ` ` `{0, 1}, ` ` ` `{2, 1}, ` ` ` `{3, 2}, ` ` ` `{3, 4}, ` ` ` `{5, 4}, ` ` ` `{5, 6}, ` ` ` `{7, 6} ` ` ` `}; ` ` ` `int` `e = ` `sizeof` `(edges) / ` `sizeof` `(edges[0]); ` ` ` ` ` `printMinEdgeReverseForRootNode(edges, e); ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

Output:

3 3

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 contribute@geeksforgeeks.org. 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.

## Recommended Posts:

- Maximum edge removal from tree to make even forest
- Remove all outgoing edges except edge with minimum weight
- Minimum Operations to make value of all vertices of the tree Zero
- Minimum edges required to add to make Euler Circuit
- Minimum edges to reverse to make path from a source to a destination
- Edge Coloring of a Graph
- Check if removing a given edge disconnects a graph
- Program to Calculate the Edge Cover of a Graph
- Delete Edge to minimize subtree sum difference
- Shortest Path in a weighted Graph where weight of an edge is 1 or 2
- Tree, Back, Edge and Cross Edges in DFS of Graph
- Paths to travel each nodes using each edge (Seven Bridges of Königsberg)
- Maximize number of nodes which are not part of any edge in a Graph
- Find maximum number of edge disjoint paths between two vertices
- Maximum Possible Edge Disjoint Spanning Tree From a Complete Graph