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