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