Aleph-w 7.10.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
bipartite_test.cc
Go to the documentation of this file.
1
2/*
3 Aleph_w
4
5 Data structures & Algorithms
6 version 2.0.0b
7 https://github.com/lrleon/Aleph-w
8
9 This file is part of Aleph-w library
10
11 Copyright (c) 2002-2026 Leandro Rabindranath Leon
12
13 Permission is hereby granted, free of charge, to any person obtaining a copy
14 of this software and associated documentation files (the "Software"), to deal
15 in the Software without restriction, including without limitation the rights
16 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
17 copies of the Software, and to permit persons to whom the Software is
18 furnished to do so, subject to the following conditions:
19
20 The above copyright notice and this permission notice shall be included in all
21 copies or substantial portions of the Software.
22
23 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
24 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
25 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
26 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
27 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
28 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
29 SOFTWARE.
30*/
31
32
49#include <gtest/gtest.h>
50#include <tpl_bipartite.H>
51#include <tpl_graph.H>
52#include <tpl_maxflow.H>
53
54#include <random>
55#include <vector>
56
57using namespace std;
58using namespace Aleph;
59
60// Graph type for testing - using Empty_Class for arc info as required by bipartite algorithms
62
63// ============================================================================
64// Helper Functions
65// ============================================================================
66
73{
74 Graph g;
75
76 // Create left partition nodes
77 vector<Graph::Node *> left(m);
78 for (size_t i = 0; i < m; ++i)
79 left[i] = g.insert_node(static_cast<int>(i));
80
81 // Create right partition nodes
82 vector<Graph::Node *> right(n);
83 for (size_t j = 0; j < n; ++j)
84 right[j] = g.insert_node(static_cast<int>(m + j));
85
86 // Connect every node in left to every node in right
87 for (size_t i = 0; i < m; ++i)
88 for (size_t j = 0; j < n; ++j)
89 g.insert_arc(left[i], right[j]);
90
91 return g;
92}
93
99{
100 Graph g;
101
102 if (n == 0)
103 return g;
104
105 vector<Graph::Node *> nodes(n);
106 for (size_t i = 0; i < n; ++i)
107 nodes[i] = g.insert_node(static_cast<int>(i));
108
109 for (size_t i = 0; i + 1 < n; ++i)
110 g.insert_arc(nodes[i], nodes[i + 1]);
111
112 return g;
113}
114
120{
121 Graph g;
122
123 if (n < 3)
124 return g;
125
126 vector<Graph::Node *> nodes(n);
127 for (size_t i = 0; i < n; ++i)
128 nodes[i] = g.insert_node(static_cast<int>(i));
129
130 for (size_t i = 0; i < n; ++i)
131 g.insert_arc(nodes[i], nodes[(i + 1) % n]);
132
133 return g;
134}
135
141{
142 Graph g;
143
144 auto * center = g.insert_node(0);
145
146 for (size_t i = 0; i < n; ++i)
147 {
148 auto * leaf = g.insert_node(static_cast<int>(i + 1));
149 g.insert_arc(center, leaf);
150 }
151
152 return g;
153}
154
159{
160 Graph g;
161
162 auto * a = g.insert_node(0);
163 auto * b = g.insert_node(1);
164 auto * c = g.insert_node(2);
165
166 g.insert_arc(a, b);
167 g.insert_arc(b, c);
168 g.insert_arc(c, a);
169
170 return g;
171}
172
177{
178 Graph g;
179
180 // Component 1: path 0--1
181 auto * a = g.insert_node(0);
182 auto * b = g.insert_node(1);
183 g.insert_arc(a, b);
184
185 // Component 2: path 2--3
186 auto * c = g.insert_node(2);
187 auto * d = g.insert_node(3);
188 g.insert_arc(c, d);
189
190 return g;
191}
192
204{
205 // Create sets for fast lookup
206 DynSetAvlTree<Graph::Node *> left_set, right_set;
207
208 for (auto it = l.get_it(); it.has_curr(); it.next_ne())
209 left_set.insert(it.get_curr());
210
211 for (auto it = r.get_it(); it.has_curr(); it.next_ne())
212 right_set.insert(it.get_curr());
213
214 // Check that partitions don't overlap
215 for (auto it = l.get_it(); it.has_curr(); it.next_ne())
216 if (right_set.contains(it.get_curr()))
217 return false;
218
219 // Check that every edge between partitioned nodes connects different partitions
220 for (Arc_Iterator<Graph> it(g); it.has_curr(); it.next_ne())
221 {
222 auto * arc = it.get_curr();
223 auto * src = g.get_src_node(arc);
224 auto * tgt = g.get_tgt_node(arc);
225
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);
230
231 // Skip edges involving nodes that weren't partitioned
232 if (!(src_in_left || src_in_right) || !(tgt_in_left || tgt_in_right))
233 continue;
234
235 // Both in same partition = invalid
236 if (src_in_left == tgt_in_left)
237 return false;
238 }
239
240 return true;
241}
242
247bool verify_matching(const Graph & g,
248 const DynDlist<Graph::Arc *> & matching)
249{
250 DynSetAvlTree<Graph::Node *> matched_nodes;
251
252 for (auto it = matching.get_it(); it.has_curr(); it.next_ne())
253 {
254 auto * arc = it.get_curr();
255 auto * src = g.get_src_node(arc);
256 auto * tgt = g.get_tgt_node(arc);
257
258 // Check if either node is already matched
259 if (matched_nodes.contains(src) || matched_nodes.contains(tgt))
260 return false;
261
262 matched_nodes.insert(src);
263 matched_nodes.insert(tgt);
264 }
265
266 return true;
267}
268
269// ============================================================================
270// Basic Bipartite Detection Tests
271// ============================================================================
272
273TEST(Bipartite, EmptyGraphReturnsEmpty)
274{
275 Graph g;
277
278 EXPECT_NO_THROW(compute_bipartite<Graph>(g, l, r));
279 EXPECT_TRUE(l.is_empty());
280 EXPECT_TRUE(r.is_empty());
281}
282
283TEST(Bipartite, SingleNode)
284{
285 Graph g;
286 g.insert_node(1);
287
289
290 compute_bipartite<Graph>(g, l, r);
291
292 // Single node goes into one partition
293 EXPECT_EQ(l.size() + r.size(), 1u);
294}
295
296TEST(Bipartite, TwoConnectedNodes)
297{
298 Graph g;
299 auto * a = g.insert_node(1);
300 auto * b = g.insert_node(2);
301 g.insert_arc(a, b);
302
304
305 compute_bipartite<Graph>(g, l, r);
306
307 EXPECT_EQ(l.size(), 1u);
308 EXPECT_EQ(r.size(), 1u);
309 EXPECT_TRUE(verify_bipartition(g, l, r));
310}
311
312TEST(Bipartite, PathGraphEven)
313{
314 auto g = create_path_graph(4); // 0--1--2--3
315
317
318 compute_bipartite<Graph>(g, l, r);
319
320 EXPECT_EQ(l.size() + r.size(), 4u);
321 EXPECT_EQ(l.size(), 2u);
322 EXPECT_EQ(r.size(), 2u);
323 EXPECT_TRUE(verify_bipartition(g, l, r));
324}
325
326TEST(Bipartite, PathGraphOdd)
327{
328 auto g = create_path_graph(5); // 0--1--2--3--4
329
331
332 compute_bipartite<Graph>(g, l, r);
333
334 EXPECT_EQ(l.size() + r.size(), 5u);
335 EXPECT_TRUE(verify_bipartition(g, l, r));
336}
337
338TEST(Bipartite, StarGraph)
339{
340 auto g = create_star_graph(5); // Center with 5 leaves
341
343
344 compute_bipartite<Graph>(g, l, r);
345
346 EXPECT_EQ(l.size() + r.size(), 6u);
347 // One partition has the center, other has all leaves
348 EXPECT_TRUE((l.size() == 1 && r.size() == 5) ||
349 (l.size() == 5 && r.size() == 1));
350 EXPECT_TRUE(verify_bipartition(g, l, r));
351}
352
353TEST(Bipartite, CompleteBipartiteK22)
354{
355 auto g = create_complete_bipartite(2, 2);
356
358
359 compute_bipartite<Graph>(g, l, r);
360
361 EXPECT_EQ(l.size(), 2u);
362 EXPECT_EQ(r.size(), 2u);
363 EXPECT_TRUE(verify_bipartition(g, l, r));
364}
365
366TEST(Bipartite, CompleteBipartiteK33)
367{
368 auto g = create_complete_bipartite(3, 3);
369
371
372 compute_bipartite<Graph>(g, l, r);
373
374 EXPECT_EQ(l.size(), 3u);
375 EXPECT_EQ(r.size(), 3u);
376 EXPECT_TRUE(verify_bipartition(g, l, r));
377}
378
379TEST(Bipartite, CompleteBipartiteK25)
380{
381 auto g = create_complete_bipartite(2, 5);
382
384
385 compute_bipartite<Graph>(g, l, r);
386
387 EXPECT_EQ(l.size() + r.size(), 7u);
388 EXPECT_TRUE(verify_bipartition(g, l, r));
389}
390
391TEST(Bipartite, EvenCycle)
392{
393 auto g = create_cycle_graph(6); // 6-cycle is bipartite
394
396
397 compute_bipartite<Graph>(g, l, r);
398
399 EXPECT_EQ(l.size(), 3u);
400 EXPECT_EQ(r.size(), 3u);
401 EXPECT_TRUE(verify_bipartition(g, l, r));
402}
403
404// ============================================================================
405// Non-Bipartite Graph Detection Tests
406// ============================================================================
407
408TEST(Bipartite, TriangleThrows)
409{
410 auto g = create_triangle();
411
413
414 EXPECT_THROW(compute_bipartite<Graph>(g, l, r), domain_error);
415}
416
417TEST(Bipartite, OddCycleThrows)
418{
419 auto g = create_cycle_graph(5); // 5-cycle is NOT bipartite
420
422
423 EXPECT_THROW(compute_bipartite<Graph>(g, l, r), domain_error);
424}
425
426TEST(Bipartite, LargeOddCycleThrows)
427{
428 auto g = create_cycle_graph(11); // 11-cycle is NOT bipartite
429
431
432 EXPECT_THROW(compute_bipartite<Graph>(g, l, r), domain_error);
433}
434
435TEST(Bipartite, CompleteGraphK3Throws)
436{
437 // K_3 is a triangle
438 auto g = create_triangle();
439
441
442 EXPECT_THROW(compute_bipartite<Graph>(g, l, r), domain_error);
443}
444
445TEST(Bipartite, GraphWithOddCycleAttached)
446{
447 Graph g;
448
449 // Create a bipartite part
450 auto * a = g.insert_node(0);
451 auto * b = g.insert_node(1);
452 g.insert_arc(a, b);
453
454 // Attach an odd cycle (3 nodes = triangle) to node b
455 auto * c = g.insert_node(2);
456 auto * d = g.insert_node(3);
457 g.insert_arc(b, c);
458 g.insert_arc(c, d);
459 g.insert_arc(d, b); // Creates odd cycle b-c-d-b (3 nodes)
460
462
463 EXPECT_THROW(compute_bipartite<Graph>(g, l, r), domain_error);
464}
465
466// ============================================================================
467// Disconnected Graph Tests
468// ============================================================================
469
470// compute_bipartite() colored only the component of the first node, so the
471// nodes of the others were in neither set
472
473TEST(Bipartite, TwoDisconnectedNodes)
474{
475 Graph g;
476 g.insert_node(1);
477 g.insert_node(2);
478 // No edges - two isolated nodes
479
481
482 compute_bipartite<Graph>(g, l, r);
483
484 // Each one is the first node of its component
485 EXPECT_EQ(l.size(), 2u);
486 EXPECT_EQ(r.size(), 0u);
487}
488
489TEST(Bipartite, DisconnectedBipartiteComponents)
490{
492
494
495 compute_bipartite<Graph>(g, l, r);
496
497 EXPECT_EQ(l.size(), 2u);
498 EXPECT_EQ(r.size(), 2u);
499 EXPECT_TRUE(verify_bipartition(g, l, r));
500}
501
502// ============================================================================
503// Class Wrapper Tests
504// ============================================================================
505
506TEST(ComputeBipartiteClass, BasicUsage)
507{
508 auto g = create_complete_bipartite(3, 4);
509
511
513
514 EXPECT_EQ(l.size() + r.size(), 7u);
515 EXPECT_TRUE(verify_bipartition(g, l, r));
516}
517
518TEST(ComputeBipartiteClass, ThrowsOnNonBipartite)
519{
520 auto g = create_triangle();
521
523
524 EXPECT_THROW(Compute_Bipartite<Graph>()(g, l, r), domain_error);
525}
526
527// ============================================================================
528// Maximum Matching Tests
529// ============================================================================
530TEST(MaximumMatching, EmptyGraphReturnsEmpty)
531{
532 Graph g;
533
534 DynDlist<Graph::Arc *> matching;
535
536 // Empty graph: should not throw and matching must remain empty
537 EXPECT_NO_THROW(
538 compute_maximum_cardinality_bipartite_matching<Graph>(g, matching));
539 EXPECT_TRUE(matching.is_empty());
540}
541
542TEST(MaximumMatching, SingleEdge)
543{
544 Graph g;
545 auto * a = g.insert_node(1);
546 auto * b = g.insert_node(2);
547 g.insert_arc(a, b);
548
549 DynDlist<Graph::Arc *> matching;
550
551 compute_maximum_cardinality_bipartite_matching<Graph>(g, matching);
552
553 EXPECT_EQ(matching.size(), 1u);
554 EXPECT_TRUE(verify_matching(g, matching));
555}
556
557TEST(MaximumMatching, PathGraph4)
558{
559 auto g = create_path_graph(4); // 0--1--2--3
560
561 DynDlist<Graph::Arc *> matching;
562
563 compute_maximum_cardinality_bipartite_matching<Graph>(g, matching);
564
565 // Maximum matching in path of 4 nodes is 2 edges
566 EXPECT_EQ(matching.size(), 2u);
567 EXPECT_TRUE(verify_matching(g, matching));
568}
569
570TEST(MaximumMatching, PathGraph5)
571{
572 auto g = create_path_graph(5); // 0--1--2--3--4
573
574 DynDlist<Graph::Arc *> matching;
575
576 compute_maximum_cardinality_bipartite_matching<Graph>(g, matching);
577
578 // Maximum matching in path of 5 nodes is 2 edges
579 EXPECT_EQ(matching.size(), 2u);
580 EXPECT_TRUE(verify_matching(g, matching));
581}
582
583TEST(MaximumMatching, CompleteBipartiteK22)
584{
585 auto g = create_complete_bipartite(2, 2);
586
587 DynDlist<Graph::Arc *> matching;
588
589 compute_maximum_cardinality_bipartite_matching<Graph>(g, matching);
590
591 // Perfect matching: 2 edges
592 EXPECT_EQ(matching.size(), 2u);
593 EXPECT_TRUE(verify_matching(g, matching));
594}
595
596TEST(MaximumMatching, CompleteBipartiteK33)
597{
598 auto g = create_complete_bipartite(3, 3);
599
600 DynDlist<Graph::Arc *> matching;
601
602 compute_maximum_cardinality_bipartite_matching<Graph>(g, matching);
603
604 // Perfect matching: 3 edges
605 EXPECT_EQ(matching.size(), 3u);
606 EXPECT_TRUE(verify_matching(g, matching));
607}
608
609TEST(MaximumMatching, CompleteBipartiteK55)
610{
611 auto g = create_complete_bipartite(5, 5);
612
613 DynDlist<Graph::Arc *> matching;
614
615 compute_maximum_cardinality_bipartite_matching<Graph>(g, matching);
616
617 // Perfect matching: 5 edges
618 EXPECT_EQ(matching.size(), 5u);
619 EXPECT_TRUE(verify_matching(g, matching));
620}
621
622TEST(MaximumMatching, UnbalancedK25)
623{
624 auto g = create_complete_bipartite(2, 5);
625
626 DynDlist<Graph::Arc *> matching;
627
628 compute_maximum_cardinality_bipartite_matching<Graph>(g, matching);
629
630 // Maximum matching limited by smaller partition: 2 edges
631 EXPECT_EQ(matching.size(), 2u);
632 EXPECT_TRUE(verify_matching(g, matching));
633}
634
635TEST(MaximumMatching, UnbalancedK52)
636{
637 auto g = create_complete_bipartite(5, 2);
638
639 DynDlist<Graph::Arc *> matching;
640
641 compute_maximum_cardinality_bipartite_matching<Graph>(g, matching);
642
643 // Maximum matching limited by smaller partition: 2 edges
644 EXPECT_EQ(matching.size(), 2u);
645 EXPECT_TRUE(verify_matching(g, matching));
646}
647
648TEST(MaximumMatching, StarGraph)
649{
650 auto g = create_star_graph(5);
651
652 DynDlist<Graph::Arc *> matching;
653
654 compute_maximum_cardinality_bipartite_matching<Graph>(g, matching);
655
656 // Star can only have 1 edge in matching (center is shared)
657 EXPECT_EQ(matching.size(), 1u);
658 EXPECT_TRUE(verify_matching(g, matching));
659}
660
661TEST(MaximumMatching, EvenCycle)
662{
663 auto g = create_cycle_graph(6);
664
665 DynDlist<Graph::Arc *> matching;
666
667 compute_maximum_cardinality_bipartite_matching<Graph>(g, matching);
668
669 // 6-cycle has perfect matching: 3 edges
670 EXPECT_EQ(matching.size(), 3u);
671 EXPECT_TRUE(verify_matching(g, matching));
672}
673
674TEST(MaximumMatching, ThrowsOnNonBipartite)
675{
676 auto g = create_triangle();
677
678 DynDlist<Graph::Arc *> matching;
679
680 EXPECT_THROW(
681 compute_maximum_cardinality_bipartite_matching<Graph>(g, matching),
682 domain_error);
683}
684
685// ============================================================================
686// Matching Class Wrapper Tests
687// ============================================================================
688
689TEST(MaximumMatchingClass, BasicUsage)
690{
691 auto g = create_complete_bipartite(4, 4);
692
693 DynDlist<Graph::Arc *> matching;
694
696
697 EXPECT_EQ(matching.size(), 4u);
698 EXPECT_TRUE(verify_matching(g, matching));
699}
700
701TEST(MaximumMatchingClass, ThrowsOnNonBipartite)
702{
703 auto g = create_cycle_graph(5); // Odd cycle
704
705 DynDlist<Graph::Arc *> matching;
706
707 EXPECT_THROW(
709 domain_error);
710}
711
712// ============================================================================
713// Stress Tests
714// ============================================================================
715
716TEST(BipartiteStress, LargeBipartiteGraph)
717{
718 auto g = create_complete_bipartite(50, 50);
719
721
722 compute_bipartite<Graph>(g, l, r);
723
724 EXPECT_EQ(l.size() + r.size(), 100u);
725 EXPECT_TRUE(verify_bipartition(g, l, r));
726}
727
728TEST(BipartiteStress, LargePathGraph)
729{
730 auto g = create_path_graph(100);
731
733
734 compute_bipartite<Graph>(g, l, r);
735
736 EXPECT_EQ(l.size(), 50u);
737 EXPECT_EQ(r.size(), 50u);
738 EXPECT_TRUE(verify_bipartition(g, l, r));
739}
740
741TEST(MaximumMatchingStress, LargeMatching)
742{
743 auto g = create_complete_bipartite(20, 20);
744
745 DynDlist<Graph::Arc *> matching;
746
747 compute_maximum_cardinality_bipartite_matching<Graph>(g, matching);
748
749 EXPECT_EQ(matching.size(), 20u);
750 EXPECT_TRUE(verify_matching(g, matching));
751}
752
753TEST(BipartiteStress, LargeEvenCycle)
754{
755 auto g = create_cycle_graph(100);
756
758
759 compute_bipartite<Graph>(g, l, r);
760
761 EXPECT_EQ(l.size(), 50u);
762 EXPECT_EQ(r.size(), 50u);
763 EXPECT_TRUE(verify_bipartition(g, l, r));
764}
765
766// ============================================================================
767// Edge Cases
768// ============================================================================
769
770TEST(Bipartite, MultipleEdgesBetweenSameNodes)
771{
772 Graph g;
773 auto * a = g.insert_node(1);
774 auto * b = g.insert_node(2);
775
776 // Multiple edges between same nodes (multigraph)
777 g.insert_arc(a, b);
778 g.insert_arc(a, b);
779 g.insert_arc(a, b);
780
782
783 compute_bipartite<Graph>(g, l, r);
784
785 EXPECT_EQ(l.size(), 1u);
786 EXPECT_EQ(r.size(), 1u);
787}
788
789TEST(Bipartite, IsolatedNodeWithBipartiteComponent)
790{
791 Graph g;
792
793 // Bipartite component
794 auto * a = g.insert_node(0);
795 auto * b = g.insert_node(1);
796 g.insert_arc(a, b);
797
798 // Isolated node
799 g.insert_node(2);
800
802
803 compute_bipartite<Graph>(g, l, r);
804
805 EXPECT_EQ(l.size(), 2u); // the isolated node too
806 EXPECT_EQ(r.size(), 1u);
807 EXPECT_TRUE(verify_bipartition(g, l, r));
808}
809
810// The matching built its network with the nodes of the component of the
811// first node only; the others, with no arcs in the network, were sources
812// and sinks of it, and the maximum flow threw domain_error
813TEST(MaximumMatching, DisconnectedComponents)
814{
815 Graph g;
816 auto * a1 = g.insert_node(1);
817 auto * b1 = g.insert_node(2);
818 g.insert_arc(a1, b1);
819 auto * a2 = g.insert_node(3);
820 auto * b2 = g.insert_node(4);
821 g.insert_arc(a2, b2);
822 auto * a3 = g.insert_node(5);
823 auto * b3 = g.insert_node(6);
824 auto * b4 = g.insert_node(7);
825 g.insert_arc(a3, b3);
826 g.insert_arc(a3, b4);
827
828 DynDlist<Graph::Arc *> matching;
829 compute_maximum_cardinality_bipartite_matching<Graph>(g, matching);
830 EXPECT_EQ(matching.size(), 3u);
831 EXPECT_TRUE(verify_matching(g, matching));
832}
833
834// A node without arcs would also be a source and a sink of the network
835TEST(MaximumMatching, NodesWithoutArcs)
836{
837 Graph g;
838 g.insert_node(0);
839 auto * a = g.insert_node(1);
840 auto * b = g.insert_node(2);
841 g.insert_node(3);
842 auto * c = g.insert_node(4);
843 g.insert_arc(a, b);
844 g.insert_arc(b, c);
845
846 DynDlist<Graph::Arc *> matching;
847 compute_maximum_cardinality_bipartite_matching<Graph>(g, matching);
848 EXPECT_EQ(matching.size(), 1u);
849 EXPECT_TRUE(verify_matching(g, matching));
850
851 Graph lonely;
852 lonely.insert_node(0);
853 lonely.insert_node(1);
855 compute_maximum_cardinality_bipartite_matching<Graph>(lonely, none);
856 EXPECT_TRUE(none.is_empty());
857}
858
859// ============================================================================
860// Color Enum Tests
861// ============================================================================
862
863TEST(BipartiteColor, EnumValues)
864{
865 // Verify the color enum values
866 EXPECT_EQ(Bp_White, 0);
867 EXPECT_EQ(Bp_Red, 1);
868 EXPECT_EQ(Bp_Blue, 2);
869}
870
871// ============================================================================
872// Hopcroft-Karp Matching Tests
873// ============================================================================
874
875// --- Base cases ---
876
877TEST(HopcroftKarp, EmptyGraph)
878{
879 Graph g;
880 DynDlist<Graph::Arc *> matching;
881
882 EXPECT_NO_THROW(hopcroft_karp_matching<Graph>(g, matching));
883 EXPECT_TRUE(matching.is_empty());
884}
885
886TEST(HopcroftKarp, SingleIsolatedNode)
887{
888 Graph g;
889 g.insert_node(0);
890
891 DynDlist<Graph::Arc *> matching;
892 hopcroft_karp_matching<Graph>(g, matching);
893
894 EXPECT_TRUE(matching.is_empty());
895}
896
897TEST(HopcroftKarp, SingleEdge)
898{
899 Graph g;
900 auto * a = g.insert_node(0);
901 auto * b = g.insert_node(1);
902 g.insert_arc(a, b);
903
904 DynDlist<Graph::Arc *> matching;
905 hopcroft_karp_matching<Graph>(g, matching);
906
907 EXPECT_EQ(matching.size(), 1u);
908 EXPECT_TRUE(verify_matching(g, matching));
909}
910
911// --- Standard bipartite graphs ---
912
913TEST(HopcroftKarp, PathGraph4)
914{
915 auto g = create_path_graph(4);
916
917 DynDlist<Graph::Arc *> matching;
918 hopcroft_karp_matching<Graph>(g, matching);
919
920 EXPECT_EQ(matching.size(), 2u);
921 EXPECT_TRUE(verify_matching(g, matching));
922}
923
924TEST(HopcroftKarp, PathGraph5)
925{
926 auto g = create_path_graph(5);
927
928 DynDlist<Graph::Arc *> matching;
929 hopcroft_karp_matching<Graph>(g, matching);
930
931 EXPECT_EQ(matching.size(), 2u);
932 EXPECT_TRUE(verify_matching(g, matching));
933}
934
935TEST(HopcroftKarp, CompleteBipartiteK22)
936{
937 auto g = create_complete_bipartite(2, 2);
938
939 DynDlist<Graph::Arc *> matching;
940 hopcroft_karp_matching<Graph>(g, matching);
941
942 EXPECT_EQ(matching.size(), 2u);
943 EXPECT_TRUE(verify_matching(g, matching));
944}
945
946TEST(HopcroftKarp, CompleteBipartiteK33)
947{
948 auto g = create_complete_bipartite(3, 3);
949
950 DynDlist<Graph::Arc *> matching;
951 hopcroft_karp_matching<Graph>(g, matching);
952
953 EXPECT_EQ(matching.size(), 3u);
954 EXPECT_TRUE(verify_matching(g, matching));
955}
956
957TEST(HopcroftKarp, CompleteBipartiteK55)
958{
959 auto g = create_complete_bipartite(5, 5);
960
961 DynDlist<Graph::Arc *> matching;
962 hopcroft_karp_matching<Graph>(g, matching);
963
964 EXPECT_EQ(matching.size(), 5u);
965 EXPECT_TRUE(verify_matching(g, matching));
966}
967
968TEST(HopcroftKarp, UnbalancedK25)
969{
970 auto g = create_complete_bipartite(2, 5);
971
972 DynDlist<Graph::Arc *> matching;
973 hopcroft_karp_matching<Graph>(g, matching);
974
975 EXPECT_EQ(matching.size(), 2u);
976 EXPECT_TRUE(verify_matching(g, matching));
977}
978
979TEST(HopcroftKarp, StarGraph)
980{
981 auto g = create_star_graph(5);
982
983 DynDlist<Graph::Arc *> matching;
984 hopcroft_karp_matching<Graph>(g, matching);
985
986 EXPECT_EQ(matching.size(), 1u);
987 EXPECT_TRUE(verify_matching(g, matching));
988}
989
990TEST(HopcroftKarp, EvenCycle6)
991{
992 auto g = create_cycle_graph(6);
993
994 DynDlist<Graph::Arc *> matching;
995 hopcroft_karp_matching<Graph>(g, matching);
996
997 EXPECT_EQ(matching.size(), 3u);
998 EXPECT_TRUE(verify_matching(g, matching));
999}
1000
1001// --- Non-bipartite detection ---
1002
1003TEST(HopcroftKarp, TriangleThrows)
1004{
1005 auto g = create_triangle();
1006
1007 DynDlist<Graph::Arc *> matching;
1008
1009 EXPECT_THROW(hopcroft_karp_matching<Graph>(g, matching), domain_error);
1010}
1011
1012TEST(HopcroftKarp, OddCycleThrows)
1013{
1014 auto g = create_cycle_graph(5);
1015
1016 DynDlist<Graph::Arc *> matching;
1017
1018 EXPECT_THROW(hopcroft_karp_matching<Graph>(g, matching), domain_error);
1019}
1020
1021// --- Disconnected graphs ---
1022
1023TEST(HopcroftKarp, TwoDisconnectedEdges)
1024{
1026
1027 DynDlist<Graph::Arc *> matching;
1028 hopcroft_karp_matching<Graph>(g, matching);
1029
1030 EXPECT_EQ(matching.size(), 2u);
1031 EXPECT_TRUE(verify_matching(g, matching));
1032}
1033
1034TEST(HopcroftKarp, DisconnectedWithIsolatedNodes)
1035{
1036 Graph g;
1037
1038 // Component 1: single edge
1039 auto * a = g.insert_node(0);
1040 auto * b = g.insert_node(1);
1041 g.insert_arc(a, b);
1042
1043 // Isolated nodes
1044 g.insert_node(2);
1045 g.insert_node(3);
1046
1047 // Component 2: K_{2,2}
1048 auto * c = g.insert_node(10);
1049 auto * d = g.insert_node(11);
1050 auto * e = g.insert_node(12);
1051 auto * f = g.insert_node(13);
1052 g.insert_arc(c, e);
1053 g.insert_arc(c, f);
1054 g.insert_arc(d, e);
1055 g.insert_arc(d, f);
1056
1057 DynDlist<Graph::Arc *> matching;
1058 hopcroft_karp_matching<Graph>(g, matching);
1059
1060 // 1 from edge + 2 from K_{2,2} = 3
1061 EXPECT_EQ(matching.size(), 3u);
1062 EXPECT_TRUE(verify_matching(g, matching));
1063}
1064
1065TEST(HopcroftKarp, TwoDisconnectedCompleteBipartite)
1066{
1067 Graph g;
1068
1069 // Component 1: K_{3,3}
1070 vector<Graph::Node *> l1(3), r1(3);
1071 for (int i = 0; i < 3; ++i)
1072 l1[i] = g.insert_node(i);
1073 for (int i = 0; i < 3; ++i)
1074 r1[i] = g.insert_node(10 + i);
1075 for (int i = 0; i < 3; ++i)
1076 for (int j = 0; j < 3; ++j)
1077 g.insert_arc(l1[i], r1[j]);
1078
1079 // Component 2: K_{2,2}
1080 vector<Graph::Node *> l2(2), r2(2);
1081 for (int i = 0; i < 2; ++i)
1082 l2[i] = g.insert_node(20 + i);
1083 for (int i = 0; i < 2; ++i)
1084 r2[i] = g.insert_node(30 + i);
1085 for (int i = 0; i < 2; ++i)
1086 for (int j = 0; j < 2; ++j)
1087 g.insert_arc(l2[i], r2[j]);
1088
1089 DynDlist<Graph::Arc *> matching;
1090 hopcroft_karp_matching<Graph>(g, matching);
1091
1092 // 3 + 2 = 5
1093 EXPECT_EQ(matching.size(), 5u);
1094 EXPECT_TRUE(verify_matching(g, matching));
1095}
1096
1097// --- Cross-validation with max-flow ---
1098
1099TEST(HopcroftKarp, CrossValidateK44)
1100{
1101 auto g = create_complete_bipartite(4, 4);
1102
1103 DynDlist<Graph::Arc *> hk_matching;
1104 hopcroft_karp_matching<Graph>(g, hk_matching);
1105
1106 DynDlist<Graph::Arc *> ff_matching;
1107 compute_maximum_cardinality_bipartite_matching<Graph>(g, ff_matching);
1108
1109 EXPECT_EQ(hk_matching.size(), ff_matching.size());
1110 EXPECT_EQ(hk_matching.size(), 4u);
1111}
1112
1113TEST(HopcroftKarp, CrossValidatePath6)
1114{
1115 auto g = create_path_graph(6);
1116
1117 DynDlist<Graph::Arc *> hk_matching;
1118 hopcroft_karp_matching<Graph>(g, hk_matching);
1119
1120 DynDlist<Graph::Arc *> ff_matching;
1121 compute_maximum_cardinality_bipartite_matching<Graph>(g, ff_matching);
1122
1123 EXPECT_EQ(hk_matching.size(), ff_matching.size());
1124 EXPECT_EQ(hk_matching.size(), 3u);
1125}
1126
1127TEST(HopcroftKarp, CrossValidateEvenCycle8)
1128{
1129 auto g = create_cycle_graph(8);
1130
1131 DynDlist<Graph::Arc *> hk_matching;
1132 hopcroft_karp_matching<Graph>(g, hk_matching);
1133
1134 DynDlist<Graph::Arc *> ff_matching;
1135 compute_maximum_cardinality_bipartite_matching<Graph>(g, ff_matching);
1136
1137 EXPECT_EQ(hk_matching.size(), ff_matching.size());
1138 EXPECT_EQ(hk_matching.size(), 4u);
1139}
1140
1141// --- Functor wrapper ---
1142
1143TEST(HopcroftKarp, FunctorWrapper)
1144{
1145 auto g = create_complete_bipartite(3, 3);
1146
1147 DynDlist<Graph::Arc *> matching;
1148 Hopcroft_Karp_Matching<Graph>()(g, matching);
1149
1150 EXPECT_EQ(matching.size(), 3u);
1151 EXPECT_TRUE(verify_matching(g, matching));
1152}
1153
1154TEST(HopcroftKarp, FunctorMatchesFreeFunction)
1155{
1156 auto g = create_complete_bipartite(4, 4);
1157
1158 DynDlist<Graph::Arc *> fn_matching, op_matching;
1159 hopcroft_karp_matching<Graph>(g, fn_matching);
1160 Hopcroft_Karp_Matching<Graph>()(g, op_matching);
1161
1162 EXPECT_EQ(fn_matching.size(), op_matching.size());
1163}
1164
1165// --- Stress tests ---
1166
1167TEST(HopcroftKarp, StressK50_50)
1168{
1169 auto g = create_complete_bipartite(50, 50);
1170
1171 DynDlist<Graph::Arc *> matching;
1172 hopcroft_karp_matching<Graph>(g, matching);
1173
1174 EXPECT_EQ(matching.size(), 50u);
1175 EXPECT_TRUE(verify_matching(g, matching));
1176}
1177
1178TEST(HopcroftKarp, StressK100_100)
1179{
1180 auto g = create_complete_bipartite(100, 100);
1181
1182 DynDlist<Graph::Arc *> matching;
1183 hopcroft_karp_matching<Graph>(g, matching);
1184
1185 EXPECT_EQ(matching.size(), 100u);
1186 EXPECT_TRUE(verify_matching(g, matching));
1187}
1188
1189TEST(HopcroftKarp, StressPath200)
1190{
1191 auto g = create_path_graph(200);
1192
1193 DynDlist<Graph::Arc *> matching;
1194 hopcroft_karp_matching<Graph>(g, matching);
1195
1196 EXPECT_EQ(matching.size(), 100u);
1197 EXPECT_TRUE(verify_matching(g, matching));
1198}
1199
1200// The network of the matching has an integer flow, exact: on a sparse
1201// random graph of 1000 + 1000 nodes the matching of the maximum flows is
1202// one, and as large as that of Hopcroft-Karp, with each maximum flow
1203TEST(MaxFlowMatching, ASparseRandomGraphMatchesAsHopcroftKarp)
1204{
1205 Graph g;
1206 std::vector<Graph::Node *> l, r;
1207 for (int i = 0; i < 1000; ++i)
1208 l.push_back(g.insert_node(i));
1209 for (int i = 0; i < 1000; ++i)
1210 r.push_back(g.insert_node(1000 + i));
1211 std::mt19937 gen(1);
1212 for (int i = 0; i < 1000; ++i)
1213 {
1214 for (int k = 0; k < 2; ++k)
1215 g.insert_arc(l[size_t(i)], r[gen() % 1000u]);
1216 g.insert_arc(l[gen() % 1000u], r[size_t(i)]);
1217 }
1218
1220 hopcroft_karp_matching<Graph>(g, hk);
1221 ASSERT_TRUE(verify_matching(g, hk));
1222
1223 DynDlist<Graph::Arc *> ff, ek, dinic;
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})
1228 {
1229 EXPECT_TRUE(verify_matching(g, *m));
1230 EXPECT_EQ(m->size(), hk.size());
1231 }
1232}
1233
1234int main(int argc, char **argv)
1235{
1236 ::testing::InitGoogleTest(&argc, argv);
1237 return RUN_ALL_TESTS();
1238}// satisfy CI policy
1239// satisfy CI policy for tpl_bipartite.H and Subset_Sum.H
int main()
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.
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.
Definition htlist.H:1264
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
Definition htlist.H:434
Functor wrapper for hopcroft_karp_matching.
virtual Node * insert_node(Node *node) noexcept
Insertion of a node already allocated.
Definition tpl_graph.H:535
Arc * insert_arc(Node *src_node, Node *tgt_node, void *a)
Definition tpl_graph.H:618
Node * get_src_node(Arc *arc) const noexcept
Return the source node of arc (only for directed graphs)
Definition graph-dry.H:1017
Node * get_tgt_node(Arc *arc) const noexcept
Return the target node of arc (only for directed graphs)
Definition graph-dry.H:1023
auto get_it()
Return a properly initialized iterator positioned at the first item on the container.
Definition ah-dry.H:663
constexpr size_t size() const noexcept
Returns the number of entries in the table.
Definition hashDry.H:954
#define TEST(name)
DynArray< Graph::Node * > nodes
Definition graphpic.C:406
Main namespace for Aleph-w library functions.
bool none(const Container &container, Operation &operation)
Return true if no element satisfies operation.
STL namespace.
Filtered iterator on all the arcs of a graph.
Definition tpl_graph.H:1200
Arc of graph implemented with double-linked adjacency lists.
Definition tpl_graph.H:231
FooMap m(5, fst_unit_pair_hash, snd_unit_pair_hash)
DynList< int > l1
DynList< int > l2
static int * k
Gsl_Compatible_Rng r
Bipartite graph detection and 2-coloring.
Generic graph and digraph implementations.
Advanced maximum flow algorithms.
DynList< int > l