test/max_matching_test.cc
changeset 345 9c2a532aa5ef
parent 338 64ad48007fb2
child 463 88ed40ad0d4f
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/test/max_matching_test.cc	Wed Oct 22 14:41:18 2008 +0100
     1.3 @@ -0,0 +1,310 @@
     1.4 +/* -*- mode: C++; indent-tabs-mode: nil; -*-
     1.5 + *
     1.6 + * This file is a part of LEMON, a generic C++ optimization library.
     1.7 + *
     1.8 + * Copyright (C) 2003-2008
     1.9 + * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
    1.10 + * (Egervary Research Group on Combinatorial Optimization, EGRES).
    1.11 + *
    1.12 + * Permission to use, modify and distribute this software is granted
    1.13 + * provided that this copyright notice appears in all copies. For
    1.14 + * precise terms see the accompanying LICENSE file.
    1.15 + *
    1.16 + * This software is provided "AS IS" with no warranty of any kind,
    1.17 + * express or implied, and with no claim as to its suitability for any
    1.18 + * purpose.
    1.19 + *
    1.20 + */
    1.21 +
    1.22 +#include <iostream>
    1.23 +#include <sstream>
    1.24 +#include <vector>
    1.25 +#include <queue>
    1.26 +#include <lemon/math.h>
    1.27 +#include <cstdlib>
    1.28 +
    1.29 +#include <lemon/max_matching.h>
    1.30 +#include <lemon/smart_graph.h>
    1.31 +#include <lemon/lgf_reader.h>
    1.32 +
    1.33 +#include "test_tools.h"
    1.34 +
    1.35 +using namespace std;
    1.36 +using namespace lemon;
    1.37 +
    1.38 +GRAPH_TYPEDEFS(SmartGraph);
    1.39 +
    1.40 +
    1.41 +const int lgfn = 3;
    1.42 +const std::string lgf[lgfn] = {
    1.43 +  "@nodes\n"
    1.44 +  "label\n"
    1.45 +  "0\n"
    1.46 +  "1\n"
    1.47 +  "2\n"
    1.48 +  "3\n"
    1.49 +  "4\n"
    1.50 +  "5\n"
    1.51 +  "6\n"
    1.52 +  "7\n"
    1.53 +  "@edges\n"
    1.54 +  "     label  weight\n"
    1.55 +  "7 4  0      984\n"
    1.56 +  "0 7  1      73\n"
    1.57 +  "7 1  2      204\n"
    1.58 +  "2 3  3      583\n"
    1.59 +  "2 7  4      565\n"
    1.60 +  "2 1  5      582\n"
    1.61 +  "0 4  6      551\n"
    1.62 +  "2 5  7      385\n"
    1.63 +  "1 5  8      561\n"
    1.64 +  "5 3  9      484\n"
    1.65 +  "7 5  10     904\n"
    1.66 +  "3 6  11     47\n"
    1.67 +  "7 6  12     888\n"
    1.68 +  "3 0  13     747\n"
    1.69 +  "6 1  14     310\n",
    1.70 +
    1.71 +  "@nodes\n"
    1.72 +  "label\n"
    1.73 +  "0\n"
    1.74 +  "1\n"
    1.75 +  "2\n"
    1.76 +  "3\n"
    1.77 +  "4\n"
    1.78 +  "5\n"
    1.79 +  "6\n"
    1.80 +  "7\n"
    1.81 +  "@edges\n"
    1.82 +  "     label  weight\n"
    1.83 +  "2 5  0      710\n"
    1.84 +  "0 5  1      241\n"
    1.85 +  "2 4  2      856\n"
    1.86 +  "2 6  3      762\n"
    1.87 +  "4 1  4      747\n"
    1.88 +  "6 1  5      962\n"
    1.89 +  "4 7  6      723\n"
    1.90 +  "1 7  7      661\n"
    1.91 +  "2 3  8      376\n"
    1.92 +  "1 0  9      416\n"
    1.93 +  "6 7  10     391\n",
    1.94 +
    1.95 +  "@nodes\n"
    1.96 +  "label\n"
    1.97 +  "0\n"
    1.98 +  "1\n"
    1.99 +  "2\n"
   1.100 +  "3\n"
   1.101 +  "4\n"
   1.102 +  "5\n"
   1.103 +  "6\n"
   1.104 +  "7\n"
   1.105 +  "@edges\n"
   1.106 +  "     label  weight\n"
   1.107 +  "6 2  0      553\n"
   1.108 +  "0 7  1      653\n"
   1.109 +  "6 3  2      22\n"
   1.110 +  "4 7  3      846\n"
   1.111 +  "7 2  4      981\n"
   1.112 +  "7 6  5      250\n"
   1.113 +  "5 2  6      539\n",
   1.114 +};
   1.115 +
   1.116 +void checkMatching(const SmartGraph& graph,
   1.117 +                   const MaxMatching<SmartGraph>& mm) {
   1.118 +  int num = 0;
   1.119 +
   1.120 +  IntNodeMap comp_index(graph);
   1.121 +  UnionFind<IntNodeMap> comp(comp_index);
   1.122 +
   1.123 +  int barrier_num = 0;
   1.124 +
   1.125 +  for (NodeIt n(graph); n != INVALID; ++n) {
   1.126 +    check(mm.decomposition(n) == MaxMatching<SmartGraph>::EVEN ||
   1.127 +          mm.matching(n) != INVALID, "Wrong Gallai-Edmonds decomposition");
   1.128 +    if (mm.decomposition(n) == MaxMatching<SmartGraph>::ODD) {
   1.129 +      ++barrier_num;
   1.130 +    } else {
   1.131 +      comp.insert(n);
   1.132 +    }
   1.133 +  }
   1.134 +
   1.135 +  for (EdgeIt e(graph); e != INVALID; ++e) {
   1.136 +    if (mm.matching(e)) {
   1.137 +      check(e == mm.matching(graph.u(e)), "Wrong matching");
   1.138 +      check(e == mm.matching(graph.v(e)), "Wrong matching");
   1.139 +      ++num;
   1.140 +    }
   1.141 +    check(mm.decomposition(graph.u(e)) != MaxMatching<SmartGraph>::EVEN ||
   1.142 +          mm.decomposition(graph.v(e)) != MaxMatching<SmartGraph>::MATCHED,
   1.143 +          "Wrong Gallai-Edmonds decomposition");
   1.144 +
   1.145 +    check(mm.decomposition(graph.v(e)) != MaxMatching<SmartGraph>::EVEN ||
   1.146 +          mm.decomposition(graph.u(e)) != MaxMatching<SmartGraph>::MATCHED,
   1.147 +          "Wrong Gallai-Edmonds decomposition");
   1.148 +
   1.149 +    if (mm.decomposition(graph.u(e)) != MaxMatching<SmartGraph>::ODD &&
   1.150 +        mm.decomposition(graph.v(e)) != MaxMatching<SmartGraph>::ODD) {
   1.151 +      comp.join(graph.u(e), graph.v(e));
   1.152 +    }
   1.153 +  }
   1.154 +
   1.155 +  std::set<int> comp_root;
   1.156 +  int odd_comp_num = 0;
   1.157 +  for (NodeIt n(graph); n != INVALID; ++n) {
   1.158 +    if (mm.decomposition(n) != MaxMatching<SmartGraph>::ODD) {
   1.159 +      int root = comp.find(n);
   1.160 +      if (comp_root.find(root) == comp_root.end()) {
   1.161 +        comp_root.insert(root);
   1.162 +        if (comp.size(n) % 2 == 1) {
   1.163 +          ++odd_comp_num;
   1.164 +        }
   1.165 +      }
   1.166 +    }
   1.167 +  }
   1.168 +
   1.169 +  check(mm.matchingSize() == num, "Wrong matching");
   1.170 +  check(2 * num == countNodes(graph) - (odd_comp_num - barrier_num),
   1.171 +         "Wrong matching");
   1.172 +  return;
   1.173 +}
   1.174 +
   1.175 +void checkWeightedMatching(const SmartGraph& graph,
   1.176 +                   const SmartGraph::EdgeMap<int>& weight,
   1.177 +                   const MaxWeightedMatching<SmartGraph>& mwm) {
   1.178 +  for (SmartGraph::EdgeIt e(graph); e != INVALID; ++e) {
   1.179 +    if (graph.u(e) == graph.v(e)) continue;
   1.180 +    int rw = mwm.nodeValue(graph.u(e)) + mwm.nodeValue(graph.v(e));
   1.181 +
   1.182 +    for (int i = 0; i < mwm.blossomNum(); ++i) {
   1.183 +      bool s = false, t = false;
   1.184 +      for (MaxWeightedMatching<SmartGraph>::BlossomIt n(mwm, i);
   1.185 +           n != INVALID; ++n) {
   1.186 +        if (graph.u(e) == n) s = true;
   1.187 +        if (graph.v(e) == n) t = true;
   1.188 +      }
   1.189 +      if (s == true && t == true) {
   1.190 +        rw += mwm.blossomValue(i);
   1.191 +      }
   1.192 +    }
   1.193 +    rw -= weight[e] * mwm.dualScale;
   1.194 +
   1.195 +    check(rw >= 0, "Negative reduced weight");
   1.196 +    check(rw == 0 || !mwm.matching(e),
   1.197 +          "Non-zero reduced weight on matching edge");
   1.198 +  }
   1.199 +
   1.200 +  int pv = 0;
   1.201 +  for (SmartGraph::NodeIt n(graph); n != INVALID; ++n) {
   1.202 +    if (mwm.matching(n) != INVALID) {
   1.203 +      check(mwm.nodeValue(n) >= 0, "Invalid node value");
   1.204 +      pv += weight[mwm.matching(n)];
   1.205 +      SmartGraph::Node o = graph.target(mwm.matching(n));
   1.206 +      check(mwm.mate(n) == o, "Invalid matching");
   1.207 +      check(mwm.matching(n) == graph.oppositeArc(mwm.matching(o)),
   1.208 +            "Invalid matching");
   1.209 +    } else {
   1.210 +      check(mwm.mate(n) == INVALID, "Invalid matching");
   1.211 +      check(mwm.nodeValue(n) == 0, "Invalid matching");
   1.212 +    }
   1.213 +  }
   1.214 +
   1.215 +  int dv = 0;
   1.216 +  for (SmartGraph::NodeIt n(graph); n != INVALID; ++n) {
   1.217 +    dv += mwm.nodeValue(n);
   1.218 +  }
   1.219 +
   1.220 +  for (int i = 0; i < mwm.blossomNum(); ++i) {
   1.221 +    check(mwm.blossomValue(i) >= 0, "Invalid blossom value");
   1.222 +    check(mwm.blossomSize(i) % 2 == 1, "Even blossom size");
   1.223 +    dv += mwm.blossomValue(i) * ((mwm.blossomSize(i) - 1) / 2);
   1.224 +  }
   1.225 +
   1.226 +  check(pv * mwm.dualScale == dv * 2, "Wrong duality");
   1.227 +
   1.228 +  return;
   1.229 +}
   1.230 +
   1.231 +void checkWeightedPerfectMatching(const SmartGraph& graph,
   1.232 +                          const SmartGraph::EdgeMap<int>& weight,
   1.233 +                          const MaxWeightedPerfectMatching<SmartGraph>& mwpm) {
   1.234 +  for (SmartGraph::EdgeIt e(graph); e != INVALID; ++e) {
   1.235 +    if (graph.u(e) == graph.v(e)) continue;
   1.236 +    int rw = mwpm.nodeValue(graph.u(e)) + mwpm.nodeValue(graph.v(e));
   1.237 +
   1.238 +    for (int i = 0; i < mwpm.blossomNum(); ++i) {
   1.239 +      bool s = false, t = false;
   1.240 +      for (MaxWeightedPerfectMatching<SmartGraph>::BlossomIt n(mwpm, i);
   1.241 +           n != INVALID; ++n) {
   1.242 +        if (graph.u(e) == n) s = true;
   1.243 +        if (graph.v(e) == n) t = true;
   1.244 +      }
   1.245 +      if (s == true && t == true) {
   1.246 +        rw += mwpm.blossomValue(i);
   1.247 +      }
   1.248 +    }
   1.249 +    rw -= weight[e] * mwpm.dualScale;
   1.250 +
   1.251 +    check(rw >= 0, "Negative reduced weight");
   1.252 +    check(rw == 0 || !mwpm.matching(e),
   1.253 +          "Non-zero reduced weight on matching edge");
   1.254 +  }
   1.255 +
   1.256 +  int pv = 0;
   1.257 +  for (SmartGraph::NodeIt n(graph); n != INVALID; ++n) {
   1.258 +    check(mwpm.matching(n) != INVALID, "Non perfect");
   1.259 +    pv += weight[mwpm.matching(n)];
   1.260 +    SmartGraph::Node o = graph.target(mwpm.matching(n));
   1.261 +    check(mwpm.mate(n) == o, "Invalid matching");
   1.262 +    check(mwpm.matching(n) == graph.oppositeArc(mwpm.matching(o)),
   1.263 +          "Invalid matching");
   1.264 +  }
   1.265 +
   1.266 +  int dv = 0;
   1.267 +  for (SmartGraph::NodeIt n(graph); n != INVALID; ++n) {
   1.268 +    dv += mwpm.nodeValue(n);
   1.269 +  }
   1.270 +
   1.271 +  for (int i = 0; i < mwpm.blossomNum(); ++i) {
   1.272 +    check(mwpm.blossomValue(i) >= 0, "Invalid blossom value");
   1.273 +    check(mwpm.blossomSize(i) % 2 == 1, "Even blossom size");
   1.274 +    dv += mwpm.blossomValue(i) * ((mwpm.blossomSize(i) - 1) / 2);
   1.275 +  }
   1.276 +
   1.277 +  check(pv * mwpm.dualScale == dv * 2, "Wrong duality");
   1.278 +
   1.279 +  return;
   1.280 +}
   1.281 +
   1.282 +
   1.283 +int main() {
   1.284 +
   1.285 +  for (int i = 0; i < lgfn; ++i) {
   1.286 +    SmartGraph graph;
   1.287 +    SmartGraph::EdgeMap<int> weight(graph);
   1.288 +
   1.289 +    istringstream lgfs(lgf[i]);
   1.290 +    graphReader(graph, lgfs).
   1.291 +      edgeMap("weight", weight).run();
   1.292 +
   1.293 +    MaxMatching<SmartGraph> mm(graph);
   1.294 +    mm.run();
   1.295 +    checkMatching(graph, mm);
   1.296 +
   1.297 +    MaxWeightedMatching<SmartGraph> mwm(graph, weight);
   1.298 +    mwm.run();
   1.299 +    checkWeightedMatching(graph, weight, mwm);
   1.300 +
   1.301 +    MaxWeightedPerfectMatching<SmartGraph> mwpm(graph, weight);
   1.302 +    bool perfect = mwpm.run();
   1.303 +
   1.304 +    check(perfect == (mm.matchingSize() * 2 == countNodes(graph)),
   1.305 +          "Perfect matching found");
   1.306 +
   1.307 +    if (perfect) {
   1.308 +      checkWeightedPerfectMatching(graph, weight, mwpm);
   1.309 +    }
   1.310 +  }
   1.311 +
   1.312 +  return 0;
   1.313 +}