test/preflow_test.cc
author Peter Kovacs <kpeter@inf.elte.hu>
Sun, 30 Nov 2008 00:50:31 +0100
changeset 393 37054b67d807
parent 391 624e673efa76
child 394 e7707b3069f1
permissions -rw-r--r--
Many doc improvements for Preflow (#176)

- More precise doc for members.
- Add doc for public types.
- Hide the doc of the traits class parameter.
- Removing \author comments.
- Use \tparam for template parameters.
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>
alpar@389
    28
alpar@389
    29
using namespace lemon;
alpar@389
    30
alpar@389
    31
void checkPreflow()
alpar@389
    32
{
alpar@389
    33
  typedef int VType;
alpar@389
    34
  typedef concepts::Digraph Digraph;
alpar@389
    35
alpar@389
    36
  typedef Digraph::Node Node;
alpar@389
    37
  typedef Digraph::Arc Arc;
alpar@389
    38
  typedef concepts::ReadMap<Arc,VType> CapMap;
alpar@389
    39
  typedef concepts::ReadWriteMap<Arc,VType> FlowMap;
alpar@389
    40
  typedef concepts::WriteMap<Node,bool> CutMap;
alpar@389
    41
alpar@389
    42
  Digraph g;
alpar@389
    43
  Node n;
alpar@389
    44
  Arc e;
alpar@389
    45
  CapMap cap;
alpar@389
    46
  FlowMap flow;
alpar@389
    47
  CutMap cut;
alpar@389
    48
alpar@391
    49
  Preflow<Digraph, CapMap>::SetFlowMap<FlowMap>::Create preflow_test(g,cap,n,n);
alpar@389
    50
alpar@389
    51
  preflow_test.capacityMap(cap);
alpar@389
    52
  flow = preflow_test.flowMap();
alpar@389
    53
  preflow_test.flowMap(flow);
alpar@389
    54
  preflow_test.source(n);
alpar@389
    55
  preflow_test.target(n);
alpar@389
    56
alpar@389
    57
  preflow_test.init();
kpeter@392
    58
  preflow_test.init(cap);
alpar@389
    59
  preflow_test.startFirstPhase();
alpar@389
    60
  preflow_test.startSecondPhase();
alpar@389
    61
  preflow_test.run();
alpar@389
    62
  preflow_test.runMinCut();
alpar@389
    63
alpar@389
    64
  preflow_test.flowValue();
alpar@389
    65
  preflow_test.minCut(n);
alpar@389
    66
  preflow_test.minCutMap(cut);
alpar@389
    67
  preflow_test.flow(e);
alpar@389
    68
alpar@389
    69
}
alpar@389
    70
alpar@389
    71
int cutValue (const SmartDigraph& g,
alpar@389
    72
              const SmartDigraph::NodeMap<bool>& cut,
alpar@389
    73
              const SmartDigraph::ArcMap<int>& cap) {
alpar@389
    74
alpar@389
    75
  int c=0;
alpar@389
    76
  for(SmartDigraph::ArcIt e(g); e!=INVALID; ++e) {
alpar@389
    77
    if (cut[g.source(e)] && !cut[g.target(e)]) c+=cap[e];
alpar@389
    78
  }
alpar@389
    79
  return c;
alpar@389
    80
}
alpar@389
    81
alpar@389
    82
bool checkFlow(const SmartDigraph& g,
alpar@389
    83
               const SmartDigraph::ArcMap<int>& flow,
alpar@389
    84
               const SmartDigraph::ArcMap<int>& cap,
alpar@389
    85
               SmartDigraph::Node s, SmartDigraph::Node t) {
alpar@389
    86
alpar@389
    87
  for (SmartDigraph::ArcIt e(g); e != INVALID; ++e) {
alpar@389
    88
    if (flow[e] < 0 || flow[e] > cap[e]) return false;
alpar@389
    89
  }
alpar@389
    90
alpar@389
    91
  for (SmartDigraph::NodeIt n(g); n != INVALID; ++n) {
alpar@389
    92
    if (n == s || n == t) continue;
alpar@389
    93
    int sum = 0;
alpar@389
    94
    for (SmartDigraph::OutArcIt e(g, n); e != INVALID; ++e) {
alpar@389
    95
      sum += flow[e];
alpar@389
    96
    }
alpar@389
    97
    for (SmartDigraph::InArcIt e(g, n); e != INVALID; ++e) {
alpar@389
    98
      sum -= flow[e];
alpar@389
    99
    }
alpar@389
   100
    if (sum != 0) return false;
alpar@389
   101
  }
alpar@389
   102
  return true;
alpar@389
   103
}
alpar@389
   104
alpar@389
   105
int main() {
alpar@389
   106
alpar@389
   107
  typedef SmartDigraph Digraph;
alpar@389
   108
alpar@389
   109
  typedef Digraph::Node Node;
alpar@389
   110
  typedef Digraph::NodeIt NodeIt;
alpar@389
   111
  typedef Digraph::ArcIt ArcIt;
alpar@389
   112
  typedef Digraph::ArcMap<int> CapMap;
alpar@389
   113
  typedef Digraph::ArcMap<int> FlowMap;
alpar@389
   114
  typedef Digraph::NodeMap<bool> CutMap;
alpar@389
   115
alpar@389
   116
  typedef Preflow<Digraph, CapMap> PType;
alpar@389
   117
alpar@389
   118
  std::string f_name;
alpar@389
   119
  if( getenv("srcdir") )
alpar@389
   120
    f_name = std::string(getenv("srcdir"));
alpar@389
   121
  else f_name = ".";
alpar@389
   122
  f_name += "/test/preflow_graph.lgf";
alpar@389
   123
alpar@389
   124
  std::ifstream file(f_name.c_str());
alpar@389
   125
alpar@389
   126
  check(file, "Input file '" << f_name << "' not found.");
alpar@389
   127
alpar@389
   128
  Digraph g;
alpar@389
   129
  Node s, t;
alpar@389
   130
  CapMap cap(g);
alpar@389
   131
  DigraphReader<Digraph>(g,file).
alpar@389
   132
    arcMap("capacity", cap).
alpar@389
   133
    node("source",s).
alpar@389
   134
    node("target",t).
alpar@389
   135
    run();
alpar@389
   136
alpar@389
   137
  PType preflow_test(g, cap, s, t);
alpar@389
   138
  preflow_test.run();
alpar@389
   139
alpar@389
   140
  check(checkFlow(g, preflow_test.flowMap(), cap, s, t),
alpar@389
   141
        "The flow is not feasible.");
alpar@389
   142
alpar@389
   143
  CutMap min_cut(g);
alpar@389
   144
  preflow_test.minCutMap(min_cut);
alpar@389
   145
  int min_cut_value=cutValue(g,min_cut,cap);
alpar@389
   146
alpar@389
   147
  check(preflow_test.flowValue() == min_cut_value,
alpar@389
   148
        "The max flow value is not equal to the three min cut values.");
alpar@389
   149
alpar@389
   150
  FlowMap flow(g);
alpar@389
   151
  for(ArcIt e(g); e!=INVALID; ++e) flow[e] = preflow_test.flowMap()[e];
alpar@389
   152
alpar@389
   153
  int flow_value=preflow_test.flowValue();
alpar@389
   154
alpar@389
   155
  for(ArcIt e(g); e!=INVALID; ++e) cap[e]=2*cap[e];
kpeter@392
   156
  preflow_test.init(flow);
alpar@389
   157
  preflow_test.startFirstPhase();
alpar@389
   158
alpar@389
   159
  CutMap min_cut1(g);
alpar@389
   160
  preflow_test.minCutMap(min_cut1);
alpar@389
   161
  min_cut_value=cutValue(g,min_cut1,cap);
alpar@389
   162
alpar@389
   163
  check(preflow_test.flowValue() == min_cut_value &&
alpar@389
   164
        min_cut_value == 2*flow_value,
alpar@389
   165
        "The max flow value or the min cut value is wrong.");
alpar@389
   166
alpar@389
   167
  preflow_test.startSecondPhase();
alpar@389
   168
alpar@389
   169
  check(checkFlow(g, preflow_test.flowMap(), cap, s, t),
alpar@389
   170
        "The flow is not feasible.");
alpar@389
   171
alpar@389
   172
  CutMap min_cut2(g);
alpar@389
   173
  preflow_test.minCutMap(min_cut2);
alpar@389
   174
  min_cut_value=cutValue(g,min_cut2,cap);
alpar@389
   175
alpar@389
   176
  check(preflow_test.flowValue() == min_cut_value &&
alpar@389
   177
        min_cut_value == 2*flow_value,
alpar@389
   178
        "The max flow value or the three min cut values were not doubled");
alpar@389
   179
alpar@389
   180
alpar@389
   181
  preflow_test.flowMap(flow);
alpar@389
   182
alpar@389
   183
  NodeIt tmp1(g,s);
alpar@389
   184
  ++tmp1;
alpar@389
   185
  if ( tmp1 != INVALID ) s=tmp1;
alpar@389
   186
alpar@389
   187
  NodeIt tmp2(g,t);
alpar@389
   188
  ++tmp2;
alpar@389
   189
  if ( tmp2 != INVALID ) t=tmp2;
alpar@389
   190
alpar@389
   191
  preflow_test.source(s);
alpar@389
   192
  preflow_test.target(t);
alpar@389
   193
alpar@389
   194
  preflow_test.run();
alpar@389
   195
alpar@389
   196
  CutMap min_cut3(g);
alpar@389
   197
  preflow_test.minCutMap(min_cut3);
alpar@389
   198
  min_cut_value=cutValue(g,min_cut3,cap);
alpar@389
   199
alpar@389
   200
alpar@389
   201
  check(preflow_test.flowValue() == min_cut_value,
alpar@389
   202
        "The max flow value or the three min cut values are incorrect.");
alpar@389
   203
alpar@389
   204
  return 0;
alpar@389
   205
}