forked from google/or-tools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cliques_test.cc
677 lines (610 loc) · 25.8 KB
/
cliques_test.cc
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
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
// Copyright 2010-2022 Google LLC
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include "ortools/graph/cliques.h"
#include <algorithm>
#include <cstdint>
#include <functional>
#include <limits>
#include <memory>
#include <random>
#include <utility>
#include <vector>
#include "absl/container/flat_hash_set.h"
#include "absl/functional/bind_front.h"
#include "absl/random/distributions.h"
#include "absl/strings/str_cat.h"
#include "benchmark/benchmark.h"
#include "gtest/gtest.h"
#include "ortools/base/callback.h"
#include "ortools/base/mathutil.h"
#include "ortools/util/time_limit.h"
#include "util/functional/to_callback.h"
namespace operations_research {
namespace {
template <typename NodeIndex>
class CliqueReporter {
public:
CliqueReporter() : CliqueReporter(std::numeric_limits<int64_t>::max()) {}
explicit CliqueReporter(int64_t max_cliques)
: remaining_cliques_(max_cliques) {}
const std::vector<std::vector<NodeIndex>>& all_cliques() {
return all_cliques_;
}
bool AppendClique(const std::vector<NodeIndex>& new_clique) {
all_cliques_.push_back(new_clique);
--remaining_cliques_;
return remaining_cliques_ <= 0;
}
std::function<CliqueResponse(const std::vector<NodeIndex>&)>
MakeCliqueCallback() {
return [this](const std::vector<NodeIndex>& clique) {
return AppendClique(clique) ? CliqueResponse::STOP
: CliqueResponse::CONTINUE;
};
}
private:
int64_t remaining_cliques_;
std::vector<std::vector<NodeIndex>> all_cliques_;
};
// An object used to build callbacks for FindCliques. Verifies that the size of
// the cliques discovered by the algorithm fits into the specified bounds, and
// counts the cliques it receives.
class CliqueSizeVerifier {
public:
CliqueSizeVerifier(int expected_min_clique_size, int expected_max_clique_size)
: expected_min_clique_size_(expected_min_clique_size),
expected_max_clique_size_(expected_max_clique_size),
num_cliques_(0) {}
int64_t num_cliques() const { return num_cliques_; }
bool AppendClique(const std::vector<int>& new_clique) {
EXPECT_GE(expected_max_clique_size_, new_clique.size());
EXPECT_LE(expected_min_clique_size_, new_clique.size());
++num_cliques_;
return false;
}
std::function<CliqueResponse(const std::vector<int>&)> MakeCliqueCallback() {
return [this](const std::vector<int>& clique) {
return AppendClique(clique) ? CliqueResponse::STOP
: CliqueResponse::CONTINUE;
};
}
private:
const int expected_min_clique_size_;
const int expected_max_clique_size_;
int64_t num_cliques_;
};
inline bool FullGraph(int index1, int index2) { return true; }
inline bool EmptyGraph(int index1, int index2) { return (index1 == index2); }
inline bool MatchingGraph(int index1, int index2) {
return (index1 / 2 == index2 / 2);
}
inline bool ModuloGraph(int num_partitions, int index1, int index2) {
return (index1 % num_partitions == index2 % num_partitions);
}
inline bool FullKPartiteGraph(int num_partitions, int index1, int index2) {
return index1 % num_partitions != index2 % num_partitions;
}
// A graph that is represented as an adjacency matrix stored in a hash set:
// there is an arc (i, j) if and only if 'arcs' contains the pair
// (min(i, j), max(i, j)).
inline bool BitmapGraph(const absl::flat_hash_set<std::pair<int, int>>& arcs,
int index1, int index2) {
const int lower = std::min(index1, index2);
const int higher = std::max(index1, index2);
return arcs.contains(std::make_pair(lower, higher));
}
// Generates a random graph with 'num_vertices' vertices, where each arc exists
// with probability 'arc_probability'. The graph is represented as an adjacency
// matrix stored in a set. There is an unoriented arc (i, j) in the graph if and
// only if the returned set contains the pair (min(i, j), max(i, j)).
absl::flat_hash_set<std::pair<int, int>> MakeRandomGraphAdjacencyMatrix(
int num_vertices, float arc_probability, int seed) {
std::mt19937 random(seed);
absl::flat_hash_set<std::pair<int, int>> arcs;
for (int v1 = 0; v1 < num_vertices; ++v1) {
for (int v2 = v1 + 1; v2 < num_vertices; ++v2) {
const bool edge_exists = absl::Bernoulli(random, arc_probability);
if (edge_exists) {
arcs.insert(std::make_pair(v1, v2));
}
}
}
return arcs;
}
template <typename NodeIndex>
void RunBronKerboschAlgorithmUntilCompletion(
BronKerboschAlgorithm<NodeIndex>* bron_kerbosch, int64_t step_size) {
CHECK_NE(bron_kerbosch, nullptr);
while (true) {
auto status = bron_kerbosch->RunIterations(step_size);
if (status == BronKerboschAlgorithmStatus::COMPLETED) {
return;
}
}
}
TEST(BronKerbosch, CompleteGraph) {
constexpr int kNumNodes[] = {1, 5, 50, 500, 5000};
for (const int num_nodes : kNumNodes) {
auto graph = FullGraph;
CliqueReporter<int> reporter;
auto callback =
absl::bind_front(&CliqueReporter<int>::AppendClique, &reporter);
operations_research::FindCliques(
::util::functional::ToPermanentCallback(graph), num_nodes,
::util::functional::ToPermanentCallback(callback));
const std::vector<std::vector<int>>& all_cliques = reporter.all_cliques();
EXPECT_EQ(1, all_cliques.size());
EXPECT_EQ(num_nodes, all_cliques[0].size());
}
}
TEST(BronKerboschAlgorithmTest, CompleteGraph) {
constexpr int kNumNodes[] = {1, 5, 50, 500, 5000};
for (const int num_nodes : kNumNodes) {
SCOPED_TRACE(absl::StrCat("num_nodes = ", num_nodes));
CliqueReporter<int> reporter;
operations_research::BronKerboschAlgorithm<int> bron_kerbosch(
FullGraph, num_nodes, reporter.MakeCliqueCallback());
bron_kerbosch.Run();
const std::vector<std::vector<int>>& all_cliques = reporter.all_cliques();
EXPECT_EQ(1, all_cliques.size());
EXPECT_EQ(num_nodes, all_cliques[0].size());
}
}
TEST(BronKerboschAlgorithmTest, RandomGraph) {
constexpr int kNumNodes = 1000;
constexpr double kArcProbability = 0.1;
constexpr int kSeed = 123456789;
constexpr int kExpectedNumCliques = 100485;
const absl::flat_hash_set<std::pair<int, int>> adjacency_matrix =
MakeRandomGraphAdjacencyMatrix(kNumNodes, kArcProbability, kSeed);
auto graph = [&adjacency_matrix](int index1, int index2) {
return BitmapGraph(adjacency_matrix, index1, index2);
};
CliqueSizeVerifier verifier(0, kNumNodes);
operations_research::BronKerboschAlgorithm<int> bron_kerbosch(
graph, kNumNodes, verifier.MakeCliqueCallback());
bron_kerbosch.Run();
EXPECT_EQ(kExpectedNumCliques, verifier.num_cliques());
}
// Instead of calling BronKerboschAlgorithm::Run once, this test runs
// BronKerboschAlgorithm::RunIteratons(1) until completion to verify that the
// algorithm can be stopped and resumed.
TEST(BronKerboschAlgorithmTest, CompleteGraphUsingRunIterations) {
constexpr int kNumNodes[] = {1, 5, 50, 500, 5000};
constexpr int kStepSize = 1;
for (const int num_nodes : kNumNodes) {
SCOPED_TRACE(absl::StrCat("num_nodes = ", num_nodes));
CliqueReporter<int> reporter;
operations_research::BronKerboschAlgorithm<int> bron_kerbosch(
FullGraph, num_nodes, reporter.MakeCliqueCallback());
RunBronKerboschAlgorithmUntilCompletion(&bron_kerbosch, kStepSize);
const std::vector<std::vector<int>>& all_cliques = reporter.all_cliques();
EXPECT_EQ(1, all_cliques.size());
EXPECT_EQ(num_nodes, all_cliques[0].size());
}
}
// Verifies that BronKerboschAlgorithm compiles correctly also for int64_t.
TEST(BronKerboschAlgorithmTest, CompleteGraphWithInt64) {
constexpr int64_t kNumNodes[] = {1, 5, 50, 500, 5000};
constexpr int kStepSize = 1;
for (const int num_nodes : kNumNodes) {
SCOPED_TRACE(absl::StrCat("num_nodes = ", num_nodes));
CliqueReporter<int64_t> reporter;
operations_research::BronKerboschAlgorithm<int64_t> bron_kerbosch(
FullGraph, num_nodes, reporter.MakeCliqueCallback());
RunBronKerboschAlgorithmUntilCompletion(&bron_kerbosch, kStepSize);
const std::vector<std::vector<int64_t>>& all_cliques =
reporter.all_cliques();
EXPECT_EQ(1, all_cliques.size());
EXPECT_EQ(num_nodes, all_cliques[0].size());
}
}
TEST(BronKerbosch, EmptyGraph) {
auto graph = EmptyGraph;
CliqueReporter<int> reporter;
auto callback =
absl::bind_front(&CliqueReporter<int>::AppendClique, &reporter);
operations_research::FindCliques(
::util::functional::ToPermanentCallback(graph), 10,
::util::functional::ToPermanentCallback(callback));
const std::vector<std::vector<int>>& all_cliques = reporter.all_cliques();
EXPECT_EQ(10, all_cliques.size());
for (int i = 0; i < 10; ++i) {
EXPECT_EQ(1, all_cliques[i].size());
}
}
TEST(BronKerboschAlgorithmTest, EmptyGraph) {
constexpr int kNumNodes[] = {1, 5, 50, 500, 5000};
for (const int num_nodes : kNumNodes) {
SCOPED_TRACE(absl::StrCat("num_nodes = ", num_nodes));
CliqueReporter<int> reporter;
operations_research::BronKerboschAlgorithm<int> bron_kerbosch(
EmptyGraph, num_nodes, reporter.MakeCliqueCallback());
bron_kerbosch.Run();
const std::vector<std::vector<int>>& all_cliques = reporter.all_cliques();
EXPECT_EQ(num_nodes, all_cliques.size());
for (const std::vector<int>& clique : all_cliques) {
EXPECT_EQ(1, clique.size());
}
}
}
TEST(BronKerboschAlgorithmTest, EmptyGraphUsingRunIterations) {
constexpr int kNumNodes[] = {1, 5, 50, 500, 5000};
constexpr int kStepSize = 1;
for (const int num_nodes : kNumNodes) {
SCOPED_TRACE(absl::StrCat("num_nodes = ", num_nodes));
CliqueReporter<int> reporter;
operations_research::BronKerboschAlgorithm<int> bron_kerbosch(
EmptyGraph, num_nodes, reporter.MakeCliqueCallback());
RunBronKerboschAlgorithmUntilCompletion(&bron_kerbosch, kStepSize);
const std::vector<std::vector<int>>& all_cliques = reporter.all_cliques();
EXPECT_EQ(num_nodes, all_cliques.size());
for (const std::vector<int>& clique : all_cliques) {
EXPECT_EQ(1, clique.size());
}
}
}
// Verifies that the return value from the clique callback is taken into account
// properly. The clique reporter in this case accepts at most three cliques.
TEST(BronKerboschAlgorithmTest, EmptyGraphWithEarlyTermination) {
constexpr int kNumNodes[] = {1, 5, 50, 500, 5000};
constexpr int kMaxCliques = 3;
for (const int num_nodes : kNumNodes) {
SCOPED_TRACE(absl::StrCat("num_nodes = ", num_nodes));
CliqueReporter<int> reporter(kMaxCliques);
operations_research::BronKerboschAlgorithm<int> bron_kerbosch(
EmptyGraph, num_nodes, reporter.MakeCliqueCallback());
bron_kerbosch.Run();
const std::vector<std::vector<int>>& all_cliques = reporter.all_cliques();
EXPECT_EQ(std::min(kMaxCliques, num_nodes), all_cliques.size());
for (const std::vector<int>& clique : all_cliques) {
EXPECT_EQ(1, clique.size());
}
}
}
// Verifies that the algorithm can be resumed after it was stopped due to the
// clique callback returning BronKerboschAlgorithmStatus::STOP.
TEST(BronKerboschAlgorithmTest, EmptyGraphStopAfterEveryClique) {
constexpr int kNumNodes[] = {1, 5, 50, 500, 5000};
for (const int num_nodes : kNumNodes) {
SCOPED_TRACE(absl::StrCat("num_nodes = ", num_nodes));
// NOTE(user): Since we decrement the remaining clique counter every
// time, it will be negative for the most of the time. However, it will not
// prevent the Bron-Kerbosch algorithm from running, it will just stop it
// after every clique it encounters.
CliqueReporter<int> reporter(1);
operations_research::BronKerboschAlgorithm<int> bron_kerbosch(
EmptyGraph, num_nodes, reporter.MakeCliqueCallback());
int num_iterations = 0;
while (bron_kerbosch.Run() != BronKerboschAlgorithmStatus::COMPLETED) {
++num_iterations;
}
const std::vector<std::vector<int>>& all_cliques = reporter.all_cliques();
EXPECT_EQ(all_cliques.size(), num_iterations);
EXPECT_EQ(num_nodes, all_cliques.size());
for (const std::vector<int>& clique : all_cliques) {
EXPECT_EQ(1, clique.size());
}
}
}
TEST(BronKerbosch, MatchingGraph) {
auto graph = MatchingGraph;
CliqueReporter<int> reporter;
auto callback =
absl::bind_front(&CliqueReporter<int>::AppendClique, &reporter);
operations_research::FindCliques(
::util::functional::ToPermanentCallback(graph), 10,
::util::functional::ToPermanentCallback(callback));
const std::vector<std::vector<int>>& all_cliques = reporter.all_cliques();
EXPECT_EQ(5, all_cliques.size());
for (int i = 0; i < 5; ++i) {
EXPECT_EQ(2, all_cliques[i].size());
EXPECT_EQ(all_cliques[i][0] / 2, all_cliques[i][1] / 2);
}
}
TEST(BronKerboschAlgorithmTest, MatchingGraph) {
constexpr int kNumNodes[] = {2, 10, 100, 1000};
for (const int num_nodes : kNumNodes) {
SCOPED_TRACE(absl::StrCat("num_nodes = ", num_nodes));
CliqueReporter<int> reporter;
operations_research::BronKerboschAlgorithm<int> bron_kerbosch(
MatchingGraph, num_nodes, reporter.MakeCliqueCallback());
bron_kerbosch.Run();
const std::vector<std::vector<int>>& all_cliques = reporter.all_cliques();
EXPECT_EQ(num_nodes / 2, all_cliques.size());
for (const std::vector<int>& clique : all_cliques) {
EXPECT_EQ(2, clique.size());
EXPECT_EQ(clique[0] / 2, clique[1] / 2);
}
}
}
TEST(BronKerbosch, ModuloGraph5) {
constexpr int kNumPartitions = 5;
auto graph = absl::bind_front(ModuloGraph, kNumPartitions);
CliqueReporter<int> reporter;
auto callback =
absl::bind_front(&CliqueReporter<int>::AppendClique, &reporter);
operations_research::FindCliques(
::util::functional::ToPermanentCallback(graph), 40,
::util::functional::ToPermanentCallback(callback));
const std::vector<std::vector<int>>& all_cliques = reporter.all_cliques();
EXPECT_EQ(5, all_cliques.size());
for (int i = 0; i < 5; ++i) {
EXPECT_EQ(8, all_cliques[i].size());
}
}
TEST(BronKerboschAlgorithmTest, ModuloGraph) {
constexpr int kNumPartitions[] = {1, 5, 10, 50, 100};
constexpr int kNumNodes = 200;
for (const int num_partitions : kNumPartitions) {
SCOPED_TRACE(absl::StrCat("num_partitions = ", num_partitions));
const auto graph = [num_partitions](int index1, int index2) {
return ModuloGraph(num_partitions, index1, index2);
};
CliqueReporter<int> reporter;
operations_research::BronKerboschAlgorithm<int> bron_kerbosch(
graph, kNumNodes, reporter.MakeCliqueCallback());
bron_kerbosch.Run();
const std::vector<std::vector<int>>& all_cliques = reporter.all_cliques();
EXPECT_EQ(num_partitions, all_cliques.size());
for (const std::vector<int>& clique : all_cliques) {
EXPECT_EQ(kNumNodes / num_partitions, clique.size());
}
}
}
TEST(BronKerbosch, CompleteGraphCover) {
auto graph = FullGraph;
CliqueReporter<int> reporter;
auto callback =
absl::bind_front(&CliqueReporter<int>::AppendClique, &reporter);
operations_research::CoverArcsByCliques(
::util::functional::ToPermanentCallback(graph), 10,
::util::functional::ToPermanentCallback(callback));
const std::vector<std::vector<int>>& all_cliques = reporter.all_cliques();
EXPECT_EQ(1, all_cliques.size());
EXPECT_EQ(10, all_cliques[0].size());
}
TEST(BronKerbosch, EmptyGraphCover) {
auto graph = EmptyGraph;
CliqueReporter<int> reporter;
auto callback =
absl::bind_front(&CliqueReporter<int>::AppendClique, &reporter);
operations_research::CoverArcsByCliques(
::util::functional::ToPermanentCallback(graph), 10,
::util::functional::ToPermanentCallback(callback));
const std::vector<std::vector<int>>& all_cliques = reporter.all_cliques();
EXPECT_EQ(0, all_cliques.size());
}
TEST(BronKerbosch, MatchingGraphCover) {
auto graph = MatchingGraph;
CliqueReporter<int> reporter;
auto callback =
absl::bind_front(&CliqueReporter<int>::AppendClique, &reporter);
operations_research::CoverArcsByCliques(
::util::functional::ToPermanentCallback(graph), 10,
::util::functional::ToPermanentCallback(callback));
const std::vector<std::vector<int>>& all_cliques = reporter.all_cliques();
EXPECT_EQ(5, all_cliques.size());
for (int i = 0; i < 5; ++i) {
EXPECT_EQ(2, all_cliques[i].size());
EXPECT_EQ(all_cliques[i][0] / 2, all_cliques[i][1] / 2);
}
}
TEST(BronKerbosch, ModuloGraph5Cover) {
constexpr int kNumPartitions = 5;
auto graph = absl::bind_front(ModuloGraph, kNumPartitions);
CliqueReporter<int> reporter;
auto callback =
absl::bind_front(&CliqueReporter<int>::AppendClique, &reporter);
operations_research::CoverArcsByCliques(
::util::functional::ToPermanentCallback(graph), 40,
::util::functional::ToPermanentCallback(callback));
const std::vector<std::vector<int>>& all_cliques = reporter.all_cliques();
EXPECT_EQ(5, all_cliques.size());
for (int i = 0; i < 5; ++i) {
EXPECT_EQ(8, all_cliques[i].size());
}
}
TEST(BronKerboschAlgorithmTest, FullKPartiteGraph) {
const int kNumPartitions[] = {2, 3, 4, 5, 6, 7};
for (const int num_partitions : kNumPartitions) {
SCOPED_TRACE(absl::StrCat("num_partitions = ", num_partitions));
const int num_nodes = num_partitions * num_partitions;
const auto graph = [num_partitions](int index1, int index2) {
return FullKPartiteGraph(num_partitions, index1, index2);
};
CliqueReporter<int> reporter;
BronKerboschAlgorithm<int> bron_kerbosch(graph, num_nodes,
reporter.MakeCliqueCallback());
bron_kerbosch.Run();
EXPECT_LT(1, reporter.all_cliques().size());
for (const std::vector<int>& clique : reporter.all_cliques()) {
EXPECT_EQ(num_partitions, clique.size());
}
}
}
// The following two tests run the Bron-Kerbosch algorithm with wall time
// limit and deterministic time limit. They use a full 15-partite graph with
// a one second time limit.
//
// The graph has approximately 2^58.6 maximal cliques, so they could be in
// theory all enumerated within the std::numeric_limits<int64_t>::max()
// iteration limit, but their number is too large that enumerating them would
// take tens to hundreds of years with the current technology (as of August
// 2015). As a result, the algorithm can end in the INTERRUPTED state only when
// the time limit is reached.
TEST(BronKerboschAlgorithmTest, WallTimeLimit) {
const int kNumPartitions = 15;
const int kNumNodes = kNumPartitions * kNumPartitions;
const int kExpectedCliqueSize = kNumPartitions;
const double kTimeLimitSeconds = 1.0;
absl::SetFlag(&FLAGS_time_limit_use_usertime, true);
TimeLimit time_limit(kTimeLimitSeconds);
const auto graph = [kNumPartitions](int index1, int index2) {
return FullKPartiteGraph(kNumPartitions, index1, index2);
};
CliqueSizeVerifier verifier(kExpectedCliqueSize, kExpectedCliqueSize);
BronKerboschAlgorithm<int> bron_kerbosch(graph, kNumNodes,
verifier.MakeCliqueCallback());
const BronKerboschAlgorithmStatus status =
bron_kerbosch.RunWithTimeLimit(&time_limit);
EXPECT_EQ(BronKerboschAlgorithmStatus::INTERRUPTED, status);
EXPECT_TRUE(time_limit.LimitReached());
}
TEST(BronKerboschAlgorithmTest, DeterministicTimeLimit) {
const int kNumPartitions = 15;
const int kNumNodes = kNumPartitions * kNumPartitions;
const int kExpectedCliqueSize = kNumPartitions;
const double kDeterministicLimit = 1.0;
absl::SetFlag(&FLAGS_time_limit_use_usertime, true);
std::unique_ptr<TimeLimit> time_limit =
TimeLimit::FromDeterministicTime(kDeterministicLimit);
const auto graph = [kNumPartitions](int index1, int index2) {
return FullKPartiteGraph(kNumPartitions, index1, index2);
};
CliqueSizeVerifier verifier(kExpectedCliqueSize, kExpectedCliqueSize);
BronKerboschAlgorithm<int> bron_kerbosch(graph, kNumNodes,
verifier.MakeCliqueCallback());
const BronKerboschAlgorithmStatus status =
bron_kerbosch.RunWithTimeLimit(time_limit.get());
EXPECT_EQ(BronKerboschAlgorithmStatus::INTERRUPTED, status);
EXPECT_TRUE(time_limit->LimitReached());
}
// A benchmark that finds all maximal cliques in a modulo graph of the given
// size.
void BM_FindCliquesInModuloGraph(benchmark::State& state) {
int num_partitions = state.range(0);
int partition_size = state.range(1);
const int kExpectedNumCliques = num_partitions;
const int kExpectedCliqueSize = partition_size;
const int kGraphSize = num_partitions * partition_size;
CliqueSizeVerifier verifier(kExpectedCliqueSize, kExpectedCliqueSize);
for (auto _ : state) {
auto graph = absl::bind_front(ModuloGraph, num_partitions);
auto callback =
absl::bind_front(&CliqueSizeVerifier::AppendClique, &verifier);
operations_research::FindCliques(
::util::functional::ToPermanentCallback(graph), kGraphSize,
::util::functional::ToPermanentCallback(callback));
}
EXPECT_EQ(state.max_iterations * kExpectedNumCliques, verifier.num_cliques());
}
BENCHMARK(BM_FindCliquesInModuloGraph)
->ArgPair(5, 1000)
->ArgPair(10, 500)
->ArgPair(50, 100)
->ArgPair(100, 50)
->ArgPair(500, 10)
->ArgPair(1000, 5);
void BM_FindCliquesInModuloGraphWithBronKerboschAlgorithm(
benchmark::State& state) {
int num_partitions = state.range(0);
int partition_size = state.range(1);
const int kExpectedNumCliques = num_partitions;
const int kExpectedCliqueSize = partition_size;
const int kGraphSize = num_partitions * partition_size;
CliqueSizeVerifier verifier(kExpectedCliqueSize, kExpectedCliqueSize);
std::unique_ptr<TimeLimit> time_limit = TimeLimit::Infinite();
auto graph = [num_partitions](int index1, int index2) {
return ModuloGraph(num_partitions, index1, index2);
};
for (auto _ : state) {
BronKerboschAlgorithm<int> bron_kerbosch(graph, kGraphSize,
verifier.MakeCliqueCallback());
bron_kerbosch.RunWithTimeLimit(time_limit.get());
}
EXPECT_EQ(state.max_iterations * kExpectedNumCliques, verifier.num_cliques());
LOG(INFO) << time_limit->DebugString();
}
BENCHMARK(BM_FindCliquesInModuloGraphWithBronKerboschAlgorithm)
->ArgPair(5, 1000)
->ArgPair(10, 500)
->ArgPair(50, 100)
->ArgPair(100, 50)
->ArgPair(500, 10)
->ArgPair(1000, 5);
// A benchmark that finds all maximal cliques in a 7-partite graph (a graph
// where the nodes are divided into 7 groups of size 7; each node is connected
// to all nodes in other groups but to no node in the same group). This graph
// contains a large number of relatively small cliques.
void BM_FindCliquesInFull7PartiteGraph(benchmark::State& state) {
constexpr int kNumPartitions = 7;
constexpr int kExpectedNumCliques = 7 * 7 * 7 * 7 * 7 * 7 * 7;
constexpr int kExpectedCliqueSize = kNumPartitions;
CliqueSizeVerifier verifier(kExpectedCliqueSize, kExpectedCliqueSize);
for (auto _ : state) {
auto graph = absl::bind_front(FullKPartiteGraph, kNumPartitions);
auto callback =
absl::bind_front(&CliqueSizeVerifier::AppendClique, &verifier);
operations_research::FindCliques(
::util::functional::ToPermanentCallback(graph),
kNumPartitions * kNumPartitions,
::util::functional::ToPermanentCallback(callback));
}
EXPECT_EQ(state.max_iterations * kExpectedNumCliques, verifier.num_cliques());
}
BENCHMARK(BM_FindCliquesInFull7PartiteGraph);
void BM_FindCliquesInFullKPartiteGraphWithBronKerboschAlgorithm(
benchmark::State& state) {
int num_partitions = state.range(0);
const int kExpectedNumCliques =
::MathUtil::IPow(num_partitions, num_partitions);
const int kExpectedCliqueSize = num_partitions;
const auto graph = [num_partitions](int index1, int index2) {
return FullKPartiteGraph(num_partitions, index1, index2);
};
CliqueSizeVerifier verifier(kExpectedCliqueSize, kExpectedCliqueSize);
std::unique_ptr<TimeLimit> time_limit = TimeLimit::Infinite();
for (auto _ : state) {
BronKerboschAlgorithm<int> bron_kerbosch(
graph, num_partitions * num_partitions, verifier.MakeCliqueCallback());
bron_kerbosch.RunWithTimeLimit(time_limit.get());
}
EXPECT_EQ(state.max_iterations * kExpectedNumCliques, verifier.num_cliques());
LOG(INFO) << time_limit->DebugString();
}
BENCHMARK(BM_FindCliquesInFullKPartiteGraphWithBronKerboschAlgorithm)
->Arg(5)
->Arg(6)
->Arg(7);
void BM_FindCliquesInRandomGraphWithBronKerboschAlgorithm(
benchmark::State& state) {
int num_nodes = state.range(0);
int arc_probability_permille = state.range(1);
const double arc_probability = arc_probability_permille / 1000.0;
const absl::flat_hash_set<std::pair<int, int>> adjacency_matrix =
MakeRandomGraphAdjacencyMatrix(num_nodes, arc_probability,
absl::GetFlag(FLAGS_test_random_seed));
const auto graph = [adjacency_matrix](int index1, int index2) {
return BitmapGraph(adjacency_matrix, index1, index2);
};
CliqueSizeVerifier verifier(0, num_nodes);
std::unique_ptr<TimeLimit> time_limit = TimeLimit::Infinite();
for (auto _ : state) {
BronKerboschAlgorithm<int> bron_kerbosch(graph, num_nodes,
verifier.MakeCliqueCallback());
bron_kerbosch.RunWithTimeLimit(time_limit.get());
}
LOG(INFO) << time_limit->DebugString();
}
BENCHMARK(BM_FindCliquesInRandomGraphWithBronKerboschAlgorithm)
->ArgPair(50, 800)
->ArgPair(100, 500)
->ArgPair(200, 100)
->ArgPair(1000, 10)
->ArgPair(1000, 20)
->ArgPair(1000, 50)
->ArgPair(1000, 100)
->ArgPair(10000, 1);
} // namespace
} // namespace operations_research