forked from TalionS/DensestSubgraph
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
503 lines (488 loc) · 26.1 KB
/
main.cpp
File metadata and controls
503 lines (488 loc) · 26.1 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
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
#include "graph.h"
#include "reduction.h"
#include "allocation.h"
#include "extraction.h"
#include "verification.h"
#include "flownetwork.h"
#include "lp.h"
#include <iostream>
#include "args.h"
#include "ratioselection.h"
#include "app.h"
#include <cstdlib>
#include <cstring>
#include <chrono>
using Heap = boost::heap::fibonacci_heap<std::pair<int, VertexID>>;
int main(int argc, char **argv) {
Args args = Args();
args.argsParse(argc, argv);
bool is_directed = args.getOption("-t") == "d";
bool is_exact = args.getOption("-a") == "e";
bool is_vw = args.getOption("-vw") == "t";
bool is_exp = args.getOption("-exp") == "t";
bool is_dc = args.getOption("-dc") == "t";
bool is_seq = args.getOption("-seq") == "t";
bool is_reduction_ablation = args.getOption("-ra") == "t";
bool is_map = args.getOption("-map") == "t";
bool is_res = args.getOption("-res") == "t";
bool is_mul = args.getOption("-multi") == "t";
bool stable = args.getOption("-stable") == "t";
bool e_stable = args.getOption("-estable") == "t";
bool is_stats = args.getOption("-stats") == "t";
bool is_sample = args.getOption("-sample") == "t";
bool is_print_c = args.getOption("-printc") == "t";
bool is_debug_core = args.getOption("-coredebug") == "t";
double sample_rate = std::stod(args.getOption("-rate"));
double epsilon = std::stod(args.getOption("-eps"));
double learning_rate = std::stod(args.getOption("-gamma"));
ui order_type = std::stoi(args.getOption("-o"));
ui iter_num = std::stoi(args.getOption("-it"));
ui res_width = std::stoi(args.getOption("-width"));
// int parallel_thread_num = std::stoi(args.getOption("-p"));
// std::string ompNumThreads = "OMP_NUM_THREADS=";
// ompNumThreads += std::to_string(parallel_thread_num);
//
// char* env = new char[ompNumThreads.size() + 1];
// std::strcpy(env, ompNumThreads.c_str());
// putenv(env);
std::string red_type = args.getOption("-red");
std::string alloc_type = args.getOption("-alloc");
std::string ext_type = args.getOption("-ext");
std::string ver_type = args.getOption("-ver");
//todo
Graph graph = Graph(is_directed);
if(!is_sample) graph.loadGraphFromFile(args.getOption("-path"));
else graph.loadGraphFromFile(args.getOption("-path"), is_sample, sample_rate);
graph.removeMultiEdges(graph);
// graph.subgraph_density_upper_bound = 1e20;
// printf("io finished.\n");
auto begin = std::chrono::steady_clock::now();
graph.init();
if(is_sample) graph.sample(sample_rate);
Reduction red;
Allocation alloc;
Extraction ext;
Verification ver;
if (!is_exact) {
if (!is_directed) {
if(red_type == "k-core" || red_type == "stable") graph.coreReduce(graph);
double ratio;
bool flag = true;
bool lp_type = alloc_type == "fista"? 1: 0;
LinearProgramming lp(is_directed, lp_type, 0, 0, order_type);
if(alloc_type == "fw" || alloc_type == "fista" || alloc_type == "mwu"){
if(order_type == 0){
}
if(order_type == 1) graph.coreOrder(graph);
if(order_type == 2) graph.CoreOrder(graph);
lp.Init(graph);
}
CoreApp ca = CoreApp();
PKMC pkmc = PKMC();
Greedy gr = Greedy();
if(alloc_type == "greedypp") gr.Init(graph);
if(alloc_type == "core-app"){
ca.Init(graph);
}
if(alloc_type == "pkmc") pkmc.Init(graph);
auto vertices = new std::vector<VertexID>[1];
ui T = 1;
auto end = std::chrono::steady_clock::now();
double opt = 0;
graph.subgraph_density_upper_bound = 1e9;
while (flag) {
std::cout<<"T = "<<T<<std::endl;
if(alloc_type == "flow-app" && red_type == "k-core") graph.coreReduce(graph, (ui) (graph.subgraph_density_lower_bound));
if(red_type == "k-core" && (alloc_type == "fw" || alloc_type == "fista" || alloc_type == "mwu")) red.UndirectedkCoreReduction(graph, lp);
if(red_type == "stable" && (alloc_type == "fw" || alloc_type == "fista" || alloc_type == "mwu")) red.UndirectedStableReduction(graph, lp);
if(alloc_type == "core-app") alloc.UndirectedCoreAppAllocation(graph, ca);
if(alloc_type == "greedy"){
alloc.UndirectedGreedyAllocation(graph);
break;
}
if(alloc_type == "flow-app"){
alloc.UndirectedFlowAppAllocation(graph);
}
if(alloc_type == "greedypp") alloc.UndirectedGreedyppAllocation(graph, gr);
if(alloc_type == "fw") alloc.UndirectedlpAllocation(graph, lp, T, is_seq);
if(alloc_type == "fista") alloc.UndirectedFistaAllocation(graph, lp, T, is_seq);
if(alloc_type == "mwu") alloc.UndirectedMWUAllocation(graph, lp, T, is_seq);
if(ext_type == "cp") ext.UndirectedlpExactExtraction(graph, lp, vertices);
if(ver_type == "cp") flag = ver.UndirectedLpAppVerification(graph, lp, vertices, epsilon);
if(ver_type == "core-app") flag = ver.UndirectedCoreAppVerification(graph, ca);
if(ver_type == "flow-app") flag = ver.UndirectedFlowAppVerification(graph, epsilon);
T<<=1;
end = std::chrono::steady_clock::now();
if(graph.subgraph_density_lower_bound > opt){
opt = graph.subgraph_density_lower_bound;
printf("%.10lf %.10lf\n", opt, std::chrono::duration<double>(end - begin).count());
}
}
} else if(!is_vw){
std::pair<double, double> ratio;
double ratio_o = 0, ratio_p = 0;
double reduction_ratio = 0;
double total_vertices_num = 0;
double total_edges_num = 0;
double total_iter_num = 0;
RatioSelection ratio_selection(graph);
bool is_init_ratio = false;
ui ratio_count = 0;
while (ratio_selection.ratioSelection(graph.getVerticesCount(),
ratio,
is_init_ratio,
is_vw,
is_dc,
ratio_o,
ratio_p,
graph.subgraph_density,
epsilon, is_res, res_width, is_map)) {
ratio_count++;
ui edges_count = 0;
bool flag = true;
bool is_init_red = false;
bool is_init_lp = false;
bool is_reduced = false;
bool is_stable_set = false;
double rho, rho_c;
double l = learning_rate * graph.subgraph_density;
double r = graph.subgraph_density_upper_bound;
FlowNetwork flow;
WCore w_core;
LinearProgramming lp(is_directed, 0, 0, 0, order_type);
std::vector<std::pair<VertexID, VertexID>> edges;
std::vector<std::vector<VertexID>> vertices(2);
std::vector<Heap> heap;
std::vector<std::vector<Heap::handle_type>> handles;
std::vector<std::vector<bool>> is_peeled;
std::pair<ui, ui> best_pos(0, 0);
Graph subgraph(is_directed, 0);
subgraph.subgraph_density = graph.subgraph_density;
while (flag) {
if (!is_reduced || alloc_type != "fw") {
is_reduced = true;
if (red_type == "exact-xy-core") {
red.xyCoreReduction(graph, subgraph, ratio, l, r, is_init_red,
is_dc, is_map, true, true, is_res, res_width, false);
} else if (red_type == "appro-xy-core") {
red.xyCoreReduction(graph, subgraph, ratio, l, r, is_init_red,
is_dc, is_map, false, true, is_res, res_width, false);
} else if (red_type == "w-core") {
red.wCoreReduction(graph, subgraph, w_core);
} else {
subgraph = graph;
}
// if (is_reduction_ablation)
// reduction_ratio += (double) subgraph.getEdgesCount() / graph.getEdgesCount();
if (is_stats) {
total_vertices_num += subgraph.getVerticesCount();
total_edges_num += subgraph.getEdgesCount();
}
// printf("subgraph edges: %d\n", subgraph.getEdgesCount());
if (subgraph.getEdgesCount() == 0) {
double c;
if (is_map) {
if (ratio.first < 1 && ratio.second > 1) {
c = 1;
} else if (ratio.second <= 1) {
c = (ratio.first + ratio.second) / 2;
} else if (ratio.first >= 1) {
c = 2 / (1 / ratio.first + 1 / ratio.second);
}
} else
c = (ratio.first + ratio.second) / 2;
if (is_res) {
ratio_o = std::max(ratio.first, c / res_width);
ratio_p = std::min(ratio.second, c * res_width);
} else {
ratio_o = ratio.first;
ratio_p = ratio.second;
}
break;
}
}
if (is_mul && alloc_type == "fw" && subgraph.subgraph_density < graph.subgraph_density) {
subgraph.subgraph_density = graph.subgraph_density;
// printf("edges #: %d\n", subgraph.getEdgesCount());
is_init_red = false;
red.xyCoreReduction(subgraph, subgraph, ratio, subgraph.subgraph_density, r, is_init_red, is_dc, is_map, false, false, is_res, res_width, true);
// printf("edges #: %d\n", subgraph.getEdgesCount());
}
if (alloc_type == "fw")
red.stableSetReduction(subgraph, lp, edges, is_stable_set, true);
if (alloc_type == "greedy")
alloc.directedBSApproAllocation(graph, ratio, heap, handles, is_peeled, edges_count, is_init_lp);
if (alloc_type == "xy-core-appro")
alloc.xyCoreApproAllocation(subgraph, best_pos);
if (alloc_type == "w-core-appro")
alloc.wCoreApproAllocation(subgraph, w_core, best_pos);
if (alloc_type == "fw")
alloc.directedCPAllocation(subgraph, lp, iter_num, is_init_lp, ratio, !is_seq, is_exp, is_map);
if (ext_type == "core-appro")
ext.directedCoreApproExtraction(graph, subgraph, best_pos);
if (ext_type == "cp")
ext.directedCPExtraction(subgraph, lp, best_pos, vertices, ratio, ratio_o, ratio_p, rho, rho_c,
is_map);
if (ext_type == "greedy")
ext.directedBSApproExtraction(graph, is_peeled, vertices);
if (ver_type == "greedy")
flag = ver.directedBSApproVerification(graph, edges_count, vertices);
if (ver_type == "no")
break;
if (ver_type == "cp")
flag = ver.directedCPVerification(graph, subgraph, lp, best_pos, vertices, ratio, rho, rho_c,
ratio_o, ratio_p, is_stable_set, edges, epsilon, stable,
is_map);
}
if (ext_type == "core-appro")
break;
if (is_stats)
total_iter_num += lp.cur_iter_num;
}
printf("ratio count: %d, density: %f, S/T: %d/%d\n", ratio_count, graph.subgraph_density,
graph.vertices[0].size(), graph.vertices[1].size());
if (is_stats)
printf ("avg vertices #: %f\navg edges #: %f\navg iterations #: %f\n", total_vertices_num / ratio_count, total_edges_num / ratio_count, total_iter_num / ratio_count);
// if (is_reduction_ablation)
// printf("reduction_ratio: %f\n", reduction_ratio / ratio_count * 100);
} else {
std::pair<double, double> ratio;
double ratio_o = 0, ratio_p = 0;
double reduction_ratio = 0;
RatioSelection ratio_selection(graph);
bool is_init_ratio = false;
ui ratio_count = 0;
while (ratio_selection.ratioSelection(graph.getVerticesCount(),
ratio,
is_init_ratio,
is_vw,
is_dc,
ratio_o,
ratio_p,
graph.subgraph_density,
epsilon, is_res, res_width, is_map)) {
ratio_count++;
Graph vw_graph(false);
if (is_dc)
graph.createVertexWeightedGraph(vw_graph, (ratio.first + ratio.second) / 2);
else
graph.createVertexWeightedGraph(vw_graph, ratio.first / ratio.second);
bool flag = true;
auto verticess = new std::vector<VertexID>[1];
std::vector<std::vector<VertexID>> vertices(2);
bool lp_type = alloc_type == "fista"? 1: 0;
LinearProgramming lp(false, lp_type, 0, 0, order_type);
vw_graph.subgraph_density_lower_bound = 0;
vw_graph.coreReduce(vw_graph, 0, true);
lp.Init(vw_graph);
ui T = 1;
while(flag) {
red.UndirectedkCoreReduction(vw_graph, lp, true);
if(ext_type == "mwu") alloc.UndirectedMWUAllocation(vw_graph, lp, T, true);
if(ext_type == "fw") alloc.UndirectedlpAllocation(vw_graph, lp, T, true);
if(ext_type == "fista") alloc.UndirectedFistaAllocation(vw_graph, lp, T, false);
ext.UndirectedlpExactExtraction(vw_graph, lp, verticess);
flag = ver.UndirectedLpAppVerification(vw_graph, lp, verticess, epsilon);
T <<= 1;
}
ext.directedVWApproExtraction(graph, vw_graph, vertices, verticess, ratio_o, ratio_p);
//printf("ratio_count: %d, density: %f, S/T: %d / %d\n", ratio_count, graph.subgraph_density, graph.vertices[0].size(), graph.vertices[1].size());
}
printf("%.10lf\n",graph.subgraph_density);
}
} else {
// Reduction red;
// Allocation alloc;
// Extraction ext;
// Verification ver;
if (!is_directed) {
double l, r;
ui T = 1;
FlowNetwork flow;
if(red_type == "k-core" || red_type == "stable") graph.coreReduce(graph);
bool lp_type = alloc_type == "fista"? 1: 0;
LinearProgramming lp(is_directed, lp_type, 0, 0, order_type);
if(alloc_type == "fw" || alloc_type == "fista" || alloc_type == "mwu"){
if(order_type == 0){
}
if(order_type == 1) graph.coreOrder(graph);
if(order_type == 2) graph.CoreOrder(graph);
lp.Init(graph);
}
lp.Init(graph);
l = graph.subgraph_density;
r = graph.subgraph_density_upper_bound;
auto vertices = new std::vector<VertexID>[1];
bool flag = true;
while (flag) {
std::cout<<"T = "<<T<<std::endl;
T <<= 1;
if (red_type == "k-core")
red.UndirectedkCoreReduction(graph, lp);
if (alloc_type == "flow-exact")
alloc.UndirectedflowExactAllocation(graph, flow, l, r);
if (alloc_type == "fw")
alloc.UndirectedlpAllocation(graph, lp, T, is_seq);
if (alloc_type == "fista")
alloc.UndirectedFistaAllocation(graph, lp, T, is_seq);
if (alloc_type == "mwu")
alloc.UndirectedMWUAllocation(graph, lp, T, is_seq);
if (ext_type == "flow-exact")
ext.UndirectedflowExactExtraction(graph, flow, l, r, vertices);
if (ext_type == "cp")
ext.UndirectedlpExactExtraction(graph, lp, vertices);
if (ver_type == "flow-exact")
flag = ver.UndirectedflowExactVerification(graph, l, r);
if (ver_type == "cp" && red_type != "stable")
flag = ver.UndirectedlpVerification(graph, lp, flow, vertices);
if(ver_type == "cp" && red_type == "stable")
flag = ver.UndirectedlpVerification(graph, lp, flow, vertices, true);
}
printf("%.10lf\n",graph.subgraph_density);
} else {
//The generation of Ratio set needs to be refined.
//How to combine divide-and-conquer strategy with our current framework
//needs to be considered.
std::pair<double, double> ratio;
double ratio_o = 0, ratio_p = 0;
double reduction_ratio = 0;
double total_edges_num = 0;
double total_vertices_num = 0;
double total_iter_num = 0;
RatioSelection ratio_selection(graph);
bool is_init_ratio = false;
ui ratio_count = 0;
ui red_count = 0;
while (ratio_selection.ratioSelection(graph.getVerticesCount(),
ratio,
is_init_ratio,
is_vw,
is_dc,
ratio_o,
ratio_p,
graph.subgraph_density,
0, is_res, res_width, is_map)) {
ratio_count++;
bool flag = true;
bool is_init_red = false;
bool is_init_lp = false;
bool is_reduced = false;
bool is_stable_set = false;
double rho, rho_c;
double l = learning_rate * graph.subgraph_density;
double r = graph.subgraph_density_upper_bound;
FlowNetwork flow;
LinearProgramming lp(is_directed, 0, 0, 0, order_type);
std::vector<std::pair<VertexID, VertexID>> edges;
std::vector<std::vector<VertexID>> vertices(2);
std::pair<ui, ui> best_pos(0, 0);
Graph subgraph(is_directed, 0);
subgraph.subgraph_density = graph.subgraph_density;
ui iter_count = 0;
if (is_print_c)
printf("%f\n", (ratio.first + ratio.second) / 2);
while (flag) {
iter_count++;
// if (!is_reduced || alloc_type != "cp") {
if (!is_reduced) {
is_reduced = true;
bool is_copy = alloc_type == "flow-exact";
if (red_type == "exact-xy-core") {
red.xyCoreReduction(graph, subgraph, ratio, l, r, is_init_red,
is_dc, is_map, true, true, is_res, res_width, false);
} else if (red_type == "appro-xy-core") {
red.xyCoreReduction(graph, subgraph, ratio, l, r, is_init_red,
is_dc, is_map, false, true, is_res, res_width, is_copy);
} else {
subgraph = graph;
}
if (is_stats) {
red_count++;
total_vertices_num += subgraph.getVerticesCount();
total_edges_num += subgraph.getEdgesCount();
}
if (is_debug_core)
printf("#vertices: %d, #edges: %d\n", subgraph.getVerticesCount(), subgraph.getEdgesCount());
if (subgraph.getEdgesCount() == 0) {
double c;
if (is_map) {
if (ratio.first < 1 && ratio.second > 1) {
c = 1;
} else if (ratio.second <= 1) {
c = (ratio.first + ratio.second) / 2;
} else if (ratio.first >= 1) {
c = 2 / (1 / ratio.first + 1 / ratio.second);
}
} else
c = (ratio.first + ratio.second) / 2;
if (is_res) {
ratio_o = std::max(ratio.first, c / res_width);
ratio_p = std::min(ratio.second, c * res_width);
} else {
ratio_o = ratio.first;
ratio_p = ratio.second;
}
break;
}
}
// if (is_mul) {
// printf("%d, %f, %f\n", lp.cur_iter_num, subgraph.subgraph_density, graph.subgraph_density);
// }
if (is_mul && alloc_type == "fw" && subgraph.subgraph_density < graph.subgraph_density) {
// printf("edges #: %d\n", subgraph.getEdgesCount());
is_init_red = false;
red.xyCoreReduction(subgraph, subgraph, ratio, subgraph.subgraph_density, r, is_init_red, is_dc, is_map, false, false, is_res, res_width, true);
subgraph.subgraph_density = graph.subgraph_density;
// printf("edges #: %d\n", subgraph.getEdgesCount());
}
// if (is_stable_set) {
// printf("edges #: %d\n", subgraph.getEdgesCount());
// }
if (is_stable_set && alloc_type == "fw" && e_stable) {
red.stableSetReduction(subgraph, lp, edges, is_stable_set, true);
// printf("edges #: %d\n", subgraph.getEdgesCount());
}
// printf("red\n");
if (alloc_type == "fw")
alloc.directedCPAllocation(subgraph, lp, iter_num, is_init_lp, ratio, !is_seq, is_exp, is_map);
if (alloc_type == "flow-exact")
alloc.flowExactAllocation(subgraph, flow, ratio, l, r, is_dc, is_map);
// printf("alloc.\n");
if (ext_type == "cp")
ext.directedCPExtraction(subgraph, lp, best_pos, vertices, ratio, ratio_o, ratio_p, rho, rho_c,
is_map);
if (ext_type == "flow-exact")
ext.flowExactExtraction(graph, subgraph, ratio, flow, l, r, ratio_o, ratio_p, is_map);
// printf("ext.\n");
if (ver_type == "cp")
flag = ver.directedCPVerification(graph, subgraph, lp, best_pos, vertices, ratio, rho, rho_c,
ratio_o, ratio_p, is_stable_set, edges, 0, false, is_map);
if (ver_type == "flow-exact")
flag = ver.flowExactVerification(graph, l, r);
// printf("ver.\n");
}
// printf("ratio_count %d, iter num: %d, ratio (%f, %f), density %f, S/T %d/%d\n", ratio_count, lp.cur_iter_num, ratio.first, ratio.second, graph.subgraph_density, graph.vertices[0].size(), graph.vertices[1].size());
if (is_stats) {
if (alloc_type == "fw")
total_iter_num += lp.cur_iter_num;
else
total_iter_num += iter_count;
}
if (is_debug_core) {
// total_iter_num += iter_count;
printf("#iter: %d\n", iter_count);
}
}
printf("ratio count: %d, density: %f, S/T: %d/%d\n", ratio_count, graph.subgraph_density,
graph.vertices[0].size(), graph.vertices[1].size());
// if (is_reduction_ablation)
// printf("reduction_ratio: %f\n", reduction_ratio / ratio_count * 100);
if (is_stats)
printf ("avg vertices #: %f\navg edges #: %f\navg iterations #: %f\n", total_vertices_num / red_count, total_edges_num / red_count, total_iter_num / ratio_count);
}
}
auto end = std::chrono::steady_clock::now();
// delete [] env;
printf("time: %f\n", std::chrono::duration<double>(end - begin).count());
return 0;
};