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