Aleph-w 7.10.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
bipartite_test.cc File Reference

Tests for Bipartite. More...

#include <gtest/gtest.h>
#include <tpl_bipartite.H>
#include <tpl_graph.H>
#include <tpl_maxflow.H>
#include <random>
#include <vector>
Include dependency graph for bipartite_test.cc:

Go to the source code of this file.

Typedefs

using Graph = List_Graph< Graph_Node< int >, Graph_Arc< Empty_Class > >
 

Functions

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..m+n-1.
 
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_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.
 
Graph create_star_graph (size_t n)
 Creates a star graph with center and n leaves Star graphs are always bipartite.
 
Graph create_triangle ()
 Creates a triangle (K_3) - the simplest non-bipartite graph.
 
Graph create_disconnected_bipartite ()
 Creates two disconnected components.
 
bool verify_bipartition (const Graph &g, const DynDlist< Graph::Node * > &l, const DynDlist< Graph::Node * > &r)
 Verifies that a bipartition is valid:
 
bool verify_matching (const Graph &g, const DynDlist< Graph::Arc * > &matching)
 Verifies that a matching is valid:
 
 TEST (Bipartite, EmptyGraphReturnsEmpty)
 
 TEST (Bipartite, SingleNode)
 
 TEST (Bipartite, TwoConnectedNodes)
 
 TEST (Bipartite, PathGraphEven)
 
 TEST (Bipartite, PathGraphOdd)
 
 TEST (Bipartite, StarGraph)
 
 TEST (Bipartite, CompleteBipartiteK22)
 
 TEST (Bipartite, CompleteBipartiteK33)
 
 TEST (Bipartite, CompleteBipartiteK25)
 
 TEST (Bipartite, EvenCycle)
 
 TEST (Bipartite, TriangleThrows)
 
 TEST (Bipartite, OddCycleThrows)
 
 TEST (Bipartite, LargeOddCycleThrows)
 
 TEST (Bipartite, CompleteGraphK3Throws)
 
 TEST (Bipartite, GraphWithOddCycleAttached)
 
 TEST (Bipartite, TwoDisconnectedNodes)
 
 TEST (Bipartite, DisconnectedBipartiteComponents)
 
 TEST (ComputeBipartiteClass, BasicUsage)
 
 TEST (ComputeBipartiteClass, ThrowsOnNonBipartite)
 
 TEST (MaximumMatching, EmptyGraphReturnsEmpty)
 
 TEST (MaximumMatching, SingleEdge)
 
 TEST (MaximumMatching, PathGraph4)
 
 TEST (MaximumMatching, PathGraph5)
 
 TEST (MaximumMatching, CompleteBipartiteK22)
 
 TEST (MaximumMatching, CompleteBipartiteK33)
 
 TEST (MaximumMatching, CompleteBipartiteK55)
 
 TEST (MaximumMatching, UnbalancedK25)
 
 TEST (MaximumMatching, UnbalancedK52)
 
 TEST (MaximumMatching, StarGraph)
 
 TEST (MaximumMatching, EvenCycle)
 
 TEST (MaximumMatching, ThrowsOnNonBipartite)
 
 TEST (MaximumMatchingClass, BasicUsage)
 
 TEST (MaximumMatchingClass, ThrowsOnNonBipartite)
 
 TEST (BipartiteStress, LargeBipartiteGraph)
 
 TEST (BipartiteStress, LargePathGraph)
 
 TEST (MaximumMatchingStress, LargeMatching)
 
 TEST (BipartiteStress, LargeEvenCycle)
 
 TEST (Bipartite, MultipleEdgesBetweenSameNodes)
 
 TEST (Bipartite, IsolatedNodeWithBipartiteComponent)
 
 TEST (MaximumMatching, DisconnectedComponents)
 
 TEST (MaximumMatching, NodesWithoutArcs)
 
 TEST (BipartiteColor, EnumValues)
 
 TEST (HopcroftKarp, EmptyGraph)
 
 TEST (HopcroftKarp, SingleIsolatedNode)
 
 TEST (HopcroftKarp, SingleEdge)
 
 TEST (HopcroftKarp, PathGraph4)
 
 TEST (HopcroftKarp, PathGraph5)
 
 TEST (HopcroftKarp, CompleteBipartiteK22)
 
 TEST (HopcroftKarp, CompleteBipartiteK33)
 
 TEST (HopcroftKarp, CompleteBipartiteK55)
 
 TEST (HopcroftKarp, UnbalancedK25)
 
 TEST (HopcroftKarp, StarGraph)
 
 TEST (HopcroftKarp, EvenCycle6)
 
 TEST (HopcroftKarp, TriangleThrows)
 
 TEST (HopcroftKarp, OddCycleThrows)
 
 TEST (HopcroftKarp, TwoDisconnectedEdges)
 
 TEST (HopcroftKarp, DisconnectedWithIsolatedNodes)
 
 TEST (HopcroftKarp, TwoDisconnectedCompleteBipartite)
 
 TEST (HopcroftKarp, CrossValidateK44)
 
 TEST (HopcroftKarp, CrossValidatePath6)
 
 TEST (HopcroftKarp, CrossValidateEvenCycle8)
 
 TEST (HopcroftKarp, FunctorWrapper)
 
 TEST (HopcroftKarp, FunctorMatchesFreeFunction)
 
 TEST (HopcroftKarp, StressK50_50)
 
 TEST (HopcroftKarp, StressK100_100)
 
 TEST (HopcroftKarp, StressPath200)
 
 TEST (MaxFlowMatching, ASparseRandomGraphMatchesAsHopcroftKarp)
 
int main (int argc, char **argv)
 

Detailed Description

Tests for Bipartite.

Comprehensive tests for tpl_bipartite.H.

Tests cover:

  • Bipartite graph detection and partition computation
  • Maximum cardinality matching
  • Edge cases (empty graphs, single nodes, disconnected components)
  • Non-bipartite graph detection

Definition in file bipartite_test.cc.

Typedef Documentation

◆ Graph

Definition at line 61 of file bipartite_test.cc.

Function Documentation

◆ create_complete_bipartite()

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..m+n-1.

Definition at line 72 of file bipartite_test.cc.

References Aleph::List_Graph< _Graph_Node, _Graph_Arc >::insert_arc(), Aleph::List_Graph< _Graph_Node, _Graph_Arc >::insert_node(), and m.

Referenced by TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), and TEST().

◆ create_cycle_graph()

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.

Definition at line 119 of file bipartite_test.cc.

References Aleph::List_Graph< _Graph_Node, _Graph_Arc >::insert_arc(), Aleph::List_Graph< _Graph_Node, _Graph_Arc >::insert_node(), and nodes.

Referenced by TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), and TEST().

◆ create_disconnected_bipartite()

Graph create_disconnected_bipartite ( )

Creates two disconnected components.

Definition at line 176 of file bipartite_test.cc.

References Aleph::List_Graph< _Graph_Node, _Graph_Arc >::insert_arc(), and Aleph::List_Graph< _Graph_Node, _Graph_Arc >::insert_node().

Referenced by TEST(), and TEST().

◆ create_path_graph()

Graph create_path_graph ( size_t  n)

Creates a path graph with n nodes: 0–1–2–...–n-1 Path graphs are always bipartite.

Definition at line 98 of file bipartite_test.cc.

References Aleph::List_Graph< _Graph_Node, _Graph_Arc >::insert_arc(), Aleph::List_Graph< _Graph_Node, _Graph_Arc >::insert_node(), and nodes.

Referenced by TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), and TEST().

◆ create_star_graph()

Graph create_star_graph ( size_t  n)

Creates a star graph with center and n leaves Star graphs are always bipartite.

Definition at line 140 of file bipartite_test.cc.

References Aleph::List_Graph< _Graph_Node, _Graph_Arc >::insert_arc(), and Aleph::List_Graph< _Graph_Node, _Graph_Arc >::insert_node().

Referenced by TEST(), TEST(), and TEST().

◆ create_triangle()

Graph create_triangle ( )

◆ main()

int main ( int  argc,
char **  argv 
)

Definition at line 1234 of file bipartite_test.cc.

◆ TEST() [1/67]

TEST ( Bipartite  ,
CompleteBipartiteK22   
)

◆ TEST() [2/67]

TEST ( Bipartite  ,
CompleteBipartiteK25   
)

◆ TEST() [3/67]

TEST ( Bipartite  ,
CompleteBipartiteK33   
)

◆ TEST() [4/67]

TEST ( Bipartite  ,
CompleteGraphK3Throws   
)

Definition at line 435 of file bipartite_test.cc.

References create_triangle(), l, and r.

◆ TEST() [5/67]

TEST ( Bipartite  ,
DisconnectedBipartiteComponents   
)

◆ TEST() [6/67]

TEST ( Bipartite  ,
EmptyGraphReturnsEmpty   
)

Definition at line 273 of file bipartite_test.cc.

References Aleph::HTList::is_empty(), l, and r.

◆ TEST() [7/67]

TEST ( Bipartite  ,
EvenCycle   
)

◆ TEST() [8/67]

TEST ( Bipartite  ,
GraphWithOddCycleAttached   
)

◆ TEST() [9/67]

◆ TEST() [10/67]

TEST ( Bipartite  ,
LargeOddCycleThrows   
)

Definition at line 426 of file bipartite_test.cc.

References create_cycle_graph(), l, and r.

◆ TEST() [11/67]

TEST ( Bipartite  ,
MultipleEdgesBetweenSameNodes   
)

◆ TEST() [12/67]

TEST ( Bipartite  ,
OddCycleThrows   
)

Definition at line 417 of file bipartite_test.cc.

References create_cycle_graph(), l, and r.

◆ TEST() [13/67]

TEST ( Bipartite  ,
PathGraphEven   
)

◆ TEST() [14/67]

TEST ( Bipartite  ,
PathGraphOdd   
)

◆ TEST() [15/67]

TEST ( Bipartite  ,
SingleNode   
)

◆ TEST() [16/67]

TEST ( Bipartite  ,
StarGraph   
)

◆ TEST() [17/67]

TEST ( Bipartite  ,
TriangleThrows   
)

Definition at line 408 of file bipartite_test.cc.

References create_triangle(), l, and r.

◆ TEST() [18/67]

◆ TEST() [19/67]

TEST ( Bipartite  ,
TwoDisconnectedNodes   
)

◆ TEST() [20/67]

TEST ( BipartiteColor  ,
EnumValues   
)

Definition at line 863 of file bipartite_test.cc.

References Aleph::Bp_Blue, Aleph::Bp_Red, and Aleph::Bp_White.

◆ TEST() [21/67]

TEST ( BipartiteStress  ,
LargeBipartiteGraph   
)

◆ TEST() [22/67]

TEST ( BipartiteStress  ,
LargeEvenCycle   
)

◆ TEST() [23/67]

TEST ( BipartiteStress  ,
LargePathGraph   
)

◆ TEST() [24/67]

TEST ( ComputeBipartiteClass  ,
BasicUsage   
)

◆ TEST() [25/67]

TEST ( ComputeBipartiteClass  ,
ThrowsOnNonBipartite   
)

Definition at line 518 of file bipartite_test.cc.

References create_triangle(), l, and r.

◆ TEST() [26/67]

TEST ( HopcroftKarp  ,
CompleteBipartiteK22   
)

◆ TEST() [27/67]

TEST ( HopcroftKarp  ,
CompleteBipartiteK33   
)

◆ TEST() [28/67]

TEST ( HopcroftKarp  ,
CompleteBipartiteK55   
)

◆ TEST() [29/67]

TEST ( HopcroftKarp  ,
CrossValidateEvenCycle8   
)

Definition at line 1127 of file bipartite_test.cc.

References create_cycle_graph(), and Aleph::DynDlist< T >::size().

◆ TEST() [30/67]

TEST ( HopcroftKarp  ,
CrossValidateK44   
)

◆ TEST() [31/67]

TEST ( HopcroftKarp  ,
CrossValidatePath6   
)

Definition at line 1113 of file bipartite_test.cc.

References create_path_graph(), and Aleph::DynDlist< T >::size().

◆ TEST() [32/67]

◆ TEST() [33/67]

TEST ( HopcroftKarp  ,
EmptyGraph   
)

Definition at line 877 of file bipartite_test.cc.

References Aleph::Dlink::is_empty().

◆ TEST() [34/67]

TEST ( HopcroftKarp  ,
EvenCycle6   
)

◆ TEST() [35/67]

TEST ( HopcroftKarp  ,
FunctorMatchesFreeFunction   
)

◆ TEST() [36/67]

TEST ( HopcroftKarp  ,
FunctorWrapper   
)

◆ TEST() [37/67]

TEST ( HopcroftKarp  ,
OddCycleThrows   
)

Definition at line 1012 of file bipartite_test.cc.

References create_cycle_graph().

◆ TEST() [38/67]

TEST ( HopcroftKarp  ,
PathGraph4   
)

◆ TEST() [39/67]

TEST ( HopcroftKarp  ,
PathGraph5   
)

◆ TEST() [40/67]

◆ TEST() [41/67]

TEST ( HopcroftKarp  ,
SingleIsolatedNode   
)

◆ TEST() [42/67]

TEST ( HopcroftKarp  ,
StarGraph   
)

◆ TEST() [43/67]

TEST ( HopcroftKarp  ,
StressK100_100   
)

◆ TEST() [44/67]

TEST ( HopcroftKarp  ,
StressK50_50   
)

◆ TEST() [45/67]

TEST ( HopcroftKarp  ,
StressPath200   
)

◆ TEST() [46/67]

TEST ( HopcroftKarp  ,
TriangleThrows   
)

Definition at line 1003 of file bipartite_test.cc.

References create_triangle().

◆ TEST() [47/67]

◆ TEST() [48/67]

TEST ( HopcroftKarp  ,
TwoDisconnectedEdges   
)

◆ TEST() [49/67]

TEST ( HopcroftKarp  ,
UnbalancedK25   
)

◆ TEST() [50/67]

◆ TEST() [51/67]

TEST ( MaximumMatching  ,
CompleteBipartiteK22   
)

◆ TEST() [52/67]

TEST ( MaximumMatching  ,
CompleteBipartiteK33   
)

◆ TEST() [53/67]

TEST ( MaximumMatching  ,
CompleteBipartiteK55   
)

◆ TEST() [54/67]

◆ TEST() [55/67]

TEST ( MaximumMatching  ,
EmptyGraphReturnsEmpty   
)

Definition at line 530 of file bipartite_test.cc.

References Aleph::Dlink::is_empty().

◆ TEST() [56/67]

TEST ( MaximumMatching  ,
EvenCycle   
)

◆ TEST() [57/67]

◆ TEST() [58/67]

TEST ( MaximumMatching  ,
PathGraph4   
)

◆ TEST() [59/67]

TEST ( MaximumMatching  ,
PathGraph5   
)

◆ TEST() [60/67]

◆ TEST() [61/67]

TEST ( MaximumMatching  ,
StarGraph   
)

◆ TEST() [62/67]

TEST ( MaximumMatching  ,
ThrowsOnNonBipartite   
)

Definition at line 674 of file bipartite_test.cc.

References create_triangle().

◆ TEST() [63/67]

TEST ( MaximumMatching  ,
UnbalancedK25   
)

◆ TEST() [64/67]

TEST ( MaximumMatching  ,
UnbalancedK52   
)

◆ TEST() [65/67]

TEST ( MaximumMatchingClass  ,
BasicUsage   
)

◆ TEST() [66/67]

TEST ( MaximumMatchingClass  ,
ThrowsOnNonBipartite   
)

Definition at line 701 of file bipartite_test.cc.

References create_cycle_graph().

◆ TEST() [67/67]

TEST ( MaximumMatchingStress  ,
LargeMatching   
)

◆ verify_bipartition()

bool verify_bipartition ( const Graph &  g,
const DynDlist< Graph::Node * > &  l,
const DynDlist< Graph::Node * > &  r 
)

Verifies that a bipartition is valid:

  • All partitioned nodes are in exactly one partition
  • No edge between partitioned nodes connects two nodes in the same partition

Note: This function only verifies edges between nodes that were partitioned. Edges involving non-partitioned nodes (from disconnected components) are ignored.

Definition at line 201 of file bipartite_test.cc.

References Aleph::DynSetTree< Key, Tree, Compare >::contains(), LocateFunctions< Container, Type >::get_it(), GraphCommon< GT, Node, Arc >::get_src_node(), GraphCommon< GT, Node, Arc >::get_tgt_node(), Aleph::DynSetTree< Key, Tree, Compare >::insert(), l, Aleph::Filter_Iterator< Container, It, Show_Item >::next_ne(), and r.

Referenced by TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), and TEST().

◆ verify_matching()