[Lemon-commits] [lemon_svn] marci: r732 - hugo/trunk/src/work/marci

Lemon SVN svn at lemon.cs.elte.hu
Mon Nov 6 20:41:13 CET 2006


Author: marci
Date: Thu May  6 19:45:12 2004
New Revision: 732

Added:
   hugo/trunk/src/work/marci/max_bipartite_matching.h
      - copied, changed from r731, /hugo/trunk/src/work/marci/bipartite_matching_try_3.cc
Modified:
   hugo/trunk/src/work/marci/bipartite_matching_try_3.cc

Log:
A max bipartite matching class in src/work/marci/max_bipartite_matching.h
which can be used for computing maximum cardinality ordinary matching, b-matching and capacitated b-matching. 


Modified: hugo/trunk/src/work/marci/bipartite_matching_try_3.cc
==============================================================================
--- hugo/trunk/src/work/marci/bipartite_matching_try_3.cc	(original)
+++ hugo/trunk/src/work/marci/bipartite_matching_try_3.cc	Thu May  6 19:45:12 2004
@@ -13,73 +13,10 @@
 #include <hugo/maps.h>
 #include <max_flow.h>
 #include <graph_gen.h>
+#include <max_bipartite_matching.h>
 
 using namespace hugo;
 
-// template <typename Graph, typename EdgeCap, typename NodeCap, 
-// 	  typename EdgeFlow, typename NodeFlow>
-// class MaxMatching : public MaxFlow<stGraphWrapper<Graph>, 
-// 				   stGraphWrapper<Graph>:: EdgeMapWrapper<EdgeCan, NodeCap>, stGraphWrapper<Graph>::EdgeMapWrapper<EdgeFlow, NodeFlow> > {
-//   typedef MaxFlow<stGraphWrapper<Graph>, 
-// 		  stGraphWrapper<Graph>::EdgeMapWrapper<EdgeCan, NodeCap>, 
-// 		  stGraphWrapper<Graph>::EdgeMapWrapper<EdgeFlow, NodeFlow> >
-//   Parent;
-// protected:
-//   stGraphWrapper<Graph> gw;
-//   stGraphWrapper<Graph>::EdgeMapWrapper<EdgeCap, NodeCap> cap;
-//   stGraphWrapper<Graph>::EdgeMapWrapper<EdgeFlow, NodeFlow> flow;
-//   //graph* g;
-//   //EdgeCap* edge_cap;
-//   //EdgeFlow* edge_flow;
-// public:
-//   MaxMatching(Graph& _g, EdgeCap& _edge_cap, NodeCap& _node_cap, 
-// 	      EdgeFlow& _edge_flow, NodeFlow& _node_flow) : 
-//     MaxFlow(), gw(_g), 
-//     cap(_edge_cap, _node_cap), flow(_edge_flow, _node_flow) {
-//     Parent::set(gw, cap, flow);
-//   }
-// };
-
-template <typename Graph, typename EdgeCap, typename NodeCap, 
-	  typename EdgeFlow, typename NodeFlow>
-class MaxMatching {
-protected:
-//   EdgeCap* edge_cap;
-//   NodeCap* node_cap;
-//   EdgeFlow* edge_flow;
-//   NodeFlow* node_flow;
-  typedef  stGraphWrapper<Graph> stGW;
-  stGW stgw;
-  typedef typename stGW::template EdgeMapWrapper<EdgeCap, NodeCap> CapMap; 
-  CapMap cap;
-  NodeFlow* node_flow;
-  typedef typename stGW::template EdgeMapWrapper<EdgeFlow, NodeFlow> FlowMap;
-  FlowMap flow;
-  MaxFlow<stGW, int, CapMap, FlowMap> mf;
-  //graph* g;
-  //EdgeCap* edge_cap;
-  //EdgeFlow* edge_flow;
-public:
-  MaxMatching(Graph& _g, EdgeCap& _edge_cap, NodeCap& _node_cap, 
-	      EdgeFlow& _edge_flow, NodeFlow& _node_flow) : 
-    stgw(_g), 
-    cap(_edge_cap, _node_cap), 
-    node_flow(0), 
-    flow(_edge_flow, _node_flow), 
-    mf(stgw, stgw.S_NODE, stgw.T_NODE, cap, flow) { }
-  MaxMatching(Graph& _g, EdgeCap& _edge_cap, NodeCap& _node_cap, 
-	      EdgeFlow& _edge_flow/*, NodeFlow& _node_flow*/) : 
-    stgw(_g), 
-    cap(_edge_cap, _node_cap), 
-    node_flow(new NodeFlow(_g)), 
-    flow(_edge_flow, *node_flow), 
-    mf(stgw, stgw.S_NODE, stgw.T_NODE, cap, flow) { }
-  ~MaxMatching() { if (node_flow) delete node_flow; }
-  void run() { mf.run(); } 
-  int matchingValue() { return mf.flowValue(); }
-};
-
-
 int main() {
   //typedef UndirListGraph Graph; 
   typedef BipartiteGraph<ListGraph> Graph;

Copied: hugo/trunk/src/work/marci/max_bipartite_matching.h (from r731, /hugo/trunk/src/work/marci/bipartite_matching_try_3.cc)
==============================================================================
--- /hugo/trunk/src/work/marci/bipartite_matching_try_3.cc	(original)
+++ hugo/trunk/src/work/marci/max_bipartite_matching.h	Thu May  6 19:45:12 2004
@@ -1,225 +1,96 @@
 // -*- c++ -*-
-#include <iostream>
-#include <fstream>
-#include <vector>
-
-#include <list_graph.h>
-//#include <smart_graph.h>
-//#include <dimacs.h>
-#include <hugo/time_measure.h>
-#include <for_each_macros.h>
-#include <bfs_iterator.h>
+#ifndef HUGO_MAX_BIPARTITE_MATCHING_H
+#define HUGO_MAX_BIPARTITE_MATCHING_H
+
+//#include <for_each_macros.h>
 #include <bipartite_graph_wrapper.h>
-#include <hugo/maps.h>
+//#include <hugo/maps.h>
 #include <max_flow.h>
-#include <graph_gen.h>
 
-using namespace hugo;
+namespace hugo {
+
+  // template <typename Graph, typename EdgeCap, typename NodeCap, 
+  // 	  typename EdgeFlow, typename NodeFlow>
+  // class MaxMatching : public MaxFlow<stGraphWrapper<Graph>, 
+  // 				   stGraphWrapper<Graph>:: EdgeMapWrapper<EdgeCan, NodeCap>, stGraphWrapper<Graph>::EdgeMapWrapper<EdgeFlow, NodeFlow> > {
+  //   typedef MaxFlow<stGraphWrapper<Graph>, 
+  // 		  stGraphWrapper<Graph>::EdgeMapWrapper<EdgeCan, NodeCap>, 
+  // 		  stGraphWrapper<Graph>::EdgeMapWrapper<EdgeFlow, NodeFlow> >
+  //   Parent;
+  // protected:
+  //   stGraphWrapper<Graph> gw;
+  //   stGraphWrapper<Graph>::EdgeMapWrapper<EdgeCap, NodeCap> cap;
+  //   stGraphWrapper<Graph>::EdgeMapWrapper<EdgeFlow, NodeFlow> flow;
+  //   //graph* g;
+  //   //EdgeCap* edge_cap;
+  //   //EdgeFlow* edge_flow;
+  // public:
+  //   MaxMatching(Graph& _g, EdgeCap& _edge_cap, NodeCap& _node_cap, 
+  // 	      EdgeFlow& _edge_flow, NodeFlow& _node_flow) : 
+  //     MaxFlow(), gw(_g), 
+  //     cap(_edge_cap, _node_cap), flow(_edge_flow, _node_flow) {
+  //     Parent::set(gw, cap, flow);
+  //   }
+  // };
+
+  /// A bipartite matching class.
+  /// This class reduces the matching problem to a flow problem and 
+  /// a preflow is used on a wrapper. Such a generic approach means that 
+  /// matchings, b-matchings an capacitated b-matchings can be handled in 
+  /// a similar way. Due to the efficiency of the preflow algorithm, an 
+  /// efficient matching framework is obtained.
+  template <typename Graph, typename EdgeCap, typename NodeCap, 
+	    typename EdgeFlow, typename NodeFlow>
+  class MaxMatching {
+  protected:
+    //   EdgeCap* edge_cap;
+    //   NodeCap* node_cap;
+    //   EdgeFlow* edge_flow;
+    //   NodeFlow* node_flow;
+    typedef  stGraphWrapper<Graph> stGW;
+    stGW stgw;
+    typedef typename stGW::template EdgeMapWrapper<EdgeCap, NodeCap> CapMap; 
+    CapMap cap;
+    NodeFlow* node_flow;
+    typedef typename stGW::template EdgeMapWrapper<EdgeFlow, NodeFlow> FlowMap;
+    FlowMap flow;
+    MaxFlow<stGW, int, CapMap, FlowMap> mf;
+    //graph* g;
+    //EdgeCap* edge_cap;
+    //EdgeFlow* edge_flow;
+  public:
+    /// For capacitated b-matchings, edge-caoacities and node-capacities 
+    /// have to be given. After running \c run the matching is is given 
+    /// back in the edge-map \c _edge_flow and \c _node_map can be used 
+    /// to obtain saturation information about nodes.
+    ///\bug Note that the values in _edge_flow and _node_flow have 
+    /// to form a flow.
+    MaxMatching(Graph& _g, EdgeCap& _edge_cap, NodeCap& _node_cap, 
+		EdgeFlow& _edge_flow, NodeFlow& _node_flow) : 
+      stgw(_g), 
+      cap(_edge_cap, _node_cap), 
+      node_flow(0), 
+      flow(_edge_flow, _node_flow), 
+      mf(stgw, stgw.S_NODE, stgw.T_NODE, cap, flow) { }
+    /// If the saturation information of nodes is not needed that the use of 
+    /// this constructor is more comfortable.
+    ///\bug Note that the values in _edge_flow and _node_flow have 
+    /// to form a flow.
+    MaxMatching(Graph& _g, EdgeCap& _edge_cap, NodeCap& _node_cap, 
+		EdgeFlow& _edge_flow/*, NodeFlow& _node_flow*/) : 
+      stgw(_g), 
+      cap(_edge_cap, _node_cap), 
+      node_flow(new NodeFlow(_g)), 
+      flow(_edge_flow, *node_flow), 
+      mf(stgw, stgw.S_NODE, stgw.T_NODE, cap, flow) { }
+    /// The class have a nontrivial destructor.
+    ~MaxMatching() { if (node_flow) delete node_flow; }
+    /// run computes the max matching.
+    void run() { mf.run(); } 
+    /// The matching value after running \c run.
+    int matchingValue() { return mf.flowValue(); }
+  };
 
-// template <typename Graph, typename EdgeCap, typename NodeCap, 
-// 	  typename EdgeFlow, typename NodeFlow>
-// class MaxMatching : public MaxFlow<stGraphWrapper<Graph>, 
-// 				   stGraphWrapper<Graph>:: EdgeMapWrapper<EdgeCan, NodeCap>, stGraphWrapper<Graph>::EdgeMapWrapper<EdgeFlow, NodeFlow> > {
-//   typedef MaxFlow<stGraphWrapper<Graph>, 
-// 		  stGraphWrapper<Graph>::EdgeMapWrapper<EdgeCan, NodeCap>, 
-// 		  stGraphWrapper<Graph>::EdgeMapWrapper<EdgeFlow, NodeFlow> >
-//   Parent;
-// protected:
-//   stGraphWrapper<Graph> gw;
-//   stGraphWrapper<Graph>::EdgeMapWrapper<EdgeCap, NodeCap> cap;
-//   stGraphWrapper<Graph>::EdgeMapWrapper<EdgeFlow, NodeFlow> flow;
-//   //graph* g;
-//   //EdgeCap* edge_cap;
-//   //EdgeFlow* edge_flow;
-// public:
-//   MaxMatching(Graph& _g, EdgeCap& _edge_cap, NodeCap& _node_cap, 
-// 	      EdgeFlow& _edge_flow, NodeFlow& _node_flow) : 
-//     MaxFlow(), gw(_g), 
-//     cap(_edge_cap, _node_cap), flow(_edge_flow, _node_flow) {
-//     Parent::set(gw, cap, flow);
-//   }
-// };
-
-template <typename Graph, typename EdgeCap, typename NodeCap, 
-	  typename EdgeFlow, typename NodeFlow>
-class MaxMatching {
-protected:
-//   EdgeCap* edge_cap;
-//   NodeCap* node_cap;
-//   EdgeFlow* edge_flow;
-//   NodeFlow* node_flow;
-  typedef  stGraphWrapper<Graph> stGW;
-  stGW stgw;
-  typedef typename stGW::template EdgeMapWrapper<EdgeCap, NodeCap> CapMap; 
-  CapMap cap;
-  NodeFlow* node_flow;
-  typedef typename stGW::template EdgeMapWrapper<EdgeFlow, NodeFlow> FlowMap;
-  FlowMap flow;
-  MaxFlow<stGW, int, CapMap, FlowMap> mf;
-  //graph* g;
-  //EdgeCap* edge_cap;
-  //EdgeFlow* edge_flow;
-public:
-  MaxMatching(Graph& _g, EdgeCap& _edge_cap, NodeCap& _node_cap, 
-	      EdgeFlow& _edge_flow, NodeFlow& _node_flow) : 
-    stgw(_g), 
-    cap(_edge_cap, _node_cap), 
-    node_flow(0), 
-    flow(_edge_flow, _node_flow), 
-    mf(stgw, stgw.S_NODE, stgw.T_NODE, cap, flow) { }
-  MaxMatching(Graph& _g, EdgeCap& _edge_cap, NodeCap& _node_cap, 
-	      EdgeFlow& _edge_flow/*, NodeFlow& _node_flow*/) : 
-    stgw(_g), 
-    cap(_edge_cap, _node_cap), 
-    node_flow(new NodeFlow(_g)), 
-    flow(_edge_flow, *node_flow), 
-    mf(stgw, stgw.S_NODE, stgw.T_NODE, cap, flow) { }
-  ~MaxMatching() { if (node_flow) delete node_flow; }
-  void run() { mf.run(); } 
-  int matchingValue() { return mf.flowValue(); }
-};
-
-
-int main() {
-  //typedef UndirListGraph Graph; 
-  typedef BipartiteGraph<ListGraph> Graph;
-  
-  typedef Graph::Node Node;
-  typedef Graph::NodeIt NodeIt;
-  typedef Graph::Edge Edge;
-  typedef Graph::EdgeIt EdgeIt;
-  typedef Graph::OutEdgeIt OutEdgeIt;
-
-  Graph g;
-
-  int a;
-  std::cout << "number of nodes in the first color class=";
-  std::cin >> a; 
-  int b;
-  std::cout << "number of nodes in the second color class=";
-  std::cin >> b; 
-  int m;
-  std::cout << "number of edges=";
-  std::cin >> m; 
-  
-  std::cout << "Generatig a random bipartite graph..." << std::endl;
-  random_init();
-  randomBipartiteGraph(g, a, b, m);
-
-//   std::cout << "Edges of the bipartite graph:" << std::endl;
-//   FOR_EACH_LOC(EdgeIt, e, g) std::cout << e << " ";
-//   std::cout << std::endl;
-
-//   std::cout << "Nodes:" << std::endl;
-//   FOR_EACH_LOC(Graph::NodeIt, v, g) std::cout << v << " ";
-//   std::cout << std::endl;
-//   std::cout << "Nodes in T:" << std::endl;
-//   FOR_EACH_INC_LOC(Graph::ClassNodeIt, v, g, Graph::T_CLASS) std::cout << v << " ";
-//   std::cout << std::endl;
-//   std::cout << "Nodes in S:" << std::endl;
-//   FOR_EACH_INC_LOC(Graph::ClassNodeIt, v, g, Graph::S_CLASS) std::cout << v << " ";
-//   std::cout << std::endl;
-
-//   std::cout << "Erasing the first node..." << std::endl;
-//   NodeIt n;
-//   g.first(n);
-//   g.erase(n);
-//   std::cout << "Nodes of the bipartite graph:" << std::endl;
-//   FOR_EACH_GLOB(n, g) std::cout << n << " ";
-//   std::cout << std::endl;
-
-//   std::cout << "Nodes in T:" << std::endl;
-//   FOR_EACH_INC_LOC(Graph::ClassNodeIt, v, g, Graph::T_CLASS) std::cout << v << " ";
-//   std::cout << std::endl;
-//   std::cout << "Nodes in S:" << std::endl;
-//   FOR_EACH_INC_LOC(Graph::ClassNodeIt, v, g, Graph::S_CLASS) std::cout << v << " ";
-//   std::cout << std::endl;
-
-  typedef stGraphWrapper<Graph> stGW;
-  stGW stgw(g);
-  ConstMap<stGW::Edge, int> const1map(1);
-
-  Timer ts;
-  ts.reset();
-  stGW::EdgeMap<int> flow(stgw);
-  MaxFlow<stGW, int, ConstMap<stGW::Edge, int>, stGW::EdgeMap<int> > 
-    max_flow_test(stgw, stgw.S_NODE, stgw.T_NODE, const1map, flow);
-  max_flow_test.run();
-//  while (max_flow_test.augmentOnShortestPath()) { }
-//  typedef ListGraph MutableGraph;
-//  while (max_flow_test.augmentOnBlockingFlow1<MutableGraph>()) {
-//  while (max_flow_test.augmentOnBlockingFlow2()) {
-//   std::cout << max_flow_test.flowValue() << std::endl;
-//  }
-  std::cout << "max flow value: " << max_flow_test.flowValue() << std::endl;
-  std::cout << "elapsed time: " << ts << std::endl;
-//   FOR_EACH_LOC(stGW::EdgeIt, e, stgw) { 
-//     if (flow[e]) std::cout << e << std::endl; 
-//   }
-  std::cout << std::endl;
-
-  typedef ConstMap<Graph::Edge, int> EdgeCap; 
-  EdgeCap ge1(1);
-  typedef ConstMap<Graph::Node, int> NodeCap;
-  NodeCap gn1(1);
-  typedef Graph::EdgeMap<int> EdgeFlow;
-  EdgeFlow gef(g); //0
-  typedef Graph::NodeMap<int> NodeFlow; 
-  NodeFlow gnf(g); //0 
-
-  typedef stGraphWrapper<Graph>::EdgeMapWrapper<EdgeCap, NodeCap> CapMap; 
-  typedef stGraphWrapper<Graph>::EdgeMapWrapper<EdgeFlow, NodeFlow> FlowMap; 
-  CapMap cm(ge1, gn1);
-  FlowMap fm(gef, gnf);
-
-  //Timer ts;
-  ts.reset();
-  //stGW::EdgeMap<int> flow(stgw);
-  MaxFlow<stGW, int, CapMap, FlowMap> 
-    max_flow_test1(stgw, stgw.S_NODE, stgw.T_NODE, cm, fm);
-  max_flow_test1.run();
-//  while (max_flow_test.augmentOnShortestPath()) { }
-//  typedef ListGraph MutableGraph;
-//  while (max_flow_test.augmentOnBlockingFlow1<MutableGraph>()) {
-//  while (max_flow_test.augmentOnBlockingFlow2()) {
-//   std::cout << max_flow_test.flowValue() << std::endl;
-//  }
-  std::cout << "max flow value: " << max_flow_test1.flowValue() << std::endl;
-  std::cout << "elapsed time: " << ts << std::endl;
-//   FOR_EACH_LOC(Graph::EdgeIt, e, g) { 
-//     if (gef[e]) std::cout << e << std::endl; 
-//   }
-  std::cout << std::endl;
-
-  ts.reset();
-  FOR_EACH_LOC(Graph::EdgeIt, e, g) gef.set(e, 0); 
-  FOR_EACH_LOC(Graph::NodeIt, n, g) gnf.set(n, 0); 
-  MaxMatching<Graph, ConstMap<Graph::Edge, int>, ConstMap<Graph::Node, int>, 
-    Graph::EdgeMap<int>, Graph::NodeMap<int> > 
-    matching_test(g, ge1, gn1, gef, gnf);
-  matching_test.run();
-
-  std::cout << "max flow value: " << matching_test.matchingValue() << std::endl;
-  std::cout << "elapsed time: " << ts << std::endl;
-//   FOR_EACH_LOC(Graph::EdgeIt, e, g) { 
-//     if (gef[e]) std::cout << e << std::endl; 
-//   }
-  std::cout << std::endl;
-
-  ts.reset();
-  FOR_EACH_LOC(Graph::EdgeIt, e, g) gef.set(e, 0); 
-  //FOR_EACH_LOC(Graph::NodeIt, n, g) gnf.set(n, 0); 
-  MaxMatching<Graph, ConstMap<Graph::Edge, int>, ConstMap<Graph::Node, int>, 
-    Graph::EdgeMap<int>, Graph::NodeMap<int> > 
-    matching_test_1(g, ge1, gn1, gef/*, gnf*/);
-  matching_test_1.run();
-
-  std::cout << "max flow value: " << matching_test_1.matchingValue() << std::endl;
-  std::cout << "elapsed time: " << ts << std::endl;
-//   FOR_EACH_LOC(Graph::EdgeIt, e, g) { 
-//     if (gef[e]) std::cout << e << std::endl; 
-//   }
-  std::cout << std::endl;
+} //namespace hugo
 
-  return 0;
-}
+#endif //HUGO_MAX_BIPARTITE_MATCHING_H



More information about the Lemon-commits mailing list