-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathKruskalMinimumSpanningTree.java
81 lines (70 loc) · 2.22 KB
/
KruskalMinimumSpanningTree.java
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
package graph;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class KruskalMinimumSpanningTree {
private final List<Edge> edges;
private final int[] parent;
private final int[] rank;
private final int noOfVertices;
public KruskalMinimumSpanningTree(List<Edge> edges, int noOfVertices) {
this.noOfVertices = noOfVertices;
this.edges = edges;
Collections.sort(edges);
this.parent = new int[noOfVertices];
this.rank = new int[noOfVertices];
for (int i = 0; i < this.noOfVertices; i++) {
this.parent[i] = i;
}
}
// union by rank
private void union(int x, int y) {
int xParent = find(x);
int yParent = find(y);
if (xParent == yParent) {
return;
}
if (rank[xParent] < rank[yParent]) {
parent[xParent] = yParent;
} else if (rank[xParent] > rank[yParent]) {
parent[yParent] = xParent;
} else {
parent[yParent] = xParent;
rank[xParent]++;
}
}
// path compression
private int find(int x) {
if (x == parent[x]) {
return x;
}
parent[x] = find(parent[x]);
return parent[x];
}
public int mst() {
int result = 0;
for (int i = 0, s = 0; i < edges.size() && s < this.noOfVertices; i++) {
Edge e = edges.get(i);
int uParent = find(e.getSource());
int vParent = find(e.getDestination());
if (uParent != vParent) {
result += e.getWeight();
union(uParent, vParent);
s++;
}
}
return result;
}
public static void main(String[] args) {
List<Edge> edges = new ArrayList<>();
edges.add(new Edge(0, 1, 10));
edges.add(new Edge(0, 2, 8));
edges.add(new Edge(1, 2, 5));
edges.add(new Edge(1, 3, 3));
edges.add(new Edge(2, 3, 4));
edges.add(new Edge(2, 4, 12));
edges.add(new Edge(3, 4, 15));
KruskalMinimumSpanningTree kmst = new KruskalMinimumSpanningTree(edges, 5);
System.out.println("Minimum spanning tree: " + kmst.mst());
}
}