Given a Directed Graph with **N** vertices value from **0 to N – 1** and **N – 1** Edges, the task is to count the number of edges must be reversed so that there is always a path from every node to **node 0**.

**Examples:**

**Approach:** The idea is to use BFS Traversal for a graph. Below are the steps:

- Create a directed graph with the direction of the edges of the given graph reversed.
- Create a queue and push
**node 0**in the queue. - During BFS Traversal on the graph do the following:
- Pop the front node(say
**current_node**) from the queue. - Traverse the Adjacency List of current node in the reverse graph and push in those node in the queue that are not visited.
- Traverse the Adjacency List of current node in the reverse graph and push in those node in the queue that are not visited.
- The total nodes inserted in the queue in the above steps is required count of edges to be reversed because the nodes which are connected to the current node and have not been visited yet in the graph cannot be reached to
**node 0**, therefore, we need to reverse their direction. Add the count of the nodes in the above step to the final count.

- Pop the front node(say

Below is the implementation of the above approach:

`// CPP program for the above approach ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Function to find minimum reversals ` `int` `minRev(vector<vector<` `int` `> > edges, ` ` ` `int` `n) ` `{ ` ` ` ` ` `// Add all adjacent nodes to ` ` ` `// the node in the graph ` ` ` `unordered_map<` `int` `, vector<` `int` `> > graph; ` ` ` `unordered_map<` `int` `, vector<` `int` `> > graph_rev; ` ` ` ` ` `for` `(` `int` `i = 0; ` ` ` `i < edges.size(); i++) { ` ` ` ` ` `int` `x = edges[i][0]; ` ` ` `int` `y = edges[i][1]; ` ` ` ` ` `// Insert edges in the graph ` ` ` `graph[x].push_back(y); ` ` ` ` ` `// Insert edges in the ` ` ` `// reversed graph ` ` ` `graph_rev[y].push_back(x); ` ` ` `} ` ` ` ` ` `queue<` `int` `> q; ` ` ` ` ` `// Create array visited to mark ` ` ` `// all the visited nodes ` ` ` `vector<` `int` `> visited(n, 0); ` ` ` `q.push(0); ` ` ` ` ` `// Stores the number of ` ` ` `// edges to be reversed ` ` ` `int` `ans = 0; ` ` ` ` ` `// BFS Traversal ` ` ` `while` `(!q.empty()) { ` ` ` ` ` `// Pop the current node ` ` ` `// from the queue ` ` ` `int` `curr = q.front(); ` ` ` ` ` `// mark the current ` ` ` `// node visited ` ` ` `visited[curr] = 1; ` ` ` ` ` `// Intitialize count of edges ` ` ` `// need to be reversed to 0 ` ` ` `int` `count = 0; ` ` ` `q.pop(); ` ` ` ` ` `// Push adjacent nodes in the ` ` ` `// reversed graph to the queue, ` ` ` `// if not visited ` ` ` `for` `(` `int` `i = 0; ` ` ` `i < graph_rev[curr].size(); ` ` ` `i++) { ` ` ` ` ` `if` `(!visited[graph_rev[curr][i]]) { ` ` ` `q.push(graph_rev[curr][i]); ` ` ` `} ` ` ` `} ` ` ` ` ` `// Push adjacent nodes in graph ` ` ` `// to the queue, if not visited ` ` ` `// count the number of ` ` ` `// nodes added to the queue ` ` ` `for` `(` `int` `i = 0; ` ` ` `i < graph[curr].size(); ` ` ` `i++) { ` ` ` ` ` `if` `(!visited[graph[curr][i]]) { ` ` ` `q.push(graph[curr][i]); ` ` ` `count++; ` ` ` `} ` ` ` `} ` ` ` ` ` `// Update the reverse edge ` ` ` `// to the final count ` ` ` `ans += count; ` ` ` `} ` ` ` ` ` `// Return the result ` ` ` `return` `ans; ` `} ` ` ` `// Driver Code ` `int` `main() ` `{ ` ` ` `vector<vector<` `int` `> > edges; ` ` ` ` ` `// Given edges to the graph ` ` ` `edges = { { 0, 1 }, { 1, 3 }, { 2, 3 }, ` ` ` `{ 4, 0 }, { 4, 5 } }; ` ` ` ` ` `// Number of nodes ` ` ` `int` `n = 6; ` ` ` ` ` `// Function Call ` ` ` `cout << minRev(edges, n); ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

**Output:**

3

**Time Complexity:** *O(V+E)* where V is the number of vertices, E is the number of edges.

**Auxiliary Space:** *O(V)* where V is the number of vertices.

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.

## Recommended Posts:

- Minimum cost path from source node to destination node via an intermediate node
- Minimum edges to be added in a directed graph so that any node can be reachable from a given node
- Minimum number of prefix reversals to sort permutation of first N numbers
- Kth largest node among all directly connected nodes to the given node in an undirected graph
- Minimum edge reversals to make a root
- Find the node whose xor with x gives minimum value
- Level of Each node in a Tree from source node (using BFS)
- Find farthest node from each node in Tree
- Check whether the cost of going from any node to any other node via all possible paths is same
- Farthest distance of a Node from each Node of a Tree
- Find the node whose sum with X has minimum set bits
- Find the node whose absolute difference with X gives minimum value
- Minimum value of distance of farthest node in a Graph
- Find the minimum number of moves to reach end of the array
- Find minimum number of steps to reach the end of String
- Minimum labelled node to be removed from undirected Graph such that there is no cycle
- Number of loops of size k starting from a specific node
- Maximum number of nodes which can be reached from each node in a graph.
- Queries for the number of nodes having values less than V in the subtree of a Node
- Number of ways to select a node from each connected component

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 Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.