# HG changeset patch # User jacint # Date 1106842314 0 # Node ID e3b3667c68571ca8a4aea2580d54250b6401a382 # Parent c91e765266d717a340a9c1a33c7dcc876c15ceb1 diff -r c91e765266d7 -r e3b3667c6857 src/test/max_matching_test.cc --- a/src/test/max_matching_test.cc Wed Jan 26 15:54:06 2005 +0000 +++ b/src/test/max_matching_test.cc Thu Jan 27 16:11:54 2005 +0000 @@ -15,6 +15,7 @@ */ #include +#include #include #include #include @@ -23,8 +24,6 @@ #include #include #include -//temporarily -#include using namespace std; using namespace lemon; @@ -39,68 +38,89 @@ typedef Graph::NodeIt NodeIt; typedef Graph::Node Node; - Graph G; + Graph g; + g.clear(); - random_init(); - randomGraph(G, random(5000), random(20000) ); + std::vector nodes; + for (int i=0; i<13; ++i) + nodes.push_back(g.addNode()); - MaxMatching max_matching(G); + g.addEdge(nodes[0], nodes[0]); + g.addEdge(nodes[6], nodes[10]); + g.addEdge(nodes[5], nodes[10]); + g.addEdge(nodes[4], nodes[10]); + g.addEdge(nodes[3], nodes[11]); + g.addEdge(nodes[1], nodes[6]); + g.addEdge(nodes[4], nodes[7]); + g.addEdge(nodes[1], nodes[8]); + g.addEdge(nodes[0], nodes[8]); + g.addEdge(nodes[3], nodes[12]); + g.addEdge(nodes[6], nodes[9]); + g.addEdge(nodes[9], nodes[11]); + g.addEdge(nodes[2], nodes[10]); + g.addEdge(nodes[10], nodes[8]); + g.addEdge(nodes[5], nodes[8]); + g.addEdge(nodes[6], nodes[3]); + g.addEdge(nodes[0], nodes[5]); + g.addEdge(nodes[6], nodes[12]); + + MaxMatching max_matching(g); max_matching.runEdmonds(0); int s=0; - Graph::NodeMap mate(G,INVALID); + Graph::NodeMap mate(g,INVALID); max_matching.writeNMapNode(mate); - for(NodeIt v(G); v!=INVALID; ++v) { + for(NodeIt v(g); v!=INVALID; ++v) { if ( mate[v]!=INVALID ) ++s; } int size=(int)s/2; //size will be used as the size of a maxmatching - for(NodeIt v(G); v!=INVALID; ++v) { + for(NodeIt v(g); v!=INVALID; ++v) { max_matching.mate(v); } check ( size == max_matching.size(), "mate() returns a different size matching than max_matching.size()" ); - Graph::NodeMap::pos_enum> pos0(G); + Graph::NodeMap::pos_enum> pos0(g); max_matching.writePos(pos0); max_matching.resetMatching(); max_matching.runEdmonds(1); s=0; max_matching.writeNMapNode(mate); - for(NodeIt v(G); v!=INVALID; ++v) { + for(NodeIt v(g); v!=INVALID; ++v) { if ( mate[v]!=INVALID ) ++s; } check ( (int)s/2 == size, "The size does not equal!" ); - Graph::NodeMap::pos_enum> pos1(G); + Graph::NodeMap::pos_enum> pos1(g); max_matching.writePos(pos1); max_matching.run(); s=0; max_matching.writeNMapNode(mate); - for(NodeIt v(G); v!=INVALID; ++v) { + for(NodeIt v(g); v!=INVALID; ++v) { if ( mate[v]!=INVALID ) ++s; } check ( (int)s/2 == size, "The size does not equal!" ); - Graph::NodeMap::pos_enum> pos2(G); + Graph::NodeMap::pos_enum> pos2(g); max_matching.writePos(pos2); max_matching.resetMatching(); max_matching.run(); s=0; max_matching.writeNMapNode(mate); - for(NodeIt v(G); v!=INVALID; ++v) { + for(NodeIt v(g); v!=INVALID; ++v) { if ( mate[v]!=INVALID ) ++s; } check ( (int)s/2 == size, "The size does not equal!" ); - Graph::NodeMap::pos_enum> pos(G); + Graph::NodeMap::pos_enum> pos(g); max_matching.writePos(pos); bool ismatching=true; - for(NodeIt v(G); v!=INVALID; ++v) { + for(NodeIt v(g); v!=INVALID; ++v) { if ( mate[v]!=INVALID ) { Node u=mate[v]; if (mate[u]!=v) ismatching=false; @@ -109,7 +129,7 @@ check ( ismatching, "It is not a matching!" ); bool coincide=true; - for(NodeIt v(G); v!=INVALID; ++v) { + for(NodeIt v(g); v!=INVALID; ++v) { if ( pos0[v] != pos1[v] || pos1[v]!=pos2[v] || pos2[v]!=pos[v] ) { coincide=false; } @@ -117,17 +137,17 @@ check ( coincide, "The decompositions do not coincide! " ); bool noedge=true; - for(UndirEdgeIt e(G); e!=INVALID; ++e) { - if ( (pos[G.target(e)]==max_matching.C && pos[G.source(e)]==max_matching.D) || - (pos[G.target(e)]==max_matching.D && pos[G.source(e)]==max_matching.C) ) + for(UndirEdgeIt e(g); e!=INVALID; ++e) { + if ( (pos[g.target(e)]==max_matching.C && pos[g.source(e)]==max_matching.D) || + (pos[g.target(e)]==max_matching.D && pos[g.source(e)]==max_matching.C) ) noedge=false; } check ( noedge, "There are edges between D and C!" ); bool oddcomp=true; - Graph::NodeMap todo(G,true); + Graph::NodeMap todo(g,true); int num_comp=0; - for(NodeIt v(G); v!=INVALID; ++v) { + for(NodeIt v(g); v!=INVALID; ++v) { if ( pos[v]==max_matching.D && todo[v] ) { int comp_size=1; ++num_comp; @@ -137,8 +157,8 @@ while (!Q.empty()) { Node w=Q.front(); Q.pop(); - for(IncEdgeIt e(G,w); e!=INVALID; ++e) { - Node u=G.target(e); + for(IncEdgeIt e(g,w); e!=INVALID; ++e) { + Node u=g.target(e); if ( pos[u]==max_matching.D && todo[u] ) { ++comp_size; Q.push(u); @@ -149,43 +169,14 @@ if ( !(comp_size % 2) ) oddcomp=false; } } - check ( oddcomp, "A component of G[D] is not odd." ); + check ( oddcomp, "A component of g[D] is not odd." ); int barrier=0; - for(NodeIt v(G); v!=INVALID; ++v) { + for(NodeIt v(g); v!=INVALID; ++v) { if ( pos[v]==max_matching.A ) ++barrier; } - int expected_size=(int)( countNodes(G)-num_comp+barrier)/2; + int expected_size=(int)( countNodes(g)-num_comp+barrier)/2; check ( size==expected_size, "The size of the matching is wrong." ); return 0; } - - -void random_init() -{ - unsigned int seed = getpid(); - seed |= seed << 15; - seed ^= time(0); - - srand(seed); -} - - -int random(int m) -{ - return int( double(m) * rand() / (RAND_MAX + 1.0) ); -} - - -/// Generates a random graph with n nodes and m edges. -/// Before generating the random graph, \c g.clear() is called. -template -void randomGraph(Graph& g, int n, int m) { - g.clear(); - std::vector nodes; - for (int i=0; i