-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMST.java
More file actions
128 lines (107 loc) · 3.54 KB
/
MST.java
File metadata and controls
128 lines (107 loc) · 3.54 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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
// Jingze Dai, 400201059, daij24
import java.util.*;
import java.lang.*;
class MST {
private static class Edge implements Comparable<Edge> {
int src, dest, weight;
public int compareTo(Edge target) {
return this.weight - target.weight;
}
}
private static class subset {
int parent, rank;
}
private int V;
private Edge[] edges;
private MST(int vertex, int edge) {
try {
V = vertex;
edges = new Edge[edge];
for (int i = 0; i < edge; ++i) {
edges[i] = new Edge();
}
} catch (IllegalArgumentException e){
System.out.println("Wrong input vertex or edge values");
System.out.println("Reminder: V is positive integer");
System.out.println("Reminder: E is positive integer");
}
}
private int find(subset[] subsets, int i) {
if (subsets[i].parent != i) {
subsets[i].parent = find(subsets, subsets[i].parent);
}
return subsets[i].parent;
}
private void Union(subset[] sbtt, int index1, int index2) {
int root1 = find(sbtt, index1);
int root2 = find(sbtt, index2);
if (sbtt[root1].rank < sbtt[root2].rank) {
sbtt[root1].parent = root2;
} else if (sbtt[root1].rank > sbtt[root2].rank) {
sbtt[root2].parent = root1;
} else {
sbtt[root2].parent = root1;
sbtt[root1].rank = sbtt[root1].rank + 1;
}
}
private void Kruskal() {
Edge[] result = new Edge[V];
int amount_edge = 0;
int i = 0;
while (i < V) {
result[i] = new Edge();
++i;
}
Arrays.sort(edges);
subset[] sbtt = new subset[V];
i = 0;
while (i < V) {
sbtt[i] = new subset();
sbtt[i].parent = i;
sbtt[i].rank = 0;
++i;
}
i = 0;
while (amount_edge < V - 1) {
Edge next_edge = edges[i++];
int x = find(sbtt, next_edge.src);
int y = find(sbtt, next_edge.dest);
if (x != y) {
result[amount_edge++] = next_edge;
Union(sbtt, x, y);
}
}
System.out.println("MST edges: ");
System.out.println();
int minimumCost = 0;
for (i = 0; i < amount_edge; ++i) {
System.out.println("Edge " + result[i].src + " - " + result[i].dest);
System.out.println("Edge Cost: " + result[i].weight);
System.out.println();
minimumCost += result[i].weight;
}
System.out.println("Total MST Cost: " + minimumCost);
}
public static void main(String[] args) {
MST Graph1 = new MST(5, 6);
Graph1.edges[0].src = 0;
Graph1.edges[0].dest = 1;
Graph1.edges[0].weight = 4;
Graph1.edges[1].src = 0;
Graph1.edges[1].dest = 2;
Graph1.edges[1].weight = 7;
Graph1.edges[2].src = 0;
Graph1.edges[2].dest = 3;
Graph1.edges[2].weight = 15;
Graph1.edges[3].src = 1;
Graph1.edges[3].dest = 3;
Graph1.edges[3].weight = 5;
Graph1.edges[4].src = 2;
Graph1.edges[4].dest = 3;
Graph1.edges[4].weight = 8;
Graph1.edges[5].src = 1;
Graph1.edges[5].dest = 4;
Graph1.edges[5].weight = 6;
Graph1.Kruskal();
}
}