49#include <gtest/gtest.h>
77 vector<Graph::Node *> left(
m);
78 for (
size_t i = 0; i <
m; ++i)
82 vector<Graph::Node *> right(n);
83 for (
size_t j = 0; j < n; ++j)
87 for (
size_t i = 0; i <
m; ++i)
88 for (
size_t j = 0; j < n; ++j)
105 vector<Graph::Node *>
nodes(n);
106 for (
size_t i = 0; i < n; ++i)
109 for (
size_t i = 0; i + 1 < n; ++i)
126 vector<Graph::Node *>
nodes(n);
127 for (
size_t i = 0; i < n; ++i)
130 for (
size_t i = 0; i < n; ++i)
146 for (
size_t i = 0; i < n; ++i)
148 auto * leaf = g.
insert_node(
static_cast<int>(i + 1));
208 for (
auto it =
l.
get_it(); it.has_curr(); it.next_ne())
209 left_set.
insert(it.get_curr());
211 for (
auto it =
r.get_it(); it.has_curr(); it.next_ne())
212 right_set.
insert(it.get_curr());
215 for (
auto it =
l.
get_it(); it.has_curr(); it.next_ne())
216 if (right_set.
contains(it.get_curr()))
222 auto * arc = it.get_curr();
226 bool src_in_left = left_set.
contains(src);
227 bool src_in_right = right_set.
contains(src);
228 bool tgt_in_left = left_set.
contains(tgt);
229 bool tgt_in_right = right_set.
contains(tgt);
232 if (!(src_in_left || src_in_right) || !(tgt_in_left || tgt_in_right))
236 if (src_in_left == tgt_in_left)
252 for (
auto it = matching.
get_it(); it.has_curr(); it.next_ne())
254 auto * arc = it.get_curr();
262 matched_nodes.
insert(src);
263 matched_nodes.
insert(tgt);
273TEST(Bipartite, EmptyGraphReturnsEmpty)
278 EXPECT_NO_THROW(compute_bipartite<Graph>(g,
l,
r));
280 EXPECT_TRUE(
r.is_empty());
290 compute_bipartite<Graph>(g,
l,
r);
293 EXPECT_EQ(
l.
size() +
r.size(), 1u);
296TEST(Bipartite, TwoConnectedNodes)
305 compute_bipartite<Graph>(g,
l,
r);
307 EXPECT_EQ(
l.
size(), 1u);
308 EXPECT_EQ(
r.size(), 1u);
318 compute_bipartite<Graph>(g,
l,
r);
320 EXPECT_EQ(
l.
size() +
r.size(), 4u);
321 EXPECT_EQ(
l.
size(), 2u);
322 EXPECT_EQ(
r.size(), 2u);
332 compute_bipartite<Graph>(g,
l,
r);
334 EXPECT_EQ(
l.
size() +
r.size(), 5u);
344 compute_bipartite<Graph>(g,
l,
r);
346 EXPECT_EQ(
l.
size() +
r.size(), 6u);
348 EXPECT_TRUE((
l.
size() == 1 &&
r.size() == 5) ||
349 (
l.
size() == 5 &&
r.size() == 1));
353TEST(Bipartite, CompleteBipartiteK22)
359 compute_bipartite<Graph>(g,
l,
r);
361 EXPECT_EQ(
l.
size(), 2u);
362 EXPECT_EQ(
r.size(), 2u);
366TEST(Bipartite, CompleteBipartiteK33)
372 compute_bipartite<Graph>(g,
l,
r);
374 EXPECT_EQ(
l.
size(), 3u);
375 EXPECT_EQ(
r.size(), 3u);
379TEST(Bipartite, CompleteBipartiteK25)
385 compute_bipartite<Graph>(g,
l,
r);
387 EXPECT_EQ(
l.
size() +
r.size(), 7u);
397 compute_bipartite<Graph>(g,
l,
r);
399 EXPECT_EQ(
l.
size(), 3u);
400 EXPECT_EQ(
r.size(), 3u);
414 EXPECT_THROW(compute_bipartite<Graph>(g,
l,
r), domain_error);
423 EXPECT_THROW(compute_bipartite<Graph>(g,
l,
r), domain_error);
426TEST(Bipartite, LargeOddCycleThrows)
432 EXPECT_THROW(compute_bipartite<Graph>(g,
l,
r), domain_error);
435TEST(Bipartite, CompleteGraphK3Throws)
442 EXPECT_THROW(compute_bipartite<Graph>(g,
l,
r), domain_error);
445TEST(Bipartite, GraphWithOddCycleAttached)
463 EXPECT_THROW(compute_bipartite<Graph>(g,
l,
r), domain_error);
473TEST(Bipartite, TwoDisconnectedNodes)
482 compute_bipartite<Graph>(g,
l,
r);
485 EXPECT_EQ(
l.
size(), 2u);
486 EXPECT_EQ(
r.size(), 0u);
489TEST(Bipartite, DisconnectedBipartiteComponents)
495 compute_bipartite<Graph>(g,
l,
r);
497 EXPECT_EQ(
l.
size(), 2u);
498 EXPECT_EQ(
r.size(), 2u);
506TEST(ComputeBipartiteClass, BasicUsage)
514 EXPECT_EQ(
l.
size() +
r.size(), 7u);
518TEST(ComputeBipartiteClass, ThrowsOnNonBipartite)
530TEST(MaximumMatching, EmptyGraphReturnsEmpty)
538 compute_maximum_cardinality_bipartite_matching<Graph>(g, matching));
542TEST(MaximumMatching, SingleEdge)
551 compute_maximum_cardinality_bipartite_matching<Graph>(g, matching);
553 EXPECT_EQ(matching.
size(), 1u);
557TEST(MaximumMatching, PathGraph4)
563 compute_maximum_cardinality_bipartite_matching<Graph>(g, matching);
566 EXPECT_EQ(matching.
size(), 2u);
570TEST(MaximumMatching, PathGraph5)
576 compute_maximum_cardinality_bipartite_matching<Graph>(g, matching);
579 EXPECT_EQ(matching.
size(), 2u);
583TEST(MaximumMatching, CompleteBipartiteK22)
589 compute_maximum_cardinality_bipartite_matching<Graph>(g, matching);
592 EXPECT_EQ(matching.
size(), 2u);
596TEST(MaximumMatching, CompleteBipartiteK33)
602 compute_maximum_cardinality_bipartite_matching<Graph>(g, matching);
605 EXPECT_EQ(matching.
size(), 3u);
609TEST(MaximumMatching, CompleteBipartiteK55)
615 compute_maximum_cardinality_bipartite_matching<Graph>(g, matching);
618 EXPECT_EQ(matching.
size(), 5u);
622TEST(MaximumMatching, UnbalancedK25)
628 compute_maximum_cardinality_bipartite_matching<Graph>(g, matching);
631 EXPECT_EQ(matching.
size(), 2u);
635TEST(MaximumMatching, UnbalancedK52)
641 compute_maximum_cardinality_bipartite_matching<Graph>(g, matching);
644 EXPECT_EQ(matching.
size(), 2u);
648TEST(MaximumMatching, StarGraph)
654 compute_maximum_cardinality_bipartite_matching<Graph>(g, matching);
657 EXPECT_EQ(matching.
size(), 1u);
661TEST(MaximumMatching, EvenCycle)
667 compute_maximum_cardinality_bipartite_matching<Graph>(g, matching);
670 EXPECT_EQ(matching.
size(), 3u);
674TEST(MaximumMatching, ThrowsOnNonBipartite)
681 compute_maximum_cardinality_bipartite_matching<Graph>(g, matching),
689TEST(MaximumMatchingClass, BasicUsage)
697 EXPECT_EQ(matching.
size(), 4u);
701TEST(MaximumMatchingClass, ThrowsOnNonBipartite)
716TEST(BipartiteStress, LargeBipartiteGraph)
722 compute_bipartite<Graph>(g,
l,
r);
724 EXPECT_EQ(
l.
size() +
r.size(), 100u);
728TEST(BipartiteStress, LargePathGraph)
734 compute_bipartite<Graph>(g,
l,
r);
736 EXPECT_EQ(
l.
size(), 50u);
737 EXPECT_EQ(
r.size(), 50u);
741TEST(MaximumMatchingStress, LargeMatching)
747 compute_maximum_cardinality_bipartite_matching<Graph>(g, matching);
749 EXPECT_EQ(matching.
size(), 20u);
753TEST(BipartiteStress, LargeEvenCycle)
759 compute_bipartite<Graph>(g,
l,
r);
761 EXPECT_EQ(
l.
size(), 50u);
762 EXPECT_EQ(
r.size(), 50u);
770TEST(Bipartite, MultipleEdgesBetweenSameNodes)
783 compute_bipartite<Graph>(g,
l,
r);
785 EXPECT_EQ(
l.
size(), 1u);
786 EXPECT_EQ(
r.size(), 1u);
789TEST(Bipartite, IsolatedNodeWithBipartiteComponent)
803 compute_bipartite<Graph>(g,
l,
r);
805 EXPECT_EQ(
l.
size(), 2u);
806 EXPECT_EQ(
r.size(), 1u);
813TEST(MaximumMatching, DisconnectedComponents)
829 compute_maximum_cardinality_bipartite_matching<Graph>(g, matching);
830 EXPECT_EQ(matching.
size(), 3u);
835TEST(MaximumMatching, NodesWithoutArcs)
847 compute_maximum_cardinality_bipartite_matching<Graph>(g, matching);
848 EXPECT_EQ(matching.
size(), 1u);
855 compute_maximum_cardinality_bipartite_matching<Graph>(lonely,
none);
856 EXPECT_TRUE(
none.is_empty());
863TEST(BipartiteColor, EnumValues)
882 EXPECT_NO_THROW(hopcroft_karp_matching<Graph>(g, matching));
886TEST(HopcroftKarp, SingleIsolatedNode)
892 hopcroft_karp_matching<Graph>(g, matching);
905 hopcroft_karp_matching<Graph>(g, matching);
907 EXPECT_EQ(matching.
size(), 1u);
918 hopcroft_karp_matching<Graph>(g, matching);
920 EXPECT_EQ(matching.
size(), 2u);
929 hopcroft_karp_matching<Graph>(g, matching);
931 EXPECT_EQ(matching.
size(), 2u);
935TEST(HopcroftKarp, CompleteBipartiteK22)
940 hopcroft_karp_matching<Graph>(g, matching);
942 EXPECT_EQ(matching.
size(), 2u);
946TEST(HopcroftKarp, CompleteBipartiteK33)
951 hopcroft_karp_matching<Graph>(g, matching);
953 EXPECT_EQ(matching.
size(), 3u);
957TEST(HopcroftKarp, CompleteBipartiteK55)
962 hopcroft_karp_matching<Graph>(g, matching);
964 EXPECT_EQ(matching.
size(), 5u);
968TEST(HopcroftKarp, UnbalancedK25)
973 hopcroft_karp_matching<Graph>(g, matching);
975 EXPECT_EQ(matching.
size(), 2u);
984 hopcroft_karp_matching<Graph>(g, matching);
986 EXPECT_EQ(matching.
size(), 1u);
995 hopcroft_karp_matching<Graph>(g, matching);
997 EXPECT_EQ(matching.
size(), 3u);
1009 EXPECT_THROW(hopcroft_karp_matching<Graph>(g, matching), domain_error);
1018 EXPECT_THROW(hopcroft_karp_matching<Graph>(g, matching), domain_error);
1023TEST(HopcroftKarp, TwoDisconnectedEdges)
1028 hopcroft_karp_matching<Graph>(g, matching);
1030 EXPECT_EQ(matching.
size(), 2u);
1034TEST(HopcroftKarp, DisconnectedWithIsolatedNodes)
1058 hopcroft_karp_matching<Graph>(g, matching);
1061 EXPECT_EQ(matching.
size(), 3u);
1065TEST(HopcroftKarp, TwoDisconnectedCompleteBipartite)
1070 vector<Graph::Node *>
l1(3), r1(3);
1071 for (
int i = 0; i < 3; ++i)
1073 for (
int i = 0; i < 3; ++i)
1075 for (
int i = 0; i < 3; ++i)
1076 for (
int j = 0; j < 3; ++j)
1080 vector<Graph::Node *>
l2(2), r2(2);
1081 for (
int i = 0; i < 2; ++i)
1083 for (
int i = 0; i < 2; ++i)
1085 for (
int i = 0; i < 2; ++i)
1086 for (
int j = 0; j < 2; ++j)
1090 hopcroft_karp_matching<Graph>(g, matching);
1093 EXPECT_EQ(matching.
size(), 5u);
1099TEST(HopcroftKarp, CrossValidateK44)
1104 hopcroft_karp_matching<Graph>(g, hk_matching);
1107 compute_maximum_cardinality_bipartite_matching<Graph>(g, ff_matching);
1109 EXPECT_EQ(hk_matching.
size(), ff_matching.
size());
1110 EXPECT_EQ(hk_matching.
size(), 4u);
1113TEST(HopcroftKarp, CrossValidatePath6)
1118 hopcroft_karp_matching<Graph>(g, hk_matching);
1121 compute_maximum_cardinality_bipartite_matching<Graph>(g, ff_matching);
1123 EXPECT_EQ(hk_matching.
size(), ff_matching.
size());
1124 EXPECT_EQ(hk_matching.
size(), 3u);
1127TEST(HopcroftKarp, CrossValidateEvenCycle8)
1132 hopcroft_karp_matching<Graph>(g, hk_matching);
1135 compute_maximum_cardinality_bipartite_matching<Graph>(g, ff_matching);
1137 EXPECT_EQ(hk_matching.
size(), ff_matching.
size());
1138 EXPECT_EQ(hk_matching.
size(), 4u);
1150 EXPECT_EQ(matching.
size(), 3u);
1154TEST(HopcroftKarp, FunctorMatchesFreeFunction)
1159 hopcroft_karp_matching<Graph>(g, fn_matching);
1162 EXPECT_EQ(fn_matching.
size(), op_matching.
size());
1172 hopcroft_karp_matching<Graph>(g, matching);
1174 EXPECT_EQ(matching.
size(), 50u);
1183 hopcroft_karp_matching<Graph>(g, matching);
1185 EXPECT_EQ(matching.
size(), 100u);
1194 hopcroft_karp_matching<Graph>(g, matching);
1196 EXPECT_EQ(matching.
size(), 100u);
1203TEST(MaxFlowMatching, ASparseRandomGraphMatchesAsHopcroftKarp)
1206 std::vector<Graph::Node *>
l,
r;
1207 for (
int i = 0; i < 1000; ++i)
1209 for (
int i = 0; i < 1000; ++i)
1211 std::mt19937 gen(1);
1212 for (
int i = 0; i < 1000; ++i)
1214 for (
int k = 0;
k < 2; ++
k)
1220 hopcroft_karp_matching<Graph>(g, hk);
1224 compute_maximum_cardinality_bipartite_matching<Graph>(g, ff);
1225 compute_maximum_cardinality_bipartite_matching<Graph, Edmonds_Karp_Maximum_Flow>(g, ek);
1226 compute_maximum_cardinality_bipartite_matching<Graph, Dinic_Maximum_Flow>(g, dinic);
1227 for (
auto *
m : {&ff, &ek, &dinic})
1236 ::testing::InitGoogleTest(&argc, argv);
1237 return RUN_ALL_TESTS();
Graph create_complete_bipartite(size_t m, size_t n)
Creates a complete bipartite graph K_{m,n} Left partition: nodes 0..m-1 Right partition: nodes m....
Graph create_path_graph(size_t n)
Creates a path graph with n nodes: 0–1–2–...–n-1 Path graphs are always bipartite.
Graph create_disconnected_bipartite()
Creates two disconnected components.
Graph create_triangle()
Creates a triangle (K_3) - the simplest non-bipartite graph.
bool verify_bipartition(const Graph &g, const DynDlist< Graph::Node * > &l, const DynDlist< Graph::Node * > &r)
Verifies that a bipartition is valid:
Graph create_cycle_graph(size_t n)
Creates a cycle graph with n nodes: 0–1–2–...–n-1–0 Even cycles are bipartite, odd cycles are not.
bool verify_matching(const Graph &g, const DynDlist< Graph::Arc * > &matching)
Verifies that a matching is valid:
Graph create_star_graph(size_t n)
Creates a star graph with center and n leaves Star graphs are always bipartite.
Class that takes a bipartite graph and computes the partition sets.
Class for computing the maximum cardinality matching of a bipartite graph.
constexpr bool is_empty() const noexcept
Return true if this (as header node) 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)
size_t size() const noexcept
Return the number of items of the list, in constant time.
Dynamic set implemented using AVL binary search trees of type Avl_Tree<Key>.
Key * insert(const Key &key)
Inserts a key into the dynamic set.
bool contains(const Key &key) const
Checks if a key exists in the set.
void next_ne() noexcept(shows_ne)
Advances the iterator to the next filtered element, as next() does.
constexpr bool is_empty() const noexcept
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)
Node * get_src_node(Arc *arc) const noexcept
Return the source node of arc (only for directed graphs)
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.
constexpr size_t size() const noexcept
Returns the number of entries in the table.
DynArray< Graph::Node * > nodes
Main namespace for Aleph-w library functions.
bool none(const Container &container, Operation &operation)
Return true if no element satisfies operation.
Filtered iterator on all the arcs of a graph.
Arc of graph implemented with double-linked adjacency lists.
FooMap m(5, fst_unit_pair_hash, snd_unit_pair_hash)
Bipartite graph detection and 2-coloring.
Generic graph and digraph implementations.
Advanced maximum flow algorithms.