-
Notifications
You must be signed in to change notification settings - Fork 0
/
final_v2.cpp
723 lines (572 loc) · 15.5 KB
/
final_v2.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
// #define TEST
#define MMAP
#include <stdio.h>
#include <string.h>
#include <bits/stdc++.h>
#include <queue>
#include <limits.h>
#include <sys/time.h>
#ifdef MMAP
#include <sys/mman.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/stat.h>
#endif
#include <ext/pb_ds/hash_policy.hpp>
#include <ext/pb_ds/assoc_container.hpp>
#include <thread>
#include <mutex>
using namespace std;
typedef unsigned int Uint;
typedef unsigned int ui;
const int MAX_EDGE_NUM = 2500000;
const int MAX_NODE_NUM = 2500000;
const int THREAD_NUM = 12;
const int TOP_NUM = 100; // TopN
int edge_num = 0, node_num = 0, u_num = 0, v_num = 0;
// u -[w]-> v
Uint U[MAX_EDGE_NUM];
Uint V[MAX_EDGE_NUM];
Uint W[MAX_EDGE_NUM];
Uint new_U[MAX_EDGE_NUM];
Uint new_V[MAX_EDGE_NUM];
Uint new_W[MAX_EDGE_NUM];
Uint U_temp[MAX_EDGE_NUM]; // 相当于题目中的 S 集合
Uint V_temp[MAX_EDGE_NUM]; // 相当于题目中的 T 集合
Uint id[MAX_EDGE_NUM * 2]; // 排序 + 去重
Uint new_id[MAX_NODE_NUM * 2]; //之后的新映射
Uint outDegree[MAX_EDGE_NUM];
Uint inDegree[MAX_EDGE_NUM];
Uint newOutDegree[MAX_EDGE_NUM];
Uint newInDegree[MAX_EDGE_NUM];
//链式前向星中的 head[], edge[].next;
Uint U_head[MAX_EDGE_NUM];
Uint V_head[MAX_EDGE_NUM];
Uint U_next[MAX_EDGE_NUM];
Uint V_next[MAX_EDGE_NUM];
Uint new_U_head[MAX_EDGE_NUM];
Uint new_V_head[MAX_EDGE_NUM];
Uint new_U_next[MAX_EDGE_NUM];
Uint new_V_next[MAX_EDGE_NUM];
Uint topo_Head[MAX_NODE_NUM];
Uint del_node[MAX_NODE_NUM];
bool del_flag[MAX_NODE_NUM];
Uint topo_pred_info[MAX_NODE_NUM][2];
Uint del_pred_num[MAX_NODE_NUM];
bool vis[MAX_NODE_NUM];
Uint cur_s = 0; // 多线程 任务片划分 当前最大分配的 s
mutex s_lock;
double CB[MAX_NODE_NUM]; //保存最终的 CB(i)
// 线下测试速度 gp_hash_table 比 unordered_map 快
__gnu_pbds::gp_hash_table<Uint, int> id_hash;
__gnu_pbds::gp_hash_table<Uint, int> new_id_hash;
Uint graph_suc[MAX_EDGE_NUM][2]; // id2 | distance
struct HashInfo
{
Uint id;
Uint hash_id;
inline HashInfo(Uint id, Uint hash_id) : id(id), hash_id(hash_id) {}
};
struct Q_queue
{
Uint node;
Uint distance;
Q_queue() {}
Q_queue(Uint node, Uint distance) : node(node), distance(distance) {}
bool operator<(const Q_queue &rhs) const
{
return distance > rhs.distance;
}
};
struct ThreadInfo
{
Uint S[MAX_NODE_NUM]; // 用数组模拟 paper 伪代码中的 stack
Uint d[MAX_NODE_NUM];
Uint sigma[MAX_NODE_NUM];
priority_queue<Q_queue> Q; //最小堆
// double delta[MAX_NODE_NUM];
// double partCB[MAX_NODE_NUM]; // 保存该线程下计算所得的 部分 CB(i)
double delta_cb[MAX_NODE_NUM][2];
Uint pred_next_ptr[MAX_NODE_NUM];
Uint pred_info[MAX_NODE_NUM];
}TInfo[THREAD_NUM];
struct Result
{
Uint node;
double cb;
Result() {}
Result(Uint node, double cb) : node(node), cb(cb) {}
bool operator<(const Result &rhs) const
{
// if (abs(cb - rhs.cb) > 0.0001)
// return cb > rhs.cb;
// else
// return new_id[id] < new_id[rhs.id];
return (abs(cb - rhs.cb) > 1e-4) ? cb > rhs.cb : new_id[node] < new_id[rhs.node];
}
};
#ifdef TEST
// 计时器
struct Time_recorder
{
vector<pair<string, int>> logs;
struct timeval start_time;
struct timeval end_time;
void setTime()
{
gettimeofday(&start_time, NULL);
}
int getElapsedTimeMS()
{
gettimeofday(&end_time, NULL);
return int(1000 * (end_time.tv_sec - start_time.tv_sec) + (end_time.tv_usec - start_time.tv_usec) / 1000);
}
void logTimeImpl(string tag, int elapsedTimeMS)
{
printf("Time consumed for %-20s is %d ms.\n", tag.c_str(), elapsedTimeMS);
}
void logTime(string tag)
{
logTimeImpl(tag, getElapsedTimeMS());
}
void resetTimeWithTag(string tag)
{
logs.emplace_back(make_pair(tag, getElapsedTimeMS()));
setTime();
}
void printLogs()
{
for (auto x : logs)
logTimeImpl(x.first, x.second);
}
};
#endif
#ifdef MMAP
void readDataWithMmap(char* inputFile)
{
struct stat stat_buf;
int fd = open(inputFile, O_RDONLY);
// fseek稍微慢一点
fstat(fd, &stat_buf);
register long length = stat_buf.st_size;
char *mbuf = (char *)mmap(NULL, length, PROT_READ, MAP_PRIVATE, fd, 0);
register Uint num = 0;
register int flag = 0;
register long index = 0;
register char ch;
while(index < length)
{
ch = *(mbuf + index);
if(ch >= '0')
{
num = num * 10 + ch - '0';
}
else if(ch != '\n')
{
switch (flag)
{
case 0:
U[edge_num] = num;
flag = 1, num = 0;
break;
case 1:
V[edge_num] = num;
flag = 2, num = 0;
break;
case 2:
W[edge_num++] = num;
flag = 0, num = 0;
break;
}
}
++index;
}
close(fd);
munmap(mbuf, length);
}
#endif
void dfs(Uint cur_id, Uint depth, Uint num, Uint tid)
{
Uint pred_id, pred_num;
auto &delta_cb = TInfo[tid].delta_cb;
for (Uint i = topo_Head[cur_id]; i != 0; i = topo_pred_info[i - 1][1])
{
pred_id = topo_pred_info[i - 1][0];
pred_num = del_pred_num[pred_id] + 1;
// TInfo[tid].partCB[cur_id] += pred_num * (num + depth);
delta_cb[cur_id][1] += pred_num * (num + depth);
if (pred_num > 1)
dfs(pred_id, depth + 1, num, tid);
}
}
//paper : A Faster Algorithm for Betweenness Centrality
void dijkstra_heap_opt(Uint s, Uint tid)
{
//不用每次都写 TInfo[tid]. 了
auto &d = TInfo[tid].d;
auto &sigma = TInfo[tid].sigma;
auto &pred_next_ptr = TInfo[tid].pred_next_ptr;
auto &Q = TInfo[tid].Q;
auto &S = TInfo[tid].S;
auto &pred_info = TInfo[tid].pred_info;
auto &delta_cb = TInfo[tid].delta_cb;
// auto &partCB = TInfo[tid].partCB;
// auto &delta = TInfo[tid].delta;
int s_index = -1; // 记录 Stack S
Uint multiple, pred_info_len = 0;
double coeff;
Uint v, w;
Uint v_dis;
Uint j_begin, j_end; //j 变量图的指针 范围[ j_begin, j_end)
d[s] = 0;
sigma[s] = 1;
Q.emplace(Q_queue(s, 0));
while (!Q.empty())
{
v_dis = Q.top().distance;
v = Q.top().node;
Q.pop();
if (v_dis > d[v])
continue;
S[++s_index] = v;
// delta[v] = 0;
delta_cb[v][0] = 0;
j_begin = new_U_head[v];
j_end = j_begin + newOutDegree[v];
while (j_begin < j_end)
{
w = graph_suc[j_begin][0];
if (d[v] + graph_suc[j_begin][1] == d[w])
{
sigma[w] += sigma[v];
pred_info[pred_next_ptr[w]++] = v;
}
else if (d[v] + graph_suc[j_begin][1] < d[w])
{
d[w] = d[v] + graph_suc[j_begin][1];
Q.emplace(Q_queue(w, d[w]));
sigma[w] = sigma[v];
pred_next_ptr[w] = new_V_head[w];
pred_info[pred_next_ptr[w]++] = v;
}
++j_begin;
}
}
multiple = del_pred_num[s] + 1; //计算删除前驱数量
dfs(s, 0, s_index, tid); //去计算这些删掉节点的 spartCB
while (s_index > 0)
{
w = S[s_index--];
// partCB[w] += delta[w] * multiple;
delta_cb[w][1] += delta_cb[w][0] * multiple;
d[w] = -1u;
coeff = (1 + delta_cb[w][0]) / sigma[w];
j_begin = new_V_head[w];
j_end = pred_next_ptr[w];
while (j_begin < j_end)
{
v = pred_info[j_begin++];
delta_cb[v][0] += sigma[v] * coeff;
}
}
d[s] = -1u;
}
void thread_process(Uint tid)
{
auto &d = TInfo[tid].d;
for (Uint i = 0; i < node_num; ++i)
{
d[i] = -1u;
}
Uint s;
while (true)
{
s_lock.lock();
if (cur_s < node_num)
{
while (del_flag[cur_s] && cur_s < node_num)
cur_s++;
if (cur_s < node_num)
s = cur_s++;
else
{
s_lock.unlock();
break;
}
s_lock.unlock();
dijkstra_heap_opt(s, tid);
}
else
{
s_lock.unlock();
break;
}
}
}
void saveResult(char *outputFile)
{
for (Uint i = 0; i < THREAD_NUM; ++i)
{
for (Uint j = 0; j < node_num; ++j)
{
// CB[j] += TInfo[i].partCB[j];
CB[j] += TInfo[i].delta_cb[j][1];
}
}
int i = 0;
priority_queue<Result> res;
while (i < TOP_NUM) //放入TOP_NUM数量的结果,如果如果来的结果比 top()值大,则交换
{
res.emplace(Result(i, CB[i]));
++i;
}
while (i < node_num)
{
// if (CB[index] > res.top().score && abs(CB[index] - res.top().score) > 0.0001)
if(CB[i] - res.top().cb > 0.0001)
{
res.pop();
res.emplace(Result(i, CB[i]));
}
++i;
}
FILE *fp = fopen(outputFile, "w");
Result result[TOP_NUM];
i = 99;
while (i >= 0)
{
result[i] = res.top();
res.pop();
i--;
}
i = 0;
while (i < 100)
{
fprintf(fp, "%u,%.3lf\n", id[new_id[result[i].node]], result[i].cb);
++i;
}
fclose(fp);
}
void sortAndUnique_u()
{
memcpy(U_temp, U, sizeof(U));
sort(U_temp, U_temp + edge_num);
u_num = unique(U_temp, U_temp + edge_num) - U_temp;
}
void sortAndUnique_v()
{
memcpy(V_temp, V, sizeof(V));
sort(V_temp, V_temp + edge_num);
v_num = unique(V_temp, V_temp + edge_num) - V_temp;
}
void mergeId()
{
register int i = 0, j = 0;
while(i < u_num && j < v_num)
{
if(U_temp[i] < V_temp[j])
{
id[node_num++] = U_temp[i++];
}
else if(U_temp[i] == V_temp[j])
{
id[node_num++] = U_temp[i++];
j++;
}
else
{
id[node_num++] = V_temp[j++];
}
}
while(i < u_num)
{
id[node_num++] = U_temp[i++];
}
while(j < v_num)
{
id[node_num++] = V_temp[j++];
}
}
int main(int argc, char *argv[])
{
#ifdef TEST
Time_recorder timeA;
string dataSet = "DataSet/";
string testData = "/test_data.txt";
string myAns = "/myAnswer.txt";
string input = dataSet + string(argv[1]) + testData;
string output = dataSet + string(argv[1]) + myAns;
char inputFile[100];
char outputFile[100];
strcpy(inputFile, input.c_str());
strcpy(outputFile, output.c_str());
#else
char inputFile[] = "/data/test_data.txt";
char outputFile[] = "/projects/student/result.txt";
#endif
#ifdef TEST
timeA.setTime();
#endif
readDataWithMmap(inputFile);
#ifdef TEST
timeA.resetTimeWithTag("Read Data");
#endif
// 多线程 对 U, V排序去重,并合并到 id 中
thread sortU(sortAndUnique_u);
thread sortV(sortAndUnique_v);
sortU.join();
sortV.join();
mergeId();
#ifdef TEST
timeA.resetTimeWithTag("U,V Sort、Unique and Conquer");
#endif
register int i;
// hash 顶点与 index 的映射
for(i = 0; i < node_num; ++i)
{
id_hash[id[i]] = i;
}
Uint u, v;
Uint index = 0;
while (index < edge_num)
{
// 将原来的顶点 换成 映射 index
u = U[index] = id_hash[U[index]];
v = V[index] = id_hash[V[index]];
// 链式前向星
U_next[index] = U_head[u];
U_head[u] = ++index;
//出入度
outDegree[u]++;
inDegree[v]++;
}
//=============================================================================
Uint cur_id = 0, cur_hash_id, next_id, hash_index = 0, u_hash_index;
//删除点前需要重新映射
i = 0;
queue<HashInfo> q;
while(true)
{
if(i == node_num)
break;
while (i < node_num && vis[i])
{
i++;
}
vis[i] = true;
q.push(HashInfo(i, hash_index));
hash_index++;
while (!q.empty())
{
HashInfo hashdata = q.front();
cur_id = hashdata.id;
cur_hash_id = hashdata.hash_id;
new_id_hash[cur_id] = cur_hash_id;
new_id[cur_hash_id] = cur_id;
u_hash_index = cur_hash_id;
q.pop();
for (Uint next_index = U_head[cur_id]; next_index != 0; next_index = U_next[next_index - 1])
{
next_id = V[next_index - 1];
if (!vis[next_id])
{
new_U[next_index - 1] = u_hash_index;
new_V[next_index - 1] = hash_index;
vis[next_id] = true;
new_id[hash_index] = next_id;
new_id_hash[next_id] = hash_index;
q.push(HashInfo(next_id, hash_index));
hash_index++;
}
else
{
new_U[next_index - 1] = u_hash_index;
new_V[next_index - 1] = new_id_hash[next_id];
}
}
}
}
for (i = 0; i < node_num; i++)
{
new_U_head[new_id_hash[i]] = U_head[i];
new_V_head[new_id_hash[i]] = V_head[i];
newOutDegree[new_id_hash[i]] = outDegree[i];
newInDegree[new_id_hash[i]] = inDegree[i];
}
#ifdef TEST
timeA.resetTimeWithTag("New Hash Id");
#endif
// “删除” 入度为0,出度为1的点
int topo_index = -1;
for (i = 0; i < node_num; ++i)
{
if (newInDegree[i] == 0 && newOutDegree[i] == 1)
{
del_node[++topo_index] = i;
del_flag[i] = true;
}
}
Uint count = 0;
while (topo_index >= 0)
{
u = del_node[topo_index--];
i = new_U_head[u];
v = new_V[i - 1];
topo_pred_info[count][0] = u;
topo_pred_info[count][1] = topo_Head[v];
topo_Head[v] = ++count;
del_pred_num[v] += del_pred_num[u] + 1; //记录 v 删掉的前驱顶点的数量
--newOutDegree[u];
--newInDegree[v];
//递归删除 所有满足入度为0,出度为1的点
if (newInDegree[v] == 0 && newOutDegree[v] == 1)
{
del_node[++topo_index] = v;
del_flag[v] = true;
}
}
#ifdef TEST
timeA.resetTimeWithTag("Del 1/0 Node ");
#endif
Uint j, suc_edge_num = 0;
for(i = 0; i < node_num; ++i)
{
if(del_flag[i] == false)
{
for(j = new_U_head[i], new_U_head[i] = suc_edge_num; j != 0; j = U_next[j - 1])
{
graph_suc[suc_edge_num][0] = new_V[j - 1];
graph_suc[suc_edge_num++][1] = W[j - 1];
}
}
}
new_U_head[i] = suc_edge_num;
Uint pre_index = 0;
for(i = 0; i < node_num; ++i)
{
new_V_head[i] = pre_index;
pre_index += newInDegree[i];
}
#ifdef TEST
timeA.resetTimeWithTag("Create Graph and Pre Index");
#endif
// 多线程 计算 CB(i)
thread threads[THREAD_NUM];
for(i = 0; i < THREAD_NUM; ++i)
{
threads[i] = thread(thread_process, i);
}
for(auto &t : threads)
{
t.join();
}
#ifdef TEST
timeA.resetTimeWithTag("Threads Compute CB");
#endif
saveResult(outputFile);
#ifdef TEST
timeA.resetTimeWithTag("Save Result");
timeA.printLogs();
#endif
return 0;
}