deba@948
|
1 |
/* -*- mode: C++; indent-tabs-mode: nil; -*-
|
deba@948
|
2 |
*
|
deba@948
|
3 |
* This file is a part of LEMON, a generic C++ optimization library.
|
deba@948
|
4 |
*
|
deba@948
|
5 |
* Copyright (C) 2003-2009
|
deba@948
|
6 |
* Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
|
deba@948
|
7 |
* (Egervary Research Group on Combinatorial Optimization, EGRES).
|
deba@948
|
8 |
*
|
deba@948
|
9 |
* Permission to use, modify and distribute this software is granted
|
deba@948
|
10 |
* provided that this copyright notice appears in all copies. For
|
deba@948
|
11 |
* precise terms see the accompanying LICENSE file.
|
deba@948
|
12 |
*
|
deba@948
|
13 |
* This software is provided "AS IS" with no warranty of any kind,
|
deba@948
|
14 |
* express or implied, and with no claim as to its suitability for any
|
deba@948
|
15 |
* purpose.
|
deba@948
|
16 |
*
|
deba@948
|
17 |
*/
|
deba@948
|
18 |
|
deba@948
|
19 |
#include <iostream>
|
deba@948
|
20 |
#include <sstream>
|
deba@948
|
21 |
#include <vector>
|
deba@948
|
22 |
#include <queue>
|
deba@948
|
23 |
#include <cstdlib>
|
deba@948
|
24 |
|
deba@948
|
25 |
#include <lemon/fractional_matching.h>
|
deba@948
|
26 |
#include <lemon/smart_graph.h>
|
deba@948
|
27 |
#include <lemon/concepts/graph.h>
|
deba@948
|
28 |
#include <lemon/concepts/maps.h>
|
deba@948
|
29 |
#include <lemon/lgf_reader.h>
|
deba@948
|
30 |
#include <lemon/math.h>
|
deba@948
|
31 |
|
deba@948
|
32 |
#include "test_tools.h"
|
deba@948
|
33 |
|
deba@948
|
34 |
using namespace std;
|
deba@948
|
35 |
using namespace lemon;
|
deba@948
|
36 |
|
deba@948
|
37 |
GRAPH_TYPEDEFS(SmartGraph);
|
deba@948
|
38 |
|
deba@948
|
39 |
|
deba@948
|
40 |
const int lgfn = 4;
|
deba@948
|
41 |
const std::string lgf[lgfn] = {
|
deba@948
|
42 |
"@nodes\n"
|
deba@948
|
43 |
"label\n"
|
deba@948
|
44 |
"0\n"
|
deba@948
|
45 |
"1\n"
|
deba@948
|
46 |
"2\n"
|
deba@948
|
47 |
"3\n"
|
deba@948
|
48 |
"4\n"
|
deba@948
|
49 |
"5\n"
|
deba@948
|
50 |
"6\n"
|
deba@948
|
51 |
"7\n"
|
deba@948
|
52 |
"@edges\n"
|
deba@948
|
53 |
" label weight\n"
|
deba@948
|
54 |
"7 4 0 984\n"
|
deba@948
|
55 |
"0 7 1 73\n"
|
deba@948
|
56 |
"7 1 2 204\n"
|
deba@948
|
57 |
"2 3 3 583\n"
|
deba@948
|
58 |
"2 7 4 565\n"
|
deba@948
|
59 |
"2 1 5 582\n"
|
deba@948
|
60 |
"0 4 6 551\n"
|
deba@948
|
61 |
"2 5 7 385\n"
|
deba@948
|
62 |
"1 5 8 561\n"
|
deba@948
|
63 |
"5 3 9 484\n"
|
deba@948
|
64 |
"7 5 10 904\n"
|
deba@948
|
65 |
"3 6 11 47\n"
|
deba@948
|
66 |
"7 6 12 888\n"
|
deba@948
|
67 |
"3 0 13 747\n"
|
deba@948
|
68 |
"6 1 14 310\n",
|
deba@948
|
69 |
|
deba@948
|
70 |
"@nodes\n"
|
deba@948
|
71 |
"label\n"
|
deba@948
|
72 |
"0\n"
|
deba@948
|
73 |
"1\n"
|
deba@948
|
74 |
"2\n"
|
deba@948
|
75 |
"3\n"
|
deba@948
|
76 |
"4\n"
|
deba@948
|
77 |
"5\n"
|
deba@948
|
78 |
"6\n"
|
deba@948
|
79 |
"7\n"
|
deba@948
|
80 |
"@edges\n"
|
deba@948
|
81 |
" label weight\n"
|
deba@948
|
82 |
"2 5 0 710\n"
|
deba@948
|
83 |
"0 5 1 241\n"
|
deba@948
|
84 |
"2 4 2 856\n"
|
deba@948
|
85 |
"2 6 3 762\n"
|
deba@948
|
86 |
"4 1 4 747\n"
|
deba@948
|
87 |
"6 1 5 962\n"
|
deba@948
|
88 |
"4 7 6 723\n"
|
deba@948
|
89 |
"1 7 7 661\n"
|
deba@948
|
90 |
"2 3 8 376\n"
|
deba@948
|
91 |
"1 0 9 416\n"
|
deba@948
|
92 |
"6 7 10 391\n",
|
deba@948
|
93 |
|
deba@948
|
94 |
"@nodes\n"
|
deba@948
|
95 |
"label\n"
|
deba@948
|
96 |
"0\n"
|
deba@948
|
97 |
"1\n"
|
deba@948
|
98 |
"2\n"
|
deba@948
|
99 |
"3\n"
|
deba@948
|
100 |
"4\n"
|
deba@948
|
101 |
"5\n"
|
deba@948
|
102 |
"6\n"
|
deba@948
|
103 |
"7\n"
|
deba@948
|
104 |
"@edges\n"
|
deba@948
|
105 |
" label weight\n"
|
deba@948
|
106 |
"6 2 0 553\n"
|
deba@948
|
107 |
"0 7 1 653\n"
|
deba@948
|
108 |
"6 3 2 22\n"
|
deba@948
|
109 |
"4 7 3 846\n"
|
deba@948
|
110 |
"7 2 4 981\n"
|
deba@948
|
111 |
"7 6 5 250\n"
|
deba@948
|
112 |
"5 2 6 539\n",
|
deba@948
|
113 |
|
deba@948
|
114 |
"@nodes\n"
|
deba@948
|
115 |
"label\n"
|
deba@948
|
116 |
"0\n"
|
deba@948
|
117 |
"@edges\n"
|
deba@948
|
118 |
" label weight\n"
|
deba@948
|
119 |
"0 0 0 100\n"
|
deba@948
|
120 |
};
|
deba@948
|
121 |
|
deba@948
|
122 |
void checkMaxFractionalMatchingCompile()
|
deba@948
|
123 |
{
|
deba@948
|
124 |
typedef concepts::Graph Graph;
|
deba@948
|
125 |
typedef Graph::Node Node;
|
deba@948
|
126 |
typedef Graph::Edge Edge;
|
deba@948
|
127 |
|
deba@948
|
128 |
Graph g;
|
deba@948
|
129 |
Node n;
|
deba@948
|
130 |
Edge e;
|
deba@948
|
131 |
|
deba@948
|
132 |
MaxFractionalMatching<Graph> mat_test(g);
|
deba@948
|
133 |
const MaxFractionalMatching<Graph>&
|
deba@948
|
134 |
const_mat_test = mat_test;
|
deba@948
|
135 |
|
deba@948
|
136 |
mat_test.init();
|
deba@948
|
137 |
mat_test.start();
|
deba@948
|
138 |
mat_test.start(true);
|
deba@948
|
139 |
mat_test.startPerfect();
|
deba@948
|
140 |
mat_test.startPerfect(true);
|
deba@948
|
141 |
mat_test.run();
|
deba@948
|
142 |
mat_test.run(true);
|
deba@948
|
143 |
mat_test.runPerfect();
|
deba@948
|
144 |
mat_test.runPerfect(true);
|
deba@948
|
145 |
|
deba@948
|
146 |
const_mat_test.matchingSize();
|
deba@948
|
147 |
const_mat_test.matching(e);
|
deba@948
|
148 |
const_mat_test.matching(n);
|
deba@948
|
149 |
const MaxFractionalMatching<Graph>::MatchingMap& mmap =
|
deba@948
|
150 |
const_mat_test.matchingMap();
|
deba@948
|
151 |
e = mmap[n];
|
deba@948
|
152 |
|
deba@948
|
153 |
const_mat_test.barrier(n);
|
deba@948
|
154 |
}
|
deba@948
|
155 |
|
deba@948
|
156 |
void checkMaxWeightedFractionalMatchingCompile()
|
deba@948
|
157 |
{
|
deba@948
|
158 |
typedef concepts::Graph Graph;
|
deba@948
|
159 |
typedef Graph::Node Node;
|
deba@948
|
160 |
typedef Graph::Edge Edge;
|
deba@948
|
161 |
typedef Graph::EdgeMap<int> WeightMap;
|
deba@948
|
162 |
|
deba@948
|
163 |
Graph g;
|
deba@948
|
164 |
Node n;
|
deba@948
|
165 |
Edge e;
|
deba@948
|
166 |
WeightMap w(g);
|
deba@948
|
167 |
|
deba@948
|
168 |
MaxWeightedFractionalMatching<Graph> mat_test(g, w);
|
deba@948
|
169 |
const MaxWeightedFractionalMatching<Graph>&
|
deba@948
|
170 |
const_mat_test = mat_test;
|
deba@948
|
171 |
|
deba@948
|
172 |
mat_test.init();
|
deba@948
|
173 |
mat_test.start();
|
deba@948
|
174 |
mat_test.run();
|
deba@948
|
175 |
|
deba@948
|
176 |
const_mat_test.matchingWeight();
|
deba@948
|
177 |
const_mat_test.matchingSize();
|
deba@948
|
178 |
const_mat_test.matching(e);
|
deba@948
|
179 |
const_mat_test.matching(n);
|
deba@948
|
180 |
const MaxWeightedFractionalMatching<Graph>::MatchingMap& mmap =
|
deba@948
|
181 |
const_mat_test.matchingMap();
|
deba@948
|
182 |
e = mmap[n];
|
deba@948
|
183 |
|
deba@948
|
184 |
const_mat_test.dualValue();
|
deba@948
|
185 |
const_mat_test.nodeValue(n);
|
deba@948
|
186 |
}
|
deba@948
|
187 |
|
deba@948
|
188 |
void checkMaxWeightedPerfectFractionalMatchingCompile()
|
deba@948
|
189 |
{
|
deba@948
|
190 |
typedef concepts::Graph Graph;
|
deba@948
|
191 |
typedef Graph::Node Node;
|
deba@948
|
192 |
typedef Graph::Edge Edge;
|
deba@948
|
193 |
typedef Graph::EdgeMap<int> WeightMap;
|
deba@948
|
194 |
|
deba@948
|
195 |
Graph g;
|
deba@948
|
196 |
Node n;
|
deba@948
|
197 |
Edge e;
|
deba@948
|
198 |
WeightMap w(g);
|
deba@948
|
199 |
|
deba@948
|
200 |
MaxWeightedPerfectFractionalMatching<Graph> mat_test(g, w);
|
deba@948
|
201 |
const MaxWeightedPerfectFractionalMatching<Graph>&
|
deba@948
|
202 |
const_mat_test = mat_test;
|
deba@948
|
203 |
|
deba@948
|
204 |
mat_test.init();
|
deba@948
|
205 |
mat_test.start();
|
deba@948
|
206 |
mat_test.run();
|
deba@948
|
207 |
|
deba@948
|
208 |
const_mat_test.matchingWeight();
|
deba@948
|
209 |
const_mat_test.matching(e);
|
deba@948
|
210 |
const_mat_test.matching(n);
|
deba@948
|
211 |
const MaxWeightedPerfectFractionalMatching<Graph>::MatchingMap& mmap =
|
deba@948
|
212 |
const_mat_test.matchingMap();
|
deba@948
|
213 |
e = mmap[n];
|
deba@948
|
214 |
|
deba@948
|
215 |
const_mat_test.dualValue();
|
deba@948
|
216 |
const_mat_test.nodeValue(n);
|
deba@948
|
217 |
}
|
deba@948
|
218 |
|
deba@948
|
219 |
void checkFractionalMatching(const SmartGraph& graph,
|
deba@948
|
220 |
const MaxFractionalMatching<SmartGraph>& mfm,
|
deba@948
|
221 |
bool allow_loops = true) {
|
deba@948
|
222 |
int pv = 0;
|
deba@948
|
223 |
for (SmartGraph::NodeIt n(graph); n != INVALID; ++n) {
|
deba@948
|
224 |
int indeg = 0;
|
deba@948
|
225 |
for (InArcIt a(graph, n); a != INVALID; ++a) {
|
deba@948
|
226 |
if (mfm.matching(graph.source(a)) == a) {
|
deba@948
|
227 |
++indeg;
|
deba@948
|
228 |
}
|
deba@948
|
229 |
}
|
deba@948
|
230 |
if (mfm.matching(n) != INVALID) {
|
deba@948
|
231 |
check(indeg == 1, "Invalid matching");
|
deba@948
|
232 |
++pv;
|
deba@948
|
233 |
} else {
|
deba@948
|
234 |
check(indeg == 0, "Invalid matching");
|
deba@948
|
235 |
}
|
deba@948
|
236 |
}
|
deba@948
|
237 |
check(pv == mfm.matchingSize(), "Wrong matching size");
|
deba@948
|
238 |
|
deba@948
|
239 |
SmartGraph::NodeMap<bool> processed(graph, false);
|
deba@948
|
240 |
for (SmartGraph::NodeIt n(graph); n != INVALID; ++n) {
|
deba@948
|
241 |
if (processed[n]) continue;
|
deba@948
|
242 |
processed[n] = true;
|
deba@948
|
243 |
if (mfm.matching(n) == INVALID) continue;
|
deba@948
|
244 |
int num = 1;
|
deba@948
|
245 |
Node v = graph.target(mfm.matching(n));
|
deba@948
|
246 |
while (v != n) {
|
deba@948
|
247 |
processed[v] = true;
|
deba@948
|
248 |
++num;
|
deba@948
|
249 |
v = graph.target(mfm.matching(v));
|
deba@948
|
250 |
}
|
deba@948
|
251 |
check(num == 2 || num % 2 == 1, "Wrong cycle size");
|
deba@948
|
252 |
check(allow_loops || num != 1, "Wrong cycle size");
|
deba@948
|
253 |
}
|
deba@948
|
254 |
|
deba@948
|
255 |
int anum = 0, bnum = 0;
|
deba@948
|
256 |
SmartGraph::NodeMap<bool> neighbours(graph, false);
|
deba@948
|
257 |
for (SmartGraph::NodeIt n(graph); n != INVALID; ++n) {
|
deba@948
|
258 |
if (!mfm.barrier(n)) continue;
|
deba@948
|
259 |
++anum;
|
deba@948
|
260 |
for (SmartGraph::InArcIt a(graph, n); a != INVALID; ++a) {
|
deba@948
|
261 |
Node u = graph.source(a);
|
deba@948
|
262 |
if (!allow_loops && u == n) continue;
|
deba@948
|
263 |
if (!neighbours[u]) {
|
deba@948
|
264 |
neighbours[u] = true;
|
deba@948
|
265 |
++bnum;
|
deba@948
|
266 |
}
|
deba@948
|
267 |
}
|
deba@948
|
268 |
}
|
deba@948
|
269 |
check(anum - bnum + mfm.matchingSize() == countNodes(graph),
|
deba@948
|
270 |
"Wrong barrier");
|
deba@948
|
271 |
}
|
deba@948
|
272 |
|
deba@948
|
273 |
void checkPerfectFractionalMatching(const SmartGraph& graph,
|
deba@948
|
274 |
const MaxFractionalMatching<SmartGraph>& mfm,
|
deba@948
|
275 |
bool perfect, bool allow_loops = true) {
|
deba@948
|
276 |
if (perfect) {
|
deba@948
|
277 |
for (SmartGraph::NodeIt n(graph); n != INVALID; ++n) {
|
deba@948
|
278 |
int indeg = 0;
|
deba@948
|
279 |
for (InArcIt a(graph, n); a != INVALID; ++a) {
|
deba@948
|
280 |
if (mfm.matching(graph.source(a)) == a) {
|
deba@948
|
281 |
++indeg;
|
deba@948
|
282 |
}
|
deba@948
|
283 |
}
|
deba@948
|
284 |
check(mfm.matching(n) != INVALID, "Invalid matching");
|
deba@948
|
285 |
check(indeg == 1, "Invalid matching");
|
deba@948
|
286 |
}
|
deba@948
|
287 |
} else {
|
deba@948
|
288 |
int anum = 0, bnum = 0;
|
deba@948
|
289 |
SmartGraph::NodeMap<bool> neighbours(graph, false);
|
deba@948
|
290 |
for (SmartGraph::NodeIt n(graph); n != INVALID; ++n) {
|
deba@948
|
291 |
if (!mfm.barrier(n)) continue;
|
deba@948
|
292 |
++anum;
|
deba@948
|
293 |
for (SmartGraph::InArcIt a(graph, n); a != INVALID; ++a) {
|
deba@948
|
294 |
Node u = graph.source(a);
|
deba@948
|
295 |
if (!allow_loops && u == n) continue;
|
deba@948
|
296 |
if (!neighbours[u]) {
|
deba@948
|
297 |
neighbours[u] = true;
|
deba@948
|
298 |
++bnum;
|
deba@948
|
299 |
}
|
deba@948
|
300 |
}
|
deba@948
|
301 |
}
|
deba@948
|
302 |
check(anum - bnum > 0, "Wrong barrier");
|
deba@948
|
303 |
}
|
deba@948
|
304 |
}
|
deba@948
|
305 |
|
deba@948
|
306 |
void checkWeightedFractionalMatching(const SmartGraph& graph,
|
deba@948
|
307 |
const SmartGraph::EdgeMap<int>& weight,
|
deba@948
|
308 |
const MaxWeightedFractionalMatching<SmartGraph>& mwfm,
|
deba@948
|
309 |
bool allow_loops = true) {
|
deba@948
|
310 |
for (SmartGraph::EdgeIt e(graph); e != INVALID; ++e) {
|
deba@948
|
311 |
if (graph.u(e) == graph.v(e) && !allow_loops) continue;
|
deba@948
|
312 |
int rw = mwfm.nodeValue(graph.u(e)) + mwfm.nodeValue(graph.v(e))
|
deba@948
|
313 |
- weight[e] * mwfm.dualScale;
|
deba@948
|
314 |
|
deba@948
|
315 |
check(rw >= 0, "Negative reduced weight");
|
deba@948
|
316 |
check(rw == 0 || !mwfm.matching(e),
|
deba@948
|
317 |
"Non-zero reduced weight on matching edge");
|
deba@948
|
318 |
}
|
deba@948
|
319 |
|
deba@948
|
320 |
int pv = 0;
|
deba@948
|
321 |
for (SmartGraph::NodeIt n(graph); n != INVALID; ++n) {
|
deba@948
|
322 |
int indeg = 0;
|
deba@948
|
323 |
for (InArcIt a(graph, n); a != INVALID; ++a) {
|
deba@948
|
324 |
if (mwfm.matching(graph.source(a)) == a) {
|
deba@948
|
325 |
++indeg;
|
deba@948
|
326 |
}
|
deba@948
|
327 |
}
|
deba@948
|
328 |
check(indeg <= 1, "Invalid matching");
|
deba@948
|
329 |
if (mwfm.matching(n) != INVALID) {
|
deba@948
|
330 |
check(mwfm.nodeValue(n) >= 0, "Invalid node value");
|
deba@948
|
331 |
check(indeg == 1, "Invalid matching");
|
deba@948
|
332 |
pv += weight[mwfm.matching(n)];
|
deba@948
|
333 |
SmartGraph::Node o = graph.target(mwfm.matching(n));
|
deba@948
|
334 |
} else {
|
deba@948
|
335 |
check(mwfm.nodeValue(n) == 0, "Invalid matching");
|
deba@948
|
336 |
check(indeg == 0, "Invalid matching");
|
deba@948
|
337 |
}
|
deba@948
|
338 |
}
|
deba@948
|
339 |
|
deba@948
|
340 |
int dv = 0;
|
deba@948
|
341 |
for (SmartGraph::NodeIt n(graph); n != INVALID; ++n) {
|
deba@948
|
342 |
dv += mwfm.nodeValue(n);
|
deba@948
|
343 |
}
|
deba@948
|
344 |
|
deba@948
|
345 |
check(pv * mwfm.dualScale == dv * 2, "Wrong duality");
|
deba@948
|
346 |
|
deba@948
|
347 |
SmartGraph::NodeMap<bool> processed(graph, false);
|
deba@948
|
348 |
for (SmartGraph::NodeIt n(graph); n != INVALID; ++n) {
|
deba@948
|
349 |
if (processed[n]) continue;
|
deba@948
|
350 |
processed[n] = true;
|
deba@948
|
351 |
if (mwfm.matching(n) == INVALID) continue;
|
deba@948
|
352 |
int num = 1;
|
deba@948
|
353 |
Node v = graph.target(mwfm.matching(n));
|
deba@948
|
354 |
while (v != n) {
|
deba@948
|
355 |
processed[v] = true;
|
deba@948
|
356 |
++num;
|
deba@948
|
357 |
v = graph.target(mwfm.matching(v));
|
deba@948
|
358 |
}
|
deba@948
|
359 |
check(num == 2 || num % 2 == 1, "Wrong cycle size");
|
deba@948
|
360 |
check(allow_loops || num != 1, "Wrong cycle size");
|
deba@948
|
361 |
}
|
deba@948
|
362 |
|
deba@948
|
363 |
return;
|
deba@948
|
364 |
}
|
deba@948
|
365 |
|
deba@948
|
366 |
void checkWeightedPerfectFractionalMatching(const SmartGraph& graph,
|
deba@948
|
367 |
const SmartGraph::EdgeMap<int>& weight,
|
deba@948
|
368 |
const MaxWeightedPerfectFractionalMatching<SmartGraph>& mwpfm,
|
deba@948
|
369 |
bool allow_loops = true) {
|
deba@948
|
370 |
for (SmartGraph::EdgeIt e(graph); e != INVALID; ++e) {
|
deba@948
|
371 |
if (graph.u(e) == graph.v(e) && !allow_loops) continue;
|
deba@948
|
372 |
int rw = mwpfm.nodeValue(graph.u(e)) + mwpfm.nodeValue(graph.v(e))
|
deba@948
|
373 |
- weight[e] * mwpfm.dualScale;
|
deba@948
|
374 |
|
deba@948
|
375 |
check(rw >= 0, "Negative reduced weight");
|
deba@948
|
376 |
check(rw == 0 || !mwpfm.matching(e),
|
deba@948
|
377 |
"Non-zero reduced weight on matching edge");
|
deba@948
|
378 |
}
|
deba@948
|
379 |
|
deba@948
|
380 |
int pv = 0;
|
deba@948
|
381 |
for (SmartGraph::NodeIt n(graph); n != INVALID; ++n) {
|
deba@948
|
382 |
int indeg = 0;
|
deba@948
|
383 |
for (InArcIt a(graph, n); a != INVALID; ++a) {
|
deba@948
|
384 |
if (mwpfm.matching(graph.source(a)) == a) {
|
deba@948
|
385 |
++indeg;
|
deba@948
|
386 |
}
|
deba@948
|
387 |
}
|
deba@948
|
388 |
check(mwpfm.matching(n) != INVALID, "Invalid perfect matching");
|
deba@948
|
389 |
check(indeg == 1, "Invalid perfect matching");
|
deba@948
|
390 |
pv += weight[mwpfm.matching(n)];
|
deba@948
|
391 |
SmartGraph::Node o = graph.target(mwpfm.matching(n));
|
deba@948
|
392 |
}
|
deba@948
|
393 |
|
deba@948
|
394 |
int dv = 0;
|
deba@948
|
395 |
for (SmartGraph::NodeIt n(graph); n != INVALID; ++n) {
|
deba@948
|
396 |
dv += mwpfm.nodeValue(n);
|
deba@948
|
397 |
}
|
deba@948
|
398 |
|
deba@948
|
399 |
check(pv * mwpfm.dualScale == dv * 2, "Wrong duality");
|
deba@948
|
400 |
|
deba@948
|
401 |
SmartGraph::NodeMap<bool> processed(graph, false);
|
deba@948
|
402 |
for (SmartGraph::NodeIt n(graph); n != INVALID; ++n) {
|
deba@948
|
403 |
if (processed[n]) continue;
|
deba@948
|
404 |
processed[n] = true;
|
deba@948
|
405 |
if (mwpfm.matching(n) == INVALID) continue;
|
deba@948
|
406 |
int num = 1;
|
deba@948
|
407 |
Node v = graph.target(mwpfm.matching(n));
|
deba@948
|
408 |
while (v != n) {
|
deba@948
|
409 |
processed[v] = true;
|
deba@948
|
410 |
++num;
|
deba@948
|
411 |
v = graph.target(mwpfm.matching(v));
|
deba@948
|
412 |
}
|
deba@948
|
413 |
check(num == 2 || num % 2 == 1, "Wrong cycle size");
|
deba@948
|
414 |
check(allow_loops || num != 1, "Wrong cycle size");
|
deba@948
|
415 |
}
|
deba@948
|
416 |
|
deba@948
|
417 |
return;
|
deba@948
|
418 |
}
|
deba@948
|
419 |
|
deba@948
|
420 |
|
deba@948
|
421 |
int main() {
|
deba@948
|
422 |
|
deba@948
|
423 |
for (int i = 0; i < lgfn; ++i) {
|
deba@948
|
424 |
SmartGraph graph;
|
deba@948
|
425 |
SmartGraph::EdgeMap<int> weight(graph);
|
deba@948
|
426 |
|
deba@948
|
427 |
istringstream lgfs(lgf[i]);
|
deba@948
|
428 |
graphReader(graph, lgfs).
|
deba@948
|
429 |
edgeMap("weight", weight).run();
|
deba@948
|
430 |
|
deba@948
|
431 |
bool perfect_with_loops;
|
deba@948
|
432 |
{
|
deba@948
|
433 |
MaxFractionalMatching<SmartGraph> mfm(graph, true);
|
deba@948
|
434 |
mfm.run();
|
deba@948
|
435 |
checkFractionalMatching(graph, mfm, true);
|
deba@948
|
436 |
perfect_with_loops = mfm.matchingSize() == countNodes(graph);
|
deba@948
|
437 |
}
|
deba@948
|
438 |
|
deba@948
|
439 |
bool perfect_without_loops;
|
deba@948
|
440 |
{
|
deba@948
|
441 |
MaxFractionalMatching<SmartGraph> mfm(graph, false);
|
deba@948
|
442 |
mfm.run();
|
deba@948
|
443 |
checkFractionalMatching(graph, mfm, false);
|
deba@948
|
444 |
perfect_without_loops = mfm.matchingSize() == countNodes(graph);
|
deba@948
|
445 |
}
|
deba@948
|
446 |
|
deba@948
|
447 |
{
|
deba@948
|
448 |
MaxFractionalMatching<SmartGraph> mfm(graph, true);
|
deba@948
|
449 |
bool result = mfm.runPerfect();
|
deba@948
|
450 |
checkPerfectFractionalMatching(graph, mfm, result, true);
|
deba@948
|
451 |
check(result == perfect_with_loops, "Wrong perfect matching");
|
deba@948
|
452 |
}
|
deba@948
|
453 |
|
deba@948
|
454 |
{
|
deba@948
|
455 |
MaxFractionalMatching<SmartGraph> mfm(graph, false);
|
deba@948
|
456 |
bool result = mfm.runPerfect();
|
deba@948
|
457 |
checkPerfectFractionalMatching(graph, mfm, result, false);
|
deba@948
|
458 |
check(result == perfect_without_loops, "Wrong perfect matching");
|
deba@948
|
459 |
}
|
deba@948
|
460 |
|
deba@948
|
461 |
{
|
deba@948
|
462 |
MaxWeightedFractionalMatching<SmartGraph> mwfm(graph, weight, true);
|
deba@948
|
463 |
mwfm.run();
|
deba@948
|
464 |
checkWeightedFractionalMatching(graph, weight, mwfm, true);
|
deba@948
|
465 |
}
|
deba@948
|
466 |
|
deba@948
|
467 |
{
|
deba@948
|
468 |
MaxWeightedFractionalMatching<SmartGraph> mwfm(graph, weight, false);
|
deba@948
|
469 |
mwfm.run();
|
deba@948
|
470 |
checkWeightedFractionalMatching(graph, weight, mwfm, false);
|
deba@948
|
471 |
}
|
deba@948
|
472 |
|
deba@948
|
473 |
{
|
deba@948
|
474 |
MaxWeightedPerfectFractionalMatching<SmartGraph> mwpfm(graph, weight,
|
deba@948
|
475 |
true);
|
deba@948
|
476 |
bool perfect = mwpfm.run();
|
deba@948
|
477 |
check(perfect == (mwpfm.matchingSize() == countNodes(graph)),
|
deba@948
|
478 |
"Perfect matching found");
|
deba@948
|
479 |
check(perfect == perfect_with_loops, "Wrong perfect matching");
|
deba@948
|
480 |
|
deba@948
|
481 |
if (perfect) {
|
deba@948
|
482 |
checkWeightedPerfectFractionalMatching(graph, weight, mwpfm, true);
|
deba@948
|
483 |
}
|
deba@948
|
484 |
}
|
deba@948
|
485 |
|
deba@948
|
486 |
{
|
deba@948
|
487 |
MaxWeightedPerfectFractionalMatching<SmartGraph> mwpfm(graph, weight,
|
deba@948
|
488 |
false);
|
deba@948
|
489 |
bool perfect = mwpfm.run();
|
deba@948
|
490 |
check(perfect == (mwpfm.matchingSize() == countNodes(graph)),
|
deba@948
|
491 |
"Perfect matching found");
|
deba@948
|
492 |
check(perfect == perfect_without_loops, "Wrong perfect matching");
|
deba@948
|
493 |
|
deba@948
|
494 |
if (perfect) {
|
deba@948
|
495 |
checkWeightedPerfectFractionalMatching(graph, weight, mwpfm, false);
|
deba@948
|
496 |
}
|
deba@948
|
497 |
}
|
deba@948
|
498 |
|
deba@948
|
499 |
}
|
deba@948
|
500 |
|
deba@948
|
501 |
return 0;
|
deba@948
|
502 |
}
|