Javatpoint Logo
Javatpoint Logo

Introduction to Disjoint Set (Union-Find Algorithm) using Python


In computer science, the Disjoint Set, commonly called the Union-Find data structure, is an effective tool for maintaining sets of objects and providing answers to queries regarding their connectedness. The Union-Find Algorithm in Python, frequently used to create the Disjoint Set, is incredibly effective at solving problems including unique or non-overlapping sets. Graph theory, networking of computers, & image processing are just a few of the diverse domains it has applications in.

Key Operations

There are two main operations that the Disjoint Set data structure supports:

Union (Merge or Join): Consolidating two sets into one set.

Find (Connected or Find Representative): Determine a set's representative (leader) element to see if two elements are a part of the same set using the Find method.





We wish to divide a collection of six elements: {0, 1, 2, 3, 4, 5} into groupings based on specific criteria. We'll conduct the union and find operations on these components using the Disjoint Set data structure.

1. Initialization

We begin with each element as its own set therefore; our Disjoint Set initially has the following values: [0, 1, 2, 3, 4, 5] for the parent array and [0, 0, 0, 0, 0, 0] for the rank array.

{0}, {1}, {2}, {3}, {4}, {5}

2. Union Operation

Let's use union operations to put the following items together:

Union(0, 1)

  • The elements 0 and 1 are combined. This process changes the parent of element 0 to element 1 or the opposite. Because it now has a child, we also raise the rank of 1.
  • Following this procedure, our Disjoint Set appears as follows:

{0, 1}, {2}, {3}, {4}, {5}

  • The rank array changes to [0, 1, 0, 0, 0, 0] and the parent array changes to [1, 1, 2, 3, 4, 5].

Union (2, 3)

  • The elements 2 and 3 are combined. With this procedure, the parent of element 2 is changed to element 3 or vice versa. Given that it now has a child, we also raise the rank of 3 to that.
  • Following this procedure, our Disjoint Set appears as follows:

{0, 1}, {2, 3}, {4}, {5}

  • The rank array changes to [0, 1, 0, 1, 0, 0] and the parent array changes to [1, 1, 3, 3, 4, 5].

Union(0, 2)

  • Element 0 (which is in the set "0, 1") and element 2 (which is in the set "2, 3") are united. Due to its higher rank than element 2, this Operation changes element 0's parent from 2 to 3.
  • Following this procedure, our Disjoint Set appears as follows:

{0, 1, 2, 3}, {4}, {5}

  • The rank array changes to [0, 1, 0, 2, 0] and the parent array changes to [3, 3, 3, 3, 4, 5].

3. Find Operation

  • Finding the representative (leading) element of a set and determining if two items are members of that set can be done using the find operation.
  • Find(0): We locate the 3 as the representation of element 0.
  • Find(4): We identify the element 4 representative, number 4.
  • These findings demonstrate that elements 0, 1, 2, and 3 are members of the same set, while element 4 is a member of a separate set.


The Disjoint Set is a flexible data structure for organising collections of objects and effectively detecting their connectedness. It is implemented in Python with the Union-Find Algorithm. It has uses in many fields, including network algorithms and graph theory. The Disjoint Set procedures Union and Find are crucial in this data structure. The Find operation locates the set's representative element, whereas the Union function combines two disparate sets into one. Together, these processes allow us to classify elements according to connectedness or other factors.

A parent array describes set relationships in a Disjoint Set implementation, while a rank array is used to optimize tree balance during Union operations. The Find operation's efficiency is further increased via path compression. We showed how the Disjoint Set functions, from initializing individual sets to conducting Union and Find operations, step-by-step. This robust data structure is a useful tool for algorithmic problem-solving because it simplifies difficult issues, including set operations and related components.

Youtube For Videos Join Our Youtube Channel: Join Now


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Trending Technologies

B.Tech / MCA