1 /* -*- mode: C++; indent-tabs-mode: nil; -*-
3 * This file is a part of LEMON, a generic C++ optimization library.
5 * Copyright (C) 2003-2013
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_algs
25 /// \brief Capacity Scaling algorithm for finding a minimum cost flow.
29 #include <lemon/core.h>
30 #include <lemon/maps.h>
31 #include <lemon/bin_heap.h>
35 /// \brief Default traits class of CapacityScaling algorithm.
37 /// Default traits class of CapacityScaling algorithm.
38 /// \tparam GR Digraph type.
39 /// \tparam V The number type used for flow amounts, capacity bounds
40 /// and supply values. By default it is \c int.
41 /// \tparam C The number type used for costs and potentials.
42 /// By default it is the same as \c V.
43 template <typename GR, typename V = int, typename C = V>
44 struct CapacityScalingDefaultTraits
46 /// The type of the digraph
48 /// The type of the flow amounts, capacity bounds and supply values
50 /// The type of the arc costs
53 /// \brief The type of the heap used for internal Dijkstra computations.
55 /// The type of the heap used for internal Dijkstra computations.
56 /// It must conform to the \ref lemon::concepts::Heap "Heap" concept,
57 /// its priority type must be \c Cost and its cross reference type
58 /// must be \ref RangeMap "RangeMap<int>".
59 typedef BinHeap<Cost, RangeMap<int> > Heap;
62 /// \addtogroup min_cost_flow_algs
65 /// \brief Implementation of the Capacity Scaling algorithm for
66 /// finding a \ref min_cost_flow "minimum cost flow".
68 /// \ref CapacityScaling implements the capacity scaling version
69 /// of the successive shortest path algorithm for finding a
70 /// \ref min_cost_flow "minimum cost flow" \cite amo93networkflows,
71 /// \cite edmondskarp72theoretical. It is an efficient dual
72 /// solution method, which runs in polynomial time
73 /// \f$O(m\log U (n+m)\log n)\f$, where <i>U</i> denotes the maximum
74 /// of node supply and arc capacity values.
76 /// This algorithm is typically slower than \ref CostScaling and
77 /// \ref NetworkSimplex, but in special cases, it can be more
78 /// efficient than them.
79 /// (For more information, see \ref min_cost_flow_algs "the module page".)
81 /// Most of the parameters of the problem (except for the digraph)
82 /// can be given using separate functions, and the algorithm can be
83 /// executed using the \ref run() function. If some parameters are not
84 /// specified, then default values will be used.
86 /// \tparam GR The digraph type the algorithm runs on.
87 /// \tparam V The number type used for flow amounts, capacity bounds
88 /// and supply values in the algorithm. By default, it is \c int.
89 /// \tparam C The number type used for costs and potentials in the
90 /// algorithm. By default, it is the same as \c V.
91 /// \tparam TR The traits class that defines various types used by the
92 /// algorithm. By default, it is \ref CapacityScalingDefaultTraits
93 /// "CapacityScalingDefaultTraits<GR, V, C>".
94 /// In most cases, this parameter should not be set directly,
95 /// consider to use the named template parameters instead.
97 /// \warning Both \c V and \c C must be signed number types.
98 /// \warning Capacity bounds and supply values must be integer, but
99 /// arc costs can be arbitrary real numbers.
100 /// \warning This algorithm does not support negative costs for
101 /// arcs having infinite upper bound.
103 template <typename GR, typename V, typename C, typename TR>
105 template < typename GR, typename V = int, typename C = V,
106 typename TR = CapacityScalingDefaultTraits<GR, V, C> >
108 class CapacityScaling
112 /// The type of the digraph
113 typedef typename TR::Digraph Digraph;
114 /// The type of the flow amounts, capacity bounds and supply values
115 typedef typename TR::Value Value;
116 /// The type of the arc costs
117 typedef typename TR::Cost Cost;
119 /// The type of the heap used for internal Dijkstra computations
120 typedef typename TR::Heap Heap;
122 /// \brief The \ref lemon::CapacityScalingDefaultTraits "traits class"
128 /// \brief Problem type constants for the \c run() function.
130 /// Enum type containing the problem type constants that can be
131 /// returned by the \ref run() function of the algorithm.
133 /// The problem has no feasible solution (flow).
135 /// The problem has optimal solution (i.e. it is feasible and
136 /// bounded), and the algorithm has found optimal flow and node
137 /// potentials (primal and dual solutions).
139 /// The digraph contains an arc of negative cost and infinite
140 /// upper bound. It means that the objective function is unbounded
141 /// on that arc, however, note that it could actually be bounded
142 /// over the feasible flows, but this algroithm cannot handle
149 TEMPLATE_DIGRAPH_TYPEDEFS(GR);
151 typedef std::vector<int> IntVector;
152 typedef std::vector<Value> ValueVector;
153 typedef std::vector<Cost> CostVector;
154 typedef std::vector<char> BoolVector;
155 // Note: vector<char> is used instead of vector<bool> for efficiency reasons
159 // Data related to the underlying digraph
166 // Parameters of the problem
170 // Data structures for storing the digraph
174 IntVector _first_out;
186 ValueVector _res_cap;
189 IntVector _excess_nodes;
190 IntVector _deficit_nodes;
198 /// \brief Constant for infinite upper bounds (capacities).
200 /// Constant for infinite upper bounds (capacities).
201 /// It is \c std::numeric_limits<Value>::infinity() if available,
202 /// \c std::numeric_limits<Value>::max() otherwise.
207 // Special implementation of the Dijkstra algorithm for finding
208 // shortest paths in the residual network of the digraph with
209 // respect to the reduced arc costs and modifying the node
210 // potentials according to the found distance labels.
211 class ResidualDijkstra
217 const IntVector &_first_out;
218 const IntVector &_target;
219 const CostVector &_cost;
220 const ValueVector &_res_cap;
221 const ValueVector &_excess;
225 IntVector _proc_nodes;
230 ResidualDijkstra(CapacityScaling& cs) :
231 _node_num(cs._node_num), _geq(cs._sum_supply < 0),
232 _first_out(cs._first_out), _target(cs._target), _cost(cs._cost),
233 _res_cap(cs._res_cap), _excess(cs._excess), _pi(cs._pi),
234 _pred(cs._pred), _dist(cs._node_num)
237 int run(int s, Value delta = 1) {
238 RangeMap<int> heap_cross_ref(_node_num, Heap::PRE_HEAP);
239 Heap heap(heap_cross_ref);
245 while (!heap.empty() && _excess[heap.top()] > -delta) {
246 int u = heap.top(), v;
247 Cost d = heap.prio() + _pi[u], dn;
248 _dist[u] = heap.prio();
249 _proc_nodes.push_back(u);
252 // Traverse outgoing residual arcs
253 int last_out = _geq ? _first_out[u+1] : _first_out[u+1] - 1;
254 for (int a = _first_out[u]; a != last_out; ++a) {
255 if (_res_cap[a] < delta) continue;
257 switch (heap.state(v)) {
259 heap.push(v, d + _cost[a] - _pi[v]);
263 dn = d + _cost[a] - _pi[v];
265 heap.decrease(v, dn);
269 case Heap::POST_HEAP:
274 if (heap.empty()) return -1;
276 // Update potentials of processed nodes
278 Cost dt = heap.prio();
279 for (int i = 0; i < int(_proc_nodes.size()); ++i) {
280 _pi[_proc_nodes[i]] += _dist[_proc_nodes[i]] - dt;
286 }; //class ResidualDijkstra
290 /// \name Named Template Parameters
293 template <typename T>
294 struct SetHeapTraits : public Traits {
298 /// \brief \ref named-templ-param "Named parameter" for setting
301 /// \ref named-templ-param "Named parameter" for setting \c Heap
302 /// type, which is used for internal Dijkstra computations.
303 /// It must conform to the \ref lemon::concepts::Heap "Heap" concept,
304 /// its priority type must be \c Cost and its cross reference type
305 /// must be \ref RangeMap "RangeMap<int>".
306 template <typename T>
308 : public CapacityScaling<GR, V, C, SetHeapTraits<T> > {
309 typedef CapacityScaling<GR, V, C, SetHeapTraits<T> > Create;
320 /// \brief Constructor.
322 /// The constructor of the class.
324 /// \param graph The digraph the algorithm runs on.
325 CapacityScaling(const GR& graph) :
326 _graph(graph), _node_id(graph), _arc_idf(graph), _arc_idb(graph),
327 INF(std::numeric_limits<Value>::has_infinity ?
328 std::numeric_limits<Value>::infinity() :
329 std::numeric_limits<Value>::max())
331 // Check the number types
332 LEMON_ASSERT(std::numeric_limits<Value>::is_signed,
333 "The flow type of CapacityScaling must be signed");
334 LEMON_ASSERT(std::numeric_limits<Cost>::is_signed,
335 "The cost type of CapacityScaling must be signed");
337 // Reset data structures
342 /// The parameters of the algorithm can be specified using these
347 /// \brief Set the lower bounds on the arcs.
349 /// This function sets the lower bounds on the arcs.
350 /// If it is not used before calling \ref run(), the lower bounds
351 /// will be set to zero on all arcs.
353 /// \param map An arc map storing the lower bounds.
354 /// Its \c Value type must be convertible to the \c Value type
355 /// of the algorithm.
357 /// \return <tt>(*this)</tt>
358 template <typename LowerMap>
359 CapacityScaling& lowerMap(const LowerMap& map) {
361 for (ArcIt a(_graph); a != INVALID; ++a) {
362 _lower[_arc_idf[a]] = map[a];
367 /// \brief Set the upper bounds (capacities) on the arcs.
369 /// This function sets the upper bounds (capacities) on the arcs.
370 /// If it is not used before calling \ref run(), the upper bounds
371 /// will be set to \ref INF on all arcs (i.e. the flow value will be
372 /// unbounded from above).
374 /// \param map An arc map storing the upper bounds.
375 /// Its \c Value type must be convertible to the \c Value type
376 /// of the algorithm.
378 /// \return <tt>(*this)</tt>
379 template<typename UpperMap>
380 CapacityScaling& upperMap(const UpperMap& map) {
381 for (ArcIt a(_graph); a != INVALID; ++a) {
382 _upper[_arc_idf[a]] = map[a];
387 /// \brief Set the costs of the arcs.
389 /// This function sets the costs of the arcs.
390 /// If it is not used before calling \ref run(), the costs
391 /// will be set to \c 1 on all arcs.
393 /// \param map An arc map storing the costs.
394 /// Its \c Value type must be convertible to the \c Cost type
395 /// of the algorithm.
397 /// \return <tt>(*this)</tt>
398 template<typename CostMap>
399 CapacityScaling& costMap(const CostMap& map) {
400 for (ArcIt a(_graph); a != INVALID; ++a) {
401 _cost[_arc_idf[a]] = map[a];
402 _cost[_arc_idb[a]] = -map[a];
407 /// \brief Set the supply values of the nodes.
409 /// This function sets the supply values of the nodes.
410 /// If neither this function nor \ref stSupply() is used before
411 /// calling \ref run(), the supply of each node will be set to zero.
413 /// \param map A node map storing the supply values.
414 /// Its \c Value type must be convertible to the \c Value type
415 /// of the algorithm.
417 /// \return <tt>(*this)</tt>
418 template<typename SupplyMap>
419 CapacityScaling& supplyMap(const SupplyMap& map) {
420 for (NodeIt n(_graph); n != INVALID; ++n) {
421 _supply[_node_id[n]] = map[n];
426 /// \brief Set single source and target nodes and a supply value.
428 /// This function sets a single source node and a single target node
429 /// and the required flow value.
430 /// If neither this function nor \ref supplyMap() is used before
431 /// calling \ref run(), the supply of each node will be set to zero.
433 /// Using this function has the same effect as using \ref supplyMap()
434 /// with a map in which \c k is assigned to \c s, \c -k is
435 /// assigned to \c t and all other nodes have zero supply value.
437 /// \param s The source node.
438 /// \param t The target node.
439 /// \param k The required amount of flow from node \c s to node \c t
440 /// (i.e. the supply of \c s and the demand of \c t).
442 /// \return <tt>(*this)</tt>
443 CapacityScaling& stSupply(const Node& s, const Node& t, Value k) {
444 for (int i = 0; i != _node_num; ++i) {
447 _supply[_node_id[s]] = k;
448 _supply[_node_id[t]] = -k;
454 /// \name Execution control
455 /// The algorithm can be executed using \ref run().
459 /// \brief Run the algorithm.
461 /// This function runs the algorithm.
462 /// The paramters can be specified using functions \ref lowerMap(),
463 /// \ref upperMap(), \ref costMap(), \ref supplyMap(), \ref stSupply().
466 /// CapacityScaling<ListDigraph> cs(graph);
467 /// cs.lowerMap(lower).upperMap(upper).costMap(cost)
468 /// .supplyMap(sup).run();
471 /// This function can be called more than once. All the given parameters
472 /// are kept for the next call, unless \ref resetParams() or \ref reset()
473 /// is used, thus only the modified parameters have to be set again.
474 /// If the underlying digraph was also modified after the construction
475 /// of the class (or the last \ref reset() call), then the \ref reset()
476 /// function must be called.
478 /// \param factor The capacity scaling factor. It must be larger than
479 /// one to use scaling. If it is less or equal to one, then scaling
480 /// will be disabled.
482 /// \return \c INFEASIBLE if no feasible flow exists,
483 /// \n \c OPTIMAL if the problem has optimal solution
484 /// (i.e. it is feasible and bounded), and the algorithm has found
485 /// optimal flow and node potentials (primal and dual solutions),
486 /// \n \c UNBOUNDED if the digraph contains an arc of negative cost
487 /// and infinite upper bound. It means that the objective function
488 /// is unbounded on that arc, however, note that it could actually be
489 /// bounded over the feasible flows, but this algroithm cannot handle
493 /// \see resetParams(), reset()
494 ProblemType run(int factor = 4) {
496 ProblemType pt = init();
497 if (pt != OPTIMAL) return pt;
501 /// \brief Reset all the parameters that have been given before.
503 /// This function resets all the paramaters that have been given
504 /// before using functions \ref lowerMap(), \ref upperMap(),
505 /// \ref costMap(), \ref supplyMap(), \ref stSupply().
507 /// It is useful for multiple \ref run() calls. Basically, all the given
508 /// parameters are kept for the next \ref run() call, unless
509 /// \ref resetParams() or \ref reset() is used.
510 /// If the underlying digraph was also modified after the construction
511 /// of the class or the last \ref reset() call, then the \ref reset()
512 /// function must be used, otherwise \ref resetParams() is sufficient.
516 /// CapacityScaling<ListDigraph> cs(graph);
519 /// cs.lowerMap(lower).upperMap(upper).costMap(cost)
520 /// .supplyMap(sup).run();
522 /// // Run again with modified cost map (resetParams() is not called,
523 /// // so only the cost map have to be set again)
525 /// cs.costMap(cost).run();
527 /// // Run again from scratch using resetParams()
528 /// // (the lower bounds will be set to zero on all arcs)
529 /// cs.resetParams();
530 /// cs.upperMap(capacity).costMap(cost)
531 /// .supplyMap(sup).run();
534 /// \return <tt>(*this)</tt>
536 /// \see reset(), run()
537 CapacityScaling& resetParams() {
538 for (int i = 0; i != _node_num; ++i) {
541 for (int j = 0; j != _res_arc_num; ++j) {
544 _cost[j] = _forward[j] ? 1 : -1;
550 /// \brief Reset the internal data structures and all the parameters
551 /// that have been given before.
553 /// This function resets the internal data structures and all the
554 /// paramaters that have been given before using functions \ref lowerMap(),
555 /// \ref upperMap(), \ref costMap(), \ref supplyMap(), \ref stSupply().
557 /// It is useful for multiple \ref run() calls. Basically, all the given
558 /// parameters are kept for the next \ref run() call, unless
559 /// \ref resetParams() or \ref reset() is used.
560 /// If the underlying digraph was also modified after the construction
561 /// of the class or the last \ref reset() call, then the \ref reset()
562 /// function must be used, otherwise \ref resetParams() is sufficient.
564 /// See \ref resetParams() for examples.
566 /// \return <tt>(*this)</tt>
568 /// \see resetParams(), run()
569 CapacityScaling& reset() {
571 _node_num = countNodes(_graph);
572 _arc_num = countArcs(_graph);
573 _res_arc_num = 2 * (_arc_num + _node_num);
577 _first_out.resize(_node_num + 1);
578 _forward.resize(_res_arc_num);
579 _source.resize(_res_arc_num);
580 _target.resize(_res_arc_num);
581 _reverse.resize(_res_arc_num);
583 _lower.resize(_res_arc_num);
584 _upper.resize(_res_arc_num);
585 _cost.resize(_res_arc_num);
586 _supply.resize(_node_num);
588 _res_cap.resize(_res_arc_num);
589 _pi.resize(_node_num);
590 _excess.resize(_node_num);
591 _pred.resize(_node_num);
594 int i = 0, j = 0, k = 2 * _arc_num + _node_num - 1;
595 for (NodeIt n(_graph); n != INVALID; ++n, ++i) {
599 for (NodeIt n(_graph); n != INVALID; ++n, ++i) {
601 for (OutArcIt a(_graph, n); a != INVALID; ++a, ++j) {
605 _target[j] = _node_id[_graph.runningNode(a)];
607 for (InArcIt a(_graph, n); a != INVALID; ++a, ++j) {
611 _target[j] = _node_id[_graph.runningNode(a)];
624 _first_out[_node_num] = k;
625 for (ArcIt a(_graph); a != INVALID; ++a) {
626 int fi = _arc_idf[a];
627 int bi = _arc_idb[a];
639 /// \name Query Functions
640 /// The results of the algorithm can be obtained using these
642 /// The \ref run() function must be called before using them.
646 /// \brief Return the total cost of the found flow.
648 /// This function returns the total cost of the found flow.
649 /// Its complexity is O(m).
651 /// \note The return type of the function can be specified as a
652 /// template parameter. For example,
654 /// cs.totalCost<double>();
656 /// It is useful if the total cost cannot be stored in the \c Cost
657 /// type of the algorithm, which is the default return type of the
660 /// \pre \ref run() must be called before using this function.
661 template <typename Number>
662 Number totalCost() const {
664 for (ArcIt a(_graph); a != INVALID; ++a) {
666 c += static_cast<Number>(_res_cap[i]) *
667 (-static_cast<Number>(_cost[i]));
673 Cost totalCost() const {
674 return totalCost<Cost>();
678 /// \brief Return the flow on the given arc.
680 /// This function returns the flow on the given arc.
682 /// \pre \ref run() must be called before using this function.
683 Value flow(const Arc& a) const {
684 return _res_cap[_arc_idb[a]];
687 /// \brief Copy the flow values (the primal solution) into the
690 /// This function copies the flow value on each arc into the given
691 /// map. The \c Value type of the algorithm must be convertible to
692 /// the \c Value type of the map.
694 /// \pre \ref run() must be called before using this function.
695 template <typename FlowMap>
696 void flowMap(FlowMap &map) const {
697 for (ArcIt a(_graph); a != INVALID; ++a) {
698 map.set(a, _res_cap[_arc_idb[a]]);
702 /// \brief Return the potential (dual value) of the given node.
704 /// This function returns the potential (dual value) of the
707 /// \pre \ref run() must be called before using this function.
708 Cost potential(const Node& n) const {
709 return _pi[_node_id[n]];
712 /// \brief Copy the potential values (the dual solution) into the
715 /// This function copies the potential (dual value) of each node
716 /// into the given map.
717 /// The \c Cost type of the algorithm must be convertible to the
718 /// \c Value type of the map.
720 /// \pre \ref run() must be called before using this function.
721 template <typename PotentialMap>
722 void potentialMap(PotentialMap &map) const {
723 for (NodeIt n(_graph); n != INVALID; ++n) {
724 map.set(n, _pi[_node_id[n]]);
732 // Initialize the algorithm
734 if (_node_num <= 1) return INFEASIBLE;
736 // Check the sum of supply values
738 for (int i = 0; i != _root; ++i) {
739 _sum_supply += _supply[i];
741 if (_sum_supply > 0) return INFEASIBLE;
743 // Check lower and upper bounds
744 LEMON_DEBUG(checkBoundMaps(),
745 "Upper bounds must be greater or equal to the lower bounds");
748 // Initialize vectors
749 for (int i = 0; i != _root; ++i) {
751 _excess[i] = _supply[i];
754 // Remove non-zero lower bounds
755 const Value MAX = std::numeric_limits<Value>::max();
758 for (int i = 0; i != _root; ++i) {
759 last_out = _first_out[i+1];
760 for (int j = _first_out[i]; j != last_out; ++j) {
764 _res_cap[j] = _upper[j] < MAX ? _upper[j] - c : INF;
766 _res_cap[j] = _upper[j] < MAX + c ? _upper[j] - c : INF;
769 _excess[_target[j]] += c;
776 for (int j = 0; j != _res_arc_num; ++j) {
777 _res_cap[j] = _forward[j] ? _upper[j] : 0;
781 // Handle negative costs
782 for (int i = 0; i != _root; ++i) {
783 last_out = _first_out[i+1] - 1;
784 for (int j = _first_out[i]; j != last_out; ++j) {
785 Value rc = _res_cap[j];
786 if (_cost[j] < 0 && rc > 0) {
787 if (rc >= MAX) return UNBOUNDED;
789 _excess[_target[j]] += rc;
791 _res_cap[_reverse[j]] += rc;
796 // Handle GEQ supply type
797 if (_sum_supply < 0) {
799 _excess[_root] = -_sum_supply;
800 for (int a = _first_out[_root]; a != _res_arc_num; ++a) {
801 int ra = _reverse[a];
802 _res_cap[a] = -_sum_supply + 1;
810 for (int a = _first_out[_root]; a != _res_arc_num; ++a) {
811 int ra = _reverse[a];
819 // Initialize delta value
822 Value max_sup = 0, max_dem = 0, max_cap = 0;
823 for (int i = 0; i != _root; ++i) {
824 Value ex = _excess[i];
825 if ( ex > max_sup) max_sup = ex;
826 if (-ex > max_dem) max_dem = -ex;
827 int last_out = _first_out[i+1] - 1;
828 for (int j = _first_out[i]; j != last_out; ++j) {
829 if (_res_cap[j] > max_cap) max_cap = _res_cap[j];
832 max_sup = std::min(std::min(max_sup, max_dem), max_cap);
833 for (_delta = 1; 2 * _delta <= max_sup; _delta *= 2) ;
842 // Check if the upper bound is greater than or equal to the lower bound
843 // on each forward arc.
844 bool checkBoundMaps() {
845 for (int j = 0; j != _res_arc_num; ++j) {
846 if (_forward[j] && _upper[j] < _lower[j]) return false;
851 ProblemType start() {
852 // Execute the algorithm
855 pt = startWithScaling();
857 pt = startWithoutScaling();
859 // Handle non-zero lower bounds
861 int limit = _first_out[_root];
862 for (int j = 0; j != limit; ++j) {
863 if (_forward[j]) _res_cap[_reverse[j]] += _lower[j];
867 // Shift potentials if necessary
868 Cost pr = _pi[_root];
869 if (_sum_supply < 0 || pr > 0) {
870 for (int i = 0; i != _node_num; ++i) {
878 // Execute the capacity scaling algorithm
879 ProblemType startWithScaling() {
880 // Perform capacity scaling phases
882 ResidualDijkstra _dijkstra(*this);
884 // Saturate all arcs not satisfying the optimality condition
886 for (int u = 0; u != _node_num; ++u) {
887 last_out = _sum_supply < 0 ?
888 _first_out[u+1] : _first_out[u+1] - 1;
889 for (int a = _first_out[u]; a != last_out; ++a) {
891 Cost c = _cost[a] + _pi[u] - _pi[v];
892 Value rc = _res_cap[a];
893 if (c < 0 && rc >= _delta) {
897 _res_cap[_reverse[a]] += rc;
902 // Find excess nodes and deficit nodes
903 _excess_nodes.clear();
904 _deficit_nodes.clear();
905 for (int u = 0; u != _node_num; ++u) {
906 Value ex = _excess[u];
907 if (ex >= _delta) _excess_nodes.push_back(u);
908 if (ex <= -_delta) _deficit_nodes.push_back(u);
910 int next_node = 0, next_def_node = 0;
912 // Find augmenting shortest paths
913 while (next_node < int(_excess_nodes.size())) {
914 // Check deficit nodes
916 bool delta_deficit = false;
917 for ( ; next_def_node < int(_deficit_nodes.size());
919 if (_excess[_deficit_nodes[next_def_node]] <= -_delta) {
920 delta_deficit = true;
924 if (!delta_deficit) break;
927 // Run Dijkstra in the residual network
928 s = _excess_nodes[next_node];
929 if ((t = _dijkstra.run(s, _delta)) == -1) {
937 // Augment along a shortest path from s to t
938 Value d = std::min(_excess[s], -_excess[t]);
942 while ((a = _pred[u]) != -1) {
943 if (_res_cap[a] < d) d = _res_cap[a];
948 while ((a = _pred[u]) != -1) {
950 _res_cap[_reverse[a]] += d;
956 if (_excess[s] < _delta) ++next_node;
959 if (_delta == 1) break;
960 _delta = _delta <= _factor ? 1 : _delta / _factor;
966 // Execute the successive shortest path algorithm
967 ProblemType startWithoutScaling() {
969 _excess_nodes.clear();
970 for (int i = 0; i != _node_num; ++i) {
971 if (_excess[i] > 0) _excess_nodes.push_back(i);
973 if (_excess_nodes.size() == 0) return OPTIMAL;
976 // Find shortest paths
978 ResidualDijkstra _dijkstra(*this);
979 while ( _excess[_excess_nodes[next_node]] > 0 ||
980 ++next_node < int(_excess_nodes.size()) )
982 // Run Dijkstra in the residual network
983 s = _excess_nodes[next_node];
984 if ((t = _dijkstra.run(s)) == -1) return INFEASIBLE;
986 // Augment along a shortest path from s to t
987 Value d = std::min(_excess[s], -_excess[t]);
991 while ((a = _pred[u]) != -1) {
992 if (_res_cap[a] < d) d = _res_cap[a];
997 while ((a = _pred[u]) != -1) {
999 _res_cap[_reverse[a]] += d;
1009 }; //class CapacityScaling
1015 #endif //LEMON_CAPACITY_SCALING_H