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 |
*
|
alpar@956
|
5 |
* Copyright (C) 2003-2010
|
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@951
|
239 |
for (SmartGraph::EdgeIt e(graph); e != INVALID; ++e) {
|
deba@951
|
240 |
check((e == mfm.matching(graph.u(e)) ? 1 : 0) +
|
alpar@956
|
241 |
(e == mfm.matching(graph.v(e)) ? 1 : 0) ==
|
deba@951
|
242 |
mfm.matching(e), "Invalid matching");
|
deba@951
|
243 |
}
|
deba@951
|
244 |
|
deba@948
|
245 |
SmartGraph::NodeMap<bool> processed(graph, false);
|
deba@948
|
246 |
for (SmartGraph::NodeIt n(graph); n != INVALID; ++n) {
|
deba@948
|
247 |
if (processed[n]) continue;
|
deba@948
|
248 |
processed[n] = true;
|
deba@948
|
249 |
if (mfm.matching(n) == INVALID) continue;
|
deba@948
|
250 |
int num = 1;
|
deba@948
|
251 |
Node v = graph.target(mfm.matching(n));
|
deba@948
|
252 |
while (v != n) {
|
deba@948
|
253 |
processed[v] = true;
|
deba@948
|
254 |
++num;
|
deba@948
|
255 |
v = graph.target(mfm.matching(v));
|
deba@948
|
256 |
}
|
deba@948
|
257 |
check(num == 2 || num % 2 == 1, "Wrong cycle size");
|
deba@948
|
258 |
check(allow_loops || num != 1, "Wrong cycle size");
|
deba@948
|
259 |
}
|
deba@948
|
260 |
|
deba@948
|
261 |
int anum = 0, bnum = 0;
|
deba@948
|
262 |
SmartGraph::NodeMap<bool> neighbours(graph, false);
|
deba@948
|
263 |
for (SmartGraph::NodeIt n(graph); n != INVALID; ++n) {
|
deba@948
|
264 |
if (!mfm.barrier(n)) continue;
|
deba@948
|
265 |
++anum;
|
deba@948
|
266 |
for (SmartGraph::InArcIt a(graph, n); a != INVALID; ++a) {
|
deba@948
|
267 |
Node u = graph.source(a);
|
deba@948
|
268 |
if (!allow_loops && u == n) continue;
|
deba@948
|
269 |
if (!neighbours[u]) {
|
deba@948
|
270 |
neighbours[u] = true;
|
deba@948
|
271 |
++bnum;
|
deba@948
|
272 |
}
|
deba@948
|
273 |
}
|
deba@948
|
274 |
}
|
deba@948
|
275 |
check(anum - bnum + mfm.matchingSize() == countNodes(graph),
|
deba@948
|
276 |
"Wrong barrier");
|
deba@948
|
277 |
}
|
deba@948
|
278 |
|
deba@948
|
279 |
void checkPerfectFractionalMatching(const SmartGraph& graph,
|
deba@948
|
280 |
const MaxFractionalMatching<SmartGraph>& mfm,
|
deba@948
|
281 |
bool perfect, bool allow_loops = true) {
|
deba@948
|
282 |
if (perfect) {
|
deba@948
|
283 |
for (SmartGraph::NodeIt n(graph); n != INVALID; ++n) {
|
deba@948
|
284 |
int indeg = 0;
|
deba@948
|
285 |
for (InArcIt a(graph, n); a != INVALID; ++a) {
|
deba@948
|
286 |
if (mfm.matching(graph.source(a)) == a) {
|
deba@948
|
287 |
++indeg;
|
deba@948
|
288 |
}
|
deba@948
|
289 |
}
|
deba@948
|
290 |
check(mfm.matching(n) != INVALID, "Invalid matching");
|
deba@948
|
291 |
check(indeg == 1, "Invalid matching");
|
deba@948
|
292 |
}
|
deba@951
|
293 |
for (SmartGraph::EdgeIt e(graph); e != INVALID; ++e) {
|
deba@951
|
294 |
check((e == mfm.matching(graph.u(e)) ? 1 : 0) +
|
alpar@956
|
295 |
(e == mfm.matching(graph.v(e)) ? 1 : 0) ==
|
deba@951
|
296 |
mfm.matching(e), "Invalid matching");
|
deba@951
|
297 |
}
|
deba@948
|
298 |
} else {
|
deba@948
|
299 |
int anum = 0, bnum = 0;
|
deba@948
|
300 |
SmartGraph::NodeMap<bool> neighbours(graph, false);
|
deba@948
|
301 |
for (SmartGraph::NodeIt n(graph); n != INVALID; ++n) {
|
deba@948
|
302 |
if (!mfm.barrier(n)) continue;
|
deba@948
|
303 |
++anum;
|
deba@948
|
304 |
for (SmartGraph::InArcIt a(graph, n); a != INVALID; ++a) {
|
deba@948
|
305 |
Node u = graph.source(a);
|
deba@948
|
306 |
if (!allow_loops && u == n) continue;
|
deba@948
|
307 |
if (!neighbours[u]) {
|
deba@948
|
308 |
neighbours[u] = true;
|
deba@948
|
309 |
++bnum;
|
deba@948
|
310 |
}
|
deba@948
|
311 |
}
|
deba@948
|
312 |
}
|
deba@948
|
313 |
check(anum - bnum > 0, "Wrong barrier");
|
deba@948
|
314 |
}
|
deba@948
|
315 |
}
|
deba@948
|
316 |
|
deba@948
|
317 |
void checkWeightedFractionalMatching(const SmartGraph& graph,
|
deba@948
|
318 |
const SmartGraph::EdgeMap<int>& weight,
|
deba@948
|
319 |
const MaxWeightedFractionalMatching<SmartGraph>& mwfm,
|
deba@948
|
320 |
bool allow_loops = true) {
|
deba@948
|
321 |
for (SmartGraph::EdgeIt e(graph); e != INVALID; ++e) {
|
deba@948
|
322 |
if (graph.u(e) == graph.v(e) && !allow_loops) continue;
|
deba@948
|
323 |
int rw = mwfm.nodeValue(graph.u(e)) + mwfm.nodeValue(graph.v(e))
|
deba@948
|
324 |
- weight[e] * mwfm.dualScale;
|
deba@948
|
325 |
|
deba@948
|
326 |
check(rw >= 0, "Negative reduced weight");
|
deba@948
|
327 |
check(rw == 0 || !mwfm.matching(e),
|
deba@948
|
328 |
"Non-zero reduced weight on matching edge");
|
deba@948
|
329 |
}
|
deba@948
|
330 |
|
deba@948
|
331 |
int pv = 0;
|
deba@948
|
332 |
for (SmartGraph::NodeIt n(graph); n != INVALID; ++n) {
|
deba@948
|
333 |
int indeg = 0;
|
deba@948
|
334 |
for (InArcIt a(graph, n); a != INVALID; ++a) {
|
deba@948
|
335 |
if (mwfm.matching(graph.source(a)) == a) {
|
deba@948
|
336 |
++indeg;
|
deba@948
|
337 |
}
|
deba@948
|
338 |
}
|
deba@948
|
339 |
check(indeg <= 1, "Invalid matching");
|
deba@948
|
340 |
if (mwfm.matching(n) != INVALID) {
|
deba@948
|
341 |
check(mwfm.nodeValue(n) >= 0, "Invalid node value");
|
deba@948
|
342 |
check(indeg == 1, "Invalid matching");
|
deba@948
|
343 |
pv += weight[mwfm.matching(n)];
|
deba@948
|
344 |
SmartGraph::Node o = graph.target(mwfm.matching(n));
|
alpar@1160
|
345 |
ignore_unused_variable_warning(o);
|
deba@948
|
346 |
} else {
|
deba@948
|
347 |
check(mwfm.nodeValue(n) == 0, "Invalid matching");
|
deba@948
|
348 |
check(indeg == 0, "Invalid matching");
|
deba@948
|
349 |
}
|
deba@948
|
350 |
}
|
deba@948
|
351 |
|
deba@951
|
352 |
for (SmartGraph::EdgeIt e(graph); e != INVALID; ++e) {
|
deba@951
|
353 |
check((e == mwfm.matching(graph.u(e)) ? 1 : 0) +
|
alpar@956
|
354 |
(e == mwfm.matching(graph.v(e)) ? 1 : 0) ==
|
deba@951
|
355 |
mwfm.matching(e), "Invalid matching");
|
deba@951
|
356 |
}
|
deba@951
|
357 |
|
deba@948
|
358 |
int dv = 0;
|
deba@948
|
359 |
for (SmartGraph::NodeIt n(graph); n != INVALID; ++n) {
|
deba@948
|
360 |
dv += mwfm.nodeValue(n);
|
deba@948
|
361 |
}
|
deba@948
|
362 |
|
deba@948
|
363 |
check(pv * mwfm.dualScale == dv * 2, "Wrong duality");
|
deba@948
|
364 |
|
deba@948
|
365 |
SmartGraph::NodeMap<bool> processed(graph, false);
|
deba@948
|
366 |
for (SmartGraph::NodeIt n(graph); n != INVALID; ++n) {
|
deba@948
|
367 |
if (processed[n]) continue;
|
deba@948
|
368 |
processed[n] = true;
|
deba@948
|
369 |
if (mwfm.matching(n) == INVALID) continue;
|
deba@948
|
370 |
int num = 1;
|
deba@948
|
371 |
Node v = graph.target(mwfm.matching(n));
|
deba@948
|
372 |
while (v != n) {
|
deba@948
|
373 |
processed[v] = true;
|
deba@948
|
374 |
++num;
|
deba@948
|
375 |
v = graph.target(mwfm.matching(v));
|
deba@948
|
376 |
}
|
deba@948
|
377 |
check(num == 2 || num % 2 == 1, "Wrong cycle size");
|
deba@948
|
378 |
check(allow_loops || num != 1, "Wrong cycle size");
|
deba@948
|
379 |
}
|
deba@948
|
380 |
|
deba@948
|
381 |
return;
|
deba@948
|
382 |
}
|
deba@948
|
383 |
|
deba@948
|
384 |
void checkWeightedPerfectFractionalMatching(const SmartGraph& graph,
|
deba@948
|
385 |
const SmartGraph::EdgeMap<int>& weight,
|
deba@948
|
386 |
const MaxWeightedPerfectFractionalMatching<SmartGraph>& mwpfm,
|
deba@948
|
387 |
bool allow_loops = true) {
|
deba@948
|
388 |
for (SmartGraph::EdgeIt e(graph); e != INVALID; ++e) {
|
deba@948
|
389 |
if (graph.u(e) == graph.v(e) && !allow_loops) continue;
|
deba@948
|
390 |
int rw = mwpfm.nodeValue(graph.u(e)) + mwpfm.nodeValue(graph.v(e))
|
deba@948
|
391 |
- weight[e] * mwpfm.dualScale;
|
deba@948
|
392 |
|
deba@948
|
393 |
check(rw >= 0, "Negative reduced weight");
|
deba@948
|
394 |
check(rw == 0 || !mwpfm.matching(e),
|
deba@948
|
395 |
"Non-zero reduced weight on matching edge");
|
deba@948
|
396 |
}
|
deba@948
|
397 |
|
deba@948
|
398 |
int pv = 0;
|
deba@948
|
399 |
for (SmartGraph::NodeIt n(graph); n != INVALID; ++n) {
|
deba@948
|
400 |
int indeg = 0;
|
deba@948
|
401 |
for (InArcIt a(graph, n); a != INVALID; ++a) {
|
deba@948
|
402 |
if (mwpfm.matching(graph.source(a)) == a) {
|
deba@948
|
403 |
++indeg;
|
deba@948
|
404 |
}
|
deba@948
|
405 |
}
|
deba@948
|
406 |
check(mwpfm.matching(n) != INVALID, "Invalid perfect matching");
|
deba@948
|
407 |
check(indeg == 1, "Invalid perfect matching");
|
deba@948
|
408 |
pv += weight[mwpfm.matching(n)];
|
deba@948
|
409 |
SmartGraph::Node o = graph.target(mwpfm.matching(n));
|
alpar@1160
|
410 |
ignore_unused_variable_warning(o);
|
deba@948
|
411 |
}
|
deba@948
|
412 |
|
deba@951
|
413 |
for (SmartGraph::EdgeIt e(graph); e != INVALID; ++e) {
|
deba@951
|
414 |
check((e == mwpfm.matching(graph.u(e)) ? 1 : 0) +
|
alpar@956
|
415 |
(e == mwpfm.matching(graph.v(e)) ? 1 : 0) ==
|
deba@951
|
416 |
mwpfm.matching(e), "Invalid matching");
|
deba@951
|
417 |
}
|
deba@951
|
418 |
|
deba@948
|
419 |
int dv = 0;
|
deba@948
|
420 |
for (SmartGraph::NodeIt n(graph); n != INVALID; ++n) {
|
deba@948
|
421 |
dv += mwpfm.nodeValue(n);
|
deba@948
|
422 |
}
|
deba@948
|
423 |
|
deba@948
|
424 |
check(pv * mwpfm.dualScale == dv * 2, "Wrong duality");
|
deba@948
|
425 |
|
deba@948
|
426 |
SmartGraph::NodeMap<bool> processed(graph, false);
|
deba@948
|
427 |
for (SmartGraph::NodeIt n(graph); n != INVALID; ++n) {
|
deba@948
|
428 |
if (processed[n]) continue;
|
deba@948
|
429 |
processed[n] = true;
|
deba@948
|
430 |
if (mwpfm.matching(n) == INVALID) continue;
|
deba@948
|
431 |
int num = 1;
|
deba@948
|
432 |
Node v = graph.target(mwpfm.matching(n));
|
deba@948
|
433 |
while (v != n) {
|
deba@948
|
434 |
processed[v] = true;
|
deba@948
|
435 |
++num;
|
deba@948
|
436 |
v = graph.target(mwpfm.matching(v));
|
deba@948
|
437 |
}
|
deba@948
|
438 |
check(num == 2 || num % 2 == 1, "Wrong cycle size");
|
deba@948
|
439 |
check(allow_loops || num != 1, "Wrong cycle size");
|
deba@948
|
440 |
}
|
deba@948
|
441 |
|
deba@948
|
442 |
return;
|
deba@948
|
443 |
}
|
deba@948
|
444 |
|
deba@948
|
445 |
|
deba@948
|
446 |
int main() {
|
deba@948
|
447 |
|
deba@948
|
448 |
for (int i = 0; i < lgfn; ++i) {
|
deba@948
|
449 |
SmartGraph graph;
|
deba@948
|
450 |
SmartGraph::EdgeMap<int> weight(graph);
|
deba@948
|
451 |
|
deba@948
|
452 |
istringstream lgfs(lgf[i]);
|
deba@948
|
453 |
graphReader(graph, lgfs).
|
deba@948
|
454 |
edgeMap("weight", weight).run();
|
deba@948
|
455 |
|
deba@948
|
456 |
bool perfect_with_loops;
|
deba@948
|
457 |
{
|
deba@948
|
458 |
MaxFractionalMatching<SmartGraph> mfm(graph, true);
|
deba@948
|
459 |
mfm.run();
|
deba@948
|
460 |
checkFractionalMatching(graph, mfm, true);
|
deba@948
|
461 |
perfect_with_loops = mfm.matchingSize() == countNodes(graph);
|
deba@948
|
462 |
}
|
deba@948
|
463 |
|
deba@948
|
464 |
bool perfect_without_loops;
|
deba@948
|
465 |
{
|
deba@948
|
466 |
MaxFractionalMatching<SmartGraph> mfm(graph, false);
|
deba@948
|
467 |
mfm.run();
|
deba@948
|
468 |
checkFractionalMatching(graph, mfm, false);
|
deba@948
|
469 |
perfect_without_loops = mfm.matchingSize() == countNodes(graph);
|
deba@948
|
470 |
}
|
deba@948
|
471 |
|
deba@948
|
472 |
{
|
deba@948
|
473 |
MaxFractionalMatching<SmartGraph> mfm(graph, true);
|
deba@948
|
474 |
bool result = mfm.runPerfect();
|
deba@948
|
475 |
checkPerfectFractionalMatching(graph, mfm, result, true);
|
deba@948
|
476 |
check(result == perfect_with_loops, "Wrong perfect matching");
|
deba@948
|
477 |
}
|
deba@948
|
478 |
|
deba@948
|
479 |
{
|
deba@948
|
480 |
MaxFractionalMatching<SmartGraph> mfm(graph, false);
|
deba@948
|
481 |
bool result = mfm.runPerfect();
|
deba@948
|
482 |
checkPerfectFractionalMatching(graph, mfm, result, false);
|
deba@948
|
483 |
check(result == perfect_without_loops, "Wrong perfect matching");
|
deba@948
|
484 |
}
|
deba@948
|
485 |
|
deba@948
|
486 |
{
|
deba@948
|
487 |
MaxWeightedFractionalMatching<SmartGraph> mwfm(graph, weight, true);
|
deba@948
|
488 |
mwfm.run();
|
deba@948
|
489 |
checkWeightedFractionalMatching(graph, weight, mwfm, true);
|
deba@948
|
490 |
}
|
deba@948
|
491 |
|
deba@948
|
492 |
{
|
deba@948
|
493 |
MaxWeightedFractionalMatching<SmartGraph> mwfm(graph, weight, false);
|
deba@948
|
494 |
mwfm.run();
|
deba@948
|
495 |
checkWeightedFractionalMatching(graph, weight, mwfm, false);
|
deba@948
|
496 |
}
|
deba@948
|
497 |
|
deba@948
|
498 |
{
|
deba@948
|
499 |
MaxWeightedPerfectFractionalMatching<SmartGraph> mwpfm(graph, weight,
|
deba@948
|
500 |
true);
|
deba@948
|
501 |
bool perfect = mwpfm.run();
|
deba@948
|
502 |
check(perfect == (mwpfm.matchingSize() == countNodes(graph)),
|
deba@948
|
503 |
"Perfect matching found");
|
deba@948
|
504 |
check(perfect == perfect_with_loops, "Wrong perfect matching");
|
deba@948
|
505 |
|
deba@948
|
506 |
if (perfect) {
|
deba@948
|
507 |
checkWeightedPerfectFractionalMatching(graph, weight, mwpfm, true);
|
deba@948
|
508 |
}
|
deba@948
|
509 |
}
|
deba@948
|
510 |
|
deba@948
|
511 |
{
|
deba@948
|
512 |
MaxWeightedPerfectFractionalMatching<SmartGraph> mwpfm(graph, weight,
|
deba@948
|
513 |
false);
|
deba@948
|
514 |
bool perfect = mwpfm.run();
|
deba@948
|
515 |
check(perfect == (mwpfm.matchingSize() == countNodes(graph)),
|
deba@948
|
516 |
"Perfect matching found");
|
deba@948
|
517 |
check(perfect == perfect_without_loops, "Wrong perfect matching");
|
deba@948
|
518 |
|
deba@948
|
519 |
if (perfect) {
|
deba@948
|
520 |
checkWeightedPerfectFractionalMatching(graph, weight, mwpfm, false);
|
deba@948
|
521 |
}
|
deba@948
|
522 |
}
|
deba@948
|
523 |
|
deba@948
|
524 |
}
|
deba@948
|
525 |
|
deba@948
|
526 |
return 0;
|
deba@948
|
527 |
}
|