-
Notifications
You must be signed in to change notification settings - Fork 0
/
k-labeled-dpl-tau.cpp
451 lines (376 loc) · 10.9 KB
/
k-labeled-dpl-tau.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
/*
Author: Gaspare Ferraro
Count the frequency of labels sequency of length k
start/finish in every node using color-coding technique (parallel version)
(by Andrea Marino)
h: V -> [n] // etichetta
path P = v1, ..., vr
label H(P) = h(v1), ..., h(vr)
t € [n]^k (stringa casuale)
Create empty dictionaries dict[i][u] and freq[u]
freq[N] path -> freq
dict[k][N] path -> <colore, h, label>
for u in V:
dict[0][u].put(<u, color(u), H(u), label(u)>)
< path, color(path), H(path), label(P) >
for i in [k]:
for u in [V]:
for v in N(u):
for path in dict[i-1][v] such that color(u) NOT IN color(path) and H(path+u) <= t:
f =freq[v].lookup(label(path))
f'=freq[u].lookup(label(path+.u))
freq[u].put(label(path+u), f+f')
dict[i][u].put(path+u)
//Note that \leq_L is the lexicographic order and a.b means the concatenation between a and b, \chi(u) is the color of u
Sketches[u]=freq[u].set() //Skecthes[u] are the pairs <\pi,f> in freq[u]
*/
#include <vector>
#include <set>
#include <map>
#include <string>
#include <unordered_set>
#include <unordered_map>
#include <stdio.h>
#include <sys/stat.h>
#include <string.h>
#include <fcntl.h>
#include <omp.h>
#include <getopt.h>
#include <unistd.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
using namespace std;
typedef long long ll;
unsigned int N, M;
unsigned int k = 0, kp = 0;
int x = -1;
unsigned int thread_count = 0;
static int verbose_flag, help_flag;
ll cont = 0;
char *labels;
int *color;
vector<int> *G;
inline int nextInt() {
int r;
scanf("%d", &r);
return r;
}
// Get pos-th bit in n
inline bool getBit(COLORSET n, int pos) { return ((n >> pos) & 1) == 1; }
// Set pos-th bit in n
inline COLORSET setBit(COLORSET n, int pos) { return n |= 1 << pos; }
// Reset pos-th bit in n
inline COLORSET clearBit(COLORSET n, int pos) { return n &= ~(1 << pos); }
// Random coloring graph using kp color
inline void randomColor() {
for (unsigned int i = 0; i < N; i++) color[i] = rand() % kp;
}
// Hashing function
vector<int> tau;
int p1 = 23;
int p2 = 29;
inline int h(int x)
{
return (p1*x+p2) % N;
}
vector<int> H(vector<int> X)
{
vector<int> ret;
for(auto x : X) ret.push_back(h(x));
return ret;
}
string L(vector<int> p)
{
string ret = "";
for(int x : p) ret += labels[x];
return ret;
}
// Dynamic programming processing
map< vector<int>, tuple<COLORSET, vector<int>, string> > *DP[MAXK+1];
map< vector<int>, ll > *freq;
void processDP() {
if( verbose_flag ) printf("K = %d\n", 0);
for(unsigned int u=0; u<N; u++)
{
vector<int> vu;
vu.push_back(u);
DP[1][u][vu] = make_tuple( setBit(0, color[u]), H(vu), string(&labels[u],1));
freq[u][vu] = 1;
}
for(unsigned int i=2; i<=k; i++)
{
if( verbose_flag ) printf("K = %d\n", i);
#pragma omp parallel for
for(unsigned int u=0; u<N; u++)
{
for(auto v : G[u])
{
for(auto e : DP[i-1][v])
{
vector<int> p = e.first;
COLORSET c = get<0>(e.second);
vector<int> h = get<1>(e.second);
string l = get<2>(e.second);
if( getBit(c, color[u]) ) continue;
vector<int> pu = vector<int>(p);
pu.push_back(u);
if( H(pu) > tau ) continue;
COLORSET cu = setBit(c, color[u]);
vector<int> hu = H(pu);
string lu = string(l);
lu += labels[u];
ll f = freq[v][p];
ll fp = freq[u][pu];
freq[u][pu] = f+fp;
DP[i][u][pu] = make_tuple(cu, hu, lu);
}
}
}
}
}
void print_usage(char *filename) {
// printf("Usage: ./%s -k length -K number -g filename -f format -t filename
// -T filename -p threadcount -h -v\n",filename);
printf(
"Usage: ./%s -k length -K number -g filename -p threadcount -n index"
"--help --verbose\n",
filename);
printf("Valid arguments:\n");
printf("-k, --path length\n");
printf("\tLength of the path.\n");
printf("-K, --color number\n");
printf("\tNumber of colors to use (default path length).\n");
printf("-g, --input filename\n");
printf("\tInput file of graph in .nme.bin format (default stdin)\n");
/*printf("-f, --format format\n");
printf("\tFormat of input file (snap, nde, nme)\n");
printf("-t, --tableout filename\n");
printf("\tOutput DP table (default stdout)\n");
printf("-T, --tablein filename\n");
printf("\tImport DP table (default stdin)\n");
printf("-l, --list filename\n");
printf("\tList k-path (default stdout)\n");
*/
printf("-t, --tau string\n");
printf("\tK integer value [0,N-1] joined by a '-' (e.g. 3-2-7-1)\n");
printf("-p, --parallel threadcount\n");
printf("\tNumber of threads to use (default maximum thread avaiable)\n");
printf("-n, --node index\n");
printf("\tIndex of the node to print sequence and frequency\n");
printf("--help\n");
printf("\tDisplay help text and exit.\n");
printf("--verbose\n");
printf("\tPrint status messages.\n");
}
bool input_graph_flag = false;
char *input_graph = NULL;
/*
bool table_in_flag = false;
char *table_in = NULL;
bool table_out_flag = false;
char *table_out = NULL;
bool list_path_flag = false;
char *list_path = NULL;
bool format_name_flag = false;
char *format_name = NULL;
*/
int main(int argc, char **argv) {
static struct option long_options[] = {
{"path", required_argument, 0, 'k'},
{"color", required_argument, 0, 'K'},
{"input", required_argument, 0, 'g'},
//{"format", required_argument, 0, 'f'},
// {"tableout",required_argument, 0, 't'},
// {"tablein", required_argument, 0, 'T'},
//{"list", required_argument, 0, 'l'},
{"tau", required_argument, 0, 't'},
{"parallel", required_argument, 0, 'p'},
{"node", required_argument, 0, 'n'},
{"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, "k:K:g:f:t:T:l:p:", long_options,
// &option_index);
//c = getopt_long(argc, argv, "k:K:g:f:l:p:", long_options, &option_index);
c = getopt_long(argc, argv, "k:K:g:p:n:t:", long_options, &option_index);
if (c == -1) break;
switch (c) {
case 'k':
if (optarg != NULL) k = atoi(optarg);
break;
case 'K':
if (optarg != NULL) kp = atoi(optarg);
break;
case 'n':
if (optarg != NULL) x = atoi(optarg);
break;
case 'g':
input_graph_flag = true;
if (optarg != NULL) input_graph = optarg;
break;
case 't':
if (optarg != NULL)
{
char *pch;
pch = strtok (optarg,"-");
while (pch != NULL)
{
tau.push_back(atoi(pch));
pch = strtok (NULL, "-");
}
}
break;
/*case 'f':
format_name_flag = true;
if (optarg != NULL) format_name = optarg;
break;
case 't':
table_in_flag = true;
if( optarg != NULL ) table_in = optarg;
break;
case 'T':
table_out_flag = true;
if( optarg != NULL ) table_out = optarg;
break;
case 'l':
list_path_flag = true;
if (optarg != NULL) list_path = optarg;
break;*/
case 'p':
if (optarg != NULL) thread_count = atoi(optarg);
break;
}
}
if (help_flag || argc == 1) {
print_usage(argv[0]);
return 0;
}
if (k == 0) {
printf("Invalid or missing path length value.\n");
return 1;
}
if (k > MAXK || kp > MAXK) {
printf("k or kp to high! (max value: %d\n", MAXK);
return 1;
}
if (kp == 0) kp = k;
if (thread_count > 0 && (int)thread_count < omp_get_max_threads()) {
omp_set_dynamic(0);
omp_set_num_threads(thread_count);
}
if( tau.size() != k )
{
printf("Invalid length of tau %zu instead of %d!\n", tau.size(), k);
return 1;
}
if (verbose_flag) {
printf("Options:\n");
printf("k = %d\n", k);
printf("kp = %d\n", kp);
printf("thread = %d\n", thread_count);
printf("input_graph = %s\n", input_graph != NULL ? input_graph : "stdin");
//printf("format_name = %s\n", format_name != NULL ? format_name : "stdin");
// printf("table_in = %s\n", table_in != NULL ? table_in :
// "stdin");
// printf("table_out = %s\n", table_out != NULL ? table_out :
// "stdin");
//printf("list_path = %s\n", list_path != NULL ? list_path : "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));
color = new int[N];
labels = new char[N];
G = new vector<int>[N];
read(input_fd, labels, N*sizeof(char));
for(unsigned int i=0; i<N; i++)
labels[i] += 'A';
int ab[2];
for (unsigned int i = 0; i < M; i++) {
read(input_fd, ab, 2 * sizeof(int));
G[ab[0]].push_back(ab[1]);
G[ab[1]].push_back(ab[0]);
}
} else {
// Read from stdin, nme format
N = nextInt();
M = nextInt();
color = new int[N];
labels = new char[N];
G = new vector<int>[N];
for(unsigned int i = 0; i<N; i++)
labels[i] = 'A'+nextInt();
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( x >= (int) N )
{
x = -1;
if( verbose_flag ) printf("Invalid node index, it will be ignored!\n");
}
// Create DP Table
for (unsigned int i = 0; i <= k; i++)
DP[i] = new map< vector<int>, tuple<COLORSET, vector<int>, string> >[N];
freq = new map<vector<int>, ll>[N];
// Random color graph
if (verbose_flag) printf("Random coloring graph...\n");
randomColor();
// Processing DP
if (verbose_flag) printf("Processing DP table...\n");
processDP();
if (verbose_flag) printf("Finish processing DP table...\n");
/*
if (verbose_flag)
{
for(unsigned int u = 0; u<N ; u++)
{
printf("[%u]: ", u);
for(auto v : DP[k][u])
printf("(%s, %llu) ", v.first.second.c_str(), v.second);
printf("\n");
}
}
*/
if( x != -1 )
{
ll cont = 0 ;
printf("Pair<String, Frequency> from node %d:\n", x);
for(auto v : freq[x])
{
if( v.first.size() != k ) continue;
cont += v.second;
printf("\tS=%s F=%llu\n", L(v.first).c_str(), v.second);
}
if( verbose_flag ) printf("%llu k-labeled colorful-path", cont);
}
return 0;
}