src/test/preflow_test.cc
author jacint
Mon, 20 Sep 2004 08:27:34 +0000
changeset 887 ec6a528dafd2
parent 880 9d0bfd35b97c
child 906 17f31d280385
permissions -rwxr-xr-x
new test graph
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;
alpar@880
    16
  typedef skeleton::StaticGraph 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@859
    72
  std::string f_name;
klao@858
    73
  if( getenv("srcdir") ) {
jacint@887
    74
    f_name = std::string(getenv("srcdir")) + "/preflow_graph.dim";
klao@858
    75
  }
klao@858
    76
  else {
jacint@887
    77
    f_name = "preflow_graph.dim";
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@887
    90
jacint@833
    91
 
jacint@887
    92
jacint@833
    93
  PType preflow_test(G, s, t, cap, flow);
jacint@833
    94
  preflow_test.run(PType::ZERO_FLOW);
jacint@887
    95
    
jacint@833
    96
  CutMap mincut(G,false);
jacint@833
    97
  preflow_test.minCut(mincut); 
jacint@833
    98
  int min_cut_value=cut_value(G,mincut,cap);
jacint@833
    99
   
jacint@833
   100
  CutMap minmincut(G,false);
jacint@833
   101
  preflow_test.minMinCut(minmincut); 
jacint@833
   102
  int min_min_cut_value=cut_value(G,minmincut,cap);
jacint@833
   103
   
jacint@833
   104
  CutMap maxmincut(G,false);
jacint@833
   105
  preflow_test.maxMinCut(maxmincut); 
jacint@833
   106
  int max_min_cut_value=cut_value(G,maxmincut,cap);
jacint@833
   107
jacint@833
   108
  check(preflow_test.flowValue() == min_cut_value &&
jacint@833
   109
	min_cut_value == min_min_cut_value &&
jacint@833
   110
	min_min_cut_value == max_min_cut_value,
jacint@833
   111
	"The max flow value is not equal to the three min cut values.");
jacint@833
   112
jacint@833
   113
  int flow_value=preflow_test.flowValue();
jacint@833
   114
jacint@833
   115
jacint@887
   116
jacint@833
   117
  for(EdgeIt e(G); e!=INVALID; ++e) cap[e]=2*cap[e]; 
jacint@833
   118
  preflow_test.setCap(cap);  
alpar@842
   119
jacint@833
   120
  preflow_test.phase1(PType::PRE_FLOW);
jacint@833
   121
jacint@833
   122
  CutMap mincut1(G,false);
jacint@833
   123
  preflow_test.minCut(mincut1); 
jacint@833
   124
  min_cut_value=cut_value(G,mincut1,cap);
jacint@833
   125
   
jacint@833
   126
  check(preflow_test.flowValue() == min_cut_value &&
jacint@833
   127
	min_cut_value == 2*flow_value,
jacint@833
   128
	"The max flow value or the min cut value is wrong.");
jacint@833
   129
jacint@833
   130
  preflow_test.phase2();
jacint@833
   131
jacint@833
   132
  CutMap mincut2(G,false);
jacint@833
   133
  preflow_test.minCut(mincut2); 
jacint@833
   134
  min_cut_value=cut_value(G,mincut2,cap);
jacint@833
   135
   
jacint@833
   136
  CutMap minmincut2(G,false);
jacint@833
   137
  preflow_test.minMinCut(minmincut2); 
jacint@833
   138
  min_min_cut_value=cut_value(G,minmincut2,cap);
jacint@833
   139
 
jacint@833
   140
  preflow_test.maxMinCut(maxmincut); 
jacint@833
   141
  max_min_cut_value=cut_value(G,maxmincut,cap);
jacint@833
   142
jacint@833
   143
  check(preflow_test.flowValue() == min_cut_value &&
jacint@833
   144
	min_cut_value == min_min_cut_value &&
jacint@833
   145
	min_min_cut_value == max_min_cut_value &&
jacint@833
   146
	min_cut_value == 2*flow_value,
jacint@833
   147
	"The max flow value or the three min cut values were not doubled");
jacint@833
   148
jacint@887
   149
jacint@887
   150
jacint@833
   151
  EdgeIt e(G);
jacint@887
   152
  for( int i=1; i==10; ++i ) {
jacint@887
   153
    flow.set(e,0);
jacint@833
   154
    ++e;
jacint@833
   155
  }
jacint@833
   156
jacint@833
   157
  preflow_test.setFlow(flow); 
jacint@887
   158
jacint@887
   159
  NodeIt tmp1(G,s);
jacint@887
   160
  ++tmp1;
jacint@887
   161
  if ( tmp1 != INVALID ) s=tmp1;
jacint@887
   162
jacint@887
   163
  NodeIt tmp2(G,t);
jacint@887
   164
  ++tmp2;
jacint@887
   165
  if ( tmp2 != INVALID ) t=tmp2;
jacint@887
   166
jacint@833
   167
  preflow_test.setSource(s);
jacint@887
   168
  preflow_test.setTarget(t); 
jacint@887
   169
  
jacint@833
   170
  preflow_test.run();
jacint@833
   171
jacint@833
   172
  CutMap mincut3(G,false);
jacint@833
   173
  preflow_test.minCut(mincut3); 
jacint@833
   174
  min_cut_value=cut_value(G,mincut3,cap);
jacint@833
   175
   
jacint@833
   176
  CutMap minmincut3(G,false);
jacint@833
   177
  preflow_test.minMinCut(minmincut3); 
jacint@833
   178
  min_min_cut_value=cut_value(G,minmincut3,cap);
jacint@833
   179
   
jacint@833
   180
  preflow_test.maxMinCut(maxmincut); 
jacint@833
   181
  max_min_cut_value=cut_value(G,maxmincut,cap);
jacint@833
   182
jacint@833
   183
  check(preflow_test.flowValue() == min_cut_value &&
jacint@833
   184
	min_cut_value == min_min_cut_value &&
jacint@833
   185
	min_min_cut_value == max_min_cut_value,
jacint@833
   186
	"The max flow value or the three min cut values are incorrect.");
jacint@833
   187
}