A Disjoint set union is an algorithm that is used to manage a collection of disjoint sets. A disjoint set is a set in which the elements are not in any other set. Also, known as union-find or merge-find.
The disjoint set union algorithm allows you to perform the following operations efficiently:
- Find: Determine which set a given element belongs to.
- Union: Merge two sets into a single set.
There are several ways to implement the disjoint set union algorithm, including the use of a randomized algorithm. Here is one way to implement the disjoint set union algorithm using a randomized approach:
- Initialize an array parent[] of size n, where n is the number of elements in the disjoint set. Set the value of each element in the parent to be its own index. This means that each element is initially in its own set.
- To find the set that a given element x belongs to, follow the chain of parent links until you reach an element whose parent is itself. This is the root element of the set, and it represents the set that x belongs to.
- To merge two sets, find the roots of the two sets and set the parent of one of the roots to be the other root.
- To improve the performance of the algorithm, you can use path compression to flatten the tree structure of the sets. This means that when you follow the parent links to find the root of a set, you also set the parent of each element on the path to be the root. This reduces the height of the tree and makes future operations faster.
- To further improve the performance, you can use randomization to choose which element to set as the root of the merged set. This can help to balance the tree and make the algorithm run more efficiently.
Overall, the disjoint set union algorithm is a useful tool for efficiently managing a collection of disjoint sets. It can be used in a variety of applications, including clustering, graph coloring, and image segmentation.
Here is an example of the disjoint set union algorithm implemented in Python:
C++
#include <bits/stdc++.h>
using namespace std;
class GFG {
public :
vector< int > parent;
vector< int > size;
public :
GFG( int n)
{
parent.resize(n);
size.resize(n);
for ( int i = 0; i < n; i++) {
parent[i] = i;
size[i] = 1;
}
}
int find( int x)
{
if (parent[x] != x) {
parent[x] = find(parent[x]);
}
return parent[x];
}
void union_( int x, int y)
{
int root_x = find(x);
int root_y = find(y);
if (root_x == root_y) {
return ;
}
if ( rand () % 2 == 0) {
int temp = root_x;
root_x = root_y;
root_y = temp;
}
parent[root_y] = root_x;
size[root_x] += size[root_y];
}
};
int main()
{
GFG ds(5);
cout << "Initial parent array: " ;
for ( int i = 0; i < 5; i++)
cout << ds.parent[i] << " " ;
cout << endl;
ds.union_(0, 1);
cout << "Parent array after union(0, 1): " ;
for ( int i = 0; i < 5; i++)
cout << ds.parent[i] << " " ;
cout << endl;
ds.union_(1, 2);
cout << "Parent array after union(1, 2): " ;
for ( int i = 0; i < 5; i++)
cout << ds.parent[i] << " " ;
cout << endl;
ds.union_(3, 4);
cout << "Parent array after union(3, 4): " ;
for ( int i = 0; i < 5; i++)
cout << ds.parent[i] << " " ;
cout << endl;
cout << "Root of set containing element 0: "
<< ds.find(0) << endl;
cout << "Root of set containing element 3: "
<< ds.find(3) << endl;
return 0;
}
|
Java
import java.io.*;
import java.util.*;
class GFG {
int [] parent;
int [] size;
Random rand = new Random();
public GFG( int n)
{
parent = new int [n];
size = new int [n];
for ( int i = 0 ; i < n; i++) {
parent[i] = i;
size[i] = 1 ;
}
}
public int find( int x)
{
if (parent[x] != x) {
parent[x] = find(parent[x]);
}
return parent[x];
}
public void union( int x, int y)
{
int rootX = find(x);
int rootY = find(y);
if (rootX == rootY) {
return ;
}
if (rand.nextInt( 2 ) == 0 ) {
int temp = rootX;
rootX = rootY;
rootY = temp;
}
parent[rootX] = rootY;
size[rootY] += size[rootX];
}
public static void main(String[] args)
{
GFG ds = new GFG( 5 );
System.out.println( "Initial parent array: "
+ Arrays.toString(ds.parent));
ds.union( 0 , 1 );
System.out.println(
"Parent array after union(0, 1): "
+ Arrays.toString(ds.parent));
ds.union( 1 , 2 );
System.out.println(
"Parent array after union(1, 2): "
+ Arrays.toString(ds.parent));
ds.union( 3 , 4 );
System.out.println(
"Parent array after union(3, 4): "
+ Arrays.toString(ds.parent));
System.out.println(
"Root of set containing element 0: "
+ ds.find( 0 ));
System.out.println(
"Root of set containing element 3: "
+ ds.find( 3 ));
}
}
|
Python3
import random
class DisjointSetUnion:
def __init__( self , n):
self .parent = [i for i in range (n)]
self .size = [ 1 ] * n
def find( self , x):
if self .parent[x] ! = x:
self .parent[x] = self .find( self .parent[x])
return self .parent[x]
def union( self , x, y):
root_x = self .find(x)
root_y = self .find(y)
if root_x = = root_y:
return
if random.randint( 0 , 1 ) = = 0 :
root_x, root_y = root_y, root_x
self .parent[root_y] = root_x
self .size[root_x] + = self .size[root_y]
ds = DisjointSetUnion( 5 )
print ( "Initial parent array: " , ds.parent)
ds.union( 0 , 1 )
print ( "Parent array after union(0, 1): " , ds.parent)
ds.union( 1 , 2 )
print ( "Parent array after union(1, 2): " , ds.parent)
ds.union( 3 , 4 )
print ( "Parent array after union(3, 4): " , ds.parent)
print ( "Root of set containing element 0: " , ds.find( 0 ))
print ( "Root of set containing element 3: " , ds.find( 3 ))
|
C#
using System;
public class GFG {
int [] parent;
int [] size;
Random rand = new Random();
public GFG( int n)
{
parent = new int [n];
size = new int [n];
for ( int i = 0; i < n; i++) {
parent[i] = i;
size[i] = 1;
}
}
public int Find( int x)
{
if (parent[x] != x) {
parent[x] = Find(parent[x]);
}
return parent[x];
}
public void Union( int x, int y)
{
int rootX = Find(x);
int rootY = Find(y);
if (rootX == rootY) {
return ;
}
if (rand.Next(2) == 0) {
int temp = rootX;
rootX = rootY;
rootY = temp;
}
parent[rootX] = rootY;
size[rootY] += size[rootX];
}
static public void Main()
{
GFG ds = new GFG(5);
Console.WriteLine( "Initial parent array: ["
+ string .Join( ", " , ds.parent)
+ "]" );
ds.Union(0, 1);
Console.WriteLine(
"Parent array after union(0, 1): ["
+ string .Join( ", " , ds.parent) + "]" );
ds.Union(1, 2);
Console.WriteLine(
"Parent array after union(1, 2): ["
+ string .Join( ", " , ds.parent) + "]" );
ds.Union(3, 4);
Console.WriteLine(
"Parent array after union(3, 4): ["
+ string .Join( ", " , ds.parent) + "]" );
Console.WriteLine(
"Root of set containing element 0: "
+ ds.Find(0));
Console.WriteLine(
"Root of set containing element 3: "
+ ds.Find(3));
}
}
|
Javascript
class DisjointSetUnion {
constructor(n) {
this .parent = [...Array(n).keys()]
this .size = Array(n).fill(1)
}
find(x) {
if ( this .parent[x] !== x) {
this .parent[x] = this .find( this .parent[x])
}
return this .parent[x]
}
union(x, y) {
let root_x = this .find(x)
let root_y = this .find(y)
if (root_x === root_y) {
return
}
if (Math.floor(Math.random() * 2) === 0) {
[root_x, root_y] = [root_y, root_x]
}
this .parent[root_y] = root_x
this .size[root_x] += this .size[root_y]
}
}
let ds = new DisjointSetUnion(5)
console.log( "Initial parent array: " , ds.parent.join( " " ))
ds.union(0, 1)
console.log( "Parent array after union(0, 1): " , ds.parent.join( " " ))
ds.union(1, 2)
console.log( "Parent array after union(1, 2): " , ds.parent.join( " " ))
ds.union(3, 4)
console.log( "Parent array after union(3, 4): " , ds.parent.join( " " ))
console.log( "Root of set containing element 0: " , ds.find(0))
console.log( "Root of set containing element 3: " , ds.find(3))
|
Output
Initial parent array: 0 1 2 3 4
Parent array after union(0, 1): 0 0 2 3 4
Parent array after union(1, 2): 2 0 2 3 4
Parent array after union(3, 4): 2 0 2 3 3
Root of set containing element 0: 2
Root of set containing element 3: 3
Below is an implementation of the DisjointSetUnion class that uses randomized linking for the union operation:
C++
#include <iostream>
#include <vector>
#include <random>
using namespace std;
class DisjointSetUnion {
public :
DisjointSetUnion( int n) {
parent = vector< int >(n);
size = vector< int >(n, 1);
for ( int i = 0; i < n; i++) {
parent[i] = i;
}
}
int find( int x) {
if (parent[x] != x) {
parent[x] = find(parent[x]);
}
return parent[x];
}
void unite( int x, int y) {
int root_x = find(x);
int root_y = find(y);
if (root_x == root_y) {
return ;
}
if ( rand () % 2 == 0) {
swap(root_x, root_y);
}
parent[root_y] = root_x;
size[root_x] += size[root_y];
}
private :
vector< int > parent;
vector< int > size;
};
int main() {
DisjointSetUnion ds(5);
cout << "Initial parent array: " ;
for ( int i = 0; i < 5; i++) {
cout << ds.find(i) << " " ;
}
cout << endl;
ds.unite(0, 1);
cout << "Parent array after union(0, 1): " ;
for ( int i = 0; i < 5; i++) {
cout << ds.find(i) << " " ;
}
cout << endl;
ds.unite(1, 2);
cout << "Parent array after union(1, 2): " ;
for ( int i = 0; i < 5; i++) {
cout << ds.find(i) << " " ;
}
cout << endl;
ds.unite(3, 4);
cout << "Parent array after union(3, 4): " ;
for ( int i = 0; i < 5; i++) {
cout << ds.find(i) << " " ;
}
cout << endl;
cout << "Root of set containing element 0: " << ds.find(0) << endl;
cout << "Root of set containing element 3: " << ds.find(3) << endl;
return 0;
}
|
Java
import java.util.Arrays;
import java.util.Random;
public class DisjointSetUnion {
private int [] parent;
private int [] size;
public DisjointSetUnion( int n)
{
this .parent = new int [n];
for ( int i = 0 ; i < n; i++) {
parent[i] = i;
}
this .size = new int [n];
Arrays.fill(size, 1 );
}
public int find( int x)
{
if (parent[x] != x) {
parent[x] = find(parent[x]);
}
return parent[x];
}
public void union( int x, int y)
{
int rootX = find(x);
int rootY = find(y);
if (rootX == rootY) {
return ;
}
Random rand = new Random();
if (rand.nextInt( 2 ) == 0 ) {
int temp = rootX;
rootX = rootY;
rootY = temp;
}
parent[rootY] = rootX;
size[rootX] += size[rootY];
}
public static void main(String[] args)
{
DisjointSetUnion ds = new DisjointSetUnion( 5 );
System.out.println( "Initial parent array: "
+ Arrays.toString(ds.parent));
ds.union( 0 , 1 );
System.out.println(
"Parent array after union(0, 1): "
+ Arrays.toString(ds.parent));
ds.union( 1 , 2 );
System.out.println(
"Parent array after union(1, 2): "
+ Arrays.toString(ds.parent));
ds.union( 3 , 4 );
System.out.println(
"Parent array after union(3, 4): "
+ Arrays.toString(ds.parent));
System.out.println(
"Root of set containing element 0: "
+ ds.find( 0 ));
System.out.println(
"Root of set containing element 3: "
+ ds.find( 3 ));
}
}
|
Python3
import random
class DisjointSetUnion:
def __init__( self , n):
self .parent = [i for i in range (n)]
self .size = [ 1 ] * n
def find( self , x):
if self .parent[x] ! = x:
self .parent[x] = self .find( self .parent[x])
return self .parent[x]
def union( self , x, y):
root_x = self .find(x)
root_y = self .find(y)
if root_x = = root_y:
return
if random.randint( 0 , 1 ) = = 0 :
root_x, root_y = root_y, root_x
self .parent[root_y] = root_x
self .size[root_x] + = self .size[root_y]
ds = DisjointSetUnion( 5 )
print ( "Initial parent array: " , ds.parent)
ds.union( 0 , 1 )
print ( "Parent array after union(0, 1): " , ds.parent)
ds.union( 1 , 2 )
print ( "Parent array after union(1, 2): " , ds.parent)
ds.union( 3 , 4 )
print ( "Parent array after union(3, 4): " , ds.parent)
print ( "Root of set containing element 0: " , ds.find( 0 ))
print ( "Root of set containing element 3: " , ds.find( 3 ))
|
C#
using System;
using System.Collections.Generic;
class DisjointSetUnion {
private List< int > parent;
private List< int > size;
public DisjointSetUnion( int n)
{
parent = new List< int >(n);
size = new List< int >(n);
for ( int i = 0; i < n; i++) {
parent.Add(i);
size.Add(1);
}
}
public int Find( int x)
{
if (parent[x] != x) {
parent[x] = Find(parent[x]);
}
return parent[x];
}
public void Unite( int x, int y)
{
int root_x = Find(x);
int root_y = Find(y);
if (root_x == root_y) {
return ;
}
if ( new Random().Next(2) == 0) {
int temp = root_x;
root_x = root_y;
root_y = temp;
}
parent[root_y] = root_x;
size[root_x] += size[root_y];
}
}
class Program {
static void Main( string [] args)
{
DisjointSetUnion ds = new DisjointSetUnion(5);
Console.Write( "Initial parent array: " );
for ( int i = 0; i < 5; i++) {
Console.Write(ds.Find(i) + " " );
}
Console.WriteLine();
ds.Unite(0, 1);
Console.Write( "Parent array after union(0, 1): " );
for ( int i = 0; i < 5; i++) {
Console.Write(ds.Find(i) + " " );
}
Console.WriteLine();
ds.Unite(1, 2);
Console.Write( "Parent array after union(1, 2): " );
for ( int i = 0; i < 5; i++) {
Console.Write(ds.Find(i) + " " );
}
Console.WriteLine();
ds.Unite(3, 4);
Console.Write( "Parent array after union(3, 4): " );
for ( int i = 0; i < 5; i++) {
Console.Write(ds.Find(i) + " " );
}
Console.WriteLine();
Console.WriteLine(
"Root of set containing element 0: "
+ ds.Find(0));
Console.WriteLine(
"Root of set containing element 3: "
+ ds.Find(3));
}
}
|
Javascript
class DisjointSetUnion {
constructor(n) {
this .parent = new Array(n);
for (let i = 0; i < n; i++) {
this .parent[i] = i;
}
this .size = new Array(n).fill(1);
}
find(x) {
if ( this .parent[x] !== x) {
this .parent[x] = this .find( this .parent[x]);
}
return this .parent[x];
}
union(x, y) {
let rootX = this .find(x);
let rootY = this .find(y);
if (rootX === rootY) {
return ;
}
if (Math.floor(Math.random() * 2) === 0) {
[rootX, rootY] = [rootY, rootX];
}
this .parent[rootY] = rootX;
this .size[rootX] += this .size[rootY];
}
}
const ds = new DisjointSetUnion(5);
console.log( "Initial parent array: " + ds.parent);
ds.union(0, 1);
console.log( "Parent array after union(0, 1): " + ds.parent);
ds.union(1, 2);
console.log( "Parent array after union(1, 2): " + ds.parent);
ds.union(3, 4);
console.log( "Parent array after union(3, 4): " + ds.parent);
console.log( "Root of set containing element 0: " + ds.find(0));
console.log( "Root of set containing element 3: " + ds.find(3));
|
Output
Initial parent array: [0, 1, 2, 3, 4]
Parent array after union(0, 1): [0, 0, 2, 3, 4]
Parent array after union(1, 2): [0, 0, 0, 3, 4]
Parent array after union(3, 4): [0, 0, 0, 4, 4]
Root of set containing element 0: 0
Root of set containing element 3: 4
The output of DisjointSetUnion class that uses randomized linking for the union operation:
Initial parent array: [0, 1, 2, 3, 4]
Parent array after union(0, 1): [0, 0, 2, 3, 4]
Parent array after union(1, 2): [0, 0, 0, 3, 4]
Parent array after union(3, 4): [0, 0, 0, 3, 3]
Root of set containing element 0: 0
Root of set containing element 3: 3
One-try and two-try variations of splitting:
The one-try and two-try variations of splitting are techniques that can be used to improve the performance of the disjoint set union (also known as union-find or merge-find) algorithm in certain cases. These variations are used to optimize the find operation, which is used to determine which set a given element belongs to.
- In the basic disjoint set union algorithm, the find operation follows the chain of parent links to find the root of the set that a given element belongs to. This can be slow if the tree structure of the sets is highly unbalanced.
- The one-try and two-try variations of splitting aim to optimize the find operation by reducing the number of times that the parent links need to be followed. They do this by temporarily modifying the parent links during the find operation in order to “split” the tree into smaller pieces.
- The find method follows the parent links as usual, but it also temporarily stores the parent of x in a local variable root. If the parent of x is x itself (indicating that x is the root of the set), then the find method returns the root instead of x. This reduces the number of times that the parent links need to be followed and can improve the performance of the find operation.
Here is an example of how the one-try variation of splitting might be implemented:
C++
int find( int x)
{
if (parent[x] != x) {
int root = parent[x];
parent[x] = find(parent[x]);
if (parent[x] == x) {
return root;
}
}
return parent[x];
}
|
Java
public int find( int x)
{
if (parent[x] != x) {
int root = parent[x];
parent[x] = find(parent[x]);
if (parent[x] == x) {
return root;
}
}
return parent[x];
}
|
Python
def find( self , x):
if self .parent[x] ! = x:
self .parent[x], root = self .find( self .parent[x]), self .parent[x]
if self .parent[x] = = x:
return root
return self .parent[x]
|
C#
int find( int x)
{
if (parent[x] != x) {
int root = parent[x];
parent[x] = find(parent[x]);
if (parent[x] == x) {
return root;
}
}
return parent[x];
}
|
Javascript
function find(x)
{
if (parent[x] != x) {
let root = parent[x];
parent[x] = find(parent[x]);
if (parent[x] == x) {
return root;
}
}
return parent[x];
}
|
The two-try variation of splitting is similar to the one-try variation, but it involves following the parent links twice instead of once. This can further improve the performance of the find operation in some cases.
Overall, the one-try and two-try variations of splitting are techniques that can be used to optimize the find operation in the disjoint set union algorithm. They can be useful in cases where the tree structure of the sets is highly unbalanced and the find operation is taking a long time. However, they may not always provide a significant improvement in performance and may require additional memory and computation.
Analysis of linking and Analysis splitting:
Time complexity: O(alpha(n)) (for find operation)
Time complexity: O(alpha(n)) (for Union operation)
One variation of the disjoint set union algorithm is to use the one-try or two-try variations of splitting to optimize the find operation. These variations can improve the performance of the find operation in cases where the tree structure of the sets is highly unbalanced, but they may not always provide a significant improvement in performance and may require additional memory and computation.
Advantages over the sequential approach:
- Speed: The disjoint set union algorithm has a time complexity of O(alpha(n)), where alpha(n) is the inverse Ackermann function. This means that it is very efficient and can scale well to large data sets. In contrast, the sequential approach has a time complexity of O(n), which can be slow for large data sets.
- Memory efficiency: The disjoint set union algorithm uses a compact data structure that requires only a single array to store the parent links. This makes it more memory efficient than the sequential approach, which uses an array of parent links and an array of rank values.
- Simplicity: The disjoint set union algorithm is relatively simple to implement and does not require the use of additional data structures or complex data structures like heaps.
Disadvantages over sequential approach:
- Slower performance for small data sets: The disjoint set union algorithm may have slower performance than the sequential approach for small data sets. This is because the time complexity of the disjoint set union algorithm is based on the inverse Ackermann function, which grows very slowly.
- More memory usage: The disjoint set union algorithm requires more memory than the sequential approach. This is because it uses a single array to store the parent links, whereas the sequential approach uses two arrays (one for the parent links and one for the rank values).
Overall, the disjoint set union algorithm is a very efficient and scalable data structure for managing a collection of disjoint sets. It is well-suited for large data sets and can be simpler to implement than the sequential approach. However, it may have slower performance and higher memory usage than the sequential approach for small data sets.
Feeling lost in the world of random DSA topics, wasting time without progress? It's time for a change! Join our DSA course, where we'll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 geeks!
Last Updated :
06 Apr, 2023
Like Article
Save Article