-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathmain_exact.cpp
147 lines (120 loc) · 4.79 KB
/
main_exact.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
#include <fstream>
#include <iostream>
#include <random>
#include <vector>
#include <algorithm>
#include <set>
#include <map>
#include <ctime>
#include <unordered_map>
#include <unordered_set>
#include "read_data.cpp"
#include "motif_id.cpp"
using namespace std;
inline long long convert_id(int hyperedge_a, int hyperedge_b){
return hyperedge_a * (1LL << 31) + hyperedge_b;
}
int main(int argc, char *argv[])
{
clock_t start;
clock_t run_start;
int progress;
string graphFile = "dblp_graph.txt";
// Read data
start = clock();
vector< vector<int> > node2hyperedge;
vector< vector<int> > hyperedge2node;
vector< unordered_set<int> > hyperedge2node_set;
read_data(graphFile, node2hyperedge, hyperedge2node, hyperedge2node_set);
int V = node2hyperedge.size(), E = hyperedge2node.size();
cout << "# of nodes: " << V << '\n';
cout << "# of hyperedges: " << E << '\n';
cout << "Reading data done: "
<< (double)(clock() - start) / CLOCKS_PER_SEC << " sec" << endl;
cout << "------------------------------------------" << endl << endl;
// Make adjacency list
start = clock(); run_start = clock();
hyperedge2node.resize(E); hyperedge2node_set.resize(E);
vector< vector<pair<int, int> > > hyperedge_adj;
vector< unordered_map<int, int> > hyperedge_inter;
hyperedge_adj.resize(E);
hyperedge_inter.resize(E);
vector<long long> upd_time(E, -1LL);
for (int hyperedge_a = 0; hyperedge_a < E; hyperedge_a++){
long long l_hyperedge_a = (long long)hyperedge_a;
for (const int &node: hyperedge2node[hyperedge_a]){
for (const int &hyperedge_b: node2hyperedge[node]){
if (hyperedge_b == hyperedge_a) continue;
if ((upd_time[hyperedge_b] >> 31) ^ hyperedge_a){
upd_time[hyperedge_b] = (l_hyperedge_a << 31) + (long long)hyperedge_adj[hyperedge_b].size();
hyperedge_adj[hyperedge_b].push_back({hyperedge_a, 0});
}
hyperedge_adj[hyperedge_b][(int)(upd_time[hyperedge_b] & 0x7FFFFFFFLL)].second++;
}
}
}
for (int hyperedge_a = 0; hyperedge_a < E; hyperedge_a++){
int deg_a = hyperedge_adj[hyperedge_a].size();
hyperedge_inter[hyperedge_a].rehash(deg_a);
for (int i = 0; i < deg_a; i++){
int hyperedge_b = hyperedge_adj[hyperedge_a][i].first;
int C_ab = hyperedge_adj[hyperedge_a][i].second;
hyperedge_inter[hyperedge_a].insert({hyperedge_b, C_ab});
}
}
cout << "Adjacency list construction done: "
<< (double)(clock() - start) / CLOCKS_PER_SEC << " sec" << endl;
cout << "------------------------------------------" << endl << endl;
// Exact hypergraph motif counting
start = clock();
vector<long long> h_motif(30, 0);
vector<int> intersection(E, 0);
std::fill(upd_time.begin(), upd_time.end(), -1LL);
for(int hyperedge_a = 0; hyperedge_a < E; hyperedge_a++){
if (hyperedge_a % 10000 == 0)
cout << "Hyperedge: " << hyperedge_a << " / " << E << endl;
long long l_hyperedge_a = (long long)hyperedge_a;
int size_a = (int)hyperedge2node[hyperedge_a].size();
int deg_a = (int)hyperedge_adj[hyperedge_a].size();
for (int i = 0; i < deg_a; i++){
int hyperedge_b = hyperedge_adj[hyperedge_a][i].first, C_ab = hyperedge_adj[hyperedge_a][i].second;
int size_b = (int)hyperedge2node[hyperedge_b].size();
int deg_b = (int)hyperedge_adj[hyperedge_b].size();
const auto &nodes = hyperedge2node_set[hyperedge_b]; auto it_end = nodes.end(); int cnt = 0;
for (const int &node: hyperedge2node[hyperedge_a]){ if(nodes.find(node) != it_end) intersection[cnt++] = node;}
for (int j = i + 1; j < deg_a; j++){
int hyperedge_c = hyperedge_adj[hyperedge_a][j].first, C_ca = hyperedge_adj[hyperedge_a][j].second;
int size_c = (int)hyperedge2node[hyperedge_c].size();
int deg_c = (int)hyperedge_adj[hyperedge_c].size();
int C_bc = hyperedge_inter[hyperedge_b][hyperedge_c];
if (C_bc){
if (hyperedge_a < hyperedge_b){
int g_abc = 0;
const auto &nodes = hyperedge2node_set[hyperedge_c]; auto it_end = nodes.end();
for (int k = 0; k < C_ab; k++){ if(nodes.find(intersection[k]) != it_end) g_abc++;}
int motif_index = get_motif_index_new(size_a, size_b, size_c, C_ab, C_bc, C_ca, g_abc);
h_motif[motif_index]++;
}
} else {
int motif_index = get_motif_index_new(size_a, size_b, size_c, C_ab, 0, C_ca, 0);
h_motif[motif_index]++;
}
}
}
}
double runtime = (double)(clock() - run_start) / CLOCKS_PER_SEC;
int index = 0;
for (int i = 0; i < 30; i++){
if (i == 0 || i == 1 || i == 4 || i == 6) continue;
cout << fixed << "motif " << ++index << ": " << fixed << h_motif[i] << endl;
}
cout << "\nMotif counting: "
<< (double)(clock() - start) / CLOCKS_PER_SEC << " sec" << endl;
cout << "Total runtime: " << runtime << endl;
cout << "------------------------------------------" << endl << endl;
node2hyperedge.clear();
hyperedge2node.clear();
hyperedge_adj.clear();
hyperedge_inter.clear();
return 0;
}