src/work/marci/max_bipartite_matching.h
author klao
Wed, 09 Mar 2005 14:23:36 +0000
changeset 1209 dc9fdf77007f
parent 768 a5e9303a5511
permissions -rw-r--r--
Fix a bug noticed by deba.
marci@510
     1
// -*- c++ -*-
alpar@921
     2
#ifndef LEMON_MAX_BIPARTITE_MATCHING_H
alpar@921
     3
#define LEMON_MAX_BIPARTITE_MATCHING_H
marci@510
     4
marci@615
     5
/// \ingroup galgs
marci@615
     6
/// \file
marci@615
     7
/// \brief Maximum bipartite matchings, b-matchings and 
marci@615
     8
/// capacitated b-matchings.
marci@615
     9
///
marci@615
    10
/// This file contains a class for bipartite maximum matching, b-matchings 
marci@615
    11
/// and capacitated b-matching computations.
marci@615
    12
///
marci@615
    13
// /// \author Marton Makai
marci@615
    14
marci@559
    15
//#include <for_each_macros.h>
marci@510
    16
#include <bipartite_graph_wrapper.h>
alpar@921
    17
//#include <lemon/maps.h>
alpar@921
    18
#include <lemon/max_flow.h>
marci@510
    19
alpar@921
    20
namespace lemon {
marci@510
    21
marci@559
    22
  // template <typename Graph, typename EdgeCap, typename NodeCap, 
marci@559
    23
  // 	  typename EdgeFlow, typename NodeFlow>
marci@559
    24
  // class MaxMatching : public MaxFlow<stGraphWrapper<Graph>, 
marci@559
    25
  // 				   stGraphWrapper<Graph>:: EdgeMapWrapper<EdgeCan, NodeCap>, stGraphWrapper<Graph>::EdgeMapWrapper<EdgeFlow, NodeFlow> > {
marci@559
    26
  //   typedef MaxFlow<stGraphWrapper<Graph>, 
marci@559
    27
  // 		  stGraphWrapper<Graph>::EdgeMapWrapper<EdgeCan, NodeCap>, 
marci@559
    28
  // 		  stGraphWrapper<Graph>::EdgeMapWrapper<EdgeFlow, NodeFlow> >
marci@559
    29
  //   Parent;
marci@559
    30
  // protected:
marci@559
    31
  //   stGraphWrapper<Graph> gw;
marci@559
    32
  //   stGraphWrapper<Graph>::EdgeMapWrapper<EdgeCap, NodeCap> cap;
marci@559
    33
  //   stGraphWrapper<Graph>::EdgeMapWrapper<EdgeFlow, NodeFlow> flow;
marci@559
    34
  //   //graph* g;
marci@559
    35
  //   //EdgeCap* edge_cap;
marci@559
    36
  //   //EdgeFlow* edge_flow;
marci@559
    37
  // public:
marci@559
    38
  //   MaxMatching(Graph& _g, EdgeCap& _edge_cap, NodeCap& _node_cap, 
marci@559
    39
  // 	      EdgeFlow& _edge_flow, NodeFlow& _node_flow) : 
marci@559
    40
  //     MaxFlow(), gw(_g), 
marci@559
    41
  //     cap(_edge_cap, _node_cap), flow(_edge_flow, _node_flow) {
marci@559
    42
  //     Parent::set(gw, cap, flow);
marci@559
    43
  //   }
marci@559
    44
  // };
marci@510
    45
marci@613
    46
  /// \brief A bipartite matching class.
marci@613
    47
  ///
marci@559
    48
  /// This class reduces the matching problem to a flow problem and 
marci@559
    49
  /// a preflow is used on a wrapper. Such a generic approach means that 
marci@559
    50
  /// matchings, b-matchings an capacitated b-matchings can be handled in 
marci@559
    51
  /// a similar way. Due to the efficiency of the preflow algorithm, an 
marci@559
    52
  /// efficient matching framework is obtained.
marci@613
    53
  /// \ingroup galgs
marci@559
    54
  template <typename Graph, typename EdgeCap, typename NodeCap, 
marci@559
    55
	    typename EdgeFlow, typename NodeFlow>
marci@613
    56
  class MaxBipartiteMatching {
marci@559
    57
  protected:
marci@559
    58
    //   EdgeCap* edge_cap;
marci@559
    59
    //   NodeCap* node_cap;
marci@559
    60
    //   EdgeFlow* edge_flow;
marci@559
    61
    //   NodeFlow* node_flow;
marci@768
    62
    typedef  stBipartiteGraphWrapper<Graph> stGW;
marci@559
    63
    stGW stgw;
marci@559
    64
    typedef typename stGW::template EdgeMapWrapper<EdgeCap, NodeCap> CapMap; 
marci@559
    65
    CapMap cap;
marci@559
    66
    NodeFlow* node_flow;
marci@559
    67
    typedef typename stGW::template EdgeMapWrapper<EdgeFlow, NodeFlow> FlowMap;
marci@559
    68
    FlowMap flow;
marci@768
    69
    typedef MaxFlow<stGW, int, CapMap, FlowMap> MaxFlow;
marci@768
    70
    MaxFlow mf;
marci@559
    71
    //graph* g;
marci@559
    72
    //EdgeCap* edge_cap;
marci@559
    73
    //EdgeFlow* edge_flow;
marci@559
    74
  public:
marci@768
    75
    enum MatchingEnum{
marci@768
    76
      ZERO_MATCHING,
marci@768
    77
      GEN_MATCHING,
marci@768
    78
      GEN_MATCHING_WITH_GOOD_NODE_FLOW,
marci@768
    79
      NO_MATCHING
marci@768
    80
    };
marci@559
    81
    /// For capacitated b-matchings, edge-caoacities and node-capacities 
marci@559
    82
    /// have to be given. After running \c run the matching is is given 
marci@559
    83
    /// back in the edge-map \c _edge_flow and \c _node_map can be used 
marci@559
    84
    /// to obtain saturation information about nodes.
marci@559
    85
    ///\bug Note that the values in _edge_flow and _node_flow have 
marci@559
    86
    /// to form a flow.
marci@613
    87
    MaxBipartiteMatching(Graph& _g, EdgeCap& _edge_cap, NodeCap& _node_cap, 
marci@559
    88
		EdgeFlow& _edge_flow, NodeFlow& _node_flow) : 
marci@559
    89
      stgw(_g), 
marci@559
    90
      cap(_edge_cap, _node_cap), 
marci@559
    91
      node_flow(0), 
marci@559
    92
      flow(_edge_flow, _node_flow), 
marci@559
    93
      mf(stgw, stgw.S_NODE, stgw.T_NODE, cap, flow) { }
marci@559
    94
    /// If the saturation information of nodes is not needed that the use of 
marci@559
    95
    /// this constructor is more comfortable.
marci@559
    96
    ///\bug Note that the values in _edge_flow and _node_flow have 
marci@559
    97
    /// to form a flow.
marci@613
    98
    MaxBipartiteMatching(Graph& _g, EdgeCap& _edge_cap, NodeCap& _node_cap, 
marci@559
    99
		EdgeFlow& _edge_flow/*, NodeFlow& _node_flow*/) : 
marci@559
   100
      stgw(_g), 
marci@559
   101
      cap(_edge_cap, _node_cap), 
marci@559
   102
      node_flow(new NodeFlow(_g)), 
marci@559
   103
      flow(_edge_flow, *node_flow), 
marci@559
   104
      mf(stgw, stgw.S_NODE, stgw.T_NODE, cap, flow) { }
marci@559
   105
    /// The class have a nontrivial destructor.
marci@613
   106
    ~MaxBipartiteMatching() { if (node_flow) delete node_flow; }
marci@559
   107
    /// run computes the max matching.
marci@768
   108
    void run(MatchingEnum me=ZERO_MATCHING) { 
marci@768
   109
      switch (me) {
marci@768
   110
	case ZERO_MATCHING:
marci@768
   111
	  mf.run(MaxFlow::ZERO_FLOW);
marci@768
   112
	  break;
marci@768
   113
	case GEN_MATCHING:
marci@768
   114
	{
marci@768
   115
	  typename stGW::OutEdgeIt e;
marci@768
   116
	  for (stgw.first(e, stgw.S_NODE); stgw.valid(e); stgw.next(e)) 
marci@768
   117
	    flow.set(e, cap[e]);
marci@768
   118
	}
marci@768
   119
	{
marci@768
   120
	  typename stGW::InEdgeIt e;
marci@768
   121
	  for (stgw.first(e, stgw.T_NODE); stgw.valid(e); stgw.next(e)) 
marci@768
   122
	    flow.set(e, 0);
marci@768
   123
	}
marci@768
   124
	mf.run(MaxFlow::PRE_FLOW);
marci@768
   125
	break;
marci@768
   126
	case GEN_MATCHING_WITH_GOOD_NODE_FLOW:
marci@768
   127
	  mf.run(MaxFlow::GEN_FLOW);
marci@768
   128
	  break;
marci@768
   129
	case NO_MATCHING:
marci@768
   130
	  mf.run(MaxFlow::NO_FLOW);
marci@768
   131
	  break;
marci@768
   132
      }
marci@768
   133
    } 
marci@559
   134
    /// The matching value after running \c run.
marci@768
   135
    int matchingValue() const { return mf.flowValue(); }
marci@559
   136
  };
marci@510
   137
alpar@921
   138
} //namespace lemon
marci@510
   139
alpar@921
   140
#endif //LEMON_MAX_BIPARTITE_MATCHING_H