src/test/preflow_test.cc
changeset 833 512e5fd7d38b
child 842 a4bb28813570
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/test/preflow_test.cc	Mon Sep 13 10:50:28 2004 +0000
     1.3 @@ -0,0 +1,166 @@
     1.4 +#include <fstream>
     1.5 +#include "test_tools.h"
     1.6 +#include <hugo/smart_graph.h>
     1.7 +#include <hugo/dimacs.h>
     1.8 +#include <hugo/preflow.h>
     1.9 +#include <hugo/skeletons/graph.h>
    1.10 +#include <hugo/skeletons/maps.h>
    1.11 +using namespace hugo;
    1.12 +
    1.13 +void check_Preflow() 
    1.14 +{
    1.15 +  typedef int VType;
    1.16 +  typedef skeleton::StaticGraphSkeleton Graph;
    1.17 +
    1.18 +  typedef Graph::Node Node;
    1.19 +  typedef Graph::Edge Edge;
    1.20 +  typedef skeleton::ReadMap<Edge,VType> CapMap;
    1.21 +  typedef skeleton::ReadWriteMap<Edge,VType> FlowMap;
    1.22 +  typedef skeleton::ReadWriteMap<Node,bool> CutMap;
    1.23 + 
    1.24 +  typedef Preflow<Graph, int, CapMap, FlowMap> PType;
    1.25 +
    1.26 +  Graph G;
    1.27 +  Node n;
    1.28 +  CapMap cap;
    1.29 +  FlowMap flow;
    1.30 +  CutMap cut;
    1.31 +
    1.32 +  PType preflow_test(G,n,n,cap,flow);
    1.33 +
    1.34 +  preflow_test.run();
    1.35 +  preflow_test.flowValue();
    1.36 +  preflow_test.setSource(n);
    1.37 +  preflow_test.setFlow(flow);
    1.38 +
    1.39 +  preflow_test.phase1(PType::NO_FLOW);
    1.40 +  preflow_test.minCut(cut);
    1.41 +
    1.42 +  preflow_test.phase2();
    1.43 +  preflow_test.setTarget(n);
    1.44 +  preflow_test.setCap(cap);
    1.45 +  preflow_test.minMinCut(cut);
    1.46 +  preflow_test.maxMinCut(cut);
    1.47 +}
    1.48 +
    1.49 +int cut_value ( SmartGraph& G, SmartGraph::NodeMap<bool>& cut, 
    1.50 +		SmartGraph::EdgeMap<int>& cap) {
    1.51 +  
    1.52 +  int c=0;
    1.53 +  for(SmartGraph::EdgeIt e(G); e!=INVALID; ++e) {
    1.54 +    if (cut[G.tail(e)] && !cut[G.head(e)]) c+=cap[e];
    1.55 +  }
    1.56 +  return c;
    1.57 +}
    1.58 +
    1.59 +int main() {
    1.60 +
    1.61 +  typedef SmartGraph Graph;
    1.62 +  
    1.63 +  typedef Graph::NodeIt NodeIt;
    1.64 +  typedef Graph::EdgeIt EdgeIt;
    1.65 +  typedef Graph::EdgeMap<int> CapMap;
    1.66 +  typedef Graph::EdgeMap<int> FlowMap;
    1.67 +  typedef Graph::NodeMap<bool> CutMap;
    1.68 +
    1.69 +  typedef Preflow<Graph, int> PType;
    1.70 +
    1.71 +  std::ifstream file("preflow_graph");
    1.72 +  
    1.73 +  Graph G;
    1.74 +  NodeIt s, t;
    1.75 +  CapMap cap(G);
    1.76 +  readDimacs(file, G, cap, s, t);
    1.77 +
    1.78 +  FlowMap flow(G,0);
    1.79 + 
    1.80 +  PType preflow_test(G, s, t, cap, flow);
    1.81 +  preflow_test.run(PType::ZERO_FLOW);
    1.82 + 
    1.83 +   
    1.84 +  CutMap mincut(G,false);
    1.85 +  preflow_test.minCut(mincut); 
    1.86 +  int min_cut_value=cut_value(G,mincut,cap);
    1.87 +   
    1.88 +  CutMap minmincut(G,false);
    1.89 +  preflow_test.minMinCut(minmincut); 
    1.90 +  int min_min_cut_value=cut_value(G,minmincut,cap);
    1.91 +   
    1.92 +  CutMap maxmincut(G,false);
    1.93 +  preflow_test.maxMinCut(maxmincut); 
    1.94 +  int max_min_cut_value=cut_value(G,maxmincut,cap);
    1.95 +
    1.96 +  check(preflow_test.flowValue() == min_cut_value &&
    1.97 +	min_cut_value == min_min_cut_value &&
    1.98 +	min_min_cut_value == max_min_cut_value,
    1.99 +	"The max flow value is not equal to the three min cut values.");
   1.100 +
   1.101 +  int flow_value=preflow_test.flowValue();
   1.102 +
   1.103 +
   1.104 +  for(EdgeIt e(G); e!=INVALID; ++e) cap[e]=2*cap[e]; 
   1.105 +  preflow_test.setCap(cap);  
   1.106 +  preflow_test.setTarget(++t); //the max flow value remains 2*flow_value
   1.107 +  //warning: ++t must be a valid node. In preflow_graph, it is.
   1.108 +
   1.109 +  preflow_test.phase1(PType::PRE_FLOW);
   1.110 +
   1.111 +  CutMap mincut1(G,false);
   1.112 +  preflow_test.minCut(mincut1); 
   1.113 +  min_cut_value=cut_value(G,mincut1,cap);
   1.114 +   
   1.115 +  check(preflow_test.flowValue() == min_cut_value &&
   1.116 +	min_cut_value == 2*flow_value,
   1.117 +	"The max flow value or the min cut value is wrong.");
   1.118 +
   1.119 +  preflow_test.phase2();
   1.120 +
   1.121 +  CutMap mincut2(G,false);
   1.122 +  preflow_test.minCut(mincut2); 
   1.123 +  min_cut_value=cut_value(G,mincut2,cap);
   1.124 +   
   1.125 +  CutMap minmincut2(G,false);
   1.126 +  preflow_test.minMinCut(minmincut2); 
   1.127 +  min_min_cut_value=cut_value(G,minmincut2,cap);
   1.128 +
   1.129 + 
   1.130 +  preflow_test.maxMinCut(maxmincut); 
   1.131 +  
   1.132 +  max_min_cut_value=cut_value(G,maxmincut,cap);
   1.133 +
   1.134 +  check(preflow_test.flowValue() == min_cut_value &&
   1.135 +	min_cut_value == min_min_cut_value &&
   1.136 +	min_min_cut_value == max_min_cut_value &&
   1.137 +	min_cut_value == 2*flow_value,
   1.138 +	"The max flow value or the three min cut values were not doubled");
   1.139 +
   1.140 +  EdgeIt e(G);
   1.141 +  for( int i=1; i==1000; ++i ) {
   1.142 +    flow[e]=0;
   1.143 +    ++e;
   1.144 +  }
   1.145 +
   1.146 +  preflow_test.setFlow(flow); 
   1.147 +  preflow_test.setSource(s);
   1.148 +
   1.149 +  preflow_test.run();
   1.150 +
   1.151 +  CutMap mincut3(G,false);
   1.152 +  preflow_test.minCut(mincut3); 
   1.153 +  min_cut_value=cut_value(G,mincut3,cap);
   1.154 +   
   1.155 +  CutMap minmincut3(G,false);
   1.156 +  preflow_test.minMinCut(minmincut3); 
   1.157 +  min_min_cut_value=cut_value(G,minmincut3,cap);
   1.158 +   
   1.159 +  preflow_test.maxMinCut(maxmincut); 
   1.160 +  max_min_cut_value=cut_value(G,maxmincut,cap);
   1.161 +
   1.162 +  check(preflow_test.flowValue() == min_cut_value &&
   1.163 +	min_cut_value == min_min_cut_value &&
   1.164 +	min_min_cut_value == max_min_cut_value,
   1.165 +	"The max flow value or the three min cut values are incorrect.");
   1.166 +}
   1.167 +
   1.168 +
   1.169 +