src/work/marci/max_bipartite_matching_demo.cc
changeset 192 100770da4336
child 194 a1680b3c516c
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/work/marci/max_bipartite_matching_demo.cc	Wed Mar 17 14:50:01 2004 +0000
     1.3 @@ -0,0 +1,107 @@
     1.4 +// -*- c++ -*-
     1.5 +#include <iostream>
     1.6 +#include <fstream>
     1.7 +#include <vector>
     1.8 +#include <cstdlib>
     1.9 +
    1.10 +#include <LEDA/graph.h>
    1.11 +#include <leda_graph_wrapper.h>
    1.12 +#include <dimacs.h>
    1.13 +#include <time_measure.h>
    1.14 +#include <edmonds_karp.h>
    1.15 +
    1.16 +/**
    1.17 + * Inicializalja a veletlenszamgeneratort.
    1.18 + * Figyelem, ez nem jo igazi random szamokhoz,
    1.19 + * erre ne bizzad a titkaidat!
    1.20 + */
    1.21 +void random_init()
    1.22 +{
    1.23 +	unsigned int seed = getpid();
    1.24 +	seed |= seed << 15;
    1.25 +	seed ^= time(0);
    1.26 +
    1.27 +	srand(seed);
    1.28 +}
    1.29 +
    1.30 +/**
    1.31 + * Egy veletlen int-et ad vissza 0 es m-1 kozott.
    1.32 + */
    1.33 +int random(int m)
    1.34 +{
    1.35 +	return int( double(m) * rand() / (RAND_MAX + 1.0) );
    1.36 +}
    1.37 +
    1.38 +using namespace hugo;
    1.39 +
    1.40 +using std::cout; 
    1.41 +using std::endl;
    1.42 +
    1.43 +int main() {
    1.44 +  leda::graph g;
    1.45 +  typedef LedaGraphWrapper<leda::graph> Graph;
    1.46 +  Graph G(g);
    1.47 +
    1.48 +  typedef Graph::Node Node;
    1.49 +  typedef Graph::NodeIt NodeIt;  
    1.50 +  typedef Graph::Edge Edge;
    1.51 +  typedef Graph::EdgeIt EdgeIt;
    1.52 +  typedef Graph::OutEdgeIt OutEdgeIt;
    1.53 +  typedef Graph::InEdgeIt InEdgeIt;
    1.54 +
    1.55 +  Node s, t;
    1.56 +  //Graph::EdgeMap<int> cap(G);
    1.57 +  //readDimacsMaxFlow(std::cin, G, s, t, cap);
    1.58 +  std::vector<Node> s_nodes;
    1.59 +  std::vector<Node> t_nodes;
    1.60 +
    1.61 +  for(int i=0; i<20; ++i) {
    1.62 +    s_nodes.push_back(G.addNode());
    1.63 +  }
    1.64 +  for(int i=0; i<20; ++i) {
    1.65 +    t_nodes.push_back(G.addNode());
    1.66 +  }
    1.67 +  random_init();
    1.68 +  for(int i=0; i<50; ++i) {
    1.69 +    G.addEdge(s_nodes[random(20)], t_nodes[random(20)]);
    1.70 +  }
    1.71 +  Graph::NodeMap<bool> s_map; //false
    1.72 +  Graph::NodeMap<bool> t_map; //false
    1.73 +  
    1.74 +  for(int i=0; i<20; ++i) {
    1.75 +    s_map.set(s_nodes[i], true);
    1.76 +    t_map.set(t_nodes[i], true);
    1.77 +  }
    1.78 +
    1.79 +  {
    1.80 +    std::cout << "on-the-fly max bipartite matching demo on wrapped leda graph..." << std::endl;
    1.81 +    Graph::EdgeMap<int> flow(G); //0 flow
    1.82 +    Graph::EdgeMap<int> capacity(G, 1);
    1.83 +
    1.84 +    Timer ts;
    1.85 +    ts.reset();
    1.86 +
    1.87 +    MaxMatching<Graph, int, Graph::EdgeMap<int>, Graph::EdgeMap<int> > max_flow_test(G, s_map, t_map, flow, cap);
    1.88 +    //max_flow_test.augmentWithBlockingFlow<Graph>();
    1.89 +    int i=0;
    1.90 +    while (max_flow_test.augmentOnShortestPath()) { 
    1.91 +//     for(EdgeIt e=G.template first<EdgeIt>(); e.valid(); ++e) { 
    1.92 +//       std::cout<<"("<<G.tail(e)<< "-"<<flow.get(e)<<"->"<<G.head(e)<<") ";
    1.93 +//     }
    1.94 +//     std::cout<<std::endl;
    1.95 +      ++i; 
    1.96 +    }
    1.97 +
    1.98 +//   std::cout << "maximum flow: "<< std::endl;
    1.99 +//   for(EdgeIt e=G.first<EdgeIt>(); e.valid(); ++e) { 
   1.100 +//     std::cout<<"("<<G.tail(e)<< "-"<<flow.get(e)<<"->"<<G.head(e)<<") ";
   1.101 +//   }
   1.102 +//   std::cout<<std::endl;
   1.103 +    std::cout << "elapsed time: " << ts << std::endl;
   1.104 +    std::cout << "number of augmentation phases: " << i << std::endl; 
   1.105 +    std::cout << "flow value: "<< max_flow_test.flowValue() << std::endl;
   1.106 +  }
   1.107 +  
   1.108 +
   1.109 +  return 0;
   1.110 +}