Test for the flow algorithms
authorjacint
Tue, 20 Jan 2004 21:27:45 +0000
changeset 23a72cac00e274
parent 22 0286c68fc680
child 24 d7321cdf5664
Test for the flow algorithms
src/work/proba.cc
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/work/proba.cc	Tue Jan 20 21:27:45 2004 +0000
     1.3 @@ -0,0 +1,222 @@
     1.4 +#include <iostream>
     1.5 +#include <vector>
     1.6 +#include <string>
     1.7 +
     1.8 +#include <marci_list_graph.hh>
     1.9 +#include <marci_graph_traits.hh>
    1.10 +#include <marci_property_vector.hh>
    1.11 +#include <preflow_push_hl.hh>
    1.12 +#include <preflow_push_max_flow.hh>
    1.13 +#include <reverse_bfs.hh>
    1.14 +
    1.15 +using namespace marci;
    1.16 +
    1.17 +
    1.18 +int main (int, char*[])
    1.19 +{
    1.20 +  typedef graph_traits<list_graph>::node_iterator node_iterator;
    1.21 +  typedef graph_traits<list_graph>::edge_iterator edge_iterator;
    1.22 +  typedef graph_traits<list_graph>::each_node_iterator each_node_iterator;
    1.23 +  typedef graph_traits<list_graph>::each_edge_iterator each_edge_iterator;
    1.24 +  typedef graph_traits<list_graph>::out_edge_iterator out_edge_iterator;
    1.25 +  typedef graph_traits<list_graph>::in_edge_iterator in_edge_iterator;
    1.26 +  typedef graph_traits<list_graph>::sym_edge_iterator sym_edge_iterator;
    1.27 +
    1.28 +  list_graph flow_test;
    1.29 + 
    1.30 +  //Ahuja könyv példája, maxflowvalue=13
    1.31 +  node_iterator s=flow_test.add_node();
    1.32 +  node_iterator v1=flow_test.add_node();
    1.33 +  node_iterator v2=flow_test.add_node();
    1.34 +  node_iterator v3=flow_test.add_node();
    1.35 +  node_iterator v4=flow_test.add_node();
    1.36 +  node_iterator v5=flow_test.add_node();
    1.37 +  node_iterator t=flow_test.add_node();
    1.38 +  
    1.39 +  node_property_vector<list_graph, std::string> node_name(flow_test);
    1.40 +  node_name.put(s, "s");
    1.41 +  node_name.put(v1, "v1");
    1.42 +  node_name.put(v2, "v2");
    1.43 +  node_name.put(v3, "v3");
    1.44 +  node_name.put(v4, "v4");
    1.45 +  node_name.put(v5, "v5");
    1.46 +  node_name.put(t, "t");
    1.47 +
    1.48 +  edge_iterator s_v1=flow_test.add_edge(s, v1);
    1.49 +  edge_iterator s_v2=flow_test.add_edge(s, v2);
    1.50 +  edge_iterator s_v3=flow_test.add_edge(s, v3);
    1.51 +  
    1.52 +  edge_iterator v2_v4=flow_test.add_edge(v2, v4);
    1.53 +  edge_iterator v2_v5=flow_test.add_edge(v2, v5);
    1.54 +
    1.55 +  edge_iterator v3_v5=flow_test.add_edge(v3, v5);
    1.56 +
    1.57 +  edge_iterator v4_t=flow_test.add_edge(v4, t);
    1.58 +  edge_iterator v5_t=flow_test.add_edge(v5, t);
    1.59 +  
    1.60 +  edge_property_vector<list_graph, int> cap(flow_test);  
    1.61 +  cap.put(s_v1, 0);
    1.62 +  cap.put(s_v2, 10);
    1.63 +  cap.put(s_v3, 10);
    1.64 +  cap.put(v2_v4, 5);
    1.65 +  cap.put(v2_v5, 8);
    1.66 +  cap.put(v3_v5, 5);
    1.67 +  cap.put(v4_t, 8);
    1.68 +  cap.put(v5_t, 8);
    1.69 +
    1.70 +
    1.71 +  
    1.72 +  //Marci példája, maxflowvalue=23
    1.73 +  /*   node_iterator s=flow_test.add_node();
    1.74 +  node_iterator v1=flow_test.add_node();
    1.75 +  node_iterator v2=flow_test.add_node();
    1.76 +  node_iterator v3=flow_test.add_node();
    1.77 +  node_iterator v4=flow_test.add_node();
    1.78 +  node_iterator t=flow_test.add_node();
    1.79 +  node_iterator w=flow_test.add_node();
    1.80 +
    1.81 +  
    1.82 +  node_property_vector<list_graph, std::string> node_name(flow_test);
    1.83 +  node_name.put(s, "s");
    1.84 +  node_name.put(v1, "v1");
    1.85 +  node_name.put(v2, "v2");
    1.86 +  node_name.put(v3, "v3");
    1.87 +  node_name.put(v4, "v4");
    1.88 +  node_name.put(t, "t");
    1.89 +  node_name.put(w, "w");
    1.90 +
    1.91 +  edge_iterator s_v1=flow_test.add_edge(s, v1);
    1.92 +  edge_iterator s_v2=flow_test.add_edge(s, v2);
    1.93 +  edge_iterator v1_v2=flow_test.add_edge(v1, v2);
    1.94 +  edge_iterator v2_v1=flow_test.add_edge(v2, v1);
    1.95 +  edge_iterator v1_v3=flow_test.add_edge(v1, v3);
    1.96 +  edge_iterator v3_v2=flow_test.add_edge(v3, v2);
    1.97 +  edge_iterator v2_v4=flow_test.add_edge(v2, v4);
    1.98 +  edge_iterator v4_v3=flow_test.add_edge(v4, v3);
    1.99 +  edge_iterator v3_t=flow_test.add_edge(v3, t);
   1.100 +  edge_iterator v4_t=flow_test.add_edge(v4, t);
   1.101 +  edge_iterator v3_v3=flow_test.add_edge(v3, v3);
   1.102 +  edge_iterator s_w=flow_test.add_edge(s, w);
   1.103 +  
   1.104 +
   1.105 +
   1.106 +  edge_property_vector<list_graph, int> cap(flow_test);  
   1.107 +  cap.put(s_v1, 16);
   1.108 +  cap.put(s_v2, 13);
   1.109 +  cap.put(v1_v2, 10);
   1.110 +  cap.put(v2_v1, 4);
   1.111 +  cap.put(v1_v3, 12);
   1.112 +  cap.put(v3_v2, 9);
   1.113 +  cap.put(v2_v4, 14);
   1.114 +  cap.put(v4_v3, 7);
   1.115 +  cap.put(v3_t, 20);
   1.116 +  cap.put(v4_t, 4);
   1.117 +  cap.put(v3_v3, 4);
   1.118 +  cap.put(s_w, 4);
   1.119 +  */
   1.120 +
   1.121 +
   1.122 +  //pelda 3, maxflowvalue=4
   1.123 +  /*    node_iterator s=flow_test.add_node();
   1.124 +  node_iterator v1=flow_test.add_node();
   1.125 +  node_iterator v2=flow_test.add_node();
   1.126 +  node_iterator t=flow_test.add_node();
   1.127 +  node_iterator w=flow_test.add_node();
   1.128 +  
   1.129 +  node_property_vector<list_graph, std::string> node_name(flow_test);
   1.130 +  node_name.put(s, "s");
   1.131 +  node_name.put(v1, "v1");
   1.132 +  node_name.put(v2, "v2");
   1.133 +  node_name.put(t, "t");
   1.134 +  node_name.put(w, "w");
   1.135 +
   1.136 +  edge_iterator s_v1=flow_test.add_edge(s, v1);
   1.137 +  edge_iterator v1_v2=flow_test.add_edge(v1, v2);
   1.138 +  edge_iterator v2_t=flow_test.add_edge(v2, t);
   1.139 +  edge_iterator v1_v1=flow_test.add_edge(v1, v1);
   1.140 +  edge_iterator s_w=flow_test.add_edge(s, w);
   1.141 +
   1.142 +
   1.143 +  edge_property_vector<list_graph, int> cap(flow_test); 
   1.144 +    
   1.145 +  cap.put(s_v1, 16);
   1.146 +  cap.put(v1_v2, 10);
   1.147 +  cap.put(v2_t, 4);
   1.148 +  cap.put(v1_v1, 3);
   1.149 +  cap.put(s_w, 5);
   1.150 +  
   1.151 +  */
   1.152 +
   1.153 +
   1.154 +
   1.155 +  std::cout << "Testing reverse_bfs..." << std::endl;
   1.156 +  
   1.157 +  reverse_bfs<list_graph> bfs_test(flow_test, t);
   1.158 +
   1.159 +  bfs_test.run();
   1.160 +
   1.161 +  for (each_node_iterator w=flow_test.first_node(); w.is_valid(); ++w) {
   1.162 +    std::cout <<"The distance of " << w << " is " << bfs_test.dist(w) <<std::endl;
   1.163 +    }
   1.164 +
   1.165 +
   1.166 +
   1.167 +
   1.168 +
   1.169 +  std::cout << "Testing preflow_push_hl..." << std::endl;
   1.170 +  
   1.171 +  preflow_push_hl<list_graph, int> preflow_push_test(flow_test, s, t, cap);
   1.172 +
   1.173 +  preflow_push_test.run();
   1.174 +
   1.175 +  std::cout << "Maximum flow value is: " << preflow_push_test.maxflow() << "."<<std::endl;
   1.176 +
   1.177 +  std::cout<< "The flow on edge s-v1 is "<< preflow_push_test.flowonedge(s_v1) << "."<<std::endl;
   1.178 +
   1.179 +  edge_property_vector<list_graph, int> flow=preflow_push_test.allflow();  
   1.180 +  for (each_edge_iterator e=flow_test.first_edge(); e.is_valid(); ++e) {
   1.181 +    std::cout <<"Flow on edge " << flow_test.tail(e) <<"-" << flow_test.head(e)<< " is " <<flow.get(e) <<std::endl;
   1.182 +    }
   1.183 +
   1.184 +  std::cout << "A minimum cut: " <<std::endl;  
   1.185 +  node_property_vector<list_graph, bool> mincut=preflow_push_test.mincut();
   1.186 +
   1.187 +  for (each_node_iterator v=flow_test.first_node(); v.is_valid(); ++v) {
   1.188 +      if (mincut.get(v)) std::cout <<node_name.get(v)<< " ";
   1.189 +    }
   1.190 +  
   1.191 +  std::cout<<"\n\n"<<std::endl;
   1.192 +
   1.193 +
   1.194 +
   1.195 +
   1.196 +  std::cout << "Testing preflow_push_max_flow..." << std::endl;
   1.197 + 
   1.198 +  preflow_push_max_flow<list_graph, int> max_flow_test(flow_test, s, t, cap);
   1.199 +
   1.200 +  max_flow_test.run();
   1.201 +
   1.202 +  std::cout << "Maximum flow value is: " << max_flow_test.maxflow() << "."<< std::endl;
   1.203 +
   1.204 +  std::cout << "A minimum cut: " <<std::endl;  
   1.205 +  node_property_vector<list_graph, bool> mincut2=max_flow_test.mincut();
   1.206 +
   1.207 +  for (each_node_iterator v=flow_test.first_node(); v.is_valid(); ++v) {
   1.208 +    if (mincut2.get(v)) std::cout <<node_name.get(v)<< " ";
   1.209 +  }
   1.210 +  
   1.211 +  std::cout << std::endl;
   1.212 +
   1.213 +
   1.214 +
   1.215 +  return 0;
   1.216 +}
   1.217 +
   1.218 +
   1.219 +
   1.220 +
   1.221 +
   1.222 +
   1.223 +
   1.224 +
   1.225 +