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" \ref amo93networkflows,
70 /// \ref edmondskarp72theoretical. It is an efficient dual
73 /// Most of the parameters of the problem (except for the digraph)
74 /// can be given using separate functions, and the algorithm can be
75 /// executed using the \ref run() function. If some parameters are not
76 /// specified, then default values will be used.
78 /// \tparam GR The digraph type the algorithm runs on.
79 /// \tparam V The number type used for flow amounts, capacity bounds
80 /// and supply values in the algorithm. By default, it is \c int.
81 /// \tparam C The number type used for costs and potentials in the
82 /// algorithm. By default, it is the same as \c V.
83 /// \tparam TR The traits class that defines various types used by the
84 /// algorithm. By default, it is \ref CapacityScalingDefaultTraits
85 /// "CapacityScalingDefaultTraits<GR, V, C>".
86 /// In most cases, this parameter should not be set directly,
87 /// consider to use the named template parameters instead.
89 /// \warning Both \c V and \c C must be signed number types.
90 /// \warning All input data (capacities, supply values, and costs) must
92 /// \warning This algorithm does not support negative costs for
93 /// arcs having infinite upper bound.
95 template <typename GR, typename V, typename C, typename TR>
97 template < typename GR, typename V = int, typename C = V,
98 typename TR = CapacityScalingDefaultTraits<GR, V, C> >
100 class CapacityScaling
104 /// The type of the digraph
105 typedef typename TR::Digraph Digraph;
106 /// The type of the flow amounts, capacity bounds and supply values
107 typedef typename TR::Value Value;
108 /// The type of the arc costs
109 typedef typename TR::Cost Cost;
111 /// The type of the heap used for internal Dijkstra computations
112 typedef typename TR::Heap Heap;
114 /// The \ref CapacityScalingDefaultTraits "traits class" of the algorithm
119 /// \brief Problem type constants for the \c run() function.
121 /// Enum type containing the problem type constants that can be
122 /// returned by the \ref run() function of the algorithm.
124 /// The problem has no feasible solution (flow).
126 /// The problem has optimal solution (i.e. it is feasible and
127 /// bounded), and the algorithm has found optimal flow and node
128 /// potentials (primal and dual solutions).
130 /// The digraph contains an arc of negative cost and infinite
131 /// upper bound. It means that the objective function is unbounded
132 /// on that arc, however, note that it could actually be bounded
133 /// over the feasible flows, but this algroithm cannot handle
140 TEMPLATE_DIGRAPH_TYPEDEFS(GR);
142 typedef std::vector<int> IntVector;
143 typedef std::vector<Value> ValueVector;
144 typedef std::vector<Cost> CostVector;
145 typedef std::vector<char> BoolVector;
146 // Note: vector<char> is used instead of vector<bool> for efficiency reasons
150 // Data related to the underlying digraph
157 // Parameters of the problem
161 // Data structures for storing the digraph
165 IntVector _first_out;
177 ValueVector _res_cap;
180 IntVector _excess_nodes;
181 IntVector _deficit_nodes;
189 /// \brief Constant for infinite upper bounds (capacities).
191 /// Constant for infinite upper bounds (capacities).
192 /// It is \c std::numeric_limits<Value>::infinity() if available,
193 /// \c std::numeric_limits<Value>::max() otherwise.
198 // Special implementation of the Dijkstra algorithm for finding
199 // shortest paths in the residual network of the digraph with
200 // respect to the reduced arc costs and modifying the node
201 // potentials according to the found distance labels.
202 class ResidualDijkstra
208 const IntVector &_first_out;
209 const IntVector &_target;
210 const CostVector &_cost;
211 const ValueVector &_res_cap;
212 const ValueVector &_excess;
216 IntVector _proc_nodes;
221 ResidualDijkstra(CapacityScaling& cs) :
222 _node_num(cs._node_num), _geq(cs._sum_supply < 0),
223 _first_out(cs._first_out), _target(cs._target), _cost(cs._cost),
224 _res_cap(cs._res_cap), _excess(cs._excess), _pi(cs._pi),
225 _pred(cs._pred), _dist(cs._node_num)
228 int run(int s, Value delta = 1) {
229 RangeMap<int> heap_cross_ref(_node_num, Heap::PRE_HEAP);
230 Heap heap(heap_cross_ref);
236 while (!heap.empty() && _excess[heap.top()] > -delta) {
237 int u = heap.top(), v;
238 Cost d = heap.prio() + _pi[u], dn;
239 _dist[u] = heap.prio();
240 _proc_nodes.push_back(u);
243 // Traverse outgoing residual arcs
244 int last_out = _geq ? _first_out[u+1] : _first_out[u+1] - 1;
245 for (int a = _first_out[u]; a != last_out; ++a) {
246 if (_res_cap[a] < delta) continue;
248 switch (heap.state(v)) {
250 heap.push(v, d + _cost[a] - _pi[v]);
254 dn = d + _cost[a] - _pi[v];
256 heap.decrease(v, dn);
260 case Heap::POST_HEAP:
265 if (heap.empty()) return -1;
267 // Update potentials of processed nodes
269 Cost dt = heap.prio();
270 for (int i = 0; i < int(_proc_nodes.size()); ++i) {
271 _pi[_proc_nodes[i]] += _dist[_proc_nodes[i]] - dt;
277 }; //class ResidualDijkstra
281 /// \name Named Template Parameters
284 template <typename T>
285 struct SetHeapTraits : public Traits {
289 /// \brief \ref named-templ-param "Named parameter" for setting
292 /// \ref named-templ-param "Named parameter" for setting \c Heap
293 /// type, which is used for internal Dijkstra computations.
294 /// It must conform to the \ref lemon::concepts::Heap "Heap" concept,
295 /// its priority type must be \c Cost and its cross reference type
296 /// must be \ref RangeMap "RangeMap<int>".
297 template <typename T>
299 : public CapacityScaling<GR, V, C, SetHeapTraits<T> > {
300 typedef CapacityScaling<GR, V, C, SetHeapTraits<T> > Create;
311 /// \brief Constructor.
313 /// The constructor of the class.
315 /// \param graph The digraph the algorithm runs on.
316 CapacityScaling(const GR& graph) :
317 _graph(graph), _node_id(graph), _arc_idf(graph), _arc_idb(graph),
318 INF(std::numeric_limits<Value>::has_infinity ?
319 std::numeric_limits<Value>::infinity() :
320 std::numeric_limits<Value>::max())
322 // Check the number types
323 LEMON_ASSERT(std::numeric_limits<Value>::is_signed,
324 "The flow type of CapacityScaling must be signed");
325 LEMON_ASSERT(std::numeric_limits<Cost>::is_signed,
326 "The cost type of CapacityScaling must be signed");
328 // Reset data structures
333 /// The parameters of the algorithm can be specified using these
338 /// \brief Set the lower bounds on the arcs.
340 /// This function sets the lower bounds on the arcs.
341 /// If it is not used before calling \ref run(), the lower bounds
342 /// will be set to zero on all arcs.
344 /// \param map An arc map storing the lower bounds.
345 /// Its \c Value type must be convertible to the \c Value type
346 /// of the algorithm.
348 /// \return <tt>(*this)</tt>
349 template <typename LowerMap>
350 CapacityScaling& lowerMap(const LowerMap& map) {
352 for (ArcIt a(_graph); a != INVALID; ++a) {
353 _lower[_arc_idf[a]] = map[a];
354 _lower[_arc_idb[a]] = map[a];
359 /// \brief Set the upper bounds (capacities) on the arcs.
361 /// This function sets the upper bounds (capacities) on the arcs.
362 /// If it is not used before calling \ref run(), the upper bounds
363 /// will be set to \ref INF on all arcs (i.e. the flow value will be
364 /// unbounded from above).
366 /// \param map An arc map storing the upper bounds.
367 /// Its \c Value type must be convertible to the \c Value type
368 /// of the algorithm.
370 /// \return <tt>(*this)</tt>
371 template<typename UpperMap>
372 CapacityScaling& upperMap(const UpperMap& map) {
373 for (ArcIt a(_graph); a != INVALID; ++a) {
374 _upper[_arc_idf[a]] = map[a];
379 /// \brief Set the costs of the arcs.
381 /// This function sets the costs of the arcs.
382 /// If it is not used before calling \ref run(), the costs
383 /// will be set to \c 1 on all arcs.
385 /// \param map An arc map storing the costs.
386 /// Its \c Value type must be convertible to the \c Cost type
387 /// of the algorithm.
389 /// \return <tt>(*this)</tt>
390 template<typename CostMap>
391 CapacityScaling& costMap(const CostMap& map) {
392 for (ArcIt a(_graph); a != INVALID; ++a) {
393 _cost[_arc_idf[a]] = map[a];
394 _cost[_arc_idb[a]] = -map[a];
399 /// \brief Set the supply values of the nodes.
401 /// This function sets the supply values of the nodes.
402 /// If neither this function nor \ref stSupply() is used before
403 /// calling \ref run(), the supply of each node will be set to zero.
405 /// \param map A node map storing the supply values.
406 /// Its \c Value type must be convertible to the \c Value type
407 /// of the algorithm.
409 /// \return <tt>(*this)</tt>
410 template<typename SupplyMap>
411 CapacityScaling& supplyMap(const SupplyMap& map) {
412 for (NodeIt n(_graph); n != INVALID; ++n) {
413 _supply[_node_id[n]] = map[n];
418 /// \brief Set single source and target nodes and a supply value.
420 /// This function sets a single source node and a single target node
421 /// and the required flow value.
422 /// If neither this function nor \ref supplyMap() is used before
423 /// calling \ref run(), the supply of each node will be set to zero.
425 /// Using this function has the same effect as using \ref supplyMap()
426 /// with a map in which \c k is assigned to \c s, \c -k is
427 /// assigned to \c t and all other nodes have zero supply value.
429 /// \param s The source node.
430 /// \param t The target node.
431 /// \param k The required amount of flow from node \c s to node \c t
432 /// (i.e. the supply of \c s and the demand of \c t).
434 /// \return <tt>(*this)</tt>
435 CapacityScaling& stSupply(const Node& s, const Node& t, Value k) {
436 for (int i = 0; i != _node_num; ++i) {
439 _supply[_node_id[s]] = k;
440 _supply[_node_id[t]] = -k;
446 /// \name Execution control
447 /// The algorithm can be executed using \ref run().
451 /// \brief Run the algorithm.
453 /// This function runs the algorithm.
454 /// The paramters can be specified using functions \ref lowerMap(),
455 /// \ref upperMap(), \ref costMap(), \ref supplyMap(), \ref stSupply().
458 /// CapacityScaling<ListDigraph> cs(graph);
459 /// cs.lowerMap(lower).upperMap(upper).costMap(cost)
460 /// .supplyMap(sup).run();
463 /// This function can be called more than once. All the given parameters
464 /// are kept for the next call, unless \ref resetParams() or \ref reset()
465 /// is used, thus only the modified parameters have to be set again.
466 /// If the underlying digraph was also modified after the construction
467 /// of the class (or the last \ref reset() call), then the \ref reset()
468 /// function must be called.
470 /// \param factor The capacity scaling factor. It must be larger than
471 /// one to use scaling. If it is less or equal to one, then scaling
472 /// will be disabled.
474 /// \return \c INFEASIBLE if no feasible flow exists,
475 /// \n \c OPTIMAL if the problem has optimal solution
476 /// (i.e. it is feasible and bounded), and the algorithm has found
477 /// optimal flow and node potentials (primal and dual solutions),
478 /// \n \c UNBOUNDED if the digraph contains an arc of negative cost
479 /// and infinite upper bound. It means that the objective function
480 /// is unbounded on that arc, however, note that it could actually be
481 /// bounded over the feasible flows, but this algroithm cannot handle
485 /// \see resetParams(), reset()
486 ProblemType run(int factor = 4) {
488 ProblemType pt = init();
489 if (pt != OPTIMAL) return pt;
493 /// \brief Reset all the parameters that have been given before.
495 /// This function resets all the paramaters that have been given
496 /// before using functions \ref lowerMap(), \ref upperMap(),
497 /// \ref costMap(), \ref supplyMap(), \ref stSupply().
499 /// It is useful for multiple \ref run() calls. Basically, all the given
500 /// parameters are kept for the next \ref run() call, unless
501 /// \ref resetParams() or \ref reset() is used.
502 /// If the underlying digraph was also modified after the construction
503 /// of the class or the last \ref reset() call, then the \ref reset()
504 /// function must be used, otherwise \ref resetParams() is sufficient.
508 /// CapacityScaling<ListDigraph> cs(graph);
511 /// cs.lowerMap(lower).upperMap(upper).costMap(cost)
512 /// .supplyMap(sup).run();
514 /// // Run again with modified cost map (resetParams() is not called,
515 /// // so only the cost map have to be set again)
517 /// cs.costMap(cost).run();
519 /// // Run again from scratch using resetParams()
520 /// // (the lower bounds will be set to zero on all arcs)
521 /// cs.resetParams();
522 /// cs.upperMap(capacity).costMap(cost)
523 /// .supplyMap(sup).run();
526 /// \return <tt>(*this)</tt>
528 /// \see reset(), run()
529 CapacityScaling& resetParams() {
530 for (int i = 0; i != _node_num; ++i) {
533 for (int j = 0; j != _res_arc_num; ++j) {
536 _cost[j] = _forward[j] ? 1 : -1;
542 /// \brief Reset the internal data structures and all the parameters
543 /// that have been given before.
545 /// This function resets the internal data structures and all the
546 /// paramaters that have been given before using functions \ref lowerMap(),
547 /// \ref upperMap(), \ref costMap(), \ref supplyMap(), \ref stSupply().
549 /// It is useful for multiple \ref run() calls. Basically, all the given
550 /// parameters are kept for the next \ref run() call, unless
551 /// \ref resetParams() or \ref reset() is used.
552 /// If the underlying digraph was also modified after the construction
553 /// of the class or the last \ref reset() call, then the \ref reset()
554 /// function must be used, otherwise \ref resetParams() is sufficient.
556 /// See \ref resetParams() for examples.
558 /// \return <tt>(*this)</tt>
560 /// \see resetParams(), run()
561 CapacityScaling& reset() {
563 _node_num = countNodes(_graph);
564 _arc_num = countArcs(_graph);
565 _res_arc_num = 2 * (_arc_num + _node_num);
569 _first_out.resize(_node_num + 1);
570 _forward.resize(_res_arc_num);
571 _source.resize(_res_arc_num);
572 _target.resize(_res_arc_num);
573 _reverse.resize(_res_arc_num);
575 _lower.resize(_res_arc_num);
576 _upper.resize(_res_arc_num);
577 _cost.resize(_res_arc_num);
578 _supply.resize(_node_num);
580 _res_cap.resize(_res_arc_num);
581 _pi.resize(_node_num);
582 _excess.resize(_node_num);
583 _pred.resize(_node_num);
586 int i = 0, j = 0, k = 2 * _arc_num + _node_num - 1;
587 for (NodeIt n(_graph); n != INVALID; ++n, ++i) {
591 for (NodeIt n(_graph); n != INVALID; ++n, ++i) {
593 for (OutArcIt a(_graph, n); a != INVALID; ++a, ++j) {
597 _target[j] = _node_id[_graph.runningNode(a)];
599 for (InArcIt a(_graph, n); a != INVALID; ++a, ++j) {
603 _target[j] = _node_id[_graph.runningNode(a)];
616 _first_out[_node_num] = k;
617 for (ArcIt a(_graph); a != INVALID; ++a) {
618 int fi = _arc_idf[a];
619 int bi = _arc_idb[a];
631 /// \name Query Functions
632 /// The results of the algorithm can be obtained using these
634 /// The \ref run() function must be called before using them.
638 /// \brief Return the total cost of the found flow.
640 /// This function returns the total cost of the found flow.
641 /// Its complexity is O(e).
643 /// \note The return type of the function can be specified as a
644 /// template parameter. For example,
646 /// cs.totalCost<double>();
648 /// It is useful if the total cost cannot be stored in the \c Cost
649 /// type of the algorithm, which is the default return type of the
652 /// \pre \ref run() must be called before using this function.
653 template <typename Number>
654 Number totalCost() const {
656 for (ArcIt a(_graph); a != INVALID; ++a) {
658 c += static_cast<Number>(_res_cap[i]) *
659 (-static_cast<Number>(_cost[i]));
665 Cost totalCost() const {
666 return totalCost<Cost>();
670 /// \brief Return the flow on the given arc.
672 /// This function returns the flow on the given arc.
674 /// \pre \ref run() must be called before using this function.
675 Value flow(const Arc& a) const {
676 return _res_cap[_arc_idb[a]];
679 /// \brief Return the flow map (the primal solution).
681 /// This function copies the flow value on each arc into the given
682 /// map. The \c Value type of the algorithm must be convertible to
683 /// the \c Value type of the map.
685 /// \pre \ref run() must be called before using this function.
686 template <typename FlowMap>
687 void flowMap(FlowMap &map) const {
688 for (ArcIt a(_graph); a != INVALID; ++a) {
689 map.set(a, _res_cap[_arc_idb[a]]);
693 /// \brief Return the potential (dual value) of the given node.
695 /// This function returns the potential (dual value) of the
698 /// \pre \ref run() must be called before using this function.
699 Cost potential(const Node& n) const {
700 return _pi[_node_id[n]];
703 /// \brief Return the potential map (the dual solution).
705 /// This function copies the potential (dual value) of each node
706 /// into the given map.
707 /// The \c Cost type of the algorithm must be convertible to the
708 /// \c Value type of the map.
710 /// \pre \ref run() must be called before using this function.
711 template <typename PotentialMap>
712 void potentialMap(PotentialMap &map) const {
713 for (NodeIt n(_graph); n != INVALID; ++n) {
714 map.set(n, _pi[_node_id[n]]);
722 // Initialize the algorithm
724 if (_node_num <= 1) return INFEASIBLE;
726 // Check the sum of supply values
728 for (int i = 0; i != _root; ++i) {
729 _sum_supply += _supply[i];
731 if (_sum_supply > 0) return INFEASIBLE;
733 // Initialize vectors
734 for (int i = 0; i != _root; ++i) {
736 _excess[i] = _supply[i];
739 // Remove non-zero lower bounds
740 const Value MAX = std::numeric_limits<Value>::max();
743 for (int i = 0; i != _root; ++i) {
744 last_out = _first_out[i+1];
745 for (int j = _first_out[i]; j != last_out; ++j) {
749 _res_cap[j] = _upper[j] < MAX ? _upper[j] - c : INF;
751 _res_cap[j] = _upper[j] < MAX + c ? _upper[j] - c : INF;
754 _excess[_target[j]] += c;
761 for (int j = 0; j != _res_arc_num; ++j) {
762 _res_cap[j] = _forward[j] ? _upper[j] : 0;
766 // Handle negative costs
767 for (int i = 0; i != _root; ++i) {
768 last_out = _first_out[i+1] - 1;
769 for (int j = _first_out[i]; j != last_out; ++j) {
770 Value rc = _res_cap[j];
771 if (_cost[j] < 0 && rc > 0) {
772 if (rc >= MAX) return UNBOUNDED;
774 _excess[_target[j]] += rc;
776 _res_cap[_reverse[j]] += rc;
781 // Handle GEQ supply type
782 if (_sum_supply < 0) {
784 _excess[_root] = -_sum_supply;
785 for (int a = _first_out[_root]; a != _res_arc_num; ++a) {
786 int ra = _reverse[a];
787 _res_cap[a] = -_sum_supply + 1;
795 for (int a = _first_out[_root]; a != _res_arc_num; ++a) {
796 int ra = _reverse[a];
804 // Initialize delta value
807 Value max_sup = 0, max_dem = 0, max_cap = 0;
808 for (int i = 0; i != _root; ++i) {
809 Value ex = _excess[i];
810 if ( ex > max_sup) max_sup = ex;
811 if (-ex > max_dem) max_dem = -ex;
812 int last_out = _first_out[i+1] - 1;
813 for (int j = _first_out[i]; j != last_out; ++j) {
814 if (_res_cap[j] > max_cap) max_cap = _res_cap[j];
817 max_sup = std::min(std::min(max_sup, max_dem), max_cap);
818 for (_delta = 1; 2 * _delta <= max_sup; _delta *= 2) ;
827 ProblemType start() {
828 // Execute the algorithm
831 pt = startWithScaling();
833 pt = startWithoutScaling();
835 // Handle non-zero lower bounds
837 int limit = _first_out[_root];
838 for (int j = 0; j != limit; ++j) {
839 if (!_forward[j]) _res_cap[j] += _lower[j];
843 // Shift potentials if necessary
844 Cost pr = _pi[_root];
845 if (_sum_supply < 0 || pr > 0) {
846 for (int i = 0; i != _node_num; ++i) {
854 // Execute the capacity scaling algorithm
855 ProblemType startWithScaling() {
856 // Perform capacity scaling phases
858 ResidualDijkstra _dijkstra(*this);
860 // Saturate all arcs not satisfying the optimality condition
862 for (int u = 0; u != _node_num; ++u) {
863 last_out = _sum_supply < 0 ?
864 _first_out[u+1] : _first_out[u+1] - 1;
865 for (int a = _first_out[u]; a != last_out; ++a) {
867 Cost c = _cost[a] + _pi[u] - _pi[v];
868 Value rc = _res_cap[a];
869 if (c < 0 && rc >= _delta) {
873 _res_cap[_reverse[a]] += rc;
878 // Find excess nodes and deficit nodes
879 _excess_nodes.clear();
880 _deficit_nodes.clear();
881 for (int u = 0; u != _node_num; ++u) {
882 Value ex = _excess[u];
883 if (ex >= _delta) _excess_nodes.push_back(u);
884 if (ex <= -_delta) _deficit_nodes.push_back(u);
886 int next_node = 0, next_def_node = 0;
888 // Find augmenting shortest paths
889 while (next_node < int(_excess_nodes.size())) {
890 // Check deficit nodes
892 bool delta_deficit = false;
893 for ( ; next_def_node < int(_deficit_nodes.size());
895 if (_excess[_deficit_nodes[next_def_node]] <= -_delta) {
896 delta_deficit = true;
900 if (!delta_deficit) break;
903 // Run Dijkstra in the residual network
904 s = _excess_nodes[next_node];
905 if ((t = _dijkstra.run(s, _delta)) == -1) {
913 // Augment along a shortest path from s to t
914 Value d = std::min(_excess[s], -_excess[t]);
918 while ((a = _pred[u]) != -1) {
919 if (_res_cap[a] < d) d = _res_cap[a];
924 while ((a = _pred[u]) != -1) {
926 _res_cap[_reverse[a]] += d;
932 if (_excess[s] < _delta) ++next_node;
935 if (_delta == 1) break;
936 _delta = _delta <= _factor ? 1 : _delta / _factor;
942 // Execute the successive shortest path algorithm
943 ProblemType startWithoutScaling() {
945 _excess_nodes.clear();
946 for (int i = 0; i != _node_num; ++i) {
947 if (_excess[i] > 0) _excess_nodes.push_back(i);
949 if (_excess_nodes.size() == 0) return OPTIMAL;
952 // Find shortest paths
954 ResidualDijkstra _dijkstra(*this);
955 while ( _excess[_excess_nodes[next_node]] > 0 ||
956 ++next_node < int(_excess_nodes.size()) )
958 // Run Dijkstra in the residual network
959 s = _excess_nodes[next_node];
960 if ((t = _dijkstra.run(s)) == -1) return INFEASIBLE;
962 // Augment along a shortest path from s to t
963 Value d = std::min(_excess[s], -_excess[t]);
967 while ((a = _pred[u]) != -1) {
968 if (_res_cap[a] < d) d = _res_cap[a];
973 while ((a = _pred[u]) != -1) {
975 _res_cap[_reverse[a]] += d;
985 }; //class CapacityScaling
991 #endif //LEMON_CAPACITY_SCALING_H