Proofs: Weighted union-find
These are the human proofs of Weighted union-find, at full length.
Weighted union-find keeps potentials that satisfy exactly the implied facts full
This proof shows: correctness, termination.
Fix an abelian group . Let be the set of facts accepted so far, that is, added by a union that returned true. Say that implies when every assignment that satisfies all facts of also satisfies this one. For each element with root , is the sum of along the path from to , excluding , and .
We prove that after each operation:
(T) The parent pointers form a forest with union by size, as in the disjoint-set forest, and at each root .
(P) Each fact of relates two elements of one tree and is satisfied by .
(I) For and in one tree, implies . For and in different trees, implies no fact about .
The forest part of (T) is proved on the disjoint-set forest page, because the parent pointers and sizes change exactly as there; the values do not affect them. In particular every find stops.
Initialization and MAKE-SET. A new element is a root with and no facts. Each tree of one element satisfies (P) and (I) trivially, and of every other element is unchanged.
Lemma 1 (FIND). FIND changes no potential, keeps (T), (P) and (I), and afterwards if is not a root.
Proof. By induction on the depth of . If , line L3 returns, nothing changes, and is the root. Let , with parent . By the induction hypothesis, FIND changes no potential and leaves if is not a root; if is the root, by (T). So before line L5, , because still points to . Line L5 sets to this value and line L6 points to the root, so the new potential of is $x.w + 0 = $ the old . The nodes of the subtree of reach the root through , and their sums up to do not change, so their potentials do not change either. No tree changes its node set, does not change, so (P) and (I) still hold. The Python form computes the same values with two loops: it collects the path, and from the node nearest the root down, it sets each value to its own value plus the new value of its parent, which is the parent's potential.
DIFF. By Lemma 1, after line L8 both finds leave and (a root has ). If the roots differ, (I) says fixes no difference, and NONE is right. Otherwise line L9 returns , which by (I) is the difference that implies.
UNION, same tree. After line L10, and . Let . By (I), implies , and by (P) satisfies . If , the new fact is implied by ; adding it changes no set of solutions, and satisfies it, so (P) and (I) hold. If , the new fact contradicts , because every solution of has the difference , and is a solution. Line L11 returns false and the fact is not added. Either way the answer is right.
UNION, different trees. The fact is the same as , so after the swap of line L12 we may assume that is in the tree with root and in the tree with root , with . Write for the potentials before line L13 and after.
Potentials after the link. Line L13 sets and , where . For , the path from now goes to and then to , so . For and for the other trees, nothing changes. The root keeps , so (T) holds.
(P). Old facts relate two elements of one old tree. Inside all potentials shift by the same , so differences are kept; the others do not change. For the new fact, .
(I), the new tree. Let . Take . If both are in , then implies , and so does ; the same holds in . If and , then every solution of has
which equals .
(I), different trees. Let and be in different trees after the union. We show that fixes no value of . Let the tree of be . For each , define for and otherwise. Each fact of relates two elements of one tree, by (P), so it is satisfied by , which shifts each tree by a constant. Then takes every value of as varies. So no fact about this difference is implied. The same argument, applied before the union, shows that the new fact relating and never contradicts , so returning true is right.
Line L14 updates the size of the new root, which is part of (T).
Conclusion. By induction on the operations, (T), (P) and (I) hold after each operation. So each union returns false exactly when its fact contradicts the accepted facts, and each DIFF returns the difference that the facts imply, or NONE when they imply none.