-
Notifications
You must be signed in to change notification settings - Fork 0
/
algo.h
1649 lines (1453 loc) · 58.1 KB
/
algo.h
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
//Contributors: Sibo Wang, Renchi Yang
#ifndef __ALGO_H__
#define __ALGO_H__
#include "graph.h"
#include "heap.h"
#include "config.h"
#include <tuple>
// #include "sfmt/SFMT.h"
#include <queue>
using namespace std;
struct PredResult {
double topk_avg_relative_err;
double topk_avg_abs_err;
double topk_recall;
double topk_precision;
int real_topk_source_count;
double topk_NDCG;
PredResult(double mae = 0, double mre = 0, double rec = 0, double pre = 0, int count = 0,double NDCG=0) :
topk_avg_relative_err(mae),
topk_avg_abs_err(mre),
topk_recall(rec),
topk_precision(pre),
real_topk_source_count(count),
topk_NDCG(NDCG){}
};
unordered_map<int, PredResult> pred_results;
Fwdidx fwd_idx;
Bwdidx bwd_idx;
iMap<double> ppr;
iMap<double> ppr_bi;
iMap<double> dht;
iMap<int> topk_filter;
// vector< boost::atomic<double> > vec_ppr;
iMap<int> rw_counter;
iMap<double> rw_bippr_counter;
// RwIdx rw_idx;
atomic<unsigned long long> num_hit_idx;
atomic<unsigned long long> num_total_rw;
atomic<unsigned long long> num_total_bi;
atomic<unsigned long long> num_total_fo;
long num_iter_topk;
vector<int> rw_idx;
vector<pair<unsigned long long, unsigned long> > rw_idx_info;
unordered_map<int, vector<int>> rw_index;
map<int, vector<pair<int, double> > > exact_topk_pprs;
map<int, vector<pair<int, double>>> exact_topk_dhts;
vector<pair<int, double> > topk_pprs;
vector<pair<int, double> > topk_dhts;
iMap<double> upper_bounds;
iMap<double> lower_bounds;
iMap<double> upper_bounds_self;
iMap<double> upper_bounds_self_init;
iMap<double> lower_bounds_self;
iMap<double> upper_bounds_dht;
iMap<double> lower_bounds_dht;
unordered_map<int, double> multi_bwd_idx_p;
unordered_map<int, unordered_map<int, double>> multi_bwd_idx_r;
vector<pair<int, double>> map_lower_bounds;
// for hubppr
vector<int> hub_fwd_idx;
//pointers to compressed fwd_idx, nodeid:{ start-pointer, start-pointer, start-pointer,...,end-pointer }
map<int, vector<unsigned long long> > hub_fwd_idx_cp_pointers;
vector<vector<unsigned long long>> hub_fwd_idx_ptrs;
vector<int> hub_fwd_idx_size;
iMap<int> hub_fwd_idx_size_k;
vector<int> hub_sample_number;
iMap<int> hub_counter;
map<int, vector<HubBwdidxWithResidual>> hub_bwd_idx;
unsigned concurrency;
vector<int> ks;
vector<unordered_map<int, double>> residual_maps;
vector<map<int, double>> reserve_maps;
inline uint32_t xor128(void) {
static uint32_t x = 123456789;
static uint32_t y = 362436069;
static uint32_t z = 521288629;
static uint32_t w = 88675123;
uint32_t t;
t = x ^ (x << 11);
x = y;
y = z;
z = w;
return w = w ^ (w >> 19) ^ (t ^ (t >> 8));
}
inline static unsigned long xshift_lrand() {
return (unsigned long) xor128();
}
inline static double xshift_drand() {
return ((double) xshift_lrand() / (double) UINT_MAX);
}
inline static unsigned long lrand() {
return rand();
// return sfmt_genrand_uint32(&sfmtSeed);
}
inline static double drand() {
return rand() * 1.0f / RAND_MAX;
// return sfmt_genrand_real1(&sfmtSeed);
}
inline int random_walk(int start, const Graph &graph) {
int cur = start;
unsigned long k;
if (graph.g[start].size() == 0) {
return start;
}
while (true) {
if (drand() < config.alpha) {
return cur;
}
if (graph.g[cur].size()) {
k = lrand() % graph.g[cur].size();
cur = graph.g[cur][k];
} else {
cur = start;
}
}
}
inline void random_walk_dht(int start, int pathid, const Graph &graph, unordered_map<int, pair<int, int>> &occur) {
int cur = start;
unsigned long k;
bool flag = true;
if (graph.g[start].size() == 0) {
//return start;
flag = false;
if (occur.find(cur) == occur.end()) {
occur.emplace(cur, make_pair(pathid, 1));
} else if (occur.at(cur).first != pathid) {
occur.at(cur).first = pathid;
occur.at(cur).second++;
}
}
while (flag) {
if (occur.find(cur) == occur.end()) {
occur.emplace(cur, make_pair(pathid, 1));
} else if (occur.at(cur).first != pathid) {
occur.at(cur).first = pathid;
occur.at(cur).second++;
}
if (drand() < config.alpha) {
//return cur;
break;
}
if (graph.g[cur].size()) {
k = lrand() % graph.g[cur].size();
cur = graph.g[cur][k];
} else {
cur = start;
}
}
}
void global_iteration(int v, vector<double> &dht_old, const Graph &graph) {
vector<double> new_dht;
/*
for(auto item:dht_old){
int nodeid=item.first;
for(int nei:graph.gr[nodeid]){
if (nei==v)continue;
int deg=graph.g[nei].size();
new_dht[nei] += (1-config.alpha)/deg* item.second;
}
}*/
double max_dht = 0;
for (int i = 0; i < graph.n; ++i) {
if (i == v)continue;
int deg = graph.g[i].size();
for (int nei:graph.g[i]) {
new_dht[i] += (1 - config.alpha) / deg * dht_old.at(nei);
}
}
new_dht[v] += 1;
swap(dht_old, new_dht);
}
inline double compute_dne_error(double max_error, int k, double max_gap_error) {
double lambda = pow((1 - config.alpha), k) / config.alpha * max_gap_error;
return (1 - config.alpha) * (1 - config.alpha) / (config.alpha * (2 - config.alpha)) * (max_error + lambda) +
lambda;
}
inline bool if_dne_stop(double &old_error, double max_error, int k, double max_gap_error) {
double new_error = compute_dne_error(max_error, k, max_gap_error);
cout << new_error << " " << old_error << " " << config.epsilon * config.delta << endl;
if (new_error <= config.epsilon * config.delta) return true;
if (old_error >= new_error && old_error / new_error < 1.0 + pow(0.1, 10)) return true;
old_error = new_error;
return false;
}
double dhe_query_basic(int query_node, int v, int den_m, const Graph &graph) {
unordered_map<int, double> dht_to_v, dht_to_v_copy;
dht_to_v[v] = 1;
set<int> neighbors, boundary;
neighbors.emplace(v);
boundary.emplace(v);
int max_node = -1;
double max_error = 0, factor_m =
(1 - config.alpha) * (1 - config.alpha) / (config.alpha * (2 - config.alpha));//\tau^2/(1-\tau^2)
// expand
while (neighbors.size() < den_m && !boundary.empty()) {
max_error = 0;
for (int node_in_bound:boundary) {
if (dht_to_v.at(node_in_bound) > max_error) {
max_error = dht_to_v.at(node_in_bound);
max_node = node_in_bound;
}
}
if (max_error * factor_m < config.epsilon * config.delta) {
break;
}
boundary.erase(max_node);
for (int nei:graph.gr[max_node]) {
neighbors.emplace(nei);
}
for (int nei:graph.gr[max_node]) {
for (int nei_of_nei:graph.gr[nei]) {
if (neighbors.find(nei_of_nei) == neighbors.end()) {
boundary.emplace(nei);
break;
}
}
}
for (int node:neighbors) {
if (node == v) {
dht_to_v[node] = 1;
continue;
} else {
dht_to_v[node] = 0;
}
int deg = graph.g[node].size();
for (int nei:graph.g[node]) {
dht_to_v[node] += (1 - config.alpha) / deg * dht_to_v[nei];
}
}
}
//refinement
Timer tm(111);
max_error = 0;
int k = 0;
double old_error = 0, max_gap_first = 0;
do {
k++;
for (int node:neighbors) {
if (node == v) {
dht_to_v_copy[node] = 1;
continue;
} else {
dht_to_v_copy[node] = 0;
}
int deg = graph.g[node].size();
for (int nei:graph.g[node]) {
dht_to_v_copy[node] += (1 - config.alpha) / deg * dht_to_v[nei];
}
if (boundary.find(node) != boundary.end() && max_error < dht_to_v_copy[node]) {
max_error = dht_to_v_copy[node];
}
}
if (k == 1) {
for (auto item:dht_to_v_copy) {
if (max_gap_first < item.second - dht_to_v[item.first]) {
max_gap_first = item.second - dht_to_v[item.first];
}
}
}
swap(dht_to_v, dht_to_v_copy);
} while (!if_dne_stop(old_error, max_error, k, max_gap_first));
return dht_to_v[query_node];
}
unsigned int SEED = 1;
inline static unsigned long lrand_thd(int core_id) {
//static thread_local std::mt19937 gen(core_id+1);
//static std::uniform_int_distribution<> dis(0, INT_MAX);
//return dis(gen);
return rand_r(&SEED);
}
inline static double drand_thd(int core_id) {
return ((double) lrand_thd(core_id) / (double) INT_MAX);
}
inline int random_walk_thd(int start, const Graph &graph, int core_id) {
int cur = start;
unsigned long k;
if (graph.g[start].size() == 0) {
return start;
}
while (true) {
if (drand_thd(core_id) < config.alpha) {
return cur;
}
if (graph.g[cur].size()) {
k = lrand_thd(core_id) % graph.g[cur].size();
cur = graph.g[cur][k];
} else {
cur = start;
}
}
}
void count_hub_dest() {
// { Timer tm(101);
int remaining;
unsigned long long last_beg_ptr;
unsigned long long end_ptr;
int hub_node;
int blocked_num;
int bit_pos;
for (int i = 0; i < hub_counter.occur.m_num; i++) {
hub_node = hub_counter.occur[i];
last_beg_ptr = hub_fwd_idx_ptrs[hub_node][hub_fwd_idx_ptrs[hub_node].size() - 2];
end_ptr = hub_fwd_idx_ptrs[hub_node][hub_fwd_idx_ptrs[hub_node].size() - 1];
remaining = hub_counter[hub_node];
if (remaining > hub_fwd_idx_size_k[hub_node]) {
for (unsigned long long ptr = last_beg_ptr; ptr < end_ptr; ptr += 2) {
if (rw_counter.notexist(hub_fwd_idx[ptr])) {
rw_counter.insert(hub_fwd_idx[ptr], hub_fwd_idx[ptr + 1]);
} else {
rw_counter[hub_fwd_idx[ptr]] += hub_fwd_idx[ptr + 1];
}
remaining -= hub_fwd_idx[ptr + 1];
}
}
for (int j = 0; j < hub_fwd_idx_ptrs[hub_node].size() - 2; j++) {
bit_pos = 1 << j;
if (bit_pos & remaining) {
for (unsigned long long ptr = hub_fwd_idx_ptrs[hub_node][j];
ptr < hub_fwd_idx_ptrs[hub_node][j + 1]; ptr += 2) {
if (rw_counter.notexist(hub_fwd_idx[ptr])) {
rw_counter.insert(hub_fwd_idx[ptr], hub_fwd_idx[ptr + 1]);
} else {
rw_counter[hub_fwd_idx[ptr]] += hub_fwd_idx[ptr + 1];
}
}
}
}
}
// }
}
inline int random_walk_with_compressed_forward_oracle(int start, const Graph &graph) {
int cur = start;
unsigned long k;
if (graph.g[start].size() == 0) {
return start;
}
while (true) {
if (hub_fwd_idx_size[cur] != 0 && (hub_counter.notexist(cur) || hub_counter[cur] < hub_fwd_idx_size[cur])) {
if (hub_counter.notexist(cur))
hub_counter.insert(cur, 1);
else
hub_counter[cur] += 1;
return -1;
}
if (drand() < config.alpha) {
return cur;
}
if (graph.g[cur].size()) {
k = lrand() % graph.g[cur].size();
cur = graph.g[cur][k];
} else
cur = start;
}
}
inline void split_line() {
INFO("-----------------------------");
}
inline void display_setting() {
INFO(config.epsilon);
INFO(config.delta);
INFO(config.pfail);
INFO(config.rmax);
INFO(config.omega);
if (config2.pfail == config.pfail) {
INFO(config2.epsilon);
INFO(config2.delta);
INFO(config2.pfail);
INFO(config2.rmax);
INFO(config2.omega);
}
}
inline void display_ppr() {
for (int i = 0; i < ppr.occur.m_num; i++) {
cout << ppr.occur[i] << "->" << ppr[ppr.occur[i]] << endl;
}
}
inline void display_dht() {
for (int i = 0; i < dht.occur.m_num; i++) {
if (dht[dht.occur[i]] <= 0)continue;
cout << dht.occur[i] << "->" << dht[dht.occur[i]] << endl;
}
}
static void display_time_usage(int used_counter, int query_size) {
if (config.algo == FORA) {
cout << "Total cost (s): " << Timer::used(used_counter) << endl;
cout << Timer::used(RONDOM_WALK) * 100.0 / Timer::used(used_counter) << "%" << " for random walk cost" << endl;
cout << Timer::used(FWD_LU) * 100.0 / Timer::used(used_counter) << "%" << " for forward push cost" << endl;
// if(config.action == TOPK)
// cout << Timer::used(SORT_MAP)*100.0/Timer::used(used_counter) << "%" << " for sorting top k cost" << endl;
split_line();
} else if (config.algo == FORA_MC) {
cout << "Total cost (s): " << Timer::used(used_counter) << endl;
cout << Timer::used(RONDOM_WALK) * 100.0 / Timer::used(used_counter) << "%" << " for random walk cost" << endl;
cout << Timer::used(FWD_LU) * 100.0 / Timer::used(used_counter) << "%" << " for forward push cost" << endl;
cout << Timer::used(MC_QUERY2) * 100.0 / Timer::used(used_counter) << "%" << " for montecarlo cost" << endl;
split_line();
} else if (config.algo == BIPPR) {
cout << "Total cost (s): " << Timer::used(used_counter) << endl;
cout << Timer::used(RONDOM_WALK) * 100.0 / Timer::used(used_counter) << "%" << " for random walk cost" << endl;
cout << Timer::used(BWD_LU) * 100.0 / Timer::used(used_counter) << "%" << " for backward push cost" << endl;
} else if (config.algo == MC) {
cout << "Total cost (s): " << Timer::used(used_counter) << endl;
cout << Timer::used(RONDOM_WALK) * 100.0 / Timer::used(used_counter) << "%" << " for random walk cost" << endl;
} else if (config.algo == FWDPUSH) {
cout << "Total cost (s): " << Timer::used(used_counter) << endl;
cout << Timer::used(FWD_LU) * 100.0 / Timer::used(used_counter) << "%" << " for forward push cost" << endl;
} else if (config.algo == MC) {
cout << "Total cost (s): " << Timer::used(used_counter) << endl;
cout << Timer::used(RONDOM_WALK) * 100.0 / Timer::used(used_counter) << "%" << " for random walk cost" << endl;
}
if (config.with_rw_idx)
cout << "Average rand-walk idx hit ratio: " << num_hit_idx * 100.0 / num_total_rw << "%" << endl;
if (config.action == TOPK) {
assert(result.real_topk_source_count > 0);
cout << "Average top-K Precision: " << result.topk_precision / result.real_topk_source_count << endl;
cout << "Average top-K Recall: " << result.topk_recall / result.real_topk_source_count << endl;
}
cout << "Average query time (s):" << Timer::used(used_counter) / query_size << endl;
cout << "Memory usage (MB):" << get_proc_memory() / 1000.0 << endl << endl;
}
static void set_result(const Graph &graph, int used_counter, int query_size) {
config.query_size = query_size;
result.m = graph.m;
result.n = graph.n;
result.avg_query_time = Timer::used(used_counter) / query_size;
result.total_mem_usage = get_proc_memory() / 1000.0;
result.total_time_usage = Timer::used(used_counter);
result.num_randwalk = num_total_rw;
if (config.with_rw_idx) {
result.num_rw_idx_use = num_hit_idx;
result.hit_idx_ratio = num_hit_idx * 1.0 / num_total_rw;
}
result.randwalk_time = Timer::used(RONDOM_WALK);
result.randwalk_time_ratio = Timer::used(RONDOM_WALK) * 100 / Timer::used(used_counter);
if (config.algo == FBRW) {
result.propagation_time = Timer::used(FWD_LU) + Timer::used(BWD_LU);
result.propagation_time_ratio = result.propagation_time * 100 / Timer::used(used_counter);
}
if (config.action == TOPK) {
result.topk_sort_time = Timer::used(SORT_MAP);
// result.topk_precision = avg_topk_precision;
// result.topk_sort_time_ratio = Timer::used(SORT_MAP)*100/Timer::used(used_counter);
}
}
inline void fora_setting(int n, long long m) {
config.rmax = config.epsilon * sqrt(config.delta / 3 / m / log(2 / config.pfail));
config.rmax *= config.rmax_scale;
// config.rmax *= config.multithread_param;
config.omega = (2 + config.epsilon) * log(2 / config.pfail) / config.delta / config.epsilon / config.epsilon;
}
inline void fb_raw_setting(int n, long long m, double ratio, double raw_epsilon) {
config.pfail = 1.0 / 2 / n;
config2 = config;
config.delta = config.alpha / n;
config2.delta = config2.alpha;
config.epsilon = raw_epsilon * ratio / (ratio + 1 + raw_epsilon);
config2.epsilon = raw_epsilon / (ratio + 1 + raw_epsilon);
config.rmax = config.epsilon * sqrt(config.delta / 3 / m / log(2 / config.pfail));
config.rmax *= config.rmax_scale;
config.omega = (2 + config.epsilon) * log(2 / config.pfail) / config.delta / config.epsilon / config.epsilon;
//config2.rmax = config2.epsilon * sqrt(config2.delta / 3 / log(2.0 / config2.pfail));
config2.rmax = config2.epsilon *
sqrt(config2.delta * m / n / config2.alpha / (2 + config2.epsilon) / log(2.0 / config2.pfail));
config2.rmax *= config2.rmax_scale;
config2.omega = config2.rmax * (2 + config2.epsilon) * log(2.0 / config2.pfail) / config2.delta / config2.epsilon /
config2.epsilon;
}
bool compare_fora_bippr_cost(int k, int m = 0, double rsum = 0) {
if (k == 0)
return false;
static double old_config2_rmax;
//比较前向和后向的复杂度,如果前向高于后向,返回真
double ratio;
double cost_fora =
(2 * config.epsilon / 3 + 2) * log(2 / config.pfail) / config.delta / config.epsilon / config.epsilon;
INFO(cost_fora);
INFO(1 / config.rmax + rsum * config.omega);
INFO(k * (2 / config2.rmax));
//ratio=(1/config.rmax+rsum*config.omega)/k/(2/config2.rmax);
ratio = ((config.rmax * m + rsum) * cost_fora * (sqrt(2) - 1) / k / (2 / config2.rmax));
old_config2_rmax = config2.rmax;
return ratio > 1;
}
bool test_run_fora(int candidate_size, long m, int n, double forward_counter, double real_num_rw) {
if (candidate_size == 0)
return true;
double new_rmax2 = config2.epsilon / 2 * sqrt(config2.delta * m / n / config2.alpha / (2 + config2.epsilon / 2) /
log(2.0 / config2.pfail));
double cost_new = forward_counter * 1.4 + real_num_rw * 2;
double cost_bi = candidate_size * 2 / new_rmax2 * m / n / config2.alpha;
double cost_bi_old = candidate_size / config2.rmax * m / n / config2.alpha;
//INFO(cost_new, candidate_size * 2 / new_rmax2, cost_bi);
//INFO(2 / new_rmax2 * m / n / config2.alpha, new_rmax2);
if (ppr_bi.occur.m_num == 0) {
return cost_new < cost_bi;
} else {
return cost_new < cost_bi - cost_bi_old;
}
}
inline void fora_bippr_setting(int n, long long m, double ratio, double raw_epsilon, bool topk = false) {
if (!topk) {
config.pfail = 1.0 / 2 / n;
config.delta = config.alpha / n;
config2 = config;
config.epsilon = raw_epsilon * ratio / (ratio + 1 + raw_epsilon);
config2.epsilon = raw_epsilon / (ratio + 1 + raw_epsilon);
config2.delta = config2.alpha;
}
config.rmax = config.epsilon * sqrt(config.delta / 3 / m / log(2 / config.pfail));
config.rmax *= config.rmax_scale;
// config.rmax *= config.multithread_param;
config.omega = (2 + config.epsilon) * log(2 / config.pfail) / config.delta / config.epsilon / config.epsilon;
config2.rmax = config2.epsilon *
sqrt(config2.delta * m / n / config2.alpha / (2 + config2.epsilon) / log(2.0 / config2.pfail));
config2.rmax *= config2.rmax_scale;
config2.omega = config2.rmax * (2 + config2.epsilon) * log(2.0 / config2.pfail) / config2.delta / config2.epsilon /
config2.epsilon;
}
inline void montecarlo_setting() {
double fwd_rw_count = 3 * log(2 / config.pfail) / config.epsilon / config.epsilon / config.delta;
config.omega = fwd_rw_count;
}
inline bool check_cost(double rsum, double &ratio, double n, double m, long forward_push_num, double raw_epsilon) {
int counter = 0;
for (int j = 0; j < fwd_idx.first.occur.m_num; ++j) {
int node = fwd_idx.first.occur[j];
if (fwd_idx.first[node] > config.delta) {
counter++;
}
}
double cost_bippr = counter * (config2.omega + m / n / config.alpha / config2.rmax);//config.alpha;
double cost_fora = rsum * config.omega + forward_push_num;
double old_rmax = config.rmax, new_ratio = ratio / 2;
int iter=0;
do {
iter++;
// reduce ratio to minimize cost
fora_bippr_setting(n, m, new_ratio, raw_epsilon);
double cost_bippr_new = counter * (config2.omega + m / n / config.alpha / config2.rmax);//config.alpha;
double cost_fora_new = rsum * config.omega + forward_push_num * old_rmax / config.rmax;
if (cost_bippr + cost_fora > cost_bippr_new + cost_fora_new) {
ratio = new_ratio;
if (iter>1) return false;
return true;
} else {
new_ratio = (new_ratio+ratio)/2;
}
} while (iter<4);
fora_bippr_setting(n, m, ratio, raw_epsilon);
return false;
}
inline void generate_ss_query(int n) {
string filename = config.graph_location + "ssquery.txt";
ofstream queryfile(filename);
for (int i = 0; i < config.query_size; i++) {
int v = rand() % n;
queryfile << v << endl;
}
}
void load_ss_query(vector<int> &queries) {
string filename = config.query_high_degree ? config.graph_location + "high_degree_ssquery.txt" :
config.graph_location + "ssquery.txt";
if (!file_exists_test(filename)) {
cerr << "query file does not exist, please generate ss query files first" << endl;
exit(0);
}
ifstream queryfile(filename);
int v;
while (queryfile >> v) {
queries.push_back(v);
}
}
void compute_precision_dht(int v) {
double precision = 0.0;
double recall = 0.0;
if (exact_topk_dhts.size() > 1 && exact_topk_dhts.find(v) != exact_topk_dhts.end()) {
unordered_map<int, double> topk_map;
for (auto &p: topk_dhts) {
if (p.second > 0) {
topk_map.insert(p);
}
}
unordered_map<int, double> exact_map;
int size_e = min(config.k, (unsigned int) exact_topk_dhts[v].size());
for (int i = 0; i < size_e; i++) {
pair<int, double> &p = exact_topk_dhts[v][i];
if (p.second > 0) {
exact_map.insert(p);
if (topk_map.find(p.first) != topk_map.end())
recall++;
}
}
for (auto &p: topk_map) {
if (exact_map.find(p.first) != exact_map.end()) {
precision++;
}
}
// for(int i=0; i<config.k; i++){
// cout << "NO." << i << " pred:" << topk_pprs[i].first << ", " << topk_pprs[i].second << "\t exact:" << exact_topk_dhts[v][i].first << ", " << exact_topk_dhts[v][i].second << endl;
// }
assert(exact_map.size() > 0);
assert(topk_map.size() > 0);
if (exact_map.size() <= 1)
return;
recall = recall * 1.0 / exact_map.size();
precision = precision * 1.0 / exact_map.size();
INFO(exact_map.size(), recall, precision);
result.topk_recall += recall;
result.topk_precision += precision;
result.real_topk_source_count++;
}
}
double backward_push(int s, int t, const Graph &graph, iMap<double> &r, double rmax, double init_residual = 1) {
double p = 0;
r.initialize(graph.n);
r.clean();
unordered_map<int, bool> idx;
idx.clear();
vector<int> q;
q.reserve(graph.n);
q.push_back(-1);
unsigned long left = 1;
double myeps = rmax;
q.push_back(t);
r.insert(t, init_residual);
idx[t] = true;
while (left < q.size()) {
int v = q[left];
idx[v] = false;
left++;
if (r[v] < myeps)
break;
if (v == s) {
p += r[v] * config.alpha;
}
double residual = (1 - config.alpha) * r[v];
r[v] = 0;
if (graph.gr[v].size() > 0) {
for (int next : graph.gr[v]) {
int cnt = graph.g[next].size();
if (r.notexist(next))
r.insert(next, residual / cnt);
else
r[next] += residual / cnt;
if (r[next] > myeps && idx[next] != true) {
// put next into q if next is not in q
idx[next] = true;//(int) q.size();
q.push_back(next);
}
}
}
}
return p;
}
double pair_wise_ppr(int source, int target, Graph &graph, double delta = 0.0000000001, double epsilon = 0.5) {
double result = 0, pfail = 1.0 / graph.n;
//set parameter
double rmax = epsilon * sqrt(graph.m * delta / graph.n / (2 + epsilon) / log(2 / pfail));
double omega = ceil(rmax * (2 + epsilon) * log(2 / pfail) / delta / epsilon / epsilon);
iMap<double> r;
result = backward_push(source, target, graph, r, rmax);
for (unsigned long i = 0; i < omega; i++) {
int destination = random_walk(source, graph);
if (r.exist(destination)) {
result += r[destination] / omega;
}
}
return result;
}
void compute_NDCG(int v, const map<int, double> ppr_self, Graph &graph) {
if (exact_topk_dhts.size() > 1 && exact_topk_dhts.find(v) != exact_topk_dhts.end()) {
// first compute IDCG
double iDCG = 0, DCG = 0, min_ssppr = 0;
unordered_map<int, double> exact_map, exact_ppr_map;
for (int k = 0; k < exact_topk_pprs[v].size(); ++k) {
pair<int, double> &p = exact_topk_pprs[v][k];
if (p.second > 0) {
exact_ppr_map.insert(p);
}
}
min_ssppr = exact_topk_pprs[v].back().second;
//cout<<"exact dht:\t";
int size_e = min(config.k, (unsigned int) exact_topk_dhts[v].size());
for (int i = 0; i < size_e; i++) {
pair<int, double> &p = exact_topk_dhts[v][i];
//cout<<p.first<<":"<<p.second<<"\t";
iDCG += (pow(2, p.second) - 1) / (log(i + 2) / log(2));
if (p.second > 0) {
exact_map.insert(p);
}
}
//cout<<endl;
//cout<<"test dht:\t";
for (int j = 0; j < exact_map.size(); ++j) {
int node = topk_dhts[j].first;
//cout<<topk_dhts[j].second;
double dht_tmp = 0;
if (exact_map.find(node) != exact_map.end()) {
dht_tmp = exact_map[node];
} else if (exact_ppr_map.find(node) != exact_ppr_map.end() && ppr_self.find(node) != ppr_self.end()) {
dht_tmp = exact_ppr_map.at(node) / ppr_self.at(node);
} else if (exact_ppr_map.find(node) != exact_ppr_map.end() && ppr_self.find(node) == ppr_self.end()) {
// recompute ppr(t,t)
double ppr_self_new = pair_wise_ppr(node, node, graph);
dht_tmp = min(exact_ppr_map.at(node) / ppr_self_new,exact_topk_dhts[v][size_e-1].second);
} else if (exact_ppr_map.find(node) == exact_ppr_map.end() && ppr_self.find(node) != ppr_self.end()) {
// recompute ppr(s,t)
double ppr_source = min(pair_wise_ppr(v, node, graph), min_ssppr);
dht_tmp = min(ppr_source / ppr_self.at(node),exact_topk_dhts[v][size_e-1].second);
} else if (exact_ppr_map.find(node) == exact_ppr_map.end() && ppr_self.find(node) == ppr_self.end()) {
// recompute ppr(s,t) and ppr(t,t)
double ppr_source = min(pair_wise_ppr(v, node, graph), min_ssppr);
double ppr_self_new = pair_wise_ppr(node, node, graph);
dht_tmp = min(ppr_source / ppr_self_new,exact_topk_dhts[v][size_e-1].second);
}
DCG += (pow(2, dht_tmp) - 1) / (log(j + 2) / log(2));
}
//cout<<endl;
double NDCG = DCG / iDCG;
INFO(DCG,iDCG, NDCG);
if (exact_map.size() <= 1)
return;
result.topk_NDCG += NDCG;
return;
}
}
inline bool cmp(double x, double y) {
return x > y;
}
// obtain the top-k ppr values from ppr map
double kth_ppr() {
Timer tm(SORT_MAP);
static vector<double> temp_ppr;
temp_ppr.clear();
temp_ppr.resize(ppr.occur.m_num);
int nodeid;
for (int i; i < ppr.occur.m_num; i++) {
// if(ppr.m_data[i]>0)
// temp_ppr[size++] = ppr.m_data[i];
temp_ppr[i] = ppr[ppr.occur[i]];
}
nth_element(temp_ppr.begin(), temp_ppr.begin() + config.k - 1, temp_ppr.end(), cmp);
return temp_ppr[config.k - 1];
}
double topk_ppr() {
topk_pprs.clear();
topk_pprs.resize(config.k);
static unordered_map<int, double> temp_ppr;
temp_ppr.clear();
// temp_ppr.resize(ppr.occur.m_num);
int nodeid;
for (long i = 0; i < ppr.occur.m_num; i++) {
nodeid = ppr.occur[i];
// INFO(nodeid);
temp_ppr[nodeid] = ppr[nodeid];
}
partial_sort_copy(temp_ppr.begin(), temp_ppr.end(), topk_pprs.begin(), topk_pprs.end(),
[](pair<int, double> const &l, pair<int, double> const &r) { return l.second > r.second; });
return topk_pprs[config.k - 1].second;
}
double topk_dht() {
topk_dhts.clear();
topk_dhts.resize(config.k);
static vector<pair<int, double> > temp_dht;
temp_dht.clear();
temp_dht.resize(dht.cur);
int nodeid, cur = 0;
for (int k = 0; k < dht.occur.m_num; ++k) {
nodeid = dht.occur[k];
if (dht.exist(nodeid))
temp_dht[cur++] = MP(nodeid, dht[nodeid]);
}
partial_sort_copy(temp_dht.begin(), temp_dht.end(), topk_dhts.begin(), topk_dhts.end(),
[](pair<int, double> const &l, pair<int, double> const &r) { return l.second > r.second; });
return topk_dhts[config.k - 1].second;
}
void compute_precision_for_dif_k_dht(int v) {
if (exact_topk_dhts.size() > 1 && exact_topk_dhts.find(v) != exact_topk_dhts.end()) {
//vector<double> true_dht(exact_topk_dhts.size())
for (auto k: ks) {
int j = 0;
unordered_map<int, double> topk_map;
for (auto &p: topk_dhts) {
if (p.second > 0) {
topk_map.insert(p);
}
j++;
if (j == k) { // only pick topk
break;
}
}
double recall = 0.0;
unordered_map<int, double> exact_map;
int size_e = min(k, (int) exact_topk_dhts[v].size());
for (int i = 0; i < size_e; i++) {
pair<int, double> &p = exact_topk_dhts[v][i];
if (p.second > 0) {
exact_map.insert(p);
if (topk_map.find(p.first) != topk_map.end())
recall++;
}
}
double precision = 0.0;
for (auto &p: topk_map) {
if (exact_map.find(p.first) != exact_map.end()) {
precision++;
}
}
if (exact_map.size() <= 1)
continue;
precision = precision * 1.0 / exact_map.size();
recall = recall * 1.0 / exact_map.size();
pred_results[k].topk_precision += precision;
pred_results[k].topk_recall += recall;
pred_results[k].real_topk_source_count++;
}
}
}
void compute_NDCG_for_dif_k_dht(int v, const map<int, double> ppr_self, Graph &graph) {
if (exact_topk_dhts.size() > 1 && exact_topk_dhts.find(v) != exact_topk_dhts.end()) {
//vector<double> true_dht(exact_topk_dhts.size())
unordered_map<int, double> exact_map, exact_ppr_map;
for (int i1 = 0; i1 < exact_topk_pprs[v].size(); ++i1) {
pair<int, double> &p = exact_topk_pprs[v][i1];
if (p.second > 0) {
exact_ppr_map.insert(p);
}
}
for (auto k: ks) {
double iDCG = 0, DCG = 0, min_ssppr = 0;
//得到ppr的哈希表
min_ssppr = exact_topk_pprs[v].back().second;
// first comput IDCG
int size_e = min((unsigned int)k, (unsigned int) exact_topk_dhts[v].size());
for (int i = 0; i < size_e; i++) {
pair<int, double> &p = exact_topk_dhts[v][i];
iDCG += (pow(2, p.second) - 1) / (log(i + 2) / log(2));
if (p.second > 0) {
exact_map.insert(p);
}
}
int size_dht_real=min(k,int(exact_map.size()));
for (int j = 0; j < size_dht_real; ++j) {
int node = topk_dhts[j].first;
double dht_tmp = 0;
if (exact_map.find(node) != exact_map.end()) {
dht_tmp = exact_map[node];
} else if (exact_ppr_map.find(node) != exact_ppr_map.end() && ppr_self.find(node) != ppr_self.end()) {
dht_tmp = exact_ppr_map.at(node) / ppr_self.at(node);
} else if (exact_ppr_map.find(node) != exact_ppr_map.end() && ppr_self.find(node) == ppr_self.end()) {
// recompute ppr(t,t)
double ppr_self_new = pair_wise_ppr(node, node, graph);
dht_tmp = min(exact_ppr_map.at(node) / ppr_self_new,exact_topk_dhts[v][size_e-1].second);
} else if (exact_ppr_map.find(node) == exact_ppr_map.end() && ppr_self.find(node) != ppr_self.end()) {
// recompute ppr(s,t)
double ppr_source = min(pair_wise_ppr(v, node, graph), min_ssppr);
dht_tmp = min(ppr_source / ppr_self.at(node),exact_topk_dhts[v][size_e-1].second);
} else if (exact_ppr_map.find(node) == exact_ppr_map.end() && ppr_self.find(node) == ppr_self.end()) {
// recompute ppr(s,t) ppr(t,t)
double ppr_source = min(pair_wise_ppr(v, node, graph), min_ssppr);
double ppr_self_new = pair_wise_ppr(node, node, graph);
dht_tmp = min(ppr_source / ppr_self_new,exact_topk_dhts[v][size_e-1].second);
}
DCG += (pow(2, dht_tmp) - 1) / (log(j + 2) / log(2));
}
double NDCG = DCG / iDCG;
INFO(k,NDCG);
pred_results[k].topk_NDCG += NDCG;
}
}
}
inline void display_precision_for_dif_k() {
split_line();
cout << config.algo << endl;
for (auto k: ks) {
cout << k << "\t";