-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathKARPET.cc
2150 lines (1882 loc) · 99.8 KB
/
KARPET.cc
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 "global.h"
#include "proto.h"
#include "pruned_landmark_labeling.h"
using namespace std;
PQEntity_AStar createPQEntity_AStar(int id, float wgt, float key, std::vector<int> path){
PQEntity_AStar entity;
entity.nodeIdx = id;//node id
entity.wgt = wgt;//weight of path from src to current node.
entity.key = key;
entity.path = path;//node ids of current path. need to change if we need to output the path itself.
return entity;
}
PQEntity_AStar_Tree createPQEntity_AStar_Tree(int id, int id_inpattern, float wgt, float key, Instance_Tree subtree, unordered_map<int, int> subtree_vertex){
PQEntity_AStar_Tree entity;
//subtree_vertex[id_inpattern]= id;
entity.node2vertex = subtree_vertex;
entity.node2vertex[id_inpattern] = id;
entity.nodeIdx = id;//current node id
entity.curId_inpattern = id_inpattern;
entity.wgt = wgt;//weight of subtree
entity.key = key;
entity.subtree = subtree;//node ids of current path -> now turned into subtree (visited part from root).
return entity;
}
//Vertex current id is in the subtree expanded so far. And now we are inserting neighbor vertex. The boolean on_left captures whether ot not the neighbor is a left child
Instance_Tree Instance_Tree_Insert(Instance_Tree subtree, int curId, int neigh,float edgewgt,bool on_left){
Instance_Tree result = subtree;
if (on_left==true){
result.map2leftcdr[curId] = neigh;
result.map2parent[neigh] = curId;
result.nodes.insert(neigh);
}
else{
result.map2rightcdr[curId] = neigh;
result.map2parent[neigh] = curId;
result.nodes.insert(neigh);
}
result.wgt += edgewgt;
return result;
}
Instance_Tree_rep Instance_Tree_Insert_rep(Instance_Tree_rep subtree, int curId, int neigh,float edgewgt,bool on_left){
if (on_left==true){
subtree.map2leftcdr[curId] = neigh;
subtree.map2parent[neigh] = curId;
subtree.nodes.insert(neigh);
}
else{
subtree.map2rightcdr[curId] = neigh;
subtree.map2parent[neigh] = curId;
subtree.nodes.insert(neigh);
}
subtree.wgt += edgewgt;
return subtree;
}
QueryResult AStar_Prophet(const graph_t& g, Query query, double& timeUsed){
int maxDepth = query.pattern.size()-1;
float minWgt = MAX_WEIGHT;
QueryResult qResult;
if( (g.typeMap[query.src]!=query.pattern[0]) || (g.typeMap[query.tgt]!=query.pattern[maxDepth]) ){
cout << query.src << " to " << query.tgt << endl;
cout << g.typeMap[query.src] << " and " << g.typeMap[query.tgt] << endl;
cout<< "src or tgt node does not follow pattern!" << endl;
return qResult;
}
int midLayer = query.pattern.size()/2 +1;//half forward and halp backward. And they meet and terminate at the same layer.
vector<unordered_map<int, float> > layers = create_Prophet_Heuristics(g, query, timeUsed);//layers stores the legitimate nodes on each level. Each layer is a set.
std::priority_queue<PQEntity_AStar, std::vector<PQEntity_AStar>, comparator_AStar> frontier;
PQEntity_AStar curNode;
vector<int> tmpPath;
tmpPath.push_back(query.src);
frontier.push(createPQEntity_AStar(query.src, 0, 0, tmpPath));//frontier is the priority queue.
int mem = 0, total = 1;
while(!frontier.empty()){
mem = max(mem, (int)frontier.size());
PQEntity_AStar curNode;
curNode = frontier.top();
frontier.pop();
int curId = curNode.nodeIdx;
int depth = curNode.path.size()-1;//start from 0.
vector<int> path = curNode.path;
if(depth==maxDepth){//reach the end of pattern.
if(curId == query.tgt && curNode.wgt<MAX_WEIGHT){
if(qResult.paths.size()<TOP_K)
qResult.paths.push_back(createPath(curNode.wgt, path));
if(qResult.paths.size()==TOP_K)
break;
}
continue;
}
//expanding the neighbors of current node.
for(int i=0; i<g.degree[curId]; i++){
int neigh = g.neighbors[g.nodes[curId]+i];//neighbor id.
//if neighbor is not in the path and follow the pattern.
//cout << depth << endl; cout << layers.size() << endl;
unordered_map<int, float>::iterator found = layers[depth+1].find(neigh);
float edgwgt = calcWgt(g.wgts[g.nodes[curId]+i], query.time);
if( found != layers[depth+1].end() && find(path.begin(), path.end(), neigh) == path.end() && edgwgt+curNode.wgt<MAX_WEIGHT){
vector<int> newPath = path;
newPath.push_back(neigh);
frontier.push(createPQEntity_AStar(neigh, edgwgt+curNode.wgt, edgwgt+curNode.wgt+found->second, newPath));
total += 1;
}
}
}
//qResult.mem = mem;
// qResult.totalPaths = total;
return qResult;
}
//WWW version of expand current PET. return true if it is front element, else not.
//Execute the expansion and insert non-front element to priority queue.
bool Expand_current_v2(const graph_t& g, Query_tree querytree, vector <int> pre_order_patterns, int& curId, PQEntity_AStar_Tree& curNode,
Instance_Tree subtree, int& total,unordered_map<int, unordered_map<int, tuple<float, float>>> node2layers, int curId_inpattern,
std::priority_queue<PQEntity_AStar_Tree, std::vector<PQEntity_AStar_Tree>, comparator_AStar_Tree>& frontier, int& numTrees, unordered_map<int, unordered_map<int, unordered_map<int, float>>> & candidxleft, unordered_map<int, unordered_map<int, unordered_map<int, float>>> & candidxright){
//each expanding operation may change: total, frontier, curId, curNode. other variables wont change.
bool on_left = true;
float old_key = curNode.key; //use this to compare with the new key later to decide it front-optimization.
bool front_found = false;
curId = curNode.nodeIdx; //must update! otherwise wrong for front element.
PQEntity_AStar_Tree Top_element;
if (find(querytree.junctions.begin(),querytree.junctions.end(), curId_inpattern) == querytree.junctions.end()){
//curId is not a junction
if (find(querytree.terminals.begin(),querytree.terminals.end(), curId_inpattern) != querytree.terminals.end()){
//--curId is a terminal
if (curId_inpattern == querytree.terminals.back()){ //it is the last terminal
return 0;
}
int next_id_pattern = *(find(pre_order_patterns.begin(), pre_order_patterns.end(), curId_inpattern)+1); //curId_impattern's next one in pre_order_patterns, going to traverse him!
//old parent is a VERTEX id
int old_parent_inpattern = querytree.map2parent[next_id_pattern];
int old_parent = curNode.node2vertex[old_parent_inpattern];
cout<<"old parent is" <<old_parent<<endl;
on_left = false; //right neighbor.
unordered_map<int, float> child_wgt = candidxright[old_parent_inpattern][old_parent];
for(auto it = child_wgt.begin(); it!= child_wgt.end(); it++){
int neigh = it->first;
float edgwgt = it->second;
subtree = curNode.subtree;
if(find(subtree.nodes.begin(), subtree.nodes.end(),neigh)== subtree.nodes.end()){
Instance_Tree new_subtree = Instance_Tree_Insert(subtree, old_parent, neigh, edgwgt, on_left);
float traversed_wgt = edgwgt+subtree.wgt;
unordered_map<int, tuple<float,float>>::iterator found = node2layers[next_id_pattern].find(neigh);
assert (found != node2layers[next_id_pattern].end() );
float leftvalue = get<0>(node2layers[old_parent_inpattern][old_parent]);
float rightvalue = get<1>(node2layers[old_parent_inpattern][old_parent]);
float key = old_key - rightvalue + (edgwgt + std::get<0>(found->second) + std::get<1>(found->second));
unordered_map<int, int> new_node2vertex = curNode.node2vertex;
new_node2vertex[next_id_pattern]= neigh;
if (!front_found && (key == old_key)){ //this is a front element! the first fond wont be inserted into PQ.
cout << "front element optimization"<<endl;
front_found = true;
Top_element = createPQEntity_AStar_Tree
(neigh, next_id_pattern, traversed_wgt, key, new_subtree, new_node2vertex);
}
else{ //not a front element
frontier.push(createPQEntity_AStar_Tree
(neigh, next_id_pattern, traversed_wgt, key, new_subtree, new_node2vertex));
total += 1;
}
}
}
}
else{ //curId is not a terminal, not a junction, just on path
int onlychild;
unordered_map<int, unordered_map<int, unordered_map<int, float>>> candidx;
if (querytree.map2leftcdr.find(curId_inpattern)!=querytree.map2leftcdr.end()){ //has a left child
onlychild = querytree.map2leftcdr[curId_inpattern];
on_left = true;
candidx = candidxleft;
}
else {
if(querytree.map2rightcdr.find(curId_inpattern)!=querytree.map2rightcdr.end()){
onlychild = querytree.map2rightcdr[curId_inpattern];
on_left = false;
candidx = candidxright;
}
else {
return 0;
}
}
unordered_map<int, float> child_wgt = candidx[curId_inpattern][curId];
for(auto it = child_wgt.begin(); it!= child_wgt.end(); it++){
int neigh = it->first;
float edgwgt = it->second;
subtree = curNode.subtree;
if(find(subtree.nodes.begin(), subtree.nodes.end(),neigh)== subtree.nodes.end()){
Instance_Tree new_subtree = Instance_Tree_Insert(subtree, curId, neigh, edgwgt, on_left);
float traversed_wgt = edgwgt+curNode.wgt;
unordered_map<int, tuple<float,float>>::iterator found = node2layers[onlychild].find(neigh);
float leftvalue = get<0>(node2layers[curId_inpattern][curId]);
float rightvalue = get<1>(node2layers[curId_inpattern][curId]);
float key;
if (on_left) key = old_key - leftvalue + (edgwgt + std::get<0>(found->second) + std::get<1>(found->second));
else key = old_key - rightvalue + (edgwgt + std::get<0>(found->second) + std::get<1>(found->second));
assert(key >= old_key - 0.001);
unordered_map<int, int> new_node2vertex = curNode.node2vertex;
new_node2vertex[onlychild]= neigh;
if (!front_found && (key == old_key)){ //this is a front element! the first fond wont be inserted into PQ.
cout << "front element optimization"<<endl;
front_found = true;
Top_element = (createPQEntity_AStar_Tree
(neigh, onlychild, traversed_wgt,key , new_subtree, new_node2vertex));
}
else{ //not a front element
frontier.push(createPQEntity_AStar_Tree
(neigh, onlychild, traversed_wgt,key , new_subtree, new_node2vertex));
total += 1;
}
}
}
}
}
else{ //curId is a junction.
//first time visiting a junction: found in left child candidates and right child candidate.
int leftchild, rightchild;
leftchild = querytree.map2leftcdr[curId_inpattern];
rightchild = querytree.map2rightcdr[curId_inpattern];
on_left = true;
//pushback with left child, update the right
unordered_map<int, float> child_wgt = candidxleft[curId_inpattern][curId];
for(auto it = child_wgt.begin(); it!= child_wgt.end(); it++){
int neigh = it->first;
float edgwgt = it->second;
subtree = curNode.subtree;
if(find(subtree.nodes.begin(), subtree.nodes.end(),neigh)== subtree.nodes.end()){
Instance_Tree new_subtree = Instance_Tree_Insert(subtree, curId, neigh, edgwgt, on_left);
float rightvalue = get<1>(node2layers[curId_inpattern][curId]);
float traversed_wgt = edgwgt+curNode.wgt;
//assert( new_subtree.wgt == traversed_wgt );
unordered_map<int, tuple<float,float>>::iterator found = node2layers[leftchild].find(neigh);
float leftvalue = get<0>(node2layers[curId_inpattern][curId]);
float key;
key = old_key - leftvalue + (edgwgt + std::get<0>(found->second) + std::get<1>(found->second)) ;
unordered_map<int, int> new_node2vertex = curNode.node2vertex;
new_node2vertex[leftchild]= neigh;
if (!front_found && (key == old_key)){ //this is a front element! the first fond wont be inserted into PQ.
front_found = true;
cout << "front element optimization"<<endl;
Top_element = (createPQEntity_AStar_Tree
(neigh, leftchild, traversed_wgt,key , new_subtree, new_node2vertex));
}
else{ //not a front element
frontier.push(createPQEntity_AStar_Tree
(neigh, leftchild, traversed_wgt,key , new_subtree, new_node2vertex));
total += 1;
}
}
}
}
if (front_found) curNode = Top_element;
return front_found;
}
std::vector<PQEntity_AStar_Tree> Expand_brute_v2(const graph_t& g, Query_tree querytree, vector <int> pre_order_patterns, int& curId, PQEntity_AStar_Tree& curNode,
Instance_Tree subtree, int& total,unordered_map<int, unordered_map<int, tuple<float, float>>> node2layers, int curId_inpattern,
int& numTrees, unordered_map<int, unordered_map<int, unordered_map<int, float>>> & candidxleft, unordered_map<int, unordered_map<int, unordered_map<int, float>>> & candidxright){
//each expanding operation may change: total, frontier, curId, curNode. other variables wont change.
std::vector<PQEntity_AStar_Tree> results;
bool on_left = true;
float old_key = curNode.key; //use this to compare with the new key later to decide it front-optimization.
bool front_found = false;
subtree = curNode.subtree;
if (find(querytree.junctions.begin(),querytree.junctions.end(), curId_inpattern) == querytree.junctions.end()){
//curId is not a junction
if (find(querytree.terminals.begin(),querytree.terminals.end(), curId_inpattern) != querytree.terminals.end()){
//--curId is a terminal
if (curId_inpattern == querytree.terminals.back()){ //it is the last terminal
return results;
}
int next_id_pattern = *(find(pre_order_patterns.begin(), pre_order_patterns.end(), curId_inpattern)+1); //curId_impattern's next one in pre_order_patterns, going to traverse him!
//old parent is a VERTEX id
int old_parent_inpattern = querytree.map2parent[next_id_pattern];
int old_parent = curNode.node2vertex[old_parent_inpattern];
on_left = false; //right neighbor.
unordered_map<int, float> child_wgt = candidxright[old_parent_inpattern][old_parent];
for(auto it = child_wgt.begin(); it!= child_wgt.end(); it++){
int neigh = it->first;
float edgwgt = it->second;
if(find(subtree.nodes.begin(), subtree.nodes.end(),neigh)== subtree.nodes.end()){
//check if vertex has been used!
Instance_Tree new_subtree = Instance_Tree_Insert(subtree, old_parent, neigh, edgwgt, on_left);
float traversed_wgt = edgwgt+curNode.wgt;
new_subtree.wgt = traversed_wgt;
unordered_map<int, tuple<float,float>>::iterator found = node2layers[next_id_pattern].find(neigh);
float key = 0;
unordered_map<int, int> new_node2vertex = curNode.node2vertex;
new_node2vertex[next_id_pattern]= neigh;
results.push_back(createPQEntity_AStar_Tree
(neigh, next_id_pattern, traversed_wgt, key, new_subtree, new_node2vertex));
total += 1;
}
}
}
else{ //curId is not a terminal, not a junction, just on path
int onlychild;
unordered_map<int, unordered_map<int, unordered_map<int, float>>> candidx;
if (querytree.map2leftcdr.find(curId_inpattern)!=querytree.map2leftcdr.end()){ //has a left child
onlychild = querytree.map2leftcdr[curId_inpattern];
on_left = true;
candidx = candidxleft;
}
else {
if(querytree.map2rightcdr.find(curId_inpattern)==querytree.map2rightcdr.end()){
onlychild = querytree.map2rightcdr[curId_inpattern];
on_left = false;
candidx = candidxright;
}
else {
return results;
}
}
unordered_map<int, float> child_wgt = candidx[curId_inpattern][curId];
for(auto it = child_wgt.begin(); it!= child_wgt.end(); it++){
int neigh = it->first;
float edgwgt = it->second;
if(find(subtree.nodes.begin(), subtree.nodes.end(),neigh)== subtree.nodes.end()){
Instance_Tree new_subtree = Instance_Tree_Insert(subtree, curId, neigh, edgwgt, on_left);
float traversed_wgt = edgwgt+curNode.wgt;
new_subtree.wgt = traversed_wgt;
unordered_map<int, tuple<float,float>>::iterator found = node2layers[onlychild].find(neigh);
float key = 0;
unordered_map<int, int> new_node2vertex = curNode.node2vertex;
new_node2vertex[onlychild]= neigh;
results.push_back(createPQEntity_AStar_Tree
(neigh, onlychild, traversed_wgt,key , new_subtree, new_node2vertex));
total += 1;
}
}
}
}
else{ //curId is a junction.
//first time visiting a junction: found in left child candidates and right child candidate.
int leftchild, rightchild;
leftchild = querytree.map2leftcdr[curId_inpattern];
rightchild = querytree.map2rightcdr[curId_inpattern];
on_left = true;
//pushback with left child, update the right
unordered_map<int, float> child_wgt = candidxleft[curId_inpattern][curId];
for(auto it = child_wgt.begin(); it!= child_wgt.end(); it++){
int neigh = it->first;
float edgwgt = it->second;
if(find(subtree.nodes.begin(), subtree.nodes.end(),neigh)== subtree.nodes.end()){
Instance_Tree new_subtree = Instance_Tree_Insert(subtree, curId, neigh, edgwgt, on_left);
float rightvalue = get<1>(node2layers[curId_inpattern][curId]);
float traversed_wgt = edgwgt+curNode.wgt;
new_subtree.wgt = traversed_wgt;
unordered_map<int, tuple<float,float>>::iterator found = node2layers[leftchild].find(neigh);
float key = 0;
unordered_map<int, int> new_node2vertex = curNode.node2vertex;
new_node2vertex[leftchild]= neigh;
//cout<< key<<"replace" <<old_key<<endl;
//assert(key >= old_key);
results.push_back(createPQEntity_AStar_Tree
(neigh, leftchild, traversed_wgt,key , new_subtree, new_node2vertex));
total += 1;
}
}
}
return results;
}
std::vector<PQEntity_AStar_Tree> Expand_backbone_v2(const graph_t& g, Query_tree querytree, vector <int> pre_order_patterns, int& curId, PQEntity_AStar_Tree& curNode,
Instance_Tree subtree, int& total,unordered_map<int, unordered_map<int, tuple<float, float>>> node2layers, int curId_inpattern,
int& numTrees, unordered_map<int, int> fixed_nodes,
unordered_map<int, unordered_map<int, unordered_map<int, float>>> & candidxleft, unordered_map<int, unordered_map<int, unordered_map<int, float>>> & candidxright){
//each expanding operation may change: total, frontier, curId, curNode. other variables wont change.
std::vector<PQEntity_AStar_Tree> results;
bool on_left = true;
float old_key = curNode.key; //use this to compare with the new key later to decide it front-optimization.
bool front_found = false;
subtree = curNode.subtree;
assert (curNode.nodeIdx == curId);
if (find(querytree.junctions.begin(),querytree.junctions.end(), curId_inpattern) == querytree.junctions.end()){
//curId is not a junction
if (find(querytree.terminals.begin(),querytree.terminals.end(), curId_inpattern) != querytree.terminals.end()){
//--curId is a terminal
if (curId_inpattern == querytree.terminals.back()){ //it is the last terminal
return results;
}
int next_id_pattern = *(find(pre_order_patterns.begin(), pre_order_patterns.end(), curId_inpattern)+1); //curId_impattern's next one in pre_order_patterns, going to traverse him!
//old parent is a VERTEX id
int old_parent_inpattern = querytree.map2parent[next_id_pattern];
int old_parent = curNode.node2vertex[old_parent_inpattern];
on_left = false; //right neighbor.
if (fixed_nodes.find(next_id_pattern)!= fixed_nodes.end()){
//this node that I wish to add is already in backbone!
//change curNode's id and id_inpattern, then return a vector with one element which is curNode.
int neigh = fixed_nodes[next_id_pattern];
Instance_Tree new_subtree = Instance_Tree_Insert(subtree, old_parent, neigh, 0, on_left); //weight included in path wgt.
unordered_map<int, int> new_node2vertex = curNode.node2vertex;
new_node2vertex[next_id_pattern]= neigh;
results.push_back(createPQEntity_AStar_Tree
(neigh, next_id_pattern, curNode.wgt, 0, new_subtree, new_node2vertex));
}
else{
unordered_map<int, float> child_wgt = candidxright[old_parent_inpattern][old_parent];
for(auto it = child_wgt.begin(); it!= child_wgt.end(); it++){
int neigh = it->first;
float edgwgt = it->second;
if(find(subtree.nodes.begin(), subtree.nodes.end(),neigh)== subtree.nodes.end()){
//check if vertex has been used!
Instance_Tree new_subtree = Instance_Tree_Insert(subtree, old_parent, neigh, edgwgt, on_left);
float traversed_wgt = edgwgt+curNode.wgt;
new_subtree.wgt = traversed_wgt;
unordered_map<int, tuple<float,float>>::iterator found = node2layers[next_id_pattern].find(neigh);
float key = 0;
unordered_map<int, int> new_node2vertex = curNode.node2vertex;
new_node2vertex[next_id_pattern]= neigh;
results.push_back(createPQEntity_AStar_Tree
(neigh, next_id_pattern, traversed_wgt, key, new_subtree, new_node2vertex));
total += 1;
}
}
}
}
else{ //curId is not a terminal, not a junction, just on path
int onlychild;
unordered_map<int, unordered_map<int, unordered_map<int, float>>> candidx;
if (querytree.map2leftcdr.find(curId_inpattern)!=querytree.map2leftcdr.end()){ //has a left child
onlychild = querytree.map2leftcdr[curId_inpattern];
on_left = true;
candidx = candidxleft;
}
else {
if(querytree.map2rightcdr.find(curId_inpattern)==querytree.map2rightcdr.end()){
onlychild = querytree.map2rightcdr[curId_inpattern];
on_left = false;
candidx = candidxright;
}
else {
return results;
}
}
if (fixed_nodes.find(onlychild)!= fixed_nodes.end()){
int neigh = fixed_nodes[onlychild];
Instance_Tree new_subtree = Instance_Tree_Insert(subtree, curId, neigh, 0, on_left); //weight included in path wgt.
unordered_map<int, int> new_node2vertex = curNode.node2vertex;
new_node2vertex[onlychild]= neigh;
results.push_back(createPQEntity_AStar_Tree
(neigh, onlychild, curNode.wgt, 0, new_subtree, new_node2vertex));
}
else{
unordered_map<int, float> child_wgt = candidx[curId_inpattern][curId];
for(auto it = child_wgt.begin(); it!= child_wgt.end(); it++){
int neigh = it->first;
float edgwgt = it->second;
if(find(subtree.nodes.begin(), subtree.nodes.end(),neigh)== subtree.nodes.end()){
Instance_Tree new_subtree = Instance_Tree_Insert(subtree, curId, neigh, edgwgt, on_left);
float traversed_wgt = edgwgt+curNode.wgt;
new_subtree.wgt = traversed_wgt;
unordered_map<int, tuple<float,float>>::iterator found = node2layers[onlychild].find(neigh);
float key = 0;
unordered_map<int, int> new_node2vertex = curNode.node2vertex;
new_node2vertex[onlychild]= neigh;
results.push_back(createPQEntity_AStar_Tree
(neigh, onlychild, traversed_wgt,key , new_subtree, new_node2vertex));
total += 1;
}
}
}
}
}
else{ //curId is a junction.
//first time visiting a junction: found in left child candidates and right child candidate.
int leftchild, rightchild;
leftchild = querytree.map2leftcdr[curId_inpattern];
rightchild = querytree.map2rightcdr[curId_inpattern];
on_left = true;
//pushback with left child, update the right
if (fixed_nodes.find(leftchild)!= fixed_nodes.end()){
int neigh = fixed_nodes[leftchild];
Instance_Tree new_subtree = Instance_Tree_Insert(subtree, curId, neigh, 0, on_left); //weight included in path wgt.
unordered_map<int, int> new_node2vertex = curNode.node2vertex;
new_node2vertex[leftchild]= neigh;
results.push_back(createPQEntity_AStar_Tree
(neigh, leftchild, curNode.wgt, 0, new_subtree, new_node2vertex));
}
else{
unordered_map<int, float> child_wgt = candidxleft[curId_inpattern][curId];
for(auto it = child_wgt.begin(); it!= child_wgt.end(); it++){
int neigh = it->first;
float edgwgt = it->second;
if(find(subtree.nodes.begin(), subtree.nodes.end(),neigh)== subtree.nodes.end()){
Instance_Tree new_subtree = Instance_Tree_Insert(subtree, curId, neigh, edgwgt, on_left);
float rightvalue = get<1>(node2layers[curId_inpattern][curId]);
float traversed_wgt = edgwgt+curNode.wgt;
new_subtree.wgt = traversed_wgt;
unordered_map<int, tuple<float,float>>::iterator found = node2layers[leftchild].find(neigh);
float key = 0;
unordered_map<int, int> new_node2vertex = curNode.node2vertex;
new_node2vertex[leftchild]= neigh;
//cout<< key<<"replace" <<old_key<<endl;
//assert(key >= old_key);
results.push_back(createPQEntity_AStar_Tree
(neigh, leftchild, traversed_wgt,key , new_subtree, new_node2vertex));
total += 1;
}
}
}
}
return results;
}
//insert all candidates of this_node , append it next to check_connection_node in the specified way.
vector<Instance_Tree> Set_insert(const graph_t& g, std::unordered_map<int, int> node2pattern, Instance_Tree Old_tree, int this_node, int check_connection_node, bool insert_parent, bool insert_left, unordered_map<int, int> vertex2node, unordered_map<int, unordered_map<int, float>> node2layers, int& numtrees){
int old_node; //the vertex from
int newnode_candidate;
unordered_set<int> nodes_insertion;
vector<Instance_Tree> new_trees;
int existing_vertex;
for (auto itr = Old_tree.nodes.begin(); itr != Old_tree.nodes.end(); ++itr){
// for existing_vertex in Old_tree.nodes:
existing_vertex = *itr;
if (vertex2node[existing_vertex] == check_connection_node){
old_node = existing_vertex;
break;
}
}
// for newnode_candidate in node2layers[this_node]:{ //rewrite in a way that calculate weight is easy...
for(int i=0; i<g.degree[old_node]; i++){ //for all neighbors of old_node, if any is in node2layers[this_node], that one is a new candidate
newnode_candidate = g.neighbors[g.nodes[old_node]+i];
if (g.typeMap[newnode_candidate] == node2pattern[this_node] && find(Old_tree.nodes.begin(), Old_tree.nodes.end(),newnode_candidate)== Old_tree.nodes.end()){
unordered_map<int, float>::iterator found = node2layers[this_node].find(newnode_candidate);
numtrees += node2layers[this_node].size();
//if newnode_candidate is in node2layers[this_node]
if (found!=node2layers[this_node].end()){
Instance_Tree new_tree;
if (insert_parent){
new_tree.map2parent[old_node] = newnode_candidate;
if (insert_left){ //check_connection is left child of this_node
new_tree.map2leftcdr[newnode_candidate] = old_node;
}
else{
new_tree.map2rightcdr[newnode_candidate] = old_node;
}
}
else{ //this_node is a child of old node
new_tree.map2parent[newnode_candidate] = old_node;
if (insert_left){
new_tree.map2leftcdr[old_node] = newnode_candidate;
}
else{
new_tree.map2rightcdr[old_node] = newnode_candidate;
}
}
nodes_insertion = Old_tree.nodes; //.insert(newnode_candidate);
nodes_insertion.insert(newnode_candidate);
new_tree.nodes = nodes_insertion;
new_tree.wgt = Old_tree.wgt + calcWgt(g.wgts[g.nodes[old_node]+i], 0);//no-time deterministic experiments in baseline, query time set as 0
if (new_tree.nodes.size()>Old_tree.nodes.size())
new_trees.push_back(new_tree);
}
}
}
return new_trees;
}
//grow the current instanse tree by one node, and return a list of expanded trees
//expand one node that is not the same as anything in imcomplete_tree or complete_instances
//dont have to check type since prophet graph has already give us the vertex2node and node2vertex with type constraints.
vector<Instance_Tree> expend_withcheck(const graph_t& g, unordered_map<int, int> vertex2node, unordered_map<int, unordered_map<int, float>> node2layers, Query_tree querytree, Instance_Tree incomplete_tree, int& numtrees){
vector<Instance_Tree> new_trees;
Instance_Tree new_tree;
bool connected = false; //fpr each check_connection_node
bool found = false; //for each this_node
bool insert_parent = false; //if this_node is parent of existing check_connection_node
bool insert_left = false; //can mean new inserted node is on the left, or can mean cexisting node is inserted node's left child
int curnode;
for (int i = 0; i< querytree.nodes_ordered.size(); i++){ //enumerate all placeholder in pattern as this node
int this_node = querytree.nodes_ordered[i];
bool this_node_inmapped = false;
for (auto itr = incomplete_tree.nodes.begin(); itr != incomplete_tree.nodes.end(); ++itr){
if(this_node == vertex2node[*itr]){
this_node_inmapped = true;
}
}
//if ( this_node not in incomplete_tree.mapped_nodes){ //a unfixed node: is it connected?
if (!this_node_inmapped){
int check_connection_node;
//for check_connection_node in incomplete_tree.mapped_nodes:
for (auto itr = incomplete_tree.nodes.begin(); itr != incomplete_tree.nodes.end(); ++itr){
check_connection_node = vertex2node[*itr];
connected = false; //for every checked node, see if it is connected
//if this_node is parent of check_connection_node{
if (querytree.map2parent.find(check_connection_node)!= querytree.map2parent.end()
&&querytree.map2parent[check_connection_node] == this_node){
connected = true;
insert_parent = true;
//if check_connection_node is leftchild of this_node{
if (querytree.map2leftcdr.find(this_node)!= querytree.map2leftcdr.end()
&&querytree.map2leftcdr[this_node] == check_connection_node){
insert_left = true;
break; //inner for loop
}
//if check_connection_node is rightchild of this_node{
if (querytree.map2rightcdr.find(this_node)!= querytree.map2rightcdr.end()
&&querytree.map2rightcdr[this_node] == check_connection_node){
insert_left = false;
break;
}
}
else {
//if this_node is leftchild of check_connection_node{
if (querytree.map2leftcdr.find(check_connection_node)!= querytree.map2leftcdr.end()
&&querytree.map2leftcdr[check_connection_node] == this_node){
connected = true;
insert_parent = false;
insert_left = true;
break;
}
//else if this_node is rightchild of check_connection_node{
else if (querytree.map2rightcdr.find(check_connection_node)!= querytree.map2rightcdr.end()
&&querytree.map2rightcdr[check_connection_node] == this_node){
connected = true;
insert_parent = false;
insert_left = false;
break;
}
}
//now we have checked this_node. if connected is true, it is connected to some and should insert next to check_connection_nodes
//if not connected, move to the next check_connection_node
}//end of inner for loop. here we have looked at all check_connection_node that exists.
if (connected){
new_trees = Set_insert(g, querytree.map2patthern, incomplete_tree, this_node, check_connection_node, insert_parent, insert_left, vertex2node, node2layers, numtrees);
found = true;
break;//outer loop break, do not check other this_node.
}
}
if (found) break;
}//here we have examined all this_node that is not there.
return new_trees;
}
//done
vector<Instance_Tree> expend_withoutcheck(const graph_t& g, unordered_map<int, int> vertex2node, unordered_map<int, unordered_map<int, float>> node2layers, Query_tree querytree, Instance_Tree incomplete_tree, int& numtrees){
vector<Instance_Tree> new_trees;
Instance_Tree new_tree;
bool connected = false; //fpr each check_connection_node
bool found = false; //for each this_node
bool insert_parent = false; //if this_node is parent of existing check_connection_node
bool insert_left = false; //can mean new inserted node is on the left, or can mean cexisting node is inserted node's left child
int curnode;
for (int i = 0; i< querytree.nodes_ordered.size(); i++){ //enumerate all placeholder in pattern as this node
int this_node = querytree.nodes_ordered[i];
bool this_node_inmapped = false; //always false without check!
if (!this_node_inmapped){
int check_connection_node;
for (auto itr = incomplete_tree.nodes.begin(); itr != incomplete_tree.nodes.end(); ++itr){
check_connection_node = vertex2node[*itr];
connected = false;
if (querytree.map2parent.find(check_connection_node)!= querytree.map2parent.end()
&&querytree.map2parent[check_connection_node] == this_node){
connected = true;
insert_parent = true;
if (querytree.map2leftcdr.find(this_node)!= querytree.map2leftcdr.end()
&&querytree.map2leftcdr[this_node] == check_connection_node){
insert_left = true;
break;
}
if (querytree.map2rightcdr.find(this_node)!= querytree.map2rightcdr.end()
&&querytree.map2rightcdr[this_node] == check_connection_node){
insert_left = false;
break;
}
}
else {
if (querytree.map2leftcdr.find(check_connection_node)!= querytree.map2leftcdr.end()
&&querytree.map2leftcdr[check_connection_node] == this_node){
connected = true;
insert_parent = false;
insert_left = true;
break;
}
else if (querytree.map2rightcdr.find(check_connection_node)!= querytree.map2rightcdr.end()
&&querytree.map2rightcdr[check_connection_node] == this_node){
connected = true;
insert_parent = false;
insert_left = false;
break;
}
}
}
if (connected){
new_trees = Set_insert(g, querytree.map2patthern, incomplete_tree, this_node, check_connection_node, insert_parent, insert_left, vertex2node, node2layers, numtrees);
found = true;
break;
}
}
if (found) break;
}
return new_trees;
}
//a comparison that takes a parameter and return an operator
struct compare_srtuct{
compare_srtuct(vector<PQEntity_AStar_Tree> complete_trees) {this -> complete_trees = complete_trees; }
bool operator() (int i, int j) {//both i and j are index that we want to compare weight on
return (complete_trees[i].wgt < complete_trees[j].wgt);
}
vector<PQEntity_AStar_Tree> complete_trees;
};
struct compare_entity{
compare_entity(vector<PQEntity_AStar_Tree> complete_trees) {this -> complete_trees = complete_trees; }
bool operator() (PQEntity_AStar_Tree tree_i, PQEntity_AStar_Tree tree_j) {//both i and j are index that we want to compare weight on
return (tree_i.wgt < tree_j.wgt);
}
vector<PQEntity_AStar_Tree> complete_trees;
};
//sort a list of matching trees and return top k lightest ones. TODO: only return top k.
vector<PQEntity_AStar_Tree> Top_k_weight(vector<PQEntity_AStar_Tree> complete_trees){
vector<PQEntity_AStar_Tree> Top_k_trees = complete_trees;
sort(Top_k_trees.begin(), Top_k_trees.end(), compare_entity(Top_k_trees));
return Top_k_trees;
}
//Take graph g and the query tree, change the vertices to node map, the mapback to the prophet mapping that discribes all that matches type
int typecheck_all(const graph_t& g, Query_tree querytree, unordered_map<int, int>& vertex2node, unordered_map<int, unordered_map<int, float>>& node2layers){
int iterationnum = querytree.patterns.size()-1;
float minWgt = MAX_WEIGHT;
for(int i=0; i<querytree.terminals_index.size(); i++){
if( (g.typeMap[querytree.nodes_ordered[querytree.terminals_index[i]]]!=querytree.patterns[querytree.terminals_index[i]])){
cout<< "src or tgt node does not follow pattern!" << endl;
return 1;
}
}
vector<GeneralizedQuery> decomposed_queries = decompo_Query_Tree(querytree);
unordered_map<int, float> junction_leftmap;
unordered_map<int, float> junction_rightmap;
//for each path query (obtained by decomposition), generate candidate prophet graph
for (int i=0; i<decomposed_queries.size(); i++){//process by the post-order
GeneralizedQuery current_query = decomposed_queries[i];
//If in the path being processed now, either source or target is a junction, return the previous iteration's node2layers to initialize the values
if (find(querytree.junctions.begin(), querytree.junctions.end(), current_query.srcs.begin()->first)!= querytree.junctions.end()){ //if src is a junction, get candidate layers from previous iteration
current_query.srcs = node2layers[current_query.srcs.begin()->first];
}
else{
if(find(querytree.junctions.begin(), querytree.junctions.end(), current_query.tgts.begin()->first)!= querytree.junctions.end()){ //tgt is a junction
current_query.tgts = node2layers[current_query.tgts.begin()->first];
}
}
vector<unordered_map<int, float> > layers;
unordered_map<int, float> current_junction_leftmap;
unordered_map<int, float> current_junction_rightmap;
//Calling path query function from Albert (which has been generalized here to handle a set of potentially matching vertices as source or target)
//It also computes the heuristic values in the process (value of a node2layer node key)
double temptime = 0.0;
layers = create_Prophet_Heuristics_generalized(g, current_query, temptime, current_junction_leftmap, current_junction_rightmap);//layers stores the legitimate nodes on each level. Each layer is a set.
for (int i=0; i< layers.size(); i++){
node2layers[current_query.nodes[i]] = layers[i]; //i-th node updated into the candidate set chart. using map--unordered map for now...
for (auto it = layers[i].begin(); it!=layers[i].end(); it++){
int vertex = it->first;
vertex2node[vertex] = current_query.nodes[i];
}
}
junction_leftmap.insert(current_junction_leftmap.begin(), current_junction_leftmap.end());
junction_rightmap.insert(current_junction_rightmap.begin(), current_junction_rightmap.end());
}
return 0;
}
//done
//Baseline 1: based on prophet graph, list all candidates and
QueryResultTrees Bruteforce(const graph_t& g, Query_tree querytree, double& timeUsed){
int mem = 0, totalTrees = 1, numTrees = 0;
vector<PQEntity_AStar_Tree> complete_trees;
vector<PQEntity_AStar_Tree> incompletetrees;
vector<PQEntity_AStar_Tree> modified_trees;
int iterationnum = querytree.patterns.size()-1;
float minWgt = MAX_WEIGHT;
QueryResultTrees result;
for(int i=0; i<querytree.terminals_index.size(); i++){
if((g.typeMap[querytree.nodes_ordered[querytree.terminals_index[i]]]!=querytree.patterns[querytree.terminals_index[i]])){
cout<< "src or tgt node does not follow pattern!" << endl;
return result;
}
}
unordered_map<int, unordered_map<int, unordered_map<int, float>>> candidxleft;
unordered_map<int, unordered_map<int, unordered_map<int, float>>> candidxright;
//map a node in pattern into vertices in input graph that maps to its left children, if have any.
unordered_map<int, unordered_map<int, tuple<float,float>>> node2vertices_hrtc = bottom_up_hrtc_compute(g, querytree, candidxleft, candidxright);
//Maps a node in pattern to vertices in input graph that potentially map to it, each comes with a tuple of (left heuristic, right heuristic).
cout<<candidxleft.size()<<endl;
cout<<candidxright.size()<<endl;
PQEntity_AStar_Tree curNode;
//Root is the last element of the post_order traversal of pattern tree
int root = querytree.nodes_ordered.back();
//for all vertices matching the root node, push them in priority queue
for(unordered_map<int, tuple<float, float>>::iterator vertex_hrtc=node2vertices_hrtc[root].begin(); vertex_hrtc!=node2vertices_hrtc[root].end(); vertex_hrtc++){
Instance_Tree tmptree;
tmptree.nodes.insert(vertex_hrtc->first);
tmptree.wgt = 0;
unordered_map<int, int> empty_node2vertex;
empty_node2vertex[root] = vertex_hrtc->first;
incompletetrees.push_back(createPQEntity_AStar_Tree(vertex_hrtc->first, root, 0, (std::get<0>(vertex_hrtc->second) + std::get<1>(vertex_hrtc->second)), tmptree, empty_node2vertex));//frontier is the priority queue. replace src with root.
//get<1> gives the left heuristic value, get<2> gives the right heuristic value.
numTrees ++;
}
//follow a pre-order to decide which is the next curId_inpattern the neighbor should match to, on which side, using this stack s.
stack <int> s;
int curId_inpattern = root;
vector <int> pre_order_patterns; //they are id of nodes, not type. NOTE
//Traversing query pattern in pre-order
while(true){
if (pre_order_patterns.size()>= querytree.nodes_ordered.size()) break;
while(true){
pre_order_patterns.push_back(curId_inpattern); //first time at one node.
cout<<"expended on current pattern is "<< curId_inpattern<< endl;
s.push(curId_inpattern);
if (querytree.map2leftcdr.find(curId_inpattern)!= querytree.map2leftcdr.end()){//current ppattern node still have left child;
curId_inpattern = querytree.map2leftcdr[curId_inpattern];
}
else//have no left child;
break;
}
if (s.empty())
break;
else{
while(true){
if (s.empty()) break; //reached the end of tree
curId_inpattern = s.top();
s.pop();
if (querytree.map2rightcdr.find(curId_inpattern)!= querytree.map2rightcdr.end()){
curId_inpattern = querytree.map2rightcdr[curId_inpattern];
break;
}
}
}
}
//Top-down traversal in pre-order
while(!incompletetrees.empty()){
if (incompletetrees.size() > mem) mem ++;
curNode = incompletetrees.back();
incompletetrees.pop_back();
int curId = curNode.nodeIdx;
int curId_inpattern = curNode.curId_inpattern;
Instance_Tree subtree = curNode.subtree;
modified_trees = Expand_brute_v2(g, querytree, pre_order_patterns, curId, curNode, subtree,totalTrees,node2vertices_hrtc, curId_inpattern, numTrees, candidxleft, candidxright);
totalTrees += modified_trees.size();
for (int i=0; i<modified_trees.size(); i++){
PQEntity_AStar_Tree modified_tree = modified_trees[i];
if(modified_tree.subtree.nodes.size() == querytree.nodes_ordered.size()){ //already complete after the growth
complete_trees.push_back(modified_tree);
}
else{
incompletetrees.push_back(modified_tree);
}
}
}
vector<PQEntity_AStar_Tree>TOPk_trees = Top_k_weight(complete_trees);
vector<Instance_Tree> trees;
for (int i = 0; i< TOPk_trees.size() && i < TOP_K; i++){
trees.push_back(TOPk_trees[i].subtree);
}
result.trees = trees;
//result.mem = mem;
result.totalTrees = totalTrees;
return result;
}
/////////////////END OF BASELINE 1/////////////////
//////////////////BASELINE 2////////////////////
//Baseline 2: decompose the tree into a longest path and feed paths to albert's algo
//from the longest path node to terminals, do sinple path matching.
////////baseline2 utils
//return one result that is not in found_paths
QueryResult AStar_Prophet_pop(const graph_t& g, Query query, double& timeUsed, vector<Path> found_paths){
Path cur_path;
int maxDepth = query.pattern.size()-1;
float minWgt = MAX_WEIGHT;
QueryResult qResult;
if( (g.typeMap[query.src]!=query.pattern[0]) || (g.typeMap[query.tgt]!=query.pattern[maxDepth]) ){
cout << query.src << " to " << query.tgt << endl;
cout << g.typeMap[query.src] << " and " << g.typeMap[query.tgt] << endl;