/* -*- mode: C++; indent-tabs-mode: nil; -*-
* This file is a part of LEMON, a generic C++ optimization library.
* Copyright (C) 2003-2010
* Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
* (Egervary Research Group on Combinatorial Optimization, EGRES).
* Permission to use, modify and distribute this software is granted
* provided that this copyright notice appears in all copies. For
* precise terms see the accompanying LICENSE file.
* This software is provided "AS IS" with no warranty of any kind,
* express or implied, and with no claim as to its suitability for any
#include <lemon/matching.h>
#include <lemon/smart_graph.h>
#include <lemon/concepts/graph.h>
#include <lemon/concepts/maps.h>
#include <lemon/lgf_reader.h>
GRAPH_TYPEDEFS(SmartGraph);
const std::string lgf[lgfn] = {
void checkMaxMatchingCompile()
typedef concepts::Graph Graph;
typedef Graph::Node Node;
typedef Graph::Edge Edge;
typedef Graph::EdgeMap<bool> MatMap;
MaxMatching<Graph> mat_test(g);
const MaxMatching<Graph>&
const_mat_test = mat_test;
mat_test.matchingInit(mat);
const_mat_test.matchingSize();
const_mat_test.matching(e);
const_mat_test.matching(n);
const MaxMatching<Graph>::MatchingMap& mmap =
const_mat_test.matchingMap();
MaxMatching<Graph>::Status stat =
const_mat_test.status(n);
const MaxMatching<Graph>::StatusMap& smap =
const_mat_test.statusMap();
const_mat_test.barrier(n);
void checkMaxWeightedMatchingCompile()
typedef concepts::Graph Graph;
typedef Graph::Node Node;
typedef Graph::Edge Edge;
typedef Graph::EdgeMap<int> WeightMap;
MaxWeightedMatching<Graph> mat_test(g, w);
const MaxWeightedMatching<Graph>&
const_mat_test = mat_test;
const_mat_test.matchingWeight();
const_mat_test.matchingSize();
const_mat_test.matching(e);
const_mat_test.matching(n);
const MaxWeightedMatching<Graph>::MatchingMap& mmap =
const_mat_test.matchingMap();
const_mat_test.dualValue();
const_mat_test.nodeValue(n);
const_mat_test.blossomNum();
const_mat_test.blossomSize(k);
const_mat_test.blossomValue(k);
void checkMaxWeightedPerfectMatchingCompile()
typedef concepts::Graph Graph;
typedef Graph::Node Node;
typedef Graph::Edge Edge;
typedef Graph::EdgeMap<int> WeightMap;
MaxWeightedPerfectMatching<Graph> mat_test(g, w);
const MaxWeightedPerfectMatching<Graph>&
const_mat_test = mat_test;
const_mat_test.matchingWeight();
const_mat_test.matching(e);
const_mat_test.matching(n);
const MaxWeightedPerfectMatching<Graph>::MatchingMap& mmap =
const_mat_test.matchingMap();
const_mat_test.dualValue();
const_mat_test.nodeValue(n);
const_mat_test.blossomNum();
const_mat_test.blossomSize(k);
const_mat_test.blossomValue(k);
void checkMatching(const SmartGraph& graph,
const MaxMatching<SmartGraph>& mm) {
IntNodeMap comp_index(graph);
UnionFind<IntNodeMap> comp(comp_index);
for (NodeIt n(graph); n != INVALID; ++n) {
check(mm.status(n) == MaxMatching<SmartGraph>::EVEN ||
mm.matching(n) != INVALID, "Wrong Gallai-Edmonds decomposition");
if (mm.status(n) == MaxMatching<SmartGraph>::ODD) {
for (EdgeIt e(graph); e != INVALID; ++e) {
check(e == mm.matching(graph.u(e)), "Wrong matching");
check(e == mm.matching(graph.v(e)), "Wrong matching");
check(mm.status(graph.u(e)) != MaxMatching<SmartGraph>::EVEN ||
mm.status(graph.v(e)) != MaxMatching<SmartGraph>::MATCHED,
"Wrong Gallai-Edmonds decomposition");
check(mm.status(graph.v(e)) != MaxMatching<SmartGraph>::EVEN ||
mm.status(graph.u(e)) != MaxMatching<SmartGraph>::MATCHED,
"Wrong Gallai-Edmonds decomposition");
if (mm.status(graph.u(e)) != MaxMatching<SmartGraph>::ODD &&
mm.status(graph.v(e)) != MaxMatching<SmartGraph>::ODD) {
comp.join(graph.u(e), graph.v(e));
for (NodeIt n(graph); n != INVALID; ++n) {
if (mm.status(n) != MaxMatching<SmartGraph>::ODD) {
if (comp_root.find(root) == comp_root.end()) {
if (comp.size(n) % 2 == 1) {
check(mm.matchingSize() == num, "Wrong matching");
check(2 * num == countNodes(graph) - (odd_comp_num - barrier_num),
void checkWeightedMatching(const SmartGraph& graph,
const SmartGraph::EdgeMap<int>& weight,
const MaxWeightedMatching<SmartGraph>& mwm) {
for (SmartGraph::EdgeIt e(graph); e != INVALID; ++e) {
if (graph.u(e) == graph.v(e)) continue;
int rw = mwm.nodeValue(graph.u(e)) + mwm.nodeValue(graph.v(e));
for (int i = 0; i < mwm.blossomNum(); ++i) {
bool s = false, t = false;
for (MaxWeightedMatching<SmartGraph>::BlossomIt n(mwm, i);
if (graph.u(e) == n) s = true;
if (graph.v(e) == n) t = true;
if (s == true && t == true) {
rw += mwm.blossomValue(i);
rw -= weight[e] * mwm.dualScale;
check(rw >= 0, "Negative reduced weight");
check(rw == 0 || !mwm.matching(e),
"Non-zero reduced weight on matching edge");
for (SmartGraph::NodeIt n(graph); n != INVALID; ++n) {
if (mwm.matching(n) != INVALID) {
check(mwm.nodeValue(n) >= 0, "Invalid node value");
pv += weight[mwm.matching(n)];
SmartGraph::Node o = graph.target(mwm.matching(n));
check(mwm.mate(n) == o, "Invalid matching");
check(mwm.matching(n) == graph.oppositeArc(mwm.matching(o)),
check(mwm.mate(n) == INVALID, "Invalid matching");
check(mwm.nodeValue(n) == 0, "Invalid matching");
for (SmartGraph::NodeIt n(graph); n != INVALID; ++n) {
for (int i = 0; i < mwm.blossomNum(); ++i) {
check(mwm.blossomValue(i) >= 0, "Invalid blossom value");
check(mwm.blossomSize(i) % 2 == 1, "Even blossom size");
dv += mwm.blossomValue(i) * ((mwm.blossomSize(i) - 1) / 2);
check(pv * mwm.dualScale == dv * 2, "Wrong duality");
void checkWeightedPerfectMatching(const SmartGraph& graph,
const SmartGraph::EdgeMap<int>& weight,
const MaxWeightedPerfectMatching<SmartGraph>& mwpm) {
for (SmartGraph::EdgeIt e(graph); e != INVALID; ++e) {
if (graph.u(e) == graph.v(e)) continue;
int rw = mwpm.nodeValue(graph.u(e)) + mwpm.nodeValue(graph.v(e));
for (int i = 0; i < mwpm.blossomNum(); ++i) {
bool s = false, t = false;
for (MaxWeightedPerfectMatching<SmartGraph>::BlossomIt n(mwpm, i);
if (graph.u(e) == n) s = true;
if (graph.v(e) == n) t = true;
if (s == true && t == true) {
rw += mwpm.blossomValue(i);
rw -= weight[e] * mwpm.dualScale;
check(rw >= 0, "Negative reduced weight");
check(rw == 0 || !mwpm.matching(e),
"Non-zero reduced weight on matching edge");
for (SmartGraph::NodeIt n(graph); n != INVALID; ++n) {
check(mwpm.matching(n) != INVALID, "Non perfect");
pv += weight[mwpm.matching(n)];
SmartGraph::Node o = graph.target(mwpm.matching(n));
check(mwpm.mate(n) == o, "Invalid matching");
check(mwpm.matching(n) == graph.oppositeArc(mwpm.matching(o)),
for (SmartGraph::NodeIt n(graph); n != INVALID; ++n) {
for (int i = 0; i < mwpm.blossomNum(); ++i) {
check(mwpm.blossomValue(i) >= 0, "Invalid blossom value");
check(mwpm.blossomSize(i) % 2 == 1, "Even blossom size");
dv += mwpm.blossomValue(i) * ((mwpm.blossomSize(i) - 1) / 2);
check(pv * mwpm.dualScale == dv * 2, "Wrong duality");
for (int i = 0; i < lgfn; ++i) {
SmartGraph::EdgeMap<int> weight(graph);
istringstream lgfs(lgf[i]);
graphReader(graph, lgfs).
edgeMap("weight", weight).run();
MaxMatching<SmartGraph> mm(graph);
checkMatching(graph, mm);
perfect = 2 * mm.matchingSize() == countNodes(graph);
MaxWeightedMatching<SmartGraph> mwm(graph, weight);
checkWeightedMatching(graph, weight, mwm);
MaxWeightedMatching<SmartGraph> mwm(graph, weight);
checkWeightedMatching(graph, weight, mwm);
MaxWeightedPerfectMatching<SmartGraph> mwpm(graph, weight);
bool result = mwpm.run();
check(result == perfect, "Perfect matching found");
checkWeightedPerfectMatching(graph, weight, mwpm);
MaxWeightedPerfectMatching<SmartGraph> mwpm(graph, weight);
bool result = mwpm.start();
check(result == perfect, "Perfect matching found");
checkWeightedPerfectMatching(graph, weight, mwpm);