Disjoint Sets
Suppose we had the following graphs:
We can represent these graphs as sets:
The intersection of these two sets is null:
Therefore, we say that and are disjoint.
Find Operation
On the disjoint set data structure, the find()
operation returns the set
an input argument belongs to. For example, find(5)
returns
and find(3)
returns If no such element exists, find()
(as
defined in these materials) returns null
.
Union Operation
The union()
operation connects two vertices on the graph. For example,
union(4,8)
results in:
Importantly, the union()
operation, at least one the disjoint set data
structure, will only connect two vertices and if, and only
if, and are members of different sets. That is, there isn't
already a path connecting the two of them. For example, union(1,2)
will
not do anything, because 1
and 2
are members of the same set —
Now, notice what happens if we call union(1,5)
:
Now all of the vertices are connected in a single graph. Additionally, the earlier individual sets are no more. We now just have one set:
This yields a valuable piece of information: The graph contains a cycle.