1 /* -*- mode: C++; indent-tabs-mode: nil; -*-
3 * This file is a part of LEMON, a generic C++ optimization library.
5 * Copyright (C) 2003-2009
6 * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
7 * (Egervary Research Group on Combinatorial Optimization, EGRES).
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.
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
25 #include <lemon/fractional_matching.h>
26 #include <lemon/smart_graph.h>
27 #include <lemon/concepts/graph.h>
28 #include <lemon/concepts/maps.h>
29 #include <lemon/lgf_reader.h>
30 #include <lemon/math.h>
32 #include "test_tools.h"
35 using namespace lemon;
37 GRAPH_TYPEDEFS(SmartGraph);
41 const std::string lgf[lgfn] = {
122 void checkMaxFractionalMatchingCompile()
124 typedef concepts::Graph Graph;
125 typedef Graph::Node Node;
126 typedef Graph::Edge Edge;
132 MaxFractionalMatching<Graph> mat_test(g);
133 const MaxFractionalMatching<Graph>&
134 const_mat_test = mat_test;
138 mat_test.start(true);
139 mat_test.startPerfect();
140 mat_test.startPerfect(true);
143 mat_test.runPerfect();
144 mat_test.runPerfect(true);
146 const_mat_test.matchingSize();
147 const_mat_test.matching(e);
148 const_mat_test.matching(n);
149 const MaxFractionalMatching<Graph>::MatchingMap& mmap =
150 const_mat_test.matchingMap();
153 const_mat_test.barrier(n);
156 void checkMaxWeightedFractionalMatchingCompile()
158 typedef concepts::Graph Graph;
159 typedef Graph::Node Node;
160 typedef Graph::Edge Edge;
161 typedef Graph::EdgeMap<int> WeightMap;
168 MaxWeightedFractionalMatching<Graph> mat_test(g, w);
169 const MaxWeightedFractionalMatching<Graph>&
170 const_mat_test = mat_test;
176 const_mat_test.matchingWeight();
177 const_mat_test.matchingSize();
178 const_mat_test.matching(e);
179 const_mat_test.matching(n);
180 const MaxWeightedFractionalMatching<Graph>::MatchingMap& mmap =
181 const_mat_test.matchingMap();
184 const_mat_test.dualValue();
185 const_mat_test.nodeValue(n);
188 void checkMaxWeightedPerfectFractionalMatchingCompile()
190 typedef concepts::Graph Graph;
191 typedef Graph::Node Node;
192 typedef Graph::Edge Edge;
193 typedef Graph::EdgeMap<int> WeightMap;
200 MaxWeightedPerfectFractionalMatching<Graph> mat_test(g, w);
201 const MaxWeightedPerfectFractionalMatching<Graph>&
202 const_mat_test = mat_test;
208 const_mat_test.matchingWeight();
209 const_mat_test.matching(e);
210 const_mat_test.matching(n);
211 const MaxWeightedPerfectFractionalMatching<Graph>::MatchingMap& mmap =
212 const_mat_test.matchingMap();
215 const_mat_test.dualValue();
216 const_mat_test.nodeValue(n);
219 void checkFractionalMatching(const SmartGraph& graph,
220 const MaxFractionalMatching<SmartGraph>& mfm,
221 bool allow_loops = true) {
223 for (SmartGraph::NodeIt n(graph); n != INVALID; ++n) {
225 for (InArcIt a(graph, n); a != INVALID; ++a) {
226 if (mfm.matching(graph.source(a)) == a) {
230 if (mfm.matching(n) != INVALID) {
231 check(indeg == 1, "Invalid matching");
234 check(indeg == 0, "Invalid matching");
237 check(pv == mfm.matchingSize(), "Wrong matching size");
239 for (SmartGraph::EdgeIt e(graph); e != INVALID; ++e) {
240 check((e == mfm.matching(graph.u(e)) ? 1 : 0) +
241 (e == mfm.matching(graph.v(e)) ? 1 : 0) ==
242 mfm.matching(e), "Invalid matching");
245 SmartGraph::NodeMap<bool> processed(graph, false);
246 for (SmartGraph::NodeIt n(graph); n != INVALID; ++n) {
247 if (processed[n]) continue;
249 if (mfm.matching(n) == INVALID) continue;
251 Node v = graph.target(mfm.matching(n));
255 v = graph.target(mfm.matching(v));
257 check(num == 2 || num % 2 == 1, "Wrong cycle size");
258 check(allow_loops || num != 1, "Wrong cycle size");
261 int anum = 0, bnum = 0;
262 SmartGraph::NodeMap<bool> neighbours(graph, false);
263 for (SmartGraph::NodeIt n(graph); n != INVALID; ++n) {
264 if (!mfm.barrier(n)) continue;
266 for (SmartGraph::InArcIt a(graph, n); a != INVALID; ++a) {
267 Node u = graph.source(a);
268 if (!allow_loops && u == n) continue;
269 if (!neighbours[u]) {
270 neighbours[u] = true;
275 check(anum - bnum + mfm.matchingSize() == countNodes(graph),
279 void checkPerfectFractionalMatching(const SmartGraph& graph,
280 const MaxFractionalMatching<SmartGraph>& mfm,
281 bool perfect, bool allow_loops = true) {
283 for (SmartGraph::NodeIt n(graph); n != INVALID; ++n) {
285 for (InArcIt a(graph, n); a != INVALID; ++a) {
286 if (mfm.matching(graph.source(a)) == a) {
290 check(mfm.matching(n) != INVALID, "Invalid matching");
291 check(indeg == 1, "Invalid matching");
293 for (SmartGraph::EdgeIt e(graph); e != INVALID; ++e) {
294 check((e == mfm.matching(graph.u(e)) ? 1 : 0) +
295 (e == mfm.matching(graph.v(e)) ? 1 : 0) ==
296 mfm.matching(e), "Invalid matching");
299 int anum = 0, bnum = 0;
300 SmartGraph::NodeMap<bool> neighbours(graph, false);
301 for (SmartGraph::NodeIt n(graph); n != INVALID; ++n) {
302 if (!mfm.barrier(n)) continue;
304 for (SmartGraph::InArcIt a(graph, n); a != INVALID; ++a) {
305 Node u = graph.source(a);
306 if (!allow_loops && u == n) continue;
307 if (!neighbours[u]) {
308 neighbours[u] = true;
313 check(anum - bnum > 0, "Wrong barrier");
317 void checkWeightedFractionalMatching(const SmartGraph& graph,
318 const SmartGraph::EdgeMap<int>& weight,
319 const MaxWeightedFractionalMatching<SmartGraph>& mwfm,
320 bool allow_loops = true) {
321 for (SmartGraph::EdgeIt e(graph); e != INVALID; ++e) {
322 if (graph.u(e) == graph.v(e) && !allow_loops) continue;
323 int rw = mwfm.nodeValue(graph.u(e)) + mwfm.nodeValue(graph.v(e))
324 - weight[e] * mwfm.dualScale;
326 check(rw >= 0, "Negative reduced weight");
327 check(rw == 0 || !mwfm.matching(e),
328 "Non-zero reduced weight on matching edge");
332 for (SmartGraph::NodeIt n(graph); n != INVALID; ++n) {
334 for (InArcIt a(graph, n); a != INVALID; ++a) {
335 if (mwfm.matching(graph.source(a)) == a) {
339 check(indeg <= 1, "Invalid matching");
340 if (mwfm.matching(n) != INVALID) {
341 check(mwfm.nodeValue(n) >= 0, "Invalid node value");
342 check(indeg == 1, "Invalid matching");
343 pv += weight[mwfm.matching(n)];
344 SmartGraph::Node o = graph.target(mwfm.matching(n));
346 check(mwfm.nodeValue(n) == 0, "Invalid matching");
347 check(indeg == 0, "Invalid matching");
351 for (SmartGraph::EdgeIt e(graph); e != INVALID; ++e) {
352 check((e == mwfm.matching(graph.u(e)) ? 1 : 0) +
353 (e == mwfm.matching(graph.v(e)) ? 1 : 0) ==
354 mwfm.matching(e), "Invalid matching");
358 for (SmartGraph::NodeIt n(graph); n != INVALID; ++n) {
359 dv += mwfm.nodeValue(n);
362 check(pv * mwfm.dualScale == dv * 2, "Wrong duality");
364 SmartGraph::NodeMap<bool> processed(graph, false);
365 for (SmartGraph::NodeIt n(graph); n != INVALID; ++n) {
366 if (processed[n]) continue;
368 if (mwfm.matching(n) == INVALID) continue;
370 Node v = graph.target(mwfm.matching(n));
374 v = graph.target(mwfm.matching(v));
376 check(num == 2 || num % 2 == 1, "Wrong cycle size");
377 check(allow_loops || num != 1, "Wrong cycle size");
383 void checkWeightedPerfectFractionalMatching(const SmartGraph& graph,
384 const SmartGraph::EdgeMap<int>& weight,
385 const MaxWeightedPerfectFractionalMatching<SmartGraph>& mwpfm,
386 bool allow_loops = true) {
387 for (SmartGraph::EdgeIt e(graph); e != INVALID; ++e) {
388 if (graph.u(e) == graph.v(e) && !allow_loops) continue;
389 int rw = mwpfm.nodeValue(graph.u(e)) + mwpfm.nodeValue(graph.v(e))
390 - weight[e] * mwpfm.dualScale;
392 check(rw >= 0, "Negative reduced weight");
393 check(rw == 0 || !mwpfm.matching(e),
394 "Non-zero reduced weight on matching edge");
398 for (SmartGraph::NodeIt n(graph); n != INVALID; ++n) {
400 for (InArcIt a(graph, n); a != INVALID; ++a) {
401 if (mwpfm.matching(graph.source(a)) == a) {
405 check(mwpfm.matching(n) != INVALID, "Invalid perfect matching");
406 check(indeg == 1, "Invalid perfect matching");
407 pv += weight[mwpfm.matching(n)];
408 SmartGraph::Node o = graph.target(mwpfm.matching(n));
411 for (SmartGraph::EdgeIt e(graph); e != INVALID; ++e) {
412 check((e == mwpfm.matching(graph.u(e)) ? 1 : 0) +
413 (e == mwpfm.matching(graph.v(e)) ? 1 : 0) ==
414 mwpfm.matching(e), "Invalid matching");
418 for (SmartGraph::NodeIt n(graph); n != INVALID; ++n) {
419 dv += mwpfm.nodeValue(n);
422 check(pv * mwpfm.dualScale == dv * 2, "Wrong duality");
424 SmartGraph::NodeMap<bool> processed(graph, false);
425 for (SmartGraph::NodeIt n(graph); n != INVALID; ++n) {
426 if (processed[n]) continue;
428 if (mwpfm.matching(n) == INVALID) continue;
430 Node v = graph.target(mwpfm.matching(n));
434 v = graph.target(mwpfm.matching(v));
436 check(num == 2 || num % 2 == 1, "Wrong cycle size");
437 check(allow_loops || num != 1, "Wrong cycle size");
446 for (int i = 0; i < lgfn; ++i) {
448 SmartGraph::EdgeMap<int> weight(graph);
450 istringstream lgfs(lgf[i]);
451 graphReader(graph, lgfs).
452 edgeMap("weight", weight).run();
454 bool perfect_with_loops;
456 MaxFractionalMatching<SmartGraph> mfm(graph, true);
458 checkFractionalMatching(graph, mfm, true);
459 perfect_with_loops = mfm.matchingSize() == countNodes(graph);
462 bool perfect_without_loops;
464 MaxFractionalMatching<SmartGraph> mfm(graph, false);
466 checkFractionalMatching(graph, mfm, false);
467 perfect_without_loops = mfm.matchingSize() == countNodes(graph);
471 MaxFractionalMatching<SmartGraph> mfm(graph, true);
472 bool result = mfm.runPerfect();
473 checkPerfectFractionalMatching(graph, mfm, result, true);
474 check(result == perfect_with_loops, "Wrong perfect matching");
478 MaxFractionalMatching<SmartGraph> mfm(graph, false);
479 bool result = mfm.runPerfect();
480 checkPerfectFractionalMatching(graph, mfm, result, false);
481 check(result == perfect_without_loops, "Wrong perfect matching");
485 MaxWeightedFractionalMatching<SmartGraph> mwfm(graph, weight, true);
487 checkWeightedFractionalMatching(graph, weight, mwfm, true);
491 MaxWeightedFractionalMatching<SmartGraph> mwfm(graph, weight, false);
493 checkWeightedFractionalMatching(graph, weight, mwfm, false);
497 MaxWeightedPerfectFractionalMatching<SmartGraph> mwpfm(graph, weight,
499 bool perfect = mwpfm.run();
500 check(perfect == (mwpfm.matchingSize() == countNodes(graph)),
501 "Perfect matching found");
502 check(perfect == perfect_with_loops, "Wrong perfect matching");
505 checkWeightedPerfectFractionalMatching(graph, weight, mwpfm, true);
510 MaxWeightedPerfectFractionalMatching<SmartGraph> mwpfm(graph, weight,
512 bool perfect = mwpfm.run();
513 check(perfect == (mwpfm.matchingSize() == countNodes(graph)),
514 "Perfect matching found");
515 check(perfect == perfect_without_loops, "Wrong perfect matching");
518 checkWeightedPerfectFractionalMatching(graph, weight, mwpfm, false);