forked from CGCL-codes/DGraph
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlpa.cpp
More file actions
80 lines (66 loc) · 1.9 KB
/
lpa.cpp
File metadata and controls
80 lines (66 loc) · 1.9 KB
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
/*
* Community Detection algorithm: LPA (Label Propagation Algorithm)
* This algorithm had better be asynchronous, synchronous may not to converge.
*/
#include <iostream>
#include <string>
#include <cstdlib>
#include <cmath>
#include <map>
#include "api.h"
using namespace std;
template <class App>
class LPAApp : public App
{
public:
LPAApp(string dir, string value_dir): App(dir, string("cc_value.bin"), value_dir){};
int min(int a, int b){return a < b ? a : b;}
void init(ID id, typename App::value_t &value)
{
value = (int)(id);
}
bool update(ID virtual_id, typename App::bor_t inbor, typename App::bor_t outbor, bor_cnt_t in_cnt, bor_cnt_t out_cnt,
const typename App::values_t rvalue, typename App::values_t wvalue)
{
int max_cnt = 0;
ID max_id = rvalue[virtual_id];
map<ID, int> lcnt;
for(int i = 0; i < in_cnt; i++)
{
ID id = rvalue[inbor[i]];
lcnt[id] = lcnt[id] + 1;
if(max_cnt < lcnt[id] || (max_cnt == lcnt[id] && id > max_id))
{
max_cnt = lcnt[id];
max_id = id;
}
}
//cout << App::map_nto[virtual_id] << ": "<< wvalue[virtual_id] << "-" << max_id << endl;
bool is_cvg = (wvalue[virtual_id] == max_id);
wvalue[virtual_id] = max_id;
return is_cvg;
}
};
int main(int argc, char *argv[])
{
if (argc != 3 && argc != 4)
{
cout << "Usage ./lpa <data dir> <generate value to dir> <print top(default 10)>" << endl;
return 0;
}
int top_num = (argc == 4 ? top_num = atoi(argv[3]) : 10) ;
string dir(argv[1]), to_dir(argv[2]);
cout << "normal para run" << endl; get_time(false);
LPAApp<UnSccDagApp<ID>> ulpa(dir, to_dir);
ulpa.reset(false);
ulpa.para_run();
get_time(); cout << endl;
ulpa.print_top(top_num);
cout << "para run" << endl; get_time(false);
LPAApp<SccDagApp<ID>> lpa(dir, to_dir);
lpa.reset(false);
lpa.para_run();
get_time(); cout << endl;
lpa.print_top(top_num);
return 0;
}