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