1 /* -*- mode: C++; indent-tabs-mode: nil; -*-
3 * This file is a part of LEMON, a generic C++ optimization library.
5 * Copyright (C) 2003-2011
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();
138 const_mat_test.matchingSize();
139 const_mat_test.matching(e);
140 const_mat_test.matching(n);
141 const MaxMatching<Graph>::MatchingMap& mmap =
142 const_mat_test.matchingMap();
144 const_mat_test.mate(n);
146 MaxMatching<Graph>::Status stat =
147 const_mat_test.status(n);
148 ::lemon::ignore_unused_variable_warning(stat);
149 const MaxMatching<Graph>::StatusMap& smap =
150 const_mat_test.statusMap();
152 const_mat_test.barrier(n);
155 void checkMaxWeightedMatchingCompile()
157 typedef concepts::Graph Graph;
158 typedef Graph::Node Node;
159 typedef Graph::Edge Edge;
160 typedef Graph::EdgeMap<int> WeightMap;
167 MaxWeightedMatching<Graph> mat_test(g, w);
168 const MaxWeightedMatching<Graph>&
169 const_mat_test = mat_test;
175 const_mat_test.matchingWeight();
176 const_mat_test.matchingSize();
177 const_mat_test.matching(e);
178 const_mat_test.matching(n);
179 const MaxWeightedMatching<Graph>::MatchingMap& mmap =
180 const_mat_test.matchingMap();
182 const_mat_test.mate(n);
185 const_mat_test.dualValue();
186 const_mat_test.nodeValue(n);
187 const_mat_test.blossomNum();
188 const_mat_test.blossomSize(k);
189 const_mat_test.blossomValue(k);
192 void checkMaxWeightedPerfectMatchingCompile()
194 typedef concepts::Graph Graph;
195 typedef Graph::Node Node;
196 typedef Graph::Edge Edge;
197 typedef Graph::EdgeMap<int> WeightMap;
204 MaxWeightedPerfectMatching<Graph> mat_test(g, w);
205 const MaxWeightedPerfectMatching<Graph>&
206 const_mat_test = mat_test;
212 const_mat_test.matchingWeight();
213 const_mat_test.matching(e);
214 const_mat_test.matching(n);
215 const MaxWeightedPerfectMatching<Graph>::MatchingMap& mmap =
216 const_mat_test.matchingMap();
218 const_mat_test.mate(n);
221 const_mat_test.dualValue();
222 const_mat_test.nodeValue(n);
223 const_mat_test.blossomNum();
224 const_mat_test.blossomSize(k);
225 const_mat_test.blossomValue(k);
228 void checkMatching(const SmartGraph& graph,
229 const MaxMatching<SmartGraph>& mm) {
232 IntNodeMap comp_index(graph);
233 UnionFind<IntNodeMap> comp(comp_index);
237 for (NodeIt n(graph); n != INVALID; ++n) {
238 check(mm.status(n) == MaxMatching<SmartGraph>::EVEN ||
239 mm.matching(n) != INVALID, "Wrong Gallai-Edmonds decomposition");
240 if (mm.status(n) == MaxMatching<SmartGraph>::ODD) {
247 for (EdgeIt e(graph); e != INVALID; ++e) {
248 if (mm.matching(e)) {
249 check(e == mm.matching(graph.u(e)), "Wrong matching");
250 check(e == mm.matching(graph.v(e)), "Wrong matching");
253 check(mm.status(graph.u(e)) != MaxMatching<SmartGraph>::EVEN ||
254 mm.status(graph.v(e)) != MaxMatching<SmartGraph>::MATCHED,
255 "Wrong Gallai-Edmonds decomposition");
257 check(mm.status(graph.v(e)) != MaxMatching<SmartGraph>::EVEN ||
258 mm.status(graph.u(e)) != MaxMatching<SmartGraph>::MATCHED,
259 "Wrong Gallai-Edmonds decomposition");
261 if (mm.status(graph.u(e)) != MaxMatching<SmartGraph>::ODD &&
262 mm.status(graph.v(e)) != MaxMatching<SmartGraph>::ODD) {
263 comp.join(graph.u(e), graph.v(e));
267 std::set<int> comp_root;
268 int odd_comp_num = 0;
269 for (NodeIt n(graph); n != INVALID; ++n) {
270 if (mm.status(n) != MaxMatching<SmartGraph>::ODD) {
271 int root = comp.find(n);
272 if (comp_root.find(root) == comp_root.end()) {
273 comp_root.insert(root);
274 if (comp.size(n) % 2 == 1) {
281 check(mm.matchingSize() == num, "Wrong matching");
282 check(2 * num == countNodes(graph) - (odd_comp_num - barrier_num),
287 void checkWeightedMatching(const SmartGraph& graph,
288 const SmartGraph::EdgeMap<int>& weight,
289 const MaxWeightedMatching<SmartGraph>& mwm) {
290 for (SmartGraph::EdgeIt e(graph); e != INVALID; ++e) {
291 if (graph.u(e) == graph.v(e)) continue;
292 int rw = mwm.nodeValue(graph.u(e)) + mwm.nodeValue(graph.v(e));
294 for (int i = 0; i < mwm.blossomNum(); ++i) {
295 bool s = false, t = false;
296 for (MaxWeightedMatching<SmartGraph>::BlossomIt n(mwm, i);
298 if (graph.u(e) == n) s = true;
299 if (graph.v(e) == n) t = true;
301 if (s == true && t == true) {
302 rw += mwm.blossomValue(i);
305 rw -= weight[e] * mwm.dualScale;
307 check(rw >= 0, "Negative reduced weight");
308 check(rw == 0 || !mwm.matching(e),
309 "Non-zero reduced weight on matching edge");
313 for (SmartGraph::NodeIt n(graph); n != INVALID; ++n) {
314 if (mwm.matching(n) != INVALID) {
315 check(mwm.nodeValue(n) >= 0, "Invalid node value");
316 pv += weight[mwm.matching(n)];
317 SmartGraph::Node o = graph.target(mwm.matching(n));
318 check(mwm.mate(n) == o, "Invalid matching");
319 check(mwm.matching(n) == graph.oppositeArc(mwm.matching(o)),
322 check(mwm.mate(n) == INVALID, "Invalid matching");
323 check(mwm.nodeValue(n) == 0, "Invalid matching");
328 for (SmartGraph::NodeIt n(graph); n != INVALID; ++n) {
329 dv += mwm.nodeValue(n);
332 for (int i = 0; i < mwm.blossomNum(); ++i) {
333 check(mwm.blossomValue(i) >= 0, "Invalid blossom value");
334 check(mwm.blossomSize(i) % 2 == 1, "Even blossom size");
335 dv += mwm.blossomValue(i) * ((mwm.blossomSize(i) - 1) / 2);
338 check(pv * mwm.dualScale == dv * 2, "Wrong duality");
343 void checkWeightedPerfectMatching(const SmartGraph& graph,
344 const SmartGraph::EdgeMap<int>& weight,
345 const MaxWeightedPerfectMatching<SmartGraph>& mwpm) {
346 for (SmartGraph::EdgeIt e(graph); e != INVALID; ++e) {
347 if (graph.u(e) == graph.v(e)) continue;
348 int rw = mwpm.nodeValue(graph.u(e)) + mwpm.nodeValue(graph.v(e));
350 for (int i = 0; i < mwpm.blossomNum(); ++i) {
351 bool s = false, t = false;
352 for (MaxWeightedPerfectMatching<SmartGraph>::BlossomIt n(mwpm, i);
354 if (graph.u(e) == n) s = true;
355 if (graph.v(e) == n) t = true;
357 if (s == true && t == true) {
358 rw += mwpm.blossomValue(i);
361 rw -= weight[e] * mwpm.dualScale;
363 check(rw >= 0, "Negative reduced weight");
364 check(rw == 0 || !mwpm.matching(e),
365 "Non-zero reduced weight on matching edge");
369 for (SmartGraph::NodeIt n(graph); n != INVALID; ++n) {
370 check(mwpm.matching(n) != INVALID, "Non perfect");
371 pv += weight[mwpm.matching(n)];
372 SmartGraph::Node o = graph.target(mwpm.matching(n));
373 check(mwpm.mate(n) == o, "Invalid matching");
374 check(mwpm.matching(n) == graph.oppositeArc(mwpm.matching(o)),
379 for (SmartGraph::NodeIt n(graph); n != INVALID; ++n) {
380 dv += mwpm.nodeValue(n);
383 for (int i = 0; i < mwpm.blossomNum(); ++i) {
384 check(mwpm.blossomValue(i) >= 0, "Invalid blossom value");
385 check(mwpm.blossomSize(i) % 2 == 1, "Even blossom size");
386 dv += mwpm.blossomValue(i) * ((mwpm.blossomSize(i) - 1) / 2);
389 check(pv * mwpm.dualScale == dv * 2, "Wrong duality");
397 for (int i = 0; i < lgfn; ++i) {
399 SmartGraph::EdgeMap<int> weight(graph);
401 istringstream lgfs(lgf[i]);
402 graphReader(graph, lgfs).
403 edgeMap("weight", weight).run();
405 MaxMatching<SmartGraph> mm(graph);
407 checkMatching(graph, mm);
409 MaxWeightedMatching<SmartGraph> mwm(graph, weight);
411 checkWeightedMatching(graph, weight, mwm);
413 MaxWeightedPerfectMatching<SmartGraph> mwpm(graph, weight);
414 bool perfect = mwpm.run();
416 check(perfect == (mm.matchingSize() * 2 == countNodes(graph)),
417 "Perfect matching found");
420 checkWeightedPerfectMatching(graph, weight, mwpm);