Skip to content

Commit 3a5ee8d

Browse files
committed
Apply google-java-format to Kruskal MST implementation
1 parent 54b8922 commit 3a5ee8d

File tree

3 files changed

+135
-145
lines changed

3 files changed

+135
-145
lines changed
Lines changed: 26 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,33 @@
11
package com.thealgorithms.graph;
22

3-
/**
4-
* Represents an edge in an undirected weighted graph.
5-
*/
3+
/** Represents an edge in an undirected weighted graph. */
64
public class Edge implements Comparable<Edge> {
75

8-
public final int source;
9-
public final int destination;
10-
public final int weight;
6+
public final int source;
7+
public final int destination;
8+
public final int weight;
119

12-
/**
13-
* Constructs an edge with given source, destination, and weight.
14-
*
15-
* @param source the source vertex
16-
* @param destination the destination vertex
17-
* @param weight the weight of the edge
18-
*/
19-
public Edge(final int source, final int destination, final int weight) {
20-
this.source = source;
21-
this.destination = destination;
22-
this.weight = weight;
23-
}
10+
/**
11+
* Constructs an edge with given source, destination, and weight.
12+
*
13+
* @param source the source vertex
14+
* @param destination the destination vertex
15+
* @param weight the weight of the edge
16+
*/
17+
public Edge(final int source, final int destination, final int weight) {
18+
this.source = source;
19+
this.destination = destination;
20+
this.weight = weight;
21+
}
2422

25-
/**
26-
* Compares edges based on their weight.
27-
*
28-
* @param other the edge to compare with
29-
* @return comparison result
30-
*/
31-
@Override
32-
public int compareTo(final Edge other) {
33-
return Integer.compare(this.weight, other.weight);
34-
}
23+
/**
24+
* Compares edges based on their weight.
25+
*
26+
* @param other the edge to compare with
27+
* @return comparison result
28+
*/
29+
@Override
30+
public int compareTo(final Edge other) {
31+
return Integer.compare(this.weight, other.weight);
32+
}
3533
}

src/main/java/com/thealgorithms/graph/KruskalMST.java

Lines changed: 62 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -6,90 +6,86 @@
66
import java.util.Objects;
77

88
/**
9-
* Implementation of Kruskal's Algorithm to find
10-
* the Minimum Spanning Tree (MST) of a connected,
9+
* Implementation of Kruskal's Algorithm to find the Minimum Spanning Tree (MST) of a connected,
1110
* undirected, weighted graph.
1211
*/
1312
public final class KruskalMST {
1413

15-
private KruskalMST() {
16-
// Utility class
14+
private KruskalMST() {
15+
// Utility class
16+
}
17+
18+
/**
19+
* Finds the Minimum Spanning Tree using Kruskal's Algorithm.
20+
*
21+
* @param vertices number of vertices in the graph
22+
* @param edges list of all edges in the graph
23+
* @return list of edges forming the MST
24+
* @throws IllegalArgumentException if vertices <= 0
25+
* @throws NullPointerException if edges is null
26+
*/
27+
public static List<Edge> findMST(final int vertices, final List<Edge> edges) {
28+
if (vertices <= 0) {
29+
throw new IllegalArgumentException("Number of vertices must be positive");
1730
}
1831

19-
/**
20-
* Finds the Minimum Spanning Tree using Kruskal's Algorithm.
21-
*
22-
* @param vertices number of vertices in the graph
23-
* @param edges list of all edges in the graph
24-
* @return list of edges forming the MST
25-
* @throws IllegalArgumentException if vertices <= 0
26-
* @throws NullPointerException if edges is null
27-
*/
28-
public static List<Edge> findMST(final int vertices, final List<Edge> edges) {
29-
if (vertices <= 0) {
30-
throw new IllegalArgumentException("Number of vertices must be positive");
31-
}
32-
33-
Objects.requireNonNull(edges, "Edges list must not be null");
32+
Objects.requireNonNull(edges, "Edges list must not be null");
3433

35-
final List<Edge> sortedEdges = new ArrayList<>(edges);
36-
Collections.sort(sortedEdges);
34+
final List<Edge> sortedEdges = new ArrayList<>(edges);
35+
Collections.sort(sortedEdges);
3736

38-
final List<Edge> mst = new ArrayList<>();
39-
final DisjointSetUnion dsu = new DisjointSetUnion(vertices);
37+
final List<Edge> mst = new ArrayList<>();
38+
final DisjointSetUnion dsu = new DisjointSetUnion(vertices);
4039

41-
for (final Edge edge : sortedEdges) {
42-
final int rootU = dsu.find(edge.source);
43-
final int rootV = dsu.find(edge.destination);
40+
for (final Edge edge : sortedEdges) {
41+
final int rootU = dsu.find(edge.source);
42+
final int rootV = dsu.find(edge.destination);
4443

45-
if (rootU != rootV) {
46-
mst.add(edge);
47-
dsu.union(rootU, rootV);
44+
if (rootU != rootV) {
45+
mst.add(edge);
46+
dsu.union(rootU, rootV);
4847

49-
if (mst.size() == vertices - 1) {
50-
break;
51-
}
52-
}
48+
if (mst.size() == vertices - 1) {
49+
break;
5350
}
54-
55-
return mst;
51+
}
5652
}
5753

58-
/**
59-
* Disjoint Set Union (Union-Find) with
60-
* path compression and union by rank.
61-
*/
62-
private static final class DisjointSetUnion {
54+
return mst;
55+
}
6356

64-
private final int[] parent;
65-
private final int[] rank;
57+
/** Disjoint Set Union (Union-Find) with path compression and union by rank. */
58+
private static final class DisjointSetUnion {
6659

67-
private DisjointSetUnion(final int size) {
68-
parent = new int[size];
69-
rank = new int[size];
60+
private final int[] parent;
61+
private final int[] rank;
7062

71-
for (int i = 0; i < size; i++) {
72-
parent[i] = i;
73-
rank[i] = 0;
74-
}
75-
}
63+
private DisjointSetUnion(final int size) {
64+
parent = new int[size];
65+
rank = new int[size];
7666

77-
private int find(final int node) {
78-
if (parent[node] != node) {
79-
parent[node] = find(parent[node]);
80-
}
81-
return parent[node];
82-
}
67+
for (int i = 0; i < size; i++) {
68+
parent[i] = i;
69+
rank[i] = 0;
70+
}
71+
}
8372

84-
private void union(final int u, final int v) {
85-
if (rank[u] < rank[v]) {
86-
parent[u] = v;
87-
} else if (rank[u] > rank[v]) {
88-
parent[v] = u;
89-
} else {
90-
parent[v] = u;
91-
rank[u]++;
92-
}
93-
}
73+
private int find(final int node) {
74+
if (parent[node] != node) {
75+
parent[node] = find(parent[node]);
76+
}
77+
return parent[node];
78+
}
79+
80+
private void union(final int u, final int v) {
81+
if (rank[u] < rank[v]) {
82+
parent[u] = v;
83+
} else if (rank[u] > rank[v]) {
84+
parent[v] = u;
85+
} else {
86+
parent[v] = u;
87+
rank[u]++;
88+
}
9489
}
90+
}
9591
}

src/test/java/com/thealgorithms/graph/KruskalMSTTest.java

Lines changed: 47 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -6,73 +6,69 @@
66

77
import java.util.ArrayList;
88
import java.util.List;
9-
109
import org.junit.jupiter.api.Test;
1110

12-
/**
13-
* Test cases for Kruskal's Minimum Spanning Tree algorithm.
14-
*/
11+
/** Test cases for Kruskal's Minimum Spanning Tree algorithm. */
1512
class KruskalMSTTest {
1613

17-
@Test
18-
void testFindMSTWithSimpleGraph() {
19-
final int vertices = 4;
20-
21-
final List<Edge> edges = new ArrayList<>();
22-
edges.add(new Edge(0, 1, 10));
23-
edges.add(new Edge(0, 2, 6));
24-
edges.add(new Edge(0, 3, 5));
25-
edges.add(new Edge(1, 3, 15));
26-
edges.add(new Edge(2, 3, 4));
14+
@Test
15+
void testFindMSTWithSimpleGraph() {
16+
final int vertices = 4;
2717

28-
final List<Edge> mst = KruskalMST.findMST(vertices, edges);
18+
final List<Edge> edges = new ArrayList<>();
19+
edges.add(new Edge(0, 1, 10));
20+
edges.add(new Edge(0, 2, 6));
21+
edges.add(new Edge(0, 3, 5));
22+
edges.add(new Edge(1, 3, 15));
23+
edges.add(new Edge(2, 3, 4));
2924

30-
assertNotNull(mst, "MST should not be null");
31-
assertEquals(vertices - 1, mst.size(), "MST should contain V-1 edges");
25+
final List<Edge> mst = KruskalMST.findMST(vertices, edges);
3226

33-
int totalWeight = 0;
34-
for (final Edge edge : mst) {
35-
totalWeight += edge.weight;
36-
}
27+
assertNotNull(mst, "MST should not be null");
28+
assertEquals(vertices - 1, mst.size(), "MST should contain V-1 edges");
3729

38-
assertEquals(19, totalWeight, "Total weight of MST is incorrect");
30+
int totalWeight = 0;
31+
for (final Edge edge : mst) {
32+
totalWeight += edge.weight;
3933
}
4034

41-
@Test
42-
void testFindMSTWithSingleVertex() {
43-
final int vertices = 1;
44-
final List<Edge> edges = new ArrayList<>();
35+
assertEquals(19, totalWeight, "Total weight of MST is incorrect");
36+
}
4537

46-
final List<Edge> mst = KruskalMST.findMST(vertices, edges);
38+
@Test
39+
void testFindMSTWithSingleVertex() {
40+
final int vertices = 1;
41+
final List<Edge> edges = new ArrayList<>();
4742

48-
assertNotNull(mst, "MST should not be null");
49-
assertEquals(0, mst.size(), "MST of single vertex graph should be empty");
50-
}
43+
final List<Edge> mst = KruskalMST.findMST(vertices, edges);
5144

52-
@Test
53-
void testInvalidVertexCountThrowsException() {
54-
final List<Edge> edges = new ArrayList<>();
45+
assertNotNull(mst, "MST should not be null");
46+
assertEquals(0, mst.size(), "MST of single vertex graph should be empty");
47+
}
5548

56-
assertThrows(
57-
IllegalArgumentException.class,
58-
() -> KruskalMST.findMST(0, edges),
59-
"Expected exception for non-positive vertex count"
60-
);
61-
}
49+
@Test
50+
void testInvalidVertexCountThrowsException() {
51+
final List<Edge> edges = new ArrayList<>();
6252

63-
@Test
64-
void testPathCompressionScenario() {
65-
final int vertices = 5;
53+
assertThrows(
54+
IllegalArgumentException.class,
55+
() -> KruskalMST.findMST(0, edges),
56+
"Expected exception for non-positive vertex count");
57+
}
6658

67-
final List<Edge> edges = new ArrayList<>();
68-
edges.add(new Edge(0, 1, 1));
69-
edges.add(new Edge(1, 2, 2));
70-
edges.add(new Edge(2, 3, 3));
71-
edges.add(new Edge(3, 4, 4));
59+
@Test
60+
void testPathCompressionScenario() {
61+
final int vertices = 5;
7262

73-
final List<Edge> mst = KruskalMST.findMST(vertices, edges);
63+
final List<Edge> edges = new ArrayList<>();
64+
edges.add(new Edge(0, 1, 1));
65+
edges.add(new Edge(1, 2, 2));
66+
edges.add(new Edge(2, 3, 3));
67+
edges.add(new Edge(3, 4, 4));
7468

75-
assertNotNull(mst);
76-
assertEquals(vertices - 1, mst.size(), "MST should contain V-1 edges");
77-
}
69+
final List<Edge> mst = KruskalMST.findMST(vertices, edges);
70+
71+
assertNotNull(mst);
72+
assertEquals(vertices - 1, mst.size(), "MST should contain V-1 edges");
73+
}
7874
}

0 commit comments

Comments
 (0)