1 /* -*- mode: C++; indent-tabs-mode: nil; -*-
3 * This file is a part of LEMON, a generic C++ optimization library.
5 * Copyright (C) 2003-2009
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_NETWORK_SIMPLEX_H
20 #define LEMON_NETWORK_SIMPLEX_H
22 /// \ingroup min_cost_flow
25 /// \brief Network simplex algorithm for finding a minimum cost flow.
31 #include <lemon/core.h>
32 #include <lemon/math.h>
36 /// \addtogroup min_cost_flow
39 /// \brief Implementation of the primal network simplex algorithm
40 /// for finding a \ref min_cost_flow "minimum cost flow".
42 /// \ref NetworkSimplex implements the primal network simplex algorithm
43 /// for finding a \ref min_cost_flow "minimum cost flow".
45 /// \tparam Digraph The digraph type the algorithm runs on.
46 /// \tparam LowerMap The type of the lower bound map.
47 /// \tparam CapacityMap The type of the capacity (upper bound) map.
48 /// \tparam CostMap The type of the cost (length) map.
49 /// \tparam SupplyMap The type of the supply map.
52 /// - Arc capacities and costs should be \e non-negative \e integers.
53 /// - Supply values should be \e signed \e integers.
54 /// - The value types of the maps should be convertible to each other.
55 /// - \c CostMap::Value must be signed type.
57 /// \note \ref NetworkSimplex provides five different pivot rule
58 /// implementations that significantly affect the efficiency of the
60 /// By default "Block Search" pivot rule is used, which proved to be
61 /// by far the most efficient according to our benchmark tests.
62 /// However another pivot rule can be selected using \ref run()
63 /// function with the proper parameter.
65 template < typename Digraph,
72 template < typename Digraph,
73 typename LowerMap = typename Digraph::template ArcMap<int>,
74 typename CapacityMap = typename Digraph::template ArcMap<int>,
75 typename CostMap = typename Digraph::template ArcMap<int>,
76 typename SupplyMap = typename Digraph::template NodeMap<int> >
80 TEMPLATE_DIGRAPH_TYPEDEFS(Digraph);
82 typedef typename CapacityMap::Value Capacity;
83 typedef typename CostMap::Value Cost;
84 typedef typename SupplyMap::Value Supply;
86 typedef std::vector<Arc> ArcVector;
87 typedef std::vector<Node> NodeVector;
88 typedef std::vector<int> IntVector;
89 typedef std::vector<bool> BoolVector;
90 typedef std::vector<Capacity> CapacityVector;
91 typedef std::vector<Cost> CostVector;
92 typedef std::vector<Supply> SupplyVector;
96 /// The type of the flow map
97 typedef typename Digraph::template ArcMap<Capacity> FlowMap;
98 /// The type of the potential map
99 typedef typename Digraph::template NodeMap<Cost> PotentialMap;
103 /// Enum type for selecting the pivot rule used by \ref run()
105 FIRST_ELIGIBLE_PIVOT,
108 CANDIDATE_LIST_PIVOT,
114 // State constants for arcs
123 // References for the original data
124 const Digraph &_graph;
125 const LowerMap *_orig_lower;
126 const CapacityMap &_orig_cap;
127 const CostMap &_orig_cost;
128 const SupplyMap *_orig_supply;
131 Capacity _orig_flow_value;
135 PotentialMap *_potential_map;
137 bool _local_potential;
139 // The number of nodes and arcs in the original graph
143 // Data structures for storing the graph
153 CapacityVector _flow;
156 // Data for storing the spanning tree structure
160 IntVector _rev_thread;
162 IntVector _last_succ;
163 IntVector _dirty_revs;
168 // Temporary data used in the current pivot iteration
169 int in_arc, join, u_in, v_in, u_out, v_out;
170 int first, second, right, last;
171 int stem, par_stem, new_stem;
176 /// \brief Implementation of the "First Eligible" pivot rule for the
177 /// \ref NetworkSimplex "network simplex" algorithm.
179 /// This class implements the "First Eligible" pivot rule
180 /// for the \ref NetworkSimplex "network simplex" algorithm.
182 /// For more information see \ref NetworkSimplex::run().
183 class FirstEligiblePivotRule
187 // References to the NetworkSimplex class
188 const IntVector &_source;
189 const IntVector &_target;
190 const CostVector &_cost;
191 const IntVector &_state;
192 const CostVector &_pi;
202 FirstEligiblePivotRule(NetworkSimplex &ns) :
203 _source(ns._source), _target(ns._target),
204 _cost(ns._cost), _state(ns._state), _pi(ns._pi),
205 _in_arc(ns.in_arc), _arc_num(ns._arc_num), _next_arc(0)
208 /// Find next entering arc
209 bool findEnteringArc() {
211 for (int e = _next_arc; e < _arc_num; ++e) {
212 c = _state[e] * (_cost[e] + _pi[_source[e]] - _pi[_target[e]]);
219 for (int e = 0; e < _next_arc; ++e) {
220 c = _state[e] * (_cost[e] + _pi[_source[e]] - _pi[_target[e]]);
230 }; //class FirstEligiblePivotRule
233 /// \brief Implementation of the "Best Eligible" pivot rule for the
234 /// \ref NetworkSimplex "network simplex" algorithm.
236 /// This class implements the "Best Eligible" pivot rule
237 /// for the \ref NetworkSimplex "network simplex" algorithm.
239 /// For more information see \ref NetworkSimplex::run().
240 class BestEligiblePivotRule
244 // References to the NetworkSimplex class
245 const IntVector &_source;
246 const IntVector &_target;
247 const CostVector &_cost;
248 const IntVector &_state;
249 const CostVector &_pi;
256 BestEligiblePivotRule(NetworkSimplex &ns) :
257 _source(ns._source), _target(ns._target),
258 _cost(ns._cost), _state(ns._state), _pi(ns._pi),
259 _in_arc(ns.in_arc), _arc_num(ns._arc_num)
262 /// Find next entering arc
263 bool findEnteringArc() {
265 for (int e = 0; e < _arc_num; ++e) {
266 c = _state[e] * (_cost[e] + _pi[_source[e]] - _pi[_target[e]]);
275 }; //class BestEligiblePivotRule
278 /// \brief Implementation of the "Block Search" pivot rule for the
279 /// \ref NetworkSimplex "network simplex" algorithm.
281 /// This class implements the "Block Search" pivot rule
282 /// for the \ref NetworkSimplex "network simplex" algorithm.
284 /// For more information see \ref NetworkSimplex::run().
285 class BlockSearchPivotRule
289 // References to the NetworkSimplex class
290 const IntVector &_source;
291 const IntVector &_target;
292 const CostVector &_cost;
293 const IntVector &_state;
294 const CostVector &_pi;
305 BlockSearchPivotRule(NetworkSimplex &ns) :
306 _source(ns._source), _target(ns._target),
307 _cost(ns._cost), _state(ns._state), _pi(ns._pi),
308 _in_arc(ns.in_arc), _arc_num(ns._arc_num), _next_arc(0)
310 // The main parameters of the pivot rule
311 const double BLOCK_SIZE_FACTOR = 2.0;
312 const int MIN_BLOCK_SIZE = 10;
314 _block_size = std::max( int(BLOCK_SIZE_FACTOR * sqrt(_arc_num)),
318 /// Find next entering arc
319 bool findEnteringArc() {
321 int cnt = _block_size;
322 int e, min_arc = _next_arc;
323 for (e = _next_arc; e < _arc_num; ++e) {
324 c = _state[e] * (_cost[e] + _pi[_source[e]] - _pi[_target[e]]);
334 if (min == 0 || cnt > 0) {
335 for (e = 0; e < _next_arc; ++e) {
336 c = _state[e] * (_cost[e] + _pi[_source[e]] - _pi[_target[e]]);
347 if (min >= 0) return false;
353 }; //class BlockSearchPivotRule
356 /// \brief Implementation of the "Candidate List" pivot rule for the
357 /// \ref NetworkSimplex "network simplex" algorithm.
359 /// This class implements the "Candidate List" pivot rule
360 /// for the \ref NetworkSimplex "network simplex" algorithm.
362 /// For more information see \ref NetworkSimplex::run().
363 class CandidateListPivotRule
367 // References to the NetworkSimplex class
368 const IntVector &_source;
369 const IntVector &_target;
370 const CostVector &_cost;
371 const IntVector &_state;
372 const CostVector &_pi;
377 IntVector _candidates;
378 int _list_length, _minor_limit;
379 int _curr_length, _minor_count;
385 CandidateListPivotRule(NetworkSimplex &ns) :
386 _source(ns._source), _target(ns._target),
387 _cost(ns._cost), _state(ns._state), _pi(ns._pi),
388 _in_arc(ns.in_arc), _arc_num(ns._arc_num), _next_arc(0)
390 // The main parameters of the pivot rule
391 const double LIST_LENGTH_FACTOR = 1.0;
392 const int MIN_LIST_LENGTH = 10;
393 const double MINOR_LIMIT_FACTOR = 0.1;
394 const int MIN_MINOR_LIMIT = 3;
396 _list_length = std::max( int(LIST_LENGTH_FACTOR * sqrt(_arc_num)),
398 _minor_limit = std::max( int(MINOR_LIMIT_FACTOR * _list_length),
400 _curr_length = _minor_count = 0;
401 _candidates.resize(_list_length);
404 /// Find next entering arc
405 bool findEnteringArc() {
407 int e, min_arc = _next_arc;
408 if (_curr_length > 0 && _minor_count < _minor_limit) {
409 // Minor iteration: select the best eligible arc from the
410 // current candidate list
413 for (int i = 0; i < _curr_length; ++i) {
415 c = _state[e] * (_cost[e] + _pi[_source[e]] - _pi[_target[e]]);
421 _candidates[i--] = _candidates[--_curr_length];
430 // Major iteration: build a new candidate list
433 for (e = _next_arc; e < _arc_num; ++e) {
434 c = _state[e] * (_cost[e] + _pi[_source[e]] - _pi[_target[e]]);
436 _candidates[_curr_length++] = e;
441 if (_curr_length == _list_length) break;
444 if (_curr_length < _list_length) {
445 for (e = 0; e < _next_arc; ++e) {
446 c = _state[e] * (_cost[e] + _pi[_source[e]] - _pi[_target[e]]);
448 _candidates[_curr_length++] = e;
453 if (_curr_length == _list_length) break;
457 if (_curr_length == 0) return false;
464 }; //class CandidateListPivotRule
467 /// \brief Implementation of the "Altering Candidate List" pivot rule
468 /// for the \ref NetworkSimplex "network simplex" algorithm.
470 /// This class implements the "Altering Candidate List" pivot rule
471 /// for the \ref NetworkSimplex "network simplex" algorithm.
473 /// For more information see \ref NetworkSimplex::run().
474 class AlteringListPivotRule
478 // References to the NetworkSimplex class
479 const IntVector &_source;
480 const IntVector &_target;
481 const CostVector &_cost;
482 const IntVector &_state;
483 const CostVector &_pi;
488 int _block_size, _head_length, _curr_length;
490 IntVector _candidates;
491 CostVector _cand_cost;
493 // Functor class to compare arcs during sort of the candidate list
497 const CostVector &_map;
499 SortFunc(const CostVector &map) : _map(map) {}
500 bool operator()(int left, int right) {
501 return _map[left] > _map[right];
510 AlteringListPivotRule(NetworkSimplex &ns) :
511 _source(ns._source), _target(ns._target),
512 _cost(ns._cost), _state(ns._state), _pi(ns._pi),
513 _in_arc(ns.in_arc), _arc_num(ns._arc_num),
514 _next_arc(0), _cand_cost(ns._arc_num), _sort_func(_cand_cost)
516 // The main parameters of the pivot rule
517 const double BLOCK_SIZE_FACTOR = 1.5;
518 const int MIN_BLOCK_SIZE = 10;
519 const double HEAD_LENGTH_FACTOR = 0.1;
520 const int MIN_HEAD_LENGTH = 3;
522 _block_size = std::max( int(BLOCK_SIZE_FACTOR * sqrt(_arc_num)),
524 _head_length = std::max( int(HEAD_LENGTH_FACTOR * _block_size),
526 _candidates.resize(_head_length + _block_size);
530 /// Find next entering arc
531 bool findEnteringArc() {
532 // Check the current candidate list
534 for (int i = 0; i < _curr_length; ++i) {
536 _cand_cost[e] = _state[e] *
537 (_cost[e] + _pi[_source[e]] - _pi[_target[e]]);
538 if (_cand_cost[e] >= 0) {
539 _candidates[i--] = _candidates[--_curr_length];
544 int cnt = _block_size;
546 int limit = _head_length;
548 for (int e = _next_arc; e < _arc_num; ++e) {
549 _cand_cost[e] = _state[e] *
550 (_cost[e] + _pi[_source[e]] - _pi[_target[e]]);
551 if (_cand_cost[e] < 0) {
552 _candidates[_curr_length++] = e;
556 if (_curr_length > limit) break;
561 if (_curr_length <= limit) {
562 for (int e = 0; e < _next_arc; ++e) {
563 _cand_cost[e] = _state[e] *
564 (_cost[e] + _pi[_source[e]] - _pi[_target[e]]);
565 if (_cand_cost[e] < 0) {
566 _candidates[_curr_length++] = e;
570 if (_curr_length > limit) break;
576 if (_curr_length == 0) return false;
577 _next_arc = last_arc + 1;
579 // Make heap of the candidate list (approximating a partial sort)
580 make_heap( _candidates.begin(), _candidates.begin() + _curr_length,
583 // Pop the first element of the heap
584 _in_arc = _candidates[0];
585 pop_heap( _candidates.begin(), _candidates.begin() + _curr_length,
587 _curr_length = std::min(_head_length, _curr_length - 1);
591 }; //class AlteringListPivotRule
595 /// \brief General constructor (with lower bounds).
597 /// General constructor (with lower bounds).
599 /// \param graph The digraph the algorithm runs on.
600 /// \param lower The lower bounds of the arcs.
601 /// \param capacity The capacities (upper bounds) of the arcs.
602 /// \param cost The cost (length) values of the arcs.
603 /// \param supply The supply values of the nodes (signed).
604 NetworkSimplex( const Digraph &graph,
605 const LowerMap &lower,
606 const CapacityMap &capacity,
608 const SupplyMap &supply ) :
609 _graph(graph), _orig_lower(&lower), _orig_cap(capacity),
610 _orig_cost(cost), _orig_supply(&supply),
611 _flow_map(NULL), _potential_map(NULL),
612 _local_flow(false), _local_potential(false),
616 /// \brief General constructor (without lower bounds).
618 /// General constructor (without lower bounds).
620 /// \param graph The digraph the algorithm runs on.
621 /// \param capacity The capacities (upper bounds) of the arcs.
622 /// \param cost The cost (length) values of the arcs.
623 /// \param supply The supply values of the nodes (signed).
624 NetworkSimplex( const Digraph &graph,
625 const CapacityMap &capacity,
627 const SupplyMap &supply ) :
628 _graph(graph), _orig_lower(NULL), _orig_cap(capacity),
629 _orig_cost(cost), _orig_supply(&supply),
630 _flow_map(NULL), _potential_map(NULL),
631 _local_flow(false), _local_potential(false),
635 /// \brief Simple constructor (with lower bounds).
637 /// Simple constructor (with lower bounds).
639 /// \param graph The digraph the algorithm runs on.
640 /// \param lower The lower bounds of the arcs.
641 /// \param capacity The capacities (upper bounds) of the arcs.
642 /// \param cost The cost (length) values of the arcs.
643 /// \param s The source node.
644 /// \param t The target node.
645 /// \param flow_value The required amount of flow from node \c s
646 /// to node \c t (i.e. the supply of \c s and the demand of \c t).
647 NetworkSimplex( const Digraph &graph,
648 const LowerMap &lower,
649 const CapacityMap &capacity,
652 Capacity flow_value ) :
653 _graph(graph), _orig_lower(&lower), _orig_cap(capacity),
654 _orig_cost(cost), _orig_supply(NULL),
655 _orig_source(s), _orig_target(t), _orig_flow_value(flow_value),
656 _flow_map(NULL), _potential_map(NULL),
657 _local_flow(false), _local_potential(false),
661 /// \brief Simple constructor (without lower bounds).
663 /// Simple constructor (without lower bounds).
665 /// \param graph The digraph the algorithm runs on.
666 /// \param capacity The capacities (upper bounds) of the arcs.
667 /// \param cost The cost (length) values of the arcs.
668 /// \param s The source node.
669 /// \param t The target node.
670 /// \param flow_value The required amount of flow from node \c s
671 /// to node \c t (i.e. the supply of \c s and the demand of \c t).
672 NetworkSimplex( const Digraph &graph,
673 const CapacityMap &capacity,
676 Capacity flow_value ) :
677 _graph(graph), _orig_lower(NULL), _orig_cap(capacity),
678 _orig_cost(cost), _orig_supply(NULL),
679 _orig_source(s), _orig_target(t), _orig_flow_value(flow_value),
680 _flow_map(NULL), _potential_map(NULL),
681 _local_flow(false), _local_potential(false),
687 if (_local_flow) delete _flow_map;
688 if (_local_potential) delete _potential_map;
691 /// \brief Set the flow map.
693 /// This function sets the flow map.
695 /// \return <tt>(*this)</tt>
696 NetworkSimplex& flowMap(FlowMap &map) {
705 /// \brief Set the potential map.
707 /// This function sets the potential map.
709 /// \return <tt>(*this)</tt>
710 NetworkSimplex& potentialMap(PotentialMap &map) {
711 if (_local_potential) {
712 delete _potential_map;
713 _local_potential = false;
715 _potential_map = ↦
719 /// \name Execution control
720 /// The algorithm can be executed using the
721 /// \ref lemon::NetworkSimplex::run() "run()" function.
724 /// \brief Run the algorithm.
726 /// This function runs the algorithm.
728 /// \param pivot_rule The pivot rule that is used during the
731 /// The available pivot rules:
733 /// - FIRST_ELIGIBLE_PIVOT The next eligible arc is selected in
734 /// a wraparound fashion in every iteration
735 /// (\ref FirstEligiblePivotRule).
737 /// - BEST_ELIGIBLE_PIVOT The best eligible arc is selected in
738 /// every iteration (\ref BestEligiblePivotRule).
740 /// - BLOCK_SEARCH_PIVOT A specified number of arcs are examined in
741 /// every iteration in a wraparound fashion and the best eligible
742 /// arc is selected from this block (\ref BlockSearchPivotRule).
744 /// - CANDIDATE_LIST_PIVOT In a major iteration a candidate list is
745 /// built from eligible arcs in a wraparound fashion and in the
746 /// following minor iterations the best eligible arc is selected
747 /// from this list (\ref CandidateListPivotRule).
749 /// - ALTERING_LIST_PIVOT It is a modified version of the
750 /// "Candidate List" pivot rule. It keeps only the several best
751 /// eligible arcs from the former candidate list and extends this
752 /// list in every iteration (\ref AlteringListPivotRule).
754 /// According to our comprehensive benchmark tests the "Block Search"
755 /// pivot rule proved to be the fastest and the most robust on
756 /// various test inputs. Thus it is the default option.
758 /// \return \c true if a feasible flow can be found.
759 bool run(PivotRuleEnum pivot_rule = BLOCK_SEARCH_PIVOT) {
760 return init() && start(pivot_rule);
765 /// \name Query Functions
766 /// The results of the algorithm can be obtained using these
768 /// \ref lemon::NetworkSimplex::run() "run()" must be called before
772 /// \brief Return a const reference to the flow map.
774 /// This function returns a const reference to an arc map storing
777 /// \pre \ref run() must be called before using this function.
778 const FlowMap& flowMap() const {
782 /// \brief Return a const reference to the potential map
783 /// (the dual solution).
785 /// This function returns a const reference to a node map storing
786 /// the found potentials (the dual solution).
788 /// \pre \ref run() must be called before using this function.
789 const PotentialMap& potentialMap() const {
790 return *_potential_map;
793 /// \brief Return the flow on the given arc.
795 /// This function returns the flow on the given arc.
797 /// \pre \ref run() must be called before using this function.
798 Capacity flow(const Arc& arc) const {
799 return (*_flow_map)[arc];
802 /// \brief Return the potential of the given node.
804 /// This function returns the potential of the given node.
806 /// \pre \ref run() must be called before using this function.
807 Cost potential(const Node& node) const {
808 return (*_potential_map)[node];
811 /// \brief Return the total cost of the found flow.
813 /// This function returns the total cost of the found flow.
814 /// The complexity of the function is \f$ O(e) \f$.
816 /// \pre \ref run() must be called before using this function.
817 Cost totalCost() const {
819 for (ArcIt e(_graph); e != INVALID; ++e)
820 c += (*_flow_map)[e] * _orig_cost[e];
828 // Initialize internal data structures
830 // Initialize result maps
832 _flow_map = new FlowMap(_graph);
835 if (!_potential_map) {
836 _potential_map = new PotentialMap(_graph);
837 _local_potential = true;
840 // Initialize vectors
841 _node_num = countNodes(_graph);
842 _arc_num = countArcs(_graph);
843 int all_node_num = _node_num + 1;
844 int all_arc_num = _arc_num + _node_num;
846 _arc_ref.resize(_arc_num);
847 _source.resize(all_arc_num);
848 _target.resize(all_arc_num);
850 _cap.resize(all_arc_num);
851 _cost.resize(all_arc_num);
852 _supply.resize(all_node_num);
853 _flow.resize(all_arc_num, 0);
854 _pi.resize(all_node_num, 0);
856 _parent.resize(all_node_num);
857 _pred.resize(all_node_num);
858 _forward.resize(all_node_num);
859 _thread.resize(all_node_num);
860 _rev_thread.resize(all_node_num);
861 _succ_num.resize(all_node_num);
862 _last_succ.resize(all_node_num);
863 _state.resize(all_arc_num, STATE_LOWER);
865 // Initialize node related data
866 bool valid_supply = true;
870 for (NodeIt n(_graph); n != INVALID; ++n, ++i) {
872 _supply[i] = (*_orig_supply)[n];
875 valid_supply = (sum == 0);
878 for (NodeIt n(_graph); n != INVALID; ++n, ++i) {
882 _supply[_node_id[_orig_source]] = _orig_flow_value;
883 _supply[_node_id[_orig_target]] = -_orig_flow_value;
885 if (!valid_supply) return false;
887 // Set data for the artificial root node
892 _rev_thread[0] = _root;
893 _succ_num[_root] = all_node_num;
894 _last_succ[_root] = _root - 1;
898 // Store the arcs in a mixed order
899 int k = std::max(int(sqrt(_arc_num)), 10);
901 for (ArcIt e(_graph); e != INVALID; ++e) {
903 if ((i += k) >= _arc_num) i = (i % k) + 1;
906 // Initialize arc maps
907 for (int i = 0; i != _arc_num; ++i) {
909 _source[i] = _node_id[_graph.source(e)];
910 _target[i] = _node_id[_graph.target(e)];
911 _cost[i] = _orig_cost[e];
912 _cap[i] = _orig_cap[e];
915 // Remove non-zero lower bounds
917 for (int i = 0; i != _arc_num; ++i) {
918 Capacity c = (*_orig_lower)[_arc_ref[i]];
921 _supply[_source[i]] -= c;
922 _supply[_target[i]] += c;
927 // Add artificial arcs and initialize the spanning tree data structure
928 Cost max_cost = std::numeric_limits<Cost>::max() / 4;
929 Capacity max_cap = std::numeric_limits<Capacity>::max();
930 for (int u = 0, e = _arc_num; u != _node_num; ++u, ++e) {
932 _rev_thread[u + 1] = u;
937 if (_supply[u] >= 0) {
938 _flow[e] = _supply[u];
942 _flow[e] = -_supply[u];
948 _state[e] = STATE_TREE;
954 // Find the join node
955 void findJoinNode() {
956 int u = _source[in_arc];
957 int v = _target[in_arc];
959 if (_succ_num[u] < _succ_num[v]) {
968 // Find the leaving arc of the cycle and returns true if the
969 // leaving arc is not the same as the entering arc
970 bool findLeavingArc() {
971 // Initialize first and second nodes according to the direction
973 if (_state[in_arc] == STATE_LOWER) {
974 first = _source[in_arc];
975 second = _target[in_arc];
977 first = _target[in_arc];
978 second = _source[in_arc];
980 delta = _cap[in_arc];
985 // Search the cycle along the path form the first node to the root
986 for (int u = first; u != join; u = _parent[u]) {
988 d = _forward[u] ? _flow[e] : _cap[e] - _flow[e];
995 // Search the cycle along the path form the second node to the root
996 for (int u = second; u != join; u = _parent[u]) {
998 d = _forward[u] ? _cap[e] - _flow[e] : _flow[e];
1016 // Change _flow and _state vectors
1017 void changeFlow(bool change) {
1018 // Augment along the cycle
1020 Capacity val = _state[in_arc] * delta;
1021 _flow[in_arc] += val;
1022 for (int u = _source[in_arc]; u != join; u = _parent[u]) {
1023 _flow[_pred[u]] += _forward[u] ? -val : val;
1025 for (int u = _target[in_arc]; u != join; u = _parent[u]) {
1026 _flow[_pred[u]] += _forward[u] ? val : -val;
1029 // Update the state of the entering and leaving arcs
1031 _state[in_arc] = STATE_TREE;
1032 _state[_pred[u_out]] =
1033 (_flow[_pred[u_out]] == 0) ? STATE_LOWER : STATE_UPPER;
1035 _state[in_arc] = -_state[in_arc];
1039 // Update the tree structure
1040 void updateTreeStructure() {
1042 int old_rev_thread = _rev_thread[u_out];
1043 int old_succ_num = _succ_num[u_out];
1044 int old_last_succ = _last_succ[u_out];
1045 v_out = _parent[u_out];
1047 u = _last_succ[u_in]; // the last successor of u_in
1048 right = _thread[u]; // the node after it
1050 // Handle the case when old_rev_thread equals to v_in
1051 // (it also means that join and v_out coincide)
1052 if (old_rev_thread == v_in) {
1053 last = _thread[_last_succ[u_out]];
1055 last = _thread[v_in];
1058 // Update _thread and _parent along the stem nodes (i.e. the nodes
1059 // between u_in and u_out, whose parent have to be changed)
1060 _thread[v_in] = stem = u_in;
1061 _dirty_revs.clear();
1062 _dirty_revs.push_back(v_in);
1064 while (stem != u_out) {
1065 // Insert the next stem node into the thread list
1066 new_stem = _parent[stem];
1067 _thread[u] = new_stem;
1068 _dirty_revs.push_back(u);
1070 // Remove the subtree of stem from the thread list
1071 w = _rev_thread[stem];
1073 _rev_thread[right] = w;
1075 // Change the parent node and shift stem nodes
1076 _parent[stem] = par_stem;
1080 // Update u and right
1081 u = _last_succ[stem] == _last_succ[par_stem] ?
1082 _rev_thread[par_stem] : _last_succ[stem];
1085 _parent[u_out] = par_stem;
1087 _rev_thread[last] = u;
1088 _last_succ[u_out] = u;
1090 // Remove the subtree of u_out from the thread list except for
1091 // the case when old_rev_thread equals to v_in
1092 // (it also means that join and v_out coincide)
1093 if (old_rev_thread != v_in) {
1094 _thread[old_rev_thread] = right;
1095 _rev_thread[right] = old_rev_thread;
1098 // Update _rev_thread using the new _thread values
1099 for (int i = 0; i < int(_dirty_revs.size()); ++i) {
1101 _rev_thread[_thread[u]] = u;
1104 // Update _pred, _forward, _last_succ and _succ_num for the
1105 // stem nodes from u_out to u_in
1106 int tmp_sc = 0, tmp_ls = _last_succ[u_out];
1110 _pred[u] = _pred[w];
1111 _forward[u] = !_forward[w];
1112 tmp_sc += _succ_num[u] - _succ_num[w];
1113 _succ_num[u] = tmp_sc;
1114 _last_succ[w] = tmp_ls;
1117 _pred[u_in] = in_arc;
1118 _forward[u_in] = (u_in == _source[in_arc]);
1119 _succ_num[u_in] = old_succ_num;
1121 // Set limits for updating _last_succ form v_in and v_out
1123 int up_limit_in = -1;
1124 int up_limit_out = -1;
1125 if (_last_succ[join] == v_in) {
1126 up_limit_out = join;
1131 // Update _last_succ from v_in towards the root
1132 for (u = v_in; u != up_limit_in && _last_succ[u] == v_in;
1134 _last_succ[u] = _last_succ[u_out];
1136 // Update _last_succ from v_out towards the root
1137 if (join != old_rev_thread && v_in != old_rev_thread) {
1138 for (u = v_out; u != up_limit_out && _last_succ[u] == old_last_succ;
1140 _last_succ[u] = old_rev_thread;
1143 for (u = v_out; u != up_limit_out && _last_succ[u] == old_last_succ;
1145 _last_succ[u] = _last_succ[u_out];
1149 // Update _succ_num from v_in to join
1150 for (u = v_in; u != join; u = _parent[u]) {
1151 _succ_num[u] += old_succ_num;
1153 // Update _succ_num from v_out to join
1154 for (u = v_out; u != join; u = _parent[u]) {
1155 _succ_num[u] -= old_succ_num;
1159 // Update potentials
1160 void updatePotential() {
1161 Cost sigma = _forward[u_in] ?
1162 _pi[v_in] - _pi[u_in] - _cost[_pred[u_in]] :
1163 _pi[v_in] - _pi[u_in] + _cost[_pred[u_in]];
1164 if (_succ_num[u_in] > _node_num / 2) {
1165 // Update in the upper subtree (which contains the root)
1166 int before = _rev_thread[u_in];
1167 int after = _thread[_last_succ[u_in]];
1168 _thread[before] = after;
1169 _pi[_root] -= sigma;
1170 for (int u = _thread[_root]; u != _root; u = _thread[u]) {
1173 _thread[before] = u_in;
1175 // Update in the lower subtree (which has been moved)
1176 int end = _thread[_last_succ[u_in]];
1177 for (int u = u_in; u != end; u = _thread[u]) {
1183 // Execute the algorithm
1184 bool start(PivotRuleEnum pivot_rule) {
1185 // Select the pivot rule implementation
1186 switch (pivot_rule) {
1187 case FIRST_ELIGIBLE_PIVOT:
1188 return start<FirstEligiblePivotRule>();
1189 case BEST_ELIGIBLE_PIVOT:
1190 return start<BestEligiblePivotRule>();
1191 case BLOCK_SEARCH_PIVOT:
1192 return start<BlockSearchPivotRule>();
1193 case CANDIDATE_LIST_PIVOT:
1194 return start<CandidateListPivotRule>();
1195 case ALTERING_LIST_PIVOT:
1196 return start<AlteringListPivotRule>();
1201 template<class PivotRuleImplementation>
1203 PivotRuleImplementation pivot(*this);
1205 // Execute the network simplex algorithm
1206 while (pivot.findEnteringArc()) {
1208 bool change = findLeavingArc();
1211 updateTreeStructure();
1216 // Check if the flow amount equals zero on all the artificial arcs
1217 for (int e = _arc_num; e != _arc_num + _node_num; ++e) {
1218 if (_flow[e] > 0) return false;
1221 // Copy flow values to _flow_map
1223 for (int i = 0; i != _arc_num; ++i) {
1224 Arc e = _arc_ref[i];
1225 _flow_map->set(e, (*_orig_lower)[e] + _flow[i]);
1228 for (int i = 0; i != _arc_num; ++i) {
1229 _flow_map->set(_arc_ref[i], _flow[i]);
1232 // Copy potential values to _potential_map
1233 for (NodeIt n(_graph); n != INVALID; ++n) {
1234 _potential_map->set(n, _pi[_node_id[n]]);
1240 }; //class NetworkSimplex
1246 #endif //LEMON_NETWORK_SIMPLEX_H