Small fixes and doc improvements in MinMeanCycle.
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_CAPACITY_SCALING_H
20 #define LEMON_CAPACITY_SCALING_H
22 /// \ingroup min_cost_flow
25 /// \brief Capacity scaling algorithm for finding a minimum cost flow.
29 #include <lemon/graph_adaptor.h>
30 #include <lemon/bin_heap.h>
34 /// \addtogroup min_cost_flow
37 /// \brief Implementation of the capacity scaling algorithm for
38 /// finding a minimum cost flow.
40 /// \ref CapacityScaling implements the capacity scaling version
41 /// of the successive shortest path algorithm for finding a minimum
44 /// \tparam Graph The directed graph type the algorithm runs on.
45 /// \tparam LowerMap The type of the lower bound map.
46 /// \tparam CapacityMap The type of the capacity (upper bound) map.
47 /// \tparam CostMap The type of the cost (length) map.
48 /// \tparam SupplyMap The type of the supply map.
51 /// - Edge capacities and costs should be \e non-negative \e integers.
52 /// - Supply values should be \e signed \e integers.
53 /// - The value types of the maps should be convertible to each other.
54 /// - \c CostMap::Value must be signed type.
56 /// \author Peter Kovacs
58 template < typename Graph,
59 typename LowerMap = typename Graph::template EdgeMap<int>,
60 typename CapacityMap = typename Graph::template EdgeMap<int>,
61 typename CostMap = typename Graph::template EdgeMap<int>,
62 typename SupplyMap = typename Graph::template NodeMap<int> >
65 GRAPH_TYPEDEFS(typename Graph);
67 typedef typename CapacityMap::Value Capacity;
68 typedef typename CostMap::Value Cost;
69 typedef typename SupplyMap::Value Supply;
70 typedef typename Graph::template EdgeMap<Capacity> CapacityEdgeMap;
71 typedef typename Graph::template NodeMap<Supply> SupplyNodeMap;
72 typedef typename Graph::template NodeMap<Edge> PredMap;
76 /// The type of the flow map.
77 typedef typename Graph::template EdgeMap<Capacity> FlowMap;
78 /// The type of the potential map.
79 typedef typename Graph::template NodeMap<Cost> PotentialMap;
83 /// \brief Special implementation of the \ref Dijkstra algorithm
84 /// for finding shortest paths in the residual network.
86 /// \ref ResidualDijkstra is a special implementation of the
87 /// \ref Dijkstra algorithm for finding shortest paths in the
88 /// residual network of the graph with respect to the reduced edge
89 /// costs and modifying the node potentials according to the
90 /// distance of the nodes.
91 class ResidualDijkstra
93 typedef typename Graph::template NodeMap<Cost> CostNodeMap;
94 typedef typename Graph::template NodeMap<Edge> PredMap;
96 typedef typename Graph::template NodeMap<int> HeapCrossRef;
97 typedef BinHeap<Cost, HeapCrossRef> Heap;
101 // The directed graph the algorithm runs on
105 const FlowMap &_flow;
106 const CapacityEdgeMap &_res_cap;
107 const CostMap &_cost;
108 const SupplyNodeMap &_excess;
109 PotentialMap &_potential;
115 // The processed (i.e. permanently labeled) nodes
116 std::vector<Node> _proc_nodes;
121 ResidualDijkstra( const Graph &graph,
123 const CapacityEdgeMap &res_cap,
125 const SupplyMap &excess,
126 PotentialMap &potential,
128 _graph(graph), _flow(flow), _res_cap(res_cap), _cost(cost),
129 _excess(excess), _potential(potential), _dist(graph),
133 /// Runs the algorithm from the given source node.
134 Node run(Node s, Capacity delta) {
135 HeapCrossRef heap_cross_ref(_graph, Heap::PRE_HEAP);
136 Heap heap(heap_cross_ref);
142 while (!heap.empty() && _excess[heap.top()] > -delta) {
143 Node u = heap.top(), v;
144 Cost d = heap.prio() + _potential[u], nd;
145 _dist[u] = heap.prio();
147 _proc_nodes.push_back(u);
149 // Traversing outgoing edges
150 for (OutEdgeIt e(_graph, u); e != INVALID; ++e) {
151 if (_res_cap[e] >= delta) {
152 v = _graph.target(e);
153 switch(heap.state(v)) {
155 heap.push(v, d + _cost[e] - _potential[v]);
159 nd = d + _cost[e] - _potential[v];
161 heap.decrease(v, nd);
165 case Heap::POST_HEAP:
171 // Traversing incoming edges
172 for (InEdgeIt e(_graph, u); e != INVALID; ++e) {
173 if (_flow[e] >= delta) {
174 v = _graph.source(e);
175 switch(heap.state(v)) {
177 heap.push(v, d - _cost[e] - _potential[v]);
181 nd = d - _cost[e] - _potential[v];
183 heap.decrease(v, nd);
187 case Heap::POST_HEAP:
193 if (heap.empty()) return INVALID;
195 // Updating potentials of processed nodes
197 Cost t_dist = heap.prio();
198 for (int i = 0; i < int(_proc_nodes.size()); ++i)
199 _potential[_proc_nodes[i]] += _dist[_proc_nodes[i]] - t_dist;
204 }; //class ResidualDijkstra
208 // The directed graph the algorithm runs on
210 // The original lower bound map
211 const LowerMap *_lower;
212 // The modified capacity map
213 CapacityEdgeMap _capacity;
214 // The original cost map
215 const CostMap &_cost;
216 // The modified supply map
217 SupplyNodeMap _supply;
220 // Edge map of the current flow
223 // Node map of the current potentials
224 PotentialMap *_potential;
225 bool _local_potential;
227 // The residual capacity map
228 CapacityEdgeMap _res_cap;
230 SupplyNodeMap _excess;
231 // The excess nodes (i.e. nodes with positive excess)
232 std::vector<Node> _excess_nodes;
233 // The deficit nodes (i.e. nodes with negative excess)
234 std::vector<Node> _deficit_nodes;
236 // The delta parameter used for capacity scaling
238 // The maximum number of phases
243 // Implementation of the Dijkstra algorithm for finding augmenting
244 // shortest paths in the residual network
245 ResidualDijkstra *_dijkstra;
249 /// \brief General constructor (with lower bounds).
251 /// General constructor (with lower bounds).
253 /// \param graph The directed graph the algorithm runs on.
254 /// \param lower The lower bounds of the edges.
255 /// \param capacity The capacities (upper bounds) of the edges.
256 /// \param cost The cost (length) values of the edges.
257 /// \param supply The supply values of the nodes (signed).
258 CapacityScaling( const Graph &graph,
259 const LowerMap &lower,
260 const CapacityMap &capacity,
262 const SupplyMap &supply ) :
263 _graph(graph), _lower(&lower), _capacity(graph), _cost(cost),
264 _supply(graph), _flow(0), _local_flow(false),
265 _potential(0), _local_potential(false),
266 _res_cap(graph), _excess(graph), _pred(graph)
268 // Removing non-zero lower bounds
269 _capacity = subMap(capacity, lower);
270 _res_cap = _capacity;
272 for (NodeIt n(_graph); n != INVALID; ++n) {
273 Supply s = supply[n];
274 for (InEdgeIt e(_graph, n); e != INVALID; ++e)
276 for (OutEdgeIt e(_graph, n); e != INVALID; ++e)
281 _valid_supply = sum == 0;
284 /// \brief General constructor (without lower bounds).
286 /// General constructor (without lower bounds).
288 /// \param graph The directed graph the algorithm runs on.
289 /// \param capacity The capacities (upper bounds) of the edges.
290 /// \param cost The cost (length) values of the edges.
291 /// \param supply The supply values of the nodes (signed).
292 CapacityScaling( const Graph &graph,
293 const CapacityMap &capacity,
295 const SupplyMap &supply ) :
296 _graph(graph), _lower(NULL), _capacity(capacity), _cost(cost),
297 _supply(supply), _flow(0), _local_flow(false),
298 _potential(0), _local_potential(false),
299 _res_cap(capacity), _excess(graph), _pred(graph)
301 // Checking the sum of supply values
303 for (NodeIt n(_graph); n != INVALID; ++n) sum += _supply[n];
304 _valid_supply = sum == 0;
307 /// \brief Simple constructor (with lower bounds).
309 /// Simple constructor (with lower bounds).
311 /// \param graph The directed graph the algorithm runs on.
312 /// \param lower The lower bounds of the edges.
313 /// \param capacity The capacities (upper bounds) of the edges.
314 /// \param cost The cost (length) values of the edges.
315 /// \param s The source node.
316 /// \param t The target node.
317 /// \param flow_value The required amount of flow from node \c s
318 /// to node \c t (i.e. the supply of \c s and the demand of \c t).
319 CapacityScaling( const Graph &graph,
320 const LowerMap &lower,
321 const CapacityMap &capacity,
324 Supply flow_value ) :
325 _graph(graph), _lower(&lower), _capacity(graph), _cost(cost),
326 _supply(graph), _flow(0), _local_flow(false),
327 _potential(0), _local_potential(false),
328 _res_cap(graph), _excess(graph), _pred(graph)
330 // Removing non-zero lower bounds
331 _capacity = subMap(capacity, lower);
332 _res_cap = _capacity;
333 for (NodeIt n(_graph); n != INVALID; ++n) {
335 if (n == s) sum = flow_value;
336 if (n == t) sum = -flow_value;
337 for (InEdgeIt e(_graph, n); e != INVALID; ++e)
339 for (OutEdgeIt e(_graph, n); e != INVALID; ++e)
343 _valid_supply = true;
346 /// \brief Simple constructor (without lower bounds).
348 /// Simple constructor (without lower bounds).
350 /// \param graph The directed graph the algorithm runs on.
351 /// \param capacity The capacities (upper bounds) of the edges.
352 /// \param cost The cost (length) values of the edges.
353 /// \param s The source node.
354 /// \param t The target node.
355 /// \param flow_value The required amount of flow from node \c s
356 /// to node \c t (i.e. the supply of \c s and the demand of \c t).
357 CapacityScaling( const Graph &graph,
358 const CapacityMap &capacity,
361 Supply flow_value ) :
362 _graph(graph), _lower(NULL), _capacity(capacity), _cost(cost),
363 _supply(graph, 0), _flow(0), _local_flow(false),
364 _potential(0), _local_potential(false),
365 _res_cap(capacity), _excess(graph), _pred(graph)
367 _supply[s] = flow_value;
368 _supply[t] = -flow_value;
369 _valid_supply = true;
374 if (_local_flow) delete _flow;
375 if (_local_potential) delete _potential;
379 /// \brief Sets the flow map.
381 /// Sets the flow map.
383 /// \return \c (*this)
384 CapacityScaling& flowMap(FlowMap &map) {
393 /// \brief Sets the potential map.
395 /// Sets the potential map.
397 /// \return \c (*this)
398 CapacityScaling& potentialMap(PotentialMap &map) {
399 if (_local_potential) {
401 _local_potential = false;
407 /// \name Execution control
408 /// The only way to execute the algorithm is to call the run()
413 /// \brief Runs the algorithm.
415 /// Runs the algorithm.
417 /// \param scaling Enable or disable capacity scaling.
418 /// If the maximum edge capacity and/or the amount of total supply
419 /// is rather small, the algorithm could be slightly faster without
422 /// \return \c true if a feasible flow can be found.
423 bool run(bool scaling = true) {
424 return init(scaling) && start();
429 /// \name Query Functions
430 /// The result of the algorithm can be obtained using these
432 /// \n run() must be called before using them.
436 /// \brief Returns a const reference to the edge map storing the
439 /// Returns a const reference to the edge map storing the found flow.
441 /// \pre \ref run() must be called before using this function.
442 const FlowMap& flowMap() const {
446 /// \brief Returns a const reference to the node map storing the
447 /// found potentials (the dual solution).
449 /// Returns a const reference to the node map storing the found
450 /// potentials (the dual solution).
452 /// \pre \ref run() must be called before using this function.
453 const PotentialMap& potentialMap() const {
457 /// \brief Returns the flow on the edge.
459 /// Returns the flow on the edge.
461 /// \pre \ref run() must be called before using this function.
462 Capacity flow(const Edge& edge) const {
463 return (*_flow)[edge];
466 /// \brief Returns the potential of the node.
468 /// Returns the potential of the node.
470 /// \pre \ref run() must be called before using this function.
471 Cost potential(const Node& node) const {
472 return (*_potential)[node];
475 /// \brief Returns the total cost of the found flow.
477 /// Returns the total cost of the found flow. The complexity of the
478 /// function is \f$ O(e) \f$.
480 /// \pre \ref run() must be called before using this function.
481 Cost totalCost() const {
483 for (EdgeIt e(_graph); e != INVALID; ++e)
484 c += (*_flow)[e] * _cost[e];
492 /// Initializes the algorithm.
493 bool init(bool scaling) {
494 if (!_valid_supply) return false;
498 _flow = new FlowMap(_graph);
502 _potential = new PotentialMap(_graph);
503 _local_potential = true;
505 for (EdgeIt e(_graph); e != INVALID; ++e) (*_flow)[e] = 0;
506 for (NodeIt n(_graph); n != INVALID; ++n) (*_potential)[n] = 0;
509 _dijkstra = new ResidualDijkstra( _graph, *_flow, _res_cap, _cost,
510 _excess, *_potential, _pred );
512 // Initializing delta value
515 Supply max_sup = 0, max_dem = 0;
516 for (NodeIt n(_graph); n != INVALID; ++n) {
517 if ( _supply[n] > max_sup) max_sup = _supply[n];
518 if (-_supply[n] > max_dem) max_dem = -_supply[n];
520 if (max_dem < max_sup) max_sup = max_dem;
522 for (_delta = 1; 2 * _delta <= max_sup; _delta *= 2)
534 return startWithScaling();
536 return startWithoutScaling();
539 /// Executes the capacity scaling algorithm.
540 bool startWithScaling() {
541 // Processing capacity scaling phases
546 // Saturating all edges not satisfying the optimality condition
547 for (EdgeIt e(_graph); e != INVALID; ++e) {
548 Node u = _graph.source(e), v = _graph.target(e);
549 Cost c = _cost[e] + (*_potential)[u] - (*_potential)[v];
550 if (c < 0 && _res_cap[e] >= _delta) {
551 _excess[u] -= _res_cap[e];
552 _excess[v] += _res_cap[e];
553 (*_flow)[e] = _capacity[e];
556 else if (c > 0 && (*_flow)[e] >= _delta) {
557 _excess[u] += (*_flow)[e];
558 _excess[v] -= (*_flow)[e];
560 _res_cap[e] = _capacity[e];
564 // Finding excess nodes and deficit nodes
565 _excess_nodes.clear();
566 _deficit_nodes.clear();
567 for (NodeIt n(_graph); n != INVALID; ++n) {
568 if (_excess[n] >= _delta) _excess_nodes.push_back(n);
569 if (_excess[n] <= -_delta) _deficit_nodes.push_back(n);
573 // Finding augmenting shortest paths
574 while (next_node < int(_excess_nodes.size())) {
575 // Checking deficit nodes
577 bool delta_deficit = false;
578 for (int i = 0; i < int(_deficit_nodes.size()); ++i) {
579 if (_excess[_deficit_nodes[i]] <= -_delta) {
580 delta_deficit = true;
584 if (!delta_deficit) break;
588 s = _excess_nodes[next_node];
589 if ((t = _dijkstra->run(s, _delta)) == INVALID) {
597 // Augmenting along a shortest path from s to t.
598 Capacity d = _excess[s] < -_excess[t] ? _excess[s] : -_excess[t];
602 while ((e = _pred[u]) != INVALID) {
604 if (u == _graph.target(e)) {
606 u = _graph.source(e);
609 u = _graph.target(e);
615 while ((e = _pred[u]) != INVALID) {
616 if (u == _graph.target(e)) {
619 u = _graph.source(e);
623 u = _graph.target(e);
629 if (_excess[s] < _delta) ++next_node;
632 if (_delta == 1) break;
633 if (++phase_cnt > _phase_num / 4) factor = 2;
634 _delta = _delta <= factor ? 1 : _delta / factor;
637 // Handling non-zero lower bounds
639 for (EdgeIt e(_graph); e != INVALID; ++e)
640 (*_flow)[e] += (*_lower)[e];
645 /// Executes the successive shortest path algorithm.
646 bool startWithoutScaling() {
647 // Finding excess nodes
648 for (NodeIt n(_graph); n != INVALID; ++n)
649 if (_excess[n] > 0) _excess_nodes.push_back(n);
650 if (_excess_nodes.size() == 0) return true;
653 // Finding shortest paths
655 while ( _excess[_excess_nodes[next_node]] > 0 ||
656 ++next_node < int(_excess_nodes.size()) )
659 s = _excess_nodes[next_node];
660 if ((t = _dijkstra->run(s, 1)) == INVALID)
663 // Augmenting along a shortest path from s to t
664 Capacity d = _excess[s] < -_excess[t] ? _excess[s] : -_excess[t];
667 while ((e = _pred[u]) != INVALID) {
669 if (u == _graph.target(e)) {
671 u = _graph.source(e);
674 u = _graph.target(e);
679 while ((e = _pred[u]) != INVALID) {
680 if (u == _graph.target(e)) {
683 u = _graph.source(e);
687 u = _graph.target(e);
694 // Handling non-zero lower bounds
696 for (EdgeIt e(_graph); e != INVALID; ++e)
697 (*_flow)[e] += (*_lower)[e];
702 }; //class CapacityScaling
708 #endif //LEMON_CAPACITY_SCALING_H