Skip to content

Commit ca7c77f

Browse files
refactor: improving DisjointSetUnion (#6378)
* refactor: improving DisjointSetUnion * refactor: remove comment as it already in description --------- Co-authored-by: Deniz Altunkapan <93663085+DenizAltunkapan@users.noreply.github.com>
1 parent 7e37d94 commit ca7c77f

File tree

1 file changed

+38
-26
lines changed

1 file changed

+38
-26
lines changed
Lines changed: 38 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,53 +1,65 @@
11
package com.thealgorithms.datastructures.disjointsetunion;
22

33
/**
4-
* Disjoint Set Union or DSU is useful for solving problems related to connected components,
5-
* cycle detection in graphs, and maintaining relationships in disjoint sets of data.
6-
* It is commonly employed in graph algorithms and problems.
4+
* Disjoint Set Union (DSU), also known as Union-Find, is a data structure that tracks a set of elements
5+
* partitioned into disjoint (non-overlapping) subsets. It supports two primary operations efficiently:
76
*
8-
* @see <a href="https://en.wikipedia.org/wiki/Disjoint-set_data_structure">Disjoint Set Union</a>
7+
* <ul>
8+
* <li>Find: Determine which subset a particular element belongs to.</li>
9+
* <li>Union: Merge two subsets into a single subset.</li>
10+
* </ul>
11+
*
12+
* @see <a href="https://en.wikipedia.org/wiki/Disjoint-set_data_structure">Disjoint Set Union (Wikipedia)</a>
913
*/
1014
public class DisjointSetUnion<T> {
1115

1216
/**
13-
* Creates a new node of DSU with parent initialised as same node
17+
* Creates a new disjoint set containing the single specified element.
18+
*
19+
* @param value the element to be placed in a new singleton set
20+
* @return a node representing the new set
1421
*/
15-
public Node<T> makeSet(final T x) {
16-
return new Node<T>(x);
22+
public Node<T> makeSet(final T value) {
23+
return new Node<>(value);
1724
}
1825

1926
/**
20-
* Finds and returns the representative (root) element of the set to which a given element belongs.
21-
* This operation uses path compression to optimize future findSet operations.
27+
* Finds and returns the representative (root) of the set containing the given node.
28+
* This method applies path compression to flatten the tree structure for future efficiency.
29+
*
30+
* @param node the node whose set representative is to be found
31+
* @return the representative (root) node of the set
2232
*/
2333
public Node<T> findSet(Node<T> node) {
24-
while (node != node.parent) {
25-
node = node.parent;
34+
if (node != node.parent) {
35+
node.parent = findSet(node.parent);
2636
}
27-
return node;
37+
return node.parent;
2838
}
2939

3040
/**
31-
* Unions two sets by merging their representative elements. The merge is performed based on the rank of each set
32-
* to ensure efficient merging and path compression to optimize future findSet operations.
41+
* Merges the sets containing the two given nodes. Union by rank is used to attach the smaller tree under the larger one.
42+
* If both sets have the same rank, one becomes the parent and its rank is incremented.
43+
*
44+
* @param x a node in the first set
45+
* @param y a node in the second set
3346
*/
34-
public void unionSets(final Node<T> x, final Node<T> y) {
35-
Node<T> nx = findSet(x);
36-
Node<T> ny = findSet(y);
47+
public void unionSets(Node<T> x, Node<T> y) {
48+
Node<T> rootX = findSet(x);
49+
Node<T> rootY = findSet(y);
3750

38-
if (nx == ny) {
39-
return; // Both elements already belong to the same set.
51+
if (rootX == rootY) {
52+
return; // They are already in the same set
4053
}
4154
// Merging happens based on rank of node, this is done to avoid long chaining of nodes and reduce time
4255
// to find root of the component. Idea is to attach small components in big, instead of other way around.
43-
if (nx.rank > ny.rank) {
44-
ny.parent = nx;
45-
} else if (ny.rank > nx.rank) {
46-
nx.parent = ny;
56+
if (rootX.rank > rootY.rank) {
57+
rootY.parent = rootX;
58+
} else if (rootY.rank > rootX.rank) {
59+
rootX.parent = rootY;
4760
} else {
48-
// Both sets have the same rank; choose one as the parent and increment the rank.
49-
ny.parent = nx;
50-
nx.rank++;
61+
rootY.parent = rootX;
62+
rootX.rank++;
5163
}
5264
}
5365
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy