test/edmonds_karp_test.cc
changeset 1224 92a884824429
child 1226 2f00ef323c2e
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/test/edmonds_karp_test.cc	Tue Nov 30 20:21:52 2010 +0100
     1.3 @@ -0,0 +1,236 @@
     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-2010
     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 +
    1.24 +#include "test_tools.h"
    1.25 +#include<lemon/smart_graph.h>
    1.26 +#include<lemon/edmonds_karp.h>
    1.27 +#include <lemon/concepts/digraph.h>
    1.28 +#include <lemon/concepts/maps.h>
    1.29 +#include <lemon/lgf_reader.h>
    1.30 +
    1.31 +using namespace lemon;
    1.32 +
    1.33 +char test_lgf[] =
    1.34 +  "@nodes\n"
    1.35 +  "label\n"
    1.36 +  "0\n"
    1.37 +  "1\n"
    1.38 +  "2\n"
    1.39 +  "3\n"
    1.40 +  "4\n"
    1.41 +  "5\n"
    1.42 +  "6\n"
    1.43 +  "7\n"
    1.44 +  "8\n"
    1.45 +  "9\n"
    1.46 +  "@arcs\n"
    1.47 +  "    label capacity\n"
    1.48 +  "0 1 0     20\n"
    1.49 +  "0 2 1     0\n"
    1.50 +  "1 1 2     3\n"
    1.51 +  "1 2 3     8\n"
    1.52 +  "1 3 4     8\n"
    1.53 +  "2 5 5     5\n"
    1.54 +  "3 2 6     5\n"
    1.55 +  "3 5 7     5\n"
    1.56 +  "3 6 8     5\n"
    1.57 +  "4 3 9     3\n"
    1.58 +  "5 7 10    3\n"
    1.59 +  "5 6 11    10\n"
    1.60 +  "5 8 12    10\n"
    1.61 +  "6 8 13    8\n"
    1.62 +  "8 9 14    20\n"
    1.63 +  "8 1 15    5\n"
    1.64 +  "9 5 16    5\n"
    1.65 +  "@attributes\n"
    1.66 +  "source 1\n"
    1.67 +  "target 8\n";
    1.68 +
    1.69 +void checkEdmondKarpCompile() {
    1.70 +  typedef int VType;
    1.71 +  typedef concepts::Digraph Digraph;
    1.72 +
    1.73 +  typedef Digraph::Node Node;
    1.74 +  typedef Digraph::Arc Arc;
    1.75 +  typedef concepts::ReadMap<Arc,VType> CapMap;
    1.76 +  typedef concepts::ReadWriteMap<Arc,VType> FlowMap;
    1.77 +  typedef concepts::WriteMap<Node,bool> CutMap;
    1.78 +
    1.79 +  Digraph g;
    1.80 +  Node n;
    1.81 +  Arc e;
    1.82 +  CapMap cap;
    1.83 +  FlowMap flow;
    1.84 +  CutMap cut;
    1.85 +  VType v;
    1.86 +  bool b;
    1.87 +  ignore_unused_variable_warning(v,b);
    1.88 +  typedef EdmondsKarp<Digraph, CapMap>
    1.89 +              ::DefFlowMap<FlowMap>
    1.90 +              ::Create EKType;
    1.91 +  EKType ek_test(g, cap, n, n);
    1.92 +  const EKType& const_ek_test = ek_test;
    1.93 +
    1.94 +  EKType::Tolerance tol = const_ek_test.tolerance();
    1.95 +  ek_test.tolerance(tol);
    1.96 +
    1.97 +  ek_test
    1.98 +    .capacityMap(cap)
    1.99 +    .flowMap(flow)
   1.100 +    .source(n)
   1.101 +    .target(n);
   1.102 +
   1.103 +  ek_test.init();
   1.104 +  ek_test.start();
   1.105 +
   1.106 +  v = const_ek_test.flowValue();
   1.107 +  v = const_ek_test.flow(e);
   1.108 +
   1.109 +  const FlowMap& fm = const_ek_test.flowMap();
   1.110 +  b = const_ek_test.minCut(n);
   1.111 +  const_ek_test.minCutMap(cut);
   1.112 +
   1.113 +  ignore_unused_variable_warning(fm);
   1.114 +}
   1.115 +
   1.116 +int cutValue (const SmartDigraph& g,
   1.117 +              const SmartDigraph::NodeMap<bool>& cut,
   1.118 +              const SmartDigraph::ArcMap<int>& cap) {
   1.119 +
   1.120 +  int c=0;
   1.121 +  for(SmartDigraph::ArcIt e(g); e!=INVALID; ++e) {
   1.122 +    if (cut[g.source(e)] && !cut[g.target(e)]) c+=cap[e];
   1.123 +  }
   1.124 +  return c;
   1.125 +}
   1.126 +
   1.127 +bool checkFlow(const SmartDigraph& g,
   1.128 +               const SmartDigraph::ArcMap<int>& flow,
   1.129 +               const SmartDigraph::ArcMap<int>& cap,
   1.130 +               SmartDigraph::Node s, SmartDigraph::Node t) {
   1.131 +
   1.132 +  for (SmartDigraph::ArcIt e(g); e != INVALID; ++e) {
   1.133 +    if (flow[e] < 0 || flow[e] > cap[e]) return false;
   1.134 +  }
   1.135 +
   1.136 +  for (SmartDigraph::NodeIt n(g); n != INVALID; ++n) {
   1.137 +    if (n == s || n == t) continue;
   1.138 +    int sum = 0;
   1.139 +    for (SmartDigraph::OutArcIt e(g, n); e != INVALID; ++e) {
   1.140 +      sum += flow[e];
   1.141 +    }
   1.142 +    for (SmartDigraph::InArcIt e(g, n); e != INVALID; ++e) {
   1.143 +      sum -= flow[e];
   1.144 +    }
   1.145 +    if (sum != 0) return false;
   1.146 +  }
   1.147 +  return true;
   1.148 +}
   1.149 +
   1.150 +int main() {
   1.151 +
   1.152 +  typedef SmartDigraph Digraph;
   1.153 +
   1.154 +  typedef Digraph::Node Node;
   1.155 +  typedef Digraph::NodeIt NodeIt;
   1.156 +  typedef Digraph::ArcIt ArcIt;
   1.157 +  typedef Digraph::ArcMap<int> CapMap;
   1.158 +  typedef Digraph::ArcMap<int> FlowMap;
   1.159 +  typedef Digraph::NodeMap<bool> CutMap;
   1.160 +
   1.161 +  typedef EdmondsKarp<Digraph, CapMap> EKType;
   1.162 +
   1.163 +  Digraph g;
   1.164 +  Node s, t;
   1.165 +  CapMap cap(g);
   1.166 +  std::istringstream input(test_lgf);
   1.167 +  DigraphReader<Digraph>(g,input).
   1.168 +    arcMap("capacity", cap).
   1.169 +    node("source",s).
   1.170 +    node("target",t).
   1.171 +    run();
   1.172 +
   1.173 +  EKType ek_test(g, cap, s, t);
   1.174 +  ek_test.run();
   1.175 +
   1.176 +  check(checkFlow(g, ek_test.flowMap(), cap, s, t),
   1.177 +        "The flow is not feasible.");
   1.178 +
   1.179 +  CutMap min_cut(g);
   1.180 +  ek_test.minCutMap(min_cut);
   1.181 +  int min_cut_value=cutValue(g,min_cut,cap);
   1.182 +
   1.183 +  check(ek_test.flowValue() == min_cut_value,
   1.184 +        "The max flow value is not equal to the three min cut values.");
   1.185 +
   1.186 +  FlowMap flow(g);
   1.187 +  for(ArcIt e(g); e!=INVALID; ++e) flow[e] = ek_test.flowMap()[e];
   1.188 +
   1.189 +  int flow_value=ek_test.flowValue();
   1.190 +
   1.191 +  for(ArcIt e(g); e!=INVALID; ++e) cap[e]=2*cap[e];
   1.192 +  ek_test.flowInit(flow);
   1.193 +  ek_test.start();
   1.194 +
   1.195 +  CutMap min_cut1(g);
   1.196 +  ek_test.minCutMap(min_cut1);
   1.197 +  min_cut_value=cutValue(g,min_cut1,cap);
   1.198 +
   1.199 +  check(ek_test.flowValue() == min_cut_value &&
   1.200 +        min_cut_value == 2*flow_value,
   1.201 +        "The max flow value or the min cut value is wrong.");
   1.202 +
   1.203 +  check(checkFlow(g, ek_test.flowMap(), cap, s, t),
   1.204 +        "The flow is not feasible.");
   1.205 +
   1.206 +  CutMap min_cut2(g);
   1.207 +  ek_test.minCutMap(min_cut2);
   1.208 +  min_cut_value=cutValue(g,min_cut2,cap);
   1.209 +
   1.210 +  check(ek_test.flowValue() == min_cut_value &&
   1.211 +        min_cut_value == 2*flow_value,
   1.212 +        "The max flow value or the three min cut values were not doubled.");
   1.213 +
   1.214 +
   1.215 +  ek_test.flowMap(flow);
   1.216 +
   1.217 +  NodeIt tmp1(g,s);
   1.218 +  ++tmp1;
   1.219 +  if ( tmp1 != INVALID ) s=tmp1;
   1.220 +
   1.221 +  NodeIt tmp2(g,t);
   1.222 +  ++tmp2;
   1.223 +  if ( tmp2 != INVALID ) t=tmp2;
   1.224 +
   1.225 +  ek_test.source(s);
   1.226 +  ek_test.target(t);
   1.227 +
   1.228 +  ek_test.run();
   1.229 +
   1.230 +  CutMap min_cut3(g);
   1.231 +  ek_test.minCutMap(min_cut3);
   1.232 +  min_cut_value=cutValue(g,min_cut3,cap);
   1.233 +
   1.234 +
   1.235 +  check(ek_test.flowValue() == min_cut_value,
   1.236 +        "The max flow value or the three min cut values are incorrect.");
   1.237 +
   1.238 +  return 0;
   1.239 +}