Disjoint Sets - Union Find (Connected Components)
Disjoint Set is an abstract data structure. It helps find the connected components in a dynamic graph in near constant time.
A dynamic graph is a graph that keeps on changing the configurationβfor example, a network connection.
You can read more about dynamic graph here --> Dynamic Graph Striver
Disjoint set mainly has two types of operations:
Finding the parent of the node
Union (creating an edge between two nodes)
Union by Rank
Union by Size
Terminology
Disjoint Set --> Disjoint set is a data structure that keeps track of a collection of disjoint (non-overlapping) sets.
Size --> Number of elements in a set or subset
Path compression --> A technique used to improve efficiency of the disjoint set. It works by making the path from a node to its root as short as possible. This is done by recursively changing the parent of each node to be its grandparent.
Rank --> Rank of node refers to the distance between furthest and current node
Parent --> Node right above the current node
Ultimate parent --> Ultimate parent refers to the topmost or root node of the current node
Path compression
If we have to find the ultimate parent, we have to traverse through the nodes connected to it. And it takes O(log n) time. But using path compression, we connect all the nodes to the ultimate parent directly.
This helps in retrieving the ultimate parent in almost constant time.

Time and Space complexity
Union by rank
O(Ξ±(n))
O(n)
Union by size
O(Ξ±(n))
O(n)
Where Ξ±(n) is the inverse Ackermann function, which is a very slowly growing function. In practice, both algorithms are essentially O(n)
Union By Rank
Union by rank is a technique to combine disjoint sets. The idea is to combine the set with the smaller rank with the set with the larger rank, so that the resulting set has the largest possible rank. This can be done by maintaining a list of the ranks of all the sets, and then always combining the two sets with the smallest ranks.
Union by rank is a simple and efficient way to combine disjoint sets. It is also relatively easy to implement, and it can be used to solve a variety of problems, such as finding the connected components of an undirected graph.
Algorithm
Initialize a list of parents and a list of ranks.
For each node, set its parent to itself and its rank to
-1.To find the representative of the set that a given node belongs to:
If the node is its own parent, then it is the representative of its set.
Otherwise, recursively call find on the parent of the node.
To unite the sets that two given nodes belong to:
Find the representatives of the sets that the two nodes belong to.
If the two nodes belong to the same set, then do nothing.
Find the rank of the two sets.
Unite the two sets by making the smaller set a child of the larger set.
If the rank of the two sets is the same, then increase the rank of the larger set by 1.
Code
Union By Size
Union by size is a technique to combine disjoint sets. The idea is to combine the smaller set with the larger set, so that the resulting set is as large as possible. This can be done by maintaining a list of the sizes of all the sets, and then always combining the two sets with the smallest sizes.
Union by size is a simple and efficient way to combine disjoint sets. It is also relatively easy to implement, and it can be used to solve a variety of problems, such as finding the connected components of an undirected graph.
Algorithm
Initialize a list of parents and a list of sizes.
For each node, set its parent to itself and its size to 1.
To find the representative of the set that a given node belongs to:
If the node is its own parent, then it is the representative of its set.
Otherwise, recursively call find on the parent of the node.
To unite the sets that two given nodes belong to:
Find the representatives of the sets that the two nodes belong to.
If the two nodes belong to the same set, then do nothing.
Find the size of the two sets.
Unite the two sets by making the smaller set a child of the larger set.
If the size of the two sets is the same, then set the parent of the smaller set to the larger set.
Code
Last updated