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