Topological Sorting
Topological sorting is an ordering of the vertices of a directed graph such that if there is an edge a->b
between vertices a and b, then a appears before b in the ordering. Consider the graph below:
One possible ordering is [4,1,5,2,3,6]
For a directed graph to have a topological ordering, it should not contain any cycle.
We can make use of DFS traversal to both check if the directed graph contains a cycle and, if doesn’t, to construct a topological sort.
Pseudo code for topological sort:
References: GeeksforGeeks