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