Union-Find Problem
Sequential Implementation of ADT Set
Suppose that the universal set is finite and known a priori.
→ inflexible though
All elements in the universal set are serially numbered by
integers 1 through n.
Thus, ElementType is int.
Suppose that the number n of elements in the universal set
is fixed as a constant, i.e., all instances of Set are arrays
of the same size n.
public class Set
{ boolean membership[ ];
int no_elements; // the number of elements
// methods for the operations on Set
}
-------------------------
S | 0 | 1 | 0 | ... | 0 | 1 | when S={c,a}
-------------------------
Symbol Table (This table can be omitted.)
--------------------------------
Serial No. Name of an Element
--------------------------------
1 xyz
2 c
3 op
.
.
.
n a
--------------------------------
An Alternative Sequential Implementation
Assumptions
(1) Elements are identified with serial numbers between 1 and n.
(2) A set is named with one of its elements that is called a
representative of the set.
Example
-------
Universal Set {a,b,c,d,e,f,g,h,i,x,y,z}
Partition {a,b,c,d,e,f,g,x},{h,i,z},{y}
^ ^ ^
representative
a b c d e f g h i x y z
-----------------------------------------------
| 1 | 1 | 1 | 1 | 1 | 1 | 1 | 8 | 8 | 1 |11 | 8 |
-----------------------------------------------
Naive Linked List Implementation
A set is represented by a linked list of elements in the set.
Example
-------
## Fig. 1 Naive ##
Tree Implementation
Assumption: A set is named with its representative.
Example
-------
{x,a,b,c,d,e,f,g} {y} {z,h,i}
^ ^ ^
representative
## Fig. 2 Tree ##
A partition of the universal set is represented by a forest
such that each set in the partition corresponds to the set of
vertices in a tree of the forest.
Remark
------
Tree representation of a set may not be unique.
A representative of the set may not be unique.
## Fig. 3 Another Tree ##
Example
-------
Find(e) returns x. ← Starting e, follow parent pointers until
we reach the root.
Union(y,z) returns one of the following arbitrarily.
## Fig. 4-union ##
Find(Union(y,z),i) returns y or z.
Storage Structure
-----------------
Array of parent pointers
## Fig. 5 Pointer Array ##
→ Array of integers!
o parent pointer → location
o nil pointer → -1
-----------------------------------
|10| 1|10| 3| 3| 3| 5|12|12|-1|-1|-1|
-----------------------------------
a b c d e f g h i x y z
This data structure achieves O(lg* n) time complexity
per operation, where lg* n is almost constant.