ClassS04CS141 | recent changes | Preferences

Topological sort by DFS

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.


ClassS04CS141 | recent changes | Preferences
This page is read-only | View other revisions
Last edited May 4, 2004 9:17 pm by Neal (diff)