# Data Structures for Disjoint Sets

Some applications involve grouping $n$ distinct objects into a collection of disjoint sets. Two important operations are then finding which set a given object belongs to and uniting the two sets.
A disjoint set data structure maintains a collection $S=\left\{{S}_{1},{S}_{2},\dots ,{S}_{k}\right\}$ of disjoint dynamic sets. Each set is identified by a representative, which usually is a member in the set.

## 1  Operations on Sets

Let $x$ be an object. We wish to support the following operations.
• MAKE-SET( $x$) creates a new set whose only member is pointed by $x$; Note that $x$ is not in the other sets.
• UNION( $x,y$) unites two dynamic sets containing objects $x$ and $y$, say ${S}_{x}$ and ${S}_{y}$, into a new set that ${S}_{x}\cup {S}_{y}$, assuming that ${S}_{x}\cap {S}_{y}=\varnothing$;
• FIND-SET( $x$) returns a pointer to the representative of the set containing $x$.
• INSERT( $a,S$) inserts an object $a$ to $S$, and returns $S\cup \left\{a\right\}$.
• DELETE( $a,S$) deletes an object $a$ from $S$, and returns $S-\left\{a\right\}$.
• SPLIT( $a,S$) partitions the objects of $S$ into two sets ${S}_{1}$ and ${S}_{2}$ such that ${S}_{1}=\left\{b | b\le a & b\in S\right\}$, and ${S}_{2}=S-{S}_{1}$.
• MINIMUM( $S$) returns the minimum object in $S$.

## 2  Applications of Disjoint-set Data Structures

Here we show two application examples.
• Connected components (CCs)
• Minimum Spanning Trees (MSTs)

### 2.1  Algorithm for Connected Components

CONNECTED-COMPONENTS $\left(G\right)$
1    for each $v\in V$
2             do  MAKE-SET $\left(v\right)$
3    for every edge $\left(u,v\right)\in E$
4             do if  FIND-SET $\left(u\right)$ $\ne$ FIND-SET $\left(v\right)$
5                         then  UNION $\left(u,v\right)$
SAME-COMPONENTS $\left(u,v\right)$
1    if  FIND-SET $\left(u\right)$ $=$ FIND-SET $\left(v\right)$
2          then return TRUE
3    return FALSE
Figure 1: A graph with four connected components: $\left\{a,b,c,d\right\}$, $\left\{e,f,g\right\}$, $\left\{h,i\right\}$, and $\left\{j\right\}$.
 Edge processed Collection of disjoint sets initial sets $\left\{a\right\}$ $\left\{b\right\}$ $\left\{c\right\}$ $\left\{d\right\}$ $\left\{e\right\}$ $\left\{f\right\}$ $\left\{g\right\}$ $\left\{h\right\}$ $\left\{i\right\}$ $\left\{j\right\}$ $\left(b,d\right)$ $\left\{a\right\}$ $\left\{b,d\right\}$ $\left\{c\right\}$ $\left\{e\right\}$ $\left\{f\right\}$ $\left\{g\right\}$ $\left\{h\right\}$ $\left\{i\right\}$ $\left\{j\right\}$ $\left(e,g\right)$ $\left\{a\right\}$ $\left\{b,d\right\}$ $\left\{c\right\}$ $\left\{e,g\right\}$ $\left\{f\right\}$ $\left\{h\right\}$ $\left\{i\right\}$ $\left\{j\right\}$ $\left(a,c\right)$ $\left\{a,c\right\}$ $\left\{b,d\right\}$ $\left\{e,g\right\}$ $\left\{f\right\}$ $\left\{h\right\}$ $\left\{i\right\}$ $\left\{j\right\}$ $\left(h,i\right)$ $\left\{a,c\right\}$ $\left\{b,d\right\}$ $\left\{e,g\right\}$ $\left\{f\right\}$ $\left\{h,i\right\}$ $\left\{j\right\}$ $\left(a,b\right)$ $\left\{a,b,c,d\right\}$ $\left\{e,g\right\}$ $\left\{f\right\}$ $\left\{h,i\right\}$ $\left\{j\right\}$ $\left(e,f\right)$ $\left\{a,b,c,d\right\}$ $\left\{e,f,g\right\}$ $\left\{h,i\right\}$ $\left\{j\right\}$ $\left(b,c\right)$ $\left\{a,b,c,d\right\}$ $\left\{e,f,g\right\}$ $\left\{h,i\right\}$ $\left\{j\right\}$
Table 1: This table shows the state of the collection of disjoint sets as each edge is processed. The processed edge is listed on the left and the rest of the columns show the state of the collection.

## 3  The Disjoint Set Representation

A set can be represented by a linked list. In this representation, each node has a pointer to the next node and a pointer to the first node.
Figure 2: Linked-list representation. Each disjoint set can be represented by a linked-list. Each node has a pointer to the head node and a pointer to the next node.
Consider the following operation sequence:
MAKE-SET $\left({x}_{1}\right)$,
MAKE-SET $\left({x}_{2}\right)$,
$:$,
MAKE-SET $\left({x}_{n-1}\right)$,
MAKE-SET $\left({x}_{n}\right)$,
UNION $\left({x}_{1},{x}_{2}\right)$,
UNION $\left({x}_{2},{x}_{3}\right)$,
$:$,
UNION $\left({x}_{n-1},{x}_{n}\right)$.
What's the total time complexity of the above operations?
A weighted-union heuristic: Assume that the representative of each set maintains the number of objects in the set, and always merge the smaller list to the larger list, then
Theorem: Using the linked list representation of disjoint sets and the weighted-union heuristic, a sequence of $m$ MAKE-SET, Union, and Find_Set operations, $n$ of which are MAKE-SET, takes $O\left(m+n\mathrm{log}n\right)$ time.
Hint: observe that for any $k\le n$, after $x$'s representative pointer has been updated $⌈\mathrm{log}k⌉$ times, the resulting set containing $x$ must have at least $k$ members.

## 4  Disjoint Set Forests

A faster implementation of disjoint sets is through the rooted trees, with each node containing one member and each tree representing one set. Each member in the tree has only one parent.
Figure 3: Rooted tree representation of disjoint sets. Each tree has a "representative" $h$ for the left tree and $a$ for the right tree.

### 4.1  The Heuristics for Disjoint Set Operations

1. union by rank.
2. path compression
The idea of union by rank is to make the root of the tree with fewer nodes point to the root of the tree with more nodes. Rather than explicitly keeping track of the size of the subtree rooted at each node, for each node we maintain a rank that approximates the logarithm of the subtree size and is also an upper bound on the height of the node.
Time complexity: $O\left(m\mathrm{log}n\right)$, assuming that there are $m$ union operations.
Path compression is quite simple and very effective. We use this approach during FIND-SET operations to make each node on the path point directly to the root. Path compression does not change any ranks.
Figure 4: Path compression takes place during the FIND-SET operation. This works by recursing from the given input node up to the root of the tree, forming a path. Then the root is returned and assigned as the parent for each node in path. The parent of $c$ after FIND-SET $\left(c\right)$ is $h$.
Time complexity: $\Theta \left(f{\mathrm{log}}_{1+f/n\right)}n\right)$ if $f\ge n$ and $\Theta \left(n+f\mathrm{log}n\right)$ otherwise, assume that there are $n$ MAKE-SET operations and $f$ FIND-SET operations.
MAKE-SET $\left(x\right)$
1     $p\left[x\right]$ $←$  $x$
2     $\mathrm{rank}\left[x\right]$ $←$  $0$
FIND-SET $\left(x\right)$
1    if  $x\ne p\left[x\right]$
2          then  $p\left[x\right]$ $←$  FIND-SET $\left(p\left[x\right]\right)$
3    return  $p\left[x\right]$
UNION $\left(x,y\right)$
1     LINK( FIND-SET $\left(x\right)$, FIND-SET $\left(y\right)$)
LINK $\left(x,y\right)$
1    if  $\mathrm{rank}\left[x\right]>\mathrm{rank}\left[y\right]$
2          then  $p\left[y\right]$ $←$  $x$
3          else   $p\left[x\right]$ $←$  $y$
4                      if  $\mathrm{rank}\left[x\right]=\mathrm{rank}\left[y\right]$
5                            then  $\mathrm{rank}\left[y\right]$ $←$  $\mathrm{rank}\left[y\right]+1$
where $\mathrm{rank}\left[x\right]$ is the height of $x$ in the tree. If both of the above methods are used together, the time complexity is $O\left(m\alpha \left(m,n\right)\right)$.
The Rank properties
• $\mathrm{rank}\left[x\right]\le \mathrm{rank}\left[p\left[x\right]\right]$
• for any tree root $x$, $\mathrm{size}\left(x\right)\ge {2}^{\mathrm{rank}\left[x\right]}$ (Link operation)
• for any integer $r$, there are at most $n/{2}^{r}$ nodes of rank $r$
• each node has rank at most $⌊\mathrm{log}n⌋$, assuming there are at $n$ objects involved.

File translated from TEX by TTM, version 3.67.
On 31 Mar 2006, 18:12.