Algorithm Search⌘K

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 G=(V,E)G = (V, E) be a directed graph with nn vertices. We say that VISIT(u)(u) 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 uu 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 g1,…,gkg_1, \ldots, g_k in which gi+1g_{i+1} was visited from the loop of gig_i through the edge (gi,gi+1)(g_i, g_{i+1}).

Proof. Line L8 only reads colours and may stop the run, and line L11 only changes LL. 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. □\square

Lemma 2 (stop means cycle). If line L8 stops the run while VISIT(u)(u) scans the edge (u,v)(u, v), then GG has a directed cycle.

Proof. The vertex vv is gray, so by Lemma 1 it is one of g1,…,gkg_1, \ldots, g_k, and u=gku = g_k is the top of the gray path, because the call of uu is the one that runs. If v=giv = g_i, then gi,gi+1,…,gkg_i, g_{i+1}, \ldots, g_k are joined by edges, and (gk,gi)=(u,v)(g_k, g_i) = (u, v) is an edge. This is a directed cycle. If i=ki = k, it is the self-loop (u,u)(u, u). □\square

Lemma 3 (cycle means stop). If GG 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 GG, so it visits each vertex. Let CC be a directed cycle and let xx be the vertex of CC whose visit starts first, and let ww be the vertex before xx on CC, so (w,x)∈E(w, x) \in E. Just before VISIT(x)(x) starts, all the vertices of CC are white, so the path along CC from xx to ww is white. By the white-path theorem, applied to this DFS, ww is a descendant of xx. So the visit of ww is active while the visit of xx is active. The visit of ww scans the edge (w,x)(w, x) before it ends, and at that moment xx is gray. Then line L8 stops the run. This contradicts the assumption. (If w=xw = x, the edge is a self-loop, and the scan of xx finds xx gray at once.) □\square

Lemma 4 (edges in LL). If the run does not stop, then for each edge (u,v)∈E(u, v) \in E, line L11 runs for vv before it runs for uu.

Proof. Look at the moment when VISIT(u)(u) scans (u,v)(u, v) at line L7. The run does not stop, so vv is not gray at line L8. If vv is black, line L11 has already run for vv, because line L11 directly follows line L10. Line L11 for uu comes later, after the loop of uu ends. If vv is white, line L9 calls VISIT(v)(v), and that call ends, with line L11 for vv, before the loop of uu goes on. So again line L11 for vv runs first. □\square

Theorem. TOPOLOGICAL-SORT terminates. If GG is acyclic, it returns a topological ordering of GG. If GG 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 GG has a directed cycle, Lemma 3 shows that the run stops at line L8 and reports a cycle.

If GG 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 LL exactly one time. So LL holds each vertex once, and the vertex inserted last is first. For each edge (u,v)(u, v), Lemma 4 says that vv is inserted before uu, so uu is in front of vv in the returned list. So LL 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 LL lists the vertices in decreasing order of ff. ■\blacksquare

Back to Topological sort