Proofs: Topological sort
These are the human proofs of Topological sort, at full length.
DFS topological sort returns a topological ordering or finds a cycle full
This proof shows: correctness, termination.
Let be a directed graph with vertices. We say that VISIT is active from line L6 to line L11. A call that starts while another call is active ends before that call ends, as on any call stack.
Lemma 1 (DFS structure). Up to the moment the run stops, TOPOLOGICAL-SORT is the procedure DFS of the depth-first search page without the clock and the parents, with line L6 as the discovery of and lines L10 and L11 as its finish. So each vertex is visited at most once, a vertex is white before its visit, gray while it is active and black after it, and the gray vertices at each moment form a path in which was visited from the loop of through the edge .
Proof. Line L8 only reads colours and may stop the run, and line L11 only changes . Removing both gives DFS without the clock and the parents. The facts are Lemmas 1 and 2 of the correctness proof of DFS, which hold for each prefix of the run.
Lemma 2 (stop means cycle). If line L8 stops the run while VISIT scans the edge , then has a directed cycle.
Proof. The vertex is gray, so by Lemma 1 it is one of , and is the top of the gray path, because the call of is the one that runs. If , then are joined by edges, and is an edge. This is a directed cycle. If , it is the self-loop .
Lemma 3 (cycle means stop). If has a directed cycle, line L8 stops the run.
Proof. Assume that the run does not stop. Then by Lemma 1 it is a full DFS of , so it visits each vertex. Let be a directed cycle and let be the vertex of whose visit starts first, and let be the vertex before on , so . Just before VISIT starts, all the vertices of are white, so the path along from to is white. By the white-path theorem, applied to this DFS, is a descendant of . So the visit of is active while the visit of is active. The visit of scans the edge before it ends, and at that moment is gray. Then line L8 stops the run. This contradicts the assumption. (If , the edge is a self-loop, and the scan of finds gray at once.)
Lemma 4 (edges in ). If the run does not stop, then for each edge , line L11 runs for before it runs for .
Proof. Look at the moment when VISIT scans at line L7. The run does not stop, so is not gray at line L8. If is black, line L11 has already run for , because line L11 directly follows line L10. Line L11 for comes later, after the loop of ends. If is white, line L9 calls VISIT, and that call ends, with line L11 for , before the loop of goes on. So again line L11 for runs first.
Theorem. TOPOLOGICAL-SORT terminates. If is acyclic, it returns a topological ordering of . If has a directed cycle, it reports a cycle.
Proof. Termination: each vertex is visited at most once, each visit runs a finite loop, and the loop of line L3 is finite.
If has a directed cycle, Lemma 3 shows that the run stops at line L8 and reports a cycle.
If is acyclic, Lemma 2 shows that line L8 never stops the run. By Lemma 1 the run is a full DFS, so each vertex is visited exactly one time, and line L11 inserts it at the front of exactly one time. So holds each vertex once, and the vertex inserted last is first. For each edge , Lemma 4 says that is inserted before , so is in front of in the returned list. So is a topological ordering.
The two cases cover all graphs, and the report is correct in each, so the procedure is correct. In the acyclic case, line L11 runs in increasing order of the finish times of the DFS, so lists the vertices in decreasing order of .