Algorithm Search⌘K

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, pos\mathrm{pos} is a bijection from VV to {0,…,n−1}\{0, \ldots, n - 1\}, and pos(x)<pos(y)\mathrm{pos}(x) < \mathrm{pos}(y) for every edge (x,y)(x, y).

It holds for the empty graph with any bijection. Assume (V) before ADD-EDGE(u,v)(u, v), and write pos\mathrm{pos} for the order before the call and pos′\mathrm{pos}' for the order after it. The case u=vu = v is a loop, which is a cycle and is refused. Line L1 is correct: if pos(u)<pos(v)\mathrm{pos}(u) < \mathrm{pos}(v), the new edge goes forward, the order stays valid, and the graph stays acyclic, since a cycle through (u,v)(u, v) would need a path from vv to uu, along which positions increase, so pos(v)<pos(u)\mathrm{pos}(v) < \mathrm{pos}(u).

From now on pos(v)<pos(u)\mathrm{pos}(v) < \mathrm{pos}(u). Write WW for the window of positions from pos(v)\mathrm{pos}(v) to pos(u)\mathrm{pos}(u).

Lemma 1 (cycle test). Line L3 returns "cycle" if and only if the graph has a path from vv to uu, that is, if and only if the new edge closes a cycle.

Proof. If the search of line L2 finds an edge into uu, it found a path from vv to uu. Conversely, let v=x0,x1,…,xk=uv = x_0, x_1, \ldots, x_k = u be a path. Positions increase along it, so pos(xt)<pos(u)\mathrm{pos}(x_t) < \mathrm{pos}(u) for t<kt < k. The search starts at vv and follows every edge out of a visited vertex to a vertex with position below pos(u)\mathrm{pos}(u), so by induction it visits x0,…,xk−1x_0, \ldots, x_{k-1}, and at xk−1x_{k-1} it examines the edge into uu. □\square

So if line L3 does not return, the new graph is acyclic. Let FF be the set found by line L2 and BB the set found by line L4. Then:

  • (F1) FF is the set of vertices yy with a path from vv to yy all of whose vertices have positions below pos(u)\mathrm{pos}(u). In particular, if f∈Ff \in F and (f,y)(f, y) is an edge with y∉Fy \notin F, then pos(y)≥pos(u)\mathrm{pos}(y) \ge \mathrm{pos}(u), and y≠uy \ne u by Lemma 1, so pos(y)>pos(u)\mathrm{pos}(y) > \mathrm{pos}(u).
  • (B1) Symmetrically, if b∈Bb \in B and (x,b)(x, b) is an edge with x∉Bx \notin B, then pos(x)<pos(v)\mathrm{pos}(x) < \mathrm{pos}(v).
  • (D) B∩F=∅B \cap F = \emptyset, and there is no edge from FF to BB: a vertex in both, or such an edge, would give a path from vv to uu, which Lemma 1 rules out.
  • (W) All positions of B∪FB \cup F lie in WW: positions increase along paths from vv, so FF has positions in WW, and positions decrease backward from uu, so BB has positions in WW.

Lemma 2 (monotone moves). After line L6, pos′(b)≤pos(b)\mathrm{pos}'(b) \le \mathrm{pos}(b) for every b∈Bb \in B and pos′(f)≥pos(f)\mathrm{pos}'(f) \ge \mathrm{pos}(f) for every f∈Ff \in F. All new positions of B∪FB \cup F lie in WW, every other vertex keeps its position, and pos′\mathrm{pos}' is a bijection.

Proof. Line L6 gives the positions PP, which are exactly the old positions of B∪FB \cup F, back to the same vertices in a new arrangement, so pos′\mathrm{pos}' is a bijection, other vertices are untouched, and new positions lie in WW by (W). Let ff be the kk-th smallest vertex of FF by old position, at old position aa. It gets the (∣B∣+k)(|B| + k)-th smallest element of PP. Fewer than kk elements of PP that belong to FF are below aa, and at most ∣B∣|B| that belong to BB, so aa is at most the (∣B∣+k)(|B| + k)-th smallest element of PP, and pos′(f)≥a\mathrm{pos}'(f) \ge a. The claim for BB is symmetric: the kk-th smallest vertex of BB gets the kk-th smallest element of PP, and its old position is at least that. □\square

Lemma 3. After line L6, every edge (x,y)(x, y) of the new graph has pos′(x)<pos′(y)\mathrm{pos}'(x) < \mathrm{pos}'(y).

Proof. We go through the cases by the groups of xx and yy. Let O=V∖(B∪F)O = V \setminus (B \cup F).

  • x,y∈Ox, y \in O: both positions are unchanged.
  • x,y∈Fx, y \in F, or x,y∈Bx, y \in B: line L6 keeps the relative order inside each group.
  • x∈Bx \in B, y∈Fy \in F: all of BB gets smaller positions than all of FF. This includes the new edge, since u∈Bu \in B and v∈Fv \in F.
  • x∈Fx \in F, y∈By \in B: impossible by (D).
  • x∈Fx \in F, y∈Oy \in O: by (F1), pos(y)>pos(u)\mathrm{pos}(y) > \mathrm{pos}(u), which is above every position of WW, and pos′(x)∈W\mathrm{pos}'(x) \in W.
  • x∈Ox \in O, y∈By \in B: by (B1), pos(x)<pos(v)\mathrm{pos}(x) < \mathrm{pos}(v), below every position of WW, and pos′(y)∈W\mathrm{pos}'(y) \in W.
  • x∈Ox \in O, y∈Fy \in F: pos′(x)=pos(x)<pos(y)≤pos′(y)\mathrm{pos}'(x) = \mathrm{pos}(x) < \mathrm{pos}(y) \le \mathrm{pos}'(y) by Lemma 2.
  • x∈Bx \in B, y∈Oy \in O: pos′(x)≤pos(x)<pos(y)=pos′(y)\mathrm{pos}'(x) \le \mathrm{pos}(x) < \mathrm{pos}(y) = \mathrm{pos}'(y) by Lemma 2. □\square

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 pos′\mathrm{pos}' is a bijection under which every edge goes forward. So (V) holds, and line L7 lists the vertices by pos\mathrm{pos}, which is a topological order. ■\blacksquare

Back to Incremental topological ordering algorithm