Disjointset data structure
From Wikipedia, the free encyclopedia
Given a set of elements, it is often useful to break them up or partition them into a number of
separate, nonoverlapping sets. A
disjointset data structure
is a data structure that keeps track of
such a partitioning. A
unionfind algorithm
is an algorithm that performs two useful operations on
such a data structure:
±
Find
: Determine which set a particular element is in. Also useful for determining if two
elements are in the same set.
±
Union
: Combine or merge two sets into a single set.
Because it supports these two operations, a disjointset data structure is sometimes called a
union
find data structure
or
mergefind set
. The other important operation,
MakeSet
, which makes a set
containing only a given element (a singleton), is generally trivial. With these three operations, many
practical partitioning problems can be solved (see the
Applications
section).
In order to define these operations more precisely, some way of representing the sets is needed. One
common approach is to select a fixed element of each set, called its
representative
, to represent the
set as a whole. Then,
Find
(x) returns the representative of the set that
x
belongs to, and
Union
takes
two set representatives as its arguments.
Disjointset linked lists
A simple approach to creating a disjointset data structure is to create a linked list for each set. The
element at the head of each list is chosen as its representative.
MakeSet
creates a list of one element.
Union
appends the two lists, a constanttime operation. The
drawback of this implementation is that
Find
requires
Ω
(n) or linear time.
This can be avoided by including in each linked list node a pointer to the head of the list; then
Find
takes constant time. However,
Union
now has to update each element of the list being appended to
make it point to the head of the new combined list, requiring
Ω
(n) time.
When the length of each list is tracked, the required time can be improved by always appending the
smaller list to the longer. Using this
weightedunion heuristic
, a sequence of
m
MakeSet
,
Union
, and
Find
operations on
n
elements requires O(
m
+
n
log
n
) time.
[1]
For asymptotically faster operations, a
different data structure is needed.
Disjointset forests
This preview has intentionally blurred sections. Sign up to view the full version.
View Full Document
This is the end of the preview.
Sign up
to
access the rest of the document.
 Fall '09
 Lee
 Disjointset data structure

Click to edit the document details