Proofs: Lempel-Even-Cederbaum st-numbering algorithm
These are the human proofs of Lempel-Even-Cederbaum st-numbering algorithm, at full length.
Correctness of the st-numbering algorithm full
This proof shows: correctness, termination.
Setting. is a finite undirected graph with vertices, , and the edge is treated as present. is the DFS tree of line L1, rooted at . Since the search from follows first and the whole search from ends before looks at another edge, is a child of , and every vertex reached later from is another child of . is the preorder number and is the vertex of smallest preorder among , the descendants of , and the ends of back edges from them.
Termination and cost. The search, the check and the loop each take finitely many steps, in total.
Line L2 is the right test
Lemma 1. Line L2 fails if and only if is disconnected or has a cut vertex. If it does not fail, then is the only child of , and is a proper ancestor of for every .
Proof. If some vertex is not reached, is disconnected. Otherwise spans , and by the standard characterization of cut vertices with low points (see the articulation point pages): the root is a cut vertex if and only if it has at least two children, and a non-root is a cut vertex if and only if it has a child with . If has a second child , then and is detected by line L2. So line L2 checks exactly both conditions. When it passes, , and is an ancestor of (the end of a back edge from the subtree of is an ancestor of in a DFS tree of an undirected graph, or lies in the subtree), so it is a proper ancestor of .
When is not 2-connected, no st-numbering exists. Suppose one exists, and let be any vertex. From every vertex there is a path of increasing numbers to and a path of decreasing numbers to , and these two paths meet only in , so at least one of them avoids (if or , take the other one). So every vertex other than reaches or in , and if neither nor is , the edge joins them. So is connected for every , and is connected; with this means 2-connected, and for the graph is . So failing is the right answer.
From now on assume that line L2 passes.
The list
The loop processes vertices in preorder, so is in when is inserted. Insertions never change the relative order of vertices already in .
Lemma 2 (no jumping). When is inserted next to , for every vertex in , the vertices and lie on the same side of , and they stay so forever.
Proof. is placed between and its neighbour in on one side, so no vertex lies between and at that moment. Relative order is never changed later.
For a vertex that has a child in , let be the child of inserted most recently.
Lemma 3 (sides of subtrees). At every moment, for every with a child in : all vertices of the subtree of that are in lie before if , and after if .
Proof. When is inserted, line L6 or L8 sets to if goes before and to if it goes after, and the subtree of in is . Afterwards, changes only when a new child of is inserted, which replaces . Every later vertex of the subtree of has its parent in that subtree, and ; by Lemma 2 it lands on the same side of as , which by induction is the side of .
Lemma 4 (the low point is opposite the parent). For every , after its insertion, lies on the other side of from .
Proof. Let , a proper ancestor of by Lemma 1. Then is in the subtree of the child of on the path to , and this child is at the time is inserted, because in preorder the subtree of a child is processed completely before the next child. By Lemma 3, if then lies after ; line L6 puts directly before , so lies before and after it. If , symmetrically lies before , line L8 puts directly after , and lies after .
Lemma 5. is first and is last in the final list.
Proof. A vertex is inserted before or after , and for by Lemma 1, so nothing is inserted before . A vertex is inserted after only if and . Then is a proper ancestor of , so it is . The sign of is set to at line L3 and changes only when a child of is inserted, which never happens since is its only child. So nothing is inserted after .
Main theorem
Theorem. If line L2 passes, the numbering of line L9 is an st-numbering of , and every vertex has neighbours in on both sides.
Proof. By Lemma 5, and . Let . The parent is a neighbour of in (the only tree edge not in might be , and ). We find a neighbour on the other side, which by Lemma 4 is the side of .
If has a back edge to , then is such a neighbour.
Otherwise is reached from a proper descendant of , so there is a child of with . Suppose when is inserted; then goes before and lies before . Between the insertions of and the loop inserts only descendants of (preorder), whose parents are or descendants of ; none of them is , because is a proper ancestor of . So is still when is inserted, and line L6 puts directly before . So lies on the side of , opposite . The case is symmetric. In both cases is a neighbour of in on the other side.
So every has a neighbour with a smaller number and one with a larger number.