Disjoint Set Union (Union-Find) - Detailed

beginner
17 min

Disjoint Set Union (Union-Find) - Detailed

Welcome to our in-depth guide on the Disjoint Set Union (Union-Find) algorithm! This powerful data structure is essential for solving various real-world problems, such as network analysis, graph algorithms, and more. By the end of this lesson, you'll have a solid understanding of how to implement and optimize the Union-Find algorithm.

Let's dive in!

What is the Disjoint Set Union (Union-Find) algorithm? šŸŽÆ

The Disjoint Set Union (Union-Find) algorithm is a data structure used to represent a set of elements where each element is either a single element or a collection of elements. The main operations it performs are Union and Find, hence the name Union-Find.

Why is it important? šŸ“

The Union-Find algorithm is important because it allows us to efficiently handle the partitioning and merging of sets, which are fundamental operations in many algorithms and data structures. It's particularly useful in solving problems related to graph traversal, minimum spanning trees, and more.

Understanding the Data Structure šŸ’”

In the Union-Find algorithm, the elements are organized into sets, where each element belongs to a set and the sets are disjoint (i.e., they do not share any elements). Each set has a root or representative element, which is the entry point for the set.

Sets and Roots šŸŽÆ

In a given Union-Find data structure, each element is either a root or a part of a path leading to a root. To find the root of a given element, we follow the path upwards until we reach the root.

Implementing the Union-Find Algorithm šŸ’”

Let's implement a basic Union-Find algorithm using an array to store the roots and optimize the Find operation.

Basic Union-Find (with Optimized Find) šŸ“

python
def make_set(x): # Make x a new set with itself as the root parent[x] = x rank[x] = 0 def find_set(x): # Find the root of set containing x if parent[x] != x: parent[x] = find_set(parent[x]) return parent[x] def union_sets(x, y): # Combine sets containing x and y root_x = find_set(x) root_y = find_set(y) # If roots are different, merge sets if root_x != root_y: parent[root_y] = root_x rank[root_x] += rank[root_y]

Optimizing the Union Operation šŸ’”

The basic Union-Find implementation above has a time complexity of O(h) for the Union operation, where h is the height of the tree. To improve this, we can use the path compression and union by rank techniques to lower the average height of the trees and reduce the time complexity to O(log n).

Union-Find with Path Compression šŸ“

python
def find_set(x): # Find the root of set containing x using path compression if parent[x] != x: parent[x] = find_set(parent[x]) return parent[x] def union_sets(x, y): # Combine sets containing x and y using path compression and union by rank root_x = find_set(x) root_y = find_set(y) if root_x != root_y: if rank[root_x] < rank[root_y]: parent[root_x] = root_y rank[root_y] += rank[root_x] else: parent[root_y] = root_x rank[root_x] += rank[root_y]

Testing Our Implementation šŸ’”

Let's create a simple example to see our implementation in action.

python
# Initialize the arrays n = 10 parent = [i for i in range(n+1)] rank = [0]*n # Create sets and perform unions make_set(1) make_set(2) union_sets(1, 2) make_set(3) union_sets(1, 3) make_set(4) union_sets(2, 4) # Check the connected components print(find_set(1)) # Output: 1 print(find_set(2)) # Output: 1 print(find_set(3)) # Output: 1 print(find_set(4)) # Output: 2

Wrapping Up šŸ“

In this lesson, we've learned about the Disjoint Set Union (Union-Find) algorithm, understood its importance, and implemented a basic version with optimized Find and Union operations. By the end of this lesson, you should have a good grasp of this essential data structure and be ready to apply it in your own projects.

Quiz šŸŽÆ

Quick Quiz
Question 1 of 1

What is the main purpose of the Disjoint Set Union (Union-Find) algorithm?