Proofs: Linked-list disjoint sets
These are the human proofs of Linked-list disjoint sets, at full length.
With weighted union, m operations on n elements take O(m + n log n) time full
This proof shows: time.amortized, time.total.
Consider a sequence of operations MAKE-SET, FIND and UNION, of which are MAKE-SET, on linked-list disjoint sets with the weighted-union rule of line L6. We prove that the sequence takes time.
Cost of each operation. MAKE-SET and FIND do a constant number of steps. A UNION with does a constant number of steps. A UNION with does a constant number of steps outside line L7, and line L7 does one step for each element of the list of the smaller set , each of which gets a new value of set. So the total time is , where is the total number of times that some element gets a new value of set at line L7.
Each element moves at most times. Fix an element . Let be the size of the set of ; it never falls, because sets only merge. Suppose moves at line L7 of some union. Then is in the set after line L6, so , and after the union is in a set of size . So each move of at least doubles . Before its first move , so after moves . A set has at most elements, so , and .
Sum. Each of the elements moves at most times, so , and the total time is . Since , this is at most per operation on average, which gives the amortized bound: with the accounting method, charge to each MAKE-SET and to each other operation; each move at line L7 is paid by one unit of the element that moves, and no element runs out.
The bound is tight. Let , and do the unions in rounds: in round , pair up the sets of size and merge each pair. Each union of two sets of size moves elements, and there are such unions, so each round moves elements, and the rounds move . With operations, the total is .
Linked-list disjoint sets keep one list per set and answer each query correctly full
This proof shows: correctness, termination.
We write for the set of elements made so far, and for each set object we write for the sequence , followed until NIL. We prove that after each operation the invariant of the page holds:
(L) For each set object , the sequence has exactly distinct elements, ends at with , and each of its elements has . Each element of is in exactly one sequence .
Under (L), the sets of the structure are the sets of elements of the lists , they are disjoint and they cover , and the representative of the set of is .
Initialization. With no elements and no set objects, (L) holds trivially.
MAKE-SET, for a new element . Lines L1 and L2 make an object with , , , and . No other field changes, and was in no list before. So (L) holds, and the new set is , as the disjoint-set type requires.
FIND. Line L3 changes nothing and returns . By (L), is in , and two elements get the same answer exactly when they have the same set object, that is, when they are in one set. It stops after two reads.
UNION. Assume (L). Line L4 sets and , the objects of the sets of and . If , the two elements are in one set, line L5 returns, and nothing changes.
Let . Line L6 may swap the names and ; the rest of the proof uses the names after the swap, and the claim to prove is symmetric in the two sets. Let and , with and . These elements are distinct, because each element is in only one list.
Line L7. The walk visits and sets . It stops after steps, because by (L), and it changes no next field, so the list it walks does not change during the walk.
Line L8. Before it, and . It sets and . Now : from the old links lead to , the new link leads to , and the old links of lead to , whose next is NIL.
Line L9. It sets and removes .
Now check (L). The list has distinct elements, equal to ; it ends at with ; its first elements had and kept it, and the last got at line L7. The object no longer exists, and no element points to it. Every other object and its list is unchanged, because lines L7 to L9 change only fields of , , and . Each element is still in exactly one list: the elements of the old lists and are now in the new , and the other lists did not change. So (L) holds. The new set of is the union of the two old sets, and all other sets are as before, which is the effect that the disjoint-set type requires of UNION. The swap of line L6 changes only which of the two objects survives, so the sets are the same with or without it.
Conclusion. By induction on the number of operations, (L) holds after each operation, each operation stops, and FIND returns a representative that is shared by exactly the members of one set.