as you see...
authormarci
Mon, 26 Apr 2004 09:55:31 +0000
changeset 410d137525538dc
parent 409 7ab7f083760a
child 411 3c8801529a1f
as you see...
src/work/marci/bipartite_matching_try.cc
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/work/marci/bipartite_matching_try.cc	Mon Apr 26 09:55:31 2004 +0000
     1.3 @@ -0,0 +1,194 @@
     1.4 +// -*- c++ -*-
     1.5 +#include <iostream>
     1.6 +#include <fstream>
     1.7 +#include <vector>
     1.8 +#include <cstdlib>
     1.9 +
    1.10 +#include <list_graph.h>
    1.11 +//#include <smart_graph.h>
    1.12 +//#include <dimacs.h>
    1.13 +#include <time_measure.h>
    1.14 +#include <for_each_macros.h>
    1.15 +#include <bfs_iterator.h>
    1.16 +#include <graph_wrapper.h>
    1.17 +#include <maps.h>
    1.18 +#include <edmonds_karp.h>
    1.19 +#include <preflow.h>
    1.20 +
    1.21 +/**
    1.22 + * Inicializalja a veletlenszamgeneratort.
    1.23 + * Figyelem, ez nem jo igazi random szamokhoz,
    1.24 + * erre ne bizzad a titkaidat!
    1.25 + */
    1.26 +void random_init()
    1.27 +{
    1.28 +	unsigned int seed = getpid();
    1.29 +	seed |= seed << 15;
    1.30 +	seed ^= time(0);
    1.31 +
    1.32 +	srand(seed);
    1.33 +}
    1.34 +
    1.35 +/**
    1.36 + * Egy veletlen int-et ad vissza 0 es m-1 kozott.
    1.37 + */
    1.38 +int random(int m)
    1.39 +{
    1.40 +  return int( double(m) * rand() / (RAND_MAX + 1.0) );
    1.41 +}
    1.42 +
    1.43 +using namespace hugo;
    1.44 +
    1.45 +int main() {
    1.46 +  typedef UndirListGraph Graph; 
    1.47 +  typedef Graph::Node Node;
    1.48 +  typedef Graph::NodeIt NodeIt;
    1.49 +  typedef Graph::Edge Edge;
    1.50 +  typedef Graph::EdgeIt EdgeIt;
    1.51 +  typedef Graph::OutEdgeIt OutEdgeIt;
    1.52 +
    1.53 +  Graph g;
    1.54 +
    1.55 +  std::vector<Graph::Node> s_nodes;
    1.56 +  std::vector<Graph::Node> t_nodes;
    1.57 +
    1.58 +  int a;
    1.59 +  std::cout << "number of nodes in the first color class=";
    1.60 +  std::cin >> a; 
    1.61 +  int b;
    1.62 +  std::cout << "number of nodes in the second color class=";
    1.63 +  std::cin >> b; 
    1.64 +  int m;
    1.65 +  std::cout << "number of edges=";
    1.66 +  std::cin >> m; 
    1.67 +  
    1.68 +
    1.69 +  for (int i=0; i<a; ++i) s_nodes.push_back(g.addNode());
    1.70 +  for (int i=0; i<b; ++i) t_nodes.push_back(g.addNode());
    1.71 +
    1.72 +  random_init();
    1.73 +  for(int i=0; i<m; ++i) {
    1.74 +    g.addEdge(s_nodes[random(a)], t_nodes[random(b)]);
    1.75 +  }
    1.76 +
    1.77 +  Graph::NodeMap<int> ref_map(g, -1);
    1.78 +
    1.79 +  IterableBoolMap< Graph::NodeMap<int> > bipartite_map(ref_map);
    1.80 +  for (int i=0; i<a; ++i) bipartite_map.insert(s_nodes[i], false);
    1.81 +  for (int i=0; i<b; ++i) bipartite_map.insert(t_nodes[i], true);
    1.82 +
    1.83 +//   Graph::Node u;
    1.84 +//   std::cout << "These nodes will be in S:\n";
    1.85 +//   //FIXME azert kellene ++, es invalid vizsgalat u-bol, hogy ezt le lehessen 
    1.86 +//   //irni 1etlen FOR_EACH-csel.
    1.87 +//   for (bipartite_map.first(u, false); g.valid(u); bipartite_map.next(u)) 
    1.88 +//     std::cout << u << " ";
    1.89 +//   std::cout << "\n";
    1.90 +//   std::cout << "These nodes will be in T:\n";
    1.91 +//   for (bipartite_map.first(u, true); g.valid(u); bipartite_map.next(u)) 
    1.92 +//     std::cout << u << " ";
    1.93 +//   std::cout << "\n";
    1.94 +
    1.95 +  typedef BipartiteGraphWrapper<Graph> BGW;
    1.96 +  BGW bgw(g, bipartite_map);
    1.97 +
    1.98 +//   std::cout << "Nodes by NodeIt:\n";
    1.99 +//   FOR_EACH_LOC(BGW::NodeIt, n, bgw) {
   1.100 +//     std::cout << n << " ";
   1.101 +//   }
   1.102 +//   std::cout << "\n";
   1.103 +//   std::cout << "Nodes in S by ClassNodeIt:\n";
   1.104 +//   FOR_EACH_INC_LOC(BGW::ClassNodeIt, n, bgw, bgw.S_CLASS) {
   1.105 +//     std::cout << n << " ";
   1.106 +//   }
   1.107 +//   std::cout << "\n";
   1.108 +//   std::cout << "Nodes in T by ClassNodeIt:\n";
   1.109 +//   FOR_EACH_INC_LOC(BGW::ClassNodeIt, n, bgw, bgw.T_CLASS) {
   1.110 +//     std::cout << n << " ";
   1.111 +//   }
   1.112 +//   std::cout << "\n";
   1.113 +//   std::cout << "Edges of the bipartite graph:\n";
   1.114 +//   FOR_EACH_LOC(BGW::EdgeIt, e, bgw) {
   1.115 +//     std::cout << bgw.tail(e) << "->" << bgw.head(e) << std::endl;
   1.116 +//   }
   1.117 +
   1.118 +  BGW::NodeMap<int> dbyj(bgw);
   1.119 +  BGW::EdgeMap<int> dbyxcj(bgw);
   1.120 +
   1.121 +  typedef stGraphWrapper<BGW> stGW;
   1.122 +  stGW stgw(bgw);
   1.123 +  ConstMap<stGW::Edge, int> const1map(1);
   1.124 +//  stGW::NodeMap<int> ize(stgw);
   1.125 +
   1.126 +//   BfsIterator< BGW, BGW::NodeMap<bool> > bfs(bgw);
   1.127 +//   Graph::NodeIt si;
   1.128 +//   Graph::Node s; 
   1.129 +//   s=g.first(si);
   1.130 +//   bfs.pushAndSetReached(BGW::Node(s));
   1.131 +//   while (!bfs.finished()) { ++bfs; }
   1.132 +
   1.133 +//   FOR_EACH_LOC(stGW::NodeIt, n, stgw) { 
   1.134 +//     std::cout << "out-edges of " << n << ":\n"; 
   1.135 +//     FOR_EACH_INC_LOC(stGW::OutEdgeIt, e, stgw, n) { 
   1.136 +//       std::cout << " " << e << "\n";
   1.137 +//       std::cout << " aNode: " << stgw.aNode(e) << "\n";
   1.138 +//       std::cout << " bNode: " << stgw.bNode(e) << "\n";      
   1.139 +//     }
   1.140 +//     std::cout << "in-edges of " << n << ":\n"; 
   1.141 +//     FOR_EACH_INC_LOC(stGW::InEdgeIt, e, stgw, n) { 
   1.142 +//       std::cout << " " << e << "\n";
   1.143 +//       std::cout << " aNode: " << stgw.aNode(e) << "\n";
   1.144 +//       std::cout << " bNode: " << stgw.bNode(e) << "\n";     
   1.145 +//     }
   1.146 +//   }
   1.147 +//   std::cout << "Edges of the stGraphWrapper:\n"; 
   1.148 +//   FOR_EACH_LOC(stGW::EdgeIt, n, stgw) { 
   1.149 +//     std::cout << " " << n << "\n";
   1.150 +//   }
   1.151 +
   1.152 +//   stGW::NodeMap<bool> b(stgw);
   1.153 +//   FOR_EACH_LOC(stGW::NodeIt, n, stgw) { 
   1.154 +//     std::cout << n << ": " << b[n] <<"\n";
   1.155 +//   }
   1.156 +
   1.157 +//   std::cout << "Bfs from s: \n";
   1.158 +//   BfsIterator< stGW, stGW::NodeMap<bool> > bfs_stgw(stgw);
   1.159 +//   bfs_stgw.pushAndSetReached(stgw.S_NODE);
   1.160 +//   while (!bfs_stgw.finished()) { 
   1.161 +//     std::cout << " " << stGW::OutEdgeIt(bfs_stgw) << "\n";
   1.162 +//     ++bfs_stgw; 
   1.163 +//   }
   1.164 +
   1.165 +
   1.166 +  Timer ts;
   1.167 +  ts.reset();
   1.168 +  stGW::EdgeMap<int> max_flow(stgw);
   1.169 +  MaxFlow<stGW, int, ConstMap<stGW::Edge, int>, stGW::EdgeMap<int> > 
   1.170 +    max_flow_test(stgw, stgw.S_NODE, stgw.T_NODE, const1map, max_flow);
   1.171 +//  while (max_flow_test.augmentOnShortestPath()) { }
   1.172 +  typedef ListGraph MutableGraph;
   1.173 +//  while (max_flow_test.augmentOnBlockingFlow1<MutableGraph>()) {
   1.174 +  while (max_flow_test.augmentOnBlockingFlow2()) {
   1.175 +   std::cout << max_flow_test.flowValue() << std::endl;
   1.176 +  }
   1.177 +  std::cout << "max flow value: " << max_flow_test.flowValue() << std::endl;
   1.178 +  std::cout << "elapsed time: " << ts << std::endl;
   1.179 +//   FOR_EACH_LOC(stGW::EdgeIt, e, stgw) { 
   1.180 +//     std::cout << e << ": " << max_flow[e] << "\n"; 
   1.181 +//   }
   1.182 +//   std::cout << "\n";
   1.183 +
   1.184 +  ts.reset();
   1.185 +  stGW::EdgeMap<int> pre_flow(stgw);
   1.186 +  Preflow<stGW, int, ConstMap<stGW::Edge, int>, stGW::EdgeMap<int> > 
   1.187 +    pre_flow_test(stgw, stgw.S_NODE, stgw.T_NODE, const1map, pre_flow, true);
   1.188 +  pre_flow_test.run();
   1.189 +  std::cout << "pre flow value: " << max_flow_test.flowValue() << std::endl;
   1.190 +  std::cout << "elapsed time: " << ts << std::endl;
   1.191 +//   FOR_EACH_LOC(stGW::EdgeIt, e, stgw) { 
   1.192 +//     std::cout << e << ": " << pre_flow[e] << "\n"; 
   1.193 +//   }
   1.194 +//   std::cout << "\n";
   1.195 +
   1.196 +  return 0;
   1.197 +}