Disjoint Set
Sets & Partitions
• Set is collection of unique members
• Partitions are subsets of Base Set
– No member overlap
– No empty subset
• Equivalence Partition: Members of a
Partition are equivalent
• Partitions are also called Disjoint Sets
(no common members)
Dynamic Equivalence
• Number and size of Equivalence Partitions
may change dynamically
• Two operations to track
– Find: check partition of any member
– Union: combine two partitions; all
members in same partition, all equivalent
Example
3 partitions (disjoint sets)
{5, 6, 9, 10} {3, 8, 12} {4, 7, 15}
Each disjoint set has unique name, given by any
member of set: 5, 3 & 4 in example above
Find shows name of partition for a member
Find(6) = 5 Find(12) = 3 Find(4) = 4
Example
3 partitions (disjoint sets)
{5, 6, 9, 10} {3, 8, 12} {4, 7, 15}
Union combines two sets
Union (5, 4): {5, 6, 9, 10, 4, 7, 15} {3, 8, 12}
Name of combined set: 5 (name of left set)
Find(4)=5, Find(10)=5 Find(8)=3
Disjoint Set Data Structure
• Disjoint set operations modeled as tree
structure (not necessarily binary)
• Initially, all elements are independent nodes
• Nodes combined through Union operation
• Tree structure formed when nodes join,
combining two disjoint sets
• Tree, or set, is identified by root node
Operation 1
Initial Status
{1}, {2}, {3}, {4}, {5}, {6}, {7}, {8}
1 2 3 4 5 6 7 8
Operation 2
Union (2, 3)
{2, 3} is now a tree, 3 pointing up to 2 (parent)
1 2 4 5 6 7 8
3
Operation 3
Union (2, 3) Union (4,5)
1 2 4 6 7 8
3 5
Operation 4
Union (2, 3) Union (4,5) Union (5,6)
1 2 4 7 8
3 5
6
Operation 5
Find (3) = 2, follow link to parent
Find (6) = 4, linked to 5, which is linked to 4
1 2 4 7 8
3 5
6
Operation 6
Union (2, 4) : Two trees joined together
Find (6)= ?
1 4 7 8
3
5
6
Implementation 1
Data Structure: Array, one entry for each node
Initialized to -1
1 2 3 4 5 6 7 8
1 2 3 4 5 6 7 8
-1 -1 -1 -1 -1 -1 -1 -1
Implementation 2
Union (2, 3): Array Entry for 3 change to 2 (parent)
1 2 4 5 6 7 8
1 2 3 4 5 6 7 8
-1 -1 2 -1 -1 -1 -1 -1
Implementation 3
Union (2, 3) Union (4,5)
1 2 4 6 7 8
3 5
1 2 3 4 5 6 7 8
-1 -1 2 -1 4 -1 -1 -1
Implementation 4
Union (5, 6)
1 2 4 7 8
3 5
1 2 3 4 5 6 7 8
-1 -1 2 -1 4 5 -1 -1
Implementation 5
Union (2,4)
2
1 4 7 8
3
5
1 2 3 4 5 6 7 8
-1 -1 2 2 4 5 -1 -1
Parent Array
• Array to track links called Parent Array
• Array initialized to -1, all nodes independent
• Union of two nodes results in changing parent
id of one node to other node
• Find checks for root node of the tree
• Linked list not used for this implementation
Union Find Algorithm
Associated Structure: Parent Array, parent[1..n]
Procedure Union(node1, node2) Procedure Find(node)
Begin Begin
Set parent[node2]:= node1 If parent[node] = -1
End Return node
Else
Find (parent[node])
End If
End
• Union changes parent id of node 2 to node 1
• Find checks parent id of current node, if (-1) then returns
current node, else checks for parent repeatedly
19
Implementation 5?
Union (3, 6) = ?
1 2 4 7 8
3 5
1 2 3 4 5 6 7 8
-1 -1 2 -1 4 5 -1 -1
Modified Union Algorithm
Associated Structure: Parent Array, parent[1..n]
Procedure Union1(node1, node2) Procedure Find(node)
Begin Begin
Set par1 := Find(node1) If parent[node] = -1
Set par2 := Find(node2) Return node
Set parent[par2]:= par1 Else
End Find (parent[node])
End If
End
• Modified Union ensures that only tree roots are joined, not any
other node
21
Implementation 5
Union (3, 6) = Union (2, 4)
2
1 4 7 8
3
5
1 2 3 4 5 6 7 8
-1 -1 2 2 4 5 -1 -1
Implementation 5A
Also, Union (5, 6) = Union (4, 6)
1 4 7 8
3
5
1 2 3 4 5 6 7 8
-1 -1 2 2 4 4 -1 -1
Analysis of Union Find
• Basic Union is O(1), independent of number of
nodes, or, elements in disjoint set
• Basic Find is O(n), where n is number of nodes
in disjoint set and also height of tree
• Modified Union uses Find(), but reduces tree
height; both approximately O(log n)
• More efficient algorithms for Union-Find
available
The End