Proofs: Offline dynamic connectivity
These are the human proofs of Offline dynamic connectivity, at full length.
The walk over the segment tree answers every query on the graph of its time full
This proof shows: correctness.
Let be the multigraph of the edges present after the operations . For a node of the segment tree, let be its range, let be its list of edge copies, and let be the set of nodes on the path from the root to , including .
Lemma 1 (lifetimes). A copy of an edge with lifetime is present in if and only if .
Proof. Line L1 pairs each removal of with the latest open start time of a copy of , and a copy that is never removed gets the end . The copy is inserted by , so it is in for , and it is deleted by , so it is not in for . Since all copies of one edge are equal, the choice of which copy a removal deletes does not change the multigraph .
Lemma 2 (canonical nodes). For a lifetime and a leaf of time , exactly one canonical node of is in if , and none is if .
Proof. Line L2 puts the copy at a set of nodes whose ranges are pairwise disjoint and have the union ; this is the property of the standard segment tree decomposition. The ranges of the nodes in are exactly the ranges of the tree that contain . If , some node of has a range that contains , and only one does, since the ranges of are disjoint. If , no range of contains .
So the multiset is the edge multiset of , by Lemmas 1 and 2.
Lemma 3 (the walk). When VISIT starts, the union-find is in the state reached from singletons by the unions of the edges in . When VISIT ends, is in the same state as when it started.
Proof. By induction on the height of . Let be the state when VISIT starts, and let be the log height saved in line L3. Line L4 applies the unions of , so is now the state of the unions of the edges of . If is internal, VISIT of the left child starts in this state, as the claim for the child needs, and by the induction hypothesis it ends in the same state; the same holds for the right child. Line L7 then undoes every union recorded after height . Each union records the two fields it wrote, and the undo restores them in the reverse order, so by the correctness of the rollback technique the state returns to . At the root, the start state is singletons, which matches the empty set of ancestors.
Theorem. For each time whose operation is a query, the answer at the leaf of is the answer for .
Proof. By Lemma 3, after line L4 at the leaf of , the union-find holds the unions of the edges in , which is the edge multiset of . The sets of a union-find after the unions of an edge set are the connected components of the graph of those edges: two vertices are in one set if and only if a chain of the unioned edges joins them, and union by size changes only the shape of the trees, not the sets. So if and only if and are connected in . The component counter starts at , decreases by one exactly at each union that merges two sets, and is restored by each undo, so it equals the number of components of . Line L5 reads these values. The leaves are visited in the order of time, left before right, so the answers come out in the order of the queries.