Given a Directed Acyclic Graph (DAG), find Topological Sort of the graph.
Topological sorting for Directed Acyclic Graph (DAG) is a linear ordering of vertices such that for every directed edge uv, vertex u comes before v in the ordering. Topological Sorting for a graph is not possible if the graph is not a DAG.
For example, a topological sorting of the following graph is “5 4 2 3 1 0”. There can be more than one topological sorting for a graph. For example, another topological sorting of the following graph is “4 5 2 3 1 0”.
Please note that the first vertex in topological sorting is always a vertex with in-degree as 0 (a vertex with no incoming edges). For above graph, vertex 4 and 5 have no incoming edges.
We have already discussed a DFS-based algorithm using stack and Kahn’s Algorithm for Topological Sorting. We have also discussed how to print all topological sorts of the DAG here. In this post, another DFS based approach is discussed for finding Topological sort of a graph by introducing concept of arrival and departure time of a vertex in DFS.
What is Arrival Time & Departure Time of Vertices in DFS?
In DFS, Arrival Time is the time at which the vertex was explored for the first time and Departure Time is the time at which we have explored all the neighbors of the vertex and we are ready to backtrack.
How to find Topological Sort of a graph using departure time?
To find Topological Sort of a graph, we run DFS starting from all unvisited vertices one by one. For any vertex, before exploring any of its neighbors, we note the arrival time of that vertex and after exploring all the neighbors of the vertex, we note its departure time. Please note only departure time is needed to find Topological Sort of a graph, so we can skip arrival time of vertex. Finally, after we have visited all the vertices of the graph, we print the vertices in order of their decreasing departure time which is our desired Topological Order of Vertices.
Below is C++ implementation of above idea –
Topological Sort of the given graph is 5 4 2 3 1 0
Time Complexity of above solution is O(V + E).
This article is contributed by Aditya Goel. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.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.
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.
- Detect cycle in Directed Graph using Topological Sort
- Check if incoming edges in a vertex of directed graph is equal to vertex itself or not
- Check if vertex X lies in subgraph of vertex Y for the given Graph
- Check if every vertex triplet in graph contains two vertices connected to third vertex
- All Topological Sorts of a Directed Acyclic Graph
- Maximum number of edges that N-vertex graph can have such that graph is Triangle free | Mantel's Theorem
- Finding minimum vertex cover size of a graph using binary search
- Find a Mother vertex in a Graph using Bit Masking
- Topological Sorting
- Kahn's algorithm for Topological Sorting
- Lexicographically Smallest Topological Ordering
- k'th heaviest adjacent node in a graph where each vertex has weight
- All vertex pairs connected with exactly k edges in a graph
- Add and Remove vertex in Adjacency Matrix representation of Graph
- Add and Remove vertex in Adjacency List representation of Graph
- Find dependencies of each Vertex in a Directed Graph
- Find a Mother Vertex in a Graph
- Find the Degree of a Particular vertex in a Graph
- Finding the path from one vertex to rest using BFS
- Graph implementation using STL for competitive programming | Set 2 (Weighted graph)