src/test/preflow_test.cc
author alpar
Mon, 13 Sep 2004 17:20:03 +0000
changeset 842 a4bb28813570
parent 833 512e5fd7d38b
child 845 e4692f92a79b
permissions -rwxr-xr-x
Fix a DANGEROUS bug.
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
  
alpar@842
    60
  typedef Graph::Node Node;
jacint@833
    61
  typedef Graph::NodeIt NodeIt;
jacint@833
    62
  typedef Graph::EdgeIt EdgeIt;
jacint@833
    63
  typedef Graph::EdgeMap<int> CapMap;
jacint@833
    64
  typedef Graph::EdgeMap<int> FlowMap;
jacint@833
    65
  typedef Graph::NodeMap<bool> CutMap;
jacint@833
    66
jacint@833
    67
  typedef Preflow<Graph, int> PType;
jacint@833
    68
jacint@833
    69
  std::ifstream file("preflow_graph");
jacint@833
    70
  
jacint@833
    71
  Graph G;
alpar@842
    72
  Node s, t;
jacint@833
    73
  CapMap cap(G);
jacint@833
    74
  readDimacs(file, G, cap, s, t);
jacint@833
    75
jacint@833
    76
  FlowMap flow(G,0);
jacint@833
    77
 
jacint@833
    78
  PType preflow_test(G, s, t, cap, flow);
jacint@833
    79
  preflow_test.run(PType::ZERO_FLOW);
jacint@833
    80
 
jacint@833
    81
   
jacint@833
    82
  CutMap mincut(G,false);
jacint@833
    83
  preflow_test.minCut(mincut); 
jacint@833
    84
  int min_cut_value=cut_value(G,mincut,cap);
jacint@833
    85
   
jacint@833
    86
  CutMap minmincut(G,false);
jacint@833
    87
  preflow_test.minMinCut(minmincut); 
jacint@833
    88
  int min_min_cut_value=cut_value(G,minmincut,cap);
jacint@833
    89
   
jacint@833
    90
  CutMap maxmincut(G,false);
jacint@833
    91
  preflow_test.maxMinCut(maxmincut); 
jacint@833
    92
  int max_min_cut_value=cut_value(G,maxmincut,cap);
jacint@833
    93
jacint@833
    94
  check(preflow_test.flowValue() == min_cut_value &&
jacint@833
    95
	min_cut_value == min_min_cut_value &&
jacint@833
    96
	min_min_cut_value == max_min_cut_value,
jacint@833
    97
	"The max flow value is not equal to the three min cut values.");
jacint@833
    98
jacint@833
    99
  int flow_value=preflow_test.flowValue();
jacint@833
   100
jacint@833
   101
jacint@833
   102
  for(EdgeIt e(G); e!=INVALID; ++e) cap[e]=2*cap[e]; 
jacint@833
   103
  preflow_test.setCap(cap);  
alpar@842
   104
alpar@842
   105
  NodeIt tmp_node(G,t);
alpar@842
   106
  ++tmp_node;
alpar@842
   107
  t=tmp_node;
alpar@842
   108
  
alpar@842
   109
  preflow_test.setTarget(t); //the max flow value remains 2*flow_value
jacint@833
   110
  //warning: ++t must be a valid node. In preflow_graph, it is.
jacint@833
   111
jacint@833
   112
  preflow_test.phase1(PType::PRE_FLOW);
jacint@833
   113
jacint@833
   114
  CutMap mincut1(G,false);
jacint@833
   115
  preflow_test.minCut(mincut1); 
jacint@833
   116
  min_cut_value=cut_value(G,mincut1,cap);
jacint@833
   117
   
jacint@833
   118
  check(preflow_test.flowValue() == min_cut_value &&
jacint@833
   119
	min_cut_value == 2*flow_value,
jacint@833
   120
	"The max flow value or the min cut value is wrong.");
jacint@833
   121
jacint@833
   122
  preflow_test.phase2();
jacint@833
   123
jacint@833
   124
  CutMap mincut2(G,false);
jacint@833
   125
  preflow_test.minCut(mincut2); 
jacint@833
   126
  min_cut_value=cut_value(G,mincut2,cap);
jacint@833
   127
   
jacint@833
   128
  CutMap minmincut2(G,false);
jacint@833
   129
  preflow_test.minMinCut(minmincut2); 
jacint@833
   130
  min_min_cut_value=cut_value(G,minmincut2,cap);
jacint@833
   131
jacint@833
   132
 
jacint@833
   133
  preflow_test.maxMinCut(maxmincut); 
jacint@833
   134
  
jacint@833
   135
  max_min_cut_value=cut_value(G,maxmincut,cap);
jacint@833
   136
jacint@833
   137
  check(preflow_test.flowValue() == min_cut_value &&
jacint@833
   138
	min_cut_value == min_min_cut_value &&
jacint@833
   139
	min_min_cut_value == max_min_cut_value &&
jacint@833
   140
	min_cut_value == 2*flow_value,
jacint@833
   141
	"The max flow value or the three min cut values were not doubled");
jacint@833
   142
jacint@833
   143
  EdgeIt e(G);
jacint@833
   144
  for( int i=1; i==1000; ++i ) {
jacint@833
   145
    flow[e]=0;
jacint@833
   146
    ++e;
jacint@833
   147
  }
jacint@833
   148
jacint@833
   149
  preflow_test.setFlow(flow); 
jacint@833
   150
  preflow_test.setSource(s);
jacint@833
   151
jacint@833
   152
  preflow_test.run();
jacint@833
   153
jacint@833
   154
  CutMap mincut3(G,false);
jacint@833
   155
  preflow_test.minCut(mincut3); 
jacint@833
   156
  min_cut_value=cut_value(G,mincut3,cap);
jacint@833
   157
   
jacint@833
   158
  CutMap minmincut3(G,false);
jacint@833
   159
  preflow_test.minMinCut(minmincut3); 
jacint@833
   160
  min_min_cut_value=cut_value(G,minmincut3,cap);
jacint@833
   161
   
jacint@833
   162
  preflow_test.maxMinCut(maxmincut); 
jacint@833
   163
  max_min_cut_value=cut_value(G,maxmincut,cap);
jacint@833
   164
jacint@833
   165
  check(preflow_test.flowValue() == min_cut_value &&
jacint@833
   166
	min_cut_value == min_min_cut_value &&
jacint@833
   167
	min_min_cut_value == max_min_cut_value,
jacint@833
   168
	"The max flow value or the three min cut values are incorrect.");
jacint@833
   169
}
jacint@833
   170
jacint@833
   171
jacint@833
   172