-
Notifications
You must be signed in to change notification settings - Fork 0
/
MST.java
205 lines (155 loc) · 5.2 KB
/
MST.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
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
import java.io.*;
import java.math.*;
import java.security.*;
import java.text.*;
import java.util.*;
import java.util.concurrent.*;
import java.util.function.*;
import java.util.regex.*;
import java.util.stream.*;
import static java.util.stream.Collectors.joining;
import static java.util.stream.Collectors.toList;
class Result {
/*
* Complete the 'lay_cable' function below.
*
* The function is expected to return an INTEGER.
* The function accepts following parameters:
* 1. INTEGER nodes
* 2. 2D_INTEGER_ARRAY edges
*/
public static int lay_cable(int nodes, List<List<Integer>> edges) {
Graph g = new Graph(nodes, edges);
int res = g.Kruskal();
return res;
}
}
class Graph{
int N; // cities
int M; // edges
Edge[] edge;
public int Kruskal(){
int minC = 0;
//Make-Sets for each Node
subset[] subsetArr = new subset[N+1];
for(int i = 0; i < subsetArr.length; i++){
subsetArr[i] = new subset(i, 0);
}
//sort edge list by ascending order
Arrays.sort(edge);
int count = 0;
int traceN = 0;
while(traceN < N-1){
// System.out.println("count "+count);
// System.out.println("array length "+edge.length);
Edge next = edge[count];
count++;
int u = find(subsetArr, next.start);
int v = find(subsetArr, next.finish);
if(u != v){
traceN++;
minC += next.cost;
Union(subsetArr, u, v);
}
}
return minC;
}
public Graph(int n, List<List<Integer>> input){
N = n;
M = input.size();
edge = new Edge[M];
for(int i = 0; i < M; i++){
List<Integer> temp = input.get(i);
edge[i] = new Edge(temp.get(0), temp.get(1), temp.get(2));
}
}
//Recursively find the parent of an element
public int find(subset[] subsetArr, int element){
if(subsetArr[element].parent != element){
subsetArr[element].parent = find(subsetArr, subsetArr[element].parent);
}
// System.out.println("element "+element);
// System.out.println("find parent "+subsetArr[element].parent);
return subsetArr[element].parent;
}
public void Union(subset[] subsetArr, int x, int y){
// System.out.println("union ");
int xRep = find(subsetArr, x);
int yRep = find(subsetArr, y);
//Attach smaller priority to larger priority
if(subsetArr[xRep].priority > subsetArr[yRep].priority){
subsetArr[yRep].parent = xRep;
}
else if(subsetArr[xRep].priority < subsetArr[yRep].priority){
subsetArr[xRep].parent = yRep;
}
//If same rank, randomly pick one
else{
subsetArr[xRep].parent = yRep;
subsetArr[yRep].priority++;
}
}
class subset{
int parent;
int priority;
public subset(){
}
public subset(int p, int pr){
this.parent = p;
this.priority = pr;
}
}
class Edge implements Comparable<Edge>{
int start;
int finish;
int cost;
public Edge(int st, int fi, int co){
this.start = st;
this.finish = fi;
this.cost = co;
}
public Edge(){
}
/* public void printEdge(){
System.out.println(this.cost+","+ this.start+","+this.finish);
}
*/
public int compareTo(Edge a){
if(this.cost > a.cost){
return 1;
}
else if(this.cost < a.cost){
return -1;
}
else{
return 0;
}
}
}
}
public class Solution {
public static void main(String[] args) throws IOException {
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(System.getenv("OUTPUT_PATH")));
String[] firstMultipleInput = bufferedReader.readLine().replaceAll("\\s+$", "").split(" ");
int n = Integer.parseInt(firstMultipleInput[0]);
int m = Integer.parseInt(firstMultipleInput[1]);
List<List<Integer>> edges = new ArrayList<>();
IntStream.range(0, m).forEach(i -> {
try {
edges.add(
Stream.of(bufferedReader.readLine().replaceAll("\\s+$", "").split(" "))
.map(Integer::parseInt)
.collect(toList())
);
} catch (IOException ex) {
throw new RuntimeException(ex);
}
});
int cost = Result.lay_cable(n, edges);
bufferedWriter.write(String.valueOf(cost));
bufferedWriter.newLine();
bufferedReader.close();
bufferedWriter.close();
}
}