-
Notifications
You must be signed in to change notification settings - Fork 0
/
Coding.cpp
1238 lines (1127 loc) · 36.7 KB
/
Coding.cpp
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 <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int MAXN = 1e4 + 10;
#define LOCAL
//Graph
bool g[MAXN][MAXN], update_g[MAXN][MAXN], new_g[MAXN][MAXN];
//A vertex can only appear once in a independent set
bool vertex_flag[MAXN];
//Vertex's degree
map<int, int> degree_mp;
//Vertex produce hard clause
vector<int> hard_clause_vertex;
//Vertex produce soft clause, Independent set of vertex
vector<ll> soft_clause_vertex, independentSet[MAXN];
//Delete vertex and it's neighbour by degree
vector<int> deleteVertexByDegree_vector_temp;
set<int> deleteVertexByDegree_set;
bool degree_vertex_vis[MAXN];
//Keep the vertices in G
vector<int> retain_n;
//Hard clause martix
bool hard_clause_martix_graph_temp[MAXN][MAXN];
vector<int> hard_clause_martix_graph_vector[MAXN];
//Soft clause
set<int> soft_clause_set[MAXN];
set<int>::iterator it;
bool soft_clause_setVis[MAXN], soft_clause_vertex_vis[MAXN];
//Unit Clause
vector<int> unit_clause_vector;
//G' vertex
bool deleteVertexAndNeighbor[MAXN];
//New_g Vertex
set<int> new_g_vertex_set;
bool new_g_vertex_vis[MAXN];
//Reduce rules
bool reduce_vertex_vis[MAXN];
vector<int> reduceVertexByDegree_0_vector_temp;
vector<int> reduceVertexByDegree_1_vector_temp;
vector<int> reduceVertexByDegree_2_vector_temp;
vector<int> reduceEdges[MAXN];
//AllNum of KplexNumResults
vector<int> kplexNumResults_vector[MAXN];
map<int, int> mp_kplex_size;
stack<string> splexStack;
//Number of Vertex
int ver;
//Number of Edge
ll edge;
//Number of Hard Clause, Number of Soft Clause
ll hard_count, soft_count;
ll reduceEdges_cnt = 0;
ll allNum = 0;
int verFlag = 0, vNum = 0;
vector<string> Split(const string& str, const string& delim) {
vector<string> res;
if("" == str) return res;
char * strs = new char[str.length() + 1];
strcpy(strs, str.c_str());
char * d = new char[delim.length() + 1];
strcpy(d, delim.c_str());
char *p = strtok(strs, d);
while(p) {
string s = p;
res.push_back(s);
p = strtok(NULL, d);
}
return res;
}
void ReadFile(string fileName){
string str;
//fstream in("./V5.wclq", ios::in);
fstream in(fileName);
if(!in) {
cout << "Read file error from ReadFile!" << endl;
}
while(getline(in, str)) {
if(str[0] == 'c'){
continue;
}else if(str[0] == 'p'){
vector<string> res = Split(str, " ");
ver = atoi(res[2].c_str());
edge = atoi(res[3].c_str());
#ifdef LOCAL
cout << "ver : " << ver << " edge : " << edge << endl;
#endif
}else if(str[0] == 'e'){
vector<string> res = Split(str, " ");
int x, y;
x = atoi(res[1].c_str());
y = atoi(res[2].c_str());
new_g_vertex_set.insert(x);
new_g_vertex_set.insert(y);
g[x][y] = g[y][x] = true;
}
}
}
int IndependentSet(){
int independent_count = 0;
independentSet[0].clear();
independentSet[independent_count++].push_back(soft_clause_vertex[0]);
for(int i = 1; i < soft_clause_vertex.size(); i++) {
int now = soft_clause_vertex[i];
int cnt = 0;
for(int j = 0; j < independent_count; j++) {
int flag = 1;
for(int k = 0; k < independentSet[j].size(); k++) {
int nw = independentSet[j][k];
if(g[nw][now]) {
flag = 0;
break;
}
}
if(flag) {
if(!vertex_flag[now]){
independentSet[j].push_back(now);
vertex_flag[now] = true;
}
} else {
cnt++;
}
}
if(cnt == independent_count) {
if(!vertex_flag[now]){
independentSet[independent_count++].push_back(now);
vertex_flag[now] = true;
}
}
}
return independent_count;
}
int Check(int independentCount){
int cnt = 0;
for(int i = 0;i < independentCount;i++){
if(!soft_clause_setVis[i]){
if(soft_clause_set[i].size() == 1){
unit_clause_vector.push_back(i);
cnt++;
soft_clause_setVis[i] = true;
}
}
}
if(unit_clause_vector.size() == 0){
for(int i = 0;i < independentCount;i++){
if(!soft_clause_setVis[i]){
unit_clause_vector.push_back(i);
cnt++;
soft_clause_setVis[i] = true;
break;
}
}
}
return cnt;
}
int IncongruentClauseSetUpperBound(int independentCount){
int cnt = 0, size;
while( size = Check(independentCount) ){
// printf("size: %d\n", size);
//count
for(int i = 0; i < size; i++) {
int idx = unit_clause_vector[i];
it = soft_clause_set[idx].begin();
int now = *it;
// printf("countnow: %d\n", now);
for(int j = i + 1;j < size;j++){
int idx = unit_clause_vector[j];
it = soft_clause_set[idx].begin();
int next = *it;
// printf("countnext: %d\n", next);
for(int k = 0;k < hard_clause_martix_graph_vector[now].size();k++){
if(next == hard_clause_martix_graph_vector[now][k]){
cnt++;
}
}
}
}
//delete
for(int i = 0;i < size;i++){
int idx = unit_clause_vector[i];
it = soft_clause_set[idx].begin();
int now = *it;
// printf("delete vertex now: %d\n", now);
if(!soft_clause_vertex_vis[now]){
for(int b = 0;b < independentCount;b++){
if(soft_clause_set[b].count(now)){
soft_clause_set[b].erase(now);
}
}
}
soft_clause_vertex_vis[now] = true;
// puts("----");
for(int a = 0;a < hard_clause_martix_graph_vector[now].size();a++){
int temp = hard_clause_martix_graph_vector[now][a];
// printf("delete vertex: %d\n", temp);
if(!soft_clause_vertex_vis[temp]){
for(int b = 0;b < independentCount;b++){
if(soft_clause_set[b].count(temp)){
soft_clause_set[b].erase(temp);
}
}
}
soft_clause_vertex_vis[temp] = true;
}
}
unit_clause_vector.clear();
// puts("");
}
#ifdef LOCAL
cout << "IncongruentClauseSetUpperBound's cnt : " << cnt << endl;
#endif
return (independentCount - cnt);
}
int DeleteVertexByDegree(int r){
int cnt = 0;
for(int i = 1;i <= ver;i++){
if(!reduce_vertex_vis[i]){
if(new_g_vertex_vis[i]){
if( (degree_mp[i] < r - 1) && (!degree_vertex_vis[i])){
deleteVertexByDegree_vector_temp.push_back(i);
deleteVertexByDegree_set.insert(i);
for(int j = 1;j <= ver;j++){
if(g[i][j] && g[j][i]){
deleteVertexByDegree_set.insert(j);
}
}
cnt++;
degree_vertex_vis[i] = true;
}
}
}
}
return cnt;
}
void DegreeUpdateG(){
for(int i = 1;i <= ver;i++){
if(!reduce_vertex_vis[i]){
if(new_g_vertex_vis[i]){
for(int j = 1;j <= ver;j++){
if(new_g_vertex_vis[j]){
if(i == j){
continue;
}
if(update_g[i][j] && update_g[j][i]){
degree_mp[i]++;
}
}
}
}
}
}
}
void Prune(int bound){
#ifdef LOCAL
puts("-------------------------");
cout << "start prune : " << endl;
#endif
int size;
while(size = DeleteVertexByDegree(bound)){
#ifdef LOCAL
cout << "size : " << size << endl;
#endif
for(int i = 0;i < size;i++){
int temp = deleteVertexByDegree_vector_temp[i];
#ifdef LOCAL
cout << "deleteVertexByDegree_temp is : " << temp << endl;
#endif
for(int j = 1;j <= ver;j++){
if(!reduce_vertex_vis[i]){
if(new_g_vertex_vis[j]){
update_g[temp][j] = update_g[j][temp] = false;
}
}
}
degree_mp.clear();
DegreeUpdateG();
}
deleteVertexByDegree_vector_temp.clear();
#ifdef LOCAL
puts("");
#endif
}
}
int Degree_0(){
int cnt = 0;
for(int i = 1;i <= ver;i++){
if(new_g_vertex_vis[i]){
if( (degree_mp[i] == 0 && !reduce_vertex_vis[i]) ){
reduceVertexByDegree_0_vector_temp.push_back(i);
cnt++;
reduce_vertex_vis[i] = true;
}
}
}
return cnt;
}
int Degree_1(){
int cnt = 0;
for(int i = 1;i <= ver;i++){
if(new_g_vertex_vis[i]){
if( (degree_mp[i] == 1 && !reduce_vertex_vis[i]) ){
reduceVertexByDegree_1_vector_temp.push_back(i);
cnt++;
reduce_vertex_vis[i] = true;
}
}
}
return cnt;
}
int Degree_2(){
int cnt = 0;
for(int i = 1;i <= ver;i++){
if(new_g_vertex_vis[i]){
if( (degree_mp[i] == 2 && !reduce_vertex_vis[i]) ){
vector<int> reduce_vertex_neighbor;
reduce_vertex_neighbor.clear();
for(int j = 1;j <= ver;j++){
if(new_g_vertex_vis[j]){
if(g[i][j] && g[j][i]){
reduce_vertex_neighbor.push_back(j);
}
}
}
int ni = reduce_vertex_neighbor[0];
int nj = reduce_vertex_neighbor[1];
if(!g[ni][nj] && !g[nj][ni]){
reduceVertexByDegree_2_vector_temp.push_back(i);
cnt++;
reduce_vertex_vis[i] = true;
}
}
}
}
return cnt;
}
void DegreeG(){
for(int i = 1;i <= ver;i++){
if(new_g_vertex_vis[i]){
for(int j = 1;j <= ver;j++){
if(new_g_vertex_vis[j]){
if(i == j){
continue;
}
if(g[i][j] && g[j][i]){
degree_mp[i]++;
}
}
}
}
}
}
void Reduce(){
#ifdef LOCAL
puts("-------------------------");
cout << "start reduce : " << endl;
#endif
int size0, size1, size2;
int flag1, flag2;
do{
flag1 = 0, flag2 = 0;
// puts("");
for(int i = 1;i <= ver;i++){
if(new_g_vertex_vis[i]){
// cout << i << " " << reduce_vertex_vis[i] << endl;
if(reduce_vertex_vis[i] == 0){
flag1++;
}
}
}
while(size0 = Degree_0()){
#ifdef LOCAL
cout << "size0 : " << size0 << endl;
#endif
for(int i = 0;i < size0;i++){
int temp = reduceVertexByDegree_0_vector_temp[i];
#ifdef LOCAL
cout << "reduceVertexByDegree_0_temp is : " << temp << endl;
#endif
degree_mp.clear();
DegreeG();
}
reduceVertexByDegree_0_vector_temp.clear();
#ifdef LOCAL
puts("");
#endif
}
while(size1 = Degree_1()){
#ifdef LOCAL
cout << "size1 : " << size1 << endl;
#endif
for(int i = 0;i < size1;i++){
int temp = reduceVertexByDegree_1_vector_temp[i];
#ifdef LOCAL
cout << "reduceVertexByDegree_1_temp is : " << temp << endl;
#endif
for(int j = 1;j <= ver;j++){
if(new_g_vertex_vis[j]){
if(g[temp][j] && g[j][temp]){
g[temp][j] = g[j][temp] = false;
reduceEdges_cnt++;
reduceEdges[reduceEdges_cnt].push_back(temp);
reduceEdges[reduceEdges_cnt].push_back(j);
}
}
}
degree_mp.clear();
DegreeG();
}
reduceVertexByDegree_1_vector_temp.clear();
#ifdef LOCAL
puts("");
#endif
}
while(size2 = Degree_2()){
#ifdef LOCAL
cout << "size2 : " << size2 << endl;
#endif
for(int i = 0;i < size2;i++){
int temp = reduceVertexByDegree_2_vector_temp[i];
#ifdef LOCAL
cout << "reduceVertexByDegree_2_temp is : " << temp << endl;
#endif
for(int j = 1;j <= ver;j++){
if(new_g_vertex_vis[j]){
if(g[temp][j] && g[j][temp]){
g[temp][j] = g[j][temp] = false;
reduceEdges_cnt++;
reduceEdges[reduceEdges_cnt].push_back(temp);
reduceEdges[reduceEdges_cnt].push_back(j);
}
}
}
degree_mp.clear();
DegreeG();
}
reduceVertexByDegree_2_vector_temp.clear();
#ifdef LOCAL
puts("");
#endif
}
// puts("");
for(int i = 1;i <= ver;i++){
if(new_g_vertex_vis[i]){
// cout << i << " " << reduce_vertex_vis[i] << endl;
if(reduce_vertex_vis[i] == 0){
flag2++;
}
}
}
}while(flag1 != flag2);
}
void StringReplace( std::string &strBig, const std::string &strsrc, const std::string &strdst) {
std::string::size_type pos = 0;
std::string::size_type srclen = strsrc.size();
std::string::size_type dstlen = strdst.size();
while( (pos = strBig.find(strsrc, pos)) != std::string::npos ) {
strBig.replace( pos, srclen, strdst );
pos += dstlen;
}
}
string GetPathOrURLShortName(std::string strFullName) {
if (strFullName.empty()) {
return "";
}
StringReplace(strFullName, "/", "\\");
std::string::size_type iPos = strFullName.find_last_of('\\') + 1;
return strFullName.substr(iPos, strFullName.length() - iPos);
}
int GetKplexNum(string s) {
fstream fin(s.c_str(), ios::in);
if(!fin) {
cerr << "Can not open file from GetKplexNum!" <<endl;
return -1;
}
char c;
int lineCnt = 0;
while(fin.get(c)) {
if(c =='\n'){
lineCnt++;
}
}
fin.close();
return lineCnt;
}
void GetKplexNumResults(string s, int kplexNum){
fstream fin(s.c_str(), ios::in);
if(!fin) {
cerr << "Can not open file from GetKplexNumResults!" << endl;
}
for(int i = 0;i < kplexNum;i++){
string temp;
getline(fin, temp);
allNum++;
string str_input = temp;
vector<int> nums;
nums.clear();
char *s_input = (char *)str_input.c_str();
const char * split = "', '";
char *p = strtok(s_input, split);
int a;
while(p != NULL) {
sscanf(p, "%d", &a);
nums.push_back(a);
p = strtok(NULL, split);
}
for(int a = 1; a < nums.size() - 1; a++) {
kplexNumResults_vector[allNum].push_back(nums[a]);
}
}
fin.close();
}
bool cmp(const pair<int, int>& a, const pair<int, int>& b) {
return a.second > b.second;
}
string GetSolutionFromSplex(string file) {
ifstream inf(file);
if (!inf) {
cerr << "Can not open file from GetSolutionFromSplex!\n";
}
string oneline;
while (inf.peek() != EOF) {
getline(inf, oneline);
splexStack.push(oneline);
}
return splexStack.top();
}
int GetS(string file) {
string solutionFromSplex = GetSolutionFromSplex(file);
char * strs = new char[solutionFromSplex.length() + 1];
strcpy(strs, solutionFromSplex.c_str());
const char *sep = ": ";
char *p = strtok(strs, sep);
int cnt = 0;
while(p) {
printf("%s\n", p);
p = strtok(NULL, sep);
cnt++;
}
return cnt - 1;
}
int main(int argc, char **argv) {
int K = atoi(argv[2]);
int _k = K;
string strFilePath = argv[1];
int fileCnt = 0, fileCntSplex = 0;
bool isReduce = false;
memset(reduce_vertex_vis, false, sizeof(reduce_vertex_vis));
while(true){
ll S = 0;
hard_count = 0, soft_count = 0;
int g_edge = 0, update_g_edge = 0, newEdge = 0;
//initialize
memset(g, false, sizeof(g));
memset(update_g, false, sizeof(update_g));
memset(new_g, false, sizeof(new_g));
memset(vertex_flag, false, sizeof(vertex_flag));
memset(hard_clause_martix_graph_temp, false, sizeof(hard_clause_martix_graph_temp));
memset(soft_clause_vertex_vis, false, sizeof(soft_clause_vertex_vis));
memset(soft_clause_setVis, false, sizeof(soft_clause_setVis));
memset(degree_vertex_vis, false, sizeof(degree_vertex_vis));
memset(deleteVertexAndNeighbor, false, sizeof(deleteVertexAndNeighbor));
memset(new_g_vertex_vis, false, sizeof(new_g_vertex_vis));
retain_n.clear();
degree_mp.clear();
hard_clause_vertex.clear();
soft_clause_vertex.clear();
deleteVertexByDegree_vector_temp.clear();
reduceVertexByDegree_0_vector_temp.clear();
reduceVertexByDegree_1_vector_temp.clear();
reduceVertexByDegree_2_vector_temp.clear();
deleteVertexByDegree_set.clear();
unit_clause_vector.clear();
new_g_vertex_set.clear();
for(int i = 1;i <= MAXN;i++){
independentSet[i].clear();
hard_clause_martix_graph_vector[i].clear();
soft_clause_set[i].clear();
}
//read file
#ifdef LOCAL
puts("----------------------------------------------------------------------------");
cout << "fileNamePath : " << strFilePath << endl;
#endif
ReadFile(strFilePath);
string fileNameExtension = GetPathOrURLShortName(strFilePath);
string fileName = fileNameExtension.substr(0, fileNameExtension.rfind("."));
#ifdef LOCAL
cout << "fileNameExtension : " << fileNameExtension << endl;
cout << "fileName : " << fileName << endl;
#endif
#ifdef LOCAL
puts("-------------------------");
cout << "new_g_vertex_set : ";
#endif
for(it = new_g_vertex_set.begin(); it != new_g_vertex_set.end(); ++it) {
#ifdef LOCAL
printf("%d ", *it);
#endif
new_g_vertex_vis[*it] = true;
}
#ifdef LOCAL
puts("");
#endif
int new_g_vertex_num = 0;
// cout << "new_g_vertex_vis : " << endl;
for(int i = 1;i <= ver;i++){
if(new_g_vertex_vis[i]){
new_g_vertex_num++;
}
// cout << i << " " << new_g_vertex_vis[i] << endl;
}
#ifdef LOCAL
cout << "new_g_vertex_num : " << new_g_vertex_num << endl;
#endif
//initialize degree_mp
for(int i = 1;i <= ver;i++){
degree_mp[i] = 0;
}
//original G
// puts("");
// puts("-------------------------");
// cout << "G : " << endl;
// for(int i = 1;i <= ver;i++){
// for(int j = 1;j <= ver;j++){
// cout << g[i][j] << " ";
// }
// puts("");
// }
//vertex's degree
DegreeG();
#ifdef LOCAL
puts("-------------------------");
cout << "start vertex's degree : " << endl;
#endif
int startVertexNum = 0;
for(int i = 1;i <= ver;i++){
if(!reduce_vertex_vis[i]){
if(new_g_vertex_vis[i]){
#ifdef LOCAL
cout << i << " " << degree_mp[i] << " " << endl;
#endif
startVertexNum++;
}
}
}
#ifdef LOCAL
cout << "startVertexNum : " << startVertexNum << endl;
#endif
if(!isReduce){
Reduce();
isReduce = true;
}
#ifdef LOCAL
puts("-------------------------");
#endif
// cout << "reduce_vertex_vis : " << endl;
int reduce_vertex_vis_num = 0;
for(int i = 1;i <= ver;i++){
// cout << i << " " << reduce_vertex_vis[i] << endl;
if(reduce_vertex_vis[i]){
reduce_vertex_vis_num++;
}
}
#ifdef LOCAL
cout << "reduce_vertex_vis_num : " << reduce_vertex_vis_num << endl;
#endif
#ifdef LOCAL
puts("-------------------------");
cout << "reduceEdges_cnt : " << reduceEdges_cnt << endl;
for(int i = 1;i <= reduceEdges_cnt;i++){
for(int j = 0;j < reduceEdges[i].size();j++){
cout << reduceEdges[i][j] << " ";
}
puts("");
}
#endif
#ifdef LOCAL
puts("-------------------------");
cout << "after reduce vertex's degree : " << endl;
#endif
int afterReduceVertexNum = 0;
for(int i = 1;i <= ver;i++){
if(!reduce_vertex_vis[i]){
if(new_g_vertex_vis[i]){
#ifdef LOCAL
cout << i << " " << degree_mp[i] << " " << endl;
#endif
afterReduceVertexNum++;
}
}
}
#ifdef LOCAL
cout << "afterReduceVertexNum : " << afterReduceVertexNum << endl;
#endif
//reduce g to update_g
// puts("");
// puts("-------------------------");
// cout << "reduce g to update_g : " << endl;
for(int i = 1;i <= ver;i++){
for(int j = 1;j <= ver;j++){
// cout << g[i][j] << " ";
if(g[i][j]){
g_edge++;
}
if(g[i][j]){
update_g[i][j] = true;
}
}
// puts("");
}
#ifdef LOCAL
puts("-------------------------");
cout << "after reduce g_edge : " << g_edge / 2 << endl;
#endif
if(g_edge == 0){
#ifdef LOCAL
cout << "Case: 1 OK!" << endl;
#endif
break;
}
/**
* Hard Clause produce
*/
for(int i = 1; i <= ver; i++) {
if(!reduce_vertex_vis[i]){
if(new_g_vertex_vis[i]){
for(int j = 1; j <= ver; j++) {
if(!reduce_vertex_vis[j]){
if(new_g_vertex_vis[j]){
if(i == j) {
continue;
} else if(i < j) {
if(!g[i][j] && !g[j][i]) {
hard_clause_vertex.push_back(i);
hard_clause_vertex.push_back(j);
}
}
}
}
}
}
}
}
#ifdef LOCAL
puts("-------------------------");
#endif
// cout << "Vertex produce hard clause : ";
// for(int i = 0; i < hard_clause_vertex.size(); i++) {
// cout << hard_clause_vertex[i] << " ";
// }
// puts("");
// cout << "Hard Clause :" << endl;
for(int i = 0; i < hard_clause_vertex.size();) {
// cout << "-" << hard_clause_vertex[i] << " -" << hard_clause_vertex[i + 1] << endl;
int temp = hard_clause_vertex[i];
int temp2 = hard_clause_vertex[i + 1];
hard_clause_martix_graph_temp[temp][temp2] = true;
hard_clause_martix_graph_temp[temp2][temp] = true;
i += 2;
hard_count++;
}
#ifdef LOCAL
cout << "hard_count : " << hard_count << endl;
#endif
//hard_clause_martix_graph_temp
for(int i = 1;i <= ver;i++){
for(int j = 1;j <= ver;j++){
int temp = hard_clause_martix_graph_temp[i][j];
if(temp == true){
hard_clause_martix_graph_vector[i].push_back(j);
}
}
}
// puts("-------------------------");
// cout << "hard_clause_martix_graph_vector : " << endl;
// for(int i = 1; i <= ver; i++) {
// if(!reduce_vertex_vis[i]){
// if(new_g_vertex_vis[i]){
// cout << i << " ";
// for(int j = 0; j < hard_clause_martix_graph_vector[i].size(); j++) {
// cout << hard_clause_martix_graph_vector[i][j] << " ";
// }
// puts("");
// }
// }
// }
/**
* Soft Clause produce
*/
for(int i = 1;i <= ver;i++){
if(!reduce_vertex_vis[i]){
if(new_g_vertex_vis[i]){
soft_clause_vertex.push_back(i);
}
}
}
// puts("-------------------------");
// cout << "Vertex produce soft clause : ";
// for(int i = 0; i < soft_clause_vertex.size(); i++) {
// cout << soft_clause_vertex[i] << " ";
// }
// puts("");
int independent_count = IndependentSet();
#ifdef LOCAL
puts("-------------------------");
cout << "independent_count : " << independent_count << endl;
#endif
#ifdef LOCAL
puts("-------------------------");
cout << "Soft Clause : " << endl;
#endif
for(int i = 0; i < independent_count; i++) {
vector<ll> independentSet_temp;
for(int j = 0; j < independentSet[i].size(); j++) {
independentSet_temp.push_back(independentSet[i][j]);
}
for(int k = 0;k < independentSet_temp.size();k++){
#ifdef LOCAL
cout << independentSet_temp[k] << " ";
#endif
int temp = independentSet_temp[k];
soft_clause_set[soft_count].insert(temp);
}
#ifdef LOCAL
puts("");
#endif
soft_count++;
independentSet_temp.clear();
}
#ifdef LOCAL
cout << "soft_count : " << soft_count << endl;
#endif
#ifdef LOCAL
puts("-------------------------");
#endif
int incongruentClauseSetUpperBound = IncongruentClauseSetUpperBound(independent_count);
#ifdef LOCAL
puts("-------------------------");
cout << "incongruentClauseSetUpperBound : " << incongruentClauseSetUpperBound << endl;
#endif
#ifdef LOCAL
puts("-------------------------");
#endif
int bound_temp = independent_count;
int bound = ceil( (bound_temp + 3) / 2 );
#ifdef LOCAL
cout << "bound : " << bound << endl;
#endif
Prune(bound);
// puts("-------------------------");
// cout << "update_g : " << endl;
for(int i = 1;i <= ver;i++){
for(int j = 1;j <= ver;j++){
// cout << update_g[i][j] << " ";
if(update_g[i][j]){
update_g_edge++;
}
}
// puts("");
}
#ifdef LOCAL
puts("-------------------------");
cout << "update_g_edge : " << update_g_edge / 2 << endl;
#endif
if(update_g_edge == 0){
#ifdef LOCAL
cout << "Case: 2 OK!" << endl;
#endif
break;
}
#ifdef LOCAL
puts("-------------------------");
cout << "end vertex's degree : " << endl;
#endif
int endVertexNum = 0;
for(int i = 1;i <= ver;i++){
if(!reduce_vertex_vis[i]){
if(new_g_vertex_vis[i]){
if(degree_mp[i]){
#ifdef LOCAL
cout << i << " " << degree_mp[i] << " " << endl;
#endif
endVertexNum++;
retain_n.push_back(i);
}
}
}
}
#ifdef LOCAL
cout << "endVertexNum : " << endVertexNum << endl;
#endif
#ifdef LOCAL
puts("-------------------------");
cout << "deleteVertexByDegree_set : ";
#endif
for(it = deleteVertexByDegree_set.begin(); it != deleteVertexByDegree_set.end(); ++it) {
#ifdef LOCAL
printf("%d ", *it);
#endif
deleteVertexAndNeighbor[*it] = true;
}
#ifdef LOCAL
puts("");
cout << "retain_n : ";
for(int i = 0;i < retain_n.size();i++){
cout << retain_n[i] << " ";
}