/* -*- mode: C++; indent-tabs-mode: nil; -*-
* This file is a part of LEMON, a generic C++ optimization library.
* Copyright (C) 2003-2009
* 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/fractional_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 checkMaxFractionalMatchingCompile()
typedef concepts::Graph Graph;
typedef Graph::Node Node;
typedef Graph::Edge Edge;
MaxFractionalMatching<Graph> mat_test(g);
const MaxFractionalMatching<Graph>&
const_mat_test = mat_test;
mat_test.startPerfect(true);
mat_test.runPerfect(true);
const_mat_test.matchingSize();
const_mat_test.matching(e);
const_mat_test.matching(n);
const MaxFractionalMatching<Graph>::MatchingMap& mmap =
const_mat_test.matchingMap();
const_mat_test.barrier(n);
void checkMaxWeightedFractionalMatchingCompile()
typedef concepts::Graph Graph;
typedef Graph::Node Node;
typedef Graph::Edge Edge;
typedef Graph::EdgeMap<int> WeightMap;
MaxWeightedFractionalMatching<Graph> mat_test(g, w);
const MaxWeightedFractionalMatching<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 MaxWeightedFractionalMatching<Graph>::MatchingMap& mmap =
const_mat_test.matchingMap();
const_mat_test.dualValue();
const_mat_test.nodeValue(n);
void checkMaxWeightedPerfectFractionalMatchingCompile()
typedef concepts::Graph Graph;
typedef Graph::Node Node;
typedef Graph::Edge Edge;
typedef Graph::EdgeMap<int> WeightMap;
MaxWeightedPerfectFractionalMatching<Graph> mat_test(g, w);
const MaxWeightedPerfectFractionalMatching<Graph>&
const_mat_test = mat_test;
const_mat_test.matchingWeight();
const_mat_test.matching(e);
const_mat_test.matching(n);
const MaxWeightedPerfectFractionalMatching<Graph>::MatchingMap& mmap =
const_mat_test.matchingMap();
const_mat_test.dualValue();
const_mat_test.nodeValue(n);
void checkFractionalMatching(const SmartGraph& graph,
const MaxFractionalMatching<SmartGraph>& mfm,
bool allow_loops = true) {
for (SmartGraph::NodeIt n(graph); n != INVALID; ++n) {
for (InArcIt a(graph, n); a != INVALID; ++a) {
if (mfm.matching(graph.source(a)) == a) {
if (mfm.matching(n) != INVALID) {
check(indeg == 1, "Invalid matching");
check(indeg == 0, "Invalid matching");
check(pv == mfm.matchingSize(), "Wrong matching size");
for (SmartGraph::EdgeIt e(graph); e != INVALID; ++e) {
check((e == mfm.matching(graph.u(e)) ? 1 : 0) +
(e == mfm.matching(graph.v(e)) ? 1 : 0) ==
mfm.matching(e), "Invalid matching");
SmartGraph::NodeMap<bool> processed(graph, false);
for (SmartGraph::NodeIt n(graph); n != INVALID; ++n) {
if (processed[n]) continue;
if (mfm.matching(n) == INVALID) continue;
Node v = graph.target(mfm.matching(n));
v = graph.target(mfm.matching(v));
check(num == 2 || num % 2 == 1, "Wrong cycle size");
check(allow_loops || num != 1, "Wrong cycle size");
SmartGraph::NodeMap<bool> neighbours(graph, false);
for (SmartGraph::NodeIt n(graph); n != INVALID; ++n) {
if (!mfm.barrier(n)) continue;
for (SmartGraph::InArcIt a(graph, n); a != INVALID; ++a) {
Node u = graph.source(a);
if (!allow_loops && u == n) continue;
check(anum - bnum + mfm.matchingSize() == countNodes(graph),
void checkPerfectFractionalMatching(const SmartGraph& graph,
const MaxFractionalMatching<SmartGraph>& mfm,
bool perfect, bool allow_loops = true) {
for (SmartGraph::NodeIt n(graph); n != INVALID; ++n) {
for (InArcIt a(graph, n); a != INVALID; ++a) {
if (mfm.matching(graph.source(a)) == a) {
check(mfm.matching(n) != INVALID, "Invalid matching");
check(indeg == 1, "Invalid matching");
for (SmartGraph::EdgeIt e(graph); e != INVALID; ++e) {
check((e == mfm.matching(graph.u(e)) ? 1 : 0) +
(e == mfm.matching(graph.v(e)) ? 1 : 0) ==
mfm.matching(e), "Invalid matching");
SmartGraph::NodeMap<bool> neighbours(graph, false);
for (SmartGraph::NodeIt n(graph); n != INVALID; ++n) {
if (!mfm.barrier(n)) continue;
for (SmartGraph::InArcIt a(graph, n); a != INVALID; ++a) {
Node u = graph.source(a);
if (!allow_loops && u == n) continue;
check(anum - bnum > 0, "Wrong barrier");
void checkWeightedFractionalMatching(const SmartGraph& graph,
const SmartGraph::EdgeMap<int>& weight,
const MaxWeightedFractionalMatching<SmartGraph>& mwfm,
bool allow_loops = true) {
for (SmartGraph::EdgeIt e(graph); e != INVALID; ++e) {
if (graph.u(e) == graph.v(e) && !allow_loops) continue;
int rw = mwfm.nodeValue(graph.u(e)) + mwfm.nodeValue(graph.v(e))
- weight[e] * mwfm.dualScale;
check(rw >= 0, "Negative reduced weight");
check(rw == 0 || !mwfm.matching(e),
"Non-zero reduced weight on matching edge");
for (SmartGraph::NodeIt n(graph); n != INVALID; ++n) {
for (InArcIt a(graph, n); a != INVALID; ++a) {
if (mwfm.matching(graph.source(a)) == a) {
check(indeg <= 1, "Invalid matching");
if (mwfm.matching(n) != INVALID) {
check(mwfm.nodeValue(n) >= 0, "Invalid node value");
check(indeg == 1, "Invalid matching");
pv += weight[mwfm.matching(n)];
SmartGraph::Node o = graph.target(mwfm.matching(n));
check(mwfm.nodeValue(n) == 0, "Invalid matching");
check(indeg == 0, "Invalid matching");
for (SmartGraph::EdgeIt e(graph); e != INVALID; ++e) {
check((e == mwfm.matching(graph.u(e)) ? 1 : 0) +
(e == mwfm.matching(graph.v(e)) ? 1 : 0) ==
mwfm.matching(e), "Invalid matching");
for (SmartGraph::NodeIt n(graph); n != INVALID; ++n) {
check(pv * mwfm.dualScale == dv * 2, "Wrong duality");
SmartGraph::NodeMap<bool> processed(graph, false);
for (SmartGraph::NodeIt n(graph); n != INVALID; ++n) {
if (processed[n]) continue;
if (mwfm.matching(n) == INVALID) continue;
Node v = graph.target(mwfm.matching(n));
v = graph.target(mwfm.matching(v));
check(num == 2 || num % 2 == 1, "Wrong cycle size");
check(allow_loops || num != 1, "Wrong cycle size");
void checkWeightedPerfectFractionalMatching(const SmartGraph& graph,
const SmartGraph::EdgeMap<int>& weight,
const MaxWeightedPerfectFractionalMatching<SmartGraph>& mwpfm,
bool allow_loops = true) {
for (SmartGraph::EdgeIt e(graph); e != INVALID; ++e) {
if (graph.u(e) == graph.v(e) && !allow_loops) continue;
int rw = mwpfm.nodeValue(graph.u(e)) + mwpfm.nodeValue(graph.v(e))
- weight[e] * mwpfm.dualScale;
check(rw >= 0, "Negative reduced weight");
check(rw == 0 || !mwpfm.matching(e),
"Non-zero reduced weight on matching edge");
for (SmartGraph::NodeIt n(graph); n != INVALID; ++n) {
for (InArcIt a(graph, n); a != INVALID; ++a) {
if (mwpfm.matching(graph.source(a)) == a) {
check(mwpfm.matching(n) != INVALID, "Invalid perfect matching");
check(indeg == 1, "Invalid perfect matching");
pv += weight[mwpfm.matching(n)];
SmartGraph::Node o = graph.target(mwpfm.matching(n));
for (SmartGraph::EdgeIt e(graph); e != INVALID; ++e) {
check((e == mwpfm.matching(graph.u(e)) ? 1 : 0) +
(e == mwpfm.matching(graph.v(e)) ? 1 : 0) ==
mwpfm.matching(e), "Invalid matching");
for (SmartGraph::NodeIt n(graph); n != INVALID; ++n) {
dv += mwpfm.nodeValue(n);
check(pv * mwpfm.dualScale == dv * 2, "Wrong duality");
SmartGraph::NodeMap<bool> processed(graph, false);
for (SmartGraph::NodeIt n(graph); n != INVALID; ++n) {
if (processed[n]) continue;
if (mwpfm.matching(n) == INVALID) continue;
Node v = graph.target(mwpfm.matching(n));
v = graph.target(mwpfm.matching(v));
check(num == 2 || num % 2 == 1, "Wrong cycle size");
check(allow_loops || num != 1, "Wrong cycle size");
for (int i = 0; i < lgfn; ++i) {
SmartGraph::EdgeMap<int> weight(graph);
istringstream lgfs(lgf[i]);
graphReader(graph, lgfs).
edgeMap("weight", weight).run();
MaxFractionalMatching<SmartGraph> mfm(graph, true);
checkFractionalMatching(graph, mfm, true);
perfect_with_loops = mfm.matchingSize() == countNodes(graph);
bool perfect_without_loops;
MaxFractionalMatching<SmartGraph> mfm(graph, false);
checkFractionalMatching(graph, mfm, false);
perfect_without_loops = mfm.matchingSize() == countNodes(graph);
MaxFractionalMatching<SmartGraph> mfm(graph, true);
bool result = mfm.runPerfect();
checkPerfectFractionalMatching(graph, mfm, result, true);
check(result == perfect_with_loops, "Wrong perfect matching");
MaxFractionalMatching<SmartGraph> mfm(graph, false);
bool result = mfm.runPerfect();
checkPerfectFractionalMatching(graph, mfm, result, false);
check(result == perfect_without_loops, "Wrong perfect matching");
MaxWeightedFractionalMatching<SmartGraph> mwfm(graph, weight, true);
checkWeightedFractionalMatching(graph, weight, mwfm, true);
MaxWeightedFractionalMatching<SmartGraph> mwfm(graph, weight, false);
checkWeightedFractionalMatching(graph, weight, mwfm, false);
MaxWeightedPerfectFractionalMatching<SmartGraph> mwpfm(graph, weight,
bool perfect = mwpfm.run();
check(perfect == (mwpfm.matchingSize() == countNodes(graph)),
"Perfect matching found");
check(perfect == perfect_with_loops, "Wrong perfect matching");
checkWeightedPerfectFractionalMatching(graph, weight, mwpfm, true);
MaxWeightedPerfectFractionalMatching<SmartGraph> mwpfm(graph, weight,
bool perfect = mwpfm.run();
check(perfect == (mwpfm.matchingSize() == countNodes(graph)),
"Perfect matching found");
check(perfect == perfect_without_loops, "Wrong perfect matching");
checkWeightedPerfectFractionalMatching(graph, weight, mwpfm, false);