-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathDAF_div.cpp
More file actions
3982 lines (3407 loc) · 137 KB
/
DAF_div.cpp
File metadata and controls
3982 lines (3407 loc) · 137 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 "DAF.h"
int* marking_cnt;
int cur_k;
int num_k;
class emb_element{
public:
int* mapped_vertex;
emb_element(){
mapped_vertex = new int[cnt_node_query];
}
};
vector<emb_element> stored_emb;
bool b_over_time_limit = false;
#ifdef WINDOWS
bool b_search_end = false;
#endif
void* timer(void* args)
{
// int time_limit_milli = 1000 * 60 * 10; //10 minutes
int time_limit_milli = 1000 * 10; //10 minutes
int sleep_time_sec = 1; //1 second
auto begin = chrono::high_resolution_clock::now();
while(true)
{
#ifdef WINDOWS
if (b_search_end)
return nullptr;
#endif
auto now = chrono::high_resolution_clock::now();
chrono::duration<double, milli> elapsed = now - begin;
if(elapsed.count() > time_limit_milli)
{
b_over_time_limit = true;
return nullptr;
}
#ifdef WINDOWS
this_thread::sleep_for(std::chrono::milliseconds(sleep_time_sec * 1000));
#else
sleep(sleep_time_sec);
#endif
}
return nullptr;
}
//for dynamic programming between a directed acyclic graph and a graph
void BFS_DAG();
void print_DAG(); //for debug
void print_CPI(); //for print CPI; TODO: rename function
//dynamic programming between a DAG and a graph
void topDownInitial();
void bottomUpIterate();
void topDownIterate();
void adjacencyListConstruction();
//ancestors for failing set
//===== some key counts ===============================
int * label_degree_to_node;
inline void preprocessDataGraph(string datagraphFile) {
//preprocess the data graph to extract some key info which is to be used in the next function
ifstream fin(datagraphFile);
string line;
getline(fin, line);
vector<string> v;
split(line, v, ' ');
cnt_node = atoi(v[2].c_str()); //get count_node_number
nodes_label = new int [cnt_node];
while (getline(fin, line)) {
if (line.at(0) == 'v') {
split(line, v, ' ');
int label = atoi(v[2].c_str());
if (label > largest_label)
largest_label = label;
unique_label_set.insert(label);
}
if (line.at(0) == 'e')
count_edge_number++;
}
cnt_unique_label = unique_label_set.size();
label_freqency = new int [ cnt_unique_label + 1 ];
memset(label_freqency, 0, sizeof(int) * (cnt_unique_label + 1));
}
inline void readAndProcessDataGraph(string datagraphFile) {
/* Function readAndProcessDataGraph(string datagraphFile)
* 1. For vertices, calculate nodes_label[v_id] and label_frequency[actual_label]
* 2. For edges, calculate the adjacent list nodes[v_id] and data_edge_matrix[v1_id*size+v2_id]
* data_edge_matrix[i*size+j] = 1 if there is an edge (i,j). data_edge_matrix[i*size+j] = 0 otherwise
* 3. For each vertex, store its degree degree_data[v_id] and core_number_data[v_id]
* 4. For each vertex, create nodes_data[sum_degree], which stores all vertices' adjacent lists,
* nodes_info[v_id], which stores the starting position of v_id's adjacent list in nodes_data
* nodes_to_label_info[v_id*(cnt_unique_label+1)+cur_label], which stores the starting positions and number of
* v_id's neighbors with cur_label in nodes_data
* 5. Build label_degree_to_node[v_id], a sorted array of all vertices by their labels and then ascending order of degree,
* and label_deg_label_pos[label], which stores a pair of
* <the largest degree of this label, the end position (not included) of this label's largest degree >
* 6. Build the NLF lightweight check
* 7. Create an |V|-sized array of HashTable only if |V| is larger than the size of edge matrix
* 8. Compute the maximum neighbor degree MAX_NB_degree_data[v_id] for each vertex
*/
/* The input file should be in the node and edge format
* This function read the data graph and store it in two formats: node, edge and adjacent list
* nodes[]: the array store a node's adjacent list
* edges[]: store all edges
* node_label: store the label of the nodes
*/
// used new label to node index with the degree sensitive
//initialize the array to all 0;
vector<vector<int> > nodes; //locally in this function, such that it'll be released to save space
nodes.resize( cnt_node );
transferredLabel = new int[largest_label + 1];
memset(transferredLabel, 0, sizeof(int) * (largest_label + 1));
int label_index = 1; //start from 1, because 0 means not found the label
nodes_info = new int[cnt_node + 1];
nodes_to_label_info = new pair<int, int> [cnt_node * (cnt_unique_label + 1)];
int node_id = 0;
//===== read data from file ================
ifstream fin(datagraphFile);
string line;
vector<string> v;
while (getline(fin, line)) {
if (line.at(0) == 'v') { //this is node
split(line, v, ' ');
int label = atoi(v[2].c_str()); // get node label
int actual_label = transferredLabel[label]; // transfer the label to the actual label we can use
if (actual_label == 0) { //means this label is first time being transfer
actual_label = label_index;
transferredLabel[label] = label_index;
label_index++; // maintain the label index. notice that 0 means not found here, therefore 0 is not a valid label
}
nodes_label[node_id] = actual_label;
node_id++;
label_freqency[actual_label] ++;
}
if (line.at(0) == 'e') {
split(line, v, ' ');
int left_node = atoi(v[1].c_str());
int right_node = atoi(v[2].c_str());
if (left_node != right_node){//put the nodes into the adjacent list
nodes[left_node].push_back(right_node);
nodes[right_node].push_back(left_node);
}
}
}
fin.close();
//===================
//====== build the degree and core number array ===============
core_number_data = new int [cnt_node];
degree_data = new int[cnt_node];
MAX_DEGREE = 0;
int sum_degree = 0;
for (int i = 0; i < cnt_node; i++) {
int degree = nodes[i].size();
degree_data[i] = degree;
//std::cout<<"degree_data["<<i<<"]: "<<degree<<std::endl;
core_number_data[i] = degree;
sum_degree += degree;
if (degree > MAX_DEGREE)
MAX_DEGREE = degree;
}
//==============================================
nodes_data = new int [sum_degree];
int cur_array_index = 0;
int cur_matrix_index = 0;
for (int i = 0; i < cnt_node; i++) {
//========================= put this node's sorted adjacent list into the array ================================================
if (nodes[i].size() == 0){ //deal with isolated nodes, specially for yeast
// cerr << i << " => degree zero node!!" << endl;
nodes_info[i] = cur_array_index;
continue;
}
nodes_info[i] = cur_array_index; // indicate the starting position of node i's adjacent list in "nodes_data"
//stable_sort(nodes[i].begin(), nodes[i].end(), sortByLabelAndDegree);//sort by label and then ascending order of degree
sort(nodes[i].begin(), nodes[i].end(), sortByLabelAndDegree);//sort by label and then ascending order of degree
copy (nodes[i].begin(), nodes[i].end(), nodes_data + cur_array_index );
cur_array_index += nodes[i].size(); // maintain the index
//=====================================================================================
int cur_label = nodes_label[ nodes[i][0] ];
int cur_count = 1;
if (nodes[i].size() == 1) { //special case: there is only one node in the adjacent list
nodes_to_label_info[i * (cnt_unique_label + 1) + cur_label] = make_pair(cur_matrix_index, cur_count);
cur_matrix_index += cur_count;
continue;
}
for (int j = 1; j < nodes[i].size(); j++) {
int this_label = nodes_label[nodes[i][j]];
if (this_label == cur_label)
cur_count++;
else {
nodes_to_label_info[i * (cnt_unique_label + 1) + cur_label] = make_pair(cur_matrix_index, cur_count);
cur_matrix_index += cur_count;
cur_label = this_label;
cur_count = 1;
}
}
nodes_to_label_info[i * (cnt_unique_label + 1) + cur_label] = make_pair(cur_matrix_index, cur_count);
cur_matrix_index += cur_count;
//=====================================================================================
} //end for
nodes_info[cnt_node] = sum_degree; //the end position for the last node of the nodes_info
//=========== build a structure for the "label to nodes with degree" array =============
int last_label = 0;
int last_degree = 0;
label_degree_to_node = new int[cnt_node];
for (int i = 0; i < cnt_node; i++)
label_degree_to_node[i] = i;
//stable_sort(label_degree_to_node, label_degree_to_node + cnt_node, sortByLabelandDegree);
sort(label_degree_to_node, label_degree_to_node + cnt_node, sortByLabelandDegree);
//<the largest degree of this label, the end position (not included) of this label's largest degree >
label_deg_label_pos = new pair<int, int> [ cnt_unique_label + 1 ];
degree_array.resize(cnt_node);
for (int i = 0; i < cnt_node; i++)
degree_array [i] = degree_data [ label_degree_to_node [i] ];
for (int i = 0; i < cnt_node; i++) {
int v = label_degree_to_node[i];
int label = nodes_label[ v ];
int degree = degree_data[ v ];
if (i == 0) {
label_deg_label_pos[ 0 ] = make_pair(0, 0); //actully, "0" is not used as label, so not necessary
last_label = label;
last_degree = degree;
} else {
if (label != last_label) //deal with a new label,
label_deg_label_pos[ last_label ] = make_pair(last_degree, i);
last_label = label;
last_degree = degree;
}
}
label_deg_label_pos[ last_label ] = make_pair(last_degree, cnt_node);//degree value is meaningless here.
//========================================================================================
NEC_mapping = new int[(cnt_unique_label + 1) * MAX_QUERY_NODE]; //label and the parent node id
memset(NEC_mapping, 0, sizeof(int) * (cnt_unique_label + 1) * MAX_QUERY_NODE);
NEC_mapping_pair = new NEC_element[(cnt_unique_label + 1) * MAX_QUERY_NODE]; // <label, parent node id>
NEC_set_array = new NEC_set_array_element[(cnt_unique_label + 1) * MAX_QUERY_NODE]; // <parent_id, sum>
//================= build the NFL lightweight check =====================
NLF_size = (cnt_unique_label + 1) / SIZEOF_INT + 1;
NLF_array = new int [ NLF_size ]; // the array for query graph
NLF_check = new int [cnt_node * NLF_size];
memset(NLF_check, 0, sizeof(int) * NLF_size * cnt_node);
for (int i = 0; i < nodes.size(); i++)
for (int j = 0; j < nodes[i].size(); j++){
int label = nodes_label[ nodes[i][j] ];
int idx = NLF_size - 1 - label / SIZEOF_INT;
int pos = label % SIZEOF_INT;
NLF_check[i * NLF_size + idx] |= (1 << pos);
}
//=====================================================================================
//============== initialize the MAX neighbor degree of data node =====================
MAX_NB_degree_data = new int [cnt_node];
for (int i = 0; i < cnt_node; i++){
int max_degree = 0;
for (int j = 0; j < nodes[i].size(); j++){
int node = nodes[i][j];
if (degree_data[node] > max_degree)
max_degree = degree_data[node];
}
MAX_NB_degree_data[i] = max_degree;
}
//=====================================================================================
}
inline void dataGraphAnalysis(string datagraphFile) {
/* The input file should be in the node and edge format
* This function read the data graph and store it in two formats: node, edge and adjacent list
* nodes[]: the array store a node's adjacent list
* edges[]: store all edges
* node_label: store the label of the nodes
*/
int AVERAGE_DEGREE_DATA_SUM = 0;
{
ifstream fin(datagraphFile);
string line;
getline(fin, line);
vector<string> v;
split(line, v, ' ');
cnt_node = atoi(v[2].c_str()); //get count_node_number
while (getline(fin, line)) {
if (line.at(0) == 'v') {
split(line, v, ' ');
int label = atoi(v[2].c_str());
if (label > largest_label)
largest_label = label;
unique_label_set.insert(label);
}
if (line.at(0) == 'e')
count_edge_number++;
}
cnt_unique_label = unique_label_set.size();
label_freqency = new int [ cnt_unique_label + 1 ];
memset(label_freqency, 0, sizeof(int) * (cnt_unique_label + 1));
}
vector<vector<int> > nodes;
nodes.resize( cnt_node );
// used new label to node index with the degree sensitive
//initilize the array to all 0;
transferredLabel = new int[largest_label + 1];
memset(transferredLabel, 0, sizeof(int) * (largest_label + 1));
int label_index = 1; //start from 1, because 0 means not found the label
ifstream fin(datagraphFile);
string line;
nodes_info = new int[cnt_node + 1];
nodes_to_label_info = new pair<int, int> [cnt_node * (cnt_unique_label + 1)];
int node_id = 0;
vector<string> v;
while (getline(fin, line)) {
if (line.at(0) == 'v') { //this is node
split(line, v, ' ');
int label = atoi(v[2].c_str()); // get node label
int actual_label = transferredLabel[label]; // transfer the label to the actual label we can use
if (actual_label == 0) { //means this label is first time being transfer
actual_label = label_index;
transferredLabel[label] = label_index;
label_index++; // maintain the label index. notice that 0 means not found here, therefore 0 is not a valid label
}
//nodes_label.push_back(actual_label); // now that we use the transferred actual label
nodes_label[node_id] = actual_label;
node_id++;
label_freqency[actual_label] ++;
}
if (line.at(0) == 'e') {
split(line, v, ' ');
int left_node = atoi(v[1].c_str());
int right_node = atoi(v[2].c_str());
if (left_node != right_node){ //put the nodes into the adjacent list
nodes[left_node].push_back(right_node);
nodes[right_node].push_back(left_node);
}
}
}
//====== build the degree vector ===============
core_number_data = new int [cnt_node];
// degree_data.resize(cnt_node);
degree_data = new int[cnt_node];
MAX_DEGREE = 0;
int node_degree_largest;
for (int i = 0; i < cnt_node; i++) {
degree_data[i] = nodes[i].size();
core_number_data[i] = nodes[i].size();
if (degree_data[i] > MAX_DEGREE) {
MAX_DEGREE = degree_data[i];
node_degree_largest = i;
}
}
//==============================================
int sum_degree_one = 0;
int sum_degree_zero = 0;
int sum_degree = 0;
for (int i = 0; i < cnt_node; i++) {
int degree = nodes[i].size();
if (degree == 1)
sum_degree_one ++;
if (degree == 0)
sum_degree_zero ++;
sum_degree += degree;
}
AVERAGE_DEGREE_DATA_SUM += (double) sum_degree / cnt_node;
cout << "Data graph info ******* " << datagraphFile << " ********" << endl;
cout << "Data graph nodes: " << cnt_node << endl;
cout << "Data graph edges: " << count_edge_number << endl;
cout << "Largest label is " << largest_label << endl;
cout << "Unique labels => " << cnt_unique_label << endl;
cout << "Average degree is " << AVERAGE_DEGREE_DATA_SUM << endl;
cout << "Largest degree is " << MAX_DEGREE << endl;
cout << "Leaf node number: " << sum_degree_one << endl;
cout << "Isolated nodes : " << sum_degree_zero << endl << endl;
}
inline void coreDecomposition_data(){
//core-decomposition for the data graph
//begin starting the core-decomposition, core number is the degree number
int* bin = new int[MAX_DEGREE + 1];
memset(bin, 0, sizeof(int) * ( MAX_DEGREE + 1) );
for (int i = 0; i < cnt_node; i ++)
bin[ core_number_data [i] ] ++;
int start = 0;
int num;
for (int d = 0; d <= MAX_DEGREE; d++){
num = bin[d];
bin[d] = start;
start += num;
}
int* pos = new int [cnt_node];
int* vert = new int [cnt_node];
for (int i = 0; i < cnt_node; i++){
pos[i] = bin[ core_number_data[i] ];
vert[ pos[i] ] = i;
bin[ core_number_data[i] ] ++;
}
for (int d = MAX_DEGREE; d > 0; d --)
bin[d] = bin[d-1];
bin[0] = 0;
for (int i = 0; i < cnt_node; i++){
int v = vert[i];
for (int j = nodes_info[v]; j < nodes_info[v + 1]; j++){
int u = nodes_data[j];
if (core_number_data[u] > core_number_data[v]){
int du = core_number_data[u];
int pu = pos[u];
int pw = bin[du];
int w = vert[pw];
if (u != w){ //if not the same node, switch the position of the two nodes.
pos[u] = pw;
pos[w] = pu;
vert[pu] = w;
vert[pw] = u;
}
bin[du] ++;
core_number_data[u]--;
}
}
}
delete[] bin;
delete[] pos;
delete[] vert;
}
//=========================================================================================
inline void readQueryGraph() {
int m;
int matrix_index = 0;
MAX_DEGREE_QUERY = 0;
//resize only if required
if (sum_degree_cur > MAX_sum_degree_cur){
MAX_sum_degree_cur = sum_degree_cur;
query_nodes_array.resize(sum_degree_cur * 2);
core_tree_node_child_array.resize(sum_degree_cur);
core_tree_node_nte_array.resize(sum_degree_cur);
}
for (int i = 0; i < cnt_node_query; i++){
query_nodes_array_info[i] = matrix_index;
fin_query >> m >> label_cur >> degree_cur;
if (degree_cur > MAX_DEGREE_QUERY)
MAX_DEGREE_QUERY = degree_cur;
nodes_label_query[i] = transferredLabel[label_cur];
node_degree_query[i] = degree_cur;
core_number_query[i] = degree_cur;
for (int j = 0; j < degree_cur; j ++){
fin_query >> query_nodes_array[matrix_index];
query_neighbor_index[i][query_nodes_array[matrix_index]] = j;
matrix_index ++;
}
}
query_nodes_array_info[cnt_node_query] = matrix_index;
}
inline void query_analysis() {
cout << "========= Analyzing query set: " << querygraphFileFolder << " ==============="<< endl;
fin_query.open(querygraphFileFolder);
char c;
int m;
double sum_average_degree = 0;
double sum_max_degree = 0;
double max_average_degree = 0;
double max_max_degree = 0;
double sum_node_number = 0;
for (int i = 0; i < count_query_file; i++) {
fin_query >> c >> m >> cnt_node_query >> sum_degree_cur;
double average_degree = (double) sum_degree_cur / (double) cnt_node_query;
sum_node_number += cnt_node_query;
sum_average_degree += average_degree;
if (max_average_degree < average_degree)
max_average_degree = average_degree;
double max_degree = 0;
for (int j = 0; j < cnt_node_query; j++) {
fin_query >> m >> label_cur >> degree_cur;
if (degree_cur > max_degree)
max_degree = degree_cur;
for (int x = 0; x < degree_cur; x ++)
fin_query >> m;
}
sum_max_degree += max_degree;
if (max_max_degree < max_degree)
max_max_degree = max_degree;
}
fin_query.close();
cout << "Average number of nodes per query is " << sum_node_number / (double)count_query_file << endl;
cout << "Average degree of all query is " << sum_average_degree / (double)count_query_file << endl;
cout << "Average max degree of all query is " << sum_max_degree / (double) count_query_file << endl;
cout << "Max average degree of all query is " << max_average_degree << endl;
cout << "Max max degree of all query is " << max_max_degree << endl;
cout << endl;
}
inline void coreDecomposition_query(){
//begin starting the core-decomposition, core number is the degree number
int * bin = bin_query; // int bin [MAX_DEGREE_QUERY + 1];
int * pos = pos_query; // int pos [cnt_node_query];
int * vert = vert_query;// int vert [cnt_node_query];
memset(bin, 0, sizeof(int) * ( MAX_DEGREE_QUERY + 1) );
for (int i = 0; i < cnt_node_query; i ++)
bin[ core_number_query [i] ] ++;
int start = 0;
int num;
for (int d = 0; d <= MAX_DEGREE_QUERY; d++){
num = bin[d];
bin[d] = start;
start += num;
}
for (int i = 0; i < cnt_node_query; i++){
pos[i] = bin[ core_number_query[i] ];
vert[ pos[i] ] = i;
bin[ core_number_query[i] ] ++;
}
for (int d = MAX_DEGREE_QUERY; d > 0; d --)
bin[d] = bin[d-1];
bin[0] = 0;
for (int i = 0; i < cnt_node_query; i++){
int v = vert[i];
for (int j = query_nodes_array_info[v]; j < query_nodes_array_info[v + 1]; j ++){
int u = query_nodes_array[j]; // nodes_query[v][j];
if (core_number_query[u] > core_number_query[v]){
int du = core_number_query[u];
int pu = pos[u];
int pw = bin[du];
int w = vert[pw];
if (u != w){ //if not the same node, switch the position of the two nodes.
pos[u] = pw;
pos[w] = pu;
vert[pu] = w;
vert[pw] = u;
}
bin[du] ++;
core_number_query[u]--;
}
}
}
}
//======================================FUNCTIONS EXCLUSIVELY FOR NORMAL INPUT ====================================================
inline void constructNECMapping(int a_node)
{
//for all of node i's children
for (int j = query_nodes_array_info[a_node]; j < query_nodes_array_info[a_node + 1]; j ++){
int child = query_nodes_array[j];
if (core_number_query[child] < 2){ // the child node is not in the 2-core
//two cases here, the NEC node or a residual tree
if (node_degree_query[child] == 1){ //degree is one ==> NEC node
//============ CASE ONE: ONE-DEGREE NODES => NEC nodes =====================
int label = nodes_label_query[child];
if (NEC_mapping[label * MAX_QUERY_NODE + a_node] == 0) {
NEC_mapping_pair[NEC_mapping_pair_index ++] = NEC_element(label, a_node, child);// child is the representative node
NEC_map [child] = child;//NEC map
NEC_mapping_Actual[label * MAX_QUERY_NODE + a_node] = child;
} else {
NEC_map [child] = NEC_mapping_Actual[label * MAX_QUERY_NODE + a_node];//NEC map
}
NEC_mapping[label * MAX_QUERY_NODE + a_node] ++; // the label with parent being i, nec_count ++
} else {
//============ CASE TWO: NORMAL CASE, THE QUERY TREE ================
// extract the query tree for extra region candidate extraction, based on DFS
// also give a DFS-based query sequence at the same time
int * dfs_stack = dfs_stack_query;
int dfs_stack_index = 0;
visited_for_query[a_node] = 1; // this is the start node's parent node (a marked node)
visited_for_query[child] = 1; // this is the start node
dfs_stack[dfs_stack_index ++] = child;
residual_tree_match_seq[residual_tree_match_seq_index ++] = child;
tree_node_parent[child] = a_node;
while (dfs_stack_index != 0) {
int current_node = dfs_stack[dfs_stack_index - 1];
dfs_stack_index--;
int added_child = 0;
for (int m = query_nodes_array_info[current_node]; m < query_nodes_array_info[current_node + 1]; m ++){
int child_node = query_nodes_array[m];
if (!visited_for_query[child_node]) {
visited_for_query[child_node] = 1;
//======== special treatment here: if a node is a leaf (degree being 1), then put it into nec node set
if (node_degree_query[child_node] == 1){
int label = nodes_label_query[child_node];
if (NEC_mapping[label * MAX_QUERY_NODE + current_node] == 0) {
NEC_mapping_pair[NEC_mapping_pair_index ++] = NEC_element(label, current_node, child_node);// child is the repesentive node
NEC_map [child_node] = child_node;//NEC map
NEC_mapping_Actual[label * MAX_QUERY_NODE + current_node] = child_node;
}
else{
NEC_map [child_node] = NEC_mapping_Actual[label * MAX_QUERY_NODE + current_node];//NEC map
}
NEC_mapping[label * MAX_QUERY_NODE + current_node]++; // the label with parent being i, nec_count ++
continue;
}
//===========================================================
tree_node_parent[child_node] = current_node;
added_child ++;
dfs_stack[dfs_stack_index ++] = child_node;
residual_tree_match_seq[residual_tree_match_seq_index ++] = child_node;
}
if (added_child == 0)//this information is recorded for extracting the matching sequence for the tree matching sequence.
residual_tree_leaf_node[residual_tree_leaf_node_index ++] = make_pair(current_node, 0);
}
}
}
}
}
}
inline void extractResidualStructures(){
residual_tree_match_seq_index = 0;
residual_tree_leaf_node_index = 0;
NEC_mapping_pair_index = 0;
memset(NEC_map, -1, sizeof(int) * cnt_node_query );
memset(visited_for_query, 0, sizeof(char) * cnt_node_query);
if(isTree)
{
//exit(1);
//constructNECMapping(root_node_id);
}
else
{
for (int i = 0; i < cnt_node_query; i++) {//for each node in the query
if (core_number_query[i] < 2)//not in the two-core
continue;
//now i must be a 2-core node => next, we check whether i is a articulation node
//constructNECMapping(i);
}
}
//================ construct the NEC set by label: each label is with a vector which contains many NECs with this label.=========
sort(NEC_mapping_pair, NEC_mapping_pair + NEC_mapping_pair_index, sort_by_NEC_label);
int last_label;
NEC_set_index = 0;
NEC_set_by_label_index.clear();
int sum;
if (NEC_mapping_pair_index == 1){
NEC_element & nec_ele = NEC_mapping_pair[0];
int label = nec_ele.label;
int parent_id = nec_ele.parent_id;
int represent_child = nec_ele.represent_node;
sum = NEC_mapping[label * MAX_QUERY_NODE + parent_id];
NEC_mapping[label * MAX_QUERY_NODE + parent_id] = 0; //reset it back to 0
NEC_set_by_label_index.push_back(make_pair(label, NEC_set_index));
NEC_set_array[NEC_set_index ++] = NEC_set_array_element(parent_id, represent_child, sum);
NEC_set_by_label_index.push_back(make_pair(-1, NEC_mapping_pair_index)); // redundant element to set the end
} else {
for (int i = 0; i < NEC_mapping_pair_index; i++) {
NEC_element & nec_ele = NEC_mapping_pair[i];
int label = nec_ele.label;
int parent_id = nec_ele.parent_id;
int represent_child = nec_ele.represent_node;
sum = NEC_mapping[label * MAX_QUERY_NODE + parent_id];
NEC_mapping[label * MAX_QUERY_NODE + parent_id] = 0; //reset it back to 0
if (i == 0) {
NEC_set_by_label_index.push_back(make_pair(label, NEC_set_index));
NEC_set_array[NEC_set_index ++] = NEC_set_array_element(parent_id, represent_child, sum);
last_label = label;
continue;
} else if (i == NEC_mapping_pair_index - 1) {
if (label != last_label)
NEC_set_by_label_index.push_back(make_pair(label, NEC_set_index));
NEC_set_array[NEC_set_index ++] = NEC_set_array_element(parent_id, represent_child, sum);
NEC_set_by_label_index.push_back(make_pair(-1, NEC_mapping_pair_index)); // redunant element to set the end
continue;
}
if (label != last_label) {
NEC_set_by_label_index.push_back(make_pair(label, NEC_set_index));
last_label = label;
}
NEC_set_array[NEC_set_index ++] = NEC_set_array_element(parent_id, represent_child, sum);
}
}
#ifdef OUTPUT_EXTRA_INFO
int sum_node = 0;
if (NEC_mapping_pair_index != 0){
for (int i = 0; i < NEC_set_by_label_index.size() - 1; i++) {
int label = NEC_set_by_label_index[i].first;
int start = NEC_set_by_label_index[i].second;
int end = NEC_set_by_label_index[i + 1].second;
for (int j = start; j < end; j++) {
int parent_id = NEC_set_array[j].parent_id;
int sum = NEC_set_array[j].sum;
sum_node += sum;
cerr << "label :" << label << " => parent id " << parent_id << " \t sum => " << sum
<< "\t representative node is " << NEC_set_array[j].represent_node<< endl;
}
}
}
cerr << "NEC classes contained: " << NEC_mapping_pair_index << " classes with " << sum_node << " nodes " << endl;
cerr << "Query trees with sum node: " << residual_tree_match_seq_index
<< " and tree leaf index is " << residual_tree_leaf_node_index << endl;
if(isTree)
{
cerr << "Nodes in tree: ";
for (int i = 0; i < residual_tree_match_seq_index; i++){
cerr << residual_tree_match_seq[i] << " ";
}
cerr << endl;
}
#endif
}
inline int start_node_selection(){
double least_ranking = DBL_MAX;
int start_node = -1;
double ranking;
int label;
int degree;
for (int i = 0; i < cnt_node_query; i++){
if (core_number_query[i] < 2 && isTree == false) //root node must be selected from the core structure
continue;
label = nodes_label_query[i];
degree = node_degree_query[i];
//binary search used here
int s = label_deg_label_pos[ label - 1 ].second;
int end = label_deg_label_pos[ label ].second;
vector<int>::iterator pos = lower_bound( degree_array.begin() + s , degree_array.begin() + end, degree);
int start = pos - degree_array.begin();
ranking = (double)(end - start) /(double)degree ;
if (ranking < least_ranking){
least_ranking = ranking;
start_node = i;
}
}
return start_node;
}
inline void BFS_DAG() {
/*
* output : true_leaf_nodes, simulation_sequence_array, level_to_sequence which maps a level to a segment in the sequence
*/
core_tree_node_child_array_index = 0;
core_tree_node_nte_array_index = 0;
bfs_sequence_index = 0; // 20170503
exploreCRSequence_indx = 0;
char * popped = visited_for_query; //popped[i] = 1 if i-node have popped from queue.
memset(popped, 0, sizeof(char) * cnt_node_query);
int * visited = visited_int_for_query; //visited[i] = level if i-node had pushed into queue, where level is it's BFS-level.
memset(visited, 0, sizeof(int) * cnt_node_query);
int * queue_array = queue_array_query;
queue_array[0] = root_node_id;
int pointer_this_start = 0;
int pointer_this_end = 1;
int pointer_next_end = 1;
int current_level = 1; //initial level starts at 1
simulation_sequence_index = 0;
level_index.clear();
visited[root_node_id] = 1;
BFS_level_query[root_node_id] = 1;
BFS_parent_query[root_node_id] = -1;
if(use_failing_set)
{
if(cnt_node_query != prev_cnt_node_query){
zero_vector = bit_vector(cnt_node_query, 0);
one_vector = bit_vector(cnt_node_query, 1);
prev_cnt_node_query = cnt_node_query;
}
}
for(int i = 0; i < cnt_node_query; i++)
{
DAG_child_query[i] = new int[node_degree_query[i]];
memset(DAG_child_query[i], -1, sizeof(int) * node_degree_query[i] );
DAG_parent_query[i] = new int[node_degree_query[i]];
memset(DAG_parent_query[i], -1, sizeof(int) * node_degree_query[i] );
DAG_child_query_parent_index[i] = new int[node_degree_query[i]];
memset(DAG_child_query_parent_index[i], -1, sizeof(int) * node_degree_query[i] );
//DAG_ancestor[i] = bit_vector(cnt_node_query, 0);
}
memset(DAG_child_query_size, 0, sizeof(int) * cnt_node_query);
memset(DAG_parent_query_size, 0, sizeof(int) * cnt_node_query);
//<sort by label_freq first, and sort by degree for each label group>
//sort label by label frequency
label_frequency_rank = new int [ cnt_unique_label + 1 ]; //later, these lines should be moved to preprocessData...()
int* temp_sort_array = new int [ cnt_unique_label + 1 ];
for(int i = 1; i < cnt_unique_label+1; i++) temp_sort_array[i] = i; //label uses [1-cnt_unique_label+1]
//label_frequency_rank[i] contains label i's rank in terms of the label_frequency. high rank has high frequency; i.e., label_frequency_rank[0] has the biggest label_frequency.
sort(temp_sort_array+1, temp_sort_array + cnt_unique_label+1, sortByLabelFrequency_Label_inc);
for(int i =1; i < cnt_unique_label+1; i++) label_frequency_rank[ temp_sort_array[i] ] = i;
//<\sort by label_freq first, and sort by degree for each label group>
while (true) {
int start = simulation_sequence_index;
//<sort by label_freq first, and sort by degree for each label group>
sort(queue_array+pointer_this_start, queue_array+pointer_this_end, sortByDegree_Query_dec);
//by sorting the array using label_frequency_rank, where distinct label has distinct rank, and then only the array can be grouped by labels.
stable_sort(queue_array+pointer_this_start, queue_array+pointer_this_end, sortByLabelFrequencyRank_Query_inc);
//<\sort by label_freq first, and sort by degree for each label group>
while (pointer_this_start != pointer_this_end) { // queue not empty
int current_node = queue_array[pointer_this_start];
pointer_this_start++;
popped[current_node] = 1;
int start = query_nodes_array_info[current_node];
int end = query_nodes_array_info[current_node + 1];
//cout<<"BFS_DAG. current_node: "<<current_node<<", (start, end): ("<<start<<", "<<end<<")"<<endl;
for (int i = start; i < end; i ++){
int childNode = query_nodes_array[i];
//cout<<"childNode "<<i<<": "<<childNode<<endl;
if (popped[childNode] == 0) //childNode is not current_node's parent
{
DAG_child_query[current_node][ DAG_child_query_size[current_node] ] = childNode;
DAG_child_query_parent_index[current_node][ DAG_child_query_size[current_node] ] = DAG_parent_query_size[childNode];
DAG_parent_query[childNode][ DAG_parent_query_size[childNode] ] = current_node;
//DAG_ancestor[childNode][current_node] = 1; //base case
//DAG_ancestor[childNode] |= DAG_ancestor[current_node]; //propagate
DAG_child_query_size[current_node]++;
DAG_parent_query_size[childNode]++;
}
if (visited[childNode] == 0) //this child node has not been visited.
{
visited[childNode] = current_level + 1; //parent node's level plus one
queue_array[pointer_next_end] = childNode;
pointer_next_end++;
BFS_level_query[childNode] = current_level + 1;
BFS_parent_query[childNode] = current_node;
if (core_number_query[childNode] < 2)