-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMaster.cpp
More file actions
1831 lines (1668 loc) · 59.5 KB
/
Master.cpp
File metadata and controls
1831 lines (1668 loc) · 59.5 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
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include "Master.h"
#define _DEBUG
#ifdef _DEBUG
#define debug_print(msg) std::cout << msg
#else
#define debug_print(msg)
#endif
Master::Master(PUNGraph G, int k, int b, bool disable_cck, bool disable_cmv) {
this->G = G;
acost = 0;
nfs = 0;
this->b = b;
this->k = k;
this->disable_cck = disable_cck;
this->disable_cmv = disable_cmv;
}
void Master::Exact_Anchoring(std::string alg, std::string vcc_data) {
double t_begin = (double)clock();
int round = 0;
double group_anchor_time = 0.0, vertex_anchor_time = 0.0;
TIntVIntV kvcc_array;
TIntV kvcc, delta_S, delta_S_bar;
TIntV Expanded_Vertex;
Load_kvcc(kvcc_array, vcc_data);
std::unordered_set<std::pair<int, int>, pair_hash> total_Inserted_Edge;
double total_gain = 0.0;
auto start_time = std::chrono::high_resolution_clock::now();
// 1. 用long
// long计算图中所有点的k顶点连通能力值,每个点的k顶点连通能力值=他所属于的最大的那个kvcc的大小
int num_vertices = 0;
for (TIntVIntV::TIter It = kvcc_array.BegI(); It < kvcc_array.EndI(); It++) {
const TIntV& kvcc = *It;
for (TIntV::TIter VIt = kvcc.BegI(); VIt < kvcc.EndI(); VIt++) {
int vertex = *VIt;
num_vertices = std::max(num_vertices, vertex + 1);
}
}
std::vector<long long> original_connectivity =
calculateKVertexConnectivity(kvcc_array, num_vertices);
// 2. 枚举图中顶点对应完全图的边 - 图中所有的边, 作为候选集的边
std::unordered_set<std::pair<int, int>, pair_hash> candidate_edges;
for (int i = 0; i < num_vertices; ++i) {
for (int j = i + 1; j < num_vertices; ++j) {
if (G->IsEdge(i, j) == false) candidate_edges.insert({i, j});
}
}
// 3. 从候选集边中依次枚举,每次选择b条边,进行插入,并且计算收益
std::vector<std::unordered_set<std::pair<int, int>, pair_hash>>
edge_combinations = generateEdgeCombinations(candidate_edges, b);
double max_gain = -DBL_MAX;
std::unordered_set<std::pair<int, int>, pair_hash> best_edges;
for (const auto& combination : edge_combinations) {
double gain = calculateGain(kvcc_array, combination, original_connectivity);
if (gain > max_gain) {
max_gain = gain;
best_edges = combination;
}
}
// 4. 统计收益最大的b条边
total_Inserted_Edge = best_edges;
total_gain = max_gain;
auto end_time = std::chrono::high_resolution_clock::now();
auto duration =
std::chrono::duration_cast<std::chrono::seconds>(end_time - start_time)
.count();
std::cout << "最终插入边数量: " << total_Inserted_Edge.size() << std::endl;
std::cout << "总的 gain: " << total_gain << std::endl;
std::cout << "算法耗时: " << duration << " 秒" << std::endl;
// 按行输出所有插入的边
std::cout << "所有插入的边:" << std::endl;
for (const auto& edge : total_Inserted_Edge) {
std::cout << edge.first << " " << edge.second << std::endl;
}
}
void Master::Anchoring(std::string alg, std::string vcc_data,
double threshold) {
double t_begin = (double)clock();
int round = 0;
double group_anchor_time = 0.0, vertex_anchor_time = 0.0;
TIntVIntV kvcc_array;
TIntV kvcc, delta_S, delta_S_bar;
TIntV Expanded_Vertex;
Load_kvcc(kvcc_array, vcc_data);
std::unordered_set<std::pair<int, int>, pair_hash> total_Inserted_Edge;
double total_gain = 0.0;
// 用于记录所有插入的边
std::vector<std::pair<int, int>> allInsertedEdges;
auto start_time = std::chrono::high_resolution_clock::now();
// 初始化各函数总耗时
double total_cal_connect_kvcc_time = 0.0;
double total_cal_mul_verices_time = 0.0;
double total_exp_sin_vertices_time = 0.0;
double total_mer_connect_kvcc_time = 0.0;
double total_exp_mul_vertices_time = 0.0;
double total_time = 0.0;
// 记录上一轮的 ra 和 rb 值
double prev_ra = 0.0;
double prev_rb = 0.0;
// 记录上一轮 CalConnectKVcc 的结果
int prev_Io = 0;
int prev_J = 0;
std::vector<std::vector<int>> prev_T;
double prev_Ro = 0.0;
int prev_Co = 0;
double prev_gain_mer = 0.0;
int initial_b = b;
double quarter_b = initial_b * 0.25;
double half_b = initial_b * 0.5;
double three_quarters_b = initial_b * 0.75;
double quarter_time = 0.0;
double half_time = 0.0;
double three_quarters_time = 0.0;
double full_time = 0.0;
double quarter_gain = 0.0;
double half_gain = 0.0;
double three_quarters_gain = 0.0;
double full_gain = 0.0;
std::cout << "开始循环,初始 b: " << b << std::endl;
while (b > 0) {
int Io, Ie, J, Co, Ce;
double Ro = 0, Re = 0;
std::vector<int> MC;
std::vector<std::vector<int>> T;
std::unordered_set<std::pair<int, int>, pair_hash> Inserted_Edge;
double gain_sin, gain_mul, gain_mer;
bool use_optimized_strategy = (alg == "approximate");
bool can_execute_cal_connect_kvcc = true;
if (use_optimized_strategy) {
can_execute_cal_connect_kvcc =
(round == 0) || (prev_ra > prev_rb * threshold);
}
if (can_execute_cal_connect_kvcc) {
// 记录 CalConnectKVcc 开始时间
auto start_cal_connect_kvcc = std::chrono::high_resolution_clock::now();
if (!disable_cck) {
CalConnectKVcc(kvcc_array, Io, J, T, Ro, Co, gain_mer);
} else {
Io = 0;
J = 0;
T.clear();
Ro = 0.0;
Co = 0;
gain_mer = 0.0;
}
// 记录 CalConnectKVcc 结束时间
auto end_cal_connect_kvcc = std::chrono::high_resolution_clock::now();
auto duration_cal_connect_kvcc =
std::chrono::duration_cast<std::chrono::seconds>(
end_cal_connect_kvcc - start_cal_connect_kvcc)
.count();
total_cal_connect_kvcc_time += duration_cal_connect_kvcc;
// 更新上一轮的结果
prev_Io = Io;
prev_J = J;
prev_T = T;
prev_Ro = Ro;
prev_Co = Co;
prev_gain_mer = gain_mer;
} else {
// 沿用上一轮的值
Io = prev_Io;
J = prev_J;
T = prev_T;
Ro = prev_Ro;
Co = prev_Co;
gain_mer = prev_gain_mer;
}
// std::cout << "T size after CalConnectKVcc: " << T.size() << std::endl;
// 记录 CalMulVerices 开始时间
auto start_cal_mul_verices = std::chrono::high_resolution_clock::now();
if (!disable_cmv) {
CalMulVerices(kvcc_array, Ie, MC, Re, Ce, gain_mul);
} else {
Ie = 0;
MC.clear();
Re = 0.0;
Ce = 0;
gain_mul = 0.0;
}
// CalMulVerices(kvcc_array, Ie, MC, Re, Ce, gain_mul);
// 记录 CalMulVerices 结束时间
auto end_cal_mul_verices = std::chrono::high_resolution_clock::now();
auto duration_cal_mul_verices =
std::chrono::duration_cast<std::chrono::seconds>(end_cal_mul_verices -
start_cal_mul_verices)
.count();
total_cal_mul_verices_time += duration_cal_mul_verices;
double ra = Ro;
double rb = Re;
double rc = 0;
int v = -1;
int single_vcc_idx = -1;
// 记录 ExpSinVertices 开始时间
auto start_exp_sin_vertices = std::chrono::high_resolution_clock::now();
std::unordered_set<std::pair<int, int>, pair_hash> Inserted_Edge_single;
ExpSinVertices(kvcc_array, rc, Inserted_Edge_single, v, single_vcc_idx,
gain_sin);
// 记录 ExpSinVertices 结束时间
auto end_exp_sin_vertices = std::chrono::high_resolution_clock::now();
auto duration_exp_sin_vertices =
std::chrono::duration_cast<std::chrono::seconds>(end_exp_sin_vertices -
start_exp_sin_vertices)
.count();
total_exp_sin_vertices_time += duration_exp_sin_vertices;
std::cout << "当前 ra: " << ra << ", rb: " << rb << ", rc: " << rc
<< std::endl;
double current_gain = 0.0;
if (ra == 0 && rb == 0 && rc == 0) {
break;
}
if (ra >= rb && ra >= rc) {
std::cout << "选择 ra 分支" << std::endl;
if (!T.empty()) {
int idx = Io;
int jdx = J;
std::vector<int> t_ij = T.front();
T.assign(T.begin() + 1, T.end());
std::vector<int> t_ji = T.front();
T.assign(T.begin() + 1, T.end());
int mer_cost = Co;
// 记录 MerConnectKVcc 开始时间
auto start_mer_connect_kvcc = std::chrono::high_resolution_clock::now();
std::unordered_set<std::pair<int, int>, pair_hash> Inserted_Edge_mc;
MerConnectKVcc(kvcc_array[idx], kvcc_array[jdx], t_ij, t_ji, ra,
mer_cost, Inserted_Edge_mc);
// 记录 MerConnectKVcc 结束时间
auto end_mer_connect_kvcc = std::chrono::high_resolution_clock::now();
auto duration_mer_connect_kvcc =
std::chrono::duration_cast<std::chrono::seconds>(
end_mer_connect_kvcc - start_mer_connect_kvcc)
.count();
total_mer_connect_kvcc_time += duration_mer_connect_kvcc;
current_gain = gain_mer;
std::cout << "MerConnectKVcc 执行完毕,插入边数量: "
<< Inserted_Edge_mc.size() << std::endl;
if (b >= static_cast<int>(Inserted_Edge_mc.size())) {
total_Inserted_Edge.insert(Inserted_Edge_mc.begin(),
Inserted_Edge_mc.end());
// 将插入的边记录到 allInsertedEdges 中
for (const auto& edge : Inserted_Edge_mc) {
allInsertedEdges.push_back(edge);
}
b -= Inserted_Edge_mc.size();
InsertEdgesIntoGraph(Inserted_Edge_mc);
// 将 idx 和 jdx 对应的 kvcc 合并
TIntV merged_kvcc = kvcc_array[idx];
for (TIntV::TIter it = kvcc_array[jdx].BegI();
it < kvcc_array[jdx].EndI(); ++it) {
if (!merged_kvcc.IsIn(*it)) {
merged_kvcc.Add(*it);
}
}
kvcc_array[idx] = merged_kvcc;
// 移除 jdx 对应的 kvcc
kvcc_array.Del(jdx);
std::cout << "插入边成功,剩余 b: " << b << std::endl;
} else {
std::cout << "成本不足,程序结束" << std::endl;
break;
}
} else {
std::cout << "T 为空,跳过此分支" << std::endl;
}
} else if (rb >= ra && rb >= rc) {
std::cout << "选择 rb 分支" << std::endl;
if (!MC.empty()) {
int idx = Ie;
std::vector<int> mc_j = MC;
std::unordered_set<std::pair<int, int>, pair_hash> Inserted_Edge_emv;
TIntV mc_j_TIntV;
for (int vertex : mc_j) {
mc_j_TIntV.Add(vertex);
}
// 记录 ExpMulVertices 开始时间
auto start_exp_mul_vertices = std::chrono::high_resolution_clock::now();
ExpMulVertices(kvcc_array[idx], mc_j_TIntV, rb, Inserted_Edge_emv);
// 记录 ExpMulVertices 结束时间
auto end_exp_mul_vertices = std::chrono::high_resolution_clock::now();
auto duration_exp_mul_vertices =
std::chrono::duration_cast<std::chrono::seconds>(
end_exp_mul_vertices - start_exp_mul_vertices)
.count();
total_exp_mul_vertices_time += duration_exp_mul_vertices;
current_gain = gain_mul;
std::cout << "ExpMulVertices 执行完毕,插入边数量: "
<< Inserted_Edge_emv.size() << std::endl;
if (b >= static_cast<int>(Inserted_Edge_emv.size())) {
total_Inserted_Edge.insert(Inserted_Edge_emv.begin(),
Inserted_Edge_emv.end());
// 将插入的边记录到 allInsertedEdges 中
for (const auto& edge : Inserted_Edge_emv) {
allInsertedEdges.push_back(edge);
}
b -= Inserted_Edge_emv.size();
InsertEdgesIntoGraph(Inserted_Edge_emv);
// 将 mc_j_TIntV 中的点添加到 kvcc_array[idx]
for (TIntV::TIter it = mc_j_TIntV.BegI(); it < mc_j_TIntV.EndI();
++it) {
if (!kvcc_array[idx].IsIn(*it)) {
kvcc_array[idx].Add(*it);
}
}
std::cout << "插入边成功,剩余 b: " << b << std::endl;
} else {
std::cout << "成本不足,程序结束" << std::endl;
break;
}
} else {
std::cout << "MC 为空,跳过此分支" << std::endl;
}
} else {
current_gain = gain_sin;
std::cout << "选择 rc 分支" << std::endl;
if (b >= static_cast<int>(Inserted_Edge_single.size())) {
total_Inserted_Edge.insert(Inserted_Edge_single.begin(),
Inserted_Edge_single.end());
// 将插入的边记录到 allInsertedEdges 中
for (const auto& edge : Inserted_Edge_single) {
allInsertedEdges.push_back(edge);
}
b -= Inserted_Edge_single.size();
InsertEdgesIntoGraph(Inserted_Edge_single);
// 将顶点 v 加到下标为 single_vcc_idx 的 kvcc 中
if (!kvcc_array[single_vcc_idx].IsIn(v)) {
kvcc_array[single_vcc_idx].Add(v);
}
std::cout << "插入边成功,剩余 b: " << b << std::endl;
} else {
std::cout << "成本不足,程序结束" << std::endl;
break;
}
}
total_gain += current_gain;
round++;
std::cout << "第 " << round << " 轮循环结束,当前总 gain: " << total_gain
<< std::endl;
// 更新上一轮的 ra 和 rb 值
prev_ra = ra;
prev_rb = rb;
// 检查 b 的消耗情况
if (initial_b - b >= quarter_b && quarter_time == 0) {
auto current_time = std::chrono::high_resolution_clock::now();
quarter_time = std::chrono::duration_cast<std::chrono::seconds>(
current_time - start_time)
.count();
quarter_gain = total_gain;
}
if (initial_b - b >= half_b && half_time == 0) {
auto current_time = std::chrono::high_resolution_clock::now();
half_time = std::chrono::duration_cast<std::chrono::seconds>(
current_time - start_time)
.count();
half_gain = total_gain;
}
if (initial_b - b >= three_quarters_b && three_quarters_time == 0) {
auto current_time = std::chrono::high_resolution_clock::now();
three_quarters_time = std::chrono::duration_cast<std::chrono::seconds>(
current_time - start_time)
.count();
three_quarters_gain = total_gain;
}
if (b == 0 && full_time == 0) {
auto current_time = std::chrono::high_resolution_clock::now();
full_time = std::chrono::duration_cast<std::chrono::seconds>(
current_time - start_time)
.count();
full_gain = total_gain;
}
}
auto current_time = std::chrono::high_resolution_clock::now();
total_time = std::chrono::duration_cast<std::chrono::seconds>(current_time -
start_time)
.count();
std::cout << "循环结束,最终插入边数量: " << total_Inserted_Edge.size()
<< std::endl;
std::cout << "最终 b: " << b << std::endl;
std::cout << "总的 gain: " << total_gain << std::endl;
// 输出各函数总耗时
std::cout << "CalConnectKVcc 总耗时: " << total_cal_connect_kvcc_time << " 秒"
<< std::endl;
std::cout << "CalMulVerices 总耗时: " << total_cal_mul_verices_time << " 秒"
<< std::endl;
std::cout << "ExpSinVertices 总耗时: " << total_exp_sin_vertices_time << " 秒"
<< std::endl;
std::cout << "MerConnectKVcc 总耗时: " << total_mer_connect_kvcc_time << " 秒"
<< std::endl;
std::cout << "ExpMulVertices 总耗时: " << total_exp_mul_vertices_time << " 秒"
<< std::endl;
// 输出 b 消耗 25%、50%、75%、100% 时的时间和 gain
std::cout << "b 消耗 25% 时,时间: " << quarter_time
<< " 秒,gain: " << quarter_gain << std::endl;
std::cout << "b 消耗 50% 时,时间: " << half_time
<< " 秒,gain: " << half_gain << std::endl;
std::cout << "b 消耗 75% 时,时间: " << three_quarters_time
<< " 秒,gain: " << three_quarters_gain << std::endl;
std::cout << "b 消耗 100% 时,时间: " << full_time
<< " 秒,gain: " << full_gain << std::endl;
std::cout << "总耗时,时间: " << total_time << " 秒,gain: " << total_gain
<< std::endl;
// 按行输出所有插入的边
std::cout << "所有插入的边:" << std::endl;
for (const auto& edge : allInsertedEdges) {
std::cout << edge.first << " " << edge.second << std::endl;
}
}
void Master::GroupSelection_together(
TIntV& G_S, TIntV& delta_S, TIntV& delta_S_bar,
unordered_set<pair<int, int>, pair_hash>& Inserted_Edge,
TIntV& Expanded_Vertex) {
cout << "kvcc: ";
for (TIntV::TIter NI = G_S.BegI(); NI < G_S.EndI(); NI++) {
cout << *NI << " ";
}
cout << endl;
// calcuate one-hop neighbor of Graph G as candidate set
delta_S = GetBoundary(G_S, delta_S_bar);
TIntV G_sub = delta_S;
G_sub.AddVMerged(delta_S_bar);
PUNGraph G_Cand = TSnap::GetSubGraph(G, G_sub);
// calculate the edge num from vertex in delta_S_bar to k-vcc,
// divide it into S_(k), S_(k-1), S_(k-2), ...
TIntVIntV S;
for (int i = 0; i <= k; i++) {
S.Add({});
}
TIntV res = {};
TIntV nb_u1 = {}, nb_u2 = {};
// in_neighs stores the neighbors of each candidate node belonging to k-vcc
// out_neighs stores the neighbors of each candidate node belonging to
// delta_S_bar
TIntIntVH in_neighs, out_neighs;
for (TIntV::TIter TI = delta_S_bar.BegI(); TI < delta_S_bar.EndI(); TI++) {
nb_u1.Clr();
nb_u2.Clr();
TUNGraph::TNodeI v = G_Cand->GetNI(*TI);
for (int i = 0; i < v.GetInDeg(); ++i) {
if (delta_S.IsIn(v.GetInNId(i))) // only consider neighbors in
// k-vcc
nb_u1.AddMerged(v.GetInNId(i));
else
nb_u2.AddMerged(v.GetInNId(i));
}
in_neighs.AddDat(*TI, nb_u1);
out_neighs.AddDat(*TI, nb_u2);
int index = nb_u1.Len();
if (index >= k) index = k;
S[index].AddMerged(*TI);
}
// calculate maximal cliques MC in each S
int num = 0;
TIntV S_total;
TIntVIntV cliques;
TIntVTIntVH cliques_neighs_union;
priority_queue<pair<vector<int>, pair<int, int>>,
vector<pair<vector<int>, pair<int, int>>>, decltype(comp)>
clique_in_neighs(comp);
int level = 0;
for (int i = k - 1; i > 0; i--) {
debug_print("S[" << i << "]: ");
for (int j = 0; j < S[i].Len(); j++) {
int v = S[i][j];
debug_print(v << " ");
}
debug_print(endl);
TIntV S_i = S[i];
TIntV neigh_Union;
TIntV S_i_temp;
S_total.Clr();
S_total.AddVMerged(S[i]);
// to be considered how to deal with?
// if (S_total.Len() < k - i + 1) continue;
PUNGraph sub_G = TSnap::GetSubGraph(G_Cand, S_total);
// PUNGraph sub_core = TSnap::GetKCore(sub_G, k - i);
// to be considered how to deal with clique size smaller than k - i + 1
TCliqueOverlap::GetMaxCliques(sub_G, 0, cliques);
// for (int clique_idx = 0; clique_idx < cliques.Len(); clique_idx++) {
for (TIntVIntV::TIter cq = cliques.BegI(); cq < cliques.EndI(); cq++) {
neigh_Union.Clr();
int flag = 0;
vector<int> vec_cq;
for (TIntV::TIter TI = cq->BegI(); TI < cq->EndI(); TI++) {
// seems can be deleted, all vertices idx greater than 0?
// int idx = in_neighs.GetDat(*TI).Len();
// if (idx == 0) {
// flag = 1;
// break;
// }
vec_cq.push_back(*TI);
neigh_Union.AddV(in_neighs.GetDat(*TI));
}
neigh_Union.Merge();
cliques_neighs_union.AddDat(*cq, neigh_Union);
clique_in_neighs.push({vec_cq, pair<int, int>(i + vec_cq.size(), i)});
vec_cq.clear();
}
}
while (!clique_in_neighs.empty()) {
vector<int> c = clique_in_neighs.top().first;
int i = clique_in_neighs.top().second.second;
TIntV c_tmp;
int flag = 0;
for (auto& v : c) {
c_tmp.Add(v);
}
clique_in_neighs.pop();
TIntV c_neighs = cliques_neighs_union.GetDat(c_tmp);
for (auto& v : c) {
if (flag != 0) break;
int need = max(1, k - i - static_cast<int>(c.size()) +
1); // eahc v need how much edges to insert,
// 至少需要插入1条边
debug_print(v << " " << endl);
for (TIntV::TIter TI = G_S.BegI(); TI < G_S.EndI(); TI++) {
if (!in_neighs.GetDat(v).IsIn(*TI)) {
// union neighs 数量达标, 可以随便选点
if (c_neighs.Len() >= k) {
if (acost >= b) {
// TODO: 如果当前clique的budget不够,应该考虑所需budget更小的
// cout << "acost: " << acost << endl;
// cout << "gain: " << Expanded_Vertex.Len() << endl;
flag = 1;
break;
}
if (need == 0) {
flag = 2; // 满足条件,已经选完了
break;
}
if (Inserted_Edge.find({*TI, v}) != Inserted_Edge.end()) continue;
Inserted_Edge.insert({*TI, v});
debug_print("(insert: " << *TI << " " << v << ") " << endl);
need--;
acost++;
}
// union neighs 数量不达标,必须从kvcc的其他点中选
else if (!c_neighs.IsIn(*TI)) {
if (acost >= b) {
// cout << "acost: " << acost << endl;
// cout << "gain: " << Expanded_Vertex.Len() << endl;
flag = 1;
break;
}
if (need == 0) {
break;
}
// insert_edges.Add({*TI, v});
if (Inserted_Edge.find({*TI, v}) != Inserted_Edge.end()) continue;
Inserted_Edge.insert({*TI, v});
debug_print("(insert: " << *TI << " " << v << ") " << endl);
// b--;
need--;
acost++;
c_neighs.Add(*TI);
}
}
}
}
if (flag != 1 ||
(flag == 1 && k - i - static_cast<int>(c.size()) + 1 <= 0 &&
c_neighs.Len() >= k)) {
for (auto& v : c) {
// v have k neighbors in kvcc
Expanded_Vertex.Add(v);
debug_print("expanded: " << v << endl);
// v is expanded, update its neighbors
update_neighbour(S, in_neighs, out_neighs, v, Expanded_Vertex, level);
}
}
if (flag == 1) {
G_S.AddVMerged(Expanded_Vertex);
return;
}
cliques.Clr();
}
G_S.AddVMerged(Expanded_Vertex);
return;
// how to select edge to insert
}
void Master::GroupSelection_multi_vertex(
TIntV& G_S, TIntV& delta_S, TIntV& delta_S_bar,
unordered_set<pair<int, int>, pair_hash>& Inserted_Edge,
TIntV& Expanded_Vertex) {
cout << "kvcc: ";
for (TIntV::TIter NI = G_S.BegI(); NI < G_S.EndI(); NI++) {
cout << *NI << " ";
}
cout << endl;
// calcuate one-hop neighbor of Graph G as candidate set
delta_S = GetBoundary(G_S, delta_S_bar);
TIntV G_sub = delta_S;
G_sub.AddVMerged(delta_S_bar);
PUNGraph G_Cand = TSnap::GetSubGraph(G, G_sub);
// calculate the edge num from vertex in delta_S_bar to k-vcc,
// divide it into S_(k), S_(k-1), S_(k-2), ...
TIntVIntV S;
for (int i = 0; i <= k; i++) {
S.Add({});
}
TIntV res = {};
TIntV nb_u1 = {}, nb_u2 = {};
// in_neighs stores the neighbors of each candidate node belonging to k-vcc
// out_neighs stores the neighbors of each candidate node belonging to
// delta_S_bar
TIntIntVH in_neighs, out_neighs;
for (TIntV::TIter TI = delta_S_bar.BegI(); TI < delta_S_bar.EndI(); TI++) {
nb_u1.Clr();
nb_u2.Clr();
TUNGraph::TNodeI v = G_Cand->GetNI(*TI);
for (int i = 0; i < v.GetInDeg(); ++i) {
if (delta_S.IsIn(v.GetInNId(i))) // only consider neighbors in
// k-vcc
nb_u1.AddMerged(v.GetInNId(i));
else
nb_u2.AddMerged(v.GetInNId(i));
}
in_neighs.AddDat(*TI, nb_u1);
out_neighs.AddDat(*TI, nb_u2);
int index = nb_u1.Len();
// impossible situation
// if (index >= k) index = k;
S[index].AddMerged(*TI);
}
// calculate maximal cliques MC in each S
int num = 0;
TIntV S_total;
TIntVIntV cliques;
TIntVTIntVH cliques_neighs_union;
priority_queue<pair<vector<int>, pair<int, int>>,
vector<pair<vector<int>, pair<int, int>>>, decltype(comp)>
clique_in_neighs(comp);
int level = 0;
for (int i = k - 1; i > 0; i--) {
debug_print("S[" << i << "]: ");
TIntV S_i = S[i];
TIntV neigh_Union;
TIntV S_i_temp;
S_total.Clr();
S_total.AddVMerged(S[i]);
// to be considered how to deal with?
// if (S_total.Len() < k - i + 1) continue;
PUNGraph sub_G = TSnap::GetSubGraph(G_Cand, S_total);
// PUNGraph sub_core = TSnap::GetKCore(sub_G, k - i);
// to be considered how to deal with clique size smaller than k - i + 1
TCliqueOverlap::GetMaxCliques(sub_G, 0, cliques);
// for (int clique_idx = 0; clique_idx < cliques.Len(); clique_idx++) {
for (TIntVIntV::TIter cq = cliques.BegI(); cq < cliques.EndI(); cq++) {
neigh_Union.Clr();
int flag = 0;
vector<int> vec_cq;
for (TIntV::TIter TI = cq->BegI(); TI < cq->EndI(); TI++) {
// seems can be deletd, all vertices idx greater than 0?
// int idx = in_neighs.GetDat(*TI).Len();
// if (idx == 0) {
// flag = 1;
// break;
// }
vec_cq.push_back(*TI);
neigh_Union.AddV(in_neighs.GetDat(*TI));
}
neigh_Union.Merge();
cliques_neighs_union.AddDat(*cq, neigh_Union);
clique_in_neighs.push({vec_cq, pair<int, int>(i + vec_cq.size(), i)});
vec_cq.clear();
}
// (clique_in_neighs.empty()) continue;
if (clique_in_neighs.empty()) continue;
vector<int> c = clique_in_neighs.top().first;
TIntV c_tmp;
int flag = 0;
for (auto& v : c) {
c_tmp.Add(v);
}
clique_in_neighs.pop();
TIntV c_neighs = cliques_neighs_union.GetDat(c_tmp);
for (auto& v : c) {
if (flag == 1) break;
int need = max(1, k - i - static_cast<int>(c.size()) +
1); // eahc v need how much edges to insert,
// 至少需要插入1条边
debug_print(v << " " << endl);
for (TIntV::TIter TI = G_S.BegI(); TI < G_S.EndI(); TI++) {
if (!in_neighs.GetDat(v).IsIn(*TI)) {
// union neighs 数量达标, 可以随便选点
if (c_neighs.Len() >= k) {
if (acost >= b) {
// cout << "acost: " << acost << endl;
// cout << "gain: " << Expanded_Vertex.Len() << endl;
flag = 1;
break;
}
if (need == 0) {
break;
}
debug_print("(insert: " << *TI << " " << v << ") " << endl);
need--;
acost++;
}
// union neighs 数量不达标,必须从kvcc的其他点中选
else if (!c_neighs.IsIn(*TI)) {
if (acost >= b) {
// cout << "acost: " << acost << endl;
// cout << "gain: " << Expanded_Vertex.Len() << endl;
flag = 1;
break;
}
if (need == 0) {
break;
}
// insert_edges.Add({*TI, v});
if (Inserted_Edge.find({*TI, v}) != Inserted_Edge.end()) continue;
Inserted_Edge.insert({*TI, v});
debug_print("(insert: " << *TI << " " << v << ") " << endl);
// b--;
need--;
acost++;
c_neighs.Add(*TI);
}
}
}
}
if (flag == 0 || flag == 1 && k - i - static_cast<int>(c.size()) + 1 <= 0 &&
c_neighs.Len() >= k) {
for (auto& v : c) {
// v have k neighbors in kvcc
Expanded_Vertex.Add(v);
debug_print("expanded: " << v << endl);
// v is expanded, update its neighbors
update_neighbour(S, in_neighs, out_neighs, v, Expanded_Vertex, level);
}
}
if (flag == 1) {
Expanded_Vertex.Merge();
// cout << "acost: " << acost << endl;
// cout << "gain: " << Expanded_Vertex.Len() << endl;
G_S.AddVMerged(Expanded_Vertex);
return;
}
}
cliques.Clr();
G_S.AddVMerged(Expanded_Vertex);
return;
// how to select edge to insert
}
// delta_S_bar 是 S 的邻居集合,但不包括 S 中的顶点
TIntV Master::GetBoundary(TIntV G_S, TIntV& delta_S_bar) {
TIntV delta_S;
delta_S.Clr();
delta_S_bar.Clr();
for (TIntV::TIter NI = G_S.BegI(); NI < G_S.EndI(); NI++) {
TUNGraph::TNodeI Node = G->GetNI(*NI);
for (int i = 0; i < Node.GetInDeg(); i++) {
if (!G_S.IsIn(Node.GetNbrNId(i))) {
delta_S.Add(Node.GetId());
delta_S_bar.Add(Node.GetNbrNId(i));
}
}
}
delta_S.Merge();
delta_S_bar.Merge();
return delta_S;
}
std::string createString(const std::string& vcc_data, int k) {
std::ostringstream oss;
oss << vcc_data << "_k=" << k
<< "_algorithm=VCCE.kvcc"; // 使用ostringstream进行拼接
return oss.str(); // 返回拼接后的字符串
}
void Master::Load_kvcc(TIntVIntV& kvcc_array, string vcc_data) {
try {
string kvcc_data_name = createString(vcc_data, this->k);
cout << kvcc_data_name << endl;
TFIn inFile(kvcc_data_name.c_str());
kvcc_array.Load(inFile);
cout << kvcc_array.Len() << endl;
} catch (TPt<TExcept>) {
cout << endl << "***kvcc result does not exist.***" << endl;
}
for (TIntVIntV::TIter TI = kvcc_array.BegI(); TI < kvcc_array.EndI(); TI++) {
PUNGraph G_kvcc = TSnap::GetSubGraph(G, *TI);
// debug_print("kvcc_nodes: " << G_kvcc->GetNodes() << " kvcc_edges: "
// << G_kvcc->GetEdges() << endl);
}
}
void Master::GroupSelection_single_vertex(
TIntV& G_S, TIntV& delta_S, TIntV& delta_S_bar,
unordered_set<pair<int, int>, pair_hash>& Inserted_Edge,
TIntV& Expanded_Vertex) {
cout << "kvcc: ";
for (TIntV::TIter NI = G_S.BegI(); NI < G_S.EndI(); NI++) {
cout << *NI << " ";
}
cout << endl;
delta_S = GetBoundary(G_S, delta_S_bar);
TIntV G_sub = delta_S;
G_sub.AddVMerged(delta_S_bar);
PUNGraph G_Cand = TSnap::GetSubGraph(G, G_sub);
// cout << delta_S_bar.Len() << endl;
TIntVIntV S;
for (int i = 0; i <= k; i++) {
S.Add({});
}
TIntV res = {};
TIntV nb_u1 = {}, nb_u2 = {};
TIntIntVH in_neighs, out_neighs;
for (TIntV::TIter TI = delta_S_bar.BegI(); TI < delta_S_bar.EndI(); TI++) {
nb_u1.Clr();
nb_u2.Clr();
TUNGraph::TNodeI v = G_Cand->GetNI(*TI);
for (int i = 0; i < v.GetInDeg(); ++i) {
if (delta_S.IsIn(v.GetInNId(i))) // only consider neighbors in
// k-vcc
nb_u1.AddMerged(v.GetInNId(i));
else
nb_u2.AddMerged(v.GetInNId(i));
}
in_neighs.AddDat(*TI, nb_u1);
out_neighs.AddDat(*TI, nb_u2);
int index = nb_u1.Len();
// impossible situation
if (index >= k) index = k;
S[index].AddMerged(*TI);
}
int level;
for (int i = k; i > 0; i--) {
debug_print("S[" << i << "]: ");
level = i;
for (int j = 0; j < S[i].Len(); j++) {
int v = S[i][j];
debug_print(v << " ");
}
debug_print(endl);
}
for (int i = k; i > 0;) {
debug_print("S[" << i << "]: " << endl);
level = i;
for (int j = 0; j < S[i].Len(); j++) {
int v = S[i][j];
debug_print("Selected Vertex:" << v << " " << endl);
int need = k - i; // v need how much edges to insert
for (TIntV::TIter TI = G_S.BegI(); TI < G_S.EndI(); TI++) {
if (!in_neighs.GetDat(v).IsIn(*TI)) {
if (acost >= b) {
// cout << "acost: " << acost << endl;
// cout << "gain: " << Expanded_Vertex.Len() << endl;
return;
}
// insert_edges.Add({*TI, v});
if (Inserted_Edge.find({*TI, v}) != Inserted_Edge.end()) continue;
Inserted_Edge.insert({*TI, v});
debug_print("(insert: " << *TI << " " << v << ") " << endl);
// b--;
need--;
acost++;
if (need == 0) {
// v have k neighbors in kvcc
Expanded_Vertex.Add(v);
debug_print("expanded: " << v << endl);
// v is expanded, update its neighbors
update_neighbour(S, in_neighs, out_neighs, v, Expanded_Vertex,
level);
break;
}
}
}
// cout<<S[i][j]<<" ";
}
// exist a vertex in S[level] that is not expanded
if (level > i) {
i = level;
} else {
i--;
}
}
// cout << "acost: " << acost << endl;
G_S.AddVMerged(Expanded_Vertex);
return;
}
void Master::update_neighbour(TIntVIntV& S, TIntIntVH& in_neighs,
TIntIntVH& out_neighs, int v, TIntV& res,
int& level) {
// res: new expanded vertices
TIntV neigh = out_neighs.GetDat(v);
// cout << neigh.Len() << endl;
// cout << level << endl;
for (TIntV::TIter NI = neigh.BegI(); NI < neigh.EndI(); NI++) {
/*cout << v <<" " << *NI << endl;*/
if (in_neighs.GetDat(*NI).Len() != 0) {
out_neighs.GetDat(*NI).DelIfIn(v);
in_neighs.GetDat(*NI).Add(v);
}
}
for (TIntV::TIter NI = neigh.BegI(); NI < neigh.EndI(); NI++) {
int idx = in_neighs.GetDat(*NI).Len() -
1; //上面的循环已经更新过,所以这里为获得idx初始值应该-1
/*cout << idx << endl;*/
// cout << "updated_idx: " << idx + 1 << " v:" << *NI << endl;
// 回到上一层
//避免越界
idx = min(k - 1, idx);
if (idx + 1 > level) {
level = idx + 1;
}
if (idx < 1) continue;
if (idx == k - 1) {
S[idx].DelIfIn(*NI);
// S[idx + 1].AddMerged(*NI);
res.AddMerged(*NI);
debug_print("expanded by update: " << *NI << endl);
in_neighs.GetDat(*NI) = {};
update_neighbour(S, in_neighs, out_neighs, *NI, res, level);
} else if (idx < k - 1) {
S[idx].DelIfIn(*NI);
S[idx + 1].AddUnique(*NI);
}
}
}
vector<int> Master::get_common_set(TIntV& Vcc_1, TIntV& Vcc_2) {
vector<int> common_neighbors;
auto it1 = Vcc_1.BegI();
auto it2 = Vcc_2.BegI();
// both vcc_1 and vcc_2 are sorted
while (it1 != Vcc_1.EndI() && it2 != Vcc_2.EndI()) {
if (*it1 == *it2) {
common_neighbors.push_back(*it1);
++it1;
++it2;