-
Notifications
You must be signed in to change notification settings - Fork 0
/
final_benchmark1.cpp
889 lines (761 loc) · 23.7 KB
/
final_benchmark1.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
/*
Author: Gaspare Ferraro
Count and find simple k colorful-path in a graph
using the color-coding technique (parallel version)
*/
#include <vector>
#include <set>
#include <map>
#include <string>
#include <algorithm>
#include <iterator>
#include <random>
#include <limits>
#include <stdio.h>
#include <sys/stat.h>
#include <string.h>
#include <fcntl.h>
#include <omp.h>
#include <getopt.h>
#include <unistd.h>
#include <time.h>
#include <sys/time.h>
#ifdef K_8
#define MAXK 8
#define COLORSET uint8_t
#elif K_16
#define MAXK 16
#define COLORSET uint16_t
#elif K_64
#define MAXK 64
#define COLORSET uint64_t
#else
#define MAXK 32
#define COLORSET uint32_t
#endif
#if defined(_WIN32)
#include <windows.h>
#include <psapi.h>
#elif defined(__unix__) || defined(__unix) || defined(unix) || \
(defined(__APPLE__) && defined(__MACH__))
#include <unistd.h>
#include <sys/resource.h>
#if defined(__APPLE__) && defined(__MACH__)
#include <mach/mach.h>
#elif(defined(_AIX) || defined(__TOS__AIX__)) || \
(defined(__sun__) || defined(__sun) || \
defined(sun) && (defined(__SVR4) || defined(__svr4__)))
#include <fcntl.h>
#include <procfs.h>
#elif defined(__linux__) || defined(__linux) || defined(linux) || \
defined(__gnu_linux__)
#include <stdio.h>
size_t getCurrentRSS() {
#if defined(_WIN32)
/* Windows -------------------------------------------------- */
PROCESS_MEMORY_COUNTERS info;
GetProcessMemoryInfo(GetCurrentProcess(), &info, sizeof(info));
return (size_t)info.WorkingSetSize;
#elif defined(__APPLE__) && defined(__MACH__)
/* OSX ------------------------------------------------------ */
struct mach_task_basic_info info;
mach_msg_type_number_t infoCount = MACH_TASK_BASIC_INFO_COUNT;
if (task_info(mach_task_self(), MACH_TASK_BASIC_INFO, (task_info_t)&info,
&infoCount) != KERN_SUCCESS)
return (size_t)0L; /* Can't access? */
return (size_t)info.resident_size;
#elif defined(__linux__) || defined(__linux) || defined(linux) || \
defined(__gnu_linux__)
/* Linux ---------------------------------------------------- */
long rss = 0L;
FILE *fp = NULL;
if ((fp = fopen("/proc/self/statm", "r")) == NULL)
return (size_t)0L; /* Can't open? */
if (fscanf(fp, "%*s%ld", &rss) != 1) {
fclose(fp);
return (size_t)0L; /* Can't read? */
}
fclose(fp);
return (size_t)rss * (size_t)sysconf(_SC_PAGESIZE);
#else
/* AIX, BSD, Solaris, and Unknown OS ------------------------ */
return (size_t)0L; /* Unsupported. */
#endif
}
#endif
#else
#error "Cannot define getPeakRSS( ) or getCurrentRSS( ) for an unknown OS."
#endif
using namespace std;
typedef long long ll;
unsigned int N, M;
unsigned q = 0;
unsigned thread_count = 0;
static int verbose_flag, help_flag;
ll cont = 0;
int *color;
char *label;
vector<int> *G;
int Sa, Sb;
int *A, *B;
inline int nextInt() {
int r;
scanf("%d", &r);
return r;
}
// Random generator
random_device rd;
mt19937_64 eng = mt19937_64(rd());
uniform_int_distribution<unsigned long long> distr;
// Get pos-th bit in n
bool getBit(COLORSET n, int pos) { return ((n >> pos) & 1) == 1; }
// Set pos-th bit in n
COLORSET setBit(COLORSET n, int pos) { return n |= 1 << pos; }
// Reset pos-th bit in n
COLORSET clearBit(COLORSET n, int pos) { return n &= ~(1 << pos); }
// Complementary set of a COLORSET
COLORSET getCompl(COLORSET n) { return ((1 << q) - 1) & (~n); }
// Random coloring graph using k color
inline void randomColor() {
for (unsigned int i = 0; i < N; i++) color[i] = eng() % q;
}
// Path label
string L(vector<int> P) {
string l = "";
for (size_t i = 0; i < P.size(); i++) l += label[P[i]];
return l;
}
// Link
map<pair<int, COLORSET>, vector<int>> links;
inline void addLink(int x, COLORSET C, int j) {
auto key = make_pair(x, C);
if (links.find(key) == links.end()) links[key] = vector<int>();
links[key].push_back(j);
}
// Oracle
vector<int> H(int x, COLORSET C) { return links[make_pair(x, C)]; }
void list_k_path(vector<int> ps, COLORSET cs, int x) {
vector<int> oracle = H(x, cs);
if (ps.size() + 1 == q) {
cont++;
for (int j : ps) printf("[%6d] ", j);
printf("\n");
for (int j : ps) printf("[%6d] ", color[j]);
printf("\n");
} else
for (int v : oracle) {
// printf("LINK TO %d\n",v);
ps.push_back(v);
list_k_path(ps, setBit(cs, color[v]), v);
ps.pop_back();
}
}
// bruteforce
set<string> dict;
map<pair<int, string>, ll> freqBrute;
vector<int> P[30];
string Pstring[30];
set<int> Pset[30];
void dfs(int t, int u, int k) {
if (Pset[t].find(u) != Pset[t].end()) return;
Pset[t].insert(u);
Pstring[t].push_back(label[u]);
P[t].push_back(u);
if (k == 0) {
#pragma omp critical
{
dict.insert(Pstring[t]);
freqBrute[make_pair(*P[t].begin(), Pstring[t])]++;
}
} else
for (int v : G[u]) dfs(t, v, k - 1);
Pset[t].erase(u);
Pstring[t].pop_back();
P[t].pop_back();
}
// Dynamic programming processing
map<COLORSET, ll> *DP[MAXK + 1];
void processDP() {
if (verbose_flag) printf("K = %u\n", 1);
// Base case
for (unsigned int u = 0; u < N; u++) DP[1][u][setBit(0, color[u])] = 1ll;
// Induction
for (unsigned int i = 2; i <= q; i++) {
if (verbose_flag) printf("K = %u\n", i);
#pragma omp parallel for schedule(static, 1)
for (unsigned int u = 0; u < N; u++) {
for (int v : G[u]) {
for (auto d : DP[i - 1][v]) {
COLORSET s = d.first;
ll f = d.second;
if (getBit(s, color[u])) continue;
ll fp = DP[i][u][setBit(s, color[u])];
DP[i][u][setBit(s, color[u])] = f + fp;
}
}
}
}
}
bool isPrefix(set<string> W, string x) {
auto it = W.lower_bound(x);
if (it == W.end()) return false;
return mismatch(x.begin(), x.end(), (*it).begin()).first == x.end();
}
map<string, ll> processFrequency(set<string> W, multiset<int> X) {
set<string> WR;
for (string w : W) {
reverse(w.begin(), w.end());
WR.insert(w);
}
vector<tuple<int, string, COLORSET>> old;
for (int x : X)
if (isPrefix(WR, string(&label[x], 1)))
old.push_back(make_tuple(x, string(&label[x], 1), setBit(0ll, color[x])));
for (int i = q - 1; i > 0; i--) {
// printf("\t\ti = %d || |T| = %zu:\n", i, old.size());
vector<tuple<int, string, COLORSET>> current;
#pragma omp parallel for schedule(dynamic)
for (int j = 0; j < (int)old.size(); j++) {
auto o = old[j];
int u = get<0>(o);
string LP = get<1>(o);
COLORSET CP = get<2>(o);
for (int v : G[u]) {
if (getBit(CP, color[v])) continue;
COLORSET CPv = setBit(CP, color[v]);
string LPv = LP + label[v];
if (!isPrefix(WR, LPv)) continue;
#pragma omp critical
{ current.push_back(make_tuple(v, LPv, CPv)); }
}
}
old = current;
}
map<string, ll> frequency;
for (auto c : old) {
string s = get<1>(c);
reverse(s.begin(), s.end());
frequency[s]++;
}
return frequency;
}
vector<int> randomPathTo(int u) {
vector<int> P;
P.push_back(u);
COLORSET D = getCompl(setBit(0l, color[u]));
for (int i = q - 1; i > 0; i--) {
vector<ll> freq;
for (int v : G[u]) freq.push_back(DP[i][v][D]);
discrete_distribution<int> distribution(freq.begin(), freq.end());
u = G[u][distribution(eng)];
P.push_back(u);
D = clearBit(D, color[u]);
}
reverse(P.begin(), P.end());
return P;
}
set<string> randomColorfulSample(vector<int> X, int r) {
set<string> W;
set<vector<int>> R;
vector<ll> freqX;
for (int x : X) freqX.push_back(DP[q][x][getCompl(0ll)]);
discrete_distribution<int> distribution(freqX.begin(), freqX.end());
while (R.size() < (size_t)r) {
int u = X[distribution(eng)];
vector<int> P = randomPathTo(u);
if (R.find(P) == R.end()) R.insert(P);
}
for (auto r : R) {
reverse(r.begin(), r.end());
W.insert(L(r));
}
return W;
}
map<pair<int, string>, ll> randomColorfulSamplePlus(vector<int> X, int r) {
map<pair<int, string>, ll> W;
set<vector<int>> R;
vector<ll> freqX;
for (int x : X) freqX.push_back(DP[q][x][getCompl(0ll)]);
discrete_distribution<int> distribution(freqX.begin(), freqX.end());
while (R.size() < (size_t)r) {
int u = X[distribution(eng)];
vector<int> P = randomPathTo(u);
if (R.find(P) == R.end()) R.insert(P);
}
for (auto r : R) {
reverse(r.begin(), r.end());
W[make_pair(*r.begin(), L(r))]++;
}
return W;
}
set<string> BCSampler(set<int> A, set<int> B, int r) {
vector<int> X;
for (int a : A) X.push_back(a);
for (int b : B) X.push_back(b);
return randomColorfulSample(X, r);
}
vector<int> naiveRandomPathTo(int u) {
vector<int> P;
set<int> Ps;
P.push_back(u);
Ps.insert(u);
for (int i = q - 1; i > 0; i--) {
vector<int> Nu;
for (int j : G[u])
if (Ps.find(j) == Ps.end()) Nu.push_back(j);
if (Nu.size() == 0) return P;
int u = Nu[rand() % Nu.size()];
Ps.insert(u);
P.push_back(u);
}
// reverse(P.begin(), P.end());
return P;
}
map<pair<int, string>, ll> baselineSampler(vector<int> X, int r) {
set<vector<int>> R;
while (R.size() < (size_t)r) {
int u = X[rand() % X.size()];
vector<int> P = naiveRandomPathTo(u);
if (P.size() == q && R.find(P) == R.end()) R.insert(P);
}
map<pair<int, string>, ll> fx;
for (auto P : R) fx[make_pair(*P.begin(), L(P))]++;
return fx;
}
double FJW(set<string> W, set<int> A, set<int> B) {
multiset<int> AiB, AB;
for (int a : A) AB.insert(a);
for (int b : B)
if (AB.find(b) == AB.end()) AB.insert(b);
for (int a : A)
if (B.find(a) != B.end()) AiB.insert(a);
long long num = 0ll;
long long den = 0ll;
map<string, ll> freqAiB = processFrequency(W, AiB);
map<string, ll> freqAB = processFrequency(W, AB);
for (string w : W) {
num += freqAiB[w];
den += freqAB[w];
}
return (double)num / (double)den;
}
double BCW(set<string> W, map<string, ll> freqA, map<string, ll> freqB,
long long R) {
ll num = 0ll;
for (string x : W) {
ll fax = freqA[x];
ll fbx = freqB[x];
num += 2 * min(fax, fbx);
}
return (double)num / (double)R;
}
double BCW(set<string> W, map<string, ll> freqA, map<string, ll> freqB) {
ll num = 0ll;
ll den = 0ll;
for (string x : W) {
ll fax = freqA[x];
ll fbx = freqB[x];
num += 2 * min(fax, fbx);
den += fax + fbx;
}
return (double)num / (double)den;
}
double FJW(set<string> W, map<string, ll> freqA, map<string, ll> freqB,
long long R) {
ll num = 0ll;
for (string x : W) {
ll fax = freqA[x];
ll fbx = freqB[x];
num += min(fax, fbx);
}
// printf("NUM = %lld DEN = %lld\n", num, R);
return (double)num / (double)R;
}
double BCW(set<string> W, set<int> A, set<int> B) {
ll num = 0ll;
ll den = 0ll;
multiset<int> mA, mB;
for (int a : A) mA.insert(a);
for (int b : B) mB.insert(b);
map<string, ll> freqA = processFrequency(W, mA);
map<string, ll> freqB = processFrequency(W, mB);
vector<string> vW = vector<string>(W.begin(), W.end());
// #pragma omp parallel for schedule(static, 1) reduction(+:num), reduction(+:
// den)
for (int i = 0; i < (int)vW.size(); i++) {
string w = vW[i];
long long fax = freqA[w];
long long fbx = freqB[w];
num += 2 * min(fax, fbx);
den += fax + fbx;
}
return (double)num / (double)den;
}
void print_usage(char *filename) {
printf(
"Usage: ./%s -q length -g filename -p threadcount -s filename"
"--help --verbose\n",
filename);
printf("Valid arguments:\n");
printf("-q, --path length\n");
printf("\tLength of the path.\n");
printf("-g, --input filename\n");
printf("\tInput file of labeled graph in nmle format (default stdin)\n");
printf("-p, --parallel threadcount\n");
printf("\tNumber of threads to use (default maximum thread avaiable)\n");
printf("--help\n");
printf("\tDisplay help text and exit.\n");
printf("--verbose\n");
printf("\nPrint status messages.\n");
}
bool input_graph_flag = false;
char *input_graph = NULL;
long long current_timestamp() {
struct timeval te;
gettimeofday(&te, NULL); // get current time
long long milliseconds = te.tv_sec * 1000LL + te.tv_usec / 1000;
return milliseconds;
}
int main(int argc, char **argv) {
static struct option long_options[] = {
{"path", required_argument, 0, 'q'},
{"input", required_argument, 0, 'g'},
{"parallel", required_argument, 0, 'p'},
{"help", no_argument, &help_flag, 1},
{"verbose", no_argument, &verbose_flag, 1},
{0, 0, 0, 0}};
int option_index = 0;
int c;
while (1) {
c = getopt_long(argc, argv, "q:g:p:", long_options, &option_index);
if (c == -1) break;
switch (c) {
case 'q':
if (optarg != NULL) q = atoi(optarg);
break;
case 'g':
input_graph_flag = true;
if (optarg != NULL) input_graph = optarg;
break;
case 'p':
if (optarg != NULL) thread_count = atoi(optarg);
break;
}
}
if (help_flag || argc == 1) {
print_usage(argv[0]);
return 0;
}
if (q == 0) {
printf("Invalid or missing path length value.\n");
return 1;
}
if (q > MAXK) {
printf("q to high! (max value: %d\n", MAXK);
return 1;
}
if (thread_count > 0 && (int)thread_count < omp_get_max_threads()) {
omp_set_dynamic(0);
omp_set_num_threads(thread_count);
}
if (verbose_flag) {
printf("Options:\n");
printf("Q = %d\n", q);
printf("thread = %d\n", thread_count);
printf("input_graph = %s\n", input_graph != NULL ? input_graph : "stdin");
}
if (verbose_flag) printf("Reading graph...\n");
if (input_graph_flag) {
if (input_graph == NULL) {
printf("Input file name missing!\n");
return 1;
}
int input_fd = open(input_graph, O_RDONLY, 0);
if (input_fd == -1) {
perror("Error opening input file");
return 1;
}
read(input_fd, &N, sizeof(int));
read(input_fd, &M, sizeof(int));
if (verbose_flag) printf("N = %d | M = %d\n", N, M);
label = new char[N + 1];
color = new int[N + 1];
int *intLabel = new int[N + 1];
if (verbose_flag) printf("Reading labels...\n");
read(input_fd, intLabel, N * sizeof(int));
for (unsigned int i = 0; i < N; i++) label[i] = 'A' + intLabel[i];
if (verbose_flag) printf("Reading edges...\n");
G = new vector<int>[N + 1];
int *ab = new int[2 * M];
read(input_fd, ab, 2 * M * sizeof(int));
for (unsigned int i = 0; i < M; i++) {
G[ab[2 * i]].push_back(ab[2 * i + 1]);
G[ab[2 * i + 1]].push_back(ab[2 * i]);
}
free(ab);
} else {
// Read from stdin, nme format
N = nextInt();
M = nextInt();
if (verbose_flag) printf("N = %d | M = %d\n", N, M);
label = new char[N + 1];
color = new int[N + 1];
G = new vector<int>[N + 1];
if (verbose_flag) printf("Reading labels...\n");
for (unsigned int i = 0; i < N; i++) label[i] = 'A' + nextInt();
if (verbose_flag) printf("Reading edges...\n");
for (unsigned int i = 0; i < M; i++) {
int a = nextInt();
int b = nextInt();
G[a].push_back(b);
G[b].push_back(a);
}
}
if (verbose_flag) printf("N = %d | M = %d\n", N, M);
if (verbose_flag) printf("|A| = %d | |B| = %d\n", Sa, Sb);
// Create DP Table
for (unsigned int i = 0; i <= q + 1; i++)
DP[i] = new map<COLORSET, ll>[N + 1];
// Random color graph
if (verbose_flag) printf("Random coloring graph...\n");
randomColor();
// Add fake node N connected to all the nodes
for (unsigned int i = 0; i < N; i++) {
G[N].push_back(i);
G[i].push_back(N);
}
color[N] = q;
N++;
q++;
// Fill dynamic programming table
if (verbose_flag) printf("Processing DP table...\n");
ll time_a = current_timestamp();
processDP();
ll time_b = current_timestamp() - time_a;
if (verbose_flag) printf("End processing DP table [%llu]ms\n", time_b);
ll time_dp = time_b;
// list_k_path(vector<int>(), setBit(0ll, color[N-1]), N-1);
N--;
q--;
for (unsigned int i = 0; i < N; i++) G[i].pop_back();
vector<int> sampleV;
for (unsigned int i = 0; i < N; i++) sampleV.push_back(i);
vector<int> Rsize;
// Rsize.push_back(20);
Rsize.push_back(100);
Rsize.push_back(500);
Rsize.push_back(1000);
Rsize.push_back(2000);
vector<pair<int, int>> ABsize;
// ABsize.push_back(make_pair(4,4));
ABsize.push_back(make_pair(10, 100));
vector<double> epsilonV;
epsilonV.push_back(0.2);
epsilonV.push_back(0.4);
epsilonV.push_back(0.6);
epsilonV.push_back(0.8);
dict.clear();
freqBrute.clear();
double bc_brute; // BC-BRUTE
double bc_2plus; // BC-2PLUS
double bc_base; // BC-2PLUS
double bc_alg3; // BC-2PLUS
double bc_2plus_rel; // BC-2PLUS_REL
double bc_base_rel; // BC-2PLUS_REL
double bc_alg3_rel; // BC-2PLUS_REL
double fj_brute; // FJ-BRUTE_REL
double fj_2plus; // FJ-2PLUS
double fj_base; // FJ-2PLUS
double fj_alg3; // FJ-2PLUS
double fj_2plus_rel; // FJ-2PLUS_REL
double fj_base_rel; // FJ-2PLUS_REL
double fj_alg3_rel; // FJ-2PLUS_REL
int tau_brute; // TAU
int tau_2plus; // TAU
int tau_base; // TAU
int tau_alg3; // TAU
size_t vmrss_brute = 0; // VmRSS
size_t vmrss_2plus = 0; // VmRSS
size_t vmrss_base = 0; // VmRSS
size_t vmrss_alg3 = 0; // VmRSS
ll time_brute = 0ll; // TIME
ll time_2plus = 0ll; // TIME
ll time_base = 0ll; // TIME
ll time_alg3 = 0ll; // TIME
srand(42);
for(double epsilon : epsilonV)
for (pair<int, int> ABs : ABsize) {
random_shuffle(sampleV.begin(), sampleV.end());
set<int> A = set<int>(sampleV.begin(), sampleV.begin() + ABs.first);
random_shuffle(sampleV.begin(), sampleV.end());
set<int> B = set<int>(sampleV.begin(), sampleV.begin() + ABs.second);
vector<int> X;
for (int a : A) X.push_back(a);
for (int b : B) X.push_back(b);
set<int> AB;
for (int a : A) AB.insert(a);
for (int b : B) AB.insert(b);
vector<int> ABv = vector<int>(AB.begin(), AB.end());
int R = 0;
long long PAB = 0;
for(int x : AB)
for(auto y : DP[q][x])
PAB += y.second;
R = log((double)PAB)/(epsilon*epsilon);
for(int exp = 0 ; exp < 50 ; exp++)
{
map<string, ll> freqA, freqB;
set<string> W;
double bcw, fjw;
long long Rp = 0ll;
// Brute force
// printf("\t[bruteforce]\n");
dict.clear();
freqA.clear();
freqB.clear();
freqBrute.clear();
vmrss_brute = getCurrentRSS();
time_brute = current_timestamp();
#pragma omp parallel for schedule(static, 1)
for (size_t i = 0; i < ABv.size(); i++) {
int tid = omp_get_thread_num();
dfs(tid, ABv[i], q - 1);
}
double realBC, realFJ;
for (auto w : freqBrute) {
int u = w.first.first;
string s = w.first.second;
ll freq = w.second;
if (A.find(u) != A.end()) {
Rp += freq;
freqA[s] += freq;
}
if (B.find(u) != B.end()) {
Rp += freq;
freqB[s] += freq;
}
}
time_brute = current_timestamp() - time_brute;
tau_brute = dict.size();
bc_brute = realBC = bcw = BCW(dict, freqA, freqB);
// printf("\t\tBRUTEFORCE BCW(A,B) \t\t= %.6f\n", bcw);
fj_brute = realFJ = fjw = FJW(dict, freqA, freqB, Rp);
// printf("\t\tBRUTEFORCE FjW(A,B) \t\t= %.6f\n", fjw);
vmrss_brute = getCurrentRSS() - vmrss_brute;
// Base line
// printf("\t[baseline]\n");
freqA.clear();
freqB.clear();
vmrss_base = getCurrentRSS();
time_base = current_timestamp();
map<pair<int, string>, ll> BLsampling = baselineSampler(X, R);
for (auto w : BLsampling) {
int u = w.first.first;
W.insert(w.first.second);
if (A.find(u) != A.end()) freqA[w.first.second] += w.second;
if (B.find(u) != B.end()) freqB[w.first.second] += w.second;
}
time_base = current_timestamp() - time_base;
bc_base = bcw = BCW(W, freqA, freqB);
bc_base_rel = abs(bcw - realBC) / realBC;
// printf("\t\tBASELINE BCW(A,B) \t\t= %.6f \t%.6f\n", bcw, rel);
tau_base = W.size();
fj_base = fjw = FJW(W, freqA, freqB, (long long)R);
fj_base_rel = abs(fjw - realFJ) / realFJ;
// printf("\t\tBASELINE FJW(A,B) \t\t= %.6f \t%.6f\n", fjw, rel);
vmrss_base = getCurrentRSS() - vmrss_base;
// ColorfulSampler OLD
// printf("\t[ColorfulSampler]\n");
time_alg3 = current_timestamp();
vmrss_alg3 = getCurrentRSS();
set<string> Sample = randomColorfulSample(X, R);
freqA = processFrequency(Sample, multiset<int>(A.begin(), A.end()));
freqB = processFrequency(Sample, multiset<int>(B.begin(), B.end()));
time_alg3 = current_timestamp() - time_alg3;
tau_alg3 = Sample.size();
bc_alg3 = bcw = BCW(Sample, freqA, freqB);
bc_alg3_rel = abs(bcw - realBC) / realBC;
// printf("\t\tCOLORFULSAMPLER BCW(A,B) \t= %.6f \t%.6f\n", bcw, rel);
Sample = randomColorfulSample(vector<int>(AB.begin(), AB.end()), R);
freqA = processFrequency(Sample, multiset<int>(A.begin(), A.end()));
freqB = processFrequency(Sample, multiset<int>(B.begin(), B.end()));
Rp = 0;
for (auto a : freqA) Rp += a.second;
for (auto b : freqB) Rp += b.second;
fj_alg3 = fjw = FJW(Sample, freqA, freqB, Rp);
fj_alg3_rel = abs(fjw - realFJ) / realFJ;
vmrss_alg3 = getCurrentRSS() - vmrss_alg3;
freqA.clear();
freqB.clear();
W.clear();
time_2plus = current_timestamp();
vmrss_2plus = getCurrentRSS();
map<pair<int, string>, ll> SamplePlus = randomColorfulSamplePlus(X, R);
for (auto w : SamplePlus) W.insert(w.first.second);
for (auto w : SamplePlus) {
int u = w.first.first;
if (A.find(u) != A.end()) {
freqA[w.first.second] += w.second;
}
if (B.find(u) != B.end()) {
freqB[w.first.second] += w.second;
}
}
time_2plus = current_timestamp() - time_2plus;
tau_2plus = W.size();
bc_2plus = bcw = BCW(W, freqA, freqB, R);
bc_2plus_rel = abs(bcw - realBC) / realBC;
// printf("\t\tCOLORFULSAMPLERPLUS BCW(A,B) \t= %.6f \t%.6f\n", bcw, rel);
SamplePlus = randomColorfulSamplePlus(vector<int>(AB.begin(), AB.end()), R);
W.clear();
for (auto w : SamplePlus) W.insert(w.first.second);
freqA.clear();
freqB.clear();
for (auto w : SamplePlus) {
int u = w.first.first;
if (A.find(u) != A.end()) {
freqA[w.first.second] += w.second;
}
if (B.find(u) != B.end()) {
freqB[w.first.second] += w.second;
}
}
fj_2plus = fjw = FJW(W, freqA, freqB, (long long)R);
fj_2plus_rel = abs(fjw - realFJ) / realFJ;
vmrss_2plus = getCurrentRSS() - vmrss_2plus;
printf("%.1f,", epsilon); // Q
printf("%2d,", q); // Q
printf("%4d,", R); // R
printf("%4zu,", A.size()); // HA
printf("%4zu,", B.size()); // HB
printf("%.6f,", bc_brute); // BC-BRUTE
printf("%.6f,", fj_brute); // FJ-BRUTE
printf("%4d,", tau_brute); // TAU
printf("%2zu,", (size_t)0); // VmRSS
printf("%4llu,", time_brute); // TIME
printf("%.6f,", bc_2plus); // BC-2PLUS
printf("%.6f,", bc_2plus_rel); // BC-2PLUS
printf("%.6f,", fj_2plus); // FJ-2PLUS
printf("%.6f,", fj_2plus_rel); // FJ-2PLUS
printf("%4d,", tau_2plus); // TAU
printf("%2zu,", (size_t)0); // VmRSS
printf("%4llu,", time_dp + time_2plus); // TIME
printf("%.6f,", bc_base); // BC-BASE
printf("%.6f,", bc_base_rel); // BC-BASE
printf("%.6f,", fj_base); // FJ-BASE
printf("%.6f,", fj_base_rel); // FJ-BASE
printf("%4d,", tau_base); // TAU
printf("%2zu,", (size_t)0); // VmRSS
printf("%4llu,", time_base); // TIME
printf("%.6f,", bc_alg3); // BC-ALG3
printf("%.6f,", bc_alg3_rel); // BC-ALG3
printf("%.6f,", fj_alg3); // FJ-ALG3
printf("%.6f,", fj_alg3_rel); // FJ-ALG3
printf("%4d,", tau_alg3); // TAU
printf("%2zu,", (size_t)0); // VmRSS
printf("%4llu", time_dp + time_alg3); // TIME
printf("\n");
}
}
return 0;
}