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
23 #include <lemon/math.h>
26 #include <lemon/max_matching.h>
27 #include <lemon/smart_graph.h>
28 #include <lemon/lgf_reader.h>
30 #include "test_tools.h"
33 using namespace lemon;
35 GRAPH_TYPEDEFS(SmartGraph);
39 const std::string lgf[lgfn] = {
113 void checkMatching(const SmartGraph& graph,
114 const MaxMatching<SmartGraph>& mm) {
117 IntNodeMap comp_index(graph);
118 UnionFind<IntNodeMap> comp(comp_index);
122 for (NodeIt n(graph); n != INVALID; ++n) {
123 check(mm.decomposition(n) == MaxMatching<SmartGraph>::EVEN ||
124 mm.matching(n) != INVALID, "Wrong Gallai-Edmonds decomposition");
125 if (mm.decomposition(n) == MaxMatching<SmartGraph>::ODD) {
132 for (EdgeIt e(graph); e != INVALID; ++e) {
133 if (mm.matching(e)) {
134 check(e == mm.matching(graph.u(e)), "Wrong matching");
135 check(e == mm.matching(graph.v(e)), "Wrong matching");
138 check(mm.decomposition(graph.u(e)) != MaxMatching<SmartGraph>::EVEN ||
139 mm.decomposition(graph.v(e)) != MaxMatching<SmartGraph>::MATCHED,
140 "Wrong Gallai-Edmonds decomposition");
142 check(mm.decomposition(graph.v(e)) != MaxMatching<SmartGraph>::EVEN ||
143 mm.decomposition(graph.u(e)) != MaxMatching<SmartGraph>::MATCHED,
144 "Wrong Gallai-Edmonds decomposition");
146 if (mm.decomposition(graph.u(e)) != MaxMatching<SmartGraph>::ODD &&
147 mm.decomposition(graph.v(e)) != MaxMatching<SmartGraph>::ODD) {
148 comp.join(graph.u(e), graph.v(e));
152 std::set<int> comp_root;
153 int odd_comp_num = 0;
154 for (NodeIt n(graph); n != INVALID; ++n) {
155 if (mm.decomposition(n) != MaxMatching<SmartGraph>::ODD) {
156 int root = comp.find(n);
157 if (comp_root.find(root) == comp_root.end()) {
158 comp_root.insert(root);
159 if (comp.size(n) % 2 == 1) {
166 check(mm.matchingSize() == num, "Wrong matching");
167 check(2 * num == countNodes(graph) - (odd_comp_num - barrier_num),
172 void checkWeightedMatching(const SmartGraph& graph,
173 const SmartGraph::EdgeMap<int>& weight,
174 const MaxWeightedMatching<SmartGraph>& mwm) {
175 for (SmartGraph::EdgeIt e(graph); e != INVALID; ++e) {
176 if (graph.u(e) == graph.v(e)) continue;
177 int rw = mwm.nodeValue(graph.u(e)) + mwm.nodeValue(graph.v(e));
179 for (int i = 0; i < mwm.blossomNum(); ++i) {
180 bool s = false, t = false;
181 for (MaxWeightedMatching<SmartGraph>::BlossomIt n(mwm, i);
183 if (graph.u(e) == n) s = true;
184 if (graph.v(e) == n) t = true;
186 if (s == true && t == true) {
187 rw += mwm.blossomValue(i);
190 rw -= weight[e] * mwm.dualScale;
192 check(rw >= 0, "Negative reduced weight");
193 check(rw == 0 || !mwm.matching(e),
194 "Non-zero reduced weight on matching edge");
198 for (SmartGraph::NodeIt n(graph); n != INVALID; ++n) {
199 if (mwm.matching(n) != INVALID) {
200 check(mwm.nodeValue(n) >= 0, "Invalid node value");
201 pv += weight[mwm.matching(n)];
202 SmartGraph::Node o = graph.target(mwm.matching(n));
203 check(mwm.mate(n) == o, "Invalid matching");
204 check(mwm.matching(n) == graph.oppositeArc(mwm.matching(o)),
207 check(mwm.mate(n) == INVALID, "Invalid matching");
208 check(mwm.nodeValue(n) == 0, "Invalid matching");
213 for (SmartGraph::NodeIt n(graph); n != INVALID; ++n) {
214 dv += mwm.nodeValue(n);
217 for (int i = 0; i < mwm.blossomNum(); ++i) {
218 check(mwm.blossomValue(i) >= 0, "Invalid blossom value");
219 check(mwm.blossomSize(i) % 2 == 1, "Even blossom size");
220 dv += mwm.blossomValue(i) * ((mwm.blossomSize(i) - 1) / 2);
223 check(pv * mwm.dualScale == dv * 2, "Wrong duality");
228 void checkWeightedPerfectMatching(const SmartGraph& graph,
229 const SmartGraph::EdgeMap<int>& weight,
230 const MaxWeightedPerfectMatching<SmartGraph>& mwpm) {
231 for (SmartGraph::EdgeIt e(graph); e != INVALID; ++e) {
232 if (graph.u(e) == graph.v(e)) continue;
233 int rw = mwpm.nodeValue(graph.u(e)) + mwpm.nodeValue(graph.v(e));
235 for (int i = 0; i < mwpm.blossomNum(); ++i) {
236 bool s = false, t = false;
237 for (MaxWeightedPerfectMatching<SmartGraph>::BlossomIt n(mwpm, i);
239 if (graph.u(e) == n) s = true;
240 if (graph.v(e) == n) t = true;
242 if (s == true && t == true) {
243 rw += mwpm.blossomValue(i);
246 rw -= weight[e] * mwpm.dualScale;
248 check(rw >= 0, "Negative reduced weight");
249 check(rw == 0 || !mwpm.matching(e),
250 "Non-zero reduced weight on matching edge");
254 for (SmartGraph::NodeIt n(graph); n != INVALID; ++n) {
255 check(mwpm.matching(n) != INVALID, "Non perfect");
256 pv += weight[mwpm.matching(n)];
257 SmartGraph::Node o = graph.target(mwpm.matching(n));
258 check(mwpm.mate(n) == o, "Invalid matching");
259 check(mwpm.matching(n) == graph.oppositeArc(mwpm.matching(o)),
264 for (SmartGraph::NodeIt n(graph); n != INVALID; ++n) {
265 dv += mwpm.nodeValue(n);
268 for (int i = 0; i < mwpm.blossomNum(); ++i) {
269 check(mwpm.blossomValue(i) >= 0, "Invalid blossom value");
270 check(mwpm.blossomSize(i) % 2 == 1, "Even blossom size");
271 dv += mwpm.blossomValue(i) * ((mwpm.blossomSize(i) - 1) / 2);
274 check(pv * mwpm.dualScale == dv * 2, "Wrong duality");
282 for (int i = 0; i < lgfn; ++i) {
284 SmartGraph::EdgeMap<int> weight(graph);
286 istringstream lgfs(lgf[i]);
287 graphReader(graph, lgfs).
288 edgeMap("weight", weight).run();
290 MaxMatching<SmartGraph> mm(graph);
292 checkMatching(graph, mm);
294 MaxWeightedMatching<SmartGraph> mwm(graph, weight);
296 checkWeightedMatching(graph, weight, mwm);
298 MaxWeightedPerfectMatching<SmartGraph> mwpm(graph, weight);
299 bool perfect = mwpm.run();
301 check(perfect == (mm.matchingSize() * 2 == countNodes(graph)),
302 "Perfect matching found");
305 checkWeightedPerfectMatching(graph, weight, mwpm);