Proofs: Union-find with rollback
These are the human proofs of Union-find with rollback, at full length.
Union-find with rollback restores the earlier forest on each undo full
This proof shows: correctness, termination.
For a node of a forest, is the set of nodes whose path to the root passes through , including . The sets of the structure are the sets of the roots . We prove that after each operation the invariants (S) and (H) of the page hold:
(S) The parent pointers form a forest, for each root , and for each non-root .
(H) Let the stack be . There are forests , with the forest of all elements as roots and the current forest, such that for each : if then , and if then and are roots of with , and is with and . Each satisfies (S).
Here MAKE-SET adds a root to all of , as if the element had been made at the start; this keeps every statement, because the new element is a root in each of them and appears in no record.
Depth bound. Under (S), if has depth , then the sizes along its path at least double at each step, so and . So each FIND stops after at most steps.
MAKE-SET. It adds a root with size 1 and , so (S) holds, and (H) holds as explained above.
FIND. Lines L2 and L3 write nothing, and by (S) they return the root of the tree of after at most steps. So connected is right, and (S) and (H) still hold.
UNION. Line L4 replaces and by their roots. If they are equal, line L5 pushes NONE and changes nothing, so (H) holds with , and (S) holds. Otherwise, after line L6, and are different roots with . Line L7 pushes and line L8 sets and adds the size, which is exactly the change that (H) describes for . We check (S) for the new forest. The parent pointers form a forest, because was a root of another tree, so no cycle is made. The new root has $|T(y)| = $ old , which line L8 stores. The new edge satisfies . For other non-roots , does not change, and grows or stays the same, so the inequality still holds. The sets and merge into one set, which is the effect that the disjoint-set type requires.
UNDO. Let and pop . If it is NONE, then is the current forest, and (H) holds for the shorter stack with the same forests . If it is , then by (H) the current forest is with changed from to and increased by . The value in equals its value in : sizes change only at the root that receives a link, and is not a root in . So line L13, which subtracts from and sets , turns into exactly. Then the current forest is , and (H) holds for the stack with the forests . By (H), satisfies (S). The sets are those of , the state before the undone union, which is the effect that undo must have.
SNAPSHOT and ROLLBACK. A snapshot returns . Later operations push and pop, and as long as at least records remain, the first records and the forests are the ones of the time of the snapshot, because a push or pop changes only the top of . So ROLLBACK, which undoes until , ends in the forest of the snapshot, by the UNDO case applied times.
Conclusion. By induction on the operations, (S) and (H) hold after each operation. So every find stops and returns the right root, each union merges the right sets, and each undo restores the forest and the sets as they were before the undone union.