jacint@1078: /* -*- C++ -*- jacint@1078: * src/test/max_matching_test.cc - Part of LEMON, a generic C++ optimization library jacint@1078: * jacint@1078: * Copyright (C) 2004 Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport jacint@1078: * (Egervary Combinatorial Optimization Research Group, EGRES). jacint@1078: * jacint@1078: * Permission to use, modify and distribute this software is granted jacint@1078: * provided that this copyright notice appears in all copies. For jacint@1078: * precise terms see the accompanying LICENSE file. jacint@1078: * jacint@1078: * This software is provided "AS IS" with no warranty of any kind, jacint@1078: * express or implied, and with no claim as to its suitability for any jacint@1078: * purpose. jacint@1078: * jacint@1078: */ jacint@1078: jacint@1078: #include jacint@1078: #include jacint@1078: #include jacint@1078: #include jacint@1078: jacint@1078: #include "test_tools.h" jacint@1078: #include jacint@1078: #include jacint@1078: #include jacint@1078: #include jacint@1078: jacint@1078: using namespace std; jacint@1078: using namespace lemon; jacint@1078: jacint@1078: int main() { jacint@1078: jacint@1078: typedef UndirListGraph Graph; jacint@1078: jacint@1078: typedef Graph::Edge Edge; jacint@1078: typedef Graph::UndirEdgeIt UndirEdgeIt; jacint@1078: typedef Graph::IncEdgeIt IncEdgeIt; jacint@1078: typedef Graph::NodeIt NodeIt; jacint@1078: typedef Graph::Node Node; jacint@1078: jacint@1078: Graph G; jacint@1078: jacint@1078: random_init(); jacint@1078: randomGraph(G, random(5000), random(20000) ); jacint@1078: jacint@1078: MaxMatching max_matching(G); jacint@1078: max_matching.runEdmonds(0); jacint@1078: jacint@1078: int s=0; jacint@1078: Graph::NodeMap mate(G,INVALID); jacint@1078: max_matching.writeNMapNode(mate); jacint@1078: for(NodeIt v(G); v!=INVALID; ++v) { jacint@1078: if ( mate[v]!=INVALID ) ++s; jacint@1078: } jacint@1078: int size=(int)s/2; //size will be used as the size of a maxmatching jacint@1078: jacint@1078: check ( size == max_matching.size(), "mate() returns a different size matching than max_matching.size()" ); jacint@1078: jacint@1078: Graph::NodeMap::pos_enum> pos0(G); jacint@1078: max_matching.writePos(pos0); jacint@1078: jacint@1078: max_matching.resetPos(); jacint@1078: max_matching.resetMatching(); jacint@1078: max_matching.runEdmonds(1); jacint@1078: s=0; jacint@1078: max_matching.writeNMapNode(mate); jacint@1078: for(NodeIt v(G); v!=INVALID; ++v) { jacint@1078: if ( mate[v]!=INVALID ) ++s; jacint@1078: } jacint@1078: check ( (int)s/2 == size, "The size does not equal!" ); jacint@1078: jacint@1078: Graph::NodeMap::pos_enum> pos1(G); jacint@1078: max_matching.writePos(pos1); jacint@1078: jacint@1078: max_matching.resetPos(); jacint@1078: max_matching.run(); jacint@1078: s=0; jacint@1078: max_matching.writeNMapNode(mate); jacint@1078: for(NodeIt v(G); v!=INVALID; ++v) { jacint@1078: if ( mate[v]!=INVALID ) ++s; jacint@1078: } jacint@1078: check ( (int)s/2 == size, "The size does not equal!" ); jacint@1078: jacint@1078: Graph::NodeMap::pos_enum> pos2(G); jacint@1078: max_matching.writePos(pos2); jacint@1078: jacint@1078: max_matching.resetPos(); jacint@1078: max_matching.resetMatching(); jacint@1078: max_matching.run(); jacint@1078: s=0; jacint@1078: max_matching.writeNMapNode(mate); jacint@1078: for(NodeIt v(G); v!=INVALID; ++v) { jacint@1078: if ( mate[v]!=INVALID ) ++s; jacint@1078: } jacint@1078: check ( (int)s/2 == size, "The size does not equal!" ); jacint@1078: jacint@1078: Graph::NodeMap::pos_enum> pos(G); jacint@1078: max_matching.writePos(pos); jacint@1078: jacint@1078: bool ismatching=true; jacint@1078: for(NodeIt v(G); v!=INVALID; ++v) { jacint@1078: if ( mate[v]!=INVALID ) { jacint@1078: Node u=mate[v]; jacint@1078: if (mate[u]!=v) ismatching=false; jacint@1078: } jacint@1078: } jacint@1078: check ( ismatching, "It is not a matching!" ); jacint@1078: jacint@1078: bool coincide=true; jacint@1078: for(NodeIt v(G); v!=INVALID; ++v) { jacint@1078: if ( pos0[v] != pos1[v] || pos1[v]!=pos2[v] || pos2[v]!=pos[v] ) { jacint@1078: coincide=false; jacint@1078: } jacint@1078: } jacint@1078: check ( coincide, "The decompositions do not coincide! " ); jacint@1078: jacint@1078: bool noedge=true; jacint@1078: for(UndirEdgeIt e(G); e!=INVALID; ++e) { jacint@1078: if ( (pos[G.target(e)]==max_matching.C && pos[G.source(e)]==max_matching.D) || jacint@1078: (pos[G.target(e)]==max_matching.D && pos[G.source(e)]==max_matching.C) ) jacint@1078: noedge=false; jacint@1078: } jacint@1078: check ( noedge, "There are edges between D and C!" ); jacint@1078: jacint@1078: bool oddcomp=true; jacint@1078: Graph::NodeMap todo(G,true); jacint@1078: int num_comp=0; jacint@1078: for(NodeIt v(G); v!=INVALID; ++v) { jacint@1078: if ( pos[v]==max_matching.D && todo[v] ) { jacint@1078: int comp_size=1; jacint@1078: ++num_comp; jacint@1078: std::queue Q; jacint@1078: Q.push(v); jacint@1078: todo.set(v,false); jacint@1078: while (!Q.empty()) { jacint@1078: Node w=Q.front(); jacint@1078: Q.pop(); jacint@1078: for(IncEdgeIt e(G,w); e!=INVALID; ++e) { jacint@1078: Node u=G.target(e); jacint@1078: if ( pos[u]==max_matching.D && todo[u] ) { jacint@1078: ++comp_size; jacint@1078: Q.push(u); jacint@1078: todo.set(u,false); jacint@1078: } jacint@1078: } jacint@1078: } jacint@1078: if ( !(comp_size % 2) ) oddcomp=false; jacint@1078: } jacint@1078: } jacint@1078: check ( oddcomp, "A component of G[D] is not odd." ); jacint@1078: jacint@1078: int barrier=0; jacint@1078: for(NodeIt v(G); v!=INVALID; ++v) { jacint@1078: if ( pos[v]==max_matching.A ) ++barrier; jacint@1078: } jacint@1078: int expected_size=(int)( countNodes(G)-num_comp+barrier)/2; jacint@1078: check ( size==expected_size, "The size of the matching is wrong." ); jacint@1078: jacint@1078: return 0; jacint@1078: } jacint@1078: jacint@1078: jacint@1078: void random_init() jacint@1078: { jacint@1078: unsigned int seed = getpid(); jacint@1078: seed |= seed << 15; jacint@1078: seed ^= time(0); jacint@1078: jacint@1078: srand(seed); jacint@1078: } jacint@1078: jacint@1078: jacint@1078: int random(int m) jacint@1078: { jacint@1078: return int( double(m) * rand() / (RAND_MAX + 1.0) ); jacint@1078: } jacint@1078: jacint@1078: jacint@1078: /// Generates a random graph with n nodes and m edges. jacint@1078: /// Before generating the random graph, \c g.clear() is called. jacint@1078: template jacint@1078: void randomGraph(Graph& g, int n, int m) { jacint@1078: g.clear(); jacint@1078: std::vector nodes; jacint@1078: for (int i=0; i