Proofs: Incremental topological ordering algorithm
These are the human proofs of Incremental topological ordering algorithm, at full length.
Local reordering keeps a valid topological order and detects every cycle full
This proof shows: correctness.
The invariant is:
(V) The graph is acyclic, is a bijection from to , and for every edge .
It holds for the empty graph with any bijection. Assume (V) before ADD-EDGE, and write for the order before the call and for the order after it. The case is a loop, which is a cycle and is refused. Line L1 is correct: if , the new edge goes forward, the order stays valid, and the graph stays acyclic, since a cycle through would need a path from to , along which positions increase, so .
From now on . Write for the window of positions from to .
Lemma 1 (cycle test). Line L3 returns "cycle" if and only if the graph has a path from to , that is, if and only if the new edge closes a cycle.
Proof. If the search of line L2 finds an edge into , it found a path from to . Conversely, let be a path. Positions increase along it, so for . The search starts at and follows every edge out of a visited vertex to a vertex with position below , so by induction it visits , and at it examines the edge into .
So if line L3 does not return, the new graph is acyclic. Let be the set found by line L2 and the set found by line L4. Then:
- (F1) is the set of vertices with a path from to all of whose vertices have positions below . In particular, if and is an edge with , then , and by Lemma 1, so .
- (B1) Symmetrically, if and is an edge with , then .
- (D) , and there is no edge from to : a vertex in both, or such an edge, would give a path from to , which Lemma 1 rules out.
- (W) All positions of lie in : positions increase along paths from , so has positions in , and positions decrease backward from , so has positions in .
Lemma 2 (monotone moves). After line L6, for every and for every . All new positions of lie in , every other vertex keeps its position, and is a bijection.
Proof. Line L6 gives the positions , which are exactly the old positions of , back to the same vertices in a new arrangement, so is a bijection, other vertices are untouched, and new positions lie in by (W). Let be the -th smallest vertex of by old position, at old position . It gets the -th smallest element of . Fewer than elements of that belong to are below , and at most that belong to , so is at most the -th smallest element of , and . The claim for is symmetric: the -th smallest vertex of gets the -th smallest element of , and its old position is at least that.
Lemma 3. After line L6, every edge of the new graph has .
Proof. We go through the cases by the groups of and . Let .
- : both positions are unchanged.
- , or : line L6 keeps the relative order inside each group.
- , : all of gets smaller positions than all of . This includes the new edge, since and .
- , : impossible by (D).
- , : by (F1), , which is above every position of , and .
- , : by (B1), , below every position of , and .
- , : by Lemma 2.
- , : by Lemma 2.
Theorem. After every call of ADD-EDGE, (V) holds; the call refuses the edge exactly when it would close a cycle; and ORDER returns a topological order of the current graph.
Proof. By induction on the calls. A refused edge leaves the graph and the order unchanged, and by Lemma 1 it is refused exactly when it closes a cycle. An accepted edge in line L1 keeps (V) as shown above. Otherwise, by Lemma 1 the new graph is acyclic, and by Lemmas 2 and 3 is a bijection under which every edge goes forward. So (V) holds, and line L7 lists the vertices by , which is a topological order.