felkesz tesztprogi
authorjacint
Sat, 08 May 2004 18:03:43 +0000
changeset 588510cf257e6f2
parent 587 266fa11f222b
child 589 d89575370bcb
felkesz tesztprogi
src/work/jacint/max_flow_test.cc
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/work/jacint/max_flow_test.cc	Sat May 08 18:03:43 2004 +0000
     1.3 @@ -0,0 +1,157 @@
     1.4 +#include <iostream>
     1.5 +
     1.6 +#include <list_graph.h>
     1.7 +#include <dimacs.h>
     1.8 +#include <max_flow.h>
     1.9 +#include <time_measure.h>
    1.10 +
    1.11 +using namespace hugo;
    1.12 +
    1.13 +int main(int, char **) {
    1.14 + 
    1.15 +  typedef ListGraph Graph;
    1.16 +  
    1.17 +  typedef Graph::Node Node;
    1.18 +  typedef Graph::EdgeIt EdgeIt;
    1.19 +
    1.20 +  Graph G;
    1.21 +  Node s, t;
    1.22 +  Graph::EdgeMap<int> cap(G);
    1.23 +  readDimacs(std::cin, G, cap, s, t);
    1.24 +  Timer ts;
    1.25 +  
    1.26 +  std::cout <<
    1.27 +    "\n  Running max_flow.h on a graph with " << 
    1.28 +    G.nodeNum() << " nodes and " << G.edgeNum() << " edges..."
    1.29 +	   << std::endl<<std::endl;
    1.30 +
    1.31 +  Graph::EdgeMap<int> flow(G,0);
    1.32 +  MaxFlow<Graph, int> max_flow_test(G, s, t, cap, flow);
    1.33 +  ts.reset();
    1.34 +  max_flow_test.run();
    1.35 +  std::cout << "Elapsed time of run(): " << std::endl 
    1.36 +	    <<ts << std::endl;
    1.37 +  
    1.38 +  Graph::NodeMap<bool> mincut(G);
    1.39 +  max_flow_test.minMinCut(mincut); 
    1.40 +  int min_min_cut_value=0;
    1.41 +  EdgeIt e;
    1.42 +  for(G.first(e); G.valid(e); G.next(e)) {
    1.43 +    if (mincut[G.tail(e)] && !mincut[G.head(e)]) min_min_cut_value+=cap[e];
    1.44 +  }
    1.45 +
    1.46 +  Graph::NodeMap<bool> cut(G);
    1.47 +  max_flow_test.minCut(cut); 
    1.48 +  int min_cut_value=0;
    1.49 +  for(G.first(e); G.valid(e); G.next(e)) {
    1.50 +    if (cut[G.tail(e)] && !cut[G.head(e)]) 
    1.51 +      min_cut_value+=cap[e];
    1.52 +  }
    1.53 +
    1.54 +  Graph::NodeMap<bool> maxcut(G);
    1.55 +  max_flow_test.maxMinCut(maxcut); 
    1.56 +  int max_min_cut_value=0;
    1.57 +  for(G.first(e); G.valid(e); G.next(e)) {
    1.58 +    if (maxcut[G.tail(e)] && !maxcut[G.head(e)]) 
    1.59 +      max_min_cut_value+=cap[e];
    1.60 +      }
    1.61 +
    1.62 +  std::cout << "\n Checking the result: " <<std::endl;  
    1.63 +  std::cout << "Flow value: "<< max_flow_test.flowValue() << std::endl;
    1.64 +  std::cout << "Min cut value: "<< min_cut_value << std::endl;
    1.65 +  std::cout << "Min min cut value: "<< min_min_cut_value << std::endl;
    1.66 +  std::cout << "Max min cut value: "<< max_min_cut_value << 
    1.67 +    std::endl;
    1.68 +
    1.69 +  if ( max_flow_test.flowValue() == min_cut_value &&
    1.70 +       min_cut_value == min_min_cut_value &&
    1.71 +       min_min_cut_value == max_min_cut_value )
    1.72 +    std::cout << "They are equal! " <<std::endl<< std::endl<<"\n";  
    1.73 +
    1.74 +
    1.75 +
    1.76 +  Graph::EdgeMap<int> flow2(G,0);
    1.77 +  std::cout << "Calling resetFlow() " << std::endl 
    1.78 +	    << ts << std::endl;
    1.79 +  max_flow_test.resetFlow(flow2);  
    1.80 +  ts.reset();
    1.81 +  max_flow_test.preflow(max_flow_test.PRE_FLOW);
    1.82 +  std::cout << "Elapsed time of preflow(PRE_FLOW) starting from the zero flow: " << std::endl 
    1.83 +	    << ts << std::endl;
    1.84 +  
    1.85 +  Graph::NodeMap<bool> mincut2(G);
    1.86 +  max_flow_test.minMinCut(mincut2); 
    1.87 +  int min_min_cut_value2=0;
    1.88 +    for(G.first(e); G.valid(e); G.next(e)) {
    1.89 +    if (mincut2[G.tail(e)] && !mincut2[G.head(e)]) min_min_cut_value2+=cap[e];
    1.90 +  }
    1.91 +
    1.92 +  Graph::NodeMap<bool> cut2(G);
    1.93 +  max_flow_test.minCut(cut2); 
    1.94 +  int min_cut_value2=0;
    1.95 +  for(G.first(e); G.valid(e); G.next(e)) {
    1.96 +    if (cut2[G.tail(e)] && !cut2[G.head(e)]) 
    1.97 +      min_cut_value2+=cap[e];
    1.98 +  }
    1.99 +
   1.100 +  Graph::NodeMap<bool> maxcut2(G);
   1.101 +  max_flow_test.maxMinCut(maxcut2); 
   1.102 +  int max_min_cut_value2=0;
   1.103 +  for(G.first(e); G.valid(e); G.next(e)) {
   1.104 +    if (maxcut2[G.tail(e)] && !maxcut2[G.head(e)]) 
   1.105 +      max_min_cut_value2+=cap[e];
   1.106 +      }
   1.107 +  
   1.108 +  std::cout << "\n Checking the result: " <<std::endl;  
   1.109 +  std::cout << "Flow value: "<< max_flow_test.flowValue() << std::endl;
   1.110 +  std::cout << "Min cut value: "<< min_cut_value2 << std::endl;
   1.111 +  std::cout << "Min min cut value: "<< min_min_cut_value2 << std::endl;
   1.112 +  std::cout << "Max min cut value: "<< max_min_cut_value2 << 
   1.113 +    std::endl;  
   1.114 +  if ( max_flow_test.flowValue() == min_cut_value &&
   1.115 +       min_cut_value == min_min_cut_value &&
   1.116 +       min_min_cut_value == max_min_cut_value )
   1.117 +    std::cout << "They are equal! " <<std::endl;  
   1.118 +
   1.119 +
   1.120 +  MaxFlow<Graph, int> max_flow_test3(G, s, t, cap, flow2);
   1.121 +  max_flow_test3.run(max_flow_test3.GEN_FLOW);
   1.122 +  std::cout << "Calling run(GEN_FLOW) from the max flow found before. " <<std::endl;  
   1.123 +  
   1.124 +  Graph::NodeMap<bool> mincut3(G);
   1.125 +  max_flow_test3.minMinCut(mincut3); 
   1.126 +  int min_min_cut_value3=0;
   1.127 +  for(G.first(e); G.valid(e); G.next(e)) {
   1.128 +    if (mincut3[G.tail(e)] && !mincut3[G.head(e)]) min_min_cut_value3+=cap[e];
   1.129 +  }
   1.130 +
   1.131 +  Graph::NodeMap<bool> cut3(G);
   1.132 +  max_flow_test3.minCut(cut3); 
   1.133 +  int min_cut_value3=0;
   1.134 +  for(G.first(e); G.valid(e); G.next(e)) {
   1.135 +    if (cut3[G.tail(e)] && !cut3[G.head(e)]) 
   1.136 +      min_cut_value3+=cap[e];
   1.137 +  }
   1.138 +
   1.139 +  Graph::NodeMap<bool> maxcut3(G);
   1.140 +  max_flow_test3.maxMinCut(maxcut3); 
   1.141 +  int max_min_cut_value3=0;
   1.142 +  for(G.first(e); G.valid(e); G.next(e)) {
   1.143 +    if (maxcut3[G.tail(e)] && !maxcut3[G.head(e)]) 
   1.144 +      max_min_cut_value3+=cap[e];
   1.145 +  }
   1.146 +
   1.147 +  std::cout << "\n Checking the result: " <<std::endl;  
   1.148 +  std::cout << "Flow value: "<< max_flow_test3.flowValue() << std::endl;
   1.149 +  std::cout << "Min cut value: "<< min_cut_value3 << std::endl;
   1.150 +  std::cout << "Min min cut value: "<< min_min_cut_value3 << std::endl;
   1.151 +  std::cout << "Max min cut value: "<< max_min_cut_value3 << 
   1.152 +    std::endl;
   1.153 +
   1.154 +  if ( max_flow_test3.flowValue() == min_cut_value3 &&
   1.155 +       min_cut_value3 == min_min_cut_value3 &&
   1.156 +       min_min_cut_value3 == max_min_cut_value3 )
   1.157 +    std::cout << "They are equal! " <<std::endl<< std::endl<<"\n";  
   1.158 +  
   1.159 +  return 0;
   1.160 +}