[510] | 1 | // -*- c++ -*- |
---|
[559] | 2 | #ifndef HUGO_MAX_BIPARTITE_MATCHING_H |
---|
| 3 | #define HUGO_MAX_BIPARTITE_MATCHING_H |
---|
[510] | 4 | |
---|
[559] | 5 | //#include <for_each_macros.h> |
---|
[510] | 6 | #include <bipartite_graph_wrapper.h> |
---|
[559] | 7 | //#include <hugo/maps.h> |
---|
[510] | 8 | #include <max_flow.h> |
---|
| 9 | |
---|
[559] | 10 | namespace hugo { |
---|
[510] | 11 | |
---|
[559] | 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 | // }; |
---|
[510] | 35 | |
---|
[613] | 36 | /// \brief A bipartite matching class. |
---|
| 37 | /// |
---|
[559] | 38 | /// This class reduces the matching problem to a flow problem and |
---|
| 39 | /// a preflow is used on a wrapper. Such a generic approach means that |
---|
| 40 | /// matchings, b-matchings an capacitated b-matchings can be handled in |
---|
| 41 | /// a similar way. Due to the efficiency of the preflow algorithm, an |
---|
| 42 | /// efficient matching framework is obtained. |
---|
[613] | 43 | /// \ingroup galgs |
---|
[559] | 44 | template <typename Graph, typename EdgeCap, typename NodeCap, |
---|
| 45 | typename EdgeFlow, typename NodeFlow> |
---|
[613] | 46 | class MaxBipartiteMatching { |
---|
[559] | 47 | protected: |
---|
| 48 | // EdgeCap* edge_cap; |
---|
| 49 | // NodeCap* node_cap; |
---|
| 50 | // EdgeFlow* edge_flow; |
---|
| 51 | // NodeFlow* node_flow; |
---|
| 52 | typedef stGraphWrapper<Graph> stGW; |
---|
| 53 | stGW stgw; |
---|
| 54 | typedef typename stGW::template EdgeMapWrapper<EdgeCap, NodeCap> CapMap; |
---|
| 55 | CapMap cap; |
---|
| 56 | NodeFlow* node_flow; |
---|
| 57 | typedef typename stGW::template EdgeMapWrapper<EdgeFlow, NodeFlow> FlowMap; |
---|
| 58 | FlowMap flow; |
---|
| 59 | MaxFlow<stGW, int, CapMap, FlowMap> mf; |
---|
| 60 | //graph* g; |
---|
| 61 | //EdgeCap* edge_cap; |
---|
| 62 | //EdgeFlow* edge_flow; |
---|
| 63 | public: |
---|
| 64 | /// For capacitated b-matchings, edge-caoacities and node-capacities |
---|
| 65 | /// have to be given. After running \c run the matching is is given |
---|
| 66 | /// back in the edge-map \c _edge_flow and \c _node_map can be used |
---|
| 67 | /// to obtain saturation information about nodes. |
---|
| 68 | ///\bug Note that the values in _edge_flow and _node_flow have |
---|
| 69 | /// to form a flow. |
---|
[613] | 70 | MaxBipartiteMatching(Graph& _g, EdgeCap& _edge_cap, NodeCap& _node_cap, |
---|
[559] | 71 | EdgeFlow& _edge_flow, NodeFlow& _node_flow) : |
---|
| 72 | stgw(_g), |
---|
| 73 | cap(_edge_cap, _node_cap), |
---|
| 74 | node_flow(0), |
---|
| 75 | flow(_edge_flow, _node_flow), |
---|
| 76 | mf(stgw, stgw.S_NODE, stgw.T_NODE, cap, flow) { } |
---|
| 77 | /// If the saturation information of nodes is not needed that the use of |
---|
| 78 | /// this constructor is more comfortable. |
---|
| 79 | ///\bug Note that the values in _edge_flow and _node_flow have |
---|
| 80 | /// to form a flow. |
---|
[613] | 81 | MaxBipartiteMatching(Graph& _g, EdgeCap& _edge_cap, NodeCap& _node_cap, |
---|
[559] | 82 | EdgeFlow& _edge_flow/*, NodeFlow& _node_flow*/) : |
---|
| 83 | stgw(_g), |
---|
| 84 | cap(_edge_cap, _node_cap), |
---|
| 85 | node_flow(new NodeFlow(_g)), |
---|
| 86 | flow(_edge_flow, *node_flow), |
---|
| 87 | mf(stgw, stgw.S_NODE, stgw.T_NODE, cap, flow) { } |
---|
| 88 | /// The class have a nontrivial destructor. |
---|
[613] | 89 | ~MaxBipartiteMatching() { if (node_flow) delete node_flow; } |
---|
[559] | 90 | /// run computes the max matching. |
---|
| 91 | void run() { mf.run(); } |
---|
| 92 | /// The matching value after running \c run. |
---|
| 93 | int matchingValue() { return mf.flowValue(); } |
---|
| 94 | }; |
---|
[510] | 95 | |
---|
[559] | 96 | } //namespace hugo |
---|
[510] | 97 | |
---|
[559] | 98 | #endif //HUGO_MAX_BIPARTITE_MATCHING_H |
---|