src/work/marci/bipartite_matching_try_3.cc
author athos
Mon, 03 May 2004 10:27:20 +0000
changeset 511 325c9430723e
child 512 d5fe2f3f95fc
permissions -rw-r--r--
getPath() function implemented.
     1 // -*- c++ -*-
     2 #include <iostream>
     3 #include <fstream>
     4 #include <vector>
     5 #include <cstdlib>
     6 
     7 #include <list_graph.h>
     8 //#include <smart_graph.h>
     9 //#include <dimacs.h>
    10 #include <time_measure.h>
    11 #include <for_each_macros.h>
    12 #include <bfs_iterator.h>
    13 #include <bipartite_graph_wrapper.h>
    14 #include <maps.h>
    15 #include <max_flow.h>
    16 
    17 using namespace hugo;
    18 
    19 // template <typename Graph, typename EdgeCap, typename NodeCap, 
    20 // 	  typename EdgeFlow, typename NodeFlow>
    21 // class MaxMatching : public MaxFlow<stGraphWrapper<Graph>, 
    22 // 				   stGraphWrapper<Graph>:: EdgeMapWrapper<EdgeCan, NodeCap>, stGraphWrapper<Graph>::EdgeMapWrapper<EdgeFlow, NodeFlow> > {
    23 //   typedef MaxFlow<stGraphWrapper<Graph>, 
    24 // 		  stGraphWrapper<Graph>::EdgeMapWrapper<EdgeCan, NodeCap>, 
    25 // 		  stGraphWrapper<Graph>::EdgeMapWrapper<EdgeFlow, NodeFlow> >
    26 //   Parent;
    27 // protected:
    28 //   stGraphWrapper<Graph> gw;
    29 //   stGraphWrapper<Graph>::EdgeMapWrapper<EdgeCap, NodeCap> cap;
    30 //   stGraphWrapper<Graph>::EdgeMapWrapper<EdgeFlow, NodeFlow> flow;
    31 //   //graph* g;
    32 //   //EdgeCap* edge_cap;
    33 //   //EdgeFlow* edge_flow;
    34 // public:
    35 //   MaxMatching(Graph& _g, EdgeCap& _edge_cap, NodeCap& _node_cap, 
    36 // 	      EdgeFlow& _edge_flow, NodeFlow& _node_flow) : 
    37 //     MaxFlow(), gw(_g), 
    38 //     cap(_edge_cap, _node_cap), flow(_edge_flow, _node_flow) {
    39 //     Parent::set(gw, cap, flow);
    40 //   }
    41 // };
    42 
    43 template <typename Graph, typename EdgeCap, typename NodeCap, 
    44 	  typename EdgeFlow, typename NodeFlow>
    45 class MaxMatching {
    46 // : public MaxFlow<stGraphWrapper<Graph>, 
    47 // 				   stGraphWrapper<Graph>:: EdgeMapWrapper<EdgeCan, NodeCap>, stGraphWrapper<Graph>::EdgeMapWrapper<EdgeFlow, NodeFlow> > {
    48 //   typedef MaxFlow<stGraphWrapper<Graph>, 
    49 // 		  stGraphWrapper<Graph>::EdgeMapWrapper<EdgeCan, NodeCap>, 
    50 // 		  stGraphWrapper<Graph>::EdgeMapWrapper<EdgeFlow, NodeFlow> >
    51 //   Parent;
    52 protected:
    53 //   EdgeCap* edge_cap;
    54 //   NodeCap* node_cap;
    55 //   EdgeFlow* edge_flow;
    56 //   NodeFlow* node_flow;
    57   typedef  stGraphWrapper<Graph> stGW;
    58   stGW stgw;
    59   typedef typename stGW::template EdgeMapWrapper<EdgeCap, NodeCap> CapMap; 
    60   CapMap cap;
    61   typedef typename stGW::template EdgeMapWrapper<EdgeFlow, NodeFlow> FlowMap;
    62   FlowMap flow;
    63   MaxFlow<stGW, int, CapMap, FlowMap> mf;
    64   //graph* g;
    65   //EdgeCap* edge_cap;
    66   //EdgeFlow* edge_flow;
    67 public:
    68   MaxMatching(Graph& _g, EdgeCap& _edge_cap, NodeCap& _node_cap, 
    69 	      EdgeFlow& _edge_flow, NodeFlow& _node_flow) : 
    70     stgw(_g), 
    71     cap(_edge_cap, _node_cap), flow(_edge_flow, _node_flow), 
    72     mf(stgw, stgw.S_NODE, stgw.T_NODE, cap, flow) { }
    73   void run() { mf.run(); } 
    74   int matchingValue() { return mf.flowValue(); }
    75 };
    76 
    77 /**
    78  * Inicializalja a veletlenszamgeneratort.
    79  * Figyelem, ez nem jo igazi random szamokhoz,
    80  * erre ne bizzad a titkaidat!
    81  */
    82 void random_init()
    83 {
    84 	unsigned int seed = getpid();
    85 	seed |= seed << 15;
    86 	seed ^= time(0);
    87 
    88 	srand(seed);
    89 }
    90 
    91 /**
    92  * Egy veletlen int-et ad vissza 0 es m-1 kozott.
    93  */
    94 int random(int m)
    95 {
    96   return int( double(m) * rand() / (RAND_MAX + 1.0) );
    97 }
    98 
    99 int main() {
   100   //typedef UndirListGraph Graph; 
   101   typedef BipartiteGraph<ListGraph> Graph;
   102   
   103   typedef Graph::Node Node;
   104   typedef Graph::NodeIt NodeIt;
   105   typedef Graph::Edge Edge;
   106   typedef Graph::EdgeIt EdgeIt;
   107   typedef Graph::OutEdgeIt OutEdgeIt;
   108 
   109   Graph g;
   110 
   111   std::vector<Graph::Node> s_nodes;
   112   std::vector<Graph::Node> t_nodes;
   113 
   114   int a;
   115   std::cout << "number of nodes in the first color class=";
   116   std::cin >> a; 
   117   int b;
   118   std::cout << "number of nodes in the second color class=";
   119   std::cin >> b; 
   120   int m;
   121   std::cout << "number of edges=";
   122   std::cin >> m; 
   123   
   124   std::cout << "Generatig a random bipartite graph..." << std::endl;
   125   for (int i=0; i<a; ++i) s_nodes.push_back(g.addNode(false));
   126   for (int i=0; i<b; ++i) t_nodes.push_back(g.addNode(true));
   127 
   128   random_init();
   129   for(int i=0; i<m; ++i) {
   130     g.addEdge(s_nodes[random(a)], t_nodes[random(b)]);
   131   }
   132 
   133   std::cout << "Edges of the bipartite graph:" << std::endl;
   134   FOR_EACH_LOC(EdgeIt, e, g) std::cout << e << " ";
   135   std::cout << std::endl;
   136 
   137   std::cout << "Nodes:" << std::endl;
   138   FOR_EACH_LOC(Graph::NodeIt, v, g) std::cout << v << " ";
   139   std::cout << std::endl;
   140   std::cout << "Nodes in T:" << std::endl;
   141   FOR_EACH_INC_LOC(Graph::ClassNodeIt, v, g, Graph::T_CLASS) std::cout << v << " ";
   142   std::cout << std::endl;
   143   std::cout << "Nodes in S:" << std::endl;
   144   FOR_EACH_INC_LOC(Graph::ClassNodeIt, v, g, Graph::S_CLASS) std::cout << v << " ";
   145   std::cout << std::endl;
   146 
   147   std::cout << "Erasing the first node..." << std::endl;
   148   NodeIt n;
   149   g.first(n);
   150   g.erase(n);
   151   std::cout << "Nodes of the bipartite graph:" << std::endl;
   152   FOR_EACH_GLOB(n, g) std::cout << n << " ";
   153   std::cout << std::endl;
   154 
   155   std::cout << "Nodes in T:" << std::endl;
   156   FOR_EACH_INC_LOC(Graph::ClassNodeIt, v, g, Graph::T_CLASS) std::cout << v << " ";
   157   std::cout << std::endl;
   158   std::cout << "Nodes in S:" << std::endl;
   159   FOR_EACH_INC_LOC(Graph::ClassNodeIt, v, g, Graph::S_CLASS) std::cout << v << " ";
   160   std::cout << std::endl;
   161 
   162   typedef stGraphWrapper<Graph> stGW;
   163   stGW stgw(g);
   164   ConstMap<stGW::Edge, int> const1map(1);
   165 
   166   Timer ts;
   167   ts.reset();
   168   stGW::EdgeMap<int> flow(stgw);
   169   MaxFlow<stGW, int, ConstMap<stGW::Edge, int>, stGW::EdgeMap<int> > 
   170     max_flow_test(stgw, stgw.S_NODE, stgw.T_NODE, const1map, flow);
   171   max_flow_test.run();
   172 //  while (max_flow_test.augmentOnShortestPath()) { }
   173 //  typedef ListGraph MutableGraph;
   174 //  while (max_flow_test.augmentOnBlockingFlow1<MutableGraph>()) {
   175 //  while (max_flow_test.augmentOnBlockingFlow2()) {
   176 //   std::cout << max_flow_test.flowValue() << std::endl;
   177 //  }
   178   std::cout << "max flow value: " << max_flow_test.flowValue() << std::endl;
   179   std::cout << "elapsed time: " << ts << std::endl;
   180   FOR_EACH_LOC(stGW::EdgeIt, e, stgw) { 
   181     if (flow[e]) std::cout << e << std::endl; 
   182   }
   183   std::cout << std::endl;
   184 
   185   typedef ConstMap<Graph::Edge, int> EdgeCap; 
   186   EdgeCap ge1(1);
   187   typedef ConstMap<Graph::Node, int> NodeCap;
   188   NodeCap gn1(1);
   189   typedef Graph::EdgeMap<int> EdgeFlow;
   190   EdgeFlow gef(g); //0
   191   typedef Graph::NodeMap<int> NodeFlow; 
   192   NodeFlow gnf(g); //0 
   193 
   194   typedef stGraphWrapper<Graph>::EdgeMapWrapper<EdgeCap, NodeCap> CapMap; 
   195   typedef stGraphWrapper<Graph>::EdgeMapWrapper<EdgeFlow, NodeFlow> FlowMap; 
   196   CapMap cm(ge1, gn1);
   197   FlowMap fm(gef, gnf);
   198 
   199   //Timer ts;
   200   ts.reset();
   201   //stGW::EdgeMap<int> flow(stgw);
   202   MaxFlow<stGW, int, CapMap, FlowMap> 
   203     max_flow_test1(stgw, stgw.S_NODE, stgw.T_NODE, cm, fm);
   204   max_flow_test1.run();
   205 //  while (max_flow_test.augmentOnShortestPath()) { }
   206 //  typedef ListGraph MutableGraph;
   207 //  while (max_flow_test.augmentOnBlockingFlow1<MutableGraph>()) {
   208 //  while (max_flow_test.augmentOnBlockingFlow2()) {
   209 //   std::cout << max_flow_test.flowValue() << std::endl;
   210 //  }
   211   std::cout << "max flow value: " << max_flow_test1.flowValue() << std::endl;
   212   std::cout << "elapsed time: " << ts << std::endl;
   213 //   FOR_EACH_LOC(Graph::EdgeIt, e, g) { 
   214 //     if (gef[e]) std::cout << e << std::endl; 
   215 //   }
   216   std::cout << std::endl;
   217 
   218   ts.reset();
   219   FOR_EACH_LOC(Graph::EdgeIt, e, g) gef.set(e, 0); 
   220   FOR_EACH_LOC(Graph::NodeIt, n, g) gnf.set(n, 0); 
   221   MaxMatching<Graph, ConstMap<Graph::Edge, int>, ConstMap<Graph::Node, int>, 
   222     Graph::EdgeMap<int>, Graph::NodeMap<int> > 
   223     matching_test(g, ge1, gn1, gef, gnf);
   224   matching_test.run();
   225 
   226   std::cout << "max flow value: " << matching_test.matchingValue() << std::endl;
   227   std::cout << "elapsed time: " << ts << std::endl;
   228 //   FOR_EACH_LOC(Graph::EdgeIt, e, g) { 
   229 //     if (gef[e]) std::cout << e << std::endl; 
   230 //   }
   231   std::cout << std::endl;
   232   return 0;
   233 }