Proofs: Online bridge finding
These are the human proofs of Online bridge finding, at full length.
Online bridge finding keeps the 2-edge-connected components and the bridge forest full
This proof shows: correctness.
Let be the current multigraph. An edge is a bridge if it lies on no cycle, which is the same as: removing it increases the number of connected components. Two vertices are 2-edge-connected if they are equal or joined by two edge-disjoint paths; this is an equivalence relation, and its classes are the 2-edge-connected components. The edges between different classes are exactly the bridges, and contracting each class gives a forest , the bridge forest, whose edges are the bridges [standard facts; see the page on two-edge-connected components].
Invariant (B). (1) The sets of are the 2-edge-connected components of . (2) The representatives with their pointers form a rooted forest isomorphic to . (3) The sets of are the connected components of , each with its number of vertices, and every vertex of a tree of the forest finds the same -root. (4) The counter equals the number of bridges of .
At the start has no edges, every vertex is its own component and its own tree, and there are no bridges, so (B) holds. Assume (B) before ADD-EDGE, and let , , .
Case 1: (line L1). Then and are 2-edge-connected in . Adding an edge between them creates no new connection between classes and adds no cycle through a bridge: a cycle in through a bridge of would use the new edge, and replacing the new edge by a path inside the class of that avoids (the class has no bridges inside it, and is not inside it) gives a closed walk in through once, so a cycle through in , a contradiction. So the classes and the bridges are unchanged, and nothing changes.
Case 2: and different connected components (lines L3 to L5). The new edge joins two components of , so it lies on no cycle of : a cycle through it would need another path between the two components. So it is a bridge of . Every old bridge stays a bridge, because a cycle of that uses an old edge cannot use the new bridge, so it would be a cycle of . Every class is unchanged, because the new edge adds no edge-disjoint pair of paths between any two vertices. So is plus the edge between the classes and , and the counter is right after line L3. REROOT (line L9) reverses the pointers on the path from to the root of its tree: this is the same tree rooted at , so (B2) still holds with as the root. Setting hangs that tree below , which adds exactly the new bridge to the forest. The union in merges the two connected components and adds their sizes. In the implementation, REROOT also points the -entries of the path and the old -root at , so all vertices of the rerooted tree reach the root , and then points to ; this keeps (B3).
Case 3: in the same connected component (line L6). Then and are in one tree of the forest by (B2) and (B3). Let be the path in , through the lowest common ancestor , and let be its bridges.
Line L7 finds . The climbs visit the ancestors of and of in order of distance. A component is marked twice only if it is an ancestor of both, and the first common ancestor reached by either climb is : a climb reaches before any higher common ancestor, and when one climb reaches a component already marked by the other, that component is a common ancestor; the first such is the lowest one on the climbing side, which is , since every proper ancestor of is above on both sides.
The new classes. Each bridge lies on a cycle of : take the new edge, then a path in from to that crosses the classes of in order and uses each once (it exists because each class is connected). So no is a bridge of , and the classes become one 2-edge-connected class of : any two of their vertices are joined by two edge-disjoint paths, around the new cycle and inside the classes. Every other bridge of is not on , so it still separates into two parts, one of which contains all of ; any cycle of through would use the new edge, whose ends are both in the other part, so it would cross twice, which a cycle cannot. So stays a bridge, and the other classes do not change.
Line L8 unions exactly the classes on into the class of and decreases the counter once per class merged, that is, times, once per bridge . So (B1) and (B4) hold. In , the merged class is adjacent to the parent of and to the children of the classes of that are not on . The merged representative takes the parent of , and the children of the other classes of still point to vertices of those classes, which now find the merged representative. So (B2) holds. The connected components do not change, and the -pointers still lead to the same root, so (B3) holds.
Queries. By (B1), two vertices are 2-edge-connected if and only if gives the same representative, and an inserted edge is a bridge if and only if its ends are in different classes. By (B3) and (B4), the connectivity queries and the bridge count are right.