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
 
    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   const MaxMatching<Graph>::StatusMap& smap =
 
   149     const_mat_test.statusMap();
 
   151   const_mat_test.barrier(n);
 
   154 void checkMaxWeightedMatchingCompile()
 
   156   typedef concepts::Graph Graph;
 
   157   typedef Graph::Node Node;
 
   158   typedef Graph::Edge Edge;
 
   159   typedef Graph::EdgeMap<int> WeightMap;
 
   166   MaxWeightedMatching<Graph> mat_test(g, w);
 
   167   const MaxWeightedMatching<Graph>&
 
   168     const_mat_test = mat_test;
 
   174   const_mat_test.matchingWeight();
 
   175   const_mat_test.matchingSize();
 
   176   const_mat_test.matching(e);
 
   177   const_mat_test.matching(n);
 
   178   const MaxWeightedMatching<Graph>::MatchingMap& mmap =
 
   179     const_mat_test.matchingMap();
 
   181   const_mat_test.mate(n);
 
   184   const_mat_test.dualValue();
 
   185   const_mat_test.nodeValue(n);
 
   186   const_mat_test.blossomNum();
 
   187   const_mat_test.blossomSize(k);
 
   188   const_mat_test.blossomValue(k);
 
   191 void checkMaxWeightedPerfectMatchingCompile()
 
   193   typedef concepts::Graph Graph;
 
   194   typedef Graph::Node Node;
 
   195   typedef Graph::Edge Edge;
 
   196   typedef Graph::EdgeMap<int> WeightMap;
 
   203   MaxWeightedPerfectMatching<Graph> mat_test(g, w);
 
   204   const MaxWeightedPerfectMatching<Graph>&
 
   205     const_mat_test = mat_test;
 
   211   const_mat_test.matchingWeight();
 
   212   const_mat_test.matching(e);
 
   213   const_mat_test.matching(n);
 
   214   const MaxWeightedPerfectMatching<Graph>::MatchingMap& mmap =
 
   215     const_mat_test.matchingMap();
 
   217   const_mat_test.mate(n);
 
   220   const_mat_test.dualValue();
 
   221   const_mat_test.nodeValue(n);
 
   222   const_mat_test.blossomNum();
 
   223   const_mat_test.blossomSize(k);
 
   224   const_mat_test.blossomValue(k);
 
   227 void checkMatching(const SmartGraph& graph,
 
   228                    const MaxMatching<SmartGraph>& mm) {
 
   231   IntNodeMap comp_index(graph);
 
   232   UnionFind<IntNodeMap> comp(comp_index);
 
   236   for (NodeIt n(graph); n != INVALID; ++n) {
 
   237     check(mm.status(n) == MaxMatching<SmartGraph>::EVEN ||
 
   238           mm.matching(n) != INVALID, "Wrong Gallai-Edmonds decomposition");
 
   239     if (mm.status(n) == MaxMatching<SmartGraph>::ODD) {
 
   246   for (EdgeIt e(graph); e != INVALID; ++e) {
 
   247     if (mm.matching(e)) {
 
   248       check(e == mm.matching(graph.u(e)), "Wrong matching");
 
   249       check(e == mm.matching(graph.v(e)), "Wrong matching");
 
   252     check(mm.status(graph.u(e)) != MaxMatching<SmartGraph>::EVEN ||
 
   253           mm.status(graph.v(e)) != MaxMatching<SmartGraph>::MATCHED,
 
   254           "Wrong Gallai-Edmonds decomposition");
 
   256     check(mm.status(graph.v(e)) != MaxMatching<SmartGraph>::EVEN ||
 
   257           mm.status(graph.u(e)) != MaxMatching<SmartGraph>::MATCHED,
 
   258           "Wrong Gallai-Edmonds decomposition");
 
   260     if (mm.status(graph.u(e)) != MaxMatching<SmartGraph>::ODD &&
 
   261         mm.status(graph.v(e)) != MaxMatching<SmartGraph>::ODD) {
 
   262       comp.join(graph.u(e), graph.v(e));
 
   266   std::set<int> comp_root;
 
   267   int odd_comp_num = 0;
 
   268   for (NodeIt n(graph); n != INVALID; ++n) {
 
   269     if (mm.status(n) != MaxMatching<SmartGraph>::ODD) {
 
   270       int root = comp.find(n);
 
   271       if (comp_root.find(root) == comp_root.end()) {
 
   272         comp_root.insert(root);
 
   273         if (comp.size(n) % 2 == 1) {
 
   280   check(mm.matchingSize() == num, "Wrong matching");
 
   281   check(2 * num == countNodes(graph) - (odd_comp_num - barrier_num),
 
   286 void checkWeightedMatching(const SmartGraph& graph,
 
   287                    const SmartGraph::EdgeMap<int>& weight,
 
   288                    const MaxWeightedMatching<SmartGraph>& mwm) {
 
   289   for (SmartGraph::EdgeIt e(graph); e != INVALID; ++e) {
 
   290     if (graph.u(e) == graph.v(e)) continue;
 
   291     int rw = mwm.nodeValue(graph.u(e)) + mwm.nodeValue(graph.v(e));
 
   293     for (int i = 0; i < mwm.blossomNum(); ++i) {
 
   294       bool s = false, t = false;
 
   295       for (MaxWeightedMatching<SmartGraph>::BlossomIt n(mwm, i);
 
   297         if (graph.u(e) == n) s = true;
 
   298         if (graph.v(e) == n) t = true;
 
   300       if (s == true && t == true) {
 
   301         rw += mwm.blossomValue(i);
 
   304     rw -= weight[e] * mwm.dualScale;
 
   306     check(rw >= 0, "Negative reduced weight");
 
   307     check(rw == 0 || !mwm.matching(e),
 
   308           "Non-zero reduced weight on matching edge");
 
   312   for (SmartGraph::NodeIt n(graph); n != INVALID; ++n) {
 
   313     if (mwm.matching(n) != INVALID) {
 
   314       check(mwm.nodeValue(n) >= 0, "Invalid node value");
 
   315       pv += weight[mwm.matching(n)];
 
   316       SmartGraph::Node o = graph.target(mwm.matching(n));
 
   317       check(mwm.mate(n) == o, "Invalid matching");
 
   318       check(mwm.matching(n) == graph.oppositeArc(mwm.matching(o)),
 
   321       check(mwm.mate(n) == INVALID, "Invalid matching");
 
   322       check(mwm.nodeValue(n) == 0, "Invalid matching");
 
   327   for (SmartGraph::NodeIt n(graph); n != INVALID; ++n) {
 
   328     dv += mwm.nodeValue(n);
 
   331   for (int i = 0; i < mwm.blossomNum(); ++i) {
 
   332     check(mwm.blossomValue(i) >= 0, "Invalid blossom value");
 
   333     check(mwm.blossomSize(i) % 2 == 1, "Even blossom size");
 
   334     dv += mwm.blossomValue(i) * ((mwm.blossomSize(i) - 1) / 2);
 
   337   check(pv * mwm.dualScale == dv * 2, "Wrong duality");
 
   342 void checkWeightedPerfectMatching(const SmartGraph& graph,
 
   343                           const SmartGraph::EdgeMap<int>& weight,
 
   344                           const MaxWeightedPerfectMatching<SmartGraph>& mwpm) {
 
   345   for (SmartGraph::EdgeIt e(graph); e != INVALID; ++e) {
 
   346     if (graph.u(e) == graph.v(e)) continue;
 
   347     int rw = mwpm.nodeValue(graph.u(e)) + mwpm.nodeValue(graph.v(e));
 
   349     for (int i = 0; i < mwpm.blossomNum(); ++i) {
 
   350       bool s = false, t = false;
 
   351       for (MaxWeightedPerfectMatching<SmartGraph>::BlossomIt n(mwpm, i);
 
   353         if (graph.u(e) == n) s = true;
 
   354         if (graph.v(e) == n) t = true;
 
   356       if (s == true && t == true) {
 
   357         rw += mwpm.blossomValue(i);
 
   360     rw -= weight[e] * mwpm.dualScale;
 
   362     check(rw >= 0, "Negative reduced weight");
 
   363     check(rw == 0 || !mwpm.matching(e),
 
   364           "Non-zero reduced weight on matching edge");
 
   368   for (SmartGraph::NodeIt n(graph); n != INVALID; ++n) {
 
   369     check(mwpm.matching(n) != INVALID, "Non perfect");
 
   370     pv += weight[mwpm.matching(n)];
 
   371     SmartGraph::Node o = graph.target(mwpm.matching(n));
 
   372     check(mwpm.mate(n) == o, "Invalid matching");
 
   373     check(mwpm.matching(n) == graph.oppositeArc(mwpm.matching(o)),
 
   378   for (SmartGraph::NodeIt n(graph); n != INVALID; ++n) {
 
   379     dv += mwpm.nodeValue(n);
 
   382   for (int i = 0; i < mwpm.blossomNum(); ++i) {
 
   383     check(mwpm.blossomValue(i) >= 0, "Invalid blossom value");
 
   384     check(mwpm.blossomSize(i) % 2 == 1, "Even blossom size");
 
   385     dv += mwpm.blossomValue(i) * ((mwpm.blossomSize(i) - 1) / 2);
 
   388   check(pv * mwpm.dualScale == dv * 2, "Wrong duality");
 
   396   for (int i = 0; i < lgfn; ++i) {
 
   398     SmartGraph::EdgeMap<int> weight(graph);
 
   400     istringstream lgfs(lgf[i]);
 
   401     graphReader(graph, lgfs).
 
   402       edgeMap("weight", weight).run();
 
   404     MaxMatching<SmartGraph> mm(graph);
 
   406     checkMatching(graph, mm);
 
   408     MaxWeightedMatching<SmartGraph> mwm(graph, weight);
 
   410     checkWeightedMatching(graph, weight, mwm);
 
   412     MaxWeightedPerfectMatching<SmartGraph> mwpm(graph, weight);
 
   413     bool perfect = mwpm.run();
 
   415     check(perfect == (mm.matchingSize() * 2 == countNodes(graph)),
 
   416           "Perfect matching found");
 
   419       checkWeightedPerfectMatching(graph, weight, mwpm);