Given a directed graph with n nodes from 0 to n-1, a 2d array edges[][] where edges[i] = [ui, vi] represents a directed edge going from node ui to node vi . The graph would form a tree if its edges were bi-directional, the task is to find the minimum edge direction reversals required for each node such that there is a directed path from that node to all other nodes. Return the array where ith index represents the minimum edge reversals required from that particular to reach all other nodes.
For node 0 the edge between the 1 and 2 should be reverse to reach every node so 2.
For node 1 already all nodes are reachable from node 1 so 0.
For node 2 the edge between the 2 and 1 should be reversed to reach every node so 1.
Naive Approach: The basic way to solve the problem is as follows:
In this approach the problem can be solve by counting the number of edges to reversed for each node , first for a edge u->v the reversal required is 0 but for from v to u it requires 1 reversal during initializing the graph for each mark the reversal for opposite direction to 1 and the same direction to 0 using a dp array , then do DFS for each node to all other nodes and then keep adding the dp[node] then the dp array will be our answer.
Time Complexity: O(n*n) where n is the number of nodes Auxiliary Space: O(n) where n is the number of nodes
Efficient approach: To solve the problem follow the below idea:
In this approach we only compute the total reversals required for one node to reach all other nodes by maintaing another array also for number of reversals required to reach some node x from the root node and also maintaining another array for storing the depth of each node from root node. From only one DFS call we maintain these arrays and the total reversals required for the root node reach all the nodes.
Using the depth we can find the reversals required in reverse direction for example the number of revesals from node x to node y is m ,the reversals required for going from node y to node x is depth from root - m. Using this logic we can compute the reversals required to discover all other nodes using one DFS only for root node and maintaing the depth and reversal required to reach all other nodes.
Like the final equation to find the reversals required to reach node x to all other nodes is :
Suppose the reversals required to for root node to all other nodes is totalReversals , the depth of node x from root node is d and number of reversals is r .
Since we know using the DFS we have computed the totalReversals from root node , suppose from any node x using (d-r) reversals we can reach root node from root node we can reach all other nodes by using the totalReversals - r (that is excluding the number of reversals required to reach node x )
So the reversals for any node x to reach all other nodes , the answer be like :
depth - reversals required to reach node x from root + total reversals for root node - number of reversals to reach node x from root
Follow these steps to solve the above approach:
Initialize the res array to store the result, depth array to store the depth of root i from the root node, visited array to keep track of visited nodes,edgeRev array to keep track of number of reversals required to reach node i from the root node, graph , and totalReversals to 0.
Construct the graph along with the reversal count which is 0 for u-> v and 1 for v-> u.
Start the DFS from the node 0 and compute the depth, edgeRev arrays and the totalReversals required for node 0 to reach all the node.
After the DFS ,assign the total reversals to the root node 0.
For each node compute the total reversals by iterating from 0 to n.
Count the reversals from the node i to root, Count the reversals from the root i to remaining nodes and add them and assing to res[i].
Return the result.
Implementation of the above approach:
Output
1 1 0 2
Time Complexity: O(n) where n is the number of nodes. Auxiliary Space: O(n) where n is the number of nodes.