bipartite graphs
authormarci
Fri, 30 Apr 2004 16:46:19 +0000
changeset 498eb8bfa683d92
parent 497 500456d50d21
child 499 767f3da8ce0e
bipartite graphs
src/work/marci/bipartite_graph_wrapper.h
src/work/marci/bipartite_matching_try.cc
src/work/marci/bipartite_matching_try_2.cc
src/work/marci/makefile
     1.1 --- a/src/work/marci/bipartite_graph_wrapper.h	Fri Apr 30 16:10:49 2004 +0000
     1.2 +++ b/src/work/marci/bipartite_graph_wrapper.h	Fri Apr 30 16:46:19 2004 +0000
     1.3 @@ -205,14 +205,26 @@
     1.4  
     1.5      /// the \c bool parameter which can be \c S_Class or \c T_Class shows 
     1.6      /// the color class where the new node is to be inserted.
     1.7 -    void addNode(bool);
     1.8 +    Node addNode(bool b) {
     1.9 +      Node n=Parent::graph->addNode();
    1.10 +      bipartite_map.update();
    1.11 +      s_false_t_true_map.insert(n, b);
    1.12 +      return n;
    1.13 +    }
    1.14  
    1.15      /// A new edge is inserted.
    1.16      ///\pre \c tail have to be in \c S_Class and \c head in \c T_Class.
    1.17 -    void addEdge(const Node& tail, const Node& head);
    1.18 +    Edge addEdge(const Node& tail, const Node& head) {
    1.19 +      return Parent::graph->addEdge(tail, head);
    1.20 +    }
    1.21  
    1.22 -    void erase(const Node&);
    1.23 -    void erase(const Edge&);
    1.24 +    void erase(const Node& n) {
    1.25 +      s_false_t_true_map.remove(n);
    1.26 +      Parent::graph->erase(n);
    1.27 +    }
    1.28 +    void erase(const Edge& e) {
    1.29 +      Parent::graph->erase(e);
    1.30 +    }
    1.31      
    1.32      void clear() {
    1.33        FOR_EACH_LOC(typename Parent::EdgeIt, e, G) erase(e);
     2.1 --- a/src/work/marci/bipartite_matching_try.cc	Fri Apr 30 16:10:49 2004 +0000
     2.2 +++ b/src/work/marci/bipartite_matching_try.cc	Fri Apr 30 16:46:19 2004 +0000
     2.3 @@ -112,8 +112,8 @@
     2.4  //     std::cout << bgw.tail(e) << "->" << bgw.head(e) << std::endl;
     2.5  //   }
     2.6  
     2.7 -  BGW::NodeMap<int> dbyj(bgw);
     2.8 -  BGW::EdgeMap<int> dbyxcj(bgw);
     2.9 +//  BGW::NodeMap<int> dbyj(bgw);
    2.10 +//  BGW::EdgeMap<int> dbyxcj(bgw);
    2.11  
    2.12    typedef stGraphWrapper<BGW> stGW;
    2.13    stGW stgw(bgw);
     3.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     3.2 +++ b/src/work/marci/bipartite_matching_try_2.cc	Fri Apr 30 16:46:19 2004 +0000
     3.3 @@ -0,0 +1,194 @@
     3.4 +// -*- c++ -*-
     3.5 +#include <iostream>
     3.6 +#include <fstream>
     3.7 +#include <vector>
     3.8 +#include <cstdlib>
     3.9 +
    3.10 +#include <list_graph.h>
    3.11 +//#include <smart_graph.h>
    3.12 +//#include <dimacs.h>
    3.13 +#include <time_measure.h>
    3.14 +#include <for_each_macros.h>
    3.15 +#include <bfs_iterator.h>
    3.16 +#include <graph_wrapper.h>
    3.17 +#include <bipartite_graph_wrapper.h>
    3.18 +#include <maps.h>
    3.19 +#include <max_flow.h>
    3.20 +
    3.21 +/**
    3.22 + * Inicializalja a veletlenszamgeneratort.
    3.23 + * Figyelem, ez nem jo igazi random szamokhoz,
    3.24 + * erre ne bizzad a titkaidat!
    3.25 + */
    3.26 +void random_init()
    3.27 +{
    3.28 +	unsigned int seed = getpid();
    3.29 +	seed |= seed << 15;
    3.30 +	seed ^= time(0);
    3.31 +
    3.32 +	srand(seed);
    3.33 +}
    3.34 +
    3.35 +/**
    3.36 + * Egy veletlen int-et ad vissza 0 es m-1 kozott.
    3.37 + */
    3.38 +int random(int m)
    3.39 +{
    3.40 +  return int( double(m) * rand() / (RAND_MAX + 1.0) );
    3.41 +}
    3.42 +
    3.43 +using namespace hugo;
    3.44 +
    3.45 +int main() {
    3.46 +  typedef UndirListGraph Graph; 
    3.47 +  typedef Graph::Node Node;
    3.48 +  typedef Graph::NodeIt NodeIt;
    3.49 +  typedef Graph::Edge Edge;
    3.50 +  typedef Graph::EdgeIt EdgeIt;
    3.51 +  typedef Graph::OutEdgeIt OutEdgeIt;
    3.52 +
    3.53 +  Graph g;
    3.54 +
    3.55 +  std::vector<Graph::Node> s_nodes;
    3.56 +  std::vector<Graph::Node> t_nodes;
    3.57 +
    3.58 +  int a;
    3.59 +  std::cout << "number of nodes in the first color class=";
    3.60 +  std::cin >> a; 
    3.61 +  int b;
    3.62 +  std::cout << "number of nodes in the second color class=";
    3.63 +  std::cin >> b; 
    3.64 +  int m;
    3.65 +  std::cout << "number of edges=";
    3.66 +  std::cin >> m; 
    3.67 +  
    3.68 +
    3.69 +  for (int i=0; i<a; ++i) s_nodes.push_back(g.addNode());
    3.70 +  for (int i=0; i<b; ++i) t_nodes.push_back(g.addNode());
    3.71 +
    3.72 +  random_init();
    3.73 +  for(int i=0; i<m; ++i) {
    3.74 +    g.addEdge(s_nodes[random(a)], t_nodes[random(b)]);
    3.75 +  }
    3.76 +
    3.77 +  Graph::NodeMap<int> ref_map(g, -1);
    3.78 +
    3.79 +  IterableBoolMap< Graph::NodeMap<int> > bipartite_map(ref_map);
    3.80 +  for (int i=0; i<a; ++i) bipartite_map.insert(s_nodes[i], false);
    3.81 +  for (int i=0; i<b; ++i) bipartite_map.insert(t_nodes[i], true);
    3.82 +
    3.83 +//   Graph::Node u;
    3.84 +//   std::cout << "These nodes will be in S:\n";
    3.85 +//   //FIXME azert kellene ++, es invalid vizsgalat u-bol, hogy ezt le lehessen 
    3.86 +//   //irni 1etlen FOR_EACH-csel.
    3.87 +//   for (bipartite_map.first(u, false); g.valid(u); bipartite_map.next(u)) 
    3.88 +//     std::cout << u << " ";
    3.89 +//   std::cout << "\n";
    3.90 +//   std::cout << "These nodes will be in T:\n";
    3.91 +//   for (bipartite_map.first(u, true); g.valid(u); bipartite_map.next(u)) 
    3.92 +//     std::cout << u << " ";
    3.93 +//   std::cout << "\n";
    3.94 +
    3.95 +  typedef BipartiteGraphWrapper<Graph> BGW;
    3.96 +  BGW bgw(g, bipartite_map);
    3.97 +
    3.98 +//   std::cout << "Nodes by NodeIt:\n";
    3.99 +//   FOR_EACH_LOC(BGW::NodeIt, n, bgw) {
   3.100 +//     std::cout << n << " ";
   3.101 +//   }
   3.102 +//   std::cout << "\n";
   3.103 +//   std::cout << "Nodes in S by ClassNodeIt:\n";
   3.104 +//   FOR_EACH_INC_LOC(BGW::ClassNodeIt, n, bgw, bgw.S_CLASS) {
   3.105 +//     std::cout << n << " ";
   3.106 +//   }
   3.107 +//   std::cout << "\n";
   3.108 +//   std::cout << "Nodes in T by ClassNodeIt:\n";
   3.109 +//   FOR_EACH_INC_LOC(BGW::ClassNodeIt, n, bgw, bgw.T_CLASS) {
   3.110 +//     std::cout << n << " ";
   3.111 +//   }
   3.112 +//   std::cout << "\n";
   3.113 +//   std::cout << "Edges of the bipartite graph:\n";
   3.114 +//   FOR_EACH_LOC(BGW::EdgeIt, e, bgw) {
   3.115 +//     std::cout << bgw.tail(e) << "->" << bgw.head(e) << std::endl;
   3.116 +//   }
   3.117 +
   3.118 +//  BGW::NodeMap<int> dbyj(bgw);
   3.119 +//  BGW::EdgeMap<int> dbyxcj(bgw);
   3.120 +
   3.121 +  typedef stGraphWrapper<BGW> stGW;
   3.122 +  stGW stgw(bgw);
   3.123 +  ConstMap<stGW::Edge, int> const1map(1);
   3.124 +//  stGW::NodeMap<int> ize(stgw);
   3.125 +
   3.126 +//   BfsIterator< BGW, BGW::NodeMap<bool> > bfs(bgw);
   3.127 +//   Graph::NodeIt si;
   3.128 +//   Graph::Node s; 
   3.129 +//   s=g.first(si);
   3.130 +//   bfs.pushAndSetReached(BGW::Node(s));
   3.131 +//   while (!bfs.finished()) { ++bfs; }
   3.132 +
   3.133 +//   FOR_EACH_LOC(stGW::NodeIt, n, stgw) { 
   3.134 +//     std::cout << "out-edges of " << n << ":\n"; 
   3.135 +//     FOR_EACH_INC_LOC(stGW::OutEdgeIt, e, stgw, n) { 
   3.136 +//       std::cout << " " << e << "\n";
   3.137 +//       std::cout << " aNode: " << stgw.aNode(e) << "\n";
   3.138 +//       std::cout << " bNode: " << stgw.bNode(e) << "\n";      
   3.139 +//     }
   3.140 +//     std::cout << "in-edges of " << n << ":\n"; 
   3.141 +//     FOR_EACH_INC_LOC(stGW::InEdgeIt, e, stgw, n) { 
   3.142 +//       std::cout << " " << e << "\n";
   3.143 +//       std::cout << " aNode: " << stgw.aNode(e) << "\n";
   3.144 +//       std::cout << " bNode: " << stgw.bNode(e) << "\n";     
   3.145 +//     }
   3.146 +//   }
   3.147 +//   std::cout << "Edges of the stGraphWrapper:\n"; 
   3.148 +//   FOR_EACH_LOC(stGW::EdgeIt, n, stgw) { 
   3.149 +//     std::cout << " " << n << "\n";
   3.150 +//   }
   3.151 +
   3.152 +//   stGW::NodeMap<bool> b(stgw);
   3.153 +//   FOR_EACH_LOC(stGW::NodeIt, n, stgw) { 
   3.154 +//     std::cout << n << ": " << b[n] <<"\n";
   3.155 +//   }
   3.156 +
   3.157 +//   std::cout << "Bfs from s: \n";
   3.158 +//   BfsIterator< stGW, stGW::NodeMap<bool> > bfs_stgw(stgw);
   3.159 +//   bfs_stgw.pushAndSetReached(stgw.S_NODE);
   3.160 +//   while (!bfs_stgw.finished()) { 
   3.161 +//     std::cout << " " << stGW::OutEdgeIt(bfs_stgw) << "\n";
   3.162 +//     ++bfs_stgw; 
   3.163 +//   }
   3.164 +
   3.165 +
   3.166 +  Timer ts;
   3.167 +  ts.reset();
   3.168 +  stGW::EdgeMap<int> max_flow(stgw);
   3.169 +  MaxFlow<stGW, int, ConstMap<stGW::Edge, int>, stGW::EdgeMap<int> > 
   3.170 +    max_flow_test(stgw, stgw.S_NODE, stgw.T_NODE, const1map, max_flow);
   3.171 +//  while (max_flow_test.augmentOnShortestPath()) { }
   3.172 +  typedef ListGraph MutableGraph;
   3.173 +//  while (max_flow_test.augmentOnBlockingFlow1<MutableGraph>()) {
   3.174 +  while (max_flow_test.augmentOnBlockingFlow2()) {
   3.175 +   std::cout << max_flow_test.flowValue() << std::endl;
   3.176 +  }
   3.177 +  std::cout << "max flow value: " << max_flow_test.flowValue() << std::endl;
   3.178 +  std::cout << "elapsed time: " << ts << std::endl;
   3.179 +//   FOR_EACH_LOC(stGW::EdgeIt, e, stgw) { 
   3.180 +//     std::cout << e << ": " << max_flow[e] << "\n"; 
   3.181 +//   }
   3.182 +//   std::cout << "\n";
   3.183 +
   3.184 +  ts.reset();
   3.185 +  stGW::EdgeMap<int> pre_flow(stgw);
   3.186 +  MaxFlow<stGW, int, ConstMap<stGW::Edge, int>, stGW::EdgeMap<int> > 
   3.187 +    pre_flow_test(stgw, stgw.S_NODE, stgw.T_NODE, const1map, pre_flow/*, true*/);
   3.188 +  pre_flow_test.run();
   3.189 +  std::cout << "pre flow value: " << max_flow_test.flowValue() << std::endl;
   3.190 +  std::cout << "elapsed time: " << ts << std::endl;
   3.191 +//   FOR_EACH_LOC(stGW::EdgeIt, e, stgw) { 
   3.192 +//     std::cout << e << ": " << pre_flow[e] << "\n"; 
   3.193 +//   }
   3.194 +//   std::cout << "\n";
   3.195 +
   3.196 +  return 0;
   3.197 +}
     4.1 --- a/src/work/marci/makefile	Fri Apr 30 16:10:49 2004 +0000
     4.2 +++ b/src/work/marci/makefile	Fri Apr 30 16:46:19 2004 +0000
     4.3 @@ -4,7 +4,7 @@
     4.4  INCLUDEDIRS ?= -I../../include -I.. -I../{marci,jacint,alpar,klao,akos,athos} -I$(BOOSTROOT)
     4.5  
     4.6  LEDABINARIES = leda_graph_demo leda_bfs_dfs max_bipartite_matching_demo
     4.7 -BINARIES = max_flow_demo iterator_bfs_demo macro_test lg_vs_sg bfsit_vs_byhand bipartite_graph_wrapper_test bipartite_matching_try
     4.8 +BINARIES = max_flow_demo iterator_bfs_demo macro_test lg_vs_sg bfsit_vs_byhand bipartite_graph_wrapper_test bipartite_matching_try bipartite_matching_try_2
     4.9  #gw_vs_not preflow_demo_boost edmonds_karp_demo_boost preflow_demo_jacint preflow_demo_athos edmonds_karp_demo_alpar preflow_demo_leda
    4.10  
    4.11  include ../makefile