3 * This file is a part of LEMON, a generic C++ optimization library
5 * Copyright (C) 2003-2008
6 * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
7 * (Egervary Research Group on Combinatorial Optimization, EGRES).
9 * Permission to use, modify and distribute this software is granted
10 * provided that this copyright notice appears in all copies. For
11 * precise terms see the accompanying LICENSE file.
13 * This software is provided "AS IS" with no warranty of any kind,
14 * express or implied, and with no claim as to its suitability for any
19 #ifndef LEMON_MIN_COST_MAX_FLOW_H
20 #define LEMON_MIN_COST_MAX_FLOW_H
22 /// \ingroup min_cost_flow
25 /// \brief An efficient algorithm for finding a minimum cost maximum flow.
27 #include <lemon/preflow.h>
28 #include <lemon/network_simplex.h>
29 #include <lemon/maps.h>
33 /// \addtogroup min_cost_flow
36 /// \brief An efficient algorithm for finding a minimum cost
39 /// \ref MinCostMaxFlow implements an efficient algorithm for
40 /// finding a maximum flow having minimal total cost from a given
41 /// source node to a given target node in a directed graph.
43 /// \ref MinCostMaxFlow uses \ref Preflow for finding the maximum
44 /// flow value and \ref NetworkSimplex for finding a minimum cost
45 /// flow of that value.
46 /// According to our benchmark tests \ref Preflow is generally the
47 /// most efficient algorithm for the maximum flow problem and
48 /// \ref NetworkSimplex is the most efficient for the minimum cost
49 /// flow problem in LEMON.
51 /// \tparam Graph The directed graph type the algorithm runs on.
52 /// \tparam CapacityMap The type of the capacity (upper bound) map.
53 /// \tparam CostMap The type of the cost (length) map.
56 /// - Edge capacities and costs should be \e non-negative \e integers.
57 /// - \c CapacityMap::Value must be convertible to \c CostMap::Value.
58 /// - \c CostMap::Value must be signed type.
60 /// \author Peter Kovacs
61 template < typename Graph,
62 typename CapacityMap = typename Graph::template EdgeMap<int>,
63 typename CostMap = typename Graph::template EdgeMap<int> >
66 GRAPH_TYPEDEFS(typename Graph);
68 typedef typename CapacityMap::Value Capacity;
69 typedef typename CostMap::Value Cost;
70 typedef typename Graph::template NodeMap<Cost> SupplyMap;
72 typedef Preflow<Graph, CapacityMap> MaxFlowImpl;
73 typedef NetworkSimplex< Graph, CapacityMap, CapacityMap,
74 CostMap, SupplyMap > MinCostFlowImpl;
78 /// The type of the flow map.
79 typedef typename Graph::template EdgeMap<Capacity> FlowMap;
80 /// The type of the potential map.
81 typedef typename Graph::template NodeMap<Cost> PotentialMap;
85 // The directed graph the algorithm runs on
88 const CapacityMap &_capacity;
92 // Edge map of the found flow
95 // Node map of the current potentials
96 PotentialMap *_potential;
97 bool _local_potential;
106 /// \brief Constructor.
110 /// \param graph The directed graph the algorithm runs on.
111 /// \param capacity The capacities (upper bounds) of the edges.
112 /// \param cost The cost (length) values of the edges.
113 /// \param s The source node.
114 /// \param t The target node.
115 MinCostMaxFlow( const Graph &graph,
116 const CapacityMap &capacity,
119 _graph(graph), _capacity(capacity), _cost(cost), _flow(0),
120 _local_flow(false), _potential(0), _local_potential(false),
121 _source(s), _target(t) {}
125 if (_local_flow) delete _flow;
126 if (_local_potential) delete _potential;
129 /// \brief Set the flow map.
131 /// Set the flow map.
133 /// \return \c (*this)
134 MinCostMaxFlow& flowMap(FlowMap &map) {
143 /// \brief Set the potential map.
145 /// Set the potential map.
147 /// \return \c (*this)
148 MinCostMaxFlow& potentialMap(PotentialMap &map) {
149 if (_local_potential) {
151 _local_potential = false;
157 /// \name Execution control
161 /// \brief Run the algorithm.
163 /// Run the algorithm.
167 _flow = new FlowMap(_graph);
171 _potential = new PotentialMap(_graph);
172 _local_potential = true;
175 MaxFlowImpl preflow(_graph, _capacity, _source, _target);
176 preflow.flowMap(*_flow).runMinCut();
177 // Running NetworkSimplex
178 MinCostFlowImpl mcf( _graph, _capacity, _cost,
179 _source, _target, preflow.flowValue() );
180 mcf.flowMap(*_flow).potentialMap(*_potential).run();
185 /// \name Query Functions
186 /// The results of the algorithm can be obtained using these
188 /// \ref lemon::MinCostMaxFlow::run() "run()" must be called before
193 /// \brief Return a const reference to the edge map storing the
196 /// Return a const reference to the edge map storing the found flow.
198 /// \pre \ref run() must be called before using this function.
199 const FlowMap& flowMap() const {
203 /// \brief Return a const reference to the node map storing the
204 /// found potentials (the dual solution).
206 /// Return a const reference to the node map storing the found
207 /// potentials (the dual solution).
209 /// \pre \ref run() must be called before using this function.
210 const PotentialMap& potentialMap() const {
214 /// \brief Return the flow on the given edge.
216 /// Return the flow on the given edge.
218 /// \pre \ref run() must be called before using this function.
219 Capacity flow(const Edge& edge) const {
220 return (*_flow)[edge];
223 /// \brief Return the potential of the given node.
225 /// Return the potential of the given node.
227 /// \pre \ref run() must be called before using this function.
228 Cost potential(const Node& node) const {
229 return (*_potential)[node];
232 /// \brief Return the total cost of the found flow.
234 /// Return the total cost of the found flow. The complexity of the
235 /// function is \f$ O(e) \f$.
237 /// \pre \ref run() must be called before using this function.
238 Cost totalCost() const {
240 for (EdgeIt e(_graph); e != INVALID; ++e)
241 c += (*_flow)[e] * _cost[e];
247 }; //class MinCostMaxFlow
253 #endif //LEMON_MIN_COST_MAX_FLOW_H