-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathwc_improved.cpp
212 lines (176 loc) · 6.37 KB
/
wc_improved.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
#include <cassert>
#include "util/graph/graph.h"
#include "../util/util.h"
#include "util/log/log.h"
#include "../util/timer.h"
#include "opt_pkt/parallel_all_edge_cnc.h"
/** WC algorithm for truss decomposition
*
* J.Wang and J. Cheng, "Truss decomposition in massive networks", Proc. VLDB Endow., vol 5, no 9, pp.
* 812-823, May 2012.
*
* */
void WCJames(graph_t *g, int *EdgeSupport, MapType &edgeToIdMap, Edge *edgeIdToEdge) {
long m = g->m;
auto *deg = (unsigned int *) malloc(g->n * sizeof(unsigned int));
assert(deg != nullptr);
for (long i = 0; i < g->n; i++) {
deg[i] = (g->num_edges[i + 1] - g->num_edges[i]);
}
long numEdges = m / 2;
int maxSupport = 0;
size_t tc_cnt = 0;
//STEP 1: Computer Support for each edge
//Maximum support of an edge
#if TIME_RESULTS
double procTime, supTime;
double startTime = timer();
#endif
#pragma omp parallel
{
//Compute the support of each edge
#pragma omp for schedule(dynamic, 6000) reduction(+:tc_cnt) reduction(max:maxSupport)
for (auto i = 0u; i < g->m; i++) {
auto sup = ComputeSupport(g, EdgeSupport, tc_cnt, i);
maxSupport = max(maxSupport, sup);
}
#pragma omp single
{
log_trace("TC Cnt: %'zu", tc_cnt / 3);
log_trace("Max Sup: %d", maxSupport);
}
#if TIME_RESULTS
supTime = timer() - startTime;
#endif
}
log_trace("Finish Sup");
//STEP 2: sort the edges in ascending order of their support -- using bin-sort
auto *sortedEdges = (eid_t *) malloc(numEdges * sizeof(eid_t));
assert(sortedEdges != nullptr);
auto *edgePos = (eid_t *) malloc(numEdges * sizeof(eid_t));
assert(edgePos != nullptr);
//number of bins is (maxSupport + 2)
//the support is in: 0 ... maxSupport
auto *bin = (unsigned int *) calloc(maxSupport + 2, sizeof(unsigned int));
assert(bin != nullptr);
for (long i = 0; i < maxSupport + 2; i++)
bin[i] = 0;
#if TIME_RESULTS
startTime = timer();
#endif
//Find number of edges with each support in 0...maxSupport
for (long i = 0; i < numEdges; i++) {
bin[EdgeSupport[i]]++;
}
unsigned int start = 0;
for (int support = 0; support < maxSupport + 1; support++) {
unsigned int num = bin[support];
bin[support] = start;
start = start + num;
}
//Do bin-sort/bucket-sort of the edges
//sortedEdges -- contains the edges in increasing order of support
//edgePos -- contains the position of an edge in sortedEdges array
for (long i = 0; i < numEdges; i++) {
edgePos[i] = bin[EdgeSupport[i]];
sortedEdges[edgePos[i]] = i;
bin[EdgeSupport[i]]++;
}
for (int d = maxSupport; d >= 1; d--)
bin[d] = bin[d - 1];
bin[0] = 0;
log_trace("Finish Sort");
//STEP 3: Compute k-truss using support of each edge
int k = 2;
//k-truss computations
long i = 0;
long numEdgesDeleted = 0;
#ifdef NAIVE_ITER_WITH_PROC
bool *proc = (bool *) malloc(numEdges * sizeof(bool));
assert(proc != nullptr);
for (long ii = 0; ii < numEdges; ii++) {
proc[ii] = false;
}
#endif
Timer global_timer;
auto sup_updater = [EdgeSupport, edgePos, bin, sortedEdges](eid_t edgeId, int k) {
if (EdgeSupport[edgeId] > k - 2) {
//swap edge <v,w> with the first edge in the bin[ EdgeSupport[edgeId] ]
int supportWV = EdgeSupport[edgeId];
unsigned int posWV = edgePos[edgeId];
unsigned int startPos = bin[supportWV]; //First position with support supportWV
unsigned int firstEdgeId = sortedEdges[startPos];
//Swap firstEdgeId and edgeId
if (firstEdgeId != edgeId) {
edgePos[edgeId] = startPos;
sortedEdges[posWV] = firstEdgeId;
edgePos[firstEdgeId] = posWV;
sortedEdges[startPos] = edgeId;
}
//Increase the starting index of bin[ supportWV ]
bin[supportWV]++;
//Decrease support of edgeId -- so edgeId is in previous bin now
EdgeSupport[edgeId] = EdgeSupport[edgeId] - 1;
}
};
while (numEdgesDeleted < numEdges) {
//If edge sortedEdges[i] has support <= k-2
while (i < numEdges && EdgeSupport[sortedEdges[i]] <= k - 2) {
eid_t e = sortedEdges[i];
Edge edge = edgeIdToEdge[e];
vid_t u = edge.u;
vid_t v = edge.v;
//Make sure: deg(u) < deg(v)
if (deg[v] < deg[u]) {
//swap u and v
vid_t temp = v;
v = u;
u = temp;
}
//Now, deg(u) < deg(v)
#ifdef NAIVE_ITER_WITH_PROC
for (eid_t j = g->num_edges[u]; j < g->num_edges[u + 1]; j++) {
vid_t w = g->adj[j];
//If <u,w> is an edge
if (!proc[g->eid[j]]) {
//If <v,w> is an edge
if (edgeToIdMap[v].contains(w)) {
//find edgeId of edge e(v,w), If edge support of <v,w> > k-2 then deccrement it
sup_updater(edgeToIdMap[v][w], k);
//find edgeId of edge <u,w>, If edge support of <u,w> > k-2 then decrement it
sup_updater(edgeToIdMap[u][w], k);
} //<v,w> is an edge
} //<u,w> is an edge
} // w is in N(u)
#else
for (auto pair: edgeToIdMap[u]) {
auto w = pair.first;
//If <v,w> is an edge
if (edgeToIdMap[v].contains(w)) {
sup_updater(edgeToIdMap[v][w], k);
sup_updater(pair.second, k);
}
}
#endif
i++;
numEdgesDeleted++;
//Delete edge 'e' from the graph
#ifdef NAIVE_ITER_WITH_PROC
proc[edgeToIdMap[u][v]] = true;
#endif
edgeToIdMap[u].erase(v);
edgeToIdMap[v].erase(u);
}
log_trace("Finish k: %d, Elapsed: %.9lfs, Deleted: %'lld", k, global_timer.elapsed(), numEdgesDeleted);
global_timer.reset();
k++;
}
#if TIME_RESULTS
procTime = timer() - startTime;
log_trace("Support Time: %9.3lfs, Proc Time: %9.3lfs", supTime, procTime);
log_trace("WC Time: %9.3lf", supTime + procTime);
#endif
#ifdef NAIVE_ITER_WITH_PROC
free(proc);
#endif
}