# Disjoint Set ADT in Data Structure

# Disjoint Set ADT in Data Structure

The Disjoint Set is an assortment of disjoint (non-overlapping) sets that keep up using ADT (Abstract Data Type). It is also referred to as the Union-Find data structure. It offers operations to effectively identify representative elements for each set and determine which set an element belongs to. It also offers operations to combine sets.

An array-based technique or a tree-based approach both can be used to build the Disjoint Set ADT.

Each element is represented by a node in a tree in the implementation that is tree-based. Every tree has a root that symbolizes the set's representative element. The tree has a single node at the start, and each element is in its own set.

**These are the common operations that the Disjoint Set ADT supports:**

**MakeSet(x):**It produces a brand-new set from a single element, x. In this implementation, x serves as the root of a brand-new tree.**Find(x):**It gives the root or representative member of the set that contains x. To complete this procedure, the tree must be climbed from node x to the root.

It is possible to use path compression throughout the traverse. To compress a path, the parent of each node that is traversed must be updated to point straight to the root. By flattening the tree, this compression makes future Find operations more efficient.

3. **Union(x,y):** here, the sets containing the components x and y are combined into a single set by the operation of union(x, y). To conduct the union, use the Find operation to locate the roots of the trees that contain x and y. If the roots differ, you should make one of them the parent of the other, essentially combining the two trees. The elements are already in the same set if the roots are the same, so there is no need for further action.

Union by rank is a further optimization that can be applied to improve this process. A rating that roughly correspondsto a node's height is given to each node in the tree.The lower-ranking tree and the higher-ranking tree are combined during the merger. If the ranks of the two trees are equal, one tree is picked at random and has its rank raised.

The disjoint-set forest, a data structure made up of several trees, is frequently used to implement the Disjoint Set ADT. The root of the tree serves as the representative element for each set that it represents.

## Implementation of the Disjoint Set ADT

**Two main methods are frequently employed to implement the Disjoint Set ADT effectively:**

### 1.Union by Rank

Each node in the disjoint-set forest is given a rank (or height) value. This is known as a union by rank. The set with the lower rank is combined with the set with the higher rank when conducting a union operation. Lowering the height of the trees as a whole, this optimization increases the effectiveness of the following processes.

### 2.Path Compression

During the Find operation, the path from an element to its representative (root) is condensed by making each visited node immediately point to the root. The tree structure flattens as a result of this compression, which also speeds up future operations by making it quicker to locate the representative element.

The Disjoint Set ADT achieves an amortized temporal complexity of O(alpha(n)), where alpha(n) is the inverse Ackermann function, by utilizing the tree-based implementation with route compression and union by rank optimizations. The operations are very effective since this complexity is regarded as almost constant in practice.

The Disjoint Set ADT is frequently used in a variety of applications, including graph techniques like Kruskal's approach for determining minimal spanning trees, linked component analysis, and cycle detection. It offers an effective technique to handle disconnected sets and establish their connections.

Let’s consider an example to demonstrate the Disjoint Set ADT.

**Example:**

Consider a scenario in which we want to use the Disjoint Set ADT to perform operations on a set of elements that includes 1, 2, 3, 4, 5, 6, 7, 8, 9, and 10.

- There are initially 10 separate sets because each element is on its own.

These are sets{1}, {2}, {3}, {4}, {5}, {6}, {7}, {8}, {9}, {10}.

2. Let's now combine sets by performing some union operations:

- Union(1, 2) joins sets with components 1 and 2 together.
- Union(2, 3): This combines sets with elements 2 and 3 in them.
- Union(4, 5): Combines sets with components 4 and 5 in them.
- Union(7, 8) joins sets that contain the elements 7 and 8 together.

Following these operations, the sets are 1, 2, 3, 5, 6, 7, 8, 9, and 10.

3. Let's carry out a few more Union operations:

- Union(3, 5): This combines sets that include the components 3 and 5.
- Union(6, 9): This combines sets with elements 6 and 9 in them.

The sets are transformed by these operations into:

{1, 2, 3, 4, 5}, {6, 9}, {7, 8}, {10}

4. Let's use the Find operation to find the representative elements (roots) of a few items now:

- Find(1):Returns the fifth element, which is the representative element in the set that contains 1.
- Find(4): Returns the fifth member, which is the representative element of the collection containing 4.
- Find(6):Returns the ninth element, which is the representative of the set containing the number 6, as the result.
- Find(10):Returns the representative member of the set containing 10, which is 10.

These processes have combined sets and allowed us to identify the representative elements of distinct elements. This exemplifies the Disjoint Set ADT's capability.

It should be noted that to obtain effective performance, the Disjoint Set ADT would be implemented using suitable data structures and methods, such as path compression and union by rank. The aforementioned illustration shows how the ADT can be used conceptually.

## Advantages of Disjoint Set ADT

**Efficient Operations:**The Disjoint Set ADT offers quick procedures for combining sets, locating the representative element (root), and generating new sets. These processes' average time complexity is almost constant, making a wide range of applications possible.**Analysis of Connectivity in Graphs:**The Disjoint Set ADT is especially helpful for studying connectivity in graphs. It enables you to quickly assess whether two components are part of the same set or whether two sets are not connected. Finding related components or spotting cycles in a graph is just a couple of the issues that this knowledge might help with.**Path Compression:**Path compression is used by the Disjoint Set ADT to improve the Find function. By making every node point directly to the root of the tree, path compression lowers the height of the tree and speeds up future search operations. Even when the number of elements and sets increases, this optimization aids in maintaining efficient performance.**Rank-Based Union:**The Disjoint Set ADT employs the union by rank optimization to maintain the balance of the resulting tree. It avoids the tree growing too tall, which would otherwise raise the time complexity of subsequent operations, by merging the smaller set into the larger set.**Versatile and useful:**The Disjoint Set ADT can be used to solve a variety of graph-related issues as well as locate connected components in graphs and implement Kruskal's approach for minimum spanning trees. Its adaptability makes it a useful tool for graph theory and algorithm creation.

## Disadvantages of Disjoint Set ADT

**Limited Functionality:**The Disjoint Set ADT is only capable of handling disjoint sets and carrying out set merging and representative element search operations. For operations like set intersection, set difference, or set element removal, it does not have built-in functionality. You might need to combine the Disjoint Set ADT with other data structures if your use case calls for such actions.**Lack of Element-level Operations:**The Disjoint Set's lack of element-level operations Direct actions on the element level, such as adding or removing a single element, are not available with ADT. It is less appropriate for situations where you regularly need to edit individual pieces in isolation because it primarily deals with sets as a whole.**Space complexity:**The Disjoint Set ADT may require more memory to store the required structures for path compression and union optimizations. Although the space overhead is usually acceptable, it should be taken into account for large-scale applications.**Neither set intersection nor set difference is supported:**The main objectives of the Disjoint Set ADT are the union of sets (Union operation) and the search for representative elements (Find operation). The operations of set intersection and set difference are not directly supported. Additional logic and data structures would be needed for these operations.**No direct access to individual elements:**The Disjoint Set ADT does not offer direct access to the elements contained within sets. Maintaining the disjoint sets and identifying their representative components are its key goals. Direct element access may necessitate the development of new data structures or changes to existing ones.

The benefits of effective Union and Find operations, as well as optimizations like Union by Rank and Path Compression, make the Disjoint Set ADT a potent tool for a variety of applications involving disjoint sets. When contemplating its application in particular contexts, nevertheless, it is important to keep in mind its restrictions on direct element access and lack of direct support for set intersection or set difference.