test/preflow_test.cc
author Peter Kovacs <kpeter@inf.elte.hu>
Fri, 12 Dec 2008 22:18:41 +0100
changeset 448 9d9990909fc8
parent 392 db3251947eba
child 423 ff48c2738fb2
permissions -rw-r--r--
Add missing const keywords (+ remove misleading ones) (#67)
alpar@389
     1
/* -*- mode: C++; indent-tabs-mode: nil; -*-
alpar@389
     2
 *
alpar@389
     3
 * This file is a part of LEMON, a generic C++ optimization library.
alpar@389
     4
 *
alpar@389
     5
 * Copyright (C) 2003-2008
alpar@389
     6
 * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
alpar@389
     7
 * (Egervary Research Group on Combinatorial Optimization, EGRES).
alpar@389
     8
 *
alpar@389
     9
 * Permission to use, modify and distribute this software is granted
alpar@389
    10
 * provided that this copyright notice appears in all copies. For
alpar@389
    11
 * precise terms see the accompanying LICENSE file.
alpar@389
    12
 *
alpar@389
    13
 * This software is provided "AS IS" with no warranty of any kind,
alpar@389
    14
 * express or implied, and with no claim as to its suitability for any
alpar@389
    15
 * purpose.
alpar@389
    16
 *
alpar@389
    17
 */
alpar@389
    18
alpar@389
    19
#include <fstream>
alpar@389
    20
#include <string>
alpar@389
    21
alpar@389
    22
#include "test_tools.h"
alpar@389
    23
#include <lemon/smart_graph.h>
alpar@389
    24
#include <lemon/preflow.h>
alpar@389
    25
#include <lemon/concepts/digraph.h>
alpar@389
    26
#include <lemon/concepts/maps.h>
alpar@389
    27
#include <lemon/lgf_reader.h>
kpeter@394
    28
#include <lemon/elevator.h>
alpar@389
    29
alpar@389
    30
using namespace lemon;
alpar@389
    31
kpeter@394
    32
void checkPreflowCompile()
alpar@389
    33
{
alpar@389
    34
  typedef int VType;
alpar@389
    35
  typedef concepts::Digraph Digraph;
alpar@389
    36
alpar@389
    37
  typedef Digraph::Node Node;
alpar@389
    38
  typedef Digraph::Arc Arc;
alpar@389
    39
  typedef concepts::ReadMap<Arc,VType> CapMap;
alpar@389
    40
  typedef concepts::ReadWriteMap<Arc,VType> FlowMap;
alpar@389
    41
  typedef concepts::WriteMap<Node,bool> CutMap;
alpar@389
    42
kpeter@394
    43
  typedef Elevator<Digraph, Digraph::Node> Elev;
kpeter@394
    44
  typedef LinkedElevator<Digraph, Digraph::Node> LinkedElev;
kpeter@394
    45
alpar@389
    46
  Digraph g;
alpar@389
    47
  Node n;
alpar@389
    48
  Arc e;
alpar@389
    49
  CapMap cap;
alpar@389
    50
  FlowMap flow;
alpar@389
    51
  CutMap cut;
alpar@389
    52
kpeter@394
    53
  Preflow<Digraph, CapMap>
kpeter@394
    54
    ::SetFlowMap<FlowMap>
kpeter@394
    55
    ::SetElevator<Elev>
kpeter@394
    56
    ::SetStandardElevator<LinkedElev>
kpeter@394
    57
    ::Create preflow_test(g,cap,n,n);
alpar@389
    58
alpar@389
    59
  preflow_test.capacityMap(cap);
alpar@389
    60
  flow = preflow_test.flowMap();
alpar@389
    61
  preflow_test.flowMap(flow);
alpar@389
    62
  preflow_test.source(n);
alpar@389
    63
  preflow_test.target(n);
alpar@389
    64
alpar@389
    65
  preflow_test.init();
kpeter@392
    66
  preflow_test.init(cap);
alpar@389
    67
  preflow_test.startFirstPhase();
alpar@389
    68
  preflow_test.startSecondPhase();
alpar@389
    69
  preflow_test.run();
alpar@389
    70
  preflow_test.runMinCut();
alpar@389
    71
alpar@389
    72
  preflow_test.flowValue();
alpar@389
    73
  preflow_test.minCut(n);
alpar@389
    74
  preflow_test.minCutMap(cut);
alpar@389
    75
  preflow_test.flow(e);
alpar@389
    76
alpar@389
    77
}
alpar@389
    78
alpar@389
    79
int cutValue (const SmartDigraph& g,
alpar@389
    80
              const SmartDigraph::NodeMap<bool>& cut,
alpar@389
    81
              const SmartDigraph::ArcMap<int>& cap) {
alpar@389
    82
alpar@389
    83
  int c=0;
alpar@389
    84
  for(SmartDigraph::ArcIt e(g); e!=INVALID; ++e) {
alpar@389
    85
    if (cut[g.source(e)] && !cut[g.target(e)]) c+=cap[e];
alpar@389
    86
  }
alpar@389
    87
  return c;
alpar@389
    88
}
alpar@389
    89
alpar@389
    90
bool checkFlow(const SmartDigraph& g,
alpar@389
    91
               const SmartDigraph::ArcMap<int>& flow,
alpar@389
    92
               const SmartDigraph::ArcMap<int>& cap,
alpar@389
    93
               SmartDigraph::Node s, SmartDigraph::Node t) {
alpar@389
    94
alpar@389
    95
  for (SmartDigraph::ArcIt e(g); e != INVALID; ++e) {
alpar@389
    96
    if (flow[e] < 0 || flow[e] > cap[e]) return false;
alpar@389
    97
  }
alpar@389
    98
alpar@389
    99
  for (SmartDigraph::NodeIt n(g); n != INVALID; ++n) {
alpar@389
   100
    if (n == s || n == t) continue;
alpar@389
   101
    int sum = 0;
alpar@389
   102
    for (SmartDigraph::OutArcIt e(g, n); e != INVALID; ++e) {
alpar@389
   103
      sum += flow[e];
alpar@389
   104
    }
alpar@389
   105
    for (SmartDigraph::InArcIt e(g, n); e != INVALID; ++e) {
alpar@389
   106
      sum -= flow[e];
alpar@389
   107
    }
alpar@389
   108
    if (sum != 0) return false;
alpar@389
   109
  }
alpar@389
   110
  return true;
alpar@389
   111
}
alpar@389
   112
alpar@389
   113
int main() {
alpar@389
   114
alpar@389
   115
  typedef SmartDigraph Digraph;
alpar@389
   116
alpar@389
   117
  typedef Digraph::Node Node;
alpar@389
   118
  typedef Digraph::NodeIt NodeIt;
alpar@389
   119
  typedef Digraph::ArcIt ArcIt;
alpar@389
   120
  typedef Digraph::ArcMap<int> CapMap;
alpar@389
   121
  typedef Digraph::ArcMap<int> FlowMap;
alpar@389
   122
  typedef Digraph::NodeMap<bool> CutMap;
alpar@389
   123
alpar@389
   124
  typedef Preflow<Digraph, CapMap> PType;
alpar@389
   125
alpar@389
   126
  std::string f_name;
alpar@389
   127
  if( getenv("srcdir") )
alpar@389
   128
    f_name = std::string(getenv("srcdir"));
alpar@389
   129
  else f_name = ".";
alpar@389
   130
  f_name += "/test/preflow_graph.lgf";
alpar@389
   131
alpar@389
   132
  std::ifstream file(f_name.c_str());
alpar@389
   133
alpar@389
   134
  check(file, "Input file '" << f_name << "' not found.");
alpar@389
   135
alpar@389
   136
  Digraph g;
alpar@389
   137
  Node s, t;
alpar@389
   138
  CapMap cap(g);
alpar@389
   139
  DigraphReader<Digraph>(g,file).
alpar@389
   140
    arcMap("capacity", cap).
alpar@389
   141
    node("source",s).
alpar@389
   142
    node("target",t).
alpar@389
   143
    run();
alpar@389
   144
alpar@389
   145
  PType preflow_test(g, cap, s, t);
alpar@389
   146
  preflow_test.run();
alpar@389
   147
alpar@389
   148
  check(checkFlow(g, preflow_test.flowMap(), cap, s, t),
alpar@389
   149
        "The flow is not feasible.");
alpar@389
   150
alpar@389
   151
  CutMap min_cut(g);
alpar@389
   152
  preflow_test.minCutMap(min_cut);
alpar@389
   153
  int min_cut_value=cutValue(g,min_cut,cap);
alpar@389
   154
alpar@389
   155
  check(preflow_test.flowValue() == min_cut_value,
alpar@389
   156
        "The max flow value is not equal to the three min cut values.");
alpar@389
   157
alpar@389
   158
  FlowMap flow(g);
alpar@389
   159
  for(ArcIt e(g); e!=INVALID; ++e) flow[e] = preflow_test.flowMap()[e];
alpar@389
   160
alpar@389
   161
  int flow_value=preflow_test.flowValue();
alpar@389
   162
alpar@389
   163
  for(ArcIt e(g); e!=INVALID; ++e) cap[e]=2*cap[e];
kpeter@392
   164
  preflow_test.init(flow);
alpar@389
   165
  preflow_test.startFirstPhase();
alpar@389
   166
alpar@389
   167
  CutMap min_cut1(g);
alpar@389
   168
  preflow_test.minCutMap(min_cut1);
alpar@389
   169
  min_cut_value=cutValue(g,min_cut1,cap);
alpar@389
   170
alpar@389
   171
  check(preflow_test.flowValue() == min_cut_value &&
alpar@389
   172
        min_cut_value == 2*flow_value,
alpar@389
   173
        "The max flow value or the min cut value is wrong.");
alpar@389
   174
alpar@389
   175
  preflow_test.startSecondPhase();
alpar@389
   176
alpar@389
   177
  check(checkFlow(g, preflow_test.flowMap(), cap, s, t),
alpar@389
   178
        "The flow is not feasible.");
alpar@389
   179
alpar@389
   180
  CutMap min_cut2(g);
alpar@389
   181
  preflow_test.minCutMap(min_cut2);
alpar@389
   182
  min_cut_value=cutValue(g,min_cut2,cap);
alpar@389
   183
alpar@389
   184
  check(preflow_test.flowValue() == min_cut_value &&
alpar@389
   185
        min_cut_value == 2*flow_value,
alpar@389
   186
        "The max flow value or the three min cut values were not doubled");
alpar@389
   187
alpar@389
   188
alpar@389
   189
  preflow_test.flowMap(flow);
alpar@389
   190
alpar@389
   191
  NodeIt tmp1(g,s);
alpar@389
   192
  ++tmp1;
alpar@389
   193
  if ( tmp1 != INVALID ) s=tmp1;
alpar@389
   194
alpar@389
   195
  NodeIt tmp2(g,t);
alpar@389
   196
  ++tmp2;
alpar@389
   197
  if ( tmp2 != INVALID ) t=tmp2;
alpar@389
   198
alpar@389
   199
  preflow_test.source(s);
alpar@389
   200
  preflow_test.target(t);
alpar@389
   201
alpar@389
   202
  preflow_test.run();
alpar@389
   203
alpar@389
   204
  CutMap min_cut3(g);
alpar@389
   205
  preflow_test.minCutMap(min_cut3);
alpar@389
   206
  min_cut_value=cutValue(g,min_cut3,cap);
alpar@389
   207
alpar@389
   208
alpar@389
   209
  check(preflow_test.flowValue() == min_cut_value,
alpar@389
   210
        "The max flow value or the three min cut values are incorrect.");
alpar@389
   211
alpar@389
   212
  return 0;
alpar@389
   213
}