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@951
|
239 |
for (SmartGraph::EdgeIt e(graph); e != INVALID; ++e) {
|
deba@951
|
240 |
check((e == mfm.matching(graph.u(e)) ? 1 : 0) +
|
deba@951
|
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) +
|
deba@951
|
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));
|
deba@948
|
345 |
} else {
|
deba@948
|
346 |
check(mwfm.nodeValue(n) == 0, "Invalid matching");
|
deba@948
|
347 |
check(indeg == 0, "Invalid matching");
|
deba@948
|
348 |
}
|
deba@948
|
349 |
}
|
deba@948
|
350 |
|
deba@951
|
351 |
for (SmartGraph::EdgeIt e(graph); e != INVALID; ++e) {
|
deba@951
|
352 |
check((e == mwfm.matching(graph.u(e)) ? 1 : 0) +
|
deba@951
|
353 |
(e == mwfm.matching(graph.v(e)) ? 1 : 0) ==
|
deba@951
|
354 |
mwfm.matching(e), "Invalid matching");
|
deba@951
|
355 |
}
|
deba@951
|
356 |
|
deba@948
|
357 |
int dv = 0;
|
deba@948
|
358 |
for (SmartGraph::NodeIt n(graph); n != INVALID; ++n) {
|
deba@948
|
359 |
dv += mwfm.nodeValue(n);
|
deba@948
|
360 |
}
|
deba@948
|
361 |
|
deba@948
|
362 |
check(pv * mwfm.dualScale == dv * 2, "Wrong duality");
|
deba@948
|
363 |
|
deba@948
|
364 |
SmartGraph::NodeMap<bool> processed(graph, false);
|
deba@948
|
365 |
for (SmartGraph::NodeIt n(graph); n != INVALID; ++n) {
|
deba@948
|
366 |
if (processed[n]) continue;
|
deba@948
|
367 |
processed[n] = true;
|
deba@948
|
368 |
if (mwfm.matching(n) == INVALID) continue;
|
deba@948
|
369 |
int num = 1;
|
deba@948
|
370 |
Node v = graph.target(mwfm.matching(n));
|
deba@948
|
371 |
while (v != n) {
|
deba@948
|
372 |
processed[v] = true;
|
deba@948
|
373 |
++num;
|
deba@948
|
374 |
v = graph.target(mwfm.matching(v));
|
deba@948
|
375 |
}
|
deba@948
|
376 |
check(num == 2 || num % 2 == 1, "Wrong cycle size");
|
deba@948
|
377 |
check(allow_loops || num != 1, "Wrong cycle size");
|
deba@948
|
378 |
}
|
deba@948
|
379 |
|
deba@948
|
380 |
return;
|
deba@948
|
381 |
}
|
deba@948
|
382 |
|
deba@948
|
383 |
void checkWeightedPerfectFractionalMatching(const SmartGraph& graph,
|
deba@948
|
384 |
const SmartGraph::EdgeMap<int>& weight,
|
deba@948
|
385 |
const MaxWeightedPerfectFractionalMatching<SmartGraph>& mwpfm,
|
deba@948
|
386 |
bool allow_loops = true) {
|
deba@948
|
387 |
for (SmartGraph::EdgeIt e(graph); e != INVALID; ++e) {
|
deba@948
|
388 |
if (graph.u(e) == graph.v(e) && !allow_loops) continue;
|
deba@948
|
389 |
int rw = mwpfm.nodeValue(graph.u(e)) + mwpfm.nodeValue(graph.v(e))
|
deba@948
|
390 |
- weight[e] * mwpfm.dualScale;
|
deba@948
|
391 |
|
deba@948
|
392 |
check(rw >= 0, "Negative reduced weight");
|
deba@948
|
393 |
check(rw == 0 || !mwpfm.matching(e),
|
deba@948
|
394 |
"Non-zero reduced weight on matching edge");
|
deba@948
|
395 |
}
|
deba@948
|
396 |
|
deba@948
|
397 |
int pv = 0;
|
deba@948
|
398 |
for (SmartGraph::NodeIt n(graph); n != INVALID; ++n) {
|
deba@948
|
399 |
int indeg = 0;
|
deba@948
|
400 |
for (InArcIt a(graph, n); a != INVALID; ++a) {
|
deba@948
|
401 |
if (mwpfm.matching(graph.source(a)) == a) {
|
deba@948
|
402 |
++indeg;
|
deba@948
|
403 |
}
|
deba@948
|
404 |
}
|
deba@948
|
405 |
check(mwpfm.matching(n) != INVALID, "Invalid perfect matching");
|
deba@948
|
406 |
check(indeg == 1, "Invalid perfect matching");
|
deba@948
|
407 |
pv += weight[mwpfm.matching(n)];
|
deba@948
|
408 |
SmartGraph::Node o = graph.target(mwpfm.matching(n));
|
deba@948
|
409 |
}
|
deba@948
|
410 |
|
deba@951
|
411 |
for (SmartGraph::EdgeIt e(graph); e != INVALID; ++e) {
|
deba@951
|
412 |
check((e == mwpfm.matching(graph.u(e)) ? 1 : 0) +
|
deba@951
|
413 |
(e == mwpfm.matching(graph.v(e)) ? 1 : 0) ==
|
deba@951
|
414 |
mwpfm.matching(e), "Invalid matching");
|
deba@951
|
415 |
}
|
deba@951
|
416 |
|
deba@948
|
417 |
int dv = 0;
|
deba@948
|
418 |
for (SmartGraph::NodeIt n(graph); n != INVALID; ++n) {
|
deba@948
|
419 |
dv += mwpfm.nodeValue(n);
|
deba@948
|
420 |
}
|
deba@948
|
421 |
|
deba@948
|
422 |
check(pv * mwpfm.dualScale == dv * 2, "Wrong duality");
|
deba@948
|
423 |
|
deba@948
|
424 |
SmartGraph::NodeMap<bool> processed(graph, false);
|
deba@948
|
425 |
for (SmartGraph::NodeIt n(graph); n != INVALID; ++n) {
|
deba@948
|
426 |
if (processed[n]) continue;
|
deba@948
|
427 |
processed[n] = true;
|
deba@948
|
428 |
if (mwpfm.matching(n) == INVALID) continue;
|
deba@948
|
429 |
int num = 1;
|
deba@948
|
430 |
Node v = graph.target(mwpfm.matching(n));
|
deba@948
|
431 |
while (v != n) {
|
deba@948
|
432 |
processed[v] = true;
|
deba@948
|
433 |
++num;
|
deba@948
|
434 |
v = graph.target(mwpfm.matching(v));
|
deba@948
|
435 |
}
|
deba@948
|
436 |
check(num == 2 || num % 2 == 1, "Wrong cycle size");
|
deba@948
|
437 |
check(allow_loops || num != 1, "Wrong cycle size");
|
deba@948
|
438 |
}
|
deba@948
|
439 |
|
deba@948
|
440 |
return;
|
deba@948
|
441 |
}
|
deba@948
|
442 |
|
deba@948
|
443 |
|
deba@948
|
444 |
int main() {
|
deba@948
|
445 |
|
deba@948
|
446 |
for (int i = 0; i < lgfn; ++i) {
|
deba@948
|
447 |
SmartGraph graph;
|
deba@948
|
448 |
SmartGraph::EdgeMap<int> weight(graph);
|
deba@948
|
449 |
|
deba@948
|
450 |
istringstream lgfs(lgf[i]);
|
deba@948
|
451 |
graphReader(graph, lgfs).
|
deba@948
|
452 |
edgeMap("weight", weight).run();
|
deba@948
|
453 |
|
deba@948
|
454 |
bool perfect_with_loops;
|
deba@948
|
455 |
{
|
deba@948
|
456 |
MaxFractionalMatching<SmartGraph> mfm(graph, true);
|
deba@948
|
457 |
mfm.run();
|
deba@948
|
458 |
checkFractionalMatching(graph, mfm, true);
|
deba@948
|
459 |
perfect_with_loops = mfm.matchingSize() == countNodes(graph);
|
deba@948
|
460 |
}
|
deba@948
|
461 |
|
deba@948
|
462 |
bool perfect_without_loops;
|
deba@948
|
463 |
{
|
deba@948
|
464 |
MaxFractionalMatching<SmartGraph> mfm(graph, false);
|
deba@948
|
465 |
mfm.run();
|
deba@948
|
466 |
checkFractionalMatching(graph, mfm, false);
|
deba@948
|
467 |
perfect_without_loops = mfm.matchingSize() == countNodes(graph);
|
deba@948
|
468 |
}
|
deba@948
|
469 |
|
deba@948
|
470 |
{
|
deba@948
|
471 |
MaxFractionalMatching<SmartGraph> mfm(graph, true);
|
deba@948
|
472 |
bool result = mfm.runPerfect();
|
deba@948
|
473 |
checkPerfectFractionalMatching(graph, mfm, result, true);
|
deba@948
|
474 |
check(result == perfect_with_loops, "Wrong perfect matching");
|
deba@948
|
475 |
}
|
deba@948
|
476 |
|
deba@948
|
477 |
{
|
deba@948
|
478 |
MaxFractionalMatching<SmartGraph> mfm(graph, false);
|
deba@948
|
479 |
bool result = mfm.runPerfect();
|
deba@948
|
480 |
checkPerfectFractionalMatching(graph, mfm, result, false);
|
deba@948
|
481 |
check(result == perfect_without_loops, "Wrong perfect matching");
|
deba@948
|
482 |
}
|
deba@948
|
483 |
|
deba@948
|
484 |
{
|
deba@948
|
485 |
MaxWeightedFractionalMatching<SmartGraph> mwfm(graph, weight, true);
|
deba@948
|
486 |
mwfm.run();
|
deba@948
|
487 |
checkWeightedFractionalMatching(graph, weight, mwfm, true);
|
deba@948
|
488 |
}
|
deba@948
|
489 |
|
deba@948
|
490 |
{
|
deba@948
|
491 |
MaxWeightedFractionalMatching<SmartGraph> mwfm(graph, weight, false);
|
deba@948
|
492 |
mwfm.run();
|
deba@948
|
493 |
checkWeightedFractionalMatching(graph, weight, mwfm, false);
|
deba@948
|
494 |
}
|
deba@948
|
495 |
|
deba@948
|
496 |
{
|
deba@948
|
497 |
MaxWeightedPerfectFractionalMatching<SmartGraph> mwpfm(graph, weight,
|
deba@948
|
498 |
true);
|
deba@948
|
499 |
bool perfect = mwpfm.run();
|
deba@948
|
500 |
check(perfect == (mwpfm.matchingSize() == countNodes(graph)),
|
deba@948
|
501 |
"Perfect matching found");
|
deba@948
|
502 |
check(perfect == perfect_with_loops, "Wrong perfect matching");
|
deba@948
|
503 |
|
deba@948
|
504 |
if (perfect) {
|
deba@948
|
505 |
checkWeightedPerfectFractionalMatching(graph, weight, mwpfm, true);
|
deba@948
|
506 |
}
|
deba@948
|
507 |
}
|
deba@948
|
508 |
|
deba@948
|
509 |
{
|
deba@948
|
510 |
MaxWeightedPerfectFractionalMatching<SmartGraph> mwpfm(graph, weight,
|
deba@948
|
511 |
false);
|
deba@948
|
512 |
bool perfect = mwpfm.run();
|
deba@948
|
513 |
check(perfect == (mwpfm.matchingSize() == countNodes(graph)),
|
deba@948
|
514 |
"Perfect matching found");
|
deba@948
|
515 |
check(perfect == perfect_without_loops, "Wrong perfect matching");
|
deba@948
|
516 |
|
deba@948
|
517 |
if (perfect) {
|
deba@948
|
518 |
checkWeightedPerfectFractionalMatching(graph, weight, mwpfm, false);
|
deba@948
|
519 |
}
|
deba@948
|
520 |
}
|
deba@948
|
521 |
|
deba@948
|
522 |
}
|
deba@948
|
523 |
|
deba@948
|
524 |
return 0;
|
deba@948
|
525 |
}
|