test/max_weighted_matching_test.cc
changeset 326 64ad48007fb2
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/test/max_weighted_matching_test.cc	Mon Oct 13 13:56:00 2008 +0200
     1.3 @@ -0,0 +1,250 @@
     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 "test_tools.h"
    1.30 +#include <lemon/smart_graph.h>
    1.31 +#include <lemon/max_matching.h>
    1.32 +#include <lemon/lgf_reader.h>
    1.33 +
    1.34 +using namespace std;
    1.35 +using namespace lemon;
    1.36 +
    1.37 +GRAPH_TYPEDEFS(SmartGraph);
    1.38 +
    1.39 +const int lgfn = 3;
    1.40 +const std::string lgf[lgfn] = {
    1.41 +  "@nodes\n"
    1.42 +  "label\n"
    1.43 +  "0\n"
    1.44 +  "1\n"
    1.45 +  "2\n"
    1.46 +  "3\n"
    1.47 +  "4\n"
    1.48 +  "5\n"
    1.49 +  "6\n"
    1.50 +  "7\n"
    1.51 +  "@edges\n"
    1.52 +  "label        weight\n"
    1.53 +  "7    4       0       984\n"
    1.54 +  "0    7       1       73\n"
    1.55 +  "7    1       2       204\n"
    1.56 +  "2    3       3       583\n"
    1.57 +  "2    7       4       565\n"
    1.58 +  "2    1       5       582\n"
    1.59 +  "0    4       6       551\n"
    1.60 +  "2    5       7       385\n"
    1.61 +  "1    5       8       561\n"
    1.62 +  "5    3       9       484\n"
    1.63 +  "7    5       10      904\n"
    1.64 +  "3    6       11      47\n"
    1.65 +  "7    6       12      888\n"
    1.66 +  "3    0       13      747\n"
    1.67 +  "6    1       14      310\n",
    1.68 +
    1.69 +  "@nodes\n"
    1.70 +  "label\n"
    1.71 +  "0\n"
    1.72 +  "1\n"
    1.73 +  "2\n"
    1.74 +  "3\n"
    1.75 +  "4\n"
    1.76 +  "5\n"
    1.77 +  "6\n"
    1.78 +  "7\n"
    1.79 +  "@edges\n"
    1.80 +  "label        weight\n"
    1.81 +  "2    5       0       710\n"
    1.82 +  "0    5       1       241\n"
    1.83 +  "2    4       2       856\n"
    1.84 +  "2    6       3       762\n"
    1.85 +  "4    1       4       747\n"
    1.86 +  "6    1       5       962\n"
    1.87 +  "4    7       6       723\n"
    1.88 +  "1    7       7       661\n"
    1.89 +  "2    3       8       376\n"
    1.90 +  "1    0       9       416\n"
    1.91 +  "6    7       10      391\n",
    1.92 +
    1.93 +  "@nodes\n"
    1.94 +  "label\n"
    1.95 +  "0\n"
    1.96 +  "1\n"
    1.97 +  "2\n"
    1.98 +  "3\n"
    1.99 +  "4\n"
   1.100 +  "5\n"
   1.101 +  "6\n"
   1.102 +  "7\n"
   1.103 +  "@edges\n"
   1.104 +  "label        weight\n"
   1.105 +  "6    2       0       553\n"
   1.106 +  "0    7       1       653\n"
   1.107 +  "6    3       2       22\n"
   1.108 +  "4    7       3       846\n"
   1.109 +  "7    2       4       981\n"
   1.110 +  "7    6       5       250\n"
   1.111 +  "5    2       6       539\n"
   1.112 +};
   1.113 +
   1.114 +void checkMatching(const SmartGraph& graph,
   1.115 +                   const SmartGraph::EdgeMap<int>& weight,
   1.116 +                   const MaxWeightedMatching<SmartGraph>& mwm) {
   1.117 +  for (SmartGraph::EdgeIt e(graph); e != INVALID; ++e) {
   1.118 +    if (graph.u(e) == graph.v(e)) continue;
   1.119 +    int rw =
   1.120 +      mwm.nodeValue(graph.u(e)) + mwm.nodeValue(graph.v(e));
   1.121 +
   1.122 +    for (int i = 0; i < mwm.blossomNum(); ++i) {
   1.123 +      bool s = false, t = false;
   1.124 +      for (MaxWeightedMatching<SmartGraph>::BlossomIt n(mwm, i);
   1.125 +           n != INVALID; ++n) {
   1.126 +        if (graph.u(e) == n) s = true;
   1.127 +        if (graph.v(e) == n) t = true;
   1.128 +      }
   1.129 +      if (s == true && t == true) {
   1.130 +        rw += mwm.blossomValue(i);
   1.131 +      }
   1.132 +    }
   1.133 +    rw -= weight[e] * mwm.dualScale;
   1.134 +
   1.135 +    check(rw >= 0, "Negative reduced weight");
   1.136 +    check(rw == 0 || !mwm.matching(e),
   1.137 +          "Non-zero reduced weight on matching arc");
   1.138 +  }
   1.139 +
   1.140 +  int pv = 0;
   1.141 +  for (SmartGraph::NodeIt n(graph); n != INVALID; ++n) {
   1.142 +    if (mwm.matching(n) != INVALID) {
   1.143 +      check(mwm.nodeValue(n) >= 0, "Invalid node value");
   1.144 +      pv += weight[mwm.matching(n)];
   1.145 +      SmartGraph::Node o = graph.target(mwm.matching(n));
   1.146 +      check(mwm.mate(n) == o, "Invalid matching");
   1.147 +      check(mwm.matching(n) == graph.oppositeArc(mwm.matching(o)),
   1.148 +            "Invalid matching");
   1.149 +    } else {
   1.150 +      check(mwm.mate(n) == INVALID, "Invalid matching");
   1.151 +      check(mwm.nodeValue(n) == 0, "Invalid matching");
   1.152 +    }
   1.153 +  }
   1.154 +
   1.155 +  int dv = 0;
   1.156 +  for (SmartGraph::NodeIt n(graph); n != INVALID; ++n) {
   1.157 +    dv += mwm.nodeValue(n);
   1.158 +  }
   1.159 +
   1.160 +  for (int i = 0; i < mwm.blossomNum(); ++i) {
   1.161 +    check(mwm.blossomValue(i) >= 0, "Invalid blossom value");
   1.162 +    check(mwm.blossomSize(i) % 2 == 1, "Even blossom size");
   1.163 +    dv += mwm.blossomValue(i) * ((mwm.blossomSize(i) - 1) / 2);
   1.164 +  }
   1.165 +
   1.166 +  check(pv * mwm.dualScale == dv * 2, "Wrong duality");
   1.167 +
   1.168 +  return;
   1.169 +}
   1.170 +
   1.171 +void checkPerfectMatching(const SmartGraph& graph,
   1.172 +                          const SmartGraph::EdgeMap<int>& weight,
   1.173 +                          const MaxWeightedPerfectMatching<SmartGraph>& mwpm) {
   1.174 +  for (SmartGraph::EdgeIt e(graph); e != INVALID; ++e) {
   1.175 +    if (graph.u(e) == graph.v(e)) continue;
   1.176 +    int rw =
   1.177 +      mwpm.nodeValue(graph.u(e)) + mwpm.nodeValue(graph.v(e));
   1.178 +
   1.179 +    for (int i = 0; i < mwpm.blossomNum(); ++i) {
   1.180 +      bool s = false, t = false;
   1.181 +      for (MaxWeightedPerfectMatching<SmartGraph>::BlossomIt n(mwpm, i);
   1.182 +           n != INVALID; ++n) {
   1.183 +        if (graph.u(e) == n) s = true;
   1.184 +        if (graph.v(e) == n) t = true;
   1.185 +      }
   1.186 +      if (s == true && t == true) {
   1.187 +        rw += mwpm.blossomValue(i);
   1.188 +      }
   1.189 +    }
   1.190 +    rw -= weight[e] * mwpm.dualScale;
   1.191 +
   1.192 +    check(rw >= 0, "Negative reduced weight");
   1.193 +    check(rw == 0 || !mwpm.matching(e),
   1.194 +          "Non-zero reduced weight on matching arc");
   1.195 +  }
   1.196 +
   1.197 +  int pv = 0;
   1.198 +  for (SmartGraph::NodeIt n(graph); n != INVALID; ++n) {
   1.199 +    check(mwpm.matching(n) != INVALID, "Non perfect");
   1.200 +    pv += weight[mwpm.matching(n)];
   1.201 +    SmartGraph::Node o = graph.target(mwpm.matching(n));
   1.202 +    check(mwpm.mate(n) == o, "Invalid matching");
   1.203 +    check(mwpm.matching(n) == graph.oppositeArc(mwpm.matching(o)),
   1.204 +          "Invalid matching");
   1.205 +  }
   1.206 +
   1.207 +  int dv = 0;
   1.208 +  for (SmartGraph::NodeIt n(graph); n != INVALID; ++n) {
   1.209 +    dv += mwpm.nodeValue(n);
   1.210 +  }
   1.211 +
   1.212 +  for (int i = 0; i < mwpm.blossomNum(); ++i) {
   1.213 +    check(mwpm.blossomValue(i) >= 0, "Invalid blossom value");
   1.214 +    check(mwpm.blossomSize(i) % 2 == 1, "Even blossom size");
   1.215 +    dv += mwpm.blossomValue(i) * ((mwpm.blossomSize(i) - 1) / 2);
   1.216 +  }
   1.217 +
   1.218 +  check(pv * mwpm.dualScale == dv * 2, "Wrong duality");
   1.219 +
   1.220 +  return;
   1.221 +}
   1.222 +
   1.223 +
   1.224 +int main() {
   1.225 +
   1.226 +  for (int i = 0; i < lgfn; ++i) {
   1.227 +    SmartGraph graph;
   1.228 +    SmartGraph::EdgeMap<int> weight(graph);
   1.229 +
   1.230 +    istringstream lgfs(lgf[i]);
   1.231 +    graphReader(graph, lgfs).
   1.232 +      edgeMap("weight", weight).run();
   1.233 +
   1.234 +    MaxWeightedMatching<SmartGraph> mwm(graph, weight);
   1.235 +    mwm.run();
   1.236 +    checkMatching(graph, weight, mwm);
   1.237 +
   1.238 +    MaxMatching<SmartGraph> mm(graph);
   1.239 +    mm.run();
   1.240 +
   1.241 +    MaxWeightedPerfectMatching<SmartGraph> mwpm(graph, weight);
   1.242 +    bool perfect = mwpm.run();
   1.243 +
   1.244 +    check(perfect == (mm.size() * 2 == countNodes(graph)),
   1.245 +          "Perfect matching found");
   1.246 +
   1.247 +    if (perfect) {
   1.248 +      checkPerfectMatching(graph, weight, mwpm);
   1.249 +    }
   1.250 +  }
   1.251 +
   1.252 +  return 0;
   1.253 +}