1414 * <li>Using Union-Find (Disjoint Set Union) to efficiently detect cycles</li>
1515 * </ol>
1616 *
17- * <p><strong> Time Complexity:</strong> O(E log E) or O(E log V), where E is the number of edges
17+ * <p>Time Complexity: O(E log E) or O(E log V), where E is the number of edges
1818 * and V is the number of vertices. Sorting edges dominates the complexity.
1919 *
20- * <p><strong> Space Complexity:</strong> O(V) for the Union-Find data structure.
20+ * <p>Space Complexity: O(V) for the Union-Find data structure.
2121 *
22- * @author guillermolara01
22+ * @see <a href="https://en.wikipedia.org/wiki/Kruskal%27s_algorithm">Kruskal's Algorithm</a>
23+ * @author saikirankanakala
2324 */
2425public final class KruskalAlgorithm {
2526
2627 private KruskalAlgorithm () {
27- // Utility class, prevent instantiation
2828 }
2929
3030 /**
3131 * Represents a weighted edge in an undirected graph.
3232 */
33- public static class Edge implements Comparable <Edge > {
33+ public static final class Edge implements Comparable <Edge > {
3434 private final int source ;
3535 private final int destination ;
3636 private final int weight ;
3737
38- /**
39- * Creates an edge with the specified endpoints and weight.
40- *
41- * @param source the source vertex
42- * @param destination the destination vertex
43- * @param weight the weight of the edge
44- */
4538 public Edge (int source , int destination , int weight ) {
4639 this .source = source ;
4740 this .destination = destination ;
@@ -72,18 +65,12 @@ public String toString() {
7265 }
7366
7467 /**
75- * Union-Find (Disjoint Set Union) data structure with path compression
76- * and union by rank for efficient cycle detection.
68+ * Union-Find data structure with path compression and union by rank.
7769 */
78- private static class UnionFind {
70+ private static final class UnionFind {
7971 private final int [] parent ;
8072 private final int [] rank ;
8173
82- /**
83- * Initializes the Union-Find structure with n elements.
84- *
85- * @param n the number of elements
86- */
8774 UnionFind (int n ) {
8875 parent = new int [n ];
8976 rank = new int [n ];
@@ -93,37 +80,21 @@ private static class UnionFind {
9380 }
9481 }
9582
96- /**
97- * Finds the representative (root) of the set containing element x.
98- * Uses path compression for optimization.
99- *
100- * @param x the element to find
101- * @return the representative of the set
102- */
10383 int find (int x ) {
10484 if (parent [x ] != x ) {
105- parent [x ] = find (parent [x ]); // Path compression
85+ parent [x ] = find (parent [x ]);
10686 }
10787 return parent [x ];
10888 }
10989
110- /**
111- * Unites the sets containing elements x and y.
112- * Uses union by rank for optimization.
113- *
114- * @param x the first element
115- * @param y the second element
116- * @return true if the sets were merged, false if they were already in the same set
117- */
11890 boolean union (int x , int y ) {
11991 int rootX = find (x );
12092 int rootY = find (y );
12193
12294 if (rootX == rootY ) {
123- return false ; // Already in the same set (would create a cycle)
95+ return false ;
12496 }
12597
126- // Union by rank
12798 if (rank [rootX ] < rank [rootY ]) {
12899 parent [rootX ] = rootY ;
129100 } else if (rank [rootX ] > rank [rootY ]) {
@@ -137,11 +108,11 @@ boolean union(int x, int y) {
137108 }
138109
139110 /**
140- * Finds the Minimum Spanning Tree of a graph using Kruskal's algorithm.
111+ * Finds the Minimum Spanning Tree using Kruskal's algorithm.
141112 *
142113 * @param vertices the number of vertices in the graph
143114 * @param edges the list of edges in the graph
144- * @return a list of edges that form the MST
115+ * @return a list of edges forming the MST
145116 * @throws IllegalArgumentException if vertices is less than 1 or edges is null
146117 */
147118 public static List <Edge > kruskal (int vertices , List <Edge > edges ) {
@@ -157,18 +128,12 @@ public static List<Edge> kruskal(int vertices, List<Edge> edges) {
157128 return mst ;
158129 }
159130
160- // Sort edges by weight
161131 edges .sort (Edge ::compareTo );
162-
163132 UnionFind uf = new UnionFind (vertices );
164133
165- // Iterate through sorted edges
166134 for (Edge edge : edges ) {
167- // If adding this edge doesn't create a cycle, include it in MST
168135 if (uf .union (edge .getSource (), edge .getDestination ())) {
169136 mst .add (edge );
170-
171- // MST is complete when we have (V-1) edges
172137 if (mst .size () == vertices - 1 ) {
173138 break ;
174139 }
@@ -182,7 +147,7 @@ public static List<Edge> kruskal(int vertices, List<Edge> edges) {
182147 * Calculates the total weight of the MST.
183148 *
184149 * @param mst the list of edges in the MST
185- * @return the total weight of all edges in the MST
150+ * @return the total weight of all edges
186151 */
187152 public static int getMSTWeight (List <Edge > mst ) {
188153 return mst .stream ().mapToInt (Edge ::getWeight ).sum ();
0 commit comments