55#include <gtest/gtest.h>
76 constexpr bool default_built = std::is_default_constructible_v<SA>;
81 std::set<int> arc_numbers(
const List &
l)
84 for (
auto it =
l.
get_it(); it.has_curr(); it.next_ne())
85 r.insert(it.get_curr()->get_info());
93 template <
class GT,
class SA>
94 size_t counted(
const SA & sa)
96 if constexpr (std::is_same_v<SA, graph_filters::Counting<GT>>)
97 return sa.calls ==
nullptr ? 0 : *sa.calls;
104 template <
class GT,
class SA>
105 void expect_called(
const SA & sa,
size_t before)
107 if constexpr (std::is_same_v<SA, graph_filters::Counting<GT>>)
109 EXPECT_GT(counted<GT>(sa), before) <<
"the filter given is not the one used";
120 s.
arcs = {{0, 1}, {2, 1}, {2, 3}};
134 struct Oriented_Cycle
137 std::vector<typename GT::Node *>
nodes;
141 for (
int i = 0; i < 4; ++i)
143 const int left =
typename GT::Node_Iterator(g).get_curr()->get_info() % 2;
144 for (
int i = 0; i < 4; ++i)
146 int u = i, v = (i + 1) % 4;
149 g.
insert_arc(nodes[
size_t(u)], nodes[
size_t(v)], i);
153 Oriented_Cycle(
const Oriented_Cycle &) =
delete;
154 Oriented_Cycle & operator = (
const Oriented_Cycle &) =
delete;
159 template <
class List>
160 void expect_cycle_matching(
const List &
m,
bool hidden)
162 const auto arcs = arc_numbers(
m);
165 EXPECT_EQ(
arcs, (std::set<int>{1, 3}));
169 EXPECT_TRUE(
arcs == (std::set<int>{0, 2}) or
arcs == (std::set<int>{1, 3}))
170 <<
m.
size() <<
" arcs";
180 s.
arcs = {{0, 1}, {1, 2}, {0, 2}};
185 template <
class List>
186 std::set<int> node_numbers(
const List &
l)
189 for (
auto it =
l.
get_it(); it.has_curr(); it.next_ne())
190 r.insert(it.get_curr()->get_info());
197 template <
class List>
198 void expect_path_bipartition(
const List &
l,
const List &
r)
200 const std::set<std::set<int>> sides = {node_numbers(
l), node_numbers(
r)};
201 EXPECT_EQ(sides, (std::set<std::set<int>>{{0, 2}, {1}}));
205 template <
class List>
206 void expect_maximum_matching(
const List &
m,
bool hidden)
208 const auto arcs = arc_numbers(
m);
211 EXPECT_EQ(
m.
size(), 1U);
216 EXPECT_EQ(
arcs, (std::set<int>{0, 2}));
232 using GT = TypeParam;
235 for_each_filter<GT>([&] (
auto sa,
const char * name)
237 using SA =
decltype(sa);
238 constexpr bool h = hides<GT, SA>;
242 DynDlist<typename GT::Node *> l, r, l2, r2;
243 Compute_Bipartite<GT, SA> bipartite(sa);
246 const size_t before = counted<GT>(sa);
247 compute_bipartite<GT, SA>(g, l, r, sa);
248 expect_path_bipartition(l, r);
249 expect_called<GT>(sa, before);
250 bipartite(g, l2, r2);
251 expect_path_bipartition(l2, r2);
255 EXPECT_THROW_OR_FILTER((compute_bipartite<GT, SA>(g, l, r, sa)), std::domain_error);
256 EXPECT_THROW_OR_FILTER(bipartite(g, l2, r2), std::domain_error);
259 if constexpr (default_built<SA>)
271 using GT = TypeParam;
274 for_each_filter<GT>([&] (
auto sa,
const char * name)
276 using SA =
decltype(sa);
277 constexpr bool h = hides<GT, SA>;
281 DynDlist<typename GT::Node *> l, r;
282 const size_t before = counted<GT>(sa);
283 const bool bipartite = compute_bipartite_all_components<GT, SA>(g, l, r, sa);
284 EXPECT_EQ(bipartite, h);
287 expect_path_bipartition(l, r);
289 expect_called<GT>(sa, before);
291 if constexpr (default_built<SA>)
294 EXPECT_FALSE((compute_bipartite_all_components<GT, SA>(g,
l2, r2)));
302 using GT = TypeParam;
303 Oriented_Cycle<GT> b;
305 for_each_filter<GT>([&] (
auto sa,
const char * name)
307 using SA =
decltype(sa);
308 constexpr bool h = hides<GT, SA>;
312 DynDlist<typename GT::Arc *> m, m2;
313 const size_t before = counted<GT>(sa);
314 compute_maximum_cardinality_bipartite_matching
315 <GT, Ford_Fulkerson_Maximum_Flow, SA>(g, m, sa);
316 expect_cycle_matching(m, h);
317 expect_called<GT>(sa, before);
319 Compute_Maximum_Cardinality_Bipartite_Matching
320 <GT, Ford_Fulkerson_Maximum_Flow, SA> matching(sa);
322 expect_cycle_matching(m2, h);
324 if constexpr (default_built<SA>)
326 DynDlist<typename GT::Arc *> m3;
327 Compute_Maximum_Cardinality_Bipartite_Matching
328 <GT, Ford_Fulkerson_Maximum_Flow, SA>()(g, m3);
329 expect_cycle_matching(m3, false);
337 using GT = TypeParam;
340 for_each_filter<GT>([&] (
auto sa,
const char * name)
342 using SA =
decltype(sa);
343 constexpr bool h = hides<GT, SA>;
347 DynDlist<typename GT::Arc *> m, m2;
348 const size_t before = counted<GT>(sa);
349 hopcroft_karp_matching<GT, SA>(g, m, sa);
350 expect_maximum_matching(m, h);
351 expect_called<GT>(sa, before);
353 Hopcroft_Karp_Matching<GT, SA> matching(sa);
355 expect_maximum_matching(m2, h);
357 if constexpr (default_built<SA>)
359 DynDlist<typename GT::Arc *> m3;
360 Hopcroft_Karp_Matching<GT, SA>()(g, m3);
361 expect_maximum_matching(m3, false);
372 struct Counting_Defaults
374 static inline size_t built = 0;
376 Counting_Defaults() noexcept { ++built; }
378 bool operator () (
typename GT::Arc * a)
const noexcept
380 return a->get_info() !=
Hidden;
390 using GT = TypeParam;
391 using SA = Counting_Defaults<GT>;
397 hopcroft_karp_matching<GT, SA>(b.
g,
m, sa);
398 expect_maximum_matching(
m,
true);
402 expect_maximum_matching(m2,
true);
403 EXPECT_EQ(SA::built, 0U);
406 hopcroft_karp_matching<GT, SA>(b.
g, m3);
407 expect_maximum_matching(m3,
true);
408 EXPECT_EQ(SA::built, 1U);
422 using GT = TypeParam;
425 for_each_filter<GT>([&] (
auto sa,
const char * name)
427 using SA =
decltype(sa);
431 DynDlist<typename GT::Arc *> m;
432 const size_t before = counted<GT>(sa);
433 const size_t n = compute_maximum_cardinality_general_matching(g, m, sa);
434 EXPECT_EQ(n, m.size());
435 expect_maximum_matching(m, hides<GT, SA>);
436 expect_called<GT>(sa, before);
443 using GT = TypeParam;
446 for_each_filter<GT>([&] (
auto sa,
const char * name)
448 using SA =
decltype(sa);
452 DynDlist<typename GT::Arc *> m;
453 const size_t n = blossom_maximum_cardinality_matching(g, m, sa);
454 EXPECT_EQ(n, m.size());
455 expect_maximum_matching(m, hides<GT, SA>);
462 using GT = TypeParam;
465 for_each_filter<GT>([&] (
auto sa,
const char * name)
467 using SA =
decltype(sa);
471 DynDlist<typename GT::Arc *> m;
472 const size_t before = counted<GT>(sa);
473 Compute_Maximum_Cardinality_General_Matching<GT, SA> matcher(sa);
474 EXPECT_EQ(matcher(g, m), m.size());
475 expect_maximum_matching(m, hides<GT, SA>);
476 expect_called<GT>(sa, before);
491 template <
class List,
class Result>
492 void expect_maximum_weight(
const List &
m,
const Result &
r,
bool hidden,
493 bool max_cardinality)
495 EXPECT_EQ(
r.cardinality,
m.
size());
498 EXPECT_EQ(
r.total_weight, 1);
499 EXPECT_EQ(arc_numbers(
m), (std::set<int>{1}));
503 EXPECT_EQ(
r.total_weight, 2);
507 EXPECT_EQ(arc_numbers(
m), (std::set<int>{0, 2}));
514 template <
class List,
class Result>
515 void expect_minimum_cost(
const List &
m,
const Result &
r,
bool hidden)
517 EXPECT_EQ(
r.cardinality,
m.
size());
520 EXPECT_EQ(
r.total_cost, 0);
521 EXPECT_EQ(arc_numbers(
m), (std::set<int>{0}));
525 EXPECT_EQ(
r.total_cost, 2);
526 EXPECT_EQ(arc_numbers(
m), (std::set<int>{0, 2}));
532 template <
class List,
class Result>
533 void expect_perfect(
const List &
m,
const Result &
r,
bool hidden)
537 EXPECT_FALSE(
r.feasible);
542 EXPECT_TRUE(
r.feasible);
543 EXPECT_EQ(
r.total_cost, 2);
544 EXPECT_EQ(arc_numbers(
m), (std::set<int>{0, 2}));
556 using GT = TypeParam;
559 for_each_filter<GT>([&] (
auto sa,
const char * name)
561 using SA =
decltype(sa);
565 for (bool max_cardinality : {false, true})
568 const size_t before = counted<GT>(sa);
571 expect_maximum_weight(
m,
r, hides<GT, SA>, max_cardinality);
572 expect_called<GT>(sa, before);
580 using GT = TypeParam;
583 for_each_filter<GT>([&] (
auto sa,
const char * name)
585 using SA =
decltype(sa);
589 for (bool max_cardinality : {false, true})
594 expect_maximum_weight(
m,
r, hides<GT, SA>, max_cardinality);
602 using GT = TypeParam;
605 for_each_filter<GT>([&] (
auto sa,
const char * name)
607 using SA =
decltype(sa);
611 DynDlist<typename GT::Arc *> m;
612 Compute_Maximum_Weight_General_Matching<GT, Dft_Dist<GT>, SA>
613 matcher(Dft_Dist<GT>(), sa, true);
614 const auto r = matcher(g, m);
615 expect_maximum_weight(m, r, hides<GT, SA>, true);
622 using GT = TypeParam;
625 for_each_filter<GT>([&] (
auto sa,
const char * name)
627 using SA =
decltype(sa);
631 DynDlist<typename GT::Arc *> m, m2;
632 const size_t before = counted<GT>(sa);
633 const auto r = compute_minimum_cost_general_matching
634 (g, m, Dft_Dist<GT>(), sa, true);
635 expect_minimum_cost(m, r, hides<GT, SA>);
636 expect_called<GT>(sa, before);
639 const auto r2 = compute_minimum_cost_general_matching(g, m2, Dft_Dist<GT>(), sa);
640 EXPECT_EQ(r2.total_cost, 0);
641 EXPECT_EQ(arc_numbers(m2).count(Hidden), 0U);
648 using GT = TypeParam;
651 for_each_filter<GT>([&] (
auto sa,
const char * name)
653 using SA =
decltype(sa);
657 DynDlist<typename GT::Arc *> m;
658 const auto r = blossom_minimum_cost_matching(g, m, Dft_Dist<GT>(), sa, true);
659 expect_minimum_cost(m, r, hides<GT, SA>);
666 using GT = TypeParam;
669 for_each_filter<GT>([&] (
auto sa,
const char * name)
671 using SA =
decltype(sa);
675 DynDlist<typename GT::Arc *> m, m2;
676 const size_t before = counted<GT>(sa);
677 const auto r = compute_minimum_cost_perfect_general_matching(g, m, Dft_Dist<GT>(), sa);
678 expect_perfect(m, r, hides<GT, SA>);
679 expect_called<GT>(sa, before);
681 const auto r2 = blossom_minimum_cost_perfect_matching(g, m2, Dft_Dist<GT>(), sa);
682 expect_perfect(m2, r2, hides<GT, SA>);
689 using GT = TypeParam;
692 for_each_filter<GT>([&] (
auto sa,
const char * name)
694 using SA =
decltype(sa);
698 DynDlist<typename GT::Arc *> m, m2;
699 Compute_Minimum_Cost_General_Matching<GT, Dft_Dist<GT>, SA>
700 matcher(Dft_Dist<GT>(), sa, true);
701 expect_minimum_cost(m, matcher(g, m), hides<GT, SA>);
703 Compute_Minimum_Cost_Perfect_General_Matching<GT, Dft_Dist<GT>, SA>
704 perfect(Dft_Dist<GT>(), sa);
705 expect_perfect(m2, perfect(g, m2), hides<GT, SA>);
Edmonds' Blossom algorithm for maximum matching in general graphs.
Maximum-weight matching in general undirected graphs.
Minimum-cost matching in general undirected graphs.
Class that takes a bipartite graph and computes the partition sets.
Default distance accessor for arc weights.
Node belonging to a double circular linked list with header node.
Dynamic doubly linked list with O(1) size and bidirectional access.
Functor wrapper for hopcroft_karp_matching.
virtual Node * insert_node(Node *node) noexcept
Insertion of a node already allocated.
Arc * insert_arc(Node *src_node, Node *tgt_node, void *a)
auto get_it()
Return a properly initialized iterator positioned at the first item on the container.
constexpr size_t size() const noexcept
Returns the number of entries in the table.
constexpr bool is_empty() const noexcept
Checks if the table is empty.
What the files of the contract matrix of the graph templates, graph_contract_matrix_*_test....
TYPED_TEST(ContractMatchingBipartite, ComputeBipartite)
TYPED_TEST_SUITE(ContractMatchingBipartite, Undirected_Kinds)
The kinds of arc filter and of distance that the graph algorithms must accept.
Every small multigraph, on every kind of graph, for the tests.
#define EXPECT_THROW_OR_FILTER(statement, type)
EXPECT_THROW(statement, type), for a statement that calls a filter: in a call of graph_harness::with_...
DynArray< Graph::Node * > nodes
DynArray< Graph::Arc * > arcs
Blossom_Weighted_Result< long long > compute_maximum_weight_general_matching(const GT &g, DynDlist< typename GT::Arc * > &matching, Weight weight=Weight(), SA sa=SA(), const bool max_cardinality=false)
Compute maximum-weight matching in a general undirected graph.
Blossom_Weighted_Result< long long > blossom_maximum_weight_matching(const GT &g, DynDlist< typename GT::Arc * > &matching, Weight weight=Weight(), SA sa=SA(), const bool max_cardinality=false)
Alias for compute_maximum_weight_general_matching().
Main namespace for Aleph-w library functions.
Itor::difference_type count(const Itor &beg, const Itor &end, const T &value)
Count elements equal to a value.
void for_each_constness(GT &g, F &&f)
Calls f(g) with g as it is and as a const graph.
constexpr int Hidden
The number of the arc that the filters hide.
::testing::Types< Aleph::List_Graph< Aleph::Graph_Node< int >, Aleph::Graph_Arc< int > >, Aleph::List_SGraph< Aleph::Graph_Snode< int >, Aleph::Graph_Sarc< int > >, Aleph::Array_Graph< Aleph::Graph_Anode< int >, Aleph::Graph_Aarc< int > > > Undirected_Kinds
The kinds of graph that are not directed, with int infos.
A graph made from a spec, with its nodes and arcs by number.
A small multigraph: its nodes are 0 .
std::vector< std::array< int, 2 > > arcs
FooMap m(5, fst_unit_pair_hash, snd_unit_pair_hash)
Bipartite graph detection and 2-coloring.
Generic graph and digraph implementations.