test/max_weighted_matching_test.cc
changeset 2549 88b81ec599ed
child 2553 bfced05fa852
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/test/max_weighted_matching_test.cc	Sat Dec 29 15:11:41 2007 +0000
     1.3 @@ -0,0 +1,253 @@
     1.4 +/* -*- C++ -*-
     1.5 + *
     1.6 + * This file is a part of LEMON, a generic C++ optimization library
     1.7 + *
     1.8 + * Copyright (C) 2003-2007
     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 <cmath>
    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/graph_reader.h>
    1.33 +
    1.34 +UGRAPH_TYPEDEFS(SmartUGraph);
    1.35 +
    1.36 +using namespace std;
    1.37 +using namespace lemon;
    1.38 +
    1.39 +const int lgfn = 3;
    1.40 +const std::string lgf[lgfn] = {   
    1.41 +  "@nodeset\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 +  "@uedgeset\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 +  "@end\n",
    1.69 +
    1.70 +  "@nodeset\n"
    1.71 +  "label\n"
    1.72 +  "0\n"
    1.73 +  "1\n"
    1.74 +  "2\n"
    1.75 +  "3\n"
    1.76 +  "4\n"
    1.77 +  "5\n"
    1.78 +  "6\n"
    1.79 +  "7\n"
    1.80 +  "@uedgeset\n"
    1.81 +  "label        weight\n"
    1.82 +  "2    5       0       710\n"
    1.83 +  "0    5       1       241\n"
    1.84 +  "2    4       2       856\n"
    1.85 +  "2    6       3       762\n"
    1.86 +  "4    1       4       747\n"
    1.87 +  "6    1       5       962\n"
    1.88 +  "4    7       6       723\n"
    1.89 +  "1    7       7       661\n"
    1.90 +  "2    3       8       376\n"
    1.91 +  "1    0       9       416\n"
    1.92 +  "6    7       10      391\n"
    1.93 +  "@end\n",
    1.94 +
    1.95 +  "@nodeset\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 +  "@uedgeset\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 +  "@end\n"
   1.115 +};
   1.116 +
   1.117 +void checkMatching(const SmartUGraph& ugraph,
   1.118 +		   const SmartUGraph::UEdgeMap<int>& weight,
   1.119 +		   const MaxWeightedMatching<SmartUGraph>& mwm) {
   1.120 +  for (SmartUGraph::UEdgeIt e(ugraph); e != INVALID; ++e) {
   1.121 +    if (ugraph.source(e) == ugraph.target(e)) continue;
   1.122 +    int rw = 
   1.123 +      mwm.nodeValue(ugraph.source(e)) + mwm.nodeValue(ugraph.target(e));
   1.124 +
   1.125 +    for (int i = 0; i < mwm.blossomNum(); ++i) {
   1.126 +      bool s = false, t = false;
   1.127 +      for (MaxWeightedMatching<SmartUGraph>::BlossomIt n(mwm, i); 
   1.128 +	   n != INVALID; ++n) {
   1.129 +	if (ugraph.source(e) == n) s = true;
   1.130 +	if (ugraph.target(e) == n) t = true;
   1.131 +      }
   1.132 +      if (s == true && t == true) {
   1.133 +	rw += mwm.blossomValue(i);
   1.134 +      }
   1.135 +    }
   1.136 +    rw -= weight[e] * mwm.dualScale;
   1.137 +
   1.138 +    check(rw >= 0, "Negative reduced weight");
   1.139 +    check(rw == 0 || !mwm.matching(e), 
   1.140 +	  "Non-zero reduced weight on matching edge");
   1.141 +  }
   1.142 +
   1.143 +  int pv = 0;
   1.144 +  for (SmartUGraph::NodeIt n(ugraph); n != INVALID; ++n) {
   1.145 +    if (mwm.matching(n) != INVALID) {
   1.146 +      check(mwm.nodeValue(n) >= 0, "Invalid node value");
   1.147 +      pv += weight[mwm.matching(n)];
   1.148 +      SmartUGraph::Node o = ugraph.target(mwm.matching(n));
   1.149 +      check(mwm.mate(n) == o, "Invalid matching");
   1.150 +      check(mwm.matching(n) == ugraph.oppositeEdge(mwm.matching(o)), 
   1.151 +	    "Invalid matching");
   1.152 +    } else {
   1.153 +      check(mwm.mate(n) == INVALID, "Invalid matching");
   1.154 +      check(mwm.nodeValue(n) == 0, "Invalid matching");
   1.155 +    }
   1.156 +  }
   1.157 +
   1.158 +  int dv = 0;
   1.159 +  for (SmartUGraph::NodeIt n(ugraph); n != INVALID; ++n) {
   1.160 +    dv += mwm.nodeValue(n);
   1.161 +  }
   1.162 +
   1.163 +  for (int i = 0; i < mwm.blossomNum(); ++i) {
   1.164 +    check(mwm.blossomValue(i) >= 0, "Invalid blossom value");
   1.165 +    check(mwm.blossomSize(i) % 2 == 1, "Even blossom size");
   1.166 +    dv += mwm.blossomValue(i) * ((mwm.blossomSize(i) - 1) / 2);
   1.167 +  }
   1.168 +
   1.169 +  check(pv * mwm.dualScale == dv * 2, "Wrong duality");
   1.170 +
   1.171 +  return;
   1.172 +}
   1.173 +
   1.174 +void checkPerfectMatching(const SmartUGraph& ugraph,
   1.175 +			  const SmartUGraph::UEdgeMap<int>& weight,
   1.176 +			  const MaxWeightedPerfectMatching<SmartUGraph>& mwpm) {
   1.177 +  for (SmartUGraph::UEdgeIt e(ugraph); e != INVALID; ++e) {
   1.178 +    if (ugraph.source(e) == ugraph.target(e)) continue;
   1.179 +    int rw = 
   1.180 +      mwpm.nodeValue(ugraph.source(e)) + mwpm.nodeValue(ugraph.target(e));
   1.181 +
   1.182 +    for (int i = 0; i < mwpm.blossomNum(); ++i) {
   1.183 +      bool s = false, t = false;
   1.184 +      for (MaxWeightedPerfectMatching<SmartUGraph>::BlossomIt n(mwpm, i); 
   1.185 +	   n != INVALID; ++n) {
   1.186 +	if (ugraph.source(e) == n) s = true;
   1.187 +	if (ugraph.target(e) == n) t = true;
   1.188 +      }
   1.189 +      if (s == true && t == true) {
   1.190 +	rw += mwpm.blossomValue(i);
   1.191 +      }
   1.192 +    }
   1.193 +    rw -= weight[e] * mwpm.dualScale;
   1.194 +
   1.195 +    check(rw >= 0, "Negative reduced weight");
   1.196 +    check(rw == 0 || !mwpm.matching(e), 
   1.197 +	  "Non-zero reduced weight on matching edge");
   1.198 +  }
   1.199 +
   1.200 +  int pv = 0;
   1.201 +  for (SmartUGraph::NodeIt n(ugraph); n != INVALID; ++n) {
   1.202 +    check(mwpm.matching(n) != INVALID, "Non perfect");
   1.203 +    pv += weight[mwpm.matching(n)];
   1.204 +    SmartUGraph::Node o = ugraph.target(mwpm.matching(n));
   1.205 +    check(mwpm.mate(n) == o, "Invalid matching");
   1.206 +    check(mwpm.matching(n) == ugraph.oppositeEdge(mwpm.matching(o)), 
   1.207 +	  "Invalid matching");
   1.208 +  }
   1.209 +
   1.210 +  int dv = 0;
   1.211 +  for (SmartUGraph::NodeIt n(ugraph); n != INVALID; ++n) {
   1.212 +    dv += mwpm.nodeValue(n);
   1.213 +  }
   1.214 +
   1.215 +  for (int i = 0; i < mwpm.blossomNum(); ++i) {
   1.216 +    check(mwpm.blossomValue(i) >= 0, "Invalid blossom value");
   1.217 +    check(mwpm.blossomSize(i) % 2 == 1, "Even blossom size");
   1.218 +    dv += mwpm.blossomValue(i) * ((mwpm.blossomSize(i) - 1) / 2);
   1.219 +  }
   1.220 +
   1.221 +  check(pv * mwpm.dualScale == dv * 2, "Wrong duality");
   1.222 +
   1.223 +  return;
   1.224 +}
   1.225 +
   1.226 +
   1.227 +int main() {
   1.228 +
   1.229 +  for (int i = 0; i < lgfn; ++i) {
   1.230 +    SmartUGraph ugraph;
   1.231 +    SmartUGraph::UEdgeMap<int> weight(ugraph);
   1.232 +    
   1.233 +    istringstream lgfs(lgf[i]);
   1.234 +    UGraphReader<SmartUGraph>(lgfs, ugraph).
   1.235 +      readUEdgeMap("weight", weight).run();
   1.236 +
   1.237 +    MaxWeightedMatching<SmartUGraph> mwm(ugraph, weight);
   1.238 +    mwm.run();
   1.239 +    checkMatching(ugraph, weight, mwm);
   1.240 +
   1.241 +    MaxMatching<SmartUGraph> mm(ugraph);
   1.242 +    mm.run();
   1.243 +
   1.244 +    MaxWeightedPerfectMatching<SmartUGraph> mwpm(ugraph, weight);
   1.245 +    bool perfect = mwpm.run();
   1.246 +
   1.247 +    check(perfect == (mm.size() * 2 == countNodes(ugraph)), 
   1.248 +	  "Perfect matching found");
   1.249 +
   1.250 +    if (perfect) {
   1.251 +      checkPerfectMatching(ugraph, weight, mwpm);
   1.252 +    }
   1.253 +  }
   1.254 +  
   1.255 +  return 0;
   1.256 +}