deba@2440: /* -*- C++ -*- deba@2440: * deba@2440: * This file is a part of LEMON, a generic C++ optimization library deba@2440: * alpar@2553: * Copyright (C) 2003-2008 deba@2440: * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport deba@2440: * (Egervary Research Group on Combinatorial Optimization, EGRES). deba@2440: * deba@2440: * Permission to use, modify and distribute this software is granted deba@2440: * provided that this copyright notice appears in all copies. For deba@2440: * precise terms see the accompanying LICENSE file. deba@2440: * deba@2440: * This software is provided "AS IS" with no warranty of any kind, deba@2440: * express or implied, and with no claim as to its suitability for any deba@2440: * purpose. deba@2440: * deba@2440: */ deba@2440: deba@2440: #ifndef LEMON_NETWORK_SIMPLEX_H deba@2440: #define LEMON_NETWORK_SIMPLEX_H deba@2440: deba@2440: /// \ingroup min_cost_flow deba@2440: /// deba@2440: /// \file kpeter@2575: /// \brief Network simplex algorithm for finding a minimum cost flow. deba@2440: kpeter@2575: #include deba@2440: #include kpeter@2630: #include kpeter@2575: kpeter@2509: #include kpeter@2575: #include deba@2440: deba@2440: namespace lemon { deba@2440: deba@2440: /// \addtogroup min_cost_flow deba@2440: /// @{ deba@2440: kpeter@2619: /// \brief Implementation of the primal network simplex algorithm kpeter@2619: /// for finding a minimum cost flow. deba@2440: /// kpeter@2619: /// \ref NetworkSimplex implements the primal network simplex algorithm kpeter@2619: /// for finding a minimum cost flow. deba@2440: /// kpeter@2575: /// \tparam Graph The directed graph type the algorithm runs on. kpeter@2575: /// \tparam LowerMap The type of the lower bound map. kpeter@2575: /// \tparam CapacityMap The type of the capacity (upper bound) map. kpeter@2575: /// \tparam CostMap The type of the cost (length) map. kpeter@2575: /// \tparam SupplyMap The type of the supply map. deba@2440: /// deba@2440: /// \warning kpeter@2575: /// - Edge capacities and costs should be \e non-negative \e integers. kpeter@2575: /// - Supply values should be \e signed \e integers. kpeter@2581: /// - The value types of the maps should be convertible to each other. kpeter@2581: /// - \c CostMap::Value must be signed type. kpeter@2575: /// kpeter@2619: /// \note \ref NetworkSimplex provides five different pivot rule kpeter@2575: /// implementations that significantly affect the efficiency of the kpeter@2575: /// algorithm. kpeter@2619: /// By default "Block Search" pivot rule is used, which proved to be kpeter@2619: /// by far the most efficient according to our benchmark tests. kpeter@2619: /// However another pivot rule can be selected using \ref run() kpeter@2619: /// function with the proper parameter. deba@2440: /// deba@2440: /// \author Peter Kovacs kpeter@2533: template < typename Graph, kpeter@2533: typename LowerMap = typename Graph::template EdgeMap, kpeter@2575: typename CapacityMap = typename Graph::template EdgeMap, kpeter@2533: typename CostMap = typename Graph::template EdgeMap, kpeter@2575: typename SupplyMap = typename Graph::template NodeMap > deba@2440: class NetworkSimplex deba@2440: { kpeter@2634: GRAPH_TYPEDEFS(typename Graph); kpeter@2634: deba@2440: typedef typename CapacityMap::Value Capacity; deba@2440: typedef typename CostMap::Value Cost; deba@2440: typedef typename SupplyMap::Value Supply; deba@2440: kpeter@2634: typedef std::vector EdgeVector; kpeter@2634: typedef std::vector NodeVector; kpeter@2634: typedef std::vector IntVector; kpeter@2634: typedef std::vector BoolVector; kpeter@2634: typedef std::vector CapacityVector; kpeter@2634: typedef std::vector CostVector; kpeter@2634: typedef std::vector SupplyVector; deba@2440: kpeter@2634: typedef typename Graph::template NodeMap IntNodeMap; kpeter@2619: deba@2440: public: deba@2440: kpeter@2556: /// The type of the flow map. deba@2440: typedef typename Graph::template EdgeMap FlowMap; kpeter@2556: /// The type of the potential map. deba@2440: typedef typename Graph::template NodeMap PotentialMap; deba@2440: kpeter@2575: public: deba@2440: kpeter@2575: /// Enum type to select the pivot rule used by \ref run(). kpeter@2575: enum PivotRuleEnum { kpeter@2575: FIRST_ELIGIBLE_PIVOT, kpeter@2575: BEST_ELIGIBLE_PIVOT, kpeter@2575: BLOCK_SEARCH_PIVOT, kpeter@2575: CANDIDATE_LIST_PIVOT, kpeter@2619: ALTERING_LIST_PIVOT kpeter@2575: }; kpeter@2575: kpeter@2575: private: kpeter@2575: kpeter@2575: /// \brief Implementation of the "First Eligible" pivot rule for the kpeter@2575: /// \ref NetworkSimplex "network simplex" algorithm. kpeter@2575: /// kpeter@2575: /// This class implements the "First Eligible" pivot rule kpeter@2575: /// for the \ref NetworkSimplex "network simplex" algorithm. kpeter@2619: /// kpeter@2619: /// For more information see \ref NetworkSimplex::run(). kpeter@2575: class FirstEligiblePivotRule kpeter@2575: { kpeter@2575: private: deba@2440: kpeter@2619: // References to the NetworkSimplex class kpeter@2634: const EdgeVector &_edge; kpeter@2634: const IntVector &_source; kpeter@2634: const IntVector &_target; kpeter@2634: const CostVector &_cost; kpeter@2634: const IntVector &_state; kpeter@2634: const CostVector &_pi; kpeter@2634: int &_in_edge; kpeter@2634: int _edge_num; kpeter@2619: kpeter@2634: // Pivot rule data kpeter@2619: int _next_edge; deba@2440: kpeter@2575: public: deba@2440: kpeter@2619: /// Constructor kpeter@2634: FirstEligiblePivotRule(NetworkSimplex &ns) : kpeter@2634: _edge(ns._edge), _source(ns._source), _target(ns._target), kpeter@2634: _cost(ns._cost), _state(ns._state), _pi(ns._pi), kpeter@2634: _in_edge(ns._in_edge), _edge_num(ns._edge_num), _next_edge(0) kpeter@2634: {} kpeter@2575: kpeter@2619: /// Find next entering edge kpeter@2630: bool findEnteringEdge() { kpeter@2634: Cost c; kpeter@2634: for (int e = _next_edge; e < _edge_num; ++e) { kpeter@2634: c = _state[e] * (_cost[e] + _pi[_source[e]] - _pi[_target[e]]); kpeter@2634: if (c < 0) { kpeter@2634: _in_edge = e; kpeter@2634: _next_edge = e + 1; kpeter@2575: return true; kpeter@2575: } kpeter@2575: } kpeter@2634: for (int e = 0; e < _next_edge; ++e) { kpeter@2634: c = _state[e] * (_cost[e] + _pi[_source[e]] - _pi[_target[e]]); kpeter@2634: if (c < 0) { kpeter@2634: _in_edge = e; kpeter@2634: _next_edge = e + 1; kpeter@2575: return true; kpeter@2575: } kpeter@2575: } kpeter@2575: return false; kpeter@2575: } kpeter@2634: kpeter@2575: }; //class FirstEligiblePivotRule kpeter@2575: kpeter@2634: kpeter@2575: /// \brief Implementation of the "Best Eligible" pivot rule for the kpeter@2575: /// \ref NetworkSimplex "network simplex" algorithm. kpeter@2575: /// kpeter@2575: /// This class implements the "Best Eligible" pivot rule kpeter@2575: /// for the \ref NetworkSimplex "network simplex" algorithm. kpeter@2619: /// kpeter@2619: /// For more information see \ref NetworkSimplex::run(). kpeter@2575: class BestEligiblePivotRule kpeter@2575: { kpeter@2575: private: kpeter@2575: kpeter@2619: // References to the NetworkSimplex class kpeter@2634: const EdgeVector &_edge; kpeter@2634: const IntVector &_source; kpeter@2634: const IntVector &_target; kpeter@2634: const CostVector &_cost; kpeter@2634: const IntVector &_state; kpeter@2634: const CostVector &_pi; kpeter@2634: int &_in_edge; kpeter@2634: int _edge_num; kpeter@2575: kpeter@2575: public: kpeter@2575: kpeter@2619: /// Constructor kpeter@2634: BestEligiblePivotRule(NetworkSimplex &ns) : kpeter@2634: _edge(ns._edge), _source(ns._source), _target(ns._target), kpeter@2634: _cost(ns._cost), _state(ns._state), _pi(ns._pi), kpeter@2634: _in_edge(ns._in_edge), _edge_num(ns._edge_num) kpeter@2634: {} kpeter@2575: kpeter@2619: /// Find next entering edge kpeter@2630: bool findEnteringEdge() { kpeter@2634: Cost c, min = 0; kpeter@2634: for (int e = 0; e < _edge_num; ++e) { kpeter@2634: c = _state[e] * (_cost[e] + _pi[_source[e]] - _pi[_target[e]]); kpeter@2634: if (c < min) { kpeter@2634: min = c; kpeter@2634: _in_edge = e; kpeter@2575: } kpeter@2575: } kpeter@2575: return min < 0; kpeter@2575: } kpeter@2634: kpeter@2575: }; //class BestEligiblePivotRule kpeter@2575: kpeter@2634: kpeter@2575: /// \brief Implementation of the "Block Search" pivot rule for the kpeter@2575: /// \ref NetworkSimplex "network simplex" algorithm. kpeter@2575: /// kpeter@2575: /// This class implements the "Block Search" pivot rule kpeter@2575: /// for the \ref NetworkSimplex "network simplex" algorithm. kpeter@2619: /// kpeter@2619: /// For more information see \ref NetworkSimplex::run(). kpeter@2575: class BlockSearchPivotRule kpeter@2575: { kpeter@2575: private: kpeter@2575: kpeter@2619: // References to the NetworkSimplex class kpeter@2634: const EdgeVector &_edge; kpeter@2634: const IntVector &_source; kpeter@2634: const IntVector &_target; kpeter@2634: const CostVector &_cost; kpeter@2634: const IntVector &_state; kpeter@2634: const CostVector &_pi; kpeter@2634: int &_in_edge; kpeter@2634: int _edge_num; kpeter@2619: kpeter@2634: // Pivot rule data kpeter@2575: int _block_size; kpeter@2634: int _next_edge; kpeter@2575: kpeter@2575: public: kpeter@2575: kpeter@2619: /// Constructor kpeter@2634: BlockSearchPivotRule(NetworkSimplex &ns) : kpeter@2634: _edge(ns._edge), _source(ns._source), _target(ns._target), kpeter@2634: _cost(ns._cost), _state(ns._state), _pi(ns._pi), kpeter@2635: _in_edge(ns._in_edge), _edge_num(ns._edge_num + ns._node_num), _next_edge(0) kpeter@2575: { kpeter@2619: // The main parameters of the pivot rule kpeter@2619: const double BLOCK_SIZE_FACTOR = 2.0; kpeter@2619: const int MIN_BLOCK_SIZE = 10; kpeter@2619: kpeter@2634: _block_size = std::max( int(BLOCK_SIZE_FACTOR * sqrt(_edge_num)), kpeter@2619: MIN_BLOCK_SIZE ); kpeter@2575: } kpeter@2575: kpeter@2619: /// Find next entering edge kpeter@2630: bool findEnteringEdge() { kpeter@2634: Cost c, min = 0; kpeter@2619: int cnt = _block_size; kpeter@2634: int e, min_edge = _next_edge; kpeter@2634: for (e = _next_edge; e < _edge_num; ++e) { kpeter@2634: c = _state[e] * (_cost[e] + _pi[_source[e]] - _pi[_target[e]]); kpeter@2634: if (c < min) { kpeter@2634: min = c; kpeter@2634: min_edge = e; kpeter@2575: } kpeter@2619: if (--cnt == 0) { kpeter@2575: if (min < 0) break; kpeter@2619: cnt = _block_size; kpeter@2575: } kpeter@2575: } kpeter@2619: if (min == 0 || cnt > 0) { kpeter@2634: for (e = 0; e < _next_edge; ++e) { kpeter@2634: c = _state[e] * (_cost[e] + _pi[_source[e]] - _pi[_target[e]]); kpeter@2634: if (c < min) { kpeter@2634: min = c; kpeter@2634: min_edge = e; kpeter@2575: } kpeter@2619: if (--cnt == 0) { kpeter@2575: if (min < 0) break; kpeter@2619: cnt = _block_size; kpeter@2575: } kpeter@2575: } kpeter@2575: } kpeter@2619: if (min >= 0) return false; kpeter@2634: _in_edge = min_edge; kpeter@2634: _next_edge = e; kpeter@2619: return true; kpeter@2575: } kpeter@2634: kpeter@2575: }; //class BlockSearchPivotRule kpeter@2575: kpeter@2634: kpeter@2575: /// \brief Implementation of the "Candidate List" pivot rule for the kpeter@2575: /// \ref NetworkSimplex "network simplex" algorithm. kpeter@2575: /// kpeter@2575: /// This class implements the "Candidate List" pivot rule kpeter@2575: /// for the \ref NetworkSimplex "network simplex" algorithm. kpeter@2619: /// kpeter@2619: /// For more information see \ref NetworkSimplex::run(). kpeter@2575: class CandidateListPivotRule kpeter@2575: { kpeter@2575: private: kpeter@2575: kpeter@2619: // References to the NetworkSimplex class kpeter@2634: const EdgeVector &_edge; kpeter@2634: const IntVector &_source; kpeter@2634: const IntVector &_target; kpeter@2634: const CostVector &_cost; kpeter@2634: const IntVector &_state; kpeter@2634: const CostVector &_pi; kpeter@2634: int &_in_edge; kpeter@2634: int _edge_num; kpeter@2575: kpeter@2634: // Pivot rule data kpeter@2634: IntVector _candidates; kpeter@2619: int _list_length, _minor_limit; kpeter@2619: int _curr_length, _minor_count; kpeter@2634: int _next_edge; kpeter@2575: kpeter@2575: public: kpeter@2575: kpeter@2619: /// Constructor kpeter@2634: CandidateListPivotRule(NetworkSimplex &ns) : kpeter@2634: _edge(ns._edge), _source(ns._source), _target(ns._target), kpeter@2634: _cost(ns._cost), _state(ns._state), _pi(ns._pi), kpeter@2634: _in_edge(ns._in_edge), _edge_num(ns._edge_num), _next_edge(0) kpeter@2575: { kpeter@2619: // The main parameters of the pivot rule kpeter@2619: const double LIST_LENGTH_FACTOR = 1.0; kpeter@2619: const int MIN_LIST_LENGTH = 10; kpeter@2619: const double MINOR_LIMIT_FACTOR = 0.1; kpeter@2619: const int MIN_MINOR_LIMIT = 3; kpeter@2619: kpeter@2634: _list_length = std::max( int(LIST_LENGTH_FACTOR * sqrt(_edge_num)), kpeter@2619: MIN_LIST_LENGTH ); kpeter@2619: _minor_limit = std::max( int(MINOR_LIMIT_FACTOR * _list_length), kpeter@2619: MIN_MINOR_LIMIT ); kpeter@2619: _curr_length = _minor_count = 0; kpeter@2619: _candidates.resize(_list_length); kpeter@2575: } kpeter@2575: kpeter@2619: /// Find next entering edge kpeter@2630: bool findEnteringEdge() { kpeter@2634: Cost min, c; kpeter@2634: int e, min_edge = _next_edge; kpeter@2619: if (_curr_length > 0 && _minor_count < _minor_limit) { kpeter@2630: // Minor iteration: select the best eligible edge from the kpeter@2630: // current candidate list kpeter@2575: ++_minor_count; kpeter@2575: min = 0; kpeter@2619: for (int i = 0; i < _curr_length; ++i) { kpeter@2575: e = _candidates[i]; kpeter@2634: c = _state[e] * (_cost[e] + _pi[_source[e]] - _pi[_target[e]]); kpeter@2634: if (c < min) { kpeter@2634: min = c; kpeter@2634: min_edge = e; kpeter@2575: } kpeter@2634: if (c >= 0) { kpeter@2619: _candidates[i--] = _candidates[--_curr_length]; kpeter@2619: } kpeter@2575: } kpeter@2634: if (min < 0) { kpeter@2634: _in_edge = min_edge; kpeter@2634: return true; kpeter@2634: } kpeter@2575: } kpeter@2575: kpeter@2630: // Major iteration: build a new candidate list kpeter@2575: min = 0; kpeter@2619: _curr_length = 0; kpeter@2634: for (e = _next_edge; e < _edge_num; ++e) { kpeter@2634: c = _state[e] * (_cost[e] + _pi[_source[e]] - _pi[_target[e]]); kpeter@2634: if (c < 0) { kpeter@2619: _candidates[_curr_length++] = e; kpeter@2634: if (c < min) { kpeter@2634: min = c; kpeter@2634: min_edge = e; kpeter@2575: } kpeter@2619: if (_curr_length == _list_length) break; kpeter@2575: } kpeter@2575: } kpeter@2619: if (_curr_length < _list_length) { kpeter@2634: for (e = 0; e < _next_edge; ++e) { kpeter@2634: c = _state[e] * (_cost[e] + _pi[_source[e]] - _pi[_target[e]]); kpeter@2634: if (c < 0) { kpeter@2619: _candidates[_curr_length++] = e; kpeter@2634: if (c < min) { kpeter@2634: min = c; kpeter@2634: min_edge = e; kpeter@2575: } kpeter@2619: if (_curr_length == _list_length) break; kpeter@2575: } kpeter@2575: } kpeter@2575: } kpeter@2619: if (_curr_length == 0) return false; kpeter@2575: _minor_count = 1; kpeter@2634: _in_edge = min_edge; kpeter@2634: _next_edge = e; kpeter@2575: return true; kpeter@2575: } kpeter@2634: kpeter@2575: }; //class CandidateListPivotRule kpeter@2575: kpeter@2634: kpeter@2619: /// \brief Implementation of the "Altering Candidate List" pivot rule kpeter@2619: /// for the \ref NetworkSimplex "network simplex" algorithm. kpeter@2619: /// kpeter@2619: /// This class implements the "Altering Candidate List" pivot rule kpeter@2619: /// for the \ref NetworkSimplex "network simplex" algorithm. kpeter@2619: /// kpeter@2619: /// For more information see \ref NetworkSimplex::run(). kpeter@2619: class AlteringListPivotRule kpeter@2619: { kpeter@2619: private: kpeter@2619: kpeter@2619: // References to the NetworkSimplex class kpeter@2634: const EdgeVector &_edge; kpeter@2634: const IntVector &_source; kpeter@2634: const IntVector &_target; kpeter@2634: const CostVector &_cost; kpeter@2634: const IntVector &_state; kpeter@2634: const CostVector &_pi; kpeter@2634: int &_in_edge; kpeter@2634: int _edge_num; kpeter@2619: kpeter@2619: int _block_size, _head_length, _curr_length; kpeter@2619: int _next_edge; kpeter@2634: IntVector _candidates; kpeter@2634: CostVector _cand_cost; kpeter@2619: kpeter@2619: // Functor class to compare edges during sort of the candidate list kpeter@2619: class SortFunc kpeter@2619: { kpeter@2619: private: kpeter@2634: const CostVector &_map; kpeter@2619: public: kpeter@2634: SortFunc(const CostVector &map) : _map(map) {} kpeter@2634: bool operator()(int left, int right) { kpeter@2634: return _map[left] > _map[right]; kpeter@2619: } kpeter@2619: }; kpeter@2619: kpeter@2619: SortFunc _sort_func; kpeter@2619: kpeter@2619: public: kpeter@2619: kpeter@2619: /// Constructor kpeter@2634: AlteringListPivotRule(NetworkSimplex &ns) : kpeter@2634: _edge(ns._edge), _source(ns._source), _target(ns._target), kpeter@2634: _cost(ns._cost), _state(ns._state), _pi(ns._pi), kpeter@2634: _in_edge(ns._in_edge), _edge_num(ns._edge_num), kpeter@2634: _next_edge(0), _cand_cost(ns._edge_num),_sort_func(_cand_cost) kpeter@2619: { kpeter@2619: // The main parameters of the pivot rule kpeter@2630: const double BLOCK_SIZE_FACTOR = 1.5; kpeter@2619: const int MIN_BLOCK_SIZE = 10; kpeter@2619: const double HEAD_LENGTH_FACTOR = 0.1; kpeter@2630: const int MIN_HEAD_LENGTH = 3; kpeter@2619: kpeter@2634: _block_size = std::max( int(BLOCK_SIZE_FACTOR * sqrt(_edge_num)), kpeter@2619: MIN_BLOCK_SIZE ); kpeter@2619: _head_length = std::max( int(HEAD_LENGTH_FACTOR * _block_size), kpeter@2619: MIN_HEAD_LENGTH ); kpeter@2619: _candidates.resize(_head_length + _block_size); kpeter@2619: _curr_length = 0; kpeter@2619: } kpeter@2619: kpeter@2619: /// Find next entering edge kpeter@2630: bool findEnteringEdge() { kpeter@2630: // Check the current candidate list kpeter@2634: int e; kpeter@2634: for (int i = 0; i < _curr_length; ++i) { kpeter@2634: e = _candidates[i]; kpeter@2634: _cand_cost[e] = _state[e] * kpeter@2634: (_cost[e] + _pi[_source[e]] - _pi[_target[e]]); kpeter@2634: if (_cand_cost[e] >= 0) { kpeter@2634: _candidates[i--] = _candidates[--_curr_length]; kpeter@2619: } kpeter@2619: } kpeter@2619: kpeter@2630: // Extend the list kpeter@2619: int cnt = _block_size; kpeter@2619: int last_edge = 0; kpeter@2619: int limit = _head_length; kpeter@2634: kpeter@2634: for (int e = _next_edge; e < _edge_num; ++e) { kpeter@2634: _cand_cost[e] = _state[e] * kpeter@2634: (_cost[e] + _pi[_source[e]] - _pi[_target[e]]); kpeter@2634: if (_cand_cost[e] < 0) { kpeter@2619: _candidates[_curr_length++] = e; kpeter@2634: last_edge = e; kpeter@2619: } kpeter@2619: if (--cnt == 0) { kpeter@2619: if (_curr_length > limit) break; kpeter@2619: limit = 0; kpeter@2619: cnt = _block_size; kpeter@2619: } kpeter@2619: } kpeter@2619: if (_curr_length <= limit) { kpeter@2634: for (int e = 0; e < _next_edge; ++e) { kpeter@2634: _cand_cost[e] = _state[e] * kpeter@2634: (_cost[e] + _pi[_source[e]] - _pi[_target[e]]); kpeter@2634: if (_cand_cost[e] < 0) { kpeter@2619: _candidates[_curr_length++] = e; kpeter@2634: last_edge = e; kpeter@2619: } kpeter@2619: if (--cnt == 0) { kpeter@2619: if (_curr_length > limit) break; kpeter@2619: limit = 0; kpeter@2619: cnt = _block_size; kpeter@2619: } kpeter@2619: } kpeter@2619: } kpeter@2619: if (_curr_length == 0) return false; kpeter@2619: _next_edge = last_edge + 1; kpeter@2619: kpeter@2630: // Make heap of the candidate list (approximating a partial sort) kpeter@2630: make_heap( _candidates.begin(), _candidates.begin() + _curr_length, kpeter@2630: _sort_func ); kpeter@2619: kpeter@2630: // Pop the first element of the heap kpeter@2634: _in_edge = _candidates[0]; kpeter@2630: pop_heap( _candidates.begin(), _candidates.begin() + _curr_length, kpeter@2630: _sort_func ); kpeter@2630: _curr_length = std::min(_head_length, _curr_length - 1); kpeter@2619: return true; kpeter@2619: } kpeter@2634: kpeter@2619: }; //class AlteringListPivotRule kpeter@2619: kpeter@2575: private: kpeter@2575: kpeter@2579: // State constants for edges kpeter@2579: enum EdgeStateEnum { kpeter@2579: STATE_UPPER = -1, kpeter@2579: STATE_TREE = 0, kpeter@2579: STATE_LOWER = 1 kpeter@2579: }; kpeter@2575: kpeter@2575: private: kpeter@2575: kpeter@2575: // The original graph kpeter@2634: const Graph &_orig_graph; kpeter@2575: // The original lower bound map kpeter@2634: const LowerMap *_orig_lower; kpeter@2634: // The original capacity map kpeter@2634: const CapacityMap &_orig_cap; kpeter@2634: // The original cost map kpeter@2634: const CostMap &_orig_cost; kpeter@2634: // The original supply map kpeter@2634: const SupplyMap *_orig_supply; kpeter@2634: // The source node (if no supply map was given) kpeter@2634: Node _orig_source; kpeter@2634: // The target node (if no supply map was given) kpeter@2634: Node _orig_target; kpeter@2634: // The flow value (if no supply map was given) kpeter@2634: Capacity _orig_flow_value; kpeter@2575: kpeter@2634: // The flow result map kpeter@2634: FlowMap *_flow_result; kpeter@2634: // The potential result map kpeter@2634: PotentialMap *_potential_result; kpeter@2634: // Indicate if the flow result map is local kpeter@2634: bool _local_flow; kpeter@2634: // Indicate if the potential result map is local kpeter@2634: bool _local_potential; kpeter@2575: kpeter@2634: // The edge references kpeter@2634: EdgeVector _edge; kpeter@2634: // The node references kpeter@2634: NodeVector _node; kpeter@2634: // The node ids kpeter@2634: IntNodeMap _node_id; kpeter@2634: // The source nodes of the edges kpeter@2634: IntVector _source; kpeter@2634: // The target nodess of the edges kpeter@2634: IntVector _target; kpeter@2575: kpeter@2634: // The (modified) capacity vector kpeter@2634: CapacityVector _cap; kpeter@2634: // The cost vector kpeter@2634: CostVector _cost; kpeter@2634: // The (modified) supply vector kpeter@2634: CostVector _supply; kpeter@2634: // The current flow vector kpeter@2634: CapacityVector _flow; kpeter@2634: // The current potential vector kpeter@2634: CostVector _pi; kpeter@2575: kpeter@2634: // The number of nodes in the original graph kpeter@2634: int _node_num; kpeter@2634: // The number of edges in the original graph kpeter@2634: int _edge_num; kpeter@2619: kpeter@2634: // The parent vector of the spanning tree structure kpeter@2634: IntVector _parent; kpeter@2634: // The pred_edge vector of the spanning tree structure kpeter@2634: IntVector _pred; kpeter@2634: // The thread vector of the spanning tree structure kpeter@2634: IntVector _thread; kpeter@2635: kpeter@2635: IntVector _rev_thread; kpeter@2635: IntVector _succ_num; kpeter@2635: IntVector _last_succ; kpeter@2635: kpeter@2635: IntVector _dirty_revs; kpeter@2635: kpeter@2634: // The forward vector of the spanning tree structure kpeter@2634: BoolVector _forward; kpeter@2634: // The state vector kpeter@2634: IntVector _state; kpeter@2634: // The root node kpeter@2634: int _root; deba@2440: kpeter@2630: // The entering edge of the current pivot iteration kpeter@2634: int _in_edge; kpeter@2575: kpeter@2630: // Temporary nodes used in the current pivot iteration kpeter@2634: int join, u_in, v_in, u_out, v_out; kpeter@2634: int right, first, second, last; kpeter@2634: int stem, par_stem, new_stem; kpeter@2634: kpeter@2556: // The maximum augment amount along the found cycle in the current kpeter@2630: // pivot iteration kpeter@2556: Capacity delta; deba@2440: kpeter@2634: public: deba@2440: kpeter@2581: /// \brief General constructor (with lower bounds). deba@2440: /// kpeter@2581: /// General constructor (with lower bounds). deba@2440: /// kpeter@2575: /// \param graph The directed graph the algorithm runs on. kpeter@2575: /// \param lower The lower bounds of the edges. kpeter@2575: /// \param capacity The capacities (upper bounds) of the edges. kpeter@2575: /// \param cost The cost (length) values of the edges. kpeter@2575: /// \param supply The supply values of the nodes (signed). kpeter@2575: NetworkSimplex( const Graph &graph, kpeter@2575: const LowerMap &lower, kpeter@2575: const CapacityMap &capacity, kpeter@2575: const CostMap &cost, kpeter@2575: const SupplyMap &supply ) : kpeter@2634: _orig_graph(graph), _orig_lower(&lower), _orig_cap(capacity), kpeter@2634: _orig_cost(cost), _orig_supply(&supply), kpeter@2623: _flow_result(NULL), _potential_result(NULL), kpeter@2581: _local_flow(false), _local_potential(false), kpeter@2634: _node_id(graph) kpeter@2634: {} deba@2440: kpeter@2581: /// \brief General constructor (without lower bounds). deba@2440: /// kpeter@2581: /// General constructor (without lower bounds). deba@2440: /// kpeter@2575: /// \param graph The directed graph the algorithm runs on. kpeter@2575: /// \param capacity The capacities (upper bounds) of the edges. kpeter@2575: /// \param cost The cost (length) values of the edges. kpeter@2575: /// \param supply The supply values of the nodes (signed). kpeter@2575: NetworkSimplex( const Graph &graph, kpeter@2575: const CapacityMap &capacity, kpeter@2575: const CostMap &cost, kpeter@2575: const SupplyMap &supply ) : kpeter@2634: _orig_graph(graph), _orig_lower(NULL), _orig_cap(capacity), kpeter@2634: _orig_cost(cost), _orig_supply(&supply), kpeter@2623: _flow_result(NULL), _potential_result(NULL), kpeter@2581: _local_flow(false), _local_potential(false), kpeter@2634: _node_id(graph) kpeter@2634: {} deba@2440: kpeter@2581: /// \brief Simple constructor (with lower bounds). deba@2440: /// kpeter@2581: /// Simple constructor (with lower bounds). deba@2440: /// kpeter@2575: /// \param graph The directed graph the algorithm runs on. kpeter@2575: /// \param lower The lower bounds of the edges. kpeter@2575: /// \param capacity The capacities (upper bounds) of the edges. kpeter@2575: /// \param cost The cost (length) values of the edges. kpeter@2575: /// \param s The source node. kpeter@2575: /// \param t The target node. kpeter@2575: /// \param flow_value The required amount of flow from node \c s kpeter@2575: /// to node \c t (i.e. the supply of \c s and the demand of \c t). kpeter@2575: NetworkSimplex( const Graph &graph, kpeter@2575: const LowerMap &lower, kpeter@2575: const CapacityMap &capacity, kpeter@2575: const CostMap &cost, kpeter@2634: Node s, Node t, kpeter@2634: Capacity flow_value ) : kpeter@2634: _orig_graph(graph), _orig_lower(&lower), _orig_cap(capacity), kpeter@2634: _orig_cost(cost), _orig_supply(NULL), kpeter@2634: _orig_source(s), _orig_target(t), _orig_flow_value(flow_value), kpeter@2623: _flow_result(NULL), _potential_result(NULL), kpeter@2581: _local_flow(false), _local_potential(false), kpeter@2634: _node_id(graph) kpeter@2634: {} deba@2440: kpeter@2581: /// \brief Simple constructor (without lower bounds). deba@2440: /// kpeter@2581: /// Simple constructor (without lower bounds). deba@2440: /// kpeter@2575: /// \param graph The directed graph the algorithm runs on. kpeter@2575: /// \param capacity The capacities (upper bounds) of the edges. kpeter@2575: /// \param cost The cost (length) values of the edges. kpeter@2575: /// \param s The source node. kpeter@2575: /// \param t The target node. kpeter@2575: /// \param flow_value The required amount of flow from node \c s kpeter@2575: /// to node \c t (i.e. the supply of \c s and the demand of \c t). kpeter@2575: NetworkSimplex( const Graph &graph, kpeter@2575: const CapacityMap &capacity, kpeter@2575: const CostMap &cost, kpeter@2634: Node s, Node t, kpeter@2634: Capacity flow_value ) : kpeter@2634: _orig_graph(graph), _orig_lower(NULL), _orig_cap(capacity), kpeter@2634: _orig_cost(cost), _orig_supply(NULL), kpeter@2634: _orig_source(s), _orig_target(t), _orig_flow_value(flow_value), kpeter@2623: _flow_result(NULL), _potential_result(NULL), kpeter@2581: _local_flow(false), _local_potential(false), kpeter@2634: _node_id(graph) kpeter@2634: {} deba@2440: kpeter@2581: /// Destructor. kpeter@2581: ~NetworkSimplex() { kpeter@2581: if (_local_flow) delete _flow_result; kpeter@2581: if (_local_potential) delete _potential_result; kpeter@2581: } kpeter@2581: kpeter@2619: /// \brief Set the flow map. kpeter@2581: /// kpeter@2619: /// Set the flow map. kpeter@2581: /// kpeter@2581: /// \return \c (*this) kpeter@2581: NetworkSimplex& flowMap(FlowMap &map) { kpeter@2581: if (_local_flow) { kpeter@2581: delete _flow_result; kpeter@2581: _local_flow = false; kpeter@2581: } kpeter@2581: _flow_result = ↦ kpeter@2581: return *this; kpeter@2581: } kpeter@2581: kpeter@2619: /// \brief Set the potential map. kpeter@2581: /// kpeter@2619: /// Set the potential map. kpeter@2581: /// kpeter@2581: /// \return \c (*this) kpeter@2581: NetworkSimplex& potentialMap(PotentialMap &map) { kpeter@2581: if (_local_potential) { kpeter@2581: delete _potential_result; kpeter@2581: _local_potential = false; kpeter@2581: } kpeter@2581: _potential_result = ↦ kpeter@2581: return *this; kpeter@2581: } kpeter@2581: kpeter@2581: /// \name Execution control kpeter@2581: kpeter@2581: /// @{ kpeter@2581: kpeter@2556: /// \brief Runs the algorithm. kpeter@2556: /// kpeter@2556: /// Runs the algorithm. kpeter@2556: /// kpeter@2575: /// \param pivot_rule The pivot rule that is used during the kpeter@2575: /// algorithm. kpeter@2575: /// kpeter@2575: /// The available pivot rules: kpeter@2575: /// kpeter@2575: /// - FIRST_ELIGIBLE_PIVOT The next eligible edge is selected in kpeter@2575: /// a wraparound fashion in every iteration kpeter@2575: /// (\ref FirstEligiblePivotRule). kpeter@2575: /// kpeter@2575: /// - BEST_ELIGIBLE_PIVOT The best eligible edge is selected in kpeter@2575: /// every iteration (\ref BestEligiblePivotRule). kpeter@2575: /// kpeter@2575: /// - BLOCK_SEARCH_PIVOT A specified number of edges are examined in kpeter@2575: /// every iteration in a wraparound fashion and the best eligible kpeter@2575: /// edge is selected from this block (\ref BlockSearchPivotRule). kpeter@2575: /// kpeter@2619: /// - CANDIDATE_LIST_PIVOT In a major iteration a candidate list is kpeter@2619: /// built from eligible edges in a wraparound fashion and in the kpeter@2619: /// following minor iterations the best eligible edge is selected kpeter@2619: /// from this list (\ref CandidateListPivotRule). kpeter@2575: /// kpeter@2619: /// - ALTERING_LIST_PIVOT It is a modified version of the kpeter@2619: /// "Candidate List" pivot rule. It keeps only the several best kpeter@2619: /// eligible edges from the former candidate list and extends this kpeter@2619: /// list in every iteration (\ref AlteringListPivotRule). kpeter@2575: /// kpeter@2619: /// According to our comprehensive benchmark tests the "Block Search" kpeter@2630: /// pivot rule proved to be the fastest and the most robust on kpeter@2630: /// various test inputs. Thus it is the default option. kpeter@2575: /// kpeter@2556: /// \return \c true if a feasible flow can be found. kpeter@2619: bool run(PivotRuleEnum pivot_rule = BLOCK_SEARCH_PIVOT) { kpeter@2575: return init() && start(pivot_rule); kpeter@2556: } kpeter@2556: kpeter@2581: /// @} kpeter@2581: kpeter@2581: /// \name Query Functions kpeter@2619: /// The results of the algorithm can be obtained using these kpeter@2619: /// functions.\n kpeter@2619: /// \ref lemon::NetworkSimplex::run() "run()" must be called before kpeter@2619: /// using them. kpeter@2581: kpeter@2581: /// @{ kpeter@2581: kpeter@2619: /// \brief Return a const reference to the edge map storing the kpeter@2575: /// found flow. deba@2440: /// kpeter@2619: /// Return a const reference to the edge map storing the found flow. deba@2440: /// deba@2440: /// \pre \ref run() must be called before using this function. deba@2440: const FlowMap& flowMap() const { kpeter@2581: return *_flow_result; deba@2440: } deba@2440: kpeter@2619: /// \brief Return a const reference to the node map storing the kpeter@2575: /// found potentials (the dual solution). deba@2440: /// kpeter@2619: /// Return a const reference to the node map storing the found kpeter@2575: /// potentials (the dual solution). deba@2440: /// deba@2440: /// \pre \ref run() must be called before using this function. deba@2440: const PotentialMap& potentialMap() const { kpeter@2581: return *_potential_result; kpeter@2581: } kpeter@2581: kpeter@2619: /// \brief Return the flow on the given edge. kpeter@2581: /// kpeter@2619: /// Return the flow on the given edge. kpeter@2581: /// kpeter@2581: /// \pre \ref run() must be called before using this function. kpeter@2581: Capacity flow(const typename Graph::Edge& edge) const { kpeter@2581: return (*_flow_result)[edge]; kpeter@2581: } kpeter@2581: kpeter@2619: /// \brief Return the potential of the given node. kpeter@2581: /// kpeter@2619: /// Return the potential of the given node. kpeter@2581: /// kpeter@2581: /// \pre \ref run() must be called before using this function. kpeter@2581: Cost potential(const typename Graph::Node& node) const { kpeter@2581: return (*_potential_result)[node]; deba@2440: } deba@2440: kpeter@2619: /// \brief Return the total cost of the found flow. deba@2440: /// kpeter@2619: /// Return the total cost of the found flow. The complexity of the deba@2440: /// function is \f$ O(e) \f$. deba@2440: /// deba@2440: /// \pre \ref run() must be called before using this function. deba@2440: Cost totalCost() const { deba@2440: Cost c = 0; kpeter@2634: for (EdgeIt e(_orig_graph); e != INVALID; ++e) kpeter@2634: c += (*_flow_result)[e] * _orig_cost[e]; deba@2440: return c; deba@2440: } deba@2440: kpeter@2581: /// @} kpeter@2581: kpeter@2575: private: deba@2440: kpeter@2634: // Initialize internal data structures deba@2440: bool init() { kpeter@2630: // Initialize result maps kpeter@2581: if (!_flow_result) { kpeter@2634: _flow_result = new FlowMap(_orig_graph); kpeter@2581: _local_flow = true; kpeter@2581: } kpeter@2581: if (!_potential_result) { kpeter@2634: _potential_result = new PotentialMap(_orig_graph); kpeter@2581: _local_potential = true; kpeter@2581: } kpeter@2634: kpeter@2634: // Initialize vectors kpeter@2634: _node_num = countNodes(_orig_graph); kpeter@2634: _edge_num = countEdges(_orig_graph); kpeter@2634: int all_node_num = _node_num + 1; kpeter@2634: int all_edge_num = _edge_num + _node_num; kpeter@2634: kpeter@2634: _edge.resize(_edge_num); kpeter@2634: _node.reserve(_node_num); kpeter@2634: _source.resize(all_edge_num); kpeter@2634: _target.resize(all_edge_num); kpeter@2634: kpeter@2634: _cap.resize(all_edge_num); kpeter@2634: _cost.resize(all_edge_num); kpeter@2634: _supply.resize(all_node_num); kpeter@2634: _flow.resize(all_edge_num, 0); kpeter@2634: _pi.resize(all_node_num, 0); kpeter@2634: kpeter@2634: _parent.resize(all_node_num); kpeter@2634: _pred.resize(all_node_num); kpeter@2635: _forward.resize(all_node_num); kpeter@2634: _thread.resize(all_node_num); kpeter@2635: _rev_thread.resize(all_node_num); kpeter@2635: _succ_num.resize(all_node_num); kpeter@2635: _last_succ.resize(all_node_num); kpeter@2634: _state.resize(all_edge_num, STATE_LOWER); kpeter@2634: kpeter@2634: // Initialize node related data kpeter@2634: bool valid_supply = true; kpeter@2634: if (_orig_supply) { kpeter@2634: Supply sum = 0; kpeter@2634: int i = 0; kpeter@2634: for (NodeIt n(_orig_graph); n != INVALID; ++n, ++i) { kpeter@2634: _node.push_back(n); kpeter@2634: _node_id[n] = i; kpeter@2634: _supply[i] = (*_orig_supply)[n]; kpeter@2634: sum += _supply[i]; kpeter@2634: } kpeter@2634: valid_supply = (sum == 0); kpeter@2634: } else { kpeter@2634: int i = 0; kpeter@2634: for (NodeIt n(_orig_graph); n != INVALID; ++n, ++i) { kpeter@2634: _node.push_back(n); kpeter@2634: _node_id[n] = i; kpeter@2634: _supply[i] = 0; kpeter@2634: } kpeter@2634: _supply[_node_id[_orig_source]] = _orig_flow_value; kpeter@2634: _supply[_node_id[_orig_target]] = -_orig_flow_value; kpeter@2634: } kpeter@2634: if (!valid_supply) return false; kpeter@2634: kpeter@2634: // Set data for the artificial root node kpeter@2634: _root = _node_num; kpeter@2634: _parent[_root] = -1; kpeter@2634: _pred[_root] = -1; kpeter@2634: _thread[_root] = 0; kpeter@2635: _rev_thread[0] = _root; kpeter@2635: _succ_num[_root] = all_node_num; kpeter@2635: _last_succ[_root] = _root - 1; kpeter@2634: _supply[_root] = 0; kpeter@2634: _pi[_root] = 0; kpeter@2634: kpeter@2634: // Store the edges in a mixed order kpeter@2634: int k = std::max(int(sqrt(_edge_num)), 10); kpeter@2634: int i = 0; kpeter@2634: for (EdgeIt e(_orig_graph); e != INVALID; ++e) { kpeter@2634: _edge[i] = e; kpeter@2634: if ((i += k) >= _edge_num) i = (i % k) + 1; deba@2440: } deba@2440: kpeter@2634: // Initialize edge maps kpeter@2634: for (int i = 0; i != _edge_num; ++i) { kpeter@2634: Edge e = _edge[i]; kpeter@2634: _source[i] = _node_id[_orig_graph.source(e)]; kpeter@2634: _target[i] = _node_id[_orig_graph.target(e)]; kpeter@2634: _cost[i] = _orig_cost[e]; kpeter@2634: _cap[i] = _orig_cap[e]; kpeter@2634: } deba@2440: kpeter@2634: // Remove non-zero lower bounds kpeter@2634: if (_orig_lower) { kpeter@2634: for (int i = 0; i != _edge_num; ++i) { kpeter@2634: Capacity c = (*_orig_lower)[_edge[i]]; kpeter@2634: if (c != 0) { kpeter@2634: _cap[i] -= c; kpeter@2634: _supply[_source[i]] -= c; kpeter@2634: _supply[_target[i]] += c; kpeter@2634: } kpeter@2634: } kpeter@2634: } kpeter@2634: kpeter@2634: // Add artificial edges and initialize the spanning tree data structure deba@2440: Cost max_cost = std::numeric_limits::max() / 4; kpeter@2634: Capacity max_cap = std::numeric_limits::max(); kpeter@2634: for (int u = 0, e = _edge_num; u != _node_num; ++u, ++e) { kpeter@2575: _parent[u] = _root; kpeter@2634: _pred[u] = e; kpeter@2635: _thread[u] = u + 1; kpeter@2635: _rev_thread[u + 1] = u; kpeter@2635: _succ_num[u] = 1; kpeter@2635: _last_succ[u] = u; kpeter@2635: _cap[e] = max_cap; kpeter@2635: _state[e] = STATE_TREE; kpeter@2575: if (_supply[u] >= 0) { kpeter@2635: _forward[u] = true; kpeter@2635: _pi[u] = 0; kpeter@2635: _source[e] = u; kpeter@2635: _target[e] = _root; kpeter@2575: _flow[e] = _supply[u]; kpeter@2635: _cost[e] = 0; kpeter@2635: } kpeter@2635: else { kpeter@2575: _forward[u] = false; kpeter@2634: _pi[u] = max_cost; kpeter@2635: _source[e] = _root; kpeter@2635: _target[e] = u; kpeter@2635: _flow[e] = -_supply[u]; kpeter@2635: _cost[e] = max_cost; kpeter@2556: } deba@2440: } deba@2440: kpeter@2575: return true; deba@2440: } deba@2440: kpeter@2630: // Find the join node kpeter@2630: void findJoinNode() { kpeter@2634: int u = _source[_in_edge]; kpeter@2634: int v = _target[_in_edge]; kpeter@2575: while (u != v) { kpeter@2635: if (_succ_num[u] < _succ_num[v]) { kpeter@2635: u = _parent[u]; kpeter@2635: } else { kpeter@2635: v = _parent[v]; kpeter@2635: } deba@2440: } kpeter@2630: join = u; deba@2440: } deba@2440: kpeter@2634: // Find the leaving edge of the cycle and returns true if the kpeter@2630: // leaving edge is not the same as the entering edge kpeter@2630: bool findLeavingEdge() { kpeter@2630: // Initialize first and second nodes according to the direction deba@2440: // of the cycle kpeter@2575: if (_state[_in_edge] == STATE_LOWER) { kpeter@2634: first = _source[_in_edge]; kpeter@2634: second = _target[_in_edge]; deba@2440: } else { kpeter@2634: first = _target[_in_edge]; kpeter@2634: second = _source[_in_edge]; deba@2440: } kpeter@2634: delta = _cap[_in_edge]; kpeter@2634: int result = 0; deba@2440: Capacity d; kpeter@2634: int e; deba@2440: kpeter@2630: // Search the cycle along the path form the first node to the root kpeter@2634: for (int u = first; u != join; u = _parent[u]) { kpeter@2634: e = _pred[u]; kpeter@2634: d = _forward[u] ? _flow[e] : _cap[e] - _flow[e]; kpeter@2556: if (d < delta) { kpeter@2556: delta = d; kpeter@2556: u_out = u; kpeter@2634: result = 1; kpeter@2556: } deba@2440: } kpeter@2630: // Search the cycle along the path form the second node to the root kpeter@2634: for (int u = second; u != join; u = _parent[u]) { kpeter@2634: e = _pred[u]; kpeter@2634: d = _forward[u] ? _cap[e] - _flow[e] : _flow[e]; kpeter@2556: if (d <= delta) { kpeter@2556: delta = d; kpeter@2556: u_out = u; kpeter@2634: result = 2; kpeter@2556: } deba@2440: } kpeter@2634: kpeter@2634: if (result == 1) { kpeter@2634: u_in = first; kpeter@2634: v_in = second; kpeter@2634: } else { kpeter@2634: u_in = second; kpeter@2634: v_in = first; kpeter@2634: } kpeter@2634: return result != 0; deba@2440: } deba@2440: kpeter@2634: // Change _flow and _state vectors kpeter@2634: void changeFlow(bool change) { kpeter@2630: // Augment along the cycle deba@2440: if (delta > 0) { kpeter@2575: Capacity val = _state[_in_edge] * delta; kpeter@2575: _flow[_in_edge] += val; kpeter@2634: for (int u = _source[_in_edge]; u != join; u = _parent[u]) { kpeter@2634: _flow[_pred[u]] += _forward[u] ? -val : val; kpeter@2556: } kpeter@2634: for (int u = _target[_in_edge]; u != join; u = _parent[u]) { kpeter@2634: _flow[_pred[u]] += _forward[u] ? val : -val; kpeter@2556: } deba@2440: } kpeter@2630: // Update the state of the entering and leaving edges deba@2440: if (change) { kpeter@2575: _state[_in_edge] = STATE_TREE; kpeter@2634: _state[_pred[u_out]] = kpeter@2634: (_flow[_pred[u_out]] == 0) ? STATE_LOWER : STATE_UPPER; deba@2440: } else { kpeter@2575: _state[_in_edge] = -_state[_in_edge]; deba@2440: } deba@2440: } kpeter@2635: kpeter@2635: // Update the tree structure kpeter@2635: void updateTreeStructure() { kpeter@2635: int u, w; kpeter@2635: int old_rev_thread = _rev_thread[u_out]; kpeter@2635: int old_succ_num = _succ_num[u_out]; kpeter@2635: int old_last_succ = _last_succ[u_out]; kpeter@2575: v_out = _parent[u_out]; deba@2440: kpeter@2635: u = _last_succ[u_in]; // the last successor of u_in kpeter@2635: right = _thread[u]; // the node after it kpeter@2635: kpeter@2635: // Handle the case when old_rev_thread equals to v_in kpeter@2635: // (it also means that join and v_out coincide) kpeter@2635: if (old_rev_thread == v_in) { kpeter@2635: last = _thread[_last_succ[u_out]]; kpeter@2635: } else { kpeter@2635: last = _thread[v_in]; kpeter@2635: } kpeter@2635: kpeter@2635: // Update _thread and _parent along the stem nodes (i.e. the nodes kpeter@2635: // between u_in and u_out, whose parent have to be changed) kpeter@2635: _thread[v_in] = stem = u_in; kpeter@2635: _dirty_revs.clear(); kpeter@2635: _dirty_revs.push_back(v_in); kpeter@2635: par_stem = v_in; kpeter@2635: while (stem != u_out) { kpeter@2635: // Insert the next stem node into the thread list kpeter@2635: new_stem = _parent[stem]; kpeter@2635: _thread[u] = new_stem; kpeter@2635: _dirty_revs.push_back(u); kpeter@2635: kpeter@2635: // Remove the subtree of stem from the thread list kpeter@2635: w = _rev_thread[stem]; kpeter@2635: _thread[w] = right; kpeter@2635: _rev_thread[right] = w; kpeter@2635: kpeter@2635: // Change the parent node and shift stem nodes kpeter@2635: _parent[stem] = par_stem; kpeter@2635: par_stem = stem; kpeter@2635: stem = new_stem; kpeter@2635: kpeter@2635: // Update u and right nodes kpeter@2635: u = _last_succ[stem] == _last_succ[par_stem] ? kpeter@2635: _rev_thread[par_stem] : _last_succ[stem]; kpeter@2635: right = _thread[u]; kpeter@2635: } kpeter@2635: _parent[u_out] = par_stem; kpeter@2635: _last_succ[u_out] = u; kpeter@2635: _thread[u] = last; kpeter@2635: _rev_thread[last] = u; kpeter@2635: kpeter@2635: // Remove the subtree of u_out from the thread list except for kpeter@2635: // the case when old_rev_thread equals to v_in kpeter@2635: // (it also means that join and v_out coincide) kpeter@2635: if (old_rev_thread != v_in) { kpeter@2635: _thread[old_rev_thread] = right; kpeter@2635: _rev_thread[right] = old_rev_thread; kpeter@2635: } kpeter@2635: kpeter@2635: // Update _rev_thread using the new _thread values kpeter@2635: for (int i = 0; i < int(_dirty_revs.size()); ++i) { kpeter@2635: u = _dirty_revs[i]; kpeter@2635: _rev_thread[_thread[u]] = u; kpeter@2635: } kpeter@2635: kpeter@2635: // Update _last_succ for the stem nodes from u_out to u_in kpeter@2635: for (u = u_out; u != u_in; u = _parent[u]) { kpeter@2635: _last_succ[_parent[u]] = _last_succ[u]; kpeter@2635: } kpeter@2635: kpeter@2635: // Set limits for updating _last_succ form v_in and v_out kpeter@2635: // towards the root kpeter@2635: int up_limit_in = -1; kpeter@2635: int up_limit_out = -1; kpeter@2635: if (_last_succ[join] == v_in) { kpeter@2635: up_limit_out = join; kpeter@2635: } else { kpeter@2635: up_limit_in = join; kpeter@2635: } kpeter@2635: kpeter@2635: // Update _last_succ from v_in towards the root kpeter@2635: for (u = v_in; u != up_limit_in && _last_succ[u] == v_in; kpeter@2635: u = _parent[u]) { kpeter@2635: _last_succ[u] = _last_succ[u_out]; kpeter@2635: } kpeter@2635: // Update _last_succ from v_out towards the root kpeter@2635: if (join != old_rev_thread && v_in != old_rev_thread) { kpeter@2635: for (u = v_out; u != up_limit_out && _last_succ[u] == old_last_succ; kpeter@2635: u = _parent[u]) { kpeter@2635: _last_succ[u] = old_rev_thread; kpeter@2635: } kpeter@2635: } else { kpeter@2635: for (u = v_out; u != up_limit_out && _last_succ[u] == old_last_succ; kpeter@2635: u = _parent[u]) { kpeter@2635: _last_succ[u] = _last_succ[u_out]; kpeter@2556: } deba@2440: } deba@2440: kpeter@2635: // Update _pred and _forward for the stem nodes from u_out to u_in kpeter@2635: u = u_out; deba@2440: while (u != u_in) { kpeter@2635: w = _parent[u]; kpeter@2635: _pred[u] = _pred[w]; kpeter@2635: _forward[u] = !_forward[w]; kpeter@2635: u = w; deba@2440: } kpeter@2634: _pred[u_in] = _in_edge; kpeter@2634: _forward[u_in] = (u_in == _source[_in_edge]); kpeter@2635: kpeter@2635: // Update _succ_num from v_in to join kpeter@2635: for (u = v_in; u != join; u = _parent[u]) { kpeter@2635: _succ_num[u] += old_succ_num; kpeter@2635: } kpeter@2635: // Update _succ_num from v_out to join kpeter@2635: for (u = v_out; u != join; u = _parent[u]) { kpeter@2635: _succ_num[u] -= old_succ_num; kpeter@2635: } kpeter@2635: // Update _succ_num for the stem nodes from u_out to u_in kpeter@2635: int tmp = 0; kpeter@2635: u = u_out; kpeter@2635: while (u != u_in) { kpeter@2635: w = _parent[u]; kpeter@2635: tmp = _succ_num[u] - _succ_num[w] + tmp; kpeter@2635: _succ_num[u] = tmp; kpeter@2635: u = w; kpeter@2635: } kpeter@2635: _succ_num[u_in] = old_succ_num; deba@2440: } deba@2440: kpeter@2635: // Update potentials kpeter@2635: void updatePotential() { kpeter@2628: Cost sigma = _forward[u_in] ? kpeter@2634: _pi[v_in] - _pi[u_in] - _cost[_pred[u_in]] : kpeter@2634: _pi[v_in] - _pi[u_in] + _cost[_pred[u_in]]; kpeter@2635: // Update in the lower subtree (which has been moved) kpeter@2635: int end = _thread[_last_succ[u_in]]; kpeter@2635: for (int u = u_in; u != end; u = _thread[u]) { kpeter@2634: _pi[u] += sigma; deba@2440: } deba@2440: } deba@2440: kpeter@2630: // Execute the algorithm kpeter@2575: bool start(PivotRuleEnum pivot_rule) { kpeter@2630: // Select the pivot rule implementation kpeter@2575: switch (pivot_rule) { kpeter@2575: case FIRST_ELIGIBLE_PIVOT: kpeter@2575: return start(); kpeter@2575: case BEST_ELIGIBLE_PIVOT: kpeter@2575: return start(); kpeter@2575: case BLOCK_SEARCH_PIVOT: kpeter@2575: return start(); kpeter@2575: case CANDIDATE_LIST_PIVOT: kpeter@2575: return start(); kpeter@2619: case ALTERING_LIST_PIVOT: kpeter@2619: return start(); kpeter@2575: } kpeter@2575: return false; kpeter@2575: } kpeter@2575: kpeter@2575: template deba@2440: bool start() { kpeter@2634: PivotRuleImplementation pivot(*this); kpeter@2635: kpeter@2630: // Execute the network simplex algorithm kpeter@2575: while (pivot.findEnteringEdge()) { kpeter@2630: findJoinNode(); kpeter@2556: bool change = findLeavingEdge(); kpeter@2634: changeFlow(change); kpeter@2556: if (change) { kpeter@2635: updateTreeStructure(); kpeter@2635: updatePotential(); kpeter@2556: } deba@2440: } kpeter@2635: kpeter@2630: // Check if the flow amount equals zero on all the artificial edges kpeter@2634: for (int e = _edge_num; e != _edge_num + _node_num; ++e) { kpeter@2575: if (_flow[e] > 0) return false; kpeter@2634: } deba@2440: kpeter@2630: // Copy flow values to _flow_result kpeter@2634: if (_orig_lower) { kpeter@2634: for (int i = 0; i != _edge_num; ++i) { kpeter@2634: Edge e = _edge[i]; kpeter@2634: (*_flow_result)[e] = (*_orig_lower)[e] + _flow[i]; kpeter@2634: } deba@2440: } else { kpeter@2634: for (int i = 0; i != _edge_num; ++i) { kpeter@2634: (*_flow_result)[_edge[i]] = _flow[i]; kpeter@2634: } deba@2440: } kpeter@2630: // Copy potential values to _potential_result kpeter@2634: for (int i = 0; i != _node_num; ++i) { kpeter@2634: (*_potential_result)[_node[i]] = _pi[i]; kpeter@2634: } kpeter@2635: deba@2440: return true; deba@2440: } deba@2440: deba@2440: }; //class NetworkSimplex deba@2440: deba@2440: ///@} deba@2440: deba@2440: } //namespace lemon deba@2440: deba@2440: #endif //LEMON_NETWORK_SIMPLEX_H