# Disjoint Set (Union-Find) in Python using OOP Concepts 1. Introduction
2. Understanding the Disjoint Set Data Structure
3. Designing the DisjointSet Class
4. Implementing the DisjointSet Class in Python using OOP Concepts
5. Example Usage and Applications
6. Conclusion

# Introduction

The Union-Find data structure, commonly known as the Disjoint Set, is a basic idea in computer science that effectively handles a collection of disjoint sets. It is a useful tool for addressing a wide range of issues that require grouping components into discrete sets and executing operations like union and find.

In this article at OpenGenus, we will look at the Disjoint Set data structure and how to construct it in Python using Object-Oriented Programming (OOP) concepts. We can construct a clean and modular solution that encapsulates the functionality of Disjoint Sets within a well-designed class by exploiting the possibilities of OOP.

The Disjoint Set data structure is used in a variety of methods and problem fields. It is used in graph theory algorithms, network connection research, picture processing, and other applications. We can unleash the Disjoint Set data structure's potential for tackling difficult issues quickly by understanding and implementing it using OOP ideas.

Throughout this essay, we will look at the fundamental notions of Disjoint Sets, such as the union and find operations. We will investigate the optimizations of union by rank and route compression, which considerably improve the data structure's performance. We will design a DisjointSet class in Python using OOP concepts that encapsulates the functionality and offers a clean and straightforward interface for working with disjoint sets.

Furthermore, we will look at practical examples and discuss real-world applications that use the Disjoint Set data structure. By the conclusion of this tutorial, you will have a basic grasp of Disjoint Sets, be able to design the data structure in Python using OOP ideas, and use it to effectively handle a variety of issues.

Let's start with the Disjoint Set (Union-Find) data structure and use OOP to construct an elegant and efficient solution in Python.

# Understanding of Disjoint Set Data Structure

The Disjoint Set data structure, also known as the Union-Find data structure, is intended to manage a collection of disjoint (non-overlapping) sets effectively. It allows you to organize components into sets and conduct operations like merging sets and determining if elements belong to the same set.

The Disjoint Set data structure is built around a forest of trees, with each tree representing a set. Each element in the set is a node in the tree, with the root of each tree serving as the set's representation or parent.

The two primary operations supported by the Disjoint Set data structure are:

1. Union Operation: The union operation combines two sets into a single set. It takes two elements from different sets and merges their respective sets into one. This operation effectively connects the roots of two trees, making one tree a subtree of the other.

2. Find Operation: The find operation determines which set an element belongs to. It takes an element as input and returns the representative element (root) of the set to which the input element belongs. This operation is often used to check whether two elements are in the same set or not.

To optimize the performance of the Disjoint Set data structure, two key techniques are commonly employed:

• Union by Rank: Each set is assigned a rank, which is an upper bound on the height of its tree. During a union operation, the set with a smaller rank is merged into the set with a larger rank. This approach helps to maintain balanced trees and prevents the creation of long paths, thereby improving the efficiency of future find operations.

• Path Compression: When performing a find operation, path compression is applied to flatten the tree structure. Along the path from the queried element to its root, each node is directly connected to the root, effectively reducing the height of the tree. Path compression optimizes future find operations by ensuring that subsequent queries encounter shorter paths.

The Disjoint Set data structure offers several benefits:

• It provides an efficient way to group elements into disjoint sets.
• It allows for quick union operations, enabling the merging of sets in near-constant time.
• It facilitates fast find operations, allowing for efficient determination of whether elements belong to the same set.
• It can be further optimized using union by rank and path compression techniques.

The Disjoint Set data structure finds extensive applications in various algorithms and problem domains. Some common use cases include:

• Finding minimum spanning trees using Kruskal's algorithm.
• Detecting connected components in a graph or network.
• Determining graph connectivity or connectedness.
• Clustering and partitioning algorithms.
• Image processing and segmentation.

By understanding the Disjoint Set data structure and its underlying principles, we can leverage its power to efficiently solve a wide range of problems. In the next section, we will dive into the process of designing and implementing the DisjointSet class using the principles of Object-Oriented Programming in Python.

# Designing the DisjointSet Class

To implement the Disjoint Set (Union-Find) data structure in Python using Object-Oriented Programming (OOP) concepts, we will design a DisjointSet class that encapsulates the functionality and provides an intuitive interface for working with disjoint sets.

Before diving into the implementation details, let's discuss the key components and attributes that the DisjointSet class should have:

1. Initialization and Constructor:

• The DisjointSet class should have a constructor that initializes the necessary data structures to store the disjoint sets.
• It can take an optional parameter to specify the size or maximum number of elements in the disjoint sets.

2. Data Structures:

• The DisjointSet class should maintain an appropriate data structure to represent the disjoint sets. This could be an array, dictionary, or any other suitable data structure based on the specific requirements.

3. Make Set Operation:

• The class should provide a method, such as make_set(element), to create a new set containing a single element.
• This method initializes a new set with the given element and assigns it a unique identifier or index.

4. Union Operation:

• The DisjointSet class should have a method, such as union(element1, element2), to merge two sets into a single set.
• This method combines the sets that contain element1 and element2, updating the necessary data structures accordingly.

5. Find Operation:

• The class should provide a method, such as find(element), to determine the representative or parent of the set to which an element belongs.
• This method should return the representative element that can be used to identify the set.

• You can include optional methods like find_set(element), which returns the entire set containing the given element.
• Another optional method, such as same_set(element1, element2), can be added to check whether two elements belong to the same set.

When designing the DisjointSet class, consider using appropriate data structures and implementing the union by rank and path compression optimizations. These optimizations help maintain balanced trees and reduce the height of the trees, resulting in improved overall performance.

Additionally, adhere to OOP principles such as encapsulation, abstraction, and modularity. Encapsulate the data and operations within the class, providing a clean interface for interacting with the disjoint sets.

Once the DisjointSet class is designed, you can proceed to implement the individual methods, incorporating union by rank and path compression techniques. Remember to properly initialize the data structures and handle edge cases to ensure the correct behavior of the class.

In the next section, we will delve into the implementation details of the DisjointSet class, demonstrating how to bring the design to life using Python and OOP concepts.

# Implementing the DisjointSet Class in Python using OOP Concepts

Now that we have designed the DisjointSet class, it's time to implement it in Python using Object-Oriented Programming (OOP) concepts. We will go through each method of the class and discuss their implementation details.

## Basic Implementation

The basic implementation initializes each element as a separate set and performs find and union operations using the parent list.

``````class DisjointSet:
def __init__(self, size):
self.parent = [-1] * size

def make_set(self, element):
# Create a new set with a single element
# Assign a unique identifier or index to the set
# Initialize the parent as itself (-1 indicates it's the root)
index = element  # Assuming element is the index itself
self.parent[index] = -1

def find(self, element):
# Find the representative element (root) of the set to which 'element' belongs
# Apply path compression to optimize future find operations
index = element
if self.parent[index] == -1:
return index
self.parent[index] = self.find(self.parent[index])  # Path compression
return self.parent[index]

def union(self, element1, element2):
# Merge the sets containing 'element1' and 'element2'
# Use union by rank to maintain balanced trees
root1 = self.find(element1)
root2 = self.find(element2)
if root1 != root2:
if self.parent[root1] < self.parent[root2]:  # Union by rank
self.parent[root1] += self.parent[root2]
self.parent[root2] = root1
else:
self.parent[root2] += self.parent[root1]
self.parent[root1] = root2
``````

1. Constructor and Initialization:

``````class DisjointSet:
def __init__(self, size):
self.parent = [-1] * size
``````
• In the constructor init, we initialize the parent array with -1 values to represent disjoint sets.
• The size parameter specifies the maximum number of elements in the disjoint sets.

2. Make Set Operation:

``````    def make_set(self, element):
index = element
self.parent[index] = -1
``````
• The make_set method creates a new set with a single element.
• It assigns a unique identifier or index to the set and initializes the parent as itself.

3. Find Operation:

``````    def find(self, element):
index = element
if self.parent[index] == -1:
return index
self.parent[index] = self.find(self.parent[index])
return self.parent[index]
``````
• The find method finds the representative or parent of the set to which an element belongs.
• It utilizes path compression, which recursively updates the parent of each traversed element to the root of the set.
• Path compression ensures shorter paths for subsequent find operations.

4. Union Operation:

``````    def union(self, element1, element2):
root1 = self.find(element1)
root2 = self.find(element2)
if root1 != root2:
if self.parent[root1] < self.parent[root2]:  # Union by rank
self.parent[root1] += self.parent[root2]
self.parent[root2] = root1
else:
self.parent[root2] += self.parent[root1]
self.parent[root1] = root2
``````
• The union method merges two sets into a single set.
• It performs union by rank, where the set with a smaller rank is merged into the set with a larger rank.
• By maintaining balanced trees, union by rank minimizes the tree height and optimizes future find operations.

5. Find Set Operation:

``````    def find_set(self, element):
rep = self.find(element)
return [i for i in range(len(self.parent)) if self.find(i) == rep]
``````
• The `find_set` method returns the entire set that contains the given element by using the `find` operation to find the representative and collecting all elements with the same representative.

6. Same Set Operation:

``````    def same_set(self, element1, element2):
return self.find(element1) == self.find(element2)
``````
• The `same_set` method checks if two elements belong to the same set by comparing their representatives obtained through the `find` operation.

## Union by Rank Optimization

In this implementation, we introduce the union by rank optimization. It keeps track of the rank (or depth) of each set and always attaches the smaller rank tree to the root of the larger rank tree during union operations. This optimization ensures a balanced tree structure, reducing the overall time complexity.

``````class DisjointSet:
def __init__(self, size):
self.parent = list(range(size))
self.rank =  * size

def find(self, element):
if self.parent[element] == element:
return element
self.parent[element] = self.find(self.parent[element])
return self.parent[element]

def union(self, element1, element2):
root1 = self.find(element1)
root2 = self.find(element2)
if root1 != root2:
if self.rank[root1] < self.rank[root2]:
self.parent[root1] = root2
elif self.rank[root1] > self.rank[root2]:
self.parent[root2] = root1
else:
self.parent[root2] = root1
self.rank[root1] += 1
``````

## Path Compression Optimization

In this final implementation, we incorporate path compression optimization during the find operation. It flattens the tree structure by directly connecting each element to its root, reducing the path length for subsequent find operations and improving overall performance.

``````class DisjointSet:
def __init__(self, size):
self.parent = list(range(size))
self.rank =  * size

def find(self, element):
if self.parent[element] == element:
return element
self.parent[element] = self.find(self.parent[element])  # Path compression
return self.parent[element]

def union(self, element1, element2):
root1 = self.find(element1)
root2 = self.find(element2)
if root1 != root2:
if self.rank[root1] < self.rank[root2]:
self.parent[root1] = root2
elif self.rank[root1] > self.rank[root2]:
self.parent[root2] = root1
else:
self.parent[root2] = root1
self.rank[root1] += 1
``````

Here is demonstration of the usage of disjoint class:

``````# Create a DisjointSet instance with a size of 10
ds = DisjointSet(10)

# Make sets with individual elements
for i in range(10):
ds.make_set(i)

# Perform union operations
ds.union(0, 1)
ds.union(2, 3)
ds.union(4, 5)
ds.union(6, 7)
ds.union(8, 9)

# Find representative elements
print(ds.find(1))  # Output: 0
print(ds.find(3))  # Output: 2
print(ds.find(5))  # Output: 4
print(ds.find(7))  # Output: 6
print(ds.find(9))  # Output: 8

# Check if elements belong to the same set
print(ds.same_set(1, 3))  # Output: False
print(ds.same_set(4, 6))  # Output: False
print(ds.same_set(8, 9))  # Output: True

# Find the entire set containing an element
print(ds.find_set(1))  # Output: [0, 1]
print(ds.find_set(5))  # Output: [4, 5]
print(ds.find_set(8))  # Output: [8, 9]
``````

# Example Usage and Applications

One common application of DisjointSet is in implementing Kruskal's algorithm for finding the Minimum Spanning Tree (MST) of a graph. The DisjointSet data structure is used to efficiently determine if two vertices belong to the same connected component or not while adding edges to the MST.

Here's an example that demonstrates how to use the DisjointSet class to find the MST of a graph using Kruskal's algorithm:

``````# Graph represented as an adjacency list
graph = [
[(1, 4), (7, 8)],
[(0, 4), (2, 8), (7, 11)],
[(1, 8), (3, 7), (5, 4), (8, 2)],
[(2, 7), (4, 9), (5, 14)],
[(3, 9), (5, 10)],
[(2, 4), (3, 14), (4, 10), (6, 2)],
[(5, 2), (7, 1), (8, 6)],
[(0, 8), (1, 11), (6, 1), (8, 7)],
[(2, 2), (6, 6), (7, 7)]
]

class Edge:
def __init__(self, u, v, weight):
self.u = u
self.v = v
self.weight = weight

def kruskal_mst(graph):
# Step 1: Initialize DisjointSet
num_vertices = len(graph)
ds = DisjointSet(num_vertices)

# Step 2: Create a list of all edges in the graph
edges = []
for u in range(num_vertices):
for v, weight in graph[u]:
edges.append(Edge(u, v, weight))

# Step 3: Sort the edges in ascending order of their weights
edges.sort(key=lambda edge: edge.weight)

# Step 4: Apply Kruskal's algorithm
mst = []
for edge in edges:
if not ds.same_set(edge.u, edge.v):
ds.union(edge.u, edge.v)
mst.append(edge)

return mst

# Find the MST of the graph
mst = kruskal_mst(graph)

# Print the edges in the MST
for edge in mst:
print(f"{edge.u} - {edge.v} : {edge.weight}")
``````

In this example, we have an undirected graph represented as an adjacency list. The kruskal_mst function implements Kruskal's algorithm using the DisjointSet data structure. It iterates over the edges in ascending order of their weights, adding each edge to the MST if it connects two disjoint sets. The resulting MST is then printed.

Applications:
The DisjointSet data structure finds applications in various algorithms and scenarios, including:
1. Connectivity Checking: DisjointSet can efficiently determine if two elements belong to the same connected component or not. It is commonly used in network analysis, image processing, and social network analysis.
2. Graph Algorithms: DisjointSet plays a crucial role in algorithms like Kruskal's algorithm for finding the MST, determining the connectivity of a graph, and detecting cycles in a graph.
3. Image Segmentation: DisjointSet can be used for image segmentation tasks, where pixels with similar characteristics are grouped into distinct segments or regions.

# Conclusion

In conclusion, the Disjoint Set, also known as the Union-Find data structure, is a powerful tool for efficiently managing disjoint sets and performing operations such as finding the representative element of a set, merging sets, and checking the connectivity between elements. It offers a simple yet effective approach to solve problems related to partitioning elements into distinct sets.

Throughout this article, we have explored the design and implementation of the DisjointSet class in Python using object-oriented programming concepts. The class provides methods for creating sets, finding the representative element of a set, merging sets, and checking the membership of elements in the same set. Additionally, helper methods such as finding the entire set containing an element and checking if two elements belong to the same set offer added functionality and convenience.

Performance analysis is a crucial aspect when evaluating any data structure. The Disjoint Set data structure, when implemented with path compression and union by rank, achieves an amortized time complexity of approximately O(α(n)), where α(n) is the inverse Ackermann function, which grows extremely slowly. This near-constant time complexity makes the Disjoint Set suitable for handling large sets efficiently.

The key to achieving this performance lies in the path compression technique, which optimizes the find operation by flattening the tree structure of the sets. Path compression ensures that subsequent find operations on the same set have even faster execution times, resulting in improved overall performance.

The Disjoint Set data structure finds various applications in graph algorithms, network analysis, image processing, and more. It is particularly useful in algorithms such as Kruskal's algorithm for finding the Minimum Spanning Tree and in solving problems related to connectivity, cycle detection, and partitioning.

By understanding and utilizing the Disjoint Set data structure, you gain a valuable tool for solving a wide range of problems efficiently and effectively. Its simplicity, combined with its favorable time complexity, makes it a powerful choice in many scenarios.

In conclusion, the Disjoint Set data structure offers an elegant solution for managing disjoint sets, providing a balance between simplicity and performance, and finding diverse applications in computer science and beyond.