1 /* -*- mode: C++; indent-tabs-mode: nil; -*-
3 * This file is a part of LEMON, a generic C++ optimization library.
5 * Copyright (C) 2003-2010
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/bin_heap.h>
34 /// \brief Default traits class of CapacityScaling algorithm.
36 /// Default traits class of CapacityScaling algorithm.
37 /// \tparam GR Digraph type.
38 /// \tparam V The number type used for flow amounts, capacity bounds
39 /// and supply values. By default it is \c int.
40 /// \tparam C The number type used for costs and potentials.
41 /// By default it is the same as \c V.
42 template <typename GR, typename V = int, typename C = V>
43 struct CapacityScalingDefaultTraits
45 /// The type of the digraph
47 /// The type of the flow amounts, capacity bounds and supply values
49 /// The type of the arc costs
52 /// \brief The type of the heap used for internal Dijkstra computations.
54 /// The type of the heap used for internal Dijkstra computations.
55 /// It must conform to the \ref lemon::concepts::Heap "Heap" concept,
56 /// its priority type must be \c Cost and its cross reference type
57 /// must be \ref RangeMap "RangeMap<int>".
58 typedef BinHeap<Cost, RangeMap<int> > Heap;
61 /// \addtogroup min_cost_flow_algs
64 /// \brief Implementation of the Capacity Scaling algorithm for
65 /// finding a \ref min_cost_flow "minimum cost flow".
67 /// \ref CapacityScaling implements the capacity scaling version
68 /// of the successive shortest path algorithm for finding a
69 /// \ref min_cost_flow "minimum cost flow" \cite amo93networkflows,
70 /// \cite edmondskarp72theoretical. It is an efficient dual
71 /// solution method, which runs in polynomial time
72 /// \f$O(m\log U (n+m)\log n)\f$, where <i>U</i> denotes the maximum
73 /// of node supply and arc capacity values.
75 /// This algorithm is typically slower than \ref CostScaling and
76 /// \ref NetworkSimplex, but in special cases, it can be more
77 /// efficient than them.
78 /// (For more information, see \ref min_cost_flow_algs "the module page".)
80 /// Most of the parameters of the problem (except for the digraph)
81 /// can be given using separate functions, and the algorithm can be
82 /// executed using the \ref run() function. If some parameters are not
83 /// specified, then default values will be used.
85 /// \tparam GR The digraph type the algorithm runs on.
86 /// \tparam V The number type used for flow amounts, capacity bounds
87 /// and supply values in the algorithm. By default, it is \c int.
88 /// \tparam C The number type used for costs and potentials in the
89 /// algorithm. By default, it is the same as \c V.
90 /// \tparam TR The traits class that defines various types used by the
91 /// algorithm. By default, it is \ref CapacityScalingDefaultTraits
92 /// "CapacityScalingDefaultTraits<GR, V, C>".
93 /// In most cases, this parameter should not be set directly,
94 /// consider to use the named template parameters instead.
96 /// \warning Both \c V and \c C must be signed number types.
97 /// \warning Capacity bounds and supply values must be integer, but
98 /// arc costs can be arbitrary real numbers.
99 /// \warning This algorithm does not support negative costs for
100 /// arcs having infinite upper bound.
102 template <typename GR, typename V, typename C, typename TR>
104 template < typename GR, typename V = int, typename C = V,
105 typename TR = CapacityScalingDefaultTraits<GR, V, C> >
107 class CapacityScaling
111 /// The type of the digraph
112 typedef typename TR::Digraph Digraph;
113 /// The type of the flow amounts, capacity bounds and supply values
114 typedef typename TR::Value Value;
115 /// The type of the arc costs
116 typedef typename TR::Cost Cost;
118 /// The type of the heap used for internal Dijkstra computations
119 typedef typename TR::Heap Heap;
121 /// \brief The \ref lemon::CapacityScalingDefaultTraits "traits class"
127 /// \brief Problem type constants for the \c run() function.
129 /// Enum type containing the problem type constants that can be
130 /// returned by the \ref run() function of the algorithm.
132 /// The problem has no feasible solution (flow).
134 /// The problem has optimal solution (i.e. it is feasible and
135 /// bounded), and the algorithm has found optimal flow and node
136 /// potentials (primal and dual solutions).
138 /// The digraph contains an arc of negative cost and infinite
139 /// upper bound. It means that the objective function is unbounded
140 /// on that arc, however, note that it could actually be bounded
141 /// over the feasible flows, but this algroithm cannot handle
148 TEMPLATE_DIGRAPH_TYPEDEFS(GR);
150 typedef std::vector<int> IntVector;
151 typedef std::vector<Value> ValueVector;
152 typedef std::vector<Cost> CostVector;
153 typedef std::vector<char> BoolVector;
154 // Note: vector<char> is used instead of vector<bool> for efficiency reasons
158 // Data related to the underlying digraph
165 // Parameters of the problem
169 // Data structures for storing the digraph
173 IntVector _first_out;
185 ValueVector _res_cap;
188 IntVector _excess_nodes;
189 IntVector _deficit_nodes;
197 /// \brief Constant for infinite upper bounds (capacities).
199 /// Constant for infinite upper bounds (capacities).
200 /// It is \c std::numeric_limits<Value>::infinity() if available,
201 /// \c std::numeric_limits<Value>::max() otherwise.
206 // Special implementation of the Dijkstra algorithm for finding
207 // shortest paths in the residual network of the digraph with
208 // respect to the reduced arc costs and modifying the node
209 // potentials according to the found distance labels.
210 class ResidualDijkstra
216 const IntVector &_first_out;
217 const IntVector &_target;
218 const CostVector &_cost;
219 const ValueVector &_res_cap;
220 const ValueVector &_excess;
224 IntVector _proc_nodes;
229 ResidualDijkstra(CapacityScaling& cs) :
230 _node_num(cs._node_num), _geq(cs._sum_supply < 0),
231 _first_out(cs._first_out), _target(cs._target), _cost(cs._cost),
232 _res_cap(cs._res_cap), _excess(cs._excess), _pi(cs._pi),
233 _pred(cs._pred), _dist(cs._node_num)
236 int run(int s, Value delta = 1) {
237 RangeMap<int> heap_cross_ref(_node_num, Heap::PRE_HEAP);
238 Heap heap(heap_cross_ref);
244 while (!heap.empty() && _excess[heap.top()] > -delta) {
245 int u = heap.top(), v;
246 Cost d = heap.prio() + _pi[u], dn;
247 _dist[u] = heap.prio();
248 _proc_nodes.push_back(u);
251 // Traverse outgoing residual arcs
252 int last_out = _geq ? _first_out[u+1] : _first_out[u+1] - 1;
253 for (int a = _first_out[u]; a != last_out; ++a) {
254 if (_res_cap[a] < delta) continue;
256 switch (heap.state(v)) {
258 heap.push(v, d + _cost[a] - _pi[v]);
262 dn = d + _cost[a] - _pi[v];
264 heap.decrease(v, dn);
268 case Heap::POST_HEAP:
273 if (heap.empty()) return -1;
275 // Update potentials of processed nodes
277 Cost dt = heap.prio();
278 for (int i = 0; i < int(_proc_nodes.size()); ++i) {
279 _pi[_proc_nodes[i]] += _dist[_proc_nodes[i]] - dt;
285 }; //class ResidualDijkstra
289 /// \name Named Template Parameters
292 template <typename T>
293 struct SetHeapTraits : public Traits {
297 /// \brief \ref named-templ-param "Named parameter" for setting
300 /// \ref named-templ-param "Named parameter" for setting \c Heap
301 /// type, which is used for internal Dijkstra computations.
302 /// It must conform to the \ref lemon::concepts::Heap "Heap" concept,
303 /// its priority type must be \c Cost and its cross reference type
304 /// must be \ref RangeMap "RangeMap<int>".
305 template <typename T>
307 : public CapacityScaling<GR, V, C, SetHeapTraits<T> > {
308 typedef CapacityScaling<GR, V, C, SetHeapTraits<T> > Create;
319 /// \brief Constructor.
321 /// The constructor of the class.
323 /// \param graph The digraph the algorithm runs on.
324 CapacityScaling(const GR& graph) :
325 _graph(graph), _node_id(graph), _arc_idf(graph), _arc_idb(graph),
326 INF(std::numeric_limits<Value>::has_infinity ?
327 std::numeric_limits<Value>::infinity() :
328 std::numeric_limits<Value>::max())
330 // Check the number types
331 LEMON_ASSERT(std::numeric_limits<Value>::is_signed,
332 "The flow type of CapacityScaling must be signed");
333 LEMON_ASSERT(std::numeric_limits<Cost>::is_signed,
334 "The cost type of CapacityScaling must be signed");
336 // Reset data structures
341 /// The parameters of the algorithm can be specified using these
346 /// \brief Set the lower bounds on the arcs.
348 /// This function sets the lower bounds on the arcs.
349 /// If it is not used before calling \ref run(), the lower bounds
350 /// will be set to zero on all arcs.
352 /// \param map An arc map storing the lower bounds.
353 /// Its \c Value type must be convertible to the \c Value type
354 /// of the algorithm.
356 /// \return <tt>(*this)</tt>
357 template <typename LowerMap>
358 CapacityScaling& lowerMap(const LowerMap& map) {
360 for (ArcIt a(_graph); a != INVALID; ++a) {
361 _lower[_arc_idf[a]] = map[a];
362 _lower[_arc_idb[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 or equal to the lower bound
844 bool checkBoundMaps() {
845 for (int j = 0; j != _res_arc_num; ++j) {
846 if (_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[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