Improved docs.
3 * This file is a part of LEMON, a generic C++ optimization library
5 * Copyright (C) 2003-2007
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 The capacity scaling algorithm for finding a minimum cost
29 #include <lemon/graph_adaptor.h>
30 #include <lemon/dijkstra.h>
35 #define SCALING_FACTOR 2
38 //#define _DEBUG_ITER_
42 /// \addtogroup min_cost_flow
45 /// \brief Implementation of the capacity scaling version of the
46 /// successive shortest path algorithm for finding a minimum cost
49 /// \ref lemon::CapacityScaling "CapacityScaling" implements the
50 /// capacity scaling version of the successive shortest path
51 /// algorithm for finding a minimum cost flow.
53 /// \param Graph The directed graph type the algorithm runs on.
54 /// \param LowerMap The type of the lower bound map.
55 /// \param CapacityMap The type of the capacity (upper bound) map.
56 /// \param CostMap The type of the cost (length) map.
57 /// \param SupplyMap The type of the supply map.
60 /// - Edge capacities and costs should be nonnegative integers.
61 /// However \c CostMap::Value should be signed type.
62 /// - Supply values should be signed integers.
63 /// - \c LowerMap::Value must be convertible to
64 /// \c CapacityMap::Value and \c CapacityMap::Value must be
65 /// convertible to \c SupplyMap::Value.
67 /// \author Peter Kovacs
69 template < typename Graph,
70 typename LowerMap = typename Graph::template EdgeMap<int>,
71 typename CapacityMap = LowerMap,
72 typename CostMap = typename Graph::template EdgeMap<int>,
73 typename SupplyMap = typename Graph::template NodeMap
74 <typename CapacityMap::Value> >
77 typedef typename Graph::Node Node;
78 typedef typename Graph::NodeIt NodeIt;
79 typedef typename Graph::Edge Edge;
80 typedef typename Graph::EdgeIt EdgeIt;
81 typedef typename Graph::InEdgeIt InEdgeIt;
82 typedef typename Graph::OutEdgeIt OutEdgeIt;
84 typedef typename LowerMap::Value Lower;
85 typedef typename CapacityMap::Value Capacity;
86 typedef typename CostMap::Value Cost;
87 typedef typename SupplyMap::Value Supply;
88 typedef typename Graph::template EdgeMap<Capacity> CapacityRefMap;
89 typedef typename Graph::template NodeMap<Supply> SupplyRefMap;
91 typedef ResGraphAdaptor< const Graph, Capacity,
92 CapacityRefMap, CapacityRefMap > ResGraph;
93 typedef typename ResGraph::Node ResNode;
94 typedef typename ResGraph::NodeIt ResNodeIt;
95 typedef typename ResGraph::Edge ResEdge;
96 typedef typename ResGraph::EdgeIt ResEdgeIt;
100 /// \brief The type of the flow map.
101 typedef CapacityRefMap FlowMap;
102 /// \brief The type of the potential map.
103 typedef typename Graph::template NodeMap<Cost> PotentialMap;
107 /// \brief Map adaptor class for handling reduced edge costs.
108 class ReducedCostMap : public MapBase<ResEdge, Cost>
113 const CostMap &cost_map;
114 const PotentialMap &pot_map;
118 ReducedCostMap( const ResGraph &_gr,
119 const CostMap &_cost,
120 const PotentialMap &_pot ) :
121 gr(_gr), cost_map(_cost), pot_map(_pot) {}
123 Cost operator[](const ResEdge &e) const {
124 return ResGraph::forward(e) ?
125 cost_map[e] - pot_map[gr.source(e)] + pot_map[gr.target(e)] :
126 -cost_map[e] - pot_map[gr.source(e)] + pot_map[gr.target(e)];
129 }; //class ReducedCostMap
131 /// \brief Map class for the \ref lemon::Dijkstra "Dijkstra"
132 /// algorithm to update node potentials.
133 class PotentialUpdateMap : public MapBase<ResNode, Cost>
138 typedef std::pair<ResNode, Cost> Pair;
139 std::vector<Pair> data;
143 void potentialMap(PotentialMap &_pot) {
151 void set(const ResNode &n, const Cost &v) {
152 data.push_back(Pair(n, v));
156 Cost val = data[data.size()-1].second;
157 for (int i = 0; i < data.size()-1; ++i)
158 (*pot)[data[i].first] += val - data[i].second;
161 }; //class PotentialUpdateMap
164 /// \brief Map adaptor class for identifing deficit nodes.
165 class DeficitBoolMap : public MapBase<ResNode, bool>
169 const SupplyRefMap &imb;
170 const Capacity δ
174 DeficitBoolMap(const SupplyRefMap &_imb, const Capacity &_delta) :
175 imb(_imb), delta(_delta) {}
177 bool operator[](const ResNode &n) const {
178 return imb[n] <= -delta;
181 }; //class DeficitBoolMap
183 /// \brief Map adaptor class for filtering edges with at least
184 /// \c delta residual capacity.
185 class DeltaFilterMap : public MapBase<ResEdge, bool>
190 const Capacity δ
194 DeltaFilterMap(const ResGraph &_gr, const Capacity &_delta) :
195 gr(_gr), delta(_delta) {}
197 bool operator[](const ResEdge &e) const {
198 return gr.rescap(e) >= delta;
201 }; //class DeltaFilterMap
203 typedef EdgeSubGraphAdaptor<const ResGraph, const DeltaFilterMap>
206 /// \brief Traits class for \ref lemon::Dijkstra "Dijkstra" class.
207 class ResDijkstraTraits :
208 public DijkstraDefaultTraits<DeltaResGraph, ReducedCostMap>
212 typedef PotentialUpdateMap DistMap;
214 static DistMap *createDistMap(const DeltaResGraph&) {
215 return new DistMap();
218 }; //class ResDijkstraTraits
220 #else //WITHOUT_CAPACITY_SCALING
221 /// \brief Map adaptor class for identifing deficit nodes.
222 class DeficitBoolMap : public MapBase<ResNode, bool>
226 const SupplyRefMap &imb;
230 DeficitBoolMap(const SupplyRefMap &_imb) : imb(_imb) {}
232 bool operator[](const ResNode &n) const {
236 }; //class DeficitBoolMap
238 /// \brief Traits class for \ref lemon::Dijkstra "Dijkstra" class.
239 class ResDijkstraTraits :
240 public DijkstraDefaultTraits<ResGraph, ReducedCostMap>
244 typedef PotentialUpdateMap DistMap;
246 static DistMap *createDistMap(const ResGraph&) {
247 return new DistMap();
250 }; //class ResDijkstraTraits
255 /// \brief The directed graph the algorithm runs on.
257 /// \brief The original lower bound map.
258 const LowerMap *lower;
259 /// \brief The modified capacity map.
260 CapacityRefMap capacity;
261 /// \brief The cost map.
263 /// \brief The modified supply map.
265 /// \brief The sum of supply values equals zero.
268 /// \brief The edge map of the current flow.
270 /// \brief The potential node map.
271 PotentialMap potential;
272 /// \brief The residual graph.
274 /// \brief The reduced cost map.
275 ReducedCostMap red_cost;
277 /// \brief The imbalance map.
278 SupplyRefMap imbalance;
279 /// \brief The excess nodes.
280 std::vector<ResNode> excess_nodes;
281 /// \brief The index of the next excess node.
285 typedef Dijkstra<DeltaResGraph, ReducedCostMap, ResDijkstraTraits>
287 /// \brief \ref lemon::Dijkstra "Dijkstra" class for finding
288 /// shortest paths in the residual graph with respect to the
289 /// reduced edge costs.
290 ResDijkstra dijkstra;
292 /// \brief The delta parameter used for capacity scaling.
294 /// \brief Edge filter map for filtering edges with at least
295 /// \c delta residual capacity.
296 DeltaFilterMap delta_filter;
297 /// \brief The delta residual graph (i.e. the subgraph of the
298 /// residual graph consisting of edges with at least \c delta
299 /// residual capacity).
300 DeltaResGraph dres_graph;
301 /// \brief Map for identifing deficit nodes.
302 DeficitBoolMap delta_deficit;
304 /// \brief The deficit nodes.
305 std::vector<ResNode> deficit_nodes;
307 #else //WITHOUT_CAPACITY_SCALING
308 typedef Dijkstra<ResGraph, ReducedCostMap, ResDijkstraTraits>
310 /// \brief \ref lemon::Dijkstra "Dijkstra" class for finding
311 /// shortest paths in the residual graph with respect to the
312 /// reduced edge costs.
313 ResDijkstra dijkstra;
314 /// \brief Map for identifing deficit nodes.
315 DeficitBoolMap has_deficit;
318 /// \brief Pred map for the \ref lemon::Dijkstra "Dijkstra" class.
319 typename ResDijkstra::PredMap pred;
320 /// \brief Dist map for the \ref lemon::Dijkstra "Dijkstra" class to
321 /// update node potentials.
322 PotentialUpdateMap updater;
326 /// \brief General constructor of the class (with lower bounds).
328 /// General constructor of the class (with lower bounds).
330 /// \param _graph The directed graph the algorithm runs on.
331 /// \param _lower The lower bounds of the edges.
332 /// \param _capacity The capacities (upper bounds) of the edges.
333 /// \param _cost The cost (length) values of the edges.
334 /// \param _supply The supply values of the nodes (signed).
335 CapacityScaling( const Graph &_graph,
336 const LowerMap &_lower,
337 const CapacityMap &_capacity,
338 const CostMap &_cost,
339 const SupplyMap &_supply ) :
340 graph(_graph), lower(&_lower), capacity(_graph), cost(_cost),
341 supply(_graph), flow(_graph, 0), potential(_graph, 0),
342 res_graph(_graph, capacity, flow),
343 red_cost(res_graph, cost, potential), imbalance(_graph),
345 delta(0), delta_filter(res_graph, delta),
346 dres_graph(res_graph, delta_filter),
347 dijkstra(dres_graph, red_cost), pred(dres_graph),
348 delta_deficit(imbalance, delta)
349 #else //WITHOUT_CAPACITY_SCALING
350 dijkstra(res_graph, red_cost), pred(res_graph),
351 has_deficit(imbalance)
354 // Removing nonzero lower bounds
355 capacity = subMap(_capacity, _lower);
357 for (NodeIt n(graph); n != INVALID; ++n) {
358 Supply s = _supply[n];
359 for (InEdgeIt e(graph, n); e != INVALID; ++e)
361 for (OutEdgeIt e(graph, n); e != INVALID; ++e)
366 valid_supply = sum == 0;
369 /// \brief General constructor of the class (without lower bounds).
371 /// General constructor of the class (without lower bounds).
373 /// \param _graph The directed graph the algorithm runs on.
374 /// \param _capacity The capacities (upper bounds) of the edges.
375 /// \param _cost The cost (length) values of the edges.
376 /// \param _supply The supply values of the nodes (signed).
377 CapacityScaling( const Graph &_graph,
378 const CapacityMap &_capacity,
379 const CostMap &_cost,
380 const SupplyMap &_supply ) :
381 graph(_graph), lower(NULL), capacity(_capacity), cost(_cost),
382 supply(_supply), flow(_graph, 0), potential(_graph, 0),
383 res_graph(_graph, capacity, flow),
384 red_cost(res_graph, cost, potential), imbalance(_graph),
386 delta(0), delta_filter(res_graph, delta),
387 dres_graph(res_graph, delta_filter),
388 dijkstra(dres_graph, red_cost), pred(dres_graph),
389 delta_deficit(imbalance, delta)
390 #else //WITHOUT_CAPACITY_SCALING
391 dijkstra(res_graph, red_cost), pred(res_graph),
392 has_deficit(imbalance)
395 // Checking the sum of supply values
397 for (NodeIt n(graph); n != INVALID; ++n) sum += supply[n];
398 valid_supply = sum == 0;
401 /// \brief Simple constructor of the class (with lower bounds).
403 /// Simple constructor of the class (with lower bounds).
405 /// \param _graph The directed graph the algorithm runs on.
406 /// \param _lower The lower bounds of the edges.
407 /// \param _capacity The capacities (upper bounds) of the edges.
408 /// \param _cost The cost (length) values of the edges.
409 /// \param _s The source node.
410 /// \param _t The target node.
411 /// \param _flow_value The required amount of flow from node \c _s
412 /// to node \c _t (i.e. the supply of \c _s and the demand of
414 CapacityScaling( const Graph &_graph,
415 const LowerMap &_lower,
416 const CapacityMap &_capacity,
417 const CostMap &_cost,
419 Supply _flow_value ) :
420 graph(_graph), lower(&_lower), capacity(_graph), cost(_cost),
421 supply(_graph), flow(_graph, 0), potential(_graph, 0),
422 res_graph(_graph, capacity, flow),
423 red_cost(res_graph, cost, potential), imbalance(_graph),
425 delta(0), delta_filter(res_graph, delta),
426 dres_graph(res_graph, delta_filter),
427 dijkstra(dres_graph, red_cost), pred(dres_graph),
428 delta_deficit(imbalance, delta)
429 #else //WITHOUT_CAPACITY_SCALING
430 dijkstra(res_graph, red_cost), pred(res_graph),
431 has_deficit(imbalance)
434 // Removing nonzero lower bounds
435 capacity = subMap(_capacity, _lower);
436 for (NodeIt n(graph); n != INVALID; ++n) {
438 if (n == _s) s = _flow_value;
439 if (n == _t) s = -_flow_value;
440 for (InEdgeIt e(graph, n); e != INVALID; ++e)
442 for (OutEdgeIt e(graph, n); e != INVALID; ++e)
449 /// \brief Simple constructor of the class (without lower bounds).
451 /// Simple constructor of the class (without lower bounds).
453 /// \param _graph The directed graph the algorithm runs on.
454 /// \param _capacity The capacities (upper bounds) of the edges.
455 /// \param _cost The cost (length) values of the edges.
456 /// \param _s The source node.
457 /// \param _t The target node.
458 /// \param _flow_value The required amount of flow from node \c _s
459 /// to node \c _t (i.e. the supply of \c _s and the demand of
461 CapacityScaling( const Graph &_graph,
462 const CapacityMap &_capacity,
463 const CostMap &_cost,
465 Supply _flow_value ) :
466 graph(_graph), lower(NULL), capacity(_capacity), cost(_cost),
467 supply(_graph, 0), flow(_graph, 0), potential(_graph, 0),
468 res_graph(_graph, capacity, flow),
469 red_cost(res_graph, cost, potential), imbalance(_graph),
471 delta(0), delta_filter(res_graph, delta),
472 dres_graph(res_graph, delta_filter),
473 dijkstra(dres_graph, red_cost), pred(dres_graph),
474 delta_deficit(imbalance, delta)
475 #else //WITHOUT_CAPACITY_SCALING
476 dijkstra(res_graph, red_cost), pred(res_graph),
477 has_deficit(imbalance)
480 supply[_s] = _flow_value;
481 supply[_t] = -_flow_value;
485 /// \brief Returns a const reference to the flow map.
487 /// Returns a const reference to the flow map.
489 /// \pre \ref run() must be called before using this function.
490 const FlowMap& flowMap() const {
494 /// \brief Returns a const reference to the potential map (the dual
497 /// Returns a const reference to the potential map (the dual
500 /// \pre \ref run() must be called before using this function.
501 const PotentialMap& potentialMap() const {
505 /// \brief Returns the total cost of the found flow.
507 /// Returns the total cost of the found flow. The complexity of the
508 /// function is \f$ O(e) \f$.
510 /// \pre \ref run() must be called before using this function.
511 Cost totalCost() const {
513 for (EdgeIt e(graph); e != INVALID; ++e)
514 c += flow[e] * cost[e];
518 /// \brief Runs the algorithm.
520 /// Runs the algorithm.
522 /// \return \c true if a feasible flow can be found.
524 return init() && start();
529 /// \brief Initializes the algorithm.
531 if (!valid_supply) return false;
534 // Initalizing Dijkstra class
535 updater.potentialMap(potential);
536 dijkstra.distMap(updater).predMap(pred);
539 // Initilaizing delta value
540 Supply max_sup = 0, max_dem = 0;
541 for (NodeIt n(graph); n != INVALID; ++n) {
542 if (supply[n] > max_sup) max_sup = supply[n];
543 if (supply[n] < -max_dem) max_dem = -supply[n];
545 if (max_dem < max_sup) max_sup = max_dem;
546 for ( delta = 1; SCALING_FACTOR * delta < max_sup;
547 delta *= SCALING_FACTOR ) ;
553 /// \brief Executes the capacity scaling version of the successive
554 /// shortest path algorithm.
556 typedef typename DeltaResGraph::EdgeIt DeltaResEdgeIt;
557 typedef typename DeltaResGraph::Edge DeltaResEdge;
562 // Processing capacity scaling phases
564 for ( ; delta >= 1; delta = delta < SCALING_FACTOR && delta > 1 ?
565 1 : delta / SCALING_FACTOR )
567 // Saturating edges not satisfying the optimality condition
569 for (DeltaResEdgeIt e(dres_graph); e != INVALID; ++e) {
570 if (red_cost[e] < 0) {
571 res_graph.augment(e, r = res_graph.rescap(e));
572 imbalance[dres_graph.source(e)] -= r;
573 imbalance[dres_graph.target(e)] += r;
577 // Finding excess nodes
578 excess_nodes.clear();
579 deficit_nodes.clear();
580 for (ResNodeIt n(res_graph); n != INVALID; ++n) {
581 if (imbalance[n] >= delta) excess_nodes.push_back(n);
582 if (imbalance[n] <= -delta) deficit_nodes.push_back(n);
586 // Finding shortest paths
587 while (next_node < excess_nodes.size()) {
588 // Checking deficit nodes
590 bool delta_def = false;
591 for (int i = 0; i < deficit_nodes.size(); ++i) {
592 if (imbalance[deficit_nodes[i]] <= -delta) {
597 if (!delta_def) break;
601 s = excess_nodes[next_node];
604 dijkstra.addSource(s);
608 if ((t = dijkstra.start(delta_deficit)) == INVALID) {
616 // Updating node potentials
619 // Augment along a shortest path from s to t
620 Capacity d = imbalance[s] < -imbalance[t] ?
621 imbalance[s] : -imbalance[t];
625 while ((e = pred[u]) != INVALID) {
626 if (res_graph.rescap(e) < d) d = res_graph.rescap(e);
627 u = dres_graph.source(e);
631 while ((e = pred[u]) != INVALID) {
632 res_graph.augment(e, d);
633 u = dres_graph.source(e);
637 if (imbalance[s] < delta) ++next_node;
641 std::cout << "Capacity Scaling algorithm finished with running Dijkstra algorithm "
642 << dijk_num << " times." << std::endl;
645 // Handling nonzero lower bounds
647 for (EdgeIt e(graph); e != INVALID; ++e)
648 flow[e] += (*lower)[e];
653 #else //WITHOUT_CAPACITY_SCALING
654 /// \brief Executes the successive shortest path algorithm without
655 /// capacity scaling.
657 // Finding excess nodes
658 for (ResNodeIt n(res_graph); n != INVALID; ++n) {
659 if (imbalance[n] > 0) excess_nodes.push_back(n);
661 if (excess_nodes.size() == 0) return true;
664 // Finding shortest paths
666 while ( imbalance[excess_nodes[next_node]] > 0 ||
667 ++next_node < excess_nodes.size() )
670 s = excess_nodes[next_node];
673 dijkstra.addSource(s);
674 if ((t = dijkstra.start(has_deficit)) == INVALID)
677 // Updating node potentials
680 // Augmenting along a shortest path from s to t
681 Capacity delta = imbalance[s] < -imbalance[t] ?
682 imbalance[s] : -imbalance[t];
685 while ((e = pred[u]) != INVALID) {
686 if (res_graph.rescap(e) < delta) delta = res_graph.rescap(e);
687 u = res_graph.source(e);
690 while ((e = pred[u]) != INVALID) {
691 res_graph.augment(e, delta);
692 u = res_graph.source(e);
694 imbalance[s] -= delta;
695 imbalance[t] += delta;
698 // Handling nonzero lower bounds
700 for (EdgeIt e(graph); e != INVALID; ++e)
701 flow[e] += (*lower)[e];
707 }; //class CapacityScaling
713 #endif //LEMON_CAPACITY_SCALING_H