src/test/preflow_test.cc
author klao
Wed, 15 Sep 2004 14:25:44 +0000
changeset 858 acc83957ee4a
parent 855 8c44b64dd436
child 859 2570784896d8
permissions -rwxr-xr-x
Handling strings with std::string
Do not segfault if srcdir env. variable is not set.
jacint@833
     1
#include <fstream>
klao@858
     2
#include <string>
klao@858
     3
jacint@833
     4
#include "test_tools.h"
jacint@833
     5
#include <hugo/smart_graph.h>
jacint@833
     6
#include <hugo/dimacs.h>
jacint@833
     7
#include <hugo/preflow.h>
jacint@833
     8
#include <hugo/skeletons/graph.h>
jacint@833
     9
#include <hugo/skeletons/maps.h>
alpar@855
    10
jacint@833
    11
using namespace hugo;
jacint@833
    12
jacint@833
    13
void check_Preflow() 
jacint@833
    14
{
jacint@833
    15
  typedef int VType;
jacint@833
    16
  typedef skeleton::StaticGraphSkeleton Graph;
jacint@833
    17
jacint@833
    18
  typedef Graph::Node Node;
jacint@833
    19
  typedef Graph::Edge Edge;
jacint@833
    20
  typedef skeleton::ReadMap<Edge,VType> CapMap;
jacint@833
    21
  typedef skeleton::ReadWriteMap<Edge,VType> FlowMap;
jacint@833
    22
  typedef skeleton::ReadWriteMap<Node,bool> CutMap;
jacint@833
    23
 
jacint@833
    24
  typedef Preflow<Graph, int, CapMap, FlowMap> PType;
jacint@833
    25
jacint@833
    26
  Graph G;
jacint@833
    27
  Node n;
jacint@833
    28
  CapMap cap;
jacint@833
    29
  FlowMap flow;
jacint@833
    30
  CutMap cut;
jacint@833
    31
jacint@833
    32
  PType preflow_test(G,n,n,cap,flow);
jacint@833
    33
jacint@833
    34
  preflow_test.run();
jacint@833
    35
  preflow_test.flowValue();
jacint@833
    36
  preflow_test.setSource(n);
jacint@833
    37
  preflow_test.setFlow(flow);
jacint@833
    38
jacint@833
    39
  preflow_test.phase1(PType::NO_FLOW);
jacint@833
    40
  preflow_test.minCut(cut);
jacint@833
    41
jacint@833
    42
  preflow_test.phase2();
jacint@833
    43
  preflow_test.setTarget(n);
jacint@833
    44
  preflow_test.setCap(cap);
jacint@833
    45
  preflow_test.minMinCut(cut);
jacint@833
    46
  preflow_test.maxMinCut(cut);
jacint@833
    47
}
jacint@833
    48
jacint@833
    49
int cut_value ( SmartGraph& G, SmartGraph::NodeMap<bool>& cut, 
jacint@833
    50
		SmartGraph::EdgeMap<int>& cap) {
jacint@833
    51
  
jacint@833
    52
  int c=0;
jacint@833
    53
  for(SmartGraph::EdgeIt e(G); e!=INVALID; ++e) {
jacint@833
    54
    if (cut[G.tail(e)] && !cut[G.head(e)]) c+=cap[e];
jacint@833
    55
  }
jacint@833
    56
  return c;
jacint@833
    57
}
jacint@833
    58
jacint@833
    59
int main() {
jacint@833
    60
jacint@833
    61
  typedef SmartGraph Graph;
jacint@833
    62
  
alpar@842
    63
  typedef Graph::Node Node;
jacint@833
    64
  typedef Graph::NodeIt NodeIt;
jacint@833
    65
  typedef Graph::EdgeIt EdgeIt;
jacint@833
    66
  typedef Graph::EdgeMap<int> CapMap;
jacint@833
    67
  typedef Graph::EdgeMap<int> FlowMap;
jacint@833
    68
  typedef Graph::NodeMap<bool> CutMap;
jacint@833
    69
jacint@833
    70
  typedef Preflow<Graph, int> PType;
jacint@833
    71
klao@858
    72
  string f_name;
klao@858
    73
  if( getenv("srcdir") ) {
klao@858
    74
    f_name = string(getenv("srcdir")) + "/preflow_graph.inp";
klao@858
    75
  }
klao@858
    76
  else {
klao@858
    77
    f_name = "preflow_graph.inp";
klao@858
    78
  }
alpar@855
    79
  
klao@858
    80
  std::ifstream file(f_name.c_str());
alpar@855
    81
  
klao@858
    82
  check(file, "Input file '" << f_name << "' not found.");
jacint@833
    83
  
jacint@833
    84
  Graph G;
alpar@842
    85
  Node s, t;
jacint@833
    86
  CapMap cap(G);
jacint@833
    87
  readDimacs(file, G, cap, s, t);
jacint@833
    88
jacint@833
    89
  FlowMap flow(G,0);
jacint@833
    90
 
jacint@833
    91
  PType preflow_test(G, s, t, cap, flow);
jacint@833
    92
  preflow_test.run(PType::ZERO_FLOW);
jacint@833
    93
 
jacint@833
    94
   
jacint@833
    95
  CutMap mincut(G,false);
jacint@833
    96
  preflow_test.minCut(mincut); 
jacint@833
    97
  int min_cut_value=cut_value(G,mincut,cap);
jacint@833
    98
   
jacint@833
    99
  CutMap minmincut(G,false);
jacint@833
   100
  preflow_test.minMinCut(minmincut); 
jacint@833
   101
  int min_min_cut_value=cut_value(G,minmincut,cap);
jacint@833
   102
   
jacint@833
   103
  CutMap maxmincut(G,false);
jacint@833
   104
  preflow_test.maxMinCut(maxmincut); 
jacint@833
   105
  int max_min_cut_value=cut_value(G,maxmincut,cap);
jacint@833
   106
jacint@833
   107
  check(preflow_test.flowValue() == min_cut_value &&
jacint@833
   108
	min_cut_value == min_min_cut_value &&
jacint@833
   109
	min_min_cut_value == max_min_cut_value,
jacint@833
   110
	"The max flow value is not equal to the three min cut values.");
jacint@833
   111
jacint@833
   112
  int flow_value=preflow_test.flowValue();
jacint@833
   113
jacint@833
   114
jacint@833
   115
  for(EdgeIt e(G); e!=INVALID; ++e) cap[e]=2*cap[e]; 
jacint@833
   116
  preflow_test.setCap(cap);  
alpar@842
   117
alpar@842
   118
  NodeIt tmp_node(G,t);
alpar@842
   119
  ++tmp_node;
alpar@842
   120
  t=tmp_node;
alpar@842
   121
  
alpar@842
   122
  preflow_test.setTarget(t); //the max flow value remains 2*flow_value
jacint@833
   123
  //warning: ++t must be a valid node. In preflow_graph, it is.
jacint@833
   124
jacint@833
   125
  preflow_test.phase1(PType::PRE_FLOW);
jacint@833
   126
jacint@833
   127
  CutMap mincut1(G,false);
jacint@833
   128
  preflow_test.minCut(mincut1); 
jacint@833
   129
  min_cut_value=cut_value(G,mincut1,cap);
jacint@833
   130
   
jacint@833
   131
  check(preflow_test.flowValue() == min_cut_value &&
jacint@833
   132
	min_cut_value == 2*flow_value,
jacint@833
   133
	"The max flow value or the min cut value is wrong.");
jacint@833
   134
jacint@833
   135
  preflow_test.phase2();
jacint@833
   136
jacint@833
   137
  CutMap mincut2(G,false);
jacint@833
   138
  preflow_test.minCut(mincut2); 
jacint@833
   139
  min_cut_value=cut_value(G,mincut2,cap);
jacint@833
   140
   
jacint@833
   141
  CutMap minmincut2(G,false);
jacint@833
   142
  preflow_test.minMinCut(minmincut2); 
jacint@833
   143
  min_min_cut_value=cut_value(G,minmincut2,cap);
jacint@833
   144
jacint@833
   145
 
jacint@833
   146
  preflow_test.maxMinCut(maxmincut); 
jacint@833
   147
  
jacint@833
   148
  max_min_cut_value=cut_value(G,maxmincut,cap);
jacint@833
   149
jacint@833
   150
  check(preflow_test.flowValue() == min_cut_value &&
jacint@833
   151
	min_cut_value == min_min_cut_value &&
jacint@833
   152
	min_min_cut_value == max_min_cut_value &&
jacint@833
   153
	min_cut_value == 2*flow_value,
jacint@833
   154
	"The max flow value or the three min cut values were not doubled");
jacint@833
   155
jacint@833
   156
  EdgeIt e(G);
jacint@833
   157
  for( int i=1; i==1000; ++i ) {
jacint@833
   158
    flow[e]=0;
jacint@833
   159
    ++e;
jacint@833
   160
  }
jacint@833
   161
jacint@833
   162
  preflow_test.setFlow(flow); 
jacint@833
   163
  preflow_test.setSource(s);
jacint@833
   164
jacint@833
   165
  preflow_test.run();
jacint@833
   166
jacint@833
   167
  CutMap mincut3(G,false);
jacint@833
   168
  preflow_test.minCut(mincut3); 
jacint@833
   169
  min_cut_value=cut_value(G,mincut3,cap);
jacint@833
   170
   
jacint@833
   171
  CutMap minmincut3(G,false);
jacint@833
   172
  preflow_test.minMinCut(minmincut3); 
jacint@833
   173
  min_min_cut_value=cut_value(G,minmincut3,cap);
jacint@833
   174
   
jacint@833
   175
  preflow_test.maxMinCut(maxmincut); 
jacint@833
   176
  max_min_cut_value=cut_value(G,maxmincut,cap);
jacint@833
   177
jacint@833
   178
  check(preflow_test.flowValue() == min_cut_value &&
jacint@833
   179
	min_cut_value == min_min_cut_value &&
jacint@833
   180
	min_min_cut_value == max_min_cut_value,
jacint@833
   181
	"The max flow value or the three min cut values are incorrect.");
jacint@833
   182
}