test/matching_test.cc
changeset 594 d657c71db7db
parent 593 7ac52d6a268e
child 870 61120524af27
child 969 7e368d9b67f7
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/test/matching_test.cc	Fri Apr 17 09:58:50 2009 +0200
     1.3 @@ -0,0 +1,424 @@
     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-2009
     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 <cstdlib>
    1.27 +
    1.28 +#include <lemon/matching.h>
    1.29 +#include <lemon/smart_graph.h>
    1.30 +#include <lemon/concepts/graph.h>
    1.31 +#include <lemon/concepts/maps.h>
    1.32 +#include <lemon/lgf_reader.h>
    1.33 +#include <lemon/math.h>
    1.34 +
    1.35 +#include "test_tools.h"
    1.36 +
    1.37 +using namespace std;
    1.38 +using namespace lemon;
    1.39 +
    1.40 +GRAPH_TYPEDEFS(SmartGraph);
    1.41 +
    1.42 +
    1.43 +const int lgfn = 3;
    1.44 +const std::string lgf[lgfn] = {
    1.45 +  "@nodes\n"
    1.46 +  "label\n"
    1.47 +  "0\n"
    1.48 +  "1\n"
    1.49 +  "2\n"
    1.50 +  "3\n"
    1.51 +  "4\n"
    1.52 +  "5\n"
    1.53 +  "6\n"
    1.54 +  "7\n"
    1.55 +  "@edges\n"
    1.56 +  "     label  weight\n"
    1.57 +  "7 4  0      984\n"
    1.58 +  "0 7  1      73\n"
    1.59 +  "7 1  2      204\n"
    1.60 +  "2 3  3      583\n"
    1.61 +  "2 7  4      565\n"
    1.62 +  "2 1  5      582\n"
    1.63 +  "0 4  6      551\n"
    1.64 +  "2 5  7      385\n"
    1.65 +  "1 5  8      561\n"
    1.66 +  "5 3  9      484\n"
    1.67 +  "7 5  10     904\n"
    1.68 +  "3 6  11     47\n"
    1.69 +  "7 6  12     888\n"
    1.70 +  "3 0  13     747\n"
    1.71 +  "6 1  14     310\n",
    1.72 +
    1.73 +  "@nodes\n"
    1.74 +  "label\n"
    1.75 +  "0\n"
    1.76 +  "1\n"
    1.77 +  "2\n"
    1.78 +  "3\n"
    1.79 +  "4\n"
    1.80 +  "5\n"
    1.81 +  "6\n"
    1.82 +  "7\n"
    1.83 +  "@edges\n"
    1.84 +  "     label  weight\n"
    1.85 +  "2 5  0      710\n"
    1.86 +  "0 5  1      241\n"
    1.87 +  "2 4  2      856\n"
    1.88 +  "2 6  3      762\n"
    1.89 +  "4 1  4      747\n"
    1.90 +  "6 1  5      962\n"
    1.91 +  "4 7  6      723\n"
    1.92 +  "1 7  7      661\n"
    1.93 +  "2 3  8      376\n"
    1.94 +  "1 0  9      416\n"
    1.95 +  "6 7  10     391\n",
    1.96 +
    1.97 +  "@nodes\n"
    1.98 +  "label\n"
    1.99 +  "0\n"
   1.100 +  "1\n"
   1.101 +  "2\n"
   1.102 +  "3\n"
   1.103 +  "4\n"
   1.104 +  "5\n"
   1.105 +  "6\n"
   1.106 +  "7\n"
   1.107 +  "@edges\n"
   1.108 +  "     label  weight\n"
   1.109 +  "6 2  0      553\n"
   1.110 +  "0 7  1      653\n"
   1.111 +  "6 3  2      22\n"
   1.112 +  "4 7  3      846\n"
   1.113 +  "7 2  4      981\n"
   1.114 +  "7 6  5      250\n"
   1.115 +  "5 2  6      539\n",
   1.116 +};
   1.117 +
   1.118 +void checkMaxMatchingCompile()
   1.119 +{
   1.120 +  typedef concepts::Graph Graph;
   1.121 +  typedef Graph::Node Node;
   1.122 +  typedef Graph::Edge Edge;
   1.123 +  typedef Graph::EdgeMap<bool> MatMap;
   1.124 +
   1.125 +  Graph g;
   1.126 +  Node n;
   1.127 +  Edge e;
   1.128 +  MatMap mat(g);
   1.129 +
   1.130 +  MaxMatching<Graph> mat_test(g);
   1.131 +  const MaxMatching<Graph>&
   1.132 +    const_mat_test = mat_test;
   1.133 +
   1.134 +  mat_test.init();
   1.135 +  mat_test.greedyInit();
   1.136 +  mat_test.matchingInit(mat);
   1.137 +  mat_test.startSparse();
   1.138 +  mat_test.startDense();
   1.139 +  mat_test.run();
   1.140 +  
   1.141 +  const_mat_test.matchingSize();
   1.142 +  const_mat_test.matching(e);
   1.143 +  const_mat_test.matching(n);
   1.144 +  const MaxMatching<Graph>::MatchingMap& mmap =
   1.145 +    const_mat_test.matchingMap();
   1.146 +  e = mmap[n];
   1.147 +  const_mat_test.mate(n);
   1.148 +
   1.149 +  MaxMatching<Graph>::Status stat = 
   1.150 +    const_mat_test.status(n);
   1.151 +  const MaxMatching<Graph>::StatusMap& smap =
   1.152 +    const_mat_test.statusMap();
   1.153 +  stat = smap[n];
   1.154 +  const_mat_test.barrier(n);
   1.155 +}
   1.156 +
   1.157 +void checkMaxWeightedMatchingCompile()
   1.158 +{
   1.159 +  typedef concepts::Graph Graph;
   1.160 +  typedef Graph::Node Node;
   1.161 +  typedef Graph::Edge Edge;
   1.162 +  typedef Graph::EdgeMap<int> WeightMap;
   1.163 +
   1.164 +  Graph g;
   1.165 +  Node n;
   1.166 +  Edge e;
   1.167 +  WeightMap w(g);
   1.168 +
   1.169 +  MaxWeightedMatching<Graph> mat_test(g, w);
   1.170 +  const MaxWeightedMatching<Graph>&
   1.171 +    const_mat_test = mat_test;
   1.172 +
   1.173 +  mat_test.init();
   1.174 +  mat_test.start();
   1.175 +  mat_test.run();
   1.176 +  
   1.177 +  const_mat_test.matchingWeight();
   1.178 +  const_mat_test.matchingSize();
   1.179 +  const_mat_test.matching(e);
   1.180 +  const_mat_test.matching(n);
   1.181 +  const MaxWeightedMatching<Graph>::MatchingMap& mmap =
   1.182 +    const_mat_test.matchingMap();
   1.183 +  e = mmap[n];
   1.184 +  const_mat_test.mate(n);
   1.185 +  
   1.186 +  int k = 0;
   1.187 +  const_mat_test.dualValue();
   1.188 +  const_mat_test.nodeValue(n);
   1.189 +  const_mat_test.blossomNum();
   1.190 +  const_mat_test.blossomSize(k);
   1.191 +  const_mat_test.blossomValue(k);
   1.192 +}
   1.193 +
   1.194 +void checkMaxWeightedPerfectMatchingCompile()
   1.195 +{
   1.196 +  typedef concepts::Graph Graph;
   1.197 +  typedef Graph::Node Node;
   1.198 +  typedef Graph::Edge Edge;
   1.199 +  typedef Graph::EdgeMap<int> WeightMap;
   1.200 +
   1.201 +  Graph g;
   1.202 +  Node n;
   1.203 +  Edge e;
   1.204 +  WeightMap w(g);
   1.205 +
   1.206 +  MaxWeightedPerfectMatching<Graph> mat_test(g, w);
   1.207 +  const MaxWeightedPerfectMatching<Graph>&
   1.208 +    const_mat_test = mat_test;
   1.209 +
   1.210 +  mat_test.init();
   1.211 +  mat_test.start();
   1.212 +  mat_test.run();
   1.213 +  
   1.214 +  const_mat_test.matchingWeight();
   1.215 +  const_mat_test.matching(e);
   1.216 +  const_mat_test.matching(n);
   1.217 +  const MaxWeightedPerfectMatching<Graph>::MatchingMap& mmap =
   1.218 +    const_mat_test.matchingMap();
   1.219 +  e = mmap[n];
   1.220 +  const_mat_test.mate(n);
   1.221 +  
   1.222 +  int k = 0;
   1.223 +  const_mat_test.dualValue();
   1.224 +  const_mat_test.nodeValue(n);
   1.225 +  const_mat_test.blossomNum();
   1.226 +  const_mat_test.blossomSize(k);
   1.227 +  const_mat_test.blossomValue(k);
   1.228 +}
   1.229 +
   1.230 +void checkMatching(const SmartGraph& graph,
   1.231 +                   const MaxMatching<SmartGraph>& mm) {
   1.232 +  int num = 0;
   1.233 +
   1.234 +  IntNodeMap comp_index(graph);
   1.235 +  UnionFind<IntNodeMap> comp(comp_index);
   1.236 +
   1.237 +  int barrier_num = 0;
   1.238 +
   1.239 +  for (NodeIt n(graph); n != INVALID; ++n) {
   1.240 +    check(mm.status(n) == MaxMatching<SmartGraph>::EVEN ||
   1.241 +          mm.matching(n) != INVALID, "Wrong Gallai-Edmonds decomposition");
   1.242 +    if (mm.status(n) == MaxMatching<SmartGraph>::ODD) {
   1.243 +      ++barrier_num;
   1.244 +    } else {
   1.245 +      comp.insert(n);
   1.246 +    }
   1.247 +  }
   1.248 +
   1.249 +  for (EdgeIt e(graph); e != INVALID; ++e) {
   1.250 +    if (mm.matching(e)) {
   1.251 +      check(e == mm.matching(graph.u(e)), "Wrong matching");
   1.252 +      check(e == mm.matching(graph.v(e)), "Wrong matching");
   1.253 +      ++num;
   1.254 +    }
   1.255 +    check(mm.status(graph.u(e)) != MaxMatching<SmartGraph>::EVEN ||
   1.256 +          mm.status(graph.v(e)) != MaxMatching<SmartGraph>::MATCHED,
   1.257 +          "Wrong Gallai-Edmonds decomposition");
   1.258 +
   1.259 +    check(mm.status(graph.v(e)) != MaxMatching<SmartGraph>::EVEN ||
   1.260 +          mm.status(graph.u(e)) != MaxMatching<SmartGraph>::MATCHED,
   1.261 +          "Wrong Gallai-Edmonds decomposition");
   1.262 +
   1.263 +    if (mm.status(graph.u(e)) != MaxMatching<SmartGraph>::ODD &&
   1.264 +        mm.status(graph.v(e)) != MaxMatching<SmartGraph>::ODD) {
   1.265 +      comp.join(graph.u(e), graph.v(e));
   1.266 +    }
   1.267 +  }
   1.268 +
   1.269 +  std::set<int> comp_root;
   1.270 +  int odd_comp_num = 0;
   1.271 +  for (NodeIt n(graph); n != INVALID; ++n) {
   1.272 +    if (mm.status(n) != MaxMatching<SmartGraph>::ODD) {
   1.273 +      int root = comp.find(n);
   1.274 +      if (comp_root.find(root) == comp_root.end()) {
   1.275 +        comp_root.insert(root);
   1.276 +        if (comp.size(n) % 2 == 1) {
   1.277 +          ++odd_comp_num;
   1.278 +        }
   1.279 +      }
   1.280 +    }
   1.281 +  }
   1.282 +
   1.283 +  check(mm.matchingSize() == num, "Wrong matching");
   1.284 +  check(2 * num == countNodes(graph) - (odd_comp_num - barrier_num),
   1.285 +         "Wrong matching");
   1.286 +  return;
   1.287 +}
   1.288 +
   1.289 +void checkWeightedMatching(const SmartGraph& graph,
   1.290 +                   const SmartGraph::EdgeMap<int>& weight,
   1.291 +                   const MaxWeightedMatching<SmartGraph>& mwm) {
   1.292 +  for (SmartGraph::EdgeIt e(graph); e != INVALID; ++e) {
   1.293 +    if (graph.u(e) == graph.v(e)) continue;
   1.294 +    int rw = mwm.nodeValue(graph.u(e)) + mwm.nodeValue(graph.v(e));
   1.295 +
   1.296 +    for (int i = 0; i < mwm.blossomNum(); ++i) {
   1.297 +      bool s = false, t = false;
   1.298 +      for (MaxWeightedMatching<SmartGraph>::BlossomIt n(mwm, i);
   1.299 +           n != INVALID; ++n) {
   1.300 +        if (graph.u(e) == n) s = true;
   1.301 +        if (graph.v(e) == n) t = true;
   1.302 +      }
   1.303 +      if (s == true && t == true) {
   1.304 +        rw += mwm.blossomValue(i);
   1.305 +      }
   1.306 +    }
   1.307 +    rw -= weight[e] * mwm.dualScale;
   1.308 +
   1.309 +    check(rw >= 0, "Negative reduced weight");
   1.310 +    check(rw == 0 || !mwm.matching(e),
   1.311 +          "Non-zero reduced weight on matching edge");
   1.312 +  }
   1.313 +
   1.314 +  int pv = 0;
   1.315 +  for (SmartGraph::NodeIt n(graph); n != INVALID; ++n) {
   1.316 +    if (mwm.matching(n) != INVALID) {
   1.317 +      check(mwm.nodeValue(n) >= 0, "Invalid node value");
   1.318 +      pv += weight[mwm.matching(n)];
   1.319 +      SmartGraph::Node o = graph.target(mwm.matching(n));
   1.320 +      check(mwm.mate(n) == o, "Invalid matching");
   1.321 +      check(mwm.matching(n) == graph.oppositeArc(mwm.matching(o)),
   1.322 +            "Invalid matching");
   1.323 +    } else {
   1.324 +      check(mwm.mate(n) == INVALID, "Invalid matching");
   1.325 +      check(mwm.nodeValue(n) == 0, "Invalid matching");
   1.326 +    }
   1.327 +  }
   1.328 +
   1.329 +  int dv = 0;
   1.330 +  for (SmartGraph::NodeIt n(graph); n != INVALID; ++n) {
   1.331 +    dv += mwm.nodeValue(n);
   1.332 +  }
   1.333 +
   1.334 +  for (int i = 0; i < mwm.blossomNum(); ++i) {
   1.335 +    check(mwm.blossomValue(i) >= 0, "Invalid blossom value");
   1.336 +    check(mwm.blossomSize(i) % 2 == 1, "Even blossom size");
   1.337 +    dv += mwm.blossomValue(i) * ((mwm.blossomSize(i) - 1) / 2);
   1.338 +  }
   1.339 +
   1.340 +  check(pv * mwm.dualScale == dv * 2, "Wrong duality");
   1.341 +
   1.342 +  return;
   1.343 +}
   1.344 +
   1.345 +void checkWeightedPerfectMatching(const SmartGraph& graph,
   1.346 +                          const SmartGraph::EdgeMap<int>& weight,
   1.347 +                          const MaxWeightedPerfectMatching<SmartGraph>& mwpm) {
   1.348 +  for (SmartGraph::EdgeIt e(graph); e != INVALID; ++e) {
   1.349 +    if (graph.u(e) == graph.v(e)) continue;
   1.350 +    int rw = mwpm.nodeValue(graph.u(e)) + mwpm.nodeValue(graph.v(e));
   1.351 +
   1.352 +    for (int i = 0; i < mwpm.blossomNum(); ++i) {
   1.353 +      bool s = false, t = false;
   1.354 +      for (MaxWeightedPerfectMatching<SmartGraph>::BlossomIt n(mwpm, i);
   1.355 +           n != INVALID; ++n) {
   1.356 +        if (graph.u(e) == n) s = true;
   1.357 +        if (graph.v(e) == n) t = true;
   1.358 +      }
   1.359 +      if (s == true && t == true) {
   1.360 +        rw += mwpm.blossomValue(i);
   1.361 +      }
   1.362 +    }
   1.363 +    rw -= weight[e] * mwpm.dualScale;
   1.364 +
   1.365 +    check(rw >= 0, "Negative reduced weight");
   1.366 +    check(rw == 0 || !mwpm.matching(e),
   1.367 +          "Non-zero reduced weight on matching edge");
   1.368 +  }
   1.369 +
   1.370 +  int pv = 0;
   1.371 +  for (SmartGraph::NodeIt n(graph); n != INVALID; ++n) {
   1.372 +    check(mwpm.matching(n) != INVALID, "Non perfect");
   1.373 +    pv += weight[mwpm.matching(n)];
   1.374 +    SmartGraph::Node o = graph.target(mwpm.matching(n));
   1.375 +    check(mwpm.mate(n) == o, "Invalid matching");
   1.376 +    check(mwpm.matching(n) == graph.oppositeArc(mwpm.matching(o)),
   1.377 +          "Invalid matching");
   1.378 +  }
   1.379 +
   1.380 +  int dv = 0;
   1.381 +  for (SmartGraph::NodeIt n(graph); n != INVALID; ++n) {
   1.382 +    dv += mwpm.nodeValue(n);
   1.383 +  }
   1.384 +
   1.385 +  for (int i = 0; i < mwpm.blossomNum(); ++i) {
   1.386 +    check(mwpm.blossomValue(i) >= 0, "Invalid blossom value");
   1.387 +    check(mwpm.blossomSize(i) % 2 == 1, "Even blossom size");
   1.388 +    dv += mwpm.blossomValue(i) * ((mwpm.blossomSize(i) - 1) / 2);
   1.389 +  }
   1.390 +
   1.391 +  check(pv * mwpm.dualScale == dv * 2, "Wrong duality");
   1.392 +
   1.393 +  return;
   1.394 +}
   1.395 +
   1.396 +
   1.397 +int main() {
   1.398 +
   1.399 +  for (int i = 0; i < lgfn; ++i) {
   1.400 +    SmartGraph graph;
   1.401 +    SmartGraph::EdgeMap<int> weight(graph);
   1.402 +
   1.403 +    istringstream lgfs(lgf[i]);
   1.404 +    graphReader(graph, lgfs).
   1.405 +      edgeMap("weight", weight).run();
   1.406 +
   1.407 +    MaxMatching<SmartGraph> mm(graph);
   1.408 +    mm.run();
   1.409 +    checkMatching(graph, mm);
   1.410 +
   1.411 +    MaxWeightedMatching<SmartGraph> mwm(graph, weight);
   1.412 +    mwm.run();
   1.413 +    checkWeightedMatching(graph, weight, mwm);
   1.414 +
   1.415 +    MaxWeightedPerfectMatching<SmartGraph> mwpm(graph, weight);
   1.416 +    bool perfect = mwpm.run();
   1.417 +
   1.418 +    check(perfect == (mm.matchingSize() * 2 == countNodes(graph)),
   1.419 +          "Perfect matching found");
   1.420 +
   1.421 +    if (perfect) {
   1.422 +      checkWeightedPerfectMatching(graph, weight, mwpm);
   1.423 +    }
   1.424 +  }
   1.425 +
   1.426 +  return 0;
   1.427 +}