test/fractional_matching_test.cc
changeset 869 636dadefe1e6
child 872 41d7ac528c3a
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/test/fractional_matching_test.cc	Fri Sep 25 21:51:36 2009 +0200
     1.3 @@ -0,0 +1,502 @@
     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/fractional_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 = 4;
    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 +  "@nodes\n"
   1.118 +  "label\n"
   1.119 +  "0\n"
   1.120 +  "@edges\n"
   1.121 +  "     label  weight\n"
   1.122 +  "0 0  0      100\n"
   1.123 +};
   1.124 +
   1.125 +void checkMaxFractionalMatchingCompile()
   1.126 +{
   1.127 +  typedef concepts::Graph Graph;
   1.128 +  typedef Graph::Node Node;
   1.129 +  typedef Graph::Edge Edge;
   1.130 +
   1.131 +  Graph g;
   1.132 +  Node n;
   1.133 +  Edge e;
   1.134 +
   1.135 +  MaxFractionalMatching<Graph> mat_test(g);
   1.136 +  const MaxFractionalMatching<Graph>&
   1.137 +    const_mat_test = mat_test;
   1.138 +
   1.139 +  mat_test.init();
   1.140 +  mat_test.start();
   1.141 +  mat_test.start(true);
   1.142 +  mat_test.startPerfect();
   1.143 +  mat_test.startPerfect(true);
   1.144 +  mat_test.run();
   1.145 +  mat_test.run(true);
   1.146 +  mat_test.runPerfect();
   1.147 +  mat_test.runPerfect(true);
   1.148 +
   1.149 +  const_mat_test.matchingSize();
   1.150 +  const_mat_test.matching(e);
   1.151 +  const_mat_test.matching(n);
   1.152 +  const MaxFractionalMatching<Graph>::MatchingMap& mmap =
   1.153 +    const_mat_test.matchingMap();
   1.154 +  e = mmap[n];
   1.155 +
   1.156 +  const_mat_test.barrier(n);
   1.157 +}
   1.158 +
   1.159 +void checkMaxWeightedFractionalMatchingCompile()
   1.160 +{
   1.161 +  typedef concepts::Graph Graph;
   1.162 +  typedef Graph::Node Node;
   1.163 +  typedef Graph::Edge Edge;
   1.164 +  typedef Graph::EdgeMap<int> WeightMap;
   1.165 +
   1.166 +  Graph g;
   1.167 +  Node n;
   1.168 +  Edge e;
   1.169 +  WeightMap w(g);
   1.170 +
   1.171 +  MaxWeightedFractionalMatching<Graph> mat_test(g, w);
   1.172 +  const MaxWeightedFractionalMatching<Graph>&
   1.173 +    const_mat_test = mat_test;
   1.174 +
   1.175 +  mat_test.init();
   1.176 +  mat_test.start();
   1.177 +  mat_test.run();
   1.178 +
   1.179 +  const_mat_test.matchingWeight();
   1.180 +  const_mat_test.matchingSize();
   1.181 +  const_mat_test.matching(e);
   1.182 +  const_mat_test.matching(n);
   1.183 +  const MaxWeightedFractionalMatching<Graph>::MatchingMap& mmap =
   1.184 +    const_mat_test.matchingMap();
   1.185 +  e = mmap[n];
   1.186 +
   1.187 +  const_mat_test.dualValue();
   1.188 +  const_mat_test.nodeValue(n);
   1.189 +}
   1.190 +
   1.191 +void checkMaxWeightedPerfectFractionalMatchingCompile()
   1.192 +{
   1.193 +  typedef concepts::Graph Graph;
   1.194 +  typedef Graph::Node Node;
   1.195 +  typedef Graph::Edge Edge;
   1.196 +  typedef Graph::EdgeMap<int> WeightMap;
   1.197 +
   1.198 +  Graph g;
   1.199 +  Node n;
   1.200 +  Edge e;
   1.201 +  WeightMap w(g);
   1.202 +
   1.203 +  MaxWeightedPerfectFractionalMatching<Graph> mat_test(g, w);
   1.204 +  const MaxWeightedPerfectFractionalMatching<Graph>&
   1.205 +    const_mat_test = mat_test;
   1.206 +
   1.207 +  mat_test.init();
   1.208 +  mat_test.start();
   1.209 +  mat_test.run();
   1.210 +
   1.211 +  const_mat_test.matchingWeight();
   1.212 +  const_mat_test.matching(e);
   1.213 +  const_mat_test.matching(n);
   1.214 +  const MaxWeightedPerfectFractionalMatching<Graph>::MatchingMap& mmap =
   1.215 +    const_mat_test.matchingMap();
   1.216 +  e = mmap[n];
   1.217 +
   1.218 +  const_mat_test.dualValue();
   1.219 +  const_mat_test.nodeValue(n);
   1.220 +}
   1.221 +
   1.222 +void checkFractionalMatching(const SmartGraph& graph,
   1.223 +                             const MaxFractionalMatching<SmartGraph>& mfm,
   1.224 +                             bool allow_loops = true) {
   1.225 +  int pv = 0;
   1.226 +  for (SmartGraph::NodeIt n(graph); n != INVALID; ++n) {
   1.227 +    int indeg = 0;
   1.228 +    for (InArcIt a(graph, n); a != INVALID; ++a) {
   1.229 +      if (mfm.matching(graph.source(a)) == a) {
   1.230 +        ++indeg;
   1.231 +      }
   1.232 +    }
   1.233 +    if (mfm.matching(n) != INVALID) {
   1.234 +      check(indeg == 1, "Invalid matching");
   1.235 +      ++pv;
   1.236 +    } else {
   1.237 +      check(indeg == 0, "Invalid matching");
   1.238 +    }
   1.239 +  }
   1.240 +  check(pv == mfm.matchingSize(), "Wrong matching size");
   1.241 +
   1.242 +  SmartGraph::NodeMap<bool> processed(graph, false);
   1.243 +  for (SmartGraph::NodeIt n(graph); n != INVALID; ++n) {
   1.244 +    if (processed[n]) continue;
   1.245 +    processed[n] = true;
   1.246 +    if (mfm.matching(n) == INVALID) continue;
   1.247 +    int num = 1;
   1.248 +    Node v = graph.target(mfm.matching(n));
   1.249 +    while (v != n) {
   1.250 +      processed[v] = true;
   1.251 +      ++num;
   1.252 +      v = graph.target(mfm.matching(v));
   1.253 +    }
   1.254 +    check(num == 2 || num % 2 == 1, "Wrong cycle size");
   1.255 +    check(allow_loops || num != 1, "Wrong cycle size");
   1.256 +  }
   1.257 +
   1.258 +  int anum = 0, bnum = 0;
   1.259 +  SmartGraph::NodeMap<bool> neighbours(graph, false);
   1.260 +  for (SmartGraph::NodeIt n(graph); n != INVALID; ++n) {
   1.261 +    if (!mfm.barrier(n)) continue;
   1.262 +    ++anum;
   1.263 +    for (SmartGraph::InArcIt a(graph, n); a != INVALID; ++a) {
   1.264 +      Node u = graph.source(a);
   1.265 +      if (!allow_loops && u == n) continue;
   1.266 +      if (!neighbours[u]) {
   1.267 +        neighbours[u] = true;
   1.268 +        ++bnum;
   1.269 +      }
   1.270 +    }
   1.271 +  }
   1.272 +  check(anum - bnum + mfm.matchingSize() == countNodes(graph),
   1.273 +        "Wrong barrier");
   1.274 +}
   1.275 +
   1.276 +void checkPerfectFractionalMatching(const SmartGraph& graph,
   1.277 +                             const MaxFractionalMatching<SmartGraph>& mfm,
   1.278 +                             bool perfect, bool allow_loops = true) {
   1.279 +  if (perfect) {
   1.280 +    for (SmartGraph::NodeIt n(graph); n != INVALID; ++n) {
   1.281 +      int indeg = 0;
   1.282 +      for (InArcIt a(graph, n); a != INVALID; ++a) {
   1.283 +        if (mfm.matching(graph.source(a)) == a) {
   1.284 +          ++indeg;
   1.285 +        }
   1.286 +      }
   1.287 +      check(mfm.matching(n) != INVALID, "Invalid matching");
   1.288 +      check(indeg == 1, "Invalid matching");
   1.289 +    }
   1.290 +  } else {
   1.291 +    int anum = 0, bnum = 0;
   1.292 +    SmartGraph::NodeMap<bool> neighbours(graph, false);
   1.293 +    for (SmartGraph::NodeIt n(graph); n != INVALID; ++n) {
   1.294 +      if (!mfm.barrier(n)) continue;
   1.295 +      ++anum;
   1.296 +      for (SmartGraph::InArcIt a(graph, n); a != INVALID; ++a) {
   1.297 +        Node u = graph.source(a);
   1.298 +        if (!allow_loops && u == n) continue;
   1.299 +        if (!neighbours[u]) {
   1.300 +          neighbours[u] = true;
   1.301 +          ++bnum;
   1.302 +        }
   1.303 +      }
   1.304 +    }
   1.305 +    check(anum - bnum > 0, "Wrong barrier");
   1.306 +  }
   1.307 +}
   1.308 +
   1.309 +void checkWeightedFractionalMatching(const SmartGraph& graph,
   1.310 +                   const SmartGraph::EdgeMap<int>& weight,
   1.311 +                   const MaxWeightedFractionalMatching<SmartGraph>& mwfm,
   1.312 +                   bool allow_loops = true) {
   1.313 +  for (SmartGraph::EdgeIt e(graph); e != INVALID; ++e) {
   1.314 +    if (graph.u(e) == graph.v(e) && !allow_loops) continue;
   1.315 +    int rw = mwfm.nodeValue(graph.u(e)) + mwfm.nodeValue(graph.v(e))
   1.316 +      - weight[e] * mwfm.dualScale;
   1.317 +
   1.318 +    check(rw >= 0, "Negative reduced weight");
   1.319 +    check(rw == 0 || !mwfm.matching(e),
   1.320 +          "Non-zero reduced weight on matching edge");
   1.321 +  }
   1.322 +
   1.323 +  int pv = 0;
   1.324 +  for (SmartGraph::NodeIt n(graph); n != INVALID; ++n) {
   1.325 +    int indeg = 0;
   1.326 +    for (InArcIt a(graph, n); a != INVALID; ++a) {
   1.327 +      if (mwfm.matching(graph.source(a)) == a) {
   1.328 +        ++indeg;
   1.329 +      }
   1.330 +    }
   1.331 +    check(indeg <= 1, "Invalid matching");
   1.332 +    if (mwfm.matching(n) != INVALID) {
   1.333 +      check(mwfm.nodeValue(n) >= 0, "Invalid node value");
   1.334 +      check(indeg == 1, "Invalid matching");
   1.335 +      pv += weight[mwfm.matching(n)];
   1.336 +      SmartGraph::Node o = graph.target(mwfm.matching(n));
   1.337 +    } else {
   1.338 +      check(mwfm.nodeValue(n) == 0, "Invalid matching");
   1.339 +      check(indeg == 0, "Invalid matching");
   1.340 +    }
   1.341 +  }
   1.342 +
   1.343 +  int dv = 0;
   1.344 +  for (SmartGraph::NodeIt n(graph); n != INVALID; ++n) {
   1.345 +    dv += mwfm.nodeValue(n);
   1.346 +  }
   1.347 +
   1.348 +  check(pv * mwfm.dualScale == dv * 2, "Wrong duality");
   1.349 +
   1.350 +  SmartGraph::NodeMap<bool> processed(graph, false);
   1.351 +  for (SmartGraph::NodeIt n(graph); n != INVALID; ++n) {
   1.352 +    if (processed[n]) continue;
   1.353 +    processed[n] = true;
   1.354 +    if (mwfm.matching(n) == INVALID) continue;
   1.355 +    int num = 1;
   1.356 +    Node v = graph.target(mwfm.matching(n));
   1.357 +    while (v != n) {
   1.358 +      processed[v] = true;
   1.359 +      ++num;
   1.360 +      v = graph.target(mwfm.matching(v));
   1.361 +    }
   1.362 +    check(num == 2 || num % 2 == 1, "Wrong cycle size");
   1.363 +    check(allow_loops || num != 1, "Wrong cycle size");
   1.364 +  }
   1.365 +
   1.366 +  return;
   1.367 +}
   1.368 +
   1.369 +void checkWeightedPerfectFractionalMatching(const SmartGraph& graph,
   1.370 +                const SmartGraph::EdgeMap<int>& weight,
   1.371 +                const MaxWeightedPerfectFractionalMatching<SmartGraph>& mwpfm,
   1.372 +                bool allow_loops = true) {
   1.373 +  for (SmartGraph::EdgeIt e(graph); e != INVALID; ++e) {
   1.374 +    if (graph.u(e) == graph.v(e) && !allow_loops) continue;
   1.375 +    int rw = mwpfm.nodeValue(graph.u(e)) + mwpfm.nodeValue(graph.v(e))
   1.376 +      - weight[e] * mwpfm.dualScale;
   1.377 +
   1.378 +    check(rw >= 0, "Negative reduced weight");
   1.379 +    check(rw == 0 || !mwpfm.matching(e),
   1.380 +          "Non-zero reduced weight on matching edge");
   1.381 +  }
   1.382 +
   1.383 +  int pv = 0;
   1.384 +  for (SmartGraph::NodeIt n(graph); n != INVALID; ++n) {
   1.385 +    int indeg = 0;
   1.386 +    for (InArcIt a(graph, n); a != INVALID; ++a) {
   1.387 +      if (mwpfm.matching(graph.source(a)) == a) {
   1.388 +        ++indeg;
   1.389 +      }
   1.390 +    }
   1.391 +    check(mwpfm.matching(n) != INVALID, "Invalid perfect matching");
   1.392 +    check(indeg == 1, "Invalid perfect matching");
   1.393 +    pv += weight[mwpfm.matching(n)];
   1.394 +    SmartGraph::Node o = graph.target(mwpfm.matching(n));
   1.395 +  }
   1.396 +
   1.397 +  int dv = 0;
   1.398 +  for (SmartGraph::NodeIt n(graph); n != INVALID; ++n) {
   1.399 +    dv += mwpfm.nodeValue(n);
   1.400 +  }
   1.401 +
   1.402 +  check(pv * mwpfm.dualScale == dv * 2, "Wrong duality");
   1.403 +
   1.404 +  SmartGraph::NodeMap<bool> processed(graph, false);
   1.405 +  for (SmartGraph::NodeIt n(graph); n != INVALID; ++n) {
   1.406 +    if (processed[n]) continue;
   1.407 +    processed[n] = true;
   1.408 +    if (mwpfm.matching(n) == INVALID) continue;
   1.409 +    int num = 1;
   1.410 +    Node v = graph.target(mwpfm.matching(n));
   1.411 +    while (v != n) {
   1.412 +      processed[v] = true;
   1.413 +      ++num;
   1.414 +      v = graph.target(mwpfm.matching(v));
   1.415 +    }
   1.416 +    check(num == 2 || num % 2 == 1, "Wrong cycle size");
   1.417 +    check(allow_loops || num != 1, "Wrong cycle size");
   1.418 +  }
   1.419 +
   1.420 +  return;
   1.421 +}
   1.422 +
   1.423 +
   1.424 +int main() {
   1.425 +
   1.426 +  for (int i = 0; i < lgfn; ++i) {
   1.427 +    SmartGraph graph;
   1.428 +    SmartGraph::EdgeMap<int> weight(graph);
   1.429 +
   1.430 +    istringstream lgfs(lgf[i]);
   1.431 +    graphReader(graph, lgfs).
   1.432 +      edgeMap("weight", weight).run();
   1.433 +
   1.434 +    bool perfect_with_loops;
   1.435 +    {
   1.436 +      MaxFractionalMatching<SmartGraph> mfm(graph, true);
   1.437 +      mfm.run();
   1.438 +      checkFractionalMatching(graph, mfm, true);
   1.439 +      perfect_with_loops = mfm.matchingSize() == countNodes(graph);
   1.440 +    }
   1.441 +
   1.442 +    bool perfect_without_loops;
   1.443 +    {
   1.444 +      MaxFractionalMatching<SmartGraph> mfm(graph, false);
   1.445 +      mfm.run();
   1.446 +      checkFractionalMatching(graph, mfm, false);
   1.447 +      perfect_without_loops = mfm.matchingSize() == countNodes(graph);
   1.448 +    }
   1.449 +
   1.450 +    {
   1.451 +      MaxFractionalMatching<SmartGraph> mfm(graph, true);
   1.452 +      bool result = mfm.runPerfect();
   1.453 +      checkPerfectFractionalMatching(graph, mfm, result, true);
   1.454 +      check(result == perfect_with_loops, "Wrong perfect matching");
   1.455 +    }
   1.456 +
   1.457 +    {
   1.458 +      MaxFractionalMatching<SmartGraph> mfm(graph, false);
   1.459 +      bool result = mfm.runPerfect();
   1.460 +      checkPerfectFractionalMatching(graph, mfm, result, false);
   1.461 +      check(result == perfect_without_loops, "Wrong perfect matching");
   1.462 +    }
   1.463 +
   1.464 +    {
   1.465 +      MaxWeightedFractionalMatching<SmartGraph> mwfm(graph, weight, true);
   1.466 +      mwfm.run();
   1.467 +      checkWeightedFractionalMatching(graph, weight, mwfm, true);
   1.468 +    }
   1.469 +
   1.470 +    {
   1.471 +      MaxWeightedFractionalMatching<SmartGraph> mwfm(graph, weight, false);
   1.472 +      mwfm.run();
   1.473 +      checkWeightedFractionalMatching(graph, weight, mwfm, false);
   1.474 +    }
   1.475 +
   1.476 +    {
   1.477 +      MaxWeightedPerfectFractionalMatching<SmartGraph> mwpfm(graph, weight,
   1.478 +                                                             true);
   1.479 +      bool perfect = mwpfm.run();
   1.480 +      check(perfect == (mwpfm.matchingSize() == countNodes(graph)),
   1.481 +            "Perfect matching found");
   1.482 +      check(perfect == perfect_with_loops, "Wrong perfect matching");
   1.483 +
   1.484 +      if (perfect) {
   1.485 +        checkWeightedPerfectFractionalMatching(graph, weight, mwpfm, true);
   1.486 +      }
   1.487 +    }
   1.488 +
   1.489 +    {
   1.490 +      MaxWeightedPerfectFractionalMatching<SmartGraph> mwpfm(graph, weight,
   1.491 +                                                             false);
   1.492 +      bool perfect = mwpfm.run();
   1.493 +      check(perfect == (mwpfm.matchingSize() == countNodes(graph)),
   1.494 +            "Perfect matching found");
   1.495 +      check(perfect == perfect_without_loops, "Wrong perfect matching");
   1.496 +
   1.497 +      if (perfect) {
   1.498 +        checkWeightedPerfectFractionalMatching(graph, weight, mwpfm, false);
   1.499 +      }
   1.500 +    }
   1.501 +
   1.502 +  }
   1.503 +
   1.504 +  return 0;
   1.505 +}