Skip to content

Commit bf8b2d3

Browse files
Add Kruskal's algorithm for MST using union-find
1 parent 768b5a2 commit bf8b2d3

File tree

2 files changed

+145
-0
lines changed

2 files changed

+145
-0
lines changed
Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
package com.thealgorithms.greedyalgorithms;
2+
3+
import java.util.ArrayList;
4+
import java.util.Collections;
5+
import java.util.List;
6+
7+
/**
8+
* Kruskal's algorithm for Minimum Spanning Tree (MST).
9+
* Uses Disjoint Set Union (Union-Find).
10+
*
11+
* @author prasanth-30011
12+
*/
13+
public final class KruskalMST {
14+
15+
private KruskalMST() {
16+
// utility class
17+
}
18+
19+
public static class Edge implements Comparable<Edge> {
20+
public final int u;
21+
public final int v;
22+
public final int weight;
23+
24+
public Edge(int u, int v, int weight) {
25+
this.u = u;
26+
this.v = v;
27+
this.weight = weight;
28+
}
29+
30+
@Override
31+
public int compareTo(Edge other) {
32+
return Integer.compare(this.weight, other.weight);
33+
}
34+
}
35+
36+
private static class DSU {
37+
private final int[] parent;
38+
private final int[] rank;
39+
40+
DSU(int n) {
41+
parent = new int[n];
42+
rank = new int[n];
43+
for (int i = 0; i < n; i++) {
44+
parent[i] = i;
45+
rank[i] = 0;
46+
}
47+
}
48+
49+
int find(int x) {
50+
if (parent[x] != x) {
51+
parent[x] = find(parent[x]);
52+
}
53+
return parent[x];
54+
}
55+
56+
boolean union(int x, int y) {
57+
int rx = find(x);
58+
int ry = find(y);
59+
if (rx == ry) {
60+
return false;
61+
}
62+
if (rank[rx] < rank[ry]) {
63+
parent[rx] = ry;
64+
} else if (rank[rx] > rank[ry]) {
65+
parent[ry] = rx;
66+
} else {
67+
parent[ry] = rx;
68+
rank[rx]++;
69+
}
70+
return true;
71+
}
72+
}
73+
74+
/**
75+
* Computes the total weight of a Minimum Spanning Tree.
76+
*
77+
* @param n number of vertices (0..n-1)
78+
* @param edges list of edges
79+
* @return total MST weight, or -1 if graph is disconnected
80+
*/
81+
public static int minimumSpanningTreeWeight(int n, List<Edge> edges) {
82+
if (n <= 0) {
83+
throw new IllegalArgumentException("Number of vertices must be positive");
84+
}
85+
86+
List<Edge> sortedEdges = new ArrayList<>(edges);
87+
Collections.sort(sortedEdges);
88+
89+
DSU dsu = new DSU(n);
90+
int mstWeight = 0;
91+
int edgesUsed = 0;
92+
93+
for (Edge e : sortedEdges) {
94+
if (dsu.union(e.u, e.v)) {
95+
mstWeight += e.weight;
96+
edgesUsed++;
97+
if (edgesUsed == n - 1) {
98+
break;
99+
}
100+
}
101+
}
102+
103+
if (edgesUsed != n - 1) {
104+
return -1; // graph not connected
105+
}
106+
return mstWeight;
107+
}
108+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package com.thealgorithms.greedyalgorithms;
2+
3+
import static org.junit.jupiter.api.Assertions.assertEquals;
4+
5+
import java.util.List;
6+
import org.junit.jupiter.api.Test;
7+
8+
class KruskalMSTTest {
9+
10+
@Test
11+
void testSimpleConnectedGraph() {
12+
int n = 4;
13+
List<KruskalMST.Edge> edges = List.of(
14+
new KruskalMST.Edge(0, 1, 10),
15+
new KruskalMST.Edge(0, 2, 6),
16+
new KruskalMST.Edge(0, 3, 5),
17+
new KruskalMST.Edge(1, 3, 15),
18+
new KruskalMST.Edge(2, 3, 4)
19+
);
20+
21+
int mstWeight = KruskalMST.minimumSpanningTreeWeight(n, edges);
22+
// Known MST: edges (2-3, 0-3, 0-1) = 4 + 5 + 10 = 19
23+
assertEquals(19, mstWeight);
24+
}
25+
26+
@Test
27+
void testDisconnectedGraph() {
28+
int n = 4;
29+
List<KruskalMST.Edge> edges = List.of(
30+
new KruskalMST.Edge(0, 1, 3),
31+
new KruskalMST.Edge(2, 3, 5)
32+
);
33+
34+
int mstWeight = KruskalMST.minimumSpanningTreeWeight(n, edges);
35+
assertEquals(-1, mstWeight);
36+
}
37+
}

0 commit comments

Comments
 (0)