Proofs: Connectivity by union-find
These are the human proofs of Connectivity by union-find, at full length.
Connectivity by union-find stops and labels the connected components full
This proof shows: correctness, termination.
Let and let be the edges in the order line L2 reads them. Self-loops and repeated edges are allowed. Let , and write when has a path from to . This is an equivalence relation, and its classes are the components of .
For a vertex , the chain of is . A vertex with is a root.
Lemma 1 (ranks). At every moment, each vertex that is not a root has . So the chain of each vertex reaches a root after at most steps, and the loops of lines L7 and L8 stop.
Proof. After line L1 every vertex is a root, so the claim holds. Only lines L8, L11 and L12 change or .
- Line L11 sets , where and are distinct roots and, after line L10, . If the claim holds for . If they are equal, line L12 raises by 1, and then .
- Line L12 raises only the rank of the root . For a child of , stays true. The ranks of non-roots never change, because lines L11 and L12 change only the ranks of roots and stops being a root after its rank is fixed.
- Line L8 sets , where is the root that line L7 found at the end of the chain of . Along the chain the ranks strictly increase, by the claim before the step, so .
So the claim holds at every moment. Along a chain the ranks strictly increase. A rank is at most , because it grows only in line L12, which runs at most once for each LINK, and there are at most LINK calls (each one makes a root into a non-root, and a non-root never becomes a root again). So a chain has at most vertices, and line L7 stops. Each step of line L8 moves one place forward on its own chain toward , so line L8 stops after at most steps.
Write for the root at the end of the chain of . Then returns , by line L7.
Lemma 2 (FIND keeps the roots). A call of FIND does not change for any vertex .
Proof. Line L8 changes only for vertices on the chain of the argument, all of which have , and it sets . Take any . If its chain does not meet a changed vertex, it is the same as before. Else let be the first changed vertex on it. Before the change, the chain of continued from to , and after it, it goes from to in one step. In both cases it ends at .
Lemma 3 (LINK merges two classes). Let be roots. After LINK, is the new parent root for each that had before, and is unchanged for every other .
Proof. After line L10 let the roots be named so that line L11 sets . This is the only change of a parent. A chain that ended at now continues to , which is still a root. A chain that ended at or at another root does not pass through , because was a root and a chain stops at its root, so it is unchanged.
Theorem. UF-COMPONENTS stops, and it returns with if and only if and are joined by a path in .
Proof. Every loop stops by Lemma 1, and the loops of lines L2 and L5 have and steps. We show by induction on the invariant
(I) After the first steps of the loop of line L2, if and only if .
Base. For , every vertex is its own root, and has no edges, so exactly when .
Step. Let , and assume (I) for . Line L3 computes and , and by Lemma 2 it does not change any .
First, we compare with . Each path of is a path of . A walk of that uses goes from one end of to the other at that place. So if and only if , or and , or and . In words: the classes of and in merge, and every other class stays as it is.
If , then by (I), the two classes are one, and equals . Line L4 does nothing, so (I) holds for . This case covers self-loops and repeated edges. If , the classes of and in are the sets and , by (I). Line L4 calls LINK, and by Lemma 3 exactly these two sets get one common root, while every other vertex keeps its root. This is the same merge as the one of , so (I) holds for .
End. After the loop, (I) holds with . Line L5 sets , and by Lemma 2 the calls of line L5 do not change any , so all labels are taken from the same root function. So if and only if .
Spanning forest. Let be the set of edges that caused a LINK. Each such edge joins two different classes of , so it closes no cycle with the earlier edges of , and is a forest. The same induction shows that and have the same classes after each step, so is a spanning forest of with edges, where is the number of components.