Union-find with lists
General information
Abstract Data Structure: Union-find
Implementation Invariant:
- There is an array
with index range and component type "ordered sequence of integral numbers". - Each non-empty sequence in this array contains one of the partition sets, that is:
- Each element of
is contained in exactly one sequence of . - There are no other numbers in these sequences.
- Each element of
- There is an array
with index range and component type . For , is in the sequence .
For a non-empty sequence in , the head of this sequence is the representative of the corresponding partition set.
Methods
The find and unite methods are quite simple and need no separate wiki pages:
- find: For input
, just return the head of the sequence . - unite: Append one sequence at the tail of the other one and clear the original position of the former sequence in
. The values of the appended elements have to be updated according to item 3 of the implementation invariant.
Asymptotic complexity
Statement: If always the shorter sequence is appended to the longer sequence (ties arbitrarily broken), the asymptotic complexity is
Proof: Obviously, a single call to the find method requires constant time. So, the contribution of all find operations is in