src/work/marci/max_bipartite_matching.h
author marci
Fri, 07 May 2004 10:57:31 +0000
changeset 576 d00c33d07114
parent 558 4cbfb435ec2b
child 613 b5b5c4ae5107
permissions -rw-r--r--
(none)
     1 // -*- c++ -*-
     2 #ifndef HUGO_MAX_BIPARTITE_MATCHING_H
     3 #define HUGO_MAX_BIPARTITE_MATCHING_H
     4 
     5 //#include <for_each_macros.h>
     6 #include <bipartite_graph_wrapper.h>
     7 //#include <hugo/maps.h>
     8 #include <max_flow.h>
     9 
    10 namespace hugo {
    11 
    12   // template <typename Graph, typename EdgeCap, typename NodeCap, 
    13   // 	  typename EdgeFlow, typename NodeFlow>
    14   // class MaxMatching : public MaxFlow<stGraphWrapper<Graph>, 
    15   // 				   stGraphWrapper<Graph>:: EdgeMapWrapper<EdgeCan, NodeCap>, stGraphWrapper<Graph>::EdgeMapWrapper<EdgeFlow, NodeFlow> > {
    16   //   typedef MaxFlow<stGraphWrapper<Graph>, 
    17   // 		  stGraphWrapper<Graph>::EdgeMapWrapper<EdgeCan, NodeCap>, 
    18   // 		  stGraphWrapper<Graph>::EdgeMapWrapper<EdgeFlow, NodeFlow> >
    19   //   Parent;
    20   // protected:
    21   //   stGraphWrapper<Graph> gw;
    22   //   stGraphWrapper<Graph>::EdgeMapWrapper<EdgeCap, NodeCap> cap;
    23   //   stGraphWrapper<Graph>::EdgeMapWrapper<EdgeFlow, NodeFlow> flow;
    24   //   //graph* g;
    25   //   //EdgeCap* edge_cap;
    26   //   //EdgeFlow* edge_flow;
    27   // public:
    28   //   MaxMatching(Graph& _g, EdgeCap& _edge_cap, NodeCap& _node_cap, 
    29   // 	      EdgeFlow& _edge_flow, NodeFlow& _node_flow) : 
    30   //     MaxFlow(), gw(_g), 
    31   //     cap(_edge_cap, _node_cap), flow(_edge_flow, _node_flow) {
    32   //     Parent::set(gw, cap, flow);
    33   //   }
    34   // };
    35 
    36   /// A bipartite matching class.
    37   /// This class reduces the matching problem to a flow problem and 
    38   /// a preflow is used on a wrapper. Such a generic approach means that 
    39   /// matchings, b-matchings an capacitated b-matchings can be handled in 
    40   /// a similar way. Due to the efficiency of the preflow algorithm, an 
    41   /// efficient matching framework is obtained.
    42   template <typename Graph, typename EdgeCap, typename NodeCap, 
    43 	    typename EdgeFlow, typename NodeFlow>
    44   class MaxMatching {
    45   protected:
    46     //   EdgeCap* edge_cap;
    47     //   NodeCap* node_cap;
    48     //   EdgeFlow* edge_flow;
    49     //   NodeFlow* node_flow;
    50     typedef  stGraphWrapper<Graph> stGW;
    51     stGW stgw;
    52     typedef typename stGW::template EdgeMapWrapper<EdgeCap, NodeCap> CapMap; 
    53     CapMap cap;
    54     NodeFlow* node_flow;
    55     typedef typename stGW::template EdgeMapWrapper<EdgeFlow, NodeFlow> FlowMap;
    56     FlowMap flow;
    57     MaxFlow<stGW, int, CapMap, FlowMap> mf;
    58     //graph* g;
    59     //EdgeCap* edge_cap;
    60     //EdgeFlow* edge_flow;
    61   public:
    62     /// For capacitated b-matchings, edge-caoacities and node-capacities 
    63     /// have to be given. After running \c run the matching is is given 
    64     /// back in the edge-map \c _edge_flow and \c _node_map can be used 
    65     /// to obtain saturation information about nodes.
    66     ///\bug Note that the values in _edge_flow and _node_flow have 
    67     /// to form a flow.
    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), 
    72       node_flow(0), 
    73       flow(_edge_flow, _node_flow), 
    74       mf(stgw, stgw.S_NODE, stgw.T_NODE, cap, flow) { }
    75     /// If the saturation information of nodes is not needed that the use of 
    76     /// this constructor is more comfortable.
    77     ///\bug Note that the values in _edge_flow and _node_flow have 
    78     /// to form a flow.
    79     MaxMatching(Graph& _g, EdgeCap& _edge_cap, NodeCap& _node_cap, 
    80 		EdgeFlow& _edge_flow/*, NodeFlow& _node_flow*/) : 
    81       stgw(_g), 
    82       cap(_edge_cap, _node_cap), 
    83       node_flow(new NodeFlow(_g)), 
    84       flow(_edge_flow, *node_flow), 
    85       mf(stgw, stgw.S_NODE, stgw.T_NODE, cap, flow) { }
    86     /// The class have a nontrivial destructor.
    87     ~MaxMatching() { if (node_flow) delete node_flow; }
    88     /// run computes the max matching.
    89     void run() { mf.run(); } 
    90     /// The matching value after running \c run.
    91     int matchingValue() { return mf.flowValue(); }
    92   };
    93 
    94 } //namespace hugo
    95 
    96 #endif //HUGO_MAX_BIPARTITE_MATCHING_H