-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMSTKruskal.java
More file actions
466 lines (392 loc) · 12 KB
/
MSTKruskal.java
File metadata and controls
466 lines (392 loc) · 12 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
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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
/*
* javac MSTKruskal.java
* java MSTKruskalAlgo
*/
import java.io.*;
import java.util.*;
import java.math.*;
class Vertex implements Comparable<Vertex>{
public int node;
private int key;
private Vertex pi;
// additional instance variable for union find disjoint set forest data structure
public int rank;
public Vertex p;
public Vertex(int node){
this.node = node;
}
// return predecessor
public Vertex get_pi(){
return pi;
}
// assign predecessor
public void set_pi(Vertex o){
this.pi = o;
}
public int get_node(){
return node;
}
public void set_key(int key){
this.key = key;
}
public int get_key(){
return key;
}
public String toString(){
return node + "(" + key + ")";
}
// Need hashCode() and equals() to compare objects
public int hashCode(){
return (int)(node*31);
}
public boolean equals(Object o){
if(o == this){
return true;
}
if(o == null || getClass() != o.getClass()){
return false;
}
Vertex other = (Vertex) o;
return node == other.node;
}
// compare Vertex objects by node;
public int compareTo(Vertex v){
if(key < v.get_key()){
return -1;
} else if(key == v.get_key()){
return 0;
} else{
return 1;
}
}
}
/*
* Undirected Weighted Edges
*/
class Edge implements Comparable<Edge>{
private Vertex u;
private Vertex v;
private int weight;
// Constructor
public Edge(Vertex u, Vertex v, int weight){
if(u.get_node() < v.get_node()){
this.u = u;
this.v = v;
} else{
this.u = v;
this.v = u;
}
this.weight = weight;
}
public Vertex get_u(){
return u;
}
public Vertex get_v(){
return v;
}
public int get_w(){
return weight;
}
public String toString(){
return "{ " + u + " - " + v + " : " + weight + " }";
}
public int hashCode(){
return u.hashCode() + v.hashCode();
}
public boolean equals(Object o){
if(o == this){
return true;
}
if(o == null || getClass() != o.getClass()){
return false;
}
Edge e = (Edge) o;
return ( (u.equals(e.get_u()) && v.equals(e.get_v())) || (u.equals(e.get_v()) && (v.equals(e.get_u()))) );
}
// compare Edge objects by weight
public int compareTo(Edge e){
if(weight < e.get_w() ){
return -1;
} else if(weight == e.get_w() ){
return 0;
} else{
return 1;
}
}
}
/*
* Created acyclic graph, which is just a
* HashMap of Vertex (key) and ArrayList<Vertex> (value)
*/
class Graph{
// instance variable
HashMap<Vertex, ArrayList<Vertex>> uag;
// Keep track of Vertex and Edge objects created
Map<Integer, Vertex> vertices_map;
Map<String, Edge> edges_map;
public Graph(){
uag = new HashMap<Vertex, ArrayList<Vertex>>();
vertices_map = new HashMap<Integer, Vertex>();
edges_map = new HashMap<String, Edge>();
}
public int numVertices(){
return vertices_map.size();
}
public int numEdges(){
return edges_map.size();
}
/* Prevent creating new object when the same node has been seen once already
* --> memory efficient
*
* Create a new Vertex object when the node occurs for the first time;
* And if the node occurs more than once, return a copy of the reference
* to the same object with the same node value.
*
*/
public Vertex getVertex(int node){
if(!vertices_map.containsKey(node)){
vertices_map.put(node, new Vertex(node));
}
return vertices_map.get(node);
}
/*
* Create or Return an Edge object depending whether it existed or not
*/
public Edge getEdge(Vertex u, Vertex v, int weight){
String key;
if(u.get_node() < v.get_node() ){
key = u.get_node() + " -- " + v.get_node();
} else{
key = v.get_node() + " -- " + u.get_node();
}
if(!edges_map.containsKey(key)){
edges_map.put(key, new Edge(u,v,weight));
}
return edges_map.get(key);
}
/*
* Overloading : Return an Edge object
*/
public Edge getEdge(Vertex u, Vertex v){
String key;
if(u.get_node() < v.get_node() ){
key = u.get_node() + " -- " + v.get_node();
} else{
key = v.get_node() + " -- " + u.get_node();
}
return edges_map.get(key);
}
/*
*
* Create a new vertex object when the node first occurs;
* And if the same node occurs more than once, return a
* copy of the reference to the same object with the
* same node value.
*
* Create adjacent list
*/
public void addEdge(int uu, int vv, int weight){
Vertex u = getVertex(uu);
Vertex v = getVertex(vv);
getEdge(u,v, weight);
if(uag.containsKey(u) == false){
ArrayList<Vertex> adj_edges = new ArrayList<Vertex>();
adj_edges.add(v);
uag.put(u,adj_edges);
} else if(uag.containsKey(u) == true){
uag.get(u).add(v);
}
if(uag.containsKey(v) == false){
ArrayList<Vertex> adj_edges_l = new ArrayList<Vertex>();
adj_edges_l.add(u);
uag.put(v,adj_edges_l);
} else if(uag.containsKey(v) == true){
uag.get(v).add(u);
}
}
// adjacent edges
public ArrayList<Vertex> adjEdges(Vertex u){
return uag.get(u);
}
public Vertex[] getAllVertices(){
Vertex[] all_vertices = new Vertex[vertices_map.size() + 1];
int index = 1;
for(Map.Entry<Integer, Vertex> g_map : vertices_map.entrySet()){
Vertex u = g_map.getValue();
all_vertices[index] = u;
index++;
}
return all_vertices;
}
public Edge[] getAllEdges(){
Edge[] all_edges = new Edge[edges_map.size()];
int index = 0;
for(Map.Entry<String, Edge> m : edges_map.entrySet()){
all_edges[index] = m.getValue();
index++;
}
return all_edges;
}
public Vertex getRoot(){
int i = (int)(Math.random() * uag.size());
System.out.println("root = " + getVertex(i));
return getVertex(i);
}
// display
public void display(){
int v = 0;
int e = 0;
for(Map.Entry<Vertex, ArrayList<Vertex>> entry: uag.entrySet()){
System.out.print(entry.getKey() + " : " );
v++;
for(int i = 0; i < entry.getValue().size(); i++){
System.out.print(entry.getValue().get(i));
e++;
}
System.out.println("");
}
System.out.println("measured vertices = " + v + " \tmeasured edges = " + e + " edge_map size = " + edges_map.size());
}
}
class UnionFindDisjointSetForest{
Vertex[] vertices;
int count; // number of clusters
public UnionFindDisjointSetForest(int numOfNodes){
count = numOfNodes;
vertices = new Vertex[numOfNodes + 1];
for(int i = 1; i <= numOfNodes; i++){
vertices[i] = new Vertex(i);
makeSet(i);
}
}
public void makeSet(int i){
Vertex x = vertices[i];
x.p = x;
x.rank = 0;
}
public void union(int i, int j){
link(findSet(i), findSet(j));
}
public void link(Vertex x, Vertex y){
if(x.rank > y.rank){
y.p = x;
} else{
x.p = y;
if(x.rank == y.rank){
y.rank++;
}
}
count--;
}
public Vertex findSet(int i){
Vertex x = vertices[i];
if(!x.equals(x.p)){
// find parent
int xp = x.p.node;
x.p = findSet(xp);
}
return x.p;
}
public int numOfClusters(){
return count;
}
}
class MSTKruskalAlgo{
public static void main(String[] args) throws IOException{
try{
Graph uag = new Graph();
read_file_and_populate(uag, "clustering1.txt");
// uag.display();
HashSet<Edge> ee = MSTKruskalAlgo(uag);
} catch(IOException e){
e.printStackTrace();
}
}
public static void read_file_and_populate(Graph uag, String file_loc) throws IOException{
FileInputStream fil = new FileInputStream(file_loc);
BufferedReader br = new BufferedReader( new InputStreamReader(fil));
String element = br.readLine();
while( (element = br.readLine()) != null ){
String[] line = element.split("\\s+");
uag.addEdge(Integer.parseInt(line[0]), Integer.parseInt(line[1]), Integer.parseInt(line[2]));
}
}
/*
* QuickSort Algo to sort the schedule[] in DECREASING order
*/
public static void quickSortAlgo(Edge[] input_arr){
quickSort(input_arr, 0, input_arr.length-1);
}
public static void quickSort(Edge[] input_arr, int p, int r){
if(p < r){
int q = randomized_partition(input_arr, p, r);
quickSort(input_arr, p, q - 1);
quickSort(input_arr, q + 1, r);
} else{
return;
}
}
public static int partition(Edge[] input_arr, int p, int r){
Edge x = input_arr[r];
int i = p - 1;
for(int j = p; j <= r - 1; j++){
//if(input_arr[j] <= x){ // <= leads to Increasing order
if(input_arr[j].compareTo(x) <= 0){
i = i + 1;
swap(input_arr, i, j);
}
}
swap(input_arr, i+1, r);
return i+1;
}
public static void swap(Edge[] input_arr, int i, int j){
Edge temp = input_arr[i];
input_arr[i] = input_arr[j];
input_arr[j] = temp;
}
/*
* Randomized Partition: generate a random number , then swap it with originally fixed pivot input_arr[r]
* before actually implementing the partition
*/
public static int randomized_partition(Edge[] input_arr, int p, int r){
int i = p + (int)(Math.random() * ((r-p) + 1));
swap(input_arr, r, i);
return partition(input_arr, p, r);
}
// End of QuickSort Algo
public static HashSet<Edge> MSTKruskalAlgo(Graph uag){
HashSet<Edge> A = new HashSet<Edge>();
HashSet<Vertex> discovered_vertices = new HashSet<Vertex>();
HashSet<Vertex> nc = new HashSet<Vertex>();
// V only has non-null values starting at index 1
Vertex[] V = uag.getAllVertices();
// hence need V.length - 1 = 500 nodes
UnionFindDisjointSetForest ufdsf = new UnionFindDisjointSetForest(V.length-1);
Edge[] E = uag.getAllEdges();
quickSortAlgo(E);
for(int i = 0; i < E.length; i++){
Vertex u = E[i].get_u();
Vertex v = E[i].get_v();
if(ufdsf.numOfClusters() > 4){
if(!ufdsf.findSet(u.node).equals(ufdsf.findSet(v.node))){
A.add(E[i]);
// maintain representative member in cluster hashset
discovered_vertices.add(u);
discovered_vertices.add(v);
ufdsf.union(u.node, v.node);
//System.out.println("discovered vertex = " + discovered_vertices.size() + ", weight = " + E[i].get_w() + ", numOfClusters = " + ufdsf.numOfClusters() );
}
}
}
int max = Integer.MAX_VALUE;
for(int j = 0; j < E.length; j++){
Vertex u = E[j].get_u();
Vertex v = E[j].get_v();
if(!ufdsf.findSet(u.node).equals( ufdsf.findSet(v.node)) ){
max = Math.min(max,E[j].get_w());
}
}
System.out.println("max spacing = " + max);
return A;
}
}