From 0580fffd860d142b7be8616d08b01a2f1a6958ba Mon Sep 17 00:00:00 2001 From: alxkm Date: Sun, 13 Jul 2025 13:46:26 +0200 Subject: [PATCH 1/2] refactor: improving DisjointSetUnion --- .../disjointsetunion/DisjointSetUnion.java | 64 +++++++++++-------- 1 file changed, 38 insertions(+), 26 deletions(-) diff --git a/src/main/java/com/thealgorithms/datastructures/disjointsetunion/DisjointSetUnion.java b/src/main/java/com/thealgorithms/datastructures/disjointsetunion/DisjointSetUnion.java index 583800998c81..f2aff8826e26 100644 --- a/src/main/java/com/thealgorithms/datastructures/disjointsetunion/DisjointSetUnion.java +++ b/src/main/java/com/thealgorithms/datastructures/disjointsetunion/DisjointSetUnion.java @@ -1,53 +1,65 @@ package com.thealgorithms.datastructures.disjointsetunion; /** - * Disjoint Set Union or DSU is useful for solving problems related to connected components, - * cycle detection in graphs, and maintaining relationships in disjoint sets of data. - * It is commonly employed in graph algorithms and problems. + * Disjoint Set Union (DSU), also known as Union-Find, is a data structure that tracks a set of elements + * partitioned into disjoint (non-overlapping) subsets. It supports two primary operations efficiently: * - * @see Disjoint Set Union + * + * + * @see Disjoint Set Union (Wikipedia) */ public class DisjointSetUnion { /** - * Creates a new node of DSU with parent initialised as same node + * Creates a new disjoint set containing the single specified element. + * + * @param value the element to be placed in a new singleton set + * @return a node representing the new set */ - public Node makeSet(final T x) { - return new Node(x); + public Node makeSet(final T value) { + return new Node<>(value); } /** - * Finds and returns the representative (root) element of the set to which a given element belongs. - * This operation uses path compression to optimize future findSet operations. + * Finds and returns the representative (root) of the set containing the given node. + * This method applies path compression to flatten the tree structure for future efficiency. + * + * @param node the node whose set representative is to be found + * @return the representative (root) node of the set */ public Node findSet(Node node) { - while (node != node.parent) { - node = node.parent; + if (node != node.parent) { + node.parent = findSet(node.parent); // Path compression } - return node; + return node.parent; } /** - * Unions two sets by merging their representative elements. The merge is performed based on the rank of each set - * to ensure efficient merging and path compression to optimize future findSet operations. + * Merges the sets containing the two given nodes. Union by rank is used to attach the smaller tree under the larger one. + * If both sets have the same rank, one becomes the parent and its rank is incremented. + * + * @param x a node in the first set + * @param y a node in the second set */ - public void unionSets(final Node x, final Node y) { - Node nx = findSet(x); - Node ny = findSet(y); + public void unionSets(Node x, Node y) { + Node rootX = findSet(x); + Node rootY = findSet(y); - if (nx == ny) { - return; // Both elements already belong to the same set. + if (rootX == rootY) { + return; // They are already in the same set } // Merging happens based on rank of node, this is done to avoid long chaining of nodes and reduce time // to find root of the component. Idea is to attach small components in big, instead of other way around. - if (nx.rank > ny.rank) { - ny.parent = nx; - } else if (ny.rank > nx.rank) { - nx.parent = ny; + if (rootX.rank > rootY.rank) { + rootY.parent = rootX; + } else if (rootY.rank > rootX.rank) { + rootX.parent = rootY; } else { - // Both sets have the same rank; choose one as the parent and increment the rank. - ny.parent = nx; - nx.rank++; + rootY.parent = rootX; + rootX.rank++; } } } From 5a5d3a0c457a5efac30cd258880f8910180a92e5 Mon Sep 17 00:00:00 2001 From: alxkm Date: Sun, 13 Jul 2025 13:50:22 +0200 Subject: [PATCH 2/2] refactor: remove comment as it already in description --- .../datastructures/disjointsetunion/DisjointSetUnion.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/com/thealgorithms/datastructures/disjointsetunion/DisjointSetUnion.java b/src/main/java/com/thealgorithms/datastructures/disjointsetunion/DisjointSetUnion.java index f2aff8826e26..55951be82c8a 100644 --- a/src/main/java/com/thealgorithms/datastructures/disjointsetunion/DisjointSetUnion.java +++ b/src/main/java/com/thealgorithms/datastructures/disjointsetunion/DisjointSetUnion.java @@ -32,7 +32,7 @@ public Node makeSet(final T value) { */ public Node findSet(Node node) { if (node != node.parent) { - node.parent = findSet(node.parent); // Path compression + node.parent = findSet(node.parent); } return node.parent; }