46# include <gtest/gtest.h>
76 const DynArray<std::pair<size_t, size_t>> & edges,
80 for (
size_t i = 0; i < n; ++i)
81 static_cast<void>(g.
insert_node(
static_cast<int>(i)));
87 const size_t id =
static_cast<size_t>(p->
get_info());
92 for (
size_t i = 0; i < n; ++i)
94 <<
"build_graph(): node id " << i <<
" not found after insertion";
96 for (
const auto & [u, v] : edges)
108 std::initializer_list<std::pair<size_t, size_t>> edges,
112 for (
const auto & edge : edges)
113 aleph_edges.append(edge);
114 EXPECT_EQ(aleph_edges.
size(), edges.size());
116 return build_graph<GT>(n, aleph_edges, arc_info);
125 for (
typename GT::Arc_Iterator it(g); it.has_curr(); it.next_ne())
126 arcs_in_graph.
insert(it.get_curr());
129 for (
auto it = matching.
get_it(); it.has_curr(); it.next_ne())
131 auto * arc = it.get_curr();
135 if (not arcs_in_graph.
contains(arc))
140 if (src ==
nullptr or tgt ==
nullptr)
157 (
size_t n,
const DynArray<std::pair<size_t, size_t>> & edges)
161 for (
size_t i = 0; i < edges.size(); ++i)
163 auto [u, v] = edges(i);
164 if (u >= n or v >= n or u == v)
168 seen.
insert(std::make_pair(u, v));
172 for (
const auto & p : seen)
173 simple_edges.append(p);
178 size_t exact_maximum_matching_size(
size_t n,
179 const DynArray<std::pair<size_t, size_t>> & edges)
188 auto simple = normalize_simple_edges(n, edges);
189 for (
size_t i = 0; i < simple.
size(); ++i)
191 auto [u, v] = simple(i);
192 adj(u) |= (uint64_t{1} << v);
193 adj(v) |= (uint64_t{1} << u);
196 const size_t state_count =
static_cast<size_t>(uint64_t{1} << n);
199 std::function<int(uint64_t)> solve = [&](uint64_t mask) ->
int
204 int & ans = memo(
static_cast<size_t>(mask));
208 const size_t i =
static_cast<size_t>(std::countr_zero(mask));
209 const uint64_t rest = mask & ~(uint64_t{1} << i);
213 uint64_t
options = adj(i) & rest;
216 const size_t j =
static_cast<size_t>(std::countr_zero(
options));
219 1 + solve(rest & ~(uint64_t{1} << j)));
225 const uint64_t
all = (uint64_t{1} << n) - 1;
226 return static_cast<size_t>(solve(
all));
232 return blossom_maximum_cardinality_matching<GT>(g, matching);
236 GT build_bipartite_graph(
size_t left_size,
238 const DynArray<std::pair<size_t, size_t>> & edges_lr)
241 const size_t n = left_size + right_size;
243 for (
size_t i = 0; i < left_size; ++i)
244 static_cast<void>(g.
insert_node(
static_cast<int>(i)));
246 for (
size_t i = 0; i < right_size; ++i)
247 static_cast<void>(g.
insert_node(
static_cast<int>(left_size + i)));
253 const size_t id =
static_cast<size_t>(p->
get_info());
258 for (
size_t i = 0; i < n; ++i)
260 <<
"build_bipartite_graph(): node id " << i <<
" not found after insertion";
262 for (
size_t i = 0; i < edges_lr.size(); ++i)
264 auto [
l,
r] = edges_lr(i);
265 if (
l >= left_size or
r >= right_size)
274 class BlossomMatchingTypedTest :
public ::testing::Test
278 using GraphBackends = ::testing::Types<
290 using Graph = TypeParam;
295 const size_t cardinality = run_blossom(g, matching);
297 EXPECT_EQ(cardinality, 0u);
304 using Graph = TypeParam;
306 auto g = build_graph<Graph>(2, {{0, 1}});
309 const size_t cardinality = run_blossom(g, matching);
311 EXPECT_EQ(cardinality, 1u);
312 EXPECT_EQ(matching.size(), 1u);
318 using Graph = TypeParam;
320 auto g = build_graph<Graph>(5, {{0, 1}, {1, 2}, {2, 3}, {3, 4}, {4, 0}});
323 const size_t cardinality = run_blossom(g, matching);
325 EXPECT_EQ(cardinality, 2u);
326 EXPECT_EQ(matching.size(), 2u);
332 using Graph = TypeParam;
335 for (
size_t i = 0; i < 6; ++i)
336 for (
size_t j = i + 1; j < 6; ++j)
337 edges.
append(std::make_pair(i, j));
338 ASSERT_EQ(edges.
size(), 15u);
340 auto g = build_graph<Graph>(6, edges);
343 const size_t cardinality = run_blossom(g, matching);
345 EXPECT_EQ(cardinality, 3u);
346 EXPECT_EQ(matching.
size(), 3u);
350TYPED_TEST(BlossomMatchingTypedTest, BlossomContractionScenario)
352 using Graph = TypeParam;
357 auto g = build_graph<Graph>(8,
358 {{0, 1}, {1, 2}, {2, 3}, {3, 4}, {4, 0},
359 {1, 5}, {2, 6}, {3, 7}});
362 const size_t cardinality = run_blossom(g, matching);
364 EXPECT_EQ(cardinality, 4u);
365 EXPECT_EQ(matching.size(), 4u);
369TYPED_TEST(BlossomMatchingTypedTest, HandlesLoopsAndParallelArcs)
371 using Graph = TypeParam;
373 auto g = build_graph<Graph>(4,
384 const size_t cardinality = run_blossom(g, matching);
386 EXPECT_EQ(cardinality, 2u);
387 EXPECT_EQ(matching.size(), 2u);
391TYPED_TEST(BlossomMatchingTypedTest, FunctorWrapperMatchesFreeFunction)
393 using Graph = TypeParam;
395 auto g = build_graph<Graph>(7,
396 {{0, 1}, {1, 2}, {2, 0}, {2, 3}, {3, 4},
397 {4, 5}, {5, 6}, {1, 6}});
402 const size_t c1 = blossom_maximum_cardinality_matching<Graph>(g, free_matching);
406 EXPECT_EQ(free_matching.size(), functor_matching.
size());
411TYPED_TEST(BlossomMatchingTypedTest, RandomGraphsAgreeWithExactDP)
413 using Graph = TypeParam;
415 std::mt19937
rng(20260220);
416 std::uniform_real_distribution<double> coin(0.0, 1.0);
418 for (
size_t n = 2; n <= 10; ++n)
419 for (
int sample = 0; sample < 30; ++sample)
421 const double p = 0.10 + 0.03 *
static_cast<double>(sample);
424 for (
size_t u = 0; u < n; ++u)
425 for (
size_t v = u + 1; v < n; ++v)
427 edges.
append(std::make_pair(u, v));
428 ASSERT_TRUE(edges.
all([] (
const auto & e) { return e.first < e.second; }));
430 auto g = build_graph<Graph>(n, edges);
433 const size_t blossom_size = run_blossom(g, matching);
434 const size_t exact_size = exact_maximum_matching_size(n, edges);
436 EXPECT_EQ(blossom_size, exact_size)
437 <<
"n=" << n <<
" sample=" << sample;
438 EXPECT_EQ(matching.
size(), blossom_size)
439 <<
"n=" << n <<
" sample=" << sample;
441 <<
"n=" << n <<
" sample=" << sample;
445TYPED_TEST(BlossomMatchingTypedTest, AgreesWithHopcroftKarpOnBipartiteGraphs)
447 using Graph = TypeParam;
450 std::uniform_real_distribution<double> coin(0.0, 1.0);
452 for (
size_t left = 1; left <= 6; ++left)
453 for (
size_t right = 1; right <= 6; ++right)
454 for (
int sample = 0; sample < 10; ++sample)
456 const double p = 0.15 + 0.07 *
static_cast<double>(sample);
459 for (
size_t l = 0;
l < left; ++
l)
460 for (
size_t r = 0;
r < right; ++
r)
463 edges_lr.
append(std::make_pair(
l,
r));
466 ASSERT_EQ(edges_lr.
size(), appended);
468 auto g = build_bipartite_graph<Graph>(left, right, edges_lr);
473 const size_t blossom_size =
474 blossom_maximum_cardinality_matching<Graph>(g, blossom_matching);
475 hopcroft_karp_matching<Graph>(g, hk_matching);
477 EXPECT_EQ(blossom_size, hk_matching.
size())
478 <<
"left=" << left <<
" right=" << right <<
" sample=" << sample;
484TYPED_TEST(BlossomMatchingTypedTest, NestedOddCyclesWithStems)
486 using Graph = TypeParam;
495 {0, 1}, {1, 2}, {2, 3}, {3, 4}, {4, 0},
496 {2, 5}, {5, 6}, {6, 7}, {7, 8}, {8, 2},
497 {1, 9}, {4, 10}, {6, 11}
500 auto g = build_graph<Graph>(12, edges);
503 const size_t blossom_size = run_blossom(g, matching);
504 const size_t exact_size = exact_maximum_matching_size(12, edges);
506 EXPECT_EQ(blossom_size, exact_size);
507 EXPECT_EQ(matching.
size(), blossom_size);
511TYPED_TEST(BlossomMatchingTypedTest, FlowerWithSharedCenterAndBridge)
513 using Graph = TypeParam;
523 {0, 1}, {1, 2}, {2, 0},
524 {0, 3}, {3, 4}, {4, 0},
525 {0, 5}, {5, 6}, {6, 0},
526 {2, 7}, {7, 8}, {8, 9}
529 auto g = build_graph<Graph>(10, edges);
532 const size_t blossom_size = run_blossom(g, matching);
533 const size_t exact_size = exact_maximum_matching_size(10, edges);
535 EXPECT_EQ(blossom_size, exact_size);
536 EXPECT_EQ(matching.
size(), blossom_size);
540TYPED_TEST(BlossomMatchingTypedTest, DenseRandomStressCrossBackendAgreement)
542 using Graph = TypeParam;
544 std::mt19937
rng(0xB10550u);
545 std::uniform_real_distribution<double> coin(0.0, 1.0);
548 for (
int sample = 0; sample < 12; ++sample)
550 const size_t n = 30 +
static_cast<size_t>(sample % 11);
551 const double p = 0.16 + 0.01 *
static_cast<double>(sample);
554 for (
size_t u = 0; u < n; ++u)
555 for (
size_t v = u + 1; v < n; ++v)
557 edges.
append(std::make_pair(u, v));
558 ASSERT_TRUE(edges.
all([] (
const auto & e) { return e.first < e.second; }));
560 auto g = build_graph<Graph>(n, edges);
562 const size_t blossom_size = run_blossom(g, matching);
564 EXPECT_EQ(matching.
size(), blossom_size)
565 <<
"sample=" << sample <<
" n=" << n;
567 <<
"sample=" << sample <<
" n=" << n;
568 EXPECT_LE(blossom_size, n / 2)
569 <<
"sample=" << sample <<
" n=" << n;
573TYPED_TEST(BlossomMatchingTypedTest, BlossomMatchingPerfRegression)
575 using Graph = TypeParam;
577 const char * run_perf = std::getenv(
"ALEPH_RUN_BLOSSOM_PERF");
578 if (run_perf ==
nullptr or std::string(run_perf) !=
"1")
579 GTEST_SKIP() <<
"Set ALEPH_RUN_BLOSSOM_PERF=1 to run blossom perf regression test";
581 constexpr size_t n = 900;
582 constexpr double p = 0.02;
583 constexpr unsigned seed = 0xB10550u;
585 constexpr long long kBudgetMs = 12000;
587 constexpr long long kBudgetMs = 25000;
591 std::uniform_real_distribution<double> coin(0.0, 1.0);
594 for (
size_t u = 0; u < n; ++u)
595 for (
size_t v = u + 1; v < n; ++v)
597 edges.
append(std::make_pair(u, v));
600 edges.
append(std::make_pair(0, 1));
601 EXPECT_TRUE(edges.
all([] (
const auto & e) { return e.first < e.second; }));
603 auto g = build_graph<Graph>(n, edges);
606 const auto t0 = std::chrono::steady_clock::now();
607 const size_t blossom_size = run_blossom(g, matching);
608 const auto elapsed_ms = std::chrono::duration_cast<std::chrono::milliseconds>(
609 std::chrono::steady_clock::now() - t0).count();
611 EXPECT_EQ(matching.
size(), blossom_size);
613 EXPECT_LE(blossom_size, n / 2);
614 EXPECT_LT(elapsed_ms, kBudgetMs)
615 <<
"Blossom perf regression: n=" << n
616 <<
", edges=" << edges.
size()
617 <<
", elapsed_ms=" << elapsed_ms
618 <<
", budget_ms=" << kBudgetMs;
627 struct Positive_Arc_Filter
630 bool operator()(
Arc * a)
const noexcept
632 return a->get_info() > 0;
637TEST(BlossomMatchingStandaloneTest, ArcFilterIsRespected)
655 const size_t cardinality =
656 blossom_maximum_cardinality_matching<Graph, Positive_Arc_Filter>(
657 g, matching, Positive_Arc_Filter());
659 EXPECT_EQ(cardinality, 1u);
660 EXPECT_EQ(matching.
size(), 1u);
664TEST(BlossomMatchingStandaloneTest, DigraphThrowsDomainError)
669 auto * n0 = g.insert_node(0);
670 auto * n1 = g.insert_node(1);
671 g.insert_arc(n0, n1, 1);
675 EXPECT_THROW(blossom_maximum_cardinality_matching<Digraph>(g, matching),
Edmonds' Blossom algorithm for maximum matching in general graphs.
#define ah_runtime_error_if(C)
Throws std::runtime_error if condition holds.
TYPED_TEST_SUITE(ArcHeapLifecycle, ArcHeapKinds)
bool verify_matching(const Graph &g, const DynDlist< Graph::Arc * > &matching)
Verifies that a matching is valid:
TYPED_TEST(BlossomMatchingTypedTest, EmptyGraph)
Simple dynamic array with automatic resizing and functional operations.
constexpr size_t size() const noexcept
Return the number of elements stored in the stack.
Functor wrapper for maximum cardinality general matching.
Generic directed graph (digraph) wrapper template.
constexpr bool is_empty() const noexcept
Return true if this (as header node) is empty.
size_t size() const noexcept
Return the current dimension of array.
T & append()
Allocate a new entry to the end of array.
bool is_empty() const noexcept
Return true if the array is empty.
Dynamic doubly linked list with O(1) size and bidirectional access.
const size_t & size() const noexcept
Return the number of elements (constant time)
Dynamic set backed by balanced binary search trees with automatic memory management.
Key * insert(const Key &key)
Inserts a key into the dynamic set.
std::pair< Key *, bool > contains_or_insert(const Key &key)
Arc for graphs implemented with simple adjacency lists.
virtual Node * insert_node(Node *node) noexcept
Insertion of a node already allocated.
Arc * insert_arc(Node *src_node, Node *tgt_node, void *a)
Graph class implemented with singly-linked adjacency lists.
Open addressing hash table with linear probing collision resolution.
constexpr bool contains(const Key &key) const noexcept
Alias for has().
bool all(Operation &operation) const
Check if all the elements of the container satisfy a condition.
NodeInfo & get_info() noexcept
Return a modifiable reference to the data contained in the node.
Node * get_src_node(Arc *arc) const noexcept
Return the source node of arc (only for directed graphs)
void for_each_node(Operation &operation) const
Unconditionally traverse all the nodes of graph and on each one perform an operation.
Node * get_tgt_node(Arc *arc) const noexcept
Return the target node of arc (only for directed graphs)
auto get_it()
Return a properly initialized iterator positioned at the first item on the container.
Key * insert(const Key &key)
Inserts a key into the hash table (copy version).
DynArray< Graph::Node * > nodes
Main namespace for Aleph-w library functions.
bool all(Container &container, Operation &operation)
Return true if all elements satisfy a predicate.
Built_Graph_T< Graph > build_graph(const size_t n, const std::vector< Edge_Def > &edges)
static struct argp_option options[]
Arc of graph implemented with double-linked adjacency lists.
Array-based graph implementation.
Dynamic array container with automatic resizing.
Bipartite graph detection and 2-coloring.
Lazy and scalable dynamic array implementation.
Dynamic set implementations based on balanced binary search trees.
Generic graph and digraph implementations.
Open addressing hash table with linear probing.
Simple graph implementation with adjacency lists.