-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathKarger.java
More file actions
382 lines (315 loc) · 10.7 KB
/
Karger.java
File metadata and controls
382 lines (315 loc) · 10.7 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
/** -----------------------------------------------------------------------
Karger.java
@author William Clift
Data Structures and Algorithms
Ursinus College
Project 4 - Karger's Algorith
13 May 2020
Compile and Run Instructions:
Compile: javac Karger.java Graph.java Edge.java GraphGenerator.java Subset.java
Run: java Karger
To run a random Graph of size [Vertices][Edges], run the following:
java GraphGenerator [Verticies] [Edges] | java Karger
------------------------------------------------------------------- **/
import java.util.List;
import java.util.ArrayList;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.RecursiveAction;
import java.util.concurrent.ConcurrentSkipListSet;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.TimeUnit;
import java.io.*;
@SuppressWarnings("unchecked")
public class Karger extends RecursiveAction implements Runnable{
ConcurrentSkipListSet<Double> cutsSet; //TODO: share this between instances to store cuts of multiple runs
Graph g;
private static final int processors = Runtime.getRuntime().availableProcessors();
int n;
int t;
public Karger(Graph graph) {
this.g = graph;
this.cutsSet = new ConcurrentSkipListSet<Double>();
}
public Karger(Graph graph, int n, ConcurrentSkipListSet<Double> cuts) {
this.g = graph;
this.cutsSet = cuts;
this.n = n;
}
public Karger(Graph graph, ConcurrentSkipListSet<Double> cuts) {
this.g = graph;
this.cutsSet = cuts;
}
public Karger(Graph graph, int t) {
this.g = graph;
this.t = t;
}
public static void main(String[] args) {
//Generate from File
//Graph graph = Graph.generateGraphFromText(new File("graph.txt"));
//Generate from Stdin
Graph graph = Graph.generateGraphFromText();
Karger k = new Karger(graph);
//System.out.println(graph.toMarkdown(true));
ConcurrentSkipListSet<Double> cutsSet = new ConcurrentSkipListSet<Double>();
//test(graph, cutsSet);
long start = 0;
long end = 0;
start = System.currentTimeMillis();
karger(new Graph(graph));
end = System.currentTimeMillis();
System.out.println("Karger: \t\t" + ((float)(end - start) / 1000F) + "\tseconds.");
start = System.currentTimeMillis();
mincut(new Graph(graph));
end = System.currentTimeMillis();
System.out.println("Min-Cut: \t\t" + ((float)(end - start) / 1000F) + "\tseconds.");
start = System.currentTimeMillis();
minCutThreaded(new Graph(graph), cutsSet);
end = System.currentTimeMillis();
System.out.println("Threaded Min-Cut: \t" + ((float)(end - start) / 1000F) + "\tseconds.");
start = System.currentTimeMillis();
fastMincut(new Graph(graph));
end = System.currentTimeMillis();
System.out.println("Karger-Stein: \t\t" + ((float)(end - start) / 1000F) + "\tseconds.");
start = System.currentTimeMillis();
fastMincutThreaded(new Graph(graph), 0, cutsSet);
end = System.currentTimeMillis();
System.out.println("Threaded Karger-Stein: \t" + ((float)(end - start) / 1000F) + "\tseconds.");
/*
System.out.println(karger(new Graph(graph)));
System.out.println(mincut(new Graph(graph)));
System.out.println(minCutThreaded(new Graph(graph), cutsSet));
System.out.println(fastMincut(new Graph(graph)));
System.out.println(fastMincutThreaded(new Graph(graph), 0, cutsSet));
*/
}
/**
* Prints out the results of a test on the system (used for analysis)
* @param graph graph that will be contracted
* @param cutsSet the cuts set of the minimum cut
**/
public static void test(Graph graph, ConcurrentSkipListSet<Double> cutsSet){
Graph g = graph;
ConcurrentSkipListSet<Double> cuts = cutsSet;
long start = 0;
long end = 0;
//Test Printouts
String printout = "";
start = System.currentTimeMillis();
karger(new Graph(g));
end = System.currentTimeMillis();
printout+=((float)(end - start) / 1000F) + " ";
start = System.currentTimeMillis();
mincut(new Graph(g));
end = System.currentTimeMillis();
printout+=((float)(end - start) / 1000F) + " ";
start = System.currentTimeMillis();
minCutThreaded(new Graph(g), cuts);
end = System.currentTimeMillis();
printout+=((float)(end - start) / 1000F) + " ";
start = System.currentTimeMillis();
fastMincut(new Graph(g));
end = System.currentTimeMillis();
printout+=((float)(end - start) / 1000F) + " ";
start = System.currentTimeMillis();
fastMincutThreaded(new Graph(g), 0, cuts);
end = System.currentTimeMillis();
printout+=((float)(end - start) / 1000F);
System.out.println(printout);
}
/**
* The threaded mincut algorithm
* @param g graph that will be contracted
* @param cuts the cuts list
* @return mincut weight of the minimum cut
**/
public static double minCutThreaded(Graph g, ConcurrentSkipListSet<Double> cuts){
//ExecutorService executor = Executors.newWorkStealingPool(nThreads);
ExecutorService executor = Executors.newCachedThreadPool();
//ExecutorService executor = Executors.newFixedThreadPool(nThreads);
int V = g.getOriginalVertexCount();
final int MAX = 2 * (V * V);
for(int i = 0; i < MAX; i++){
if(Math.pow(2, i) <= processors){
Runnable thread = new Karger(new Graph(g), cuts);
executor.execute(thread);
}else{
cuts.add(karger(new Graph(g)));
}
}
executor.shutdown();
try {
executor.awaitTermination(Long.MAX_VALUE, TimeUnit.NANOSECONDS);
} catch(InterruptedException e) {
System.err.println(e.getMessage());
}
return cuts.first(); //Returns the minimum
}
/**
* Recursive override
**/
@Override
public void run() {
//TODO for parallel Karger
cutsSet.add(karger(g));
}
/**
* Runts 2*V^2 iterations of karger for 98% confidence
* Contrac the graph by one node
* @param g graph that will be contracted
* @param n the iteration of the recursive algorithm
* @param cuts the minimum cuts lift
* @return cutWeight weight of the minimum cut
**/
public static double fastMincutThreaded(Graph g, int n, ConcurrentSkipListSet<Double> cuts) {
double cutWeight = Double.POSITIVE_INFINITY;
double td = g.getOriginalVertexCount() / Math.sqrt(2) + 1;
int t = (int) td;
if(g.getCurrentVertexCount() < t || g.getCurrentVertexCount()< 6){
cutWeight = mincut(new Graph(g));
}else{
Graph c1 = new Graph(g);
Graph c2 = new Graph(g);
if(Math.pow(2, n) <= processors && n >= 0){
invokeAll(new Karger(c1, n++, cuts), new Karger(c2, n++, cuts));
}
else{
Karger k1 = new Karger(c1, n++, cuts);
Karger k2 = new Karger(c2, n++, cuts);
k1.compute();
k2.compute();
}
cutWeight = cuts.first();
}
return cutWeight;
}
/**
* Recursive override
**/
@Override
public void compute() {
//TODO for parallel Karger-Stein
double td = g.getCurrentVertexCount() / Math.sqrt(2) + 1;
int t = (int) td;
while(g.getCurrentVertexCount() > t) {
this.g = contract(g);
}
cutsSet.add(fastMincutThreaded(g, n, cutsSet));
}
/**
* Runts 2*V^2 iterations of karger for 98% confidence
* Contrac the graph by one node
* @param g graph that will be contracted
* @return mincut weight of the minimum cut
**/
public static double fastMincut(Graph g) {
double mincut = Double.POSITIVE_INFINITY;
ConcurrentSkipListSet<Double> cuts = new ConcurrentSkipListSet<Double>();
mincut = fastMincutThreaded(g, -1, cuts);
return mincut;
}
/**
* Runts 2*V^2 iterations of karger for 98% confidence
* Contrac the graph by one node
* @param g graph that will be contracted
* @return mincut weight of the minimum cut
**/
public static double mincut(Graph g) {
double mincut = Double.POSITIVE_INFINITY;
//TODO: run karger(g) tn^2=t|V|^2times and take the min
int V = g.getCurrentVertexCount();
final int MAX = 2 * (V * V);
for(int i = 0; i < MAX; i++){
Graph copy = new Graph(g);
double cut = karger(copy);
if(cut < mincut){
mincut = cut;
}
}
return mincut;
}
/**
* Runs one iteration
* Contrac the graph by one node
* @param g graph that will be contracted
* @return g graph returned
**/
public static Graph contract(Graph g){
// Get data of given graph
int V = g.getOriginalVertexCount();
int E = g.getOriginalEdgesCount();
List<Edge> edges = g.getEdges();
List<Subset> subsets = g.getSubsets();
for (int v = 0; v < V; v++) {
subsets.add(new Subset());
subsets.get(v).parent = v;
subsets.get(v).rank = 0;
}
boolean done = false;
while(!done){
// Pick a random edge
int i = (int)(Math.random() * E);
int subset1 = Subset.find(subsets, edges.get(i).s());
int subset2 = Subset.find(subsets, edges.get(i).t());
if (subset1 != subset2) {
g.decrementVertexCount();
Subset.union(subsets, subset1, subset2);
done = true;
}
}
return g;
}
/**
* Runs one iteration
* Karger's algorithm down to t vertices.
* Unless you intend to modify original graph,
* it is recommended that you copy the graph
* before calling.
* @param graph graph that will be contracted
* @param t the number of iterations down to
* @return cut weight after run
**/
public static double fastKarger(Graph g, double t) {
double cutWeight = Double.POSITIVE_INFINITY;
// Get data of given graph
int V = g.getOriginalVertexCount();
g.setCurrentVertexCount(V);
while (g.getCurrentVertexCount() > t) {
g = contract(g);
}
ConcurrentSkipListSet<Double> cutsSet = new ConcurrentSkipListSet<Double>();
for(int i = 0; i < t; i++){
cutsSet.add(karger(new Graph(g)));
}
cutWeight = cutsSet.first();
return cutWeight;
}
/**
* Runs one iteration
* Karger's algorithm down to 2 vertices.
* Unless you intend to modify original graph,
* it is recommended that you copy the graph
* before calling.
* @param graph graph that will be contracted
* @return cutWeight
**/
public static double karger(Graph graph) {
// Get data of given graph
int V = graph.getOriginalVertexCount();
int E = graph.getOriginalEdgesCount();
List<Edge> edges = graph.getEdges();
List<Subset> subsets = graph.getSubsets();
while (graph.getCurrentVertexCount() > 2) {
graph = contract(graph);
}
double cutWeight = 0D;
for (int i = 0; i < E; i++) {
int subset1 = Subset.find(subsets, edges.get(i).s());
int subset2 = Subset.find(subsets, edges.get(i).t());
if (subset1 != subset2) {
cutWeight += edges.get(i).getWeight(); //TODO: change to handle weights
}
}
return cutWeight;
}
}