A topological sort of a directed acyclic graph is a linear ordering of all its vertices such that if , then appears before in the ordering. We can use DFS to find a topological sort of a directed acyclic graph . Steps with precondition DAG :
- Call DFS(G) to compute finishing times for each vertex .
- as each vertex is finished, insert it onto the front of a linked list.
- return the linked list of vertices.
A directed graph is acyclic a depth-first search of never produces a back edge.
The topological sort algorithm take time.