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