jacint@1078: /* -*- C++ -*- jacint@1078: * alpar@1956: * This file is a part of LEMON, a generic C++ optimization library alpar@1956: * alpar@1956: * Copyright (C) 2003-2006 alpar@1956: * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport alpar@1359: * (Egervary Research Group on Combinatorial Optimization, 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@1098: #include jacint@1078: #include alpar@1494: #include jacint@1078: #include jacint@1078: jacint@1078: #include "test_tools.h" jacint@1078: #include jacint@1092: #include jacint@1078: jacint@1078: using namespace std; jacint@1078: using namespace lemon; jacint@1078: jacint@1078: int main() { jacint@1078: klao@1909: typedef ListUGraph Graph; jacint@1078: jacint@1078: typedef Graph::Edge Edge; klao@1909: typedef Graph::UEdgeIt UEdgeIt; jacint@1078: typedef Graph::IncEdgeIt IncEdgeIt; jacint@1078: typedef Graph::NodeIt NodeIt; jacint@1078: typedef Graph::Node Node; jacint@1078: jacint@1098: Graph g; jacint@1098: g.clear(); jacint@1078: jacint@1098: std::vector nodes; jacint@1098: for (int i=0; i<13; ++i) jacint@1098: nodes.push_back(g.addNode()); jacint@1078: jacint@1098: g.addEdge(nodes[0], nodes[0]); jacint@1098: g.addEdge(nodes[6], nodes[10]); jacint@1098: g.addEdge(nodes[5], nodes[10]); jacint@1098: g.addEdge(nodes[4], nodes[10]); jacint@1098: g.addEdge(nodes[3], nodes[11]); jacint@1098: g.addEdge(nodes[1], nodes[6]); jacint@1098: g.addEdge(nodes[4], nodes[7]); jacint@1098: g.addEdge(nodes[1], nodes[8]); jacint@1098: g.addEdge(nodes[0], nodes[8]); jacint@1098: g.addEdge(nodes[3], nodes[12]); jacint@1098: g.addEdge(nodes[6], nodes[9]); jacint@1098: g.addEdge(nodes[9], nodes[11]); jacint@1098: g.addEdge(nodes[2], nodes[10]); jacint@1098: g.addEdge(nodes[10], nodes[8]); jacint@1098: g.addEdge(nodes[5], nodes[8]); jacint@1098: g.addEdge(nodes[6], nodes[3]); jacint@1098: g.addEdge(nodes[0], nodes[5]); jacint@1098: g.addEdge(nodes[6], nodes[12]); jacint@1098: jacint@1098: MaxMatching max_matching(g); jacint@1078: max_matching.runEdmonds(0); jacint@1078: jacint@1078: int s=0; jacint@1098: Graph::NodeMap mate(g,INVALID); jacint@1078: max_matching.writeNMapNode(mate); jacint@1098: 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@1092: jacint@1098: for(NodeIt v(g); v!=INVALID; ++v) { jacint@1092: max_matching.mate(v); jacint@1092: } jacint@1092: jacint@1078: check ( size == max_matching.size(), "mate() returns a different size matching than max_matching.size()" ); jacint@1078: jacint@1098: Graph::NodeMap::pos_enum> pos0(g); jacint@1078: max_matching.writePos(pos0); jacint@1078: jacint@1078: max_matching.resetMatching(); jacint@1078: max_matching.runEdmonds(1); jacint@1078: s=0; jacint@1078: max_matching.writeNMapNode(mate); jacint@1098: 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@1098: Graph::NodeMap::pos_enum> pos1(g); jacint@1078: max_matching.writePos(pos1); jacint@1078: jacint@1078: max_matching.run(); jacint@1078: s=0; jacint@1078: max_matching.writeNMapNode(mate); jacint@1098: 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@1098: Graph::NodeMap::pos_enum> pos2(g); jacint@1078: max_matching.writePos(pos2); jacint@1078: jacint@1078: max_matching.resetMatching(); jacint@1078: max_matching.run(); jacint@1078: s=0; jacint@1078: max_matching.writeNMapNode(mate); jacint@1098: 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@1098: Graph::NodeMap::pos_enum> pos(g); jacint@1078: max_matching.writePos(pos); jacint@1078: jacint@1078: bool ismatching=true; jacint@1098: 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@1098: 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; klao@1909: for(UEdgeIt e(g); e!=INVALID; ++e) { jacint@1098: if ( (pos[g.target(e)]==max_matching.C && pos[g.source(e)]==max_matching.D) || jacint@1098: (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@1098: Graph::NodeMap todo(g,true); jacint@1078: int num_comp=0; jacint@1098: 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@1098: for(IncEdgeIt e(g,w); e!=INVALID; ++e) { klao@1158: Node u=g.runningNode(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@1098: check ( oddcomp, "A component of g[D] is not odd." ); jacint@1078: jacint@1078: int barrier=0; jacint@1098: for(NodeIt v(g); v!=INVALID; ++v) { jacint@1078: if ( pos[v]==max_matching.A ) ++barrier; jacint@1078: } jacint@1098: 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: }