COIN-OR::LEMON - Graph Library

source: lemon-0.x/test/max_matching_test.cc @ 1979:c2992fd74dad

Last change on this file since 1979:c2992fd74dad was 1956:a055123339d5, checked in by Alpar Juttner, 18 years ago

Unified copyright notices

File size: 4.8 KB
RevLine 
[1078]1/* -*- C++ -*-
2 *
[1956]3 * This file is a part of LEMON, a generic C++ optimization library
4 *
5 * Copyright (C) 2003-2006
6 * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
[1359]7 * (Egervary Research Group on Combinatorial Optimization, EGRES).
[1078]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>
[1098]20#include <vector>
[1078]21#include <queue>
[1494]22#include <cmath>
[1078]23#include <cstdlib>
24
25#include "test_tools.h"
26#include <lemon/invalid.h>
27#include <lemon/list_graph.h>
[1092]28#include <lemon/max_matching.h>
[1078]29
30using namespace std;
31using namespace lemon;
32
33int main() {
34
[1909]35  typedef ListUGraph Graph;
[1078]36
37  typedef Graph::Edge Edge;
[1909]38  typedef Graph::UEdgeIt UEdgeIt;
[1078]39  typedef Graph::IncEdgeIt IncEdgeIt;
40  typedef Graph::NodeIt NodeIt;
41  typedef Graph::Node Node;
42   
[1098]43  Graph g;
44  g.clear();
[1078]45
[1098]46  std::vector<Graph::Node> nodes;
47  for (int i=0; i<13; ++i)
48      nodes.push_back(g.addNode());
[1078]49
[1098]50  g.addEdge(nodes[0], nodes[0]);
51  g.addEdge(nodes[6], nodes[10]);
52  g.addEdge(nodes[5], nodes[10]);
53  g.addEdge(nodes[4], nodes[10]);
54  g.addEdge(nodes[3], nodes[11]); 
55  g.addEdge(nodes[1], nodes[6]);
56  g.addEdge(nodes[4], nodes[7]); 
57  g.addEdge(nodes[1], nodes[8]);
58  g.addEdge(nodes[0], nodes[8]);
59  g.addEdge(nodes[3], nodes[12]);
60  g.addEdge(nodes[6], nodes[9]);
61  g.addEdge(nodes[9], nodes[11]);
62  g.addEdge(nodes[2], nodes[10]);
63  g.addEdge(nodes[10], nodes[8]);
64  g.addEdge(nodes[5], nodes[8]);
65  g.addEdge(nodes[6], nodes[3]);
66  g.addEdge(nodes[0], nodes[5]);
67  g.addEdge(nodes[6], nodes[12]);
68 
69  MaxMatching<Graph> max_matching(g);
[1078]70  max_matching.runEdmonds(0);
71 
72  int s=0;
[1098]73  Graph::NodeMap<Node> mate(g,INVALID);
[1078]74  max_matching.writeNMapNode(mate);
[1098]75  for(NodeIt v(g); v!=INVALID; ++v) {
[1078]76    if ( mate[v]!=INVALID ) ++s;
77  }
78  int size=(int)s/2;  //size will be used as the size of a maxmatching
[1092]79
[1098]80  for(NodeIt v(g); v!=INVALID; ++v) {
[1092]81    max_matching.mate(v);
82  }
83
[1078]84  check ( size == max_matching.size(), "mate() returns a different size matching than max_matching.size()" );
85
[1098]86  Graph::NodeMap<MaxMatching<Graph>::pos_enum> pos0(g);
[1078]87  max_matching.writePos(pos0);
88 
89  max_matching.resetMatching();
90  max_matching.runEdmonds(1);
91  s=0;
92  max_matching.writeNMapNode(mate);
[1098]93  for(NodeIt v(g); v!=INVALID; ++v) {
[1078]94    if ( mate[v]!=INVALID ) ++s;
95  }
96  check ( (int)s/2 == size, "The size does not equal!" );
97
[1098]98  Graph::NodeMap<MaxMatching<Graph>::pos_enum> pos1(g);
[1078]99  max_matching.writePos(pos1);
100
101  max_matching.run();
102  s=0;
103  max_matching.writeNMapNode(mate);
[1098]104  for(NodeIt v(g); v!=INVALID; ++v) {
[1078]105    if ( mate[v]!=INVALID ) ++s;
106  }
107  check ( (int)s/2 == size, "The size does not equal!" );
108 
[1098]109  Graph::NodeMap<MaxMatching<Graph>::pos_enum> pos2(g);
[1078]110  max_matching.writePos(pos2);
111
112  max_matching.resetMatching();
113  max_matching.run();
114  s=0;
115  max_matching.writeNMapNode(mate);
[1098]116  for(NodeIt v(g); v!=INVALID; ++v) {
[1078]117    if ( mate[v]!=INVALID ) ++s;
118  }
119  check ( (int)s/2 == size, "The size does not equal!" );
120 
[1098]121  Graph::NodeMap<MaxMatching<Graph>::pos_enum> pos(g);
[1078]122  max_matching.writePos(pos);
123   
124  bool ismatching=true;
[1098]125  for(NodeIt v(g); v!=INVALID; ++v) {
[1078]126    if ( mate[v]!=INVALID ) {
127      Node u=mate[v];
128      if (mate[u]!=v) ismatching=false;
129    }
130  } 
131  check ( ismatching, "It is not a matching!" );
132
133  bool coincide=true;
[1098]134  for(NodeIt v(g); v!=INVALID; ++v) {
[1078]135   if ( pos0[v] != pos1[v] || pos1[v]!=pos2[v] || pos2[v]!=pos[v] ) {
136     coincide=false;
137    }
138  }
139  check ( coincide, "The decompositions do not coincide! " );
140
141  bool noedge=true;
[1909]142  for(UEdgeIt e(g); e!=INVALID; ++e) {
[1098]143   if ( (pos[g.target(e)]==max_matching.C && pos[g.source(e)]==max_matching.D) ||
144         (pos[g.target(e)]==max_matching.D && pos[g.source(e)]==max_matching.C) )
[1078]145      noedge=false;
146  }
147  check ( noedge, "There are edges between D and C!" );
148 
149  bool oddcomp=true;
[1098]150  Graph::NodeMap<bool> todo(g,true);
[1078]151  int num_comp=0;
[1098]152  for(NodeIt v(g); v!=INVALID; ++v) {
[1078]153   if ( pos[v]==max_matching.D && todo[v] ) {
154      int comp_size=1;
155      ++num_comp;
156      std::queue<Node> Q;
157      Q.push(v);
158      todo.set(v,false);
159      while (!Q.empty()) {
160        Node w=Q.front();       
161        Q.pop();
[1098]162        for(IncEdgeIt e(g,w); e!=INVALID; ++e) {
[1158]163          Node u=g.runningNode(e);
[1078]164          if ( pos[u]==max_matching.D && todo[u] ) {
165            ++comp_size;
166            Q.push(u);
167            todo.set(u,false);
168          }
169        }
170      }
171      if ( !(comp_size % 2) ) oddcomp=false; 
172    }
173  }
[1098]174  check ( oddcomp, "A component of g[D] is not odd." );
[1078]175
176  int barrier=0;
[1098]177  for(NodeIt v(g); v!=INVALID; ++v) {
[1078]178    if ( pos[v]==max_matching.A ) ++barrier;
179  }
[1098]180  int expected_size=(int)( countNodes(g)-num_comp+barrier)/2;
[1078]181  check ( size==expected_size, "The size of the matching is wrong." );
182 
183  return 0;
184}
Note: See TracBrowser for help on using the repository browser.