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 :

  1. Call DFS(G) to compute finishing times for each vertex .
  2. as each vertex is finished, insert it onto the front of a linked list.
  3. 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.