COIN-OR::LEMON - Graph Library

source: lemon-main/test/max_matching_test.cc @ 326:64ad48007fb2

Last change on this file since 326:64ad48007fb2 was 326:64ad48007fb2, checked in by Balazs Dezso <deba@…>, 16 years ago

Port maximum matching algorithms from svn 3498 (ticket #48)

File size: 4.7 KB
Line 
1/* -*- mode: C++; indent-tabs-mode: nil; -*-
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
29using namespace std;
30using namespace lemon;
31
32int main() {
33
34  typedef ListGraph Graph;
35
36  GRAPH_TYPEDEFS(Graph);
37
38  Graph g;
39  g.clear();
40
41  std::vector<Graph::Node> nodes;
42  for (int i=0; i<13; ++i)
43      nodes.push_back(g.addNode());
44
45  g.addEdge(nodes[0], nodes[0]);
46  g.addEdge(nodes[6], nodes[10]);
47  g.addEdge(nodes[5], nodes[10]);
48  g.addEdge(nodes[4], nodes[10]);
49  g.addEdge(nodes[3], nodes[11]);
50  g.addEdge(nodes[1], nodes[6]);
51  g.addEdge(nodes[4], nodes[7]);
52  g.addEdge(nodes[1], nodes[8]);
53  g.addEdge(nodes[0], nodes[8]);
54  g.addEdge(nodes[3], nodes[12]);
55  g.addEdge(nodes[6], nodes[9]);
56  g.addEdge(nodes[9], nodes[11]);
57  g.addEdge(nodes[2], nodes[10]);
58  g.addEdge(nodes[10], nodes[8]);
59  g.addEdge(nodes[5], nodes[8]);
60  g.addEdge(nodes[6], nodes[3]);
61  g.addEdge(nodes[0], nodes[5]);
62  g.addEdge(nodes[6], nodes[12]);
63
64  MaxMatching<Graph> max_matching(g);
65  max_matching.init();
66  max_matching.startDense();
67
68  int s=0;
69  Graph::NodeMap<Node> mate(g,INVALID);
70  max_matching.mateMap(mate);
71  for(NodeIt v(g); v!=INVALID; ++v) {
72    if ( mate[v]!=INVALID ) ++s;
73  }
74  int size=int(s/2);  //size will be used as the size of a maxmatching
75
76  for(NodeIt v(g); v!=INVALID; ++v) {
77    max_matching.mate(v);
78  }
79
80  check ( size == max_matching.size(), "mate() returns a different size matching than max_matching.size()" );
81
82  Graph::NodeMap<MaxMatching<Graph>::DecompType> pos0(g);
83  max_matching.decomposition(pos0);
84
85  max_matching.init();
86  max_matching.startSparse();
87  s=0;
88  max_matching.mateMap(mate);
89  for(NodeIt v(g); v!=INVALID; ++v) {
90    if ( mate[v]!=INVALID ) ++s;
91  }
92  check ( int(s/2) == size, "The size does not equal!" );
93
94  Graph::NodeMap<MaxMatching<Graph>::DecompType> pos1(g);
95  max_matching.decomposition(pos1);
96
97  max_matching.run();
98  s=0;
99  max_matching.mateMap(mate);
100  for(NodeIt v(g); v!=INVALID; ++v) {
101    if ( mate[v]!=INVALID ) ++s;
102  }
103  check ( int(s/2) == size, "The size does not equal!" );
104
105  Graph::NodeMap<MaxMatching<Graph>::DecompType> pos2(g);
106  max_matching.decomposition(pos2);
107
108  max_matching.run();
109  s=0;
110  max_matching.mateMap(mate);
111  for(NodeIt v(g); v!=INVALID; ++v) {
112    if ( mate[v]!=INVALID ) ++s;
113  }
114  check ( int(s/2) == size, "The size does not equal!" );
115
116  Graph::NodeMap<MaxMatching<Graph>::DecompType> pos(g);
117  max_matching.decomposition(pos);
118
119  bool ismatching=true;
120  for(NodeIt v(g); v!=INVALID; ++v) {
121    if ( mate[v]!=INVALID ) {
122      Node u=mate[v];
123      if (mate[u]!=v) ismatching=false;
124    }
125  }
126  check ( ismatching, "It is not a matching!" );
127
128  bool coincide=true;
129  for(NodeIt v(g); v!=INVALID; ++v) {
130   if ( pos0[v] != pos1[v] || pos1[v]!=pos2[v] || pos2[v]!=pos[v] ) {
131     coincide=false;
132    }
133  }
134  check ( coincide, "The decompositions do not coincide! " );
135
136  bool noarc=true;
137  for(EdgeIt e(g); e!=INVALID; ++e) {
138   if ( (pos[g.v(e)]==max_matching.C &&
139         pos[g.u(e)]==max_matching.D) ||
140         (pos[g.v(e)]==max_matching.D &&
141          pos[g.u(e)]==max_matching.C) )
142      noarc=false;
143  }
144  check ( noarc, "There are arcs between D and C!" );
145
146  bool oddcomp=true;
147  Graph::NodeMap<bool> todo(g,true);
148  int num_comp=0;
149  for(NodeIt v(g); v!=INVALID; ++v) {
150   if ( pos[v]==max_matching.D && todo[v] ) {
151      int comp_size=1;
152      ++num_comp;
153      std::queue<Node> Q;
154      Q.push(v);
155      todo.set(v,false);
156      while (!Q.empty()) {
157        Node w=Q.front();
158        Q.pop();
159        for(IncEdgeIt e(g,w); e!=INVALID; ++e) {
160          Node u=g.runningNode(e);
161          if ( pos[u]==max_matching.D && todo[u] ) {
162            ++comp_size;
163            Q.push(u);
164            todo.set(u,false);
165          }
166        }
167      }
168      if ( !(comp_size % 2) ) oddcomp=false;
169    }
170  }
171  check ( oddcomp, "A component of g[D] is not odd." );
172
173  int barrier=0;
174  for(NodeIt v(g); v!=INVALID; ++v) {
175    if ( pos[v]==max_matching.A ) ++barrier;
176  }
177  int expected_size=int( countNodes(g)-num_comp+barrier)/2;
178  check ( size==expected_size, "The size of the matching is wrong." );
179
180  return 0;
181}
Note: See TracBrowser for help on using the repository browser.