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@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>
|
marci@559
|
17 |
//#include <hugo/maps.h>
|
marci@510
|
18 |
#include <max_flow.h>
|
marci@510
|
19 |
|
marci@559
|
20 |
namespace hugo {
|
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@559
|
62 |
typedef stGraphWrapper<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@559
|
69 |
MaxFlow<stGW, int, CapMap, FlowMap> mf;
|
marci@559
|
70 |
//graph* g;
|
marci@559
|
71 |
//EdgeCap* edge_cap;
|
marci@559
|
72 |
//EdgeFlow* edge_flow;
|
marci@559
|
73 |
public:
|
marci@559
|
74 |
/// For capacitated b-matchings, edge-caoacities and node-capacities
|
marci@559
|
75 |
/// have to be given. After running \c run the matching is is given
|
marci@559
|
76 |
/// back in the edge-map \c _edge_flow and \c _node_map can be used
|
marci@559
|
77 |
/// to obtain saturation information about nodes.
|
marci@559
|
78 |
///\bug Note that the values in _edge_flow and _node_flow have
|
marci@559
|
79 |
/// to form a flow.
|
marci@613
|
80 |
MaxBipartiteMatching(Graph& _g, EdgeCap& _edge_cap, NodeCap& _node_cap,
|
marci@559
|
81 |
EdgeFlow& _edge_flow, NodeFlow& _node_flow) :
|
marci@559
|
82 |
stgw(_g),
|
marci@559
|
83 |
cap(_edge_cap, _node_cap),
|
marci@559
|
84 |
node_flow(0),
|
marci@559
|
85 |
flow(_edge_flow, _node_flow),
|
marci@559
|
86 |
mf(stgw, stgw.S_NODE, stgw.T_NODE, cap, flow) { }
|
marci@559
|
87 |
/// If the saturation information of nodes is not needed that the use of
|
marci@559
|
88 |
/// this constructor is more comfortable.
|
marci@559
|
89 |
///\bug Note that the values in _edge_flow and _node_flow have
|
marci@559
|
90 |
/// to form a flow.
|
marci@613
|
91 |
MaxBipartiteMatching(Graph& _g, EdgeCap& _edge_cap, NodeCap& _node_cap,
|
marci@559
|
92 |
EdgeFlow& _edge_flow/*, NodeFlow& _node_flow*/) :
|
marci@559
|
93 |
stgw(_g),
|
marci@559
|
94 |
cap(_edge_cap, _node_cap),
|
marci@559
|
95 |
node_flow(new NodeFlow(_g)),
|
marci@559
|
96 |
flow(_edge_flow, *node_flow),
|
marci@559
|
97 |
mf(stgw, stgw.S_NODE, stgw.T_NODE, cap, flow) { }
|
marci@559
|
98 |
/// The class have a nontrivial destructor.
|
marci@613
|
99 |
~MaxBipartiteMatching() { if (node_flow) delete node_flow; }
|
marci@559
|
100 |
/// run computes the max matching.
|
marci@559
|
101 |
void run() { mf.run(); }
|
marci@559
|
102 |
/// The matching value after running \c run.
|
marci@559
|
103 |
int matchingValue() { return mf.flowValue(); }
|
marci@559
|
104 |
};
|
marci@510
|
105 |
|
marci@559
|
106 |
} //namespace hugo
|
marci@510
|
107 |
|
marci@559
|
108 |
#endif //HUGO_MAX_BIPARTITE_MATCHING_H
|