Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding
Ninjas X Naukri.com

Last Updated: 21 Dec, 2020

Moderate

```
1. It is guaranteed that the given graph is DAG.
2. There will be no multiple edges and self-loops in the given DAG.
3. There can be multiple correct solutions, you can find any one of them.
4. Don’t print anything, just return an array representing the topological sort of the vertices of the given DAG.
```

```
The first line of input contains an integer ‘T’ denoting the number of test cases. The description of ‘T’ test cases follows.
The first line of each test case contains two space-separated integers ‘V’, ‘E’, representing the number vertices and edges in the graph respectively.
Then ‘E’ lines follow, each containing 2 space-separated integers ‘u’, ‘v’ representing that there is a directed edge from vertex ‘u’ to vertex ‘v’
```

```
For each test case, return an array representing the topological sort of the vertices of the given DAG.
```

```
You do not need to print anything. It has already been taken care of. Just implement the given function.
```

```
1 <= T <= 50
1 <= V <= 10^4
0 <= E <= 10^4
0 <= u, v < V
Where ‘T’ is the total number of test cases, ‘V’ is the number of vertices, ‘E’ is the number of edges, and ‘u’ and ‘v’ both represent the vertex of a given graph.
Time limit: 2 sec
```

In the Depth First Search (DFS), we start from a vertex, we first print it and then recursively call DFS for its adjacent vertices. In topological sorting, we use a stack. We don’t print the vertex immediately, we first recursively call topological sorting for all its adjacent vertices, then push it to a stack. Finally, print contents of the stack. Note that a vertex is pushed to stack only when all of its adjacent vertices (and their adjacent vertices and so on) are already in the stack.

- This is a recursive approach.
- Make an adjacency list of the given graph.
- Make a boolean array ‘visited’ of size ‘V’ and initially fill it by ‘false’. This will keep track of the vertex that is already visited.
- Make a stack that will keep topological sorting of vertices in the top to bottom order.
- Run a loop where ‘i’ ranges from 0 to V-1. For each vertex ‘i’ which is not already visited, we will call a recursive function that will perform the following steps -:
- Mark the current vertex ‘i’ visited by setting ‘visited[i]’:= true.
- Iterate over all the adjacent vertices of ‘i’ that are not already visited and recursively call this function.
- Push vertex ‘i’ in the stack.

- Make a vector ‘result’ and push all the elements of the stack in it from top to bottom.
- Return the vector ‘result’, It will represent topological sorting of given DAG.

The Kahn’s Algorithm can be implemented as follow -:

- Make adjacency list of the given graph
- Make an integer array ‘indegree’ of size ‘V’ that will store indegree (number of incoming edges to the vertex) of all vertices.
- Iterate over the edges and compute indegree of all vertices.
- Make a queue and enqueue all vertices that have in-degree 0.
- Make a vector ‘result’ that will store topological sorting of the given DAG.
- Run a while loop till queue not become empty and in each iteration perform following steps -:
- Dequeue the front element of the queue, and push it into the ‘result vector.
- Decrease in-degree by 1 for all its neighbouring vertices.
- If the in-degree of a neighbouring vertex is reduced to zero, then add it to the queue.

- Return ‘result’.

Similar problems

Minimum Swaps To Make Identical Array

Moderate

Posted: 22 Jan, 2022

Find Center of Star Graph

Easy

Posted: 26 Jan, 2022

Critical Connections in a Network

Hard

Posted: 27 Jan, 2022

Critical Connections in a Network

Hard

Posted: 27 Jan, 2022

Critical Connections in a Network

Hard

Posted: 27 Jan, 2022

Critical Connections in a Network

Hard

Posted: 27 Jan, 2022

COUNT ISLANDS

Moderate

Posted: 14 Sep, 2022

Distance to a Cycle in Undirected Graph

Moderate

Posted: 7 Nov, 2022