test/preflow_test.cc
changeset 389 660db48f324f
child 391 624e673efa76
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/test/preflow_test.cc	Fri Nov 21 14:11:29 2008 +0000
     1.3 @@ -0,0 +1,205 @@
     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 <fstream>
    1.23 +#include <string>
    1.24 +
    1.25 +#include "test_tools.h"
    1.26 +#include <lemon/smart_graph.h>
    1.27 +#include <lemon/preflow.h>
    1.28 +#include <lemon/concepts/digraph.h>
    1.29 +#include <lemon/concepts/maps.h>
    1.30 +#include <lemon/lgf_reader.h>
    1.31 +
    1.32 +using namespace lemon;
    1.33 +
    1.34 +void checkPreflow()
    1.35 +{
    1.36 +  typedef int VType;
    1.37 +  typedef concepts::Digraph Digraph;
    1.38 +
    1.39 +  typedef Digraph::Node Node;
    1.40 +  typedef Digraph::Arc Arc;
    1.41 +  typedef concepts::ReadMap<Arc,VType> CapMap;
    1.42 +  typedef concepts::ReadWriteMap<Arc,VType> FlowMap;
    1.43 +  typedef concepts::WriteMap<Node,bool> CutMap;
    1.44 +
    1.45 +  Digraph g;
    1.46 +  Node n;
    1.47 +  Arc e;
    1.48 +  CapMap cap;
    1.49 +  FlowMap flow;
    1.50 +  CutMap cut;
    1.51 +
    1.52 +  Preflow<Digraph, CapMap>::DefFlowMap<FlowMap>::Create preflow_test(g,cap,n,n);
    1.53 +
    1.54 +  preflow_test.capacityMap(cap);
    1.55 +  flow = preflow_test.flowMap();
    1.56 +  preflow_test.flowMap(flow);
    1.57 +  preflow_test.source(n);
    1.58 +  preflow_test.target(n);
    1.59 +
    1.60 +  preflow_test.init();
    1.61 +  preflow_test.flowInit(cap);
    1.62 +  preflow_test.startFirstPhase();
    1.63 +  preflow_test.startSecondPhase();
    1.64 +  preflow_test.run();
    1.65 +  preflow_test.runMinCut();
    1.66 +
    1.67 +  preflow_test.flowValue();
    1.68 +  preflow_test.minCut(n);
    1.69 +  preflow_test.minCutMap(cut);
    1.70 +  preflow_test.flow(e);
    1.71 +
    1.72 +}
    1.73 +
    1.74 +int cutValue (const SmartDigraph& g,
    1.75 +              const SmartDigraph::NodeMap<bool>& cut,
    1.76 +              const SmartDigraph::ArcMap<int>& cap) {
    1.77 +
    1.78 +  int c=0;
    1.79 +  for(SmartDigraph::ArcIt e(g); e!=INVALID; ++e) {
    1.80 +    if (cut[g.source(e)] && !cut[g.target(e)]) c+=cap[e];
    1.81 +  }
    1.82 +  return c;
    1.83 +}
    1.84 +
    1.85 +bool checkFlow(const SmartDigraph& g,
    1.86 +               const SmartDigraph::ArcMap<int>& flow,
    1.87 +               const SmartDigraph::ArcMap<int>& cap,
    1.88 +               SmartDigraph::Node s, SmartDigraph::Node t) {
    1.89 +
    1.90 +  for (SmartDigraph::ArcIt e(g); e != INVALID; ++e) {
    1.91 +    if (flow[e] < 0 || flow[e] > cap[e]) return false;
    1.92 +  }
    1.93 +
    1.94 +  for (SmartDigraph::NodeIt n(g); n != INVALID; ++n) {
    1.95 +    if (n == s || n == t) continue;
    1.96 +    int sum = 0;
    1.97 +    for (SmartDigraph::OutArcIt e(g, n); e != INVALID; ++e) {
    1.98 +      sum += flow[e];
    1.99 +    }
   1.100 +    for (SmartDigraph::InArcIt e(g, n); e != INVALID; ++e) {
   1.101 +      sum -= flow[e];
   1.102 +    }
   1.103 +    if (sum != 0) return false;
   1.104 +  }
   1.105 +  return true;
   1.106 +}
   1.107 +
   1.108 +int main() {
   1.109 +
   1.110 +  typedef SmartDigraph Digraph;
   1.111 +
   1.112 +  typedef Digraph::Node Node;
   1.113 +  typedef Digraph::NodeIt NodeIt;
   1.114 +  typedef Digraph::ArcIt ArcIt;
   1.115 +  typedef Digraph::ArcMap<int> CapMap;
   1.116 +  typedef Digraph::ArcMap<int> FlowMap;
   1.117 +  typedef Digraph::NodeMap<bool> CutMap;
   1.118 +
   1.119 +  typedef Preflow<Digraph, CapMap> PType;
   1.120 +
   1.121 +  std::string f_name;
   1.122 +  if( getenv("srcdir") )
   1.123 +    f_name = std::string(getenv("srcdir"));
   1.124 +  else f_name = ".";
   1.125 +  f_name += "/test/preflow_graph.lgf";
   1.126 +
   1.127 +  std::ifstream file(f_name.c_str());
   1.128 +
   1.129 +  check(file, "Input file '" << f_name << "' not found.");
   1.130 +
   1.131 +  Digraph g;
   1.132 +  Node s, t;
   1.133 +  CapMap cap(g);
   1.134 +  DigraphReader<Digraph>(g,file).
   1.135 +    arcMap("capacity", cap).
   1.136 +    node("source",s).
   1.137 +    node("target",t).
   1.138 +    run();
   1.139 +
   1.140 +  PType preflow_test(g, cap, s, t);
   1.141 +  preflow_test.run();
   1.142 +
   1.143 +  check(checkFlow(g, preflow_test.flowMap(), cap, s, t),
   1.144 +        "The flow is not feasible.");
   1.145 +
   1.146 +  CutMap min_cut(g);
   1.147 +  preflow_test.minCutMap(min_cut);
   1.148 +  int min_cut_value=cutValue(g,min_cut,cap);
   1.149 +
   1.150 +  check(preflow_test.flowValue() == min_cut_value,
   1.151 +        "The max flow value is not equal to the three min cut values.");
   1.152 +
   1.153 +  FlowMap flow(g);
   1.154 +  for(ArcIt e(g); e!=INVALID; ++e) flow[e] = preflow_test.flowMap()[e];
   1.155 +
   1.156 +  int flow_value=preflow_test.flowValue();
   1.157 +
   1.158 +  for(ArcIt e(g); e!=INVALID; ++e) cap[e]=2*cap[e];
   1.159 +  preflow_test.flowInit(flow);
   1.160 +  preflow_test.startFirstPhase();
   1.161 +
   1.162 +  CutMap min_cut1(g);
   1.163 +  preflow_test.minCutMap(min_cut1);
   1.164 +  min_cut_value=cutValue(g,min_cut1,cap);
   1.165 +
   1.166 +  check(preflow_test.flowValue() == min_cut_value &&
   1.167 +        min_cut_value == 2*flow_value,
   1.168 +        "The max flow value or the min cut value is wrong.");
   1.169 +
   1.170 +  preflow_test.startSecondPhase();
   1.171 +
   1.172 +  check(checkFlow(g, preflow_test.flowMap(), cap, s, t),
   1.173 +        "The flow is not feasible.");
   1.174 +
   1.175 +  CutMap min_cut2(g);
   1.176 +  preflow_test.minCutMap(min_cut2);
   1.177 +  min_cut_value=cutValue(g,min_cut2,cap);
   1.178 +
   1.179 +  check(preflow_test.flowValue() == min_cut_value &&
   1.180 +        min_cut_value == 2*flow_value,
   1.181 +        "The max flow value or the three min cut values were not doubled");
   1.182 +
   1.183 +
   1.184 +  preflow_test.flowMap(flow);
   1.185 +
   1.186 +  NodeIt tmp1(g,s);
   1.187 +  ++tmp1;
   1.188 +  if ( tmp1 != INVALID ) s=tmp1;
   1.189 +
   1.190 +  NodeIt tmp2(g,t);
   1.191 +  ++tmp2;
   1.192 +  if ( tmp2 != INVALID ) t=tmp2;
   1.193 +
   1.194 +  preflow_test.source(s);
   1.195 +  preflow_test.target(t);
   1.196 +
   1.197 +  preflow_test.run();
   1.198 +
   1.199 +  CutMap min_cut3(g);
   1.200 +  preflow_test.minCutMap(min_cut3);
   1.201 +  min_cut_value=cutValue(g,min_cut3,cap);
   1.202 +
   1.203 +
   1.204 +  check(preflow_test.flowValue() == min_cut_value,
   1.205 +        "The max flow value or the three min cut values are incorrect.");
   1.206 +
   1.207 +  return 0;
   1.208 +}