forked from jingnanshi/pmc
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpmcx_maxclique_basic.cpp
More file actions
executable file
·316 lines (254 loc) · 10.5 KB
/
pmcx_maxclique_basic.cpp
File metadata and controls
executable file
·316 lines (254 loc) · 10.5 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
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
/**
============================================================================
Name : Parallel Maximum Clique (PMC) Library
Author : Ryan A. Rossi (rrossi@purdue.edu)
Description : A general high-performance parallel framework for computing
maximum cliques. The library is designed to be fast for large
sparse graphs.
Copyright (C) 2012-2013, Ryan A. Rossi, All rights reserved.
Please cite the following paper if used:
Ryan A. Rossi, David F. Gleich, Assefaw H. Gebremedhin, Md. Mostofa
Patwary, A Fast Parallel Maximum Clique Algorithm for Large Sparse Graphs
and Temporal Strong Components, arXiv preprint 1302.6256, 2013.
See http://ryanrossi.com/pmc for more information.
============================================================================
*/
#include "pmc/pmcx_maxclique_basic.h"
#include "pmc/pmc_neigh_coloring.h"
#include <cstring>
using namespace std;
using namespace pmc;
int pmcx_maxclique_basic::search(pmc_graph& G, vector<int>& sol) {
degree = G.get_degree();
bool_vector pruned(G.num_vertices());
int mc = lb, i = 0, u = 0;
// initial pruning
int lb_idx = G.initial_pruning(G, pruned, lb);
// set to worst case bound of cores/coloring
vector<Vertex> P, T;
P.reserve(G.get_max_degree()+1);
T.reserve(G.get_max_degree()+1);
vector<int> C, C_max;
C.reserve(G.get_max_degree()+1);
C_max.reserve(G.get_max_degree()+1);
// init the neigh coloring array
vector< vector<int> > colors(G.get_max_degree()+1);
for (int i = 0; i < G.get_max_degree()+1; i++) colors[i].reserve(G.get_max_degree()+1);
// order verts for our search routine
vector<Vertex> V;
V.reserve(G.num_vertices());
G.order_vertices(V,G,lb_idx,lb,vertex_ordering,decr_order);
DEBUG_PRINTF("|V| = %i\n", V.size());
vector<short> ind(G.num_vertices(),0);
vector<int> es = G.get_edges_array();
vector<long long> vs = G.get_vertices_array();
vector<double> induce_time(num_threads,get_time());
for (int t = 0; t < num_threads; ++t) induce_time[t] = induce_time[t] + t/4;
#pragma omp parallel for schedule(dynamic) shared(pruned, G, T, V, mc, C_max, induce_time) \
firstprivate(colors,ind,vs,es) private(u, P, C) num_threads(num_threads)
for (i = 0; i < (V.size()) - (mc-1); ++i) {
if (G.time_left(C_max,sec,time_limit,time_expired_msg)) {
u = V[i].get_id();
if ((*bound)[u] > mc) {
P.push_back(V[i]);
for (long long j = vs[u]; j < vs[u + 1]; ++j)
if (!pruned[es[j]])
if ((*bound)[es[j]] > mc)
P.push_back(Vertex(es[j], (*degree)[es[j]]));
if (P.size() > mc) {
neigh_coloring_bound(vs,es,P,ind,C,colors,mc);
if (P.back().get_bound() > mc) {
branch(vs,es,P, ind, C, C_max, colors, pruned, mc);
}
}
P = T;
}
pruned[u] = true;
// dynamically reduce graph in a thread-safe manner
if ((get_time() - induce_time[omp_get_thread_num()]) > wait_time) {
G.reduce_graph( vs, es, pruned, G);
G.graph_stats(G, mc, i+lb_idx, sec);
induce_time[omp_get_thread_num()] = get_time();
}
}
}
sol.resize(mc);
for (int i = 0; i < C_max.size(); i++) sol[i] = C_max[i];
G.print_break();
return sol.size();
}
void pmcx_maxclique_basic::branch(
vector<long long>& vs,
vector<int>& es,
vector<Vertex> &P,
vector<short>& ind,
vector<int>& C,
vector<int>& C_max,
vector< vector<int> >& colors,
const bool_vector& pruned,
int& mc) {
// stop early if ub is reached
if (not_reached_ub) {
while (P.size() > 0) {
// terminating condition
if (C.size() + P.back().get_bound() > mc) {
int v = P.back().get_id(); C.push_back(v);
vector<Vertex> R; R.reserve(P.size());
for (long long j = vs[v]; j < vs[v + 1]; j++) ind[es[j]] = 1;
// intersection of N(v) and P - {v}
for (int k = 0; k < P.size() - 1; k++)
if (ind[P[k].get_id()])
if (!pruned[P[k].get_id()])
if ((*bound)[P[k].get_id()] > mc)
R.push_back(P[k]);
for (long long j = vs[v]; j < vs[v + 1]; j++) ind[es[j]] = 0;
if (R.size() > 0) {
// color graph induced by R and sort for O(1) bound check
neigh_coloring_bound(vs, es, R, ind, C, colors, mc);
// search reordered R
branch(vs, es, R, ind, C, C_max, colors, pruned, mc);
}
else if (C.size() > mc) {
// obtain lock
#pragma omp critical (update_mc)
if (C.size() > mc) {
// ensure updated max is flushed
mc = C.size();
C_max = C;
print_mc_info(C,sec);
if (mc >= param_ub) {
not_reached_ub = false;
DEBUG_PRINTF("[pmc: upper bound reached] omega = %i\n", mc);
}
}
}
// backtrack and search another branch
R.clear();
C.pop_back();
}
else return;
P.pop_back();
}
}
}
/**
* Dense graphs: we use ADJ matrix + CSC Representation
* ADJ: O(1) edge lookups
* CSC: O(1) time to compute degree
*
*/
int pmcx_maxclique_basic::search_dense(pmc_graph& G, vector<int>& sol) {
degree = G.get_degree();
auto adj = G.adj;
bool_vector pruned(G.num_vertices());
int mc = lb, i = 0, u = 0;
// initial pruning
int lb_idx = G.initial_pruning(G, pruned, lb, adj);
// set to worst case bound of cores/coloring
vector<Vertex> P, T;
P.reserve(G.get_max_degree()+1);
T.reserve(G.get_max_degree()+1);
vector<int> C, C_max;
C.reserve(G.get_max_degree()+1);
C_max.reserve(G.get_max_degree()+1);
// init the neigh coloring array
vector< vector<int> > colors(G.get_max_degree()+1);
for (int i = 0; i < G.get_max_degree()+1; i++) colors[i].reserve(G.get_max_degree()+1);
// order verts for our search routine
vector<Vertex> V;
V.reserve(G.num_vertices());
G.order_vertices(V,G,lb_idx,lb,vertex_ordering,decr_order);
DEBUG_PRINTF("|V| = %u\n", V.size());
vector<short> ind(G.num_vertices(),0);
vector<int> es = G.get_edges_array();
vector<long long> vs = G.get_vertices_array();
vector<double> induce_time(num_threads,get_time());
for (int t = 0; t < num_threads; ++t) induce_time[t] = induce_time[t] + t/4;
#pragma omp parallel for schedule(dynamic) shared(pruned, G, adj, T, V, mc, C_max, induce_time) \
firstprivate(colors,ind,vs,es) private(u, P, C) num_threads(num_threads)
for (i = 0; i < (V.size()) - (mc-1); ++i) {
if (G.time_left(C_max,sec,time_limit,time_expired_msg)) {
u = V[i].get_id();
if ((*bound)[u] > mc) {
P.push_back(V[i]);
for (long long j = vs[u]; j < vs[u + 1]; ++j)
if (!pruned[es[j]])
if ((*bound)[es[j]] > mc)
P.push_back(Vertex(es[j], (*degree)[es[j]]));
if (P.size() > mc) {
neigh_coloring_dense(P,C,colors,mc, adj);
if (P.back().get_bound() > mc) {
branch_dense(vs,es,P, ind, C, C_max, colors, pruned, mc, adj);
}
}
P = T;
}
pruned[u] = true;
for (long long j = vs[u]; j < vs[u + 1]; j++) {
adj[u][es[j]] = false;
adj[es[j]][u] = false;
}
// dynamically reduce graph in a thread-safe manner
if ((get_time() - induce_time[omp_get_thread_num()]) > wait_time) {
G.reduce_graph( vs, es, pruned, G);
G.graph_stats(G, mc, i+lb_idx, sec);
induce_time[omp_get_thread_num()] = get_time();
}
}
}
sol.resize(mc);
for (int i = 0; i < C_max.size(); i++) sol[i] = C_max[i];
G.print_break();
return sol.size();
}
void pmcx_maxclique_basic::branch_dense(
vector<long long>& vs,
vector<int>& es,
vector<Vertex> &P,
vector<short>& ind,
vector<int>& C,
vector<int>& C_max,
vector< vector<int> >& colors,
bool_vector& pruned,
int& mc,
std::vector<bool_vector>& adj) {
// stop early if ub is reached
if (not_reached_ub) {
while (P.size() > 0) {
// terminating condition
if (C.size() + P.back().get_bound() > mc) {
int v = P.back().get_id(); C.push_back(v);
vector<Vertex> R; R.reserve(P.size());
for (int k = 0; k < P.size() - 1; k++)
// indicates neighbor AND pruned, since threads dynamically update it
if (adj[v][P[k].get_id()])
if ((*bound)[P[k].get_id()] > mc)
R.push_back(P[k]);
if (R.size() > 0) {
// color graph induced by R and sort for O(1)
neigh_coloring_dense(R, C, colors, mc, adj);
branch_dense(vs, es, R, ind, C, C_max, colors, pruned, mc, adj);
}
else if (C.size() > mc) {
// obtain lock
#pragma omp critical (update_mc)
if (C.size() > mc) {
// ensure updated max is flushed
mc = C.size();
C_max = C;
print_mc_info(C,sec);
if (mc >= param_ub) {
not_reached_ub = false;
DEBUG_PRINTF("[pmc: upper bound reached] omega = %i\n", mc);
}
}
}
// backtrack and search another branch
R.clear();
C.pop_back();
}
else return;
P.pop_back();
}
}
}