deba@338: /* -*- mode: C++; indent-tabs-mode: nil; -*- deba@338: * deba@338: * This file is a part of LEMON, a generic C++ optimization library. deba@338: * deba@338: * Copyright (C) 2003-2008 deba@338: * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport deba@338: * (Egervary Research Group on Combinatorial Optimization, EGRES). deba@338: * deba@338: * Permission to use, modify and distribute this software is granted deba@338: * provided that this copyright notice appears in all copies. For deba@338: * precise terms see the accompanying LICENSE file. deba@338: * deba@338: * This software is provided "AS IS" with no warranty of any kind, deba@338: * express or implied, and with no claim as to its suitability for any deba@338: * purpose. deba@338: * deba@338: */ deba@338: deba@338: #include deba@338: #include deba@338: #include deba@338: #include deba@338: #include deba@338: deba@338: #include "test_tools.h" deba@338: #include deba@338: #include deba@338: deba@338: using namespace std; deba@338: using namespace lemon; deba@338: deba@338: int main() { deba@338: deba@338: typedef ListGraph Graph; deba@338: deba@338: GRAPH_TYPEDEFS(Graph); deba@338: deba@338: Graph g; deba@338: g.clear(); deba@338: deba@338: std::vector nodes; deba@338: for (int i=0; i<13; ++i) deba@338: nodes.push_back(g.addNode()); deba@338: deba@338: g.addEdge(nodes[0], nodes[0]); deba@338: g.addEdge(nodes[6], nodes[10]); deba@338: g.addEdge(nodes[5], nodes[10]); deba@338: g.addEdge(nodes[4], nodes[10]); deba@338: g.addEdge(nodes[3], nodes[11]); deba@338: g.addEdge(nodes[1], nodes[6]); deba@338: g.addEdge(nodes[4], nodes[7]); deba@338: g.addEdge(nodes[1], nodes[8]); deba@338: g.addEdge(nodes[0], nodes[8]); deba@338: g.addEdge(nodes[3], nodes[12]); deba@338: g.addEdge(nodes[6], nodes[9]); deba@338: g.addEdge(nodes[9], nodes[11]); deba@338: g.addEdge(nodes[2], nodes[10]); deba@338: g.addEdge(nodes[10], nodes[8]); deba@338: g.addEdge(nodes[5], nodes[8]); deba@338: g.addEdge(nodes[6], nodes[3]); deba@338: g.addEdge(nodes[0], nodes[5]); deba@338: g.addEdge(nodes[6], nodes[12]); deba@338: deba@338: MaxMatching max_matching(g); deba@338: max_matching.init(); deba@338: max_matching.startDense(); deba@338: deba@338: int s=0; deba@338: Graph::NodeMap mate(g,INVALID); deba@338: max_matching.mateMap(mate); deba@338: for(NodeIt v(g); v!=INVALID; ++v) { deba@338: if ( mate[v]!=INVALID ) ++s; deba@338: } deba@338: int size=int(s/2); //size will be used as the size of a maxmatching deba@338: deba@338: for(NodeIt v(g); v!=INVALID; ++v) { deba@338: max_matching.mate(v); deba@338: } deba@338: deba@338: check ( size == max_matching.size(), "mate() returns a different size matching than max_matching.size()" ); deba@338: deba@338: Graph::NodeMap::DecompType> pos0(g); deba@338: max_matching.decomposition(pos0); deba@338: deba@338: max_matching.init(); deba@338: max_matching.startSparse(); deba@338: s=0; deba@338: max_matching.mateMap(mate); deba@338: for(NodeIt v(g); v!=INVALID; ++v) { deba@338: if ( mate[v]!=INVALID ) ++s; deba@338: } deba@338: check ( int(s/2) == size, "The size does not equal!" ); deba@338: deba@338: Graph::NodeMap::DecompType> pos1(g); deba@338: max_matching.decomposition(pos1); deba@338: deba@338: max_matching.run(); deba@338: s=0; deba@338: max_matching.mateMap(mate); deba@338: for(NodeIt v(g); v!=INVALID; ++v) { deba@338: if ( mate[v]!=INVALID ) ++s; deba@338: } deba@338: check ( int(s/2) == size, "The size does not equal!" ); deba@338: deba@338: Graph::NodeMap::DecompType> pos2(g); deba@338: max_matching.decomposition(pos2); deba@338: deba@338: max_matching.run(); deba@338: s=0; deba@338: max_matching.mateMap(mate); deba@338: for(NodeIt v(g); v!=INVALID; ++v) { deba@338: if ( mate[v]!=INVALID ) ++s; deba@338: } deba@338: check ( int(s/2) == size, "The size does not equal!" ); deba@338: deba@338: Graph::NodeMap::DecompType> pos(g); deba@338: max_matching.decomposition(pos); deba@338: deba@338: bool ismatching=true; deba@338: for(NodeIt v(g); v!=INVALID; ++v) { deba@338: if ( mate[v]!=INVALID ) { deba@338: Node u=mate[v]; deba@338: if (mate[u]!=v) ismatching=false; deba@338: } deba@338: } deba@338: check ( ismatching, "It is not a matching!" ); deba@338: deba@338: bool coincide=true; deba@338: for(NodeIt v(g); v!=INVALID; ++v) { deba@338: if ( pos0[v] != pos1[v] || pos1[v]!=pos2[v] || pos2[v]!=pos[v] ) { deba@338: coincide=false; deba@338: } deba@338: } deba@338: check ( coincide, "The decompositions do not coincide! " ); deba@338: deba@338: bool noarc=true; deba@338: for(EdgeIt e(g); e!=INVALID; ++e) { deba@338: if ( (pos[g.v(e)]==max_matching.C && deba@338: pos[g.u(e)]==max_matching.D) || deba@338: (pos[g.v(e)]==max_matching.D && deba@338: pos[g.u(e)]==max_matching.C) ) deba@338: noarc=false; deba@338: } deba@338: check ( noarc, "There are arcs between D and C!" ); deba@338: deba@338: bool oddcomp=true; deba@338: Graph::NodeMap todo(g,true); deba@338: int num_comp=0; deba@338: for(NodeIt v(g); v!=INVALID; ++v) { deba@338: if ( pos[v]==max_matching.D && todo[v] ) { deba@338: int comp_size=1; deba@338: ++num_comp; deba@338: std::queue Q; deba@338: Q.push(v); deba@338: todo.set(v,false); deba@338: while (!Q.empty()) { deba@338: Node w=Q.front(); deba@338: Q.pop(); deba@338: for(IncEdgeIt e(g,w); e!=INVALID; ++e) { deba@338: Node u=g.runningNode(e); deba@338: if ( pos[u]==max_matching.D && todo[u] ) { deba@338: ++comp_size; deba@338: Q.push(u); deba@338: todo.set(u,false); deba@338: } deba@338: } deba@338: } deba@338: if ( !(comp_size % 2) ) oddcomp=false; deba@338: } deba@338: } deba@338: check ( oddcomp, "A component of g[D] is not odd." ); deba@338: deba@338: int barrier=0; deba@338: for(NodeIt v(g); v!=INVALID; ++v) { deba@338: if ( pos[v]==max_matching.A ) ++barrier; deba@338: } deba@338: int expected_size=int( countNodes(g)-num_comp+barrier)/2; deba@338: check ( size==expected_size, "The size of the matching is wrong." ); deba@338: deba@338: return 0; deba@338: }