Given a directed, acyclic graph, a topological ordering of the vertices is an ordering of the vertices v1,v2,…,vn such that every edge (vi, vj) in the graph goes forward in the ordering (that is, i < j).
We defined the DFS post-order number of the vertices of the digraph to be the numbering that numbers the vertices in the order in which DFS finishes exploring them.
In a DAG, the DFS produces no back edges (see CyclesByDFS and DFSInDiGraphs).
For any edge (U,W), it must be that the DFS finishes exploring W before it finishes exploring U. Thus, the post-order numbering of the vertices, reversed, gives a topological ordering of the vertices.
Thus, DFS can be used to do topological sorting in linear time.
* GoodrichAndTomassia section 6.4.4 describes a different linear-time algorithm