-
Notifications
You must be signed in to change notification settings - Fork 1
/
kpkc.h
487 lines (408 loc) · 14.3 KB
/
kpkc.h
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
#ifndef KPartiteKClique_header
#define KPartiteKClique_header
#include <cstdint>
#include <vector>
#include <cassert>
#include <stdexcept>
#include <algorithm>
// namespace k-partite-k-clicque
namespace kpkc
{
inline uint64_t one_set_bit(int n){
return ((uint64_t) 1) << (n % 64);
}
class Bitset;
class Bitset {
public:
Bitset() = default;
Bitset(int n_vertices, bool fill=false);
Bitset(const bool* set_bits, int n_vertices);
Bitset(const Bitset&) = delete;
Bitset(Bitset&&) = default;
Bitset& operator=(const Bitset&) = delete;
Bitset& operator=(Bitset&&) = default;
void unset(int index){
data[index/64] &= ~one_set_bit(index % 64);
}
bool has(int index) const {
return data[index/64] & one_set_bit(index % 64);
}
void set(int index);
int intersection_count(const Bitset& r, int start, int stop) const;
int count(int start, int stop) const;
int first(int start) const;
void intersection_assign(const Bitset& l, const Bitset& r);
protected:
std::vector<uint64_t> data;
int limbs{0};
void allocate(int n_vertices);
uint64_t& operator[](int i){ return data[i]; }
const uint64_t& operator[](int i) const { return data[i]; }
};
enum Algorithm { kpkc, FindClique };
template<Algorithm A>
class KPartiteKClique {
class Vertex_template;
class Vertex;
class KPartiteGraph;
public:
KPartiteKClique(const bool* const* incidences, const int n_vertices, const int* first_per_part, const int k, const int prec_depth=5);
virtual ~KPartiteKClique() = default;
const int* k_clique() const { return _k_clique.data(); }
bool next();
KPartiteKClique(const KPartiteKClique&) = delete;
KPartiteKClique& operator=(const KPartiteKClique&) = delete;
private:
std::vector<int> _k_clique;
std::vector<int> parts;
int k;
int current_depth;
int n_vertices;
int prec_depth;
std::vector<Vertex_template> all_vertices;
std::vector<KPartiteGraph> recursive_graphs;
KPartiteGraph* current_graph(){
return &(recursive_graphs[current_depth]);
}
KPartiteGraph* next_graph(){
return &(recursive_graphs[current_depth + 1]);
}
bool backtrack();
void finish_init();
};
template class KPartiteKClique<kpkc>;
template class KPartiteKClique<FindClique>;
template<Algorithm A>
class KPartiteKClique<A>::Vertex_template {
// Takes care of the memory allocation for vertices.
friend KPartiteKClique<A>;
friend KPartiteKClique<A>::Vertex;
friend void intersection(Bitset& c, const Vertex_template& l, const Bitset& r){
c.intersection_assign(l.bitset, r);
}
public:
int index; // The index in the original graph.
int part; // The part in the orginal graph.
Vertex_template(const Vertex_template&) = delete;
Vertex_template& operator=(const Vertex_template&) = delete;
Vertex_template(Vertex_template&&) = default;
Vertex_template& operator=(Vertex_template&&) = default;
Vertex_template(KPartiteKClique<A>* problem, const bool* incidences, int n_vertices, int part, int index);
private:
Bitset bitset;
KPartiteKClique<A>* problem;
};
template<Algorithm A>
class KPartiteKClique<A>::KPartiteGraph {
public:
Bitset active_vertices;
std::vector<int> part_sizes;
std::vector<Vertex> vertices;
KPartiteGraph() = default;
KPartiteGraph(KPartiteKClique<A>* problem, bool fill);
bool permits_another_choice() const;
bool select(KPartiteGraph* next);
bool select();
// kpkc
Vertex* last_vertex();
void pop_last_vertex();
bool set_weights();
// FindClique
int selected_part;
bool set_part_sizes();
int count(int start, int stop) const {
return active_vertices.count(start, stop);
}
int count(int part) const {
return count(get_parts()[part], get_parts()[part+1]);
}
int first(int part) const;
void pop_vertex(int part, int vertex);
KPartiteGraph(const KPartiteGraph&) = delete;
KPartiteGraph& operator=(const KPartiteGraph&) = delete;
KPartiteGraph(KPartiteGraph&&) = default;
KPartiteGraph& operator=(KPartiteGraph&&) = default;
private:
const int* get_parts() const { assert(problem); return problem->parts.data(); }
const int get_k() const { assert(problem); return problem->k; }
KPartiteKClique<A>::KPartiteGraph* current_graph(){ return problem->current_graph(); }
KPartiteKClique<A>::KPartiteGraph* next_graph(){ return problem->next_graph(); }
KPartiteKClique<A>* problem{nullptr};
};
template<>
inline KPartiteKClique<FindClique>::Vertex* KPartiteKClique<FindClique>::KPartiteGraph::last_vertex() { return NULL; }
template<>
inline void KPartiteKClique<FindClique>::KPartiteGraph::pop_last_vertex() {}
template<>
inline bool KPartiteKClique<FindClique>::KPartiteGraph::set_weights() { return false; }
template<>
inline bool KPartiteKClique<kpkc>::KPartiteGraph::set_part_sizes() { return false;}
template<>
inline int KPartiteKClique<kpkc>::KPartiteGraph::count(int, int) const { return 0;}
template<>
inline int KPartiteKClique<kpkc>::KPartiteGraph::count(int) const { return 0;}
template<>
inline int KPartiteKClique<kpkc>::KPartiteGraph::first(int) const { return 0;}
template<>
inline void KPartiteKClique<kpkc>::KPartiteGraph::pop_vertex(int, int) {}
template<>
class KPartiteKClique<FindClique>:: Vertex {};
template<>
class KPartiteKClique<kpkc>::Vertex {
// Vertex is a shallow copy of Vertex_template.
friend bool operator<(const Vertex& l, const Vertex& r){
// The lower the weight, the higher the obstruction when
// selecting this vertex.
// We want to select vertices with high obstruction first
// and put the last (to be popped first).
return l.weight > r.weight;
}
friend void intersection(Bitset& c, const Vertex& l, const Bitset& r){
c.intersection_assign(*(l.bitset), r);
}
public:
int index; // The index in the original graph.
int part; // The part in the orginal graph.
int weight; // The higher, the higher the likelihood of a k-clique with this vertex.
Vertex() = default;
Vertex(const Vertex_template& obj){
bitset = &(obj.bitset);
part = obj.part;
weight = -1;
index = obj.index;
problem = obj.problem;
if (1 != bitset->count(get_parts()[part], get_parts()[part + 1]))
throw std::invalid_argument("the graph is not k-partite");
}
Vertex(const Vertex& obj){
bitset = obj.bitset;
weight = obj.weight;
part = obj.part;
index = obj.index;
problem = obj.problem;
}
bool set_weight(){
// The weight is the number of vertices that are still available when
// selecting this vertex.
// However, when selecting the vertex no longer allows a k-clique,
// the weight is always set to 0.
//
// Return ``true`` if and only if this vertex is newly removed.
int counter = 0;
int tmp;
Bitset& active_vertices = get_active_vertices();
if (!active_vertices.has(index)){
weight = 0;
return false;
}
if (problem->current_depth > problem->prec_depth){
weight = 1;
return false;
}
for (int i=0; i<get_k(); i++){
tmp = intersection_count(active_vertices, i);
counter += tmp;
if (!tmp){
// This vertex would not allow for a k-clique anymore.
weight = 0;
active_vertices.unset(index);
return true;
}
}
weight = counter;
return false;
}
int intersection_count(const Bitset& r, int start, int stop) const {
return bitset->intersection_count(r, start, stop);
}
int intersection_count(const Bitset& r, int part) const {
return intersection_count(r, get_parts()[part], get_parts()[part+1]);
}
private:
const Bitset* bitset;
KPartiteKClique* problem;
const int* get_parts() const { return problem->parts.data(); }
const int get_k() const { return problem->k; }
Bitset& get_active_vertices() { return problem->current_graph()->active_vertices; }
};
// KPartiteGraph functions that are inlined
template<>
inline bool KPartiteKClique<kpkc>::KPartiteGraph::set_weights(){
bool new_knowledge = false;
for(Vertex& v: vertices)
new_knowledge |= v.set_weight();
return new_knowledge;
}
template<>
inline void KPartiteKClique<kpkc>::KPartiteGraph::pop_last_vertex(){
Vertex& v = vertices.back();
part_sizes[v.part] -= 1;
active_vertices.unset(v.index);
vertices.pop_back();
}
template<>
inline KPartiteKClique<kpkc>::Vertex* KPartiteKClique<kpkc>::KPartiteGraph::last_vertex(){
/*
Get the last vertex, which is (possibly) a valid choice.
Pop all vertices that are no longer valid choices.
*/
if (!vertices.size())
return NULL;
Vertex& v = vertices.back();
// Remove all vertices,
// that are no longer
// a valid choice.
while (!v.weight){
pop_last_vertex();
if (!vertices.size())
return NULL;
v = vertices.back();
}
return &v;
}
template<>
inline int KPartiteKClique<FindClique>::KPartiteGraph::count(int start, int stop) const {
return active_vertices.count(start, stop);
}
template<>
inline int KPartiteKClique<FindClique>::KPartiteGraph::count(int part) const {
return count(get_parts()[part], get_parts()[part+1]);
}
template<>
inline int KPartiteKClique<FindClique>::KPartiteGraph::first(int part) const {
int the_first = active_vertices.first(get_parts()[part]);
return (the_first < get_parts()[part+1]) ? the_first : -1;
}
template<>
inline void KPartiteKClique<FindClique>::KPartiteGraph::pop_vertex(int part, int vertex){
active_vertices.unset(vertex);
part_sizes[part] -= 1;
}
template<>
inline bool KPartiteKClique<FindClique>::KPartiteGraph::set_part_sizes(){
/*
Set the sizes of the parts.
Return false, if any part has size 0, otherwise true
*/
int i;
int min_so_far = problem->n_vertices;
selected_part = -1;
for(i=0; i < problem->k; i++){
if (part_sizes[i] != 1){
int j = count(i);
part_sizes[i] = j;
if (j == 0){
// this part is empty; need to backtrack
selected_part = -2;
return false;
}
if (j == 1){
// this part has a unique choice
selected_part = i;
return true;
} else if (j < min_so_far){
min_so_far = j;
selected_part = i;
}
}
}
return true;
}
template<>
inline bool KPartiteKClique<kpkc>::KPartiteGraph::permits_another_choice() const {
for (int i=0; i<get_k(); i++){
if (part_sizes[i] == 0)
return false;
}
return true;
}
template<>
inline bool KPartiteKClique<FindClique>::KPartiteGraph::permits_another_choice() const {
return ((selected_part >= 0) && (part_sizes[selected_part]));
}
template<>
inline bool KPartiteKClique<kpkc>::KPartiteGraph::select(KPartiteGraph* next){
/*
Select the last (valid) vertex of the current graph set up the next graph
to be all vertices connected to that last vertex.
Return false, if there are no vertices left.
*/
Vertex* v = last_vertex();
if (!v)
return false;
// Copy the current sizes.
for (int i=0; i<get_k(); i++)
next->part_sizes[i] = part_sizes[i];
// Select v.
problem->_k_clique[v->part] = v->index;
intersection(next->active_vertices, *v, active_vertices);
int part = v->part;
// v may no longer be selected.
// In current not, because we have removed it.
// In next not, because it is selected already.
pop_last_vertex();
next->vertices.assign(vertices.begin(), vertices.end());
// Note the part size of ``_k_clique[v.part]``:
// current is one smaller than next.
// This is intentional, as in next the vertex was selected, not
// removed.
// Apply the new knowledge.
if (part_sizes[part] == 1){
this->set_weights();
sort(vertices.begin(), vertices.end());
}
if (part_sizes[part] == 0)
vertices.resize(0);
// Raise the current
// depth, such that the
// weights get set
// accordingly.
problem->current_depth += 1;
next->set_weights();
// When setting weights, we also discover that some vertices are not
// longer possible.
// This information can be used to call set weights again and get
// preciser results.
// This appears to pay off if we are not too deep in the recursion
// tree.
if (problem->current_depth < problem->prec_depth && next->set_weights())
next->set_weights();
sort(next->vertices.begin(), next->vertices.end());
return true;
}
template<>
bool KPartiteKClique<kpkc>::KPartiteGraph::select(){
/*
Select the last (valid) vertex of the current graph and return
whether it exists.
It is assumed that there is no next graph.
*/
assert(problem->current_depth == problem->k -1);
Vertex* v = last_vertex();
if (!v)
return false;
// Select v.
problem->_k_clique[v->part] = v->index;
int part = v->part;
// v may no longer be selected.
// In current not, because we have removed it.
pop_last_vertex();
if (part_sizes[part] == 0)
vertices.resize(0);
return true;
}
template<Algorithm A>
bool KPartiteKClique<A>::backtrack(){
/*
Go the the last valid graph.
If none exists, return false.
*/
while (current_depth >= 1){
current_depth -= 1;
if (current_graph()->permits_another_choice())
return true;
}
return false;
}
}
#endif