Skip to content

Commit 3d194be

Browse files
fix: format code, add Wikipedia reference, and clean up javadoc
1 parent 87ef61f commit 3d194be

File tree

2 files changed

+22
-67
lines changed

2 files changed

+22
-67
lines changed

src/main/java/com/thealgorithms/greedyalgorithms/KruskalAlgorithm.java

Lines changed: 12 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -14,34 +14,27 @@
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
*/
2425
public 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();

src/test/java/com/thealgorithms/greedyalgorithms/KruskalAlgorithmTest.java

Lines changed: 10 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,6 @@ class KruskalAlgorithmTest {
1212

1313
@Test
1414
void testSimpleGraph() {
15-
// Graph with 4 vertices
16-
// 1
17-
// / | \
18-
// 2 3 4
19-
// 0---1---2
20-
// | |
21-
// 3-------4
2215
List<KruskalAlgorithm.Edge> edges = new ArrayList<>();
2316
edges.add(new KruskalAlgorithm.Edge(0, 1, 10));
2417
edges.add(new KruskalAlgorithm.Edge(0, 2, 6));
@@ -28,35 +21,33 @@ void testSimpleGraph() {
2821

2922
List<KruskalAlgorithm.Edge> mst = KruskalAlgorithm.kruskal(4, edges);
3023

31-
assertEquals(3, mst.size(), "MST should have V-1 edges");
32-
assertEquals(19, KruskalAlgorithm.getMSTWeight(mst), "Total MST weight should be 19");
24+
assertEquals(3, mst.size());
25+
assertEquals(19, KruskalAlgorithm.getMSTWeight(mst));
3326
}
3427

3528
@Test
3629
void testDisconnectedGraph() {
37-
// Two disconnected components
3830
List<KruskalAlgorithm.Edge> edges = new ArrayList<>();
3931
edges.add(new KruskalAlgorithm.Edge(0, 1, 1));
4032
edges.add(new KruskalAlgorithm.Edge(2, 3, 2));
4133

4234
List<KruskalAlgorithm.Edge> mst = KruskalAlgorithm.kruskal(4, edges);
4335

44-
assertEquals(2, mst.size(), "MST should include both components");
45-
assertEquals(3, KruskalAlgorithm.getMSTWeight(mst), "Total MST weight should be 3");
36+
assertEquals(2, mst.size());
37+
assertEquals(3, KruskalAlgorithm.getMSTWeight(mst));
4638
}
4739

4840
@Test
4941
void testSingleVertex() {
5042
List<KruskalAlgorithm.Edge> edges = new ArrayList<>();
5143
List<KruskalAlgorithm.Edge> mst = KruskalAlgorithm.kruskal(1, edges);
5244

53-
assertTrue(mst.isEmpty(), "MST of single vertex should be empty");
45+
assertTrue(mst.isEmpty());
5446
assertEquals(0, KruskalAlgorithm.getMSTWeight(mst));
5547
}
5648

5749
@Test
5850
void testCompleteGraph() {
59-
// Complete graph with 4 vertices (K4)
6051
List<KruskalAlgorithm.Edge> edges = new ArrayList<>();
6152
edges.add(new KruskalAlgorithm.Edge(0, 1, 1));
6253
edges.add(new KruskalAlgorithm.Edge(0, 2, 2));
@@ -67,30 +58,29 @@ void testCompleteGraph() {
6758

6859
List<KruskalAlgorithm.Edge> mst = KruskalAlgorithm.kruskal(4, edges);
6960

70-
assertEquals(3, mst.size(), "MST should have V-1 edges");
71-
assertEquals(6, KruskalAlgorithm.getMSTWeight(mst), "Total MST weight should be 6 (1+2+3)");
61+
assertEquals(3, mst.size());
62+
assertEquals(6, KruskalAlgorithm.getMSTWeight(mst));
7263
}
7364

7465
@Test
7566
void testEqualWeights() {
76-
// Graph where all edges have the same weight
7767
List<KruskalAlgorithm.Edge> edges = new ArrayList<>();
7868
edges.add(new KruskalAlgorithm.Edge(0, 1, 1));
7969
edges.add(new KruskalAlgorithm.Edge(1, 2, 1));
8070
edges.add(new KruskalAlgorithm.Edge(2, 0, 1));
8171

8272
List<KruskalAlgorithm.Edge> mst = KruskalAlgorithm.kruskal(3, edges);
8373

84-
assertEquals(2, mst.size(), "MST should have V-1 edges");
85-
assertEquals(2, KruskalAlgorithm.getMSTWeight(mst), "Total MST weight should be 2");
74+
assertEquals(2, mst.size());
75+
assertEquals(2, KruskalAlgorithm.getMSTWeight(mst));
8676
}
8777

8878
@Test
8979
void testEmptyGraph() {
9080
List<KruskalAlgorithm.Edge> edges = new ArrayList<>();
9181
List<KruskalAlgorithm.Edge> mst = KruskalAlgorithm.kruskal(5, edges);
9282

93-
assertTrue(mst.isEmpty(), "MST of empty graph should be empty");
83+
assertTrue(mst.isEmpty());
9484
}
9585

9686
@Test

0 commit comments

Comments
 (0)