test/max_matching_test.cc
author kpeter
Mon, 25 Feb 2008 12:35:06 +0000
changeset 2579 691ce54544c5
parent 2553 bfced05fa852
permissions -rw-r--r--
Bug fixes in min cost flow files.
Use enum type instead of static constants in NetworkSimplex to avoid
linker errors.
     1 /* -*- C++ -*-
     2  *
     3  * This file is a part of LEMON, a generic C++ optimization library
     4  *
     5  * Copyright (C) 2003-2008
     6  * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
     7  * (Egervary Research Group on Combinatorial Optimization, EGRES).
     8  *
     9  * Permission to use, modify and distribute this software is granted
    10  * provided that this copyright notice appears in all copies. For
    11  * precise terms see the accompanying LICENSE file.
    12  *
    13  * This software is provided "AS IS" with no warranty of any kind,
    14  * express or implied, and with no claim as to its suitability for any
    15  * purpose.
    16  *
    17  */
    18 
    19 #include <iostream>
    20 #include <vector>
    21 #include <queue>
    22 #include <lemon/math.h>
    23 #include <cstdlib>
    24 
    25 #include "test_tools.h"
    26 #include <lemon/list_graph.h>
    27 #include <lemon/max_matching.h>
    28 
    29 using namespace std;
    30 using namespace lemon;
    31 
    32 int main() {
    33 
    34   typedef ListUGraph Graph;
    35 
    36   typedef Graph::Edge Edge;
    37   typedef Graph::UEdgeIt UEdgeIt;
    38   typedef Graph::IncEdgeIt IncEdgeIt;
    39   typedef Graph::NodeIt NodeIt;
    40   typedef Graph::Node Node;
    41    
    42   Graph g;
    43   g.clear();
    44 
    45   std::vector<Graph::Node> nodes;
    46   for (int i=0; i<13; ++i)
    47       nodes.push_back(g.addNode());
    48 
    49   g.addEdge(nodes[0], nodes[0]);
    50   g.addEdge(nodes[6], nodes[10]);
    51   g.addEdge(nodes[5], nodes[10]);
    52   g.addEdge(nodes[4], nodes[10]);
    53   g.addEdge(nodes[3], nodes[11]);  
    54   g.addEdge(nodes[1], nodes[6]);
    55   g.addEdge(nodes[4], nodes[7]);  
    56   g.addEdge(nodes[1], nodes[8]);
    57   g.addEdge(nodes[0], nodes[8]);
    58   g.addEdge(nodes[3], nodes[12]);
    59   g.addEdge(nodes[6], nodes[9]);
    60   g.addEdge(nodes[9], nodes[11]);
    61   g.addEdge(nodes[2], nodes[10]);
    62   g.addEdge(nodes[10], nodes[8]);
    63   g.addEdge(nodes[5], nodes[8]);
    64   g.addEdge(nodes[6], nodes[3]);
    65   g.addEdge(nodes[0], nodes[5]);
    66   g.addEdge(nodes[6], nodes[12]);
    67   
    68   MaxMatching<Graph> max_matching(g);
    69   max_matching.init();
    70   max_matching.startDense();
    71   
    72   int s=0;
    73   Graph::NodeMap<Node> mate(g,INVALID);
    74   max_matching.mateMap(mate);
    75   for(NodeIt v(g); v!=INVALID; ++v) {
    76     if ( mate[v]!=INVALID ) ++s;
    77   }
    78   int size=int(s/2);  //size will be used as the size of a maxmatching
    79 
    80   for(NodeIt v(g); v!=INVALID; ++v) {
    81     max_matching.mate(v);
    82   }
    83 
    84   check ( size == max_matching.size(), "mate() returns a different size matching than max_matching.size()" );
    85 
    86   Graph::NodeMap<MaxMatching<Graph>::DecompType> pos0(g);
    87   max_matching.decomposition(pos0);
    88   
    89   max_matching.init();
    90   max_matching.startSparse();
    91   s=0;
    92   max_matching.mateMap(mate);
    93   for(NodeIt v(g); v!=INVALID; ++v) {
    94     if ( mate[v]!=INVALID ) ++s;
    95   }
    96   check ( int(s/2) == size, "The size does not equal!" );
    97 
    98   Graph::NodeMap<MaxMatching<Graph>::DecompType> pos1(g);
    99   max_matching.decomposition(pos1);
   100 
   101   max_matching.run();
   102   s=0;
   103   max_matching.mateMap(mate);
   104   for(NodeIt v(g); v!=INVALID; ++v) {
   105     if ( mate[v]!=INVALID ) ++s;
   106   }
   107   check ( int(s/2) == size, "The size does not equal!" ); 
   108   
   109   Graph::NodeMap<MaxMatching<Graph>::DecompType> pos2(g);
   110   max_matching.decomposition(pos2);
   111 
   112   max_matching.run();
   113   s=0;
   114   max_matching.mateMap(mate);
   115   for(NodeIt v(g); v!=INVALID; ++v) {
   116     if ( mate[v]!=INVALID ) ++s;
   117   }
   118   check ( int(s/2) == size, "The size does not equal!" ); 
   119   
   120   Graph::NodeMap<MaxMatching<Graph>::DecompType> pos(g);
   121   max_matching.decomposition(pos);
   122    
   123   bool ismatching=true;
   124   for(NodeIt v(g); v!=INVALID; ++v) {
   125     if ( mate[v]!=INVALID ) {
   126       Node u=mate[v];
   127       if (mate[u]!=v) ismatching=false; 
   128     }
   129   }  
   130   check ( ismatching, "It is not a matching!" );
   131 
   132   bool coincide=true;
   133   for(NodeIt v(g); v!=INVALID; ++v) {
   134    if ( pos0[v] != pos1[v] || pos1[v]!=pos2[v] || pos2[v]!=pos[v] ) {
   135      coincide=false;
   136     }
   137   }
   138   check ( coincide, "The decompositions do not coincide! " );
   139 
   140   bool noedge=true;
   141   for(UEdgeIt e(g); e!=INVALID; ++e) {
   142    if ( (pos[g.target(e)]==max_matching.C && 
   143 	 pos[g.source(e)]==max_matching.D) || 
   144 	 (pos[g.target(e)]==max_matching.D && 
   145 	  pos[g.source(e)]==max_matching.C) )
   146       noedge=false; 
   147   }
   148   check ( noedge, "There are edges between D and C!" );
   149   
   150   bool oddcomp=true;
   151   Graph::NodeMap<bool> todo(g,true);
   152   int num_comp=0;
   153   for(NodeIt v(g); v!=INVALID; ++v) {
   154    if ( pos[v]==max_matching.D && todo[v] ) {
   155       int comp_size=1;
   156       ++num_comp;
   157       std::queue<Node> Q;
   158       Q.push(v);
   159       todo.set(v,false);
   160       while (!Q.empty()) {
   161 	Node w=Q.front();	
   162 	Q.pop();
   163 	for(IncEdgeIt e(g,w); e!=INVALID; ++e) {
   164 	  Node u=g.runningNode(e);
   165 	  if ( pos[u]==max_matching.D && todo[u] ) {
   166 	    ++comp_size;
   167 	    Q.push(u);
   168 	    todo.set(u,false);
   169 	  }
   170 	}
   171       }
   172       if ( !(comp_size % 2) ) oddcomp=false;  
   173     }
   174   }
   175   check ( oddcomp, "A component of g[D] is not odd." );
   176 
   177   int barrier=0;
   178   for(NodeIt v(g); v!=INVALID; ++v) {
   179     if ( pos[v]==max_matching.A ) ++barrier;
   180   }
   181   int expected_size=int( countNodes(g)-num_comp+barrier)/2;
   182   check ( size==expected_size, "The size of the matching is wrong." );
   183   
   184   return 0;
   185 }