1 /* -*- mode: C++; indent-tabs-mode: nil; -*-
3 * This file is a part of LEMON, a generic C++ optimization library.
5 * Copyright (C) 2003-2013
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/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] = {
115 void checkMaxMatchingCompile()
117 typedef concepts::Graph Graph;
118 typedef Graph::Node Node;
119 typedef Graph::Edge Edge;
120 typedef Graph::EdgeMap<bool> MatMap;
127 MaxMatching<Graph> mat_test(g);
128 const MaxMatching<Graph>&
129 const_mat_test = mat_test;
132 mat_test.greedyInit();
133 mat_test.matchingInit(mat);
134 mat_test.startSparse();
135 mat_test.startDense();
137 mat_test.startSparse(false);
138 mat_test.startDense(false);
141 const_mat_test.matchingSize();
142 const_mat_test.matching(e);
143 const_mat_test.matching(n);
144 const MaxMatching<Graph>::MatchingMap& mmap =
145 const_mat_test.matchingMap();
147 const_mat_test.mate(n);
149 MaxMatching<Graph>::Status stat =
150 const_mat_test.status(n);
151 ::lemon::ignore_unused_variable_warning(stat);
152 const MaxMatching<Graph>::StatusMap& smap =
153 const_mat_test.statusMap();
155 const_mat_test.barrier(n);
158 void checkMaxWeightedMatchingCompile()
160 typedef concepts::Graph Graph;
161 typedef Graph::Node Node;
162 typedef Graph::Edge Edge;
163 typedef Graph::EdgeMap<int> WeightMap;
170 MaxWeightedMatching<Graph> mat_test(g, w);
171 const MaxWeightedMatching<Graph>&
172 const_mat_test = mat_test;
178 const_mat_test.matchingWeight();
179 const_mat_test.matchingSize();
180 const_mat_test.matching(e);
181 const_mat_test.matching(n);
182 const MaxWeightedMatching<Graph>::MatchingMap& mmap =
183 const_mat_test.matchingMap();
185 const_mat_test.mate(n);
188 const_mat_test.dualValue();
189 const_mat_test.nodeValue(n);
190 const_mat_test.blossomNum();
191 const_mat_test.blossomSize(k);
192 const_mat_test.blossomValue(k);
195 void checkMaxWeightedPerfectMatchingCompile()
197 typedef concepts::Graph Graph;
198 typedef Graph::Node Node;
199 typedef Graph::Edge Edge;
200 typedef Graph::EdgeMap<int> WeightMap;
207 MaxWeightedPerfectMatching<Graph> mat_test(g, w);
208 const MaxWeightedPerfectMatching<Graph>&
209 const_mat_test = mat_test;
215 const_mat_test.matchingWeight();
216 const_mat_test.matching(e);
217 const_mat_test.matching(n);
218 const MaxWeightedPerfectMatching<Graph>::MatchingMap& mmap =
219 const_mat_test.matchingMap();
221 const_mat_test.mate(n);
224 const_mat_test.dualValue();
225 const_mat_test.nodeValue(n);
226 const_mat_test.blossomNum();
227 const_mat_test.blossomSize(k);
228 const_mat_test.blossomValue(k);
231 void checkMatching(const SmartGraph& graph,
232 const MaxMatching<SmartGraph>& mm) {
235 IntNodeMap comp_index(graph);
236 UnionFind<IntNodeMap> comp(comp_index);
240 for (NodeIt n(graph); n != INVALID; ++n) {
241 check(mm.status(n) == MaxMatching<SmartGraph>::EVEN ||
242 mm.matching(n) != INVALID, "Wrong Gallai-Edmonds decomposition");
243 if (mm.status(n) == MaxMatching<SmartGraph>::ODD) {
250 for (EdgeIt e(graph); e != INVALID; ++e) {
251 if (mm.matching(e)) {
252 check(e == mm.matching(graph.u(e)), "Wrong matching");
253 check(e == mm.matching(graph.v(e)), "Wrong matching");
256 check(mm.status(graph.u(e)) != MaxMatching<SmartGraph>::EVEN ||
257 mm.status(graph.v(e)) != MaxMatching<SmartGraph>::MATCHED,
258 "Wrong Gallai-Edmonds decomposition");
260 check(mm.status(graph.v(e)) != MaxMatching<SmartGraph>::EVEN ||
261 mm.status(graph.u(e)) != MaxMatching<SmartGraph>::MATCHED,
262 "Wrong Gallai-Edmonds decomposition");
264 if (mm.status(graph.u(e)) != MaxMatching<SmartGraph>::ODD &&
265 mm.status(graph.v(e)) != MaxMatching<SmartGraph>::ODD) {
266 comp.join(graph.u(e), graph.v(e));
270 std::set<int> comp_root;
271 int odd_comp_num = 0;
272 for (NodeIt n(graph); n != INVALID; ++n) {
273 if (mm.status(n) != MaxMatching<SmartGraph>::ODD) {
274 int root = comp.find(n);
275 if (comp_root.find(root) == comp_root.end()) {
276 comp_root.insert(root);
277 if (comp.size(n) % 2 == 1) {
284 check(mm.matchingSize() == num, "Wrong matching");
285 check(2 * num == countNodes(graph) - (odd_comp_num - barrier_num),
290 void checkWeightedMatching(const SmartGraph& graph,
291 const SmartGraph::EdgeMap<int>& weight,
292 const MaxWeightedMatching<SmartGraph>& mwm) {
293 for (SmartGraph::EdgeIt e(graph); e != INVALID; ++e) {
294 if (graph.u(e) == graph.v(e)) continue;
295 int rw = mwm.nodeValue(graph.u(e)) + mwm.nodeValue(graph.v(e));
297 for (int i = 0; i < mwm.blossomNum(); ++i) {
298 bool s = false, t = false;
299 for (MaxWeightedMatching<SmartGraph>::BlossomIt n(mwm, i);
301 if (graph.u(e) == n) s = true;
302 if (graph.v(e) == n) t = true;
304 if (s == true && t == true) {
305 rw += mwm.blossomValue(i);
308 rw -= weight[e] * mwm.dualScale;
310 check(rw >= 0, "Negative reduced weight");
311 check(rw == 0 || !mwm.matching(e),
312 "Non-zero reduced weight on matching edge");
316 for (SmartGraph::NodeIt n(graph); n != INVALID; ++n) {
317 if (mwm.matching(n) != INVALID) {
318 check(mwm.nodeValue(n) >= 0, "Invalid node value");
319 pv += weight[mwm.matching(n)];
320 SmartGraph::Node o = graph.target(mwm.matching(n));
321 check(mwm.mate(n) == o, "Invalid matching");
322 check(mwm.matching(n) == graph.oppositeArc(mwm.matching(o)),
325 check(mwm.mate(n) == INVALID, "Invalid matching");
326 check(mwm.nodeValue(n) == 0, "Invalid matching");
331 for (SmartGraph::NodeIt n(graph); n != INVALID; ++n) {
332 dv += mwm.nodeValue(n);
335 for (int i = 0; i < mwm.blossomNum(); ++i) {
336 check(mwm.blossomValue(i) >= 0, "Invalid blossom value");
337 check(mwm.blossomSize(i) % 2 == 1, "Even blossom size");
338 dv += mwm.blossomValue(i) * ((mwm.blossomSize(i) - 1) / 2);
341 check(pv * mwm.dualScale == dv * 2, "Wrong duality");
346 void checkWeightedPerfectMatching(const SmartGraph& graph,
347 const SmartGraph::EdgeMap<int>& weight,
348 const MaxWeightedPerfectMatching<SmartGraph>& mwpm) {
349 for (SmartGraph::EdgeIt e(graph); e != INVALID; ++e) {
350 if (graph.u(e) == graph.v(e)) continue;
351 int rw = mwpm.nodeValue(graph.u(e)) + mwpm.nodeValue(graph.v(e));
353 for (int i = 0; i < mwpm.blossomNum(); ++i) {
354 bool s = false, t = false;
355 for (MaxWeightedPerfectMatching<SmartGraph>::BlossomIt n(mwpm, i);
357 if (graph.u(e) == n) s = true;
358 if (graph.v(e) == n) t = true;
360 if (s == true && t == true) {
361 rw += mwpm.blossomValue(i);
364 rw -= weight[e] * mwpm.dualScale;
366 check(rw >= 0, "Negative reduced weight");
367 check(rw == 0 || !mwpm.matching(e),
368 "Non-zero reduced weight on matching edge");
372 for (SmartGraph::NodeIt n(graph); n != INVALID; ++n) {
373 check(mwpm.matching(n) != INVALID, "Non perfect");
374 pv += weight[mwpm.matching(n)];
375 SmartGraph::Node o = graph.target(mwpm.matching(n));
376 check(mwpm.mate(n) == o, "Invalid matching");
377 check(mwpm.matching(n) == graph.oppositeArc(mwpm.matching(o)),
382 for (SmartGraph::NodeIt n(graph); n != INVALID; ++n) {
383 dv += mwpm.nodeValue(n);
386 for (int i = 0; i < mwpm.blossomNum(); ++i) {
387 check(mwpm.blossomValue(i) >= 0, "Invalid blossom value");
388 check(mwpm.blossomSize(i) % 2 == 1, "Even blossom size");
389 dv += mwpm.blossomValue(i) * ((mwpm.blossomSize(i) - 1) / 2);
392 check(pv * mwpm.dualScale == dv * 2, "Wrong duality");
400 for (int i = 0; i < lgfn; ++i) {
402 SmartGraph::EdgeMap<int> weight(graph);
404 istringstream lgfs(lgf[i]);
405 graphReader(graph, lgfs).
406 edgeMap("weight", weight).run();
411 MaxMatching<SmartGraph> mm(graph);
413 checkMatching(graph, mm);
414 size = mm.matchingSize();
415 perfect = 2 * mm.matchingSize() == countNodes(graph);
419 MaxMatching<SmartGraph> mm(graph);
422 checkMatching(graph, mm);
423 check(size == mm.matchingSize(), "Inconsistent matching size");
427 MaxMatching<SmartGraph> mm(graph);
430 checkMatching(graph, mm);
431 check(size == mm.matchingSize(), "Inconsistent matching size");
435 MaxMatching<SmartGraph> mm(graph);
438 checkMatching(graph, mm);
439 check(size == mm.matchingSize(), "Inconsistent matching size");
443 MaxMatching<SmartGraph> mm(graph);
446 checkMatching(graph, mm);
447 check(size == mm.matchingSize(), "Inconsistent matching size");
451 MaxMatching<SmartGraph> mm(graph);
453 check(size == mm.matchingSize(), "Inconsistent max cardinality matching");
457 MaxMatching<SmartGraph> mm(graph);
459 mm.startSparse(false);
460 check(size == mm.matchingSize(), "Inconsistent matching size");
464 MaxMatching<SmartGraph> mm(graph);
466 mm.startDense(false);
467 check(size == mm.matchingSize(), "Inconsistent matching size");
471 MaxMatching<SmartGraph> mm(graph);
473 mm.startSparse(false);
474 check(size == mm.matchingSize(), "Inconsistent matching size");
478 MaxMatching<SmartGraph> mm(graph);
480 mm.startDense(false);
481 check(size == mm.matchingSize(), "Inconsistent matching size");
485 MaxWeightedMatching<SmartGraph> mwm(graph, weight);
487 checkWeightedMatching(graph, weight, mwm);
491 MaxWeightedMatching<SmartGraph> mwm(graph, weight);
494 checkWeightedMatching(graph, weight, mwm);
498 MaxWeightedPerfectMatching<SmartGraph> mwpm(graph, weight);
499 bool result = mwpm.run();
501 check(result == perfect, "Perfect matching found");
503 checkWeightedPerfectMatching(graph, weight, mwpm);
508 MaxWeightedPerfectMatching<SmartGraph> mwpm(graph, weight);
510 bool result = mwpm.start();
512 check(result == perfect, "Perfect matching found");
514 checkWeightedPerfectMatching(graph, weight, mwpm);