COIN-OR::LEMON - Graph Library

Changeset 2556:74c2c81055e1 in lemon-0.x for lemon/network_simplex.h


Ignore:
Timestamp:
01/13/08 11:32:14 (16 years ago)
Author:
Peter Kovacs
Branch:
default
Phase:
public
Convert:
svn:c9d7d8f5-90d6-0310-b91f-818b3a526b0e/lemon/trunk@3437
Message:

Cleanup in the minimum cost flow files.
The changes only affects the documentation and the look of the source codes.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • lemon/network_simplex.h

    r2553 r2556  
    2323///
    2424/// \file
    25 /// \brief The network simplex algorithm for finding a minimum cost
    26 /// flow.
     25/// \brief The network simplex algorithm for finding a minimum cost flow.
    2726
    2827#include <limits>
     
    4140
    4241
    43 /// \brief State constant for edges at their lower bounds.
    44 #define LOWER   1
    45 /// \brief State constant for edges in the spanning tree.
    46 #define TREE    0
    47 /// \brief State constant for edges at their upper bounds.
    48 #define UPPER   -1
     42// State constant for edges at their lower bounds.
     43#define LOWER   1
     44// State constant for edges in the spanning tree.
     45#define TREE    0
     46// State constant for edges at their upper bounds.
     47#define UPPER   -1
    4948
    5049#ifdef EDGE_BLOCK_PIVOT
    5150  #include <cmath>
    52   /// \brief Lower bound for the size of blocks.
    53   #define MIN_BLOCK_SIZE        10
     51  #define MIN_BLOCK_SIZE        10
    5452#endif
    5553
    5654#ifdef CANDIDATE_LIST_PIVOT
    5755  #include <vector>
    58   #define LIST_LENGTH_DIV           50
    59   #define MINOR_LIMIT_DIV           200
     56  #define LIST_LENGTH_DIV       50
     57  #define MINOR_LIMIT_DIV       200
    6058#endif
    6159
     
    6462  #include <algorithm>
    6563  #define LIST_LENGTH_DIV       100
    66   #define LOWER_DIV             4
     64  #define LOWER_DIV             4
    6765#endif
    6866
     
    7573  /// finding a minimum cost flow.
    7674  ///
    77   /// \ref lemon::NetworkSimplex "NetworkSimplex" implements the
    78   /// network simplex algorithm for finding a minimum cost flow.
     75  /// \ref NetworkSimplex implements the network simplex algorithm for
     76  /// finding a minimum cost flow.
    7977  ///
    8078  /// \param Graph The directed graph type the algorithm runs on.
     
    8583  ///
    8684  /// \warning
    87   /// - Edge capacities and costs should be nonnegative integers.
    88   ///   However \c CostMap::Value should be signed type.
     85  /// - Edge capacities and costs should be non-negative integers.
     86  ///   However \c CostMap::Value should be signed type.
    8987  /// - Supply values should be signed integers.
    9088  /// - \c LowerMap::Value must be convertible to
    91   ///   \c CapacityMap::Value and \c CapacityMap::Value must be
    92   ///   convertible to \c SupplyMap::Value.
     89  ///   \c CapacityMap::Value and \c CapacityMap::Value must be
     90  ///   convertible to \c SupplyMap::Value.
    9391  ///
    9492  /// \author Peter Kovacs
     
    108106
    109107    typedef SmartGraph SGraph;
    110     typedef typename SGraph::Node Node;
    111     typedef typename SGraph::NodeIt NodeIt;
    112     typedef typename SGraph::Edge Edge;
    113     typedef typename SGraph::EdgeIt EdgeIt;
    114     typedef typename SGraph::InEdgeIt InEdgeIt;
    115     typedef typename SGraph::OutEdgeIt OutEdgeIt;
     108    GRAPH_TYPEDEFS(typename SGraph);
    116109
    117110    typedef typename SGraph::template EdgeMap<Lower> SLowerMap;
     
    132125  public:
    133126
    134     /// \brief The type of the flow map.
     127    /// The type of the flow map.
    135128    typedef typename Graph::template EdgeMap<Capacity> FlowMap;
    136     /// \brief The type of the potential map.
     129    /// The type of the potential map.
    137130    typedef typename Graph::template NodeMap<Cost> PotentialMap;
    138131
    139132  protected:
    140133
    141     /// \brief Map adaptor class for handling reduced edge costs.
     134    /// Map adaptor class for handling reduced edge costs.
    142135    class ReducedCostMap : public MapBase<Edge, Cost>
    143136    {
     
    151144
    152145      ReducedCostMap( const SGraph &_gr,
    153                       const SCostMap &_cm,
    154                       const SPotentialMap &_pm ) :
    155         gr(_gr), cost_map(_cm), pot_map(_pm) {}
     146                      const SCostMap &_cm,
     147                      const SPotentialMap &_pm ) :
     148        gr(_gr), cost_map(_cm), pot_map(_pm) {}
    156149
    157150      Cost operator[](const Edge &e) const {
    158         return cost_map[e] - pot_map[gr.source(e)] + pot_map[gr.target(e)];
     151        return cost_map[e] - pot_map[gr.source(e)] + pot_map[gr.target(e)];
    159152      }
    160153
     
    163156  protected:
    164157
    165     /// \brief The directed graph the algorithm runs on.
     158    /// The directed graph the algorithm runs on.
    166159    SGraph graph;
    167     /// \brief The original graph.
     160    /// The original graph.
    168161    const Graph &graph_ref;
    169     /// \brief The original lower bound map.
     162    /// The original lower bound map.
    170163    const LowerMap *lower;
    171     /// \brief The capacity map.
     164    /// The capacity map.
    172165    SCapacityMap capacity;
    173     /// \brief The cost map.
     166    /// The cost map.
    174167    SCostMap cost;
    175     /// \brief The supply map.
     168    /// The supply map.
    176169    SSupplyMap supply;
    177     /// \brief The reduced cost map.
     170    /// The reduced cost map.
    178171    ReducedCostMap red_cost;
    179     /// \brief The sum of supply values equals zero.
    180172    bool valid_supply;
    181173
    182     /// \brief The edge map of the current flow.
     174    /// The edge map of the current flow.
    183175    SCapacityMap flow;
    184     /// \brief The edge map of the found flow on the original graph.
     176    /// The potential node map.
     177    SPotentialMap potential;
    185178    FlowMap flow_result;
    186     /// \brief The potential node map.
    187     SPotentialMap potential;
    188     /// \brief The potential node map on the original graph.
    189179    PotentialMap potential_result;
    190180
    191     /// \brief Node reference for the original graph.
     181    /// Node reference for the original graph.
    192182    NodeRefMap node_ref;
    193     /// \brief Edge reference for the original graph.
     183    /// Edge reference for the original graph.
    194184    EdgeRefMap edge_ref;
    195185
    196     /// \brief The depth node map of the spanning tree structure.
     186    /// The \c depth node map of the spanning tree structure.
    197187    IntNodeMap depth;
    198     /// \brief The parent node map of the spanning tree structure.
     188    /// The \c parent node map of the spanning tree structure.
    199189    NodeNodeMap parent;
    200     /// \brief The pred_edge node map of the spanning tree structure.
     190    /// The \c pred_edge node map of the spanning tree structure.
    201191    EdgeNodeMap pred_edge;
    202     /// \brief The thread node map of the spanning tree structure.
     192    /// The \c thread node map of the spanning tree structure.
    203193    NodeNodeMap thread;
    204     /// \brief The forward node map of the spanning tree structure.
     194    /// The \c forward node map of the spanning tree structure.
    205195    BoolNodeMap forward;
    206     /// \brief The state edge map.
     196    /// The state edge map.
    207197    IntEdgeMap state;
    208 
     198    /// The root node of the starting spanning tree.
     199    Node root;
     200
     201    // The entering edge of the current pivot iteration.
     202    Edge in_edge;
     203    // Temporary nodes used in the current pivot iteration.
     204    Node join, u_in, v_in, u_out, v_out;
     205    Node right, first, second, last;
     206    Node stem, par_stem, new_stem;
     207    // The maximum augment amount along the found cycle in the current
     208    // pivot iteration.
     209    Capacity delta;
    209210
    210211#ifdef EDGE_BLOCK_PIVOT
    211     /// \brief The size of blocks for the "Edge Block" pivot rule.
     212    /// The size of blocks for the "Edge Block" pivot rule.
    212213    int block_size;
    213214#endif
     
    235236#endif
    236237
    237     // Root node of the starting spanning tree.
    238     Node root;
    239     // The entering edge of the current pivot iteration.
    240     Edge in_edge;
    241     // Temporary nodes used in the current pivot iteration.
    242     Node join, u_in, v_in, u_out, v_out;
    243     Node right, first, second, last;
    244     Node stem, par_stem, new_stem;
    245     // The maximum augment amount along the cycle in the current pivot
    246     // iteration.
    247     Capacity delta;
    248 
    249238  public :
    250239
     
    259248    /// \param _supply The supply values of the nodes (signed).
    260249    NetworkSimplex( const Graph &_graph,
    261                     const LowerMap &_lower,
    262                     const CapacityMap &_capacity,
    263                     const CostMap &_cost,
    264                     const SupplyMap &_supply ) :
     250                    const LowerMap &_lower,
     251                    const CapacityMap &_capacity,
     252                    const CostMap &_cost,
     253                    const SupplyMap &_supply ) :
    265254      graph_ref(_graph), lower(&_lower), capacity(graph), cost(graph),
    266255      supply(graph), flow(graph), flow_result(_graph), potential(graph),
     
    273262      Supply sum = 0;
    274263      for (typename Graph::NodeIt n(graph_ref); n != INVALID; ++n)
    275         sum += _supply[n];
     264        sum += _supply[n];
    276265      if (!(valid_supply = sum == 0)) return;
    277266
     
    280269      graph.reserveEdge(countEdges(graph_ref) + countNodes(graph_ref));
    281270      copyGraph(graph, graph_ref)
    282         .edgeMap(cost, _cost)
    283         .nodeRef(node_ref)
    284         .edgeRef(edge_ref)
    285         .run();
    286 
    287       // Removing nonzero lower bounds
     271        .edgeMap(cost, _cost)
     272        .nodeRef(node_ref)
     273        .edgeRef(edge_ref)
     274        .run();
     275
     276      // Removing non-zero lower bounds
    288277      for (typename Graph::EdgeIt e(graph_ref); e != INVALID; ++e) {
    289         capacity[edge_ref[e]] = _capacity[e] - _lower[e];
     278        capacity[edge_ref[e]] = _capacity[e] - _lower[e];
    290279      }
    291280      for (typename Graph::NodeIt n(graph_ref); n != INVALID; ++n) {
    292         Supply s = _supply[n];
    293         for (typename Graph::InEdgeIt e(graph_ref, n); e != INVALID; ++e)
    294           s += _lower[e];
    295         for (typename Graph::OutEdgeIt e(graph_ref, n); e != INVALID; ++e)
    296           s -= _lower[e];
    297         supply[node_ref[n]] = s;
     281        Supply s = _supply[n];
     282        for (typename Graph::InEdgeIt e(graph_ref, n); e != INVALID; ++e)
     283          s += _lower[e];
     284        for (typename Graph::OutEdgeIt e(graph_ref, n); e != INVALID; ++e)
     285          s -= _lower[e];
     286        supply[node_ref[n]] = s;
    298287      }
    299288    }
     
    308297    /// \param _supply The supply values of the nodes (signed).
    309298    NetworkSimplex( const Graph &_graph,
    310                     const CapacityMap &_capacity,
    311                     const CostMap &_cost,
    312                     const SupplyMap &_supply ) :
     299                    const CapacityMap &_capacity,
     300                    const CostMap &_cost,
     301                    const SupplyMap &_supply ) :
    313302      graph_ref(_graph), lower(NULL), capacity(graph), cost(graph),
    314303      supply(graph), flow(graph), flow_result(_graph), potential(graph),
     
    321310      Supply sum = 0;
    322311      for (typename Graph::NodeIt n(graph_ref); n != INVALID; ++n)
    323         sum += _supply[n];
     312        sum += _supply[n];
    324313      if (!(valid_supply = sum == 0)) return;
    325314
    326315      // Copying graph_ref to graph
    327316      copyGraph(graph, graph_ref)
    328         .edgeMap(capacity, _capacity)
    329         .edgeMap(cost, _cost)
    330         .nodeMap(supply, _supply)
    331         .nodeRef(node_ref)
    332         .edgeRef(edge_ref)
    333         .run();
     317        .edgeMap(capacity, _capacity)
     318        .edgeMap(cost, _cost)
     319        .nodeMap(supply, _supply)
     320        .nodeRef(node_ref)
     321        .edgeRef(edge_ref)
     322        .run();
    334323    }
    335324
     
    345334    /// \param _t The target node.
    346335    /// \param _flow_value The required amount of flow from node \c _s
    347     /// to node \c _t (i.e. the supply of \c _s and the demand of
    348     /// \c _t).
     336    /// to node \c _t (i.e. the supply of \c _s and the demand of \c _t).
    349337    NetworkSimplex( const Graph &_graph,
    350                     const LowerMap &_lower,
    351                     const CapacityMap &_capacity,
    352                     const CostMap &_cost,
    353                     typename Graph::Node _s,
    354                     typename Graph::Node _t,
    355                     typename SupplyMap::Value _flow_value ) :
     338                    const LowerMap &_lower,
     339                    const CapacityMap &_capacity,
     340                    const CostMap &_cost,
     341                    typename Graph::Node _s,
     342                    typename Graph::Node _t,
     343                    typename SupplyMap::Value _flow_value ) :
    356344      graph_ref(_graph), lower(&_lower), capacity(graph), cost(graph),
    357345      supply(graph), flow(graph), flow_result(_graph), potential(graph),
     
    363351      // Copying graph_ref to graph
    364352      copyGraph(graph, graph_ref)
    365         .edgeMap(cost, _cost)
    366         .nodeRef(node_ref)
    367         .edgeRef(edge_ref)
    368         .run();
    369 
    370       // Removing nonzero lower bounds
     353        .edgeMap(cost, _cost)
     354        .nodeRef(node_ref)
     355        .edgeRef(edge_ref)
     356        .run();
     357
     358      // Removing non-zero lower bounds
    371359      for (typename Graph::EdgeIt e(graph_ref); e != INVALID; ++e) {
    372         capacity[edge_ref[e]] = _capacity[e] - _lower[e];
     360        capacity[edge_ref[e]] = _capacity[e] - _lower[e];
    373361      }
    374362      for (typename Graph::NodeIt n(graph_ref); n != INVALID; ++n) {
    375         Supply s = 0;
    376         if (n == _s) s =  _flow_value;
    377         if (n == _t) s = -_flow_value;
    378         for (typename Graph::InEdgeIt e(graph_ref, n); e != INVALID; ++e)
    379           s += _lower[e];
    380         for (typename Graph::OutEdgeIt e(graph_ref, n); e != INVALID; ++e)
    381           s -= _lower[e];
    382         supply[node_ref[n]] = s;
     363        Supply s = 0;
     364        if (n == _s) s =  _flow_value;
     365        if (n == _t) s = -_flow_value;
     366        for (typename Graph::InEdgeIt e(graph_ref, n); e != INVALID; ++e)
     367          s += _lower[e];
     368        for (typename Graph::OutEdgeIt e(graph_ref, n); e != INVALID; ++e)
     369          s -= _lower[e];
     370        supply[node_ref[n]] = s;
    383371      }
    384372      valid_supply = true;
     
    395383    /// \param _t The target node.
    396384    /// \param _flow_value The required amount of flow from node \c _s
    397     /// to node \c _t (i.e. the supply of \c _s and the demand of
    398     /// \c _t).
     385    /// to node \c _t (i.e. the supply of \c _s and the demand of \c _t).
    399386    NetworkSimplex( const Graph &_graph,
    400                     const CapacityMap &_capacity,
    401                     const CostMap &_cost,
    402                     typename Graph::Node _s,
    403                     typename Graph::Node _t,
    404                     typename SupplyMap::Value _flow_value ) :
     387                    const CapacityMap &_capacity,
     388                    const CostMap &_cost,
     389                    typename Graph::Node _s,
     390                    typename Graph::Node _t,
     391                    typename SupplyMap::Value _flow_value ) :
    405392      graph_ref(_graph), lower(NULL), capacity(graph), cost(graph),
    406393      supply(graph, 0), flow(graph), flow_result(_graph), potential(graph),
     
    412399      // Copying graph_ref to graph
    413400      copyGraph(graph, graph_ref)
    414         .edgeMap(capacity, _capacity)
    415         .edgeMap(cost, _cost)
    416         .nodeRef(node_ref)
    417         .edgeRef(edge_ref)
    418         .run();
     401        .edgeMap(capacity, _capacity)
     402        .edgeMap(cost, _cost)
     403        .nodeRef(node_ref)
     404        .edgeRef(edge_ref)
     405        .run();
    419406      supply[node_ref[_s]] =  _flow_value;
    420407      supply[node_ref[_t]] = -_flow_value;
    421408      valid_supply = true;
     409    }
     410
     411    /// \brief Runs the algorithm.
     412    ///
     413    /// Runs the algorithm.
     414    ///
     415    /// \return \c true if a feasible flow can be found.
     416    bool run() {
     417      return init() && start();
    422418    }
    423419
     
    451447      Cost c = 0;
    452448      for (typename Graph::EdgeIt e(graph_ref); e != INVALID; ++e)
    453         c += flow_result[e] * cost[edge_ref[e]];
     449        c += flow_result[e] * cost[edge_ref[e]];
    454450      return c;
    455     }
    456 
    457     /// \brief Runs the algorithm.
    458     ///
    459     /// Runs the algorithm.
    460     ///
    461     /// \return \c true if a feasible flow can be found.
    462     bool run() {
    463       return init() && start();
    464451    }
    465452
     
    473460      // Initializing state and flow maps
    474461      for (EdgeIt e(graph); e != INVALID; ++e) {
    475         flow[e] = 0;
    476         state[e] = LOWER;
     462        flow[e] = 0;
     463        state[e] = LOWER;
    477464      }
    478465
     
    492479      Cost max_cost = std::numeric_limits<Cost>::max() / 4;
    493480      for (NodeIt u(graph); u != INVALID; ++u) {
    494         if (u == root) continue;
    495         thread[last] = u;
    496         last = u;
    497         parent[u] = root;
    498         depth[u] = 1;
    499         sum += supply[u];
    500         if (supply[u] >= 0) {
    501           e = graph.addEdge(u, root);
    502           flow[e] = supply[u];
    503           forward[u] = true;
    504           potential[u] = max_cost;
    505         } else {
    506           e = graph.addEdge(root, u);
    507           flow[e] = -supply[u];
    508           forward[u] = false;
    509           potential[u] = -max_cost;
    510         }
    511         cost[e] = max_cost;
    512         capacity[e] = std::numeric_limits<Capacity>::max();
    513         state[e] = TREE;
    514         pred_edge[u] = e;
     481        if (u == root) continue;
     482        thread[last] = u;
     483        last = u;
     484        parent[u] = root;
     485        depth[u] = 1;
     486        sum += supply[u];
     487        if (supply[u] >= 0) {
     488          e = graph.addEdge(u, root);
     489          flow[e] = supply[u];
     490          forward[u] = true;
     491          potential[u] = max_cost;
     492        } else {
     493          e = graph.addEdge(root, u);
     494          flow[e] = -supply[u];
     495          forward[u] = false;
     496          potential[u] = -max_cost;
     497        }
     498        cost[e] = max_cost;
     499        capacity[e] = std::numeric_limits<Capacity>::max();
     500        state[e] = TREE;
     501        pred_edge[u] = e;
    515502      }
    516503      thread[last] = root;
     
    521508      block_size = 2 * int(sqrt(countEdges(graph)));
    522509      if (block_size < MIN_BLOCK_SIZE) block_size = MIN_BLOCK_SIZE;
    523 //      block_size = edge_num >= BLOCK_NUM * MIN_BLOCK_SIZE ?
    524 //                   edge_num / BLOCK_NUM : MIN_BLOCK_SIZE;
    525510#endif
    526511#ifdef CANDIDATE_LIST_PIVOT
     
    544529    bool findEnteringEdge(EdgeIt &next_edge) {
    545530      for (EdgeIt e = next_edge; e != INVALID; ++e) {
    546         if (state[e] * red_cost[e] < 0) {
    547           in_edge = e;
    548           next_edge = ++e;
    549           return true;
    550         }
     531        if (state[e] * red_cost[e] < 0) {
     532          in_edge = e;
     533          next_edge = ++e;
     534          return true;
     535        }
    551536      }
    552537      for (EdgeIt e(graph); e != next_edge; ++e) {
    553         if (state[e] * red_cost[e] < 0) {
    554           in_edge = e;
    555           next_edge = ++e;
    556           return true;
    557         }
     538        if (state[e] * red_cost[e] < 0) {
     539          in_edge = e;
     540          next_edge = ++e;
     541          return true;
     542        }
    558543      }
    559544      return false;
     
    567552      Cost min = 0;
    568553      for (EdgeIt e(graph); e != INVALID; ++e) {
    569         if (state[e] * red_cost[e] < min) {
    570           min = state[e] * red_cost[e];
    571           in_edge = e;
    572         }
     554        if (state[e] * red_cost[e] < min) {
     555          min = state[e] * red_cost[e];
     556          in_edge = e;
     557        }
    573558      }
    574559      return min < 0;
     
    585570      int cnt = 0;
    586571      for (EdgeIt e = next_edge; e != INVALID; ++e) {
    587         if ((curr = state[e] * red_cost[e]) < min) {
    588           min = curr;
    589           min_edge = e;
    590         }
    591         if (++cnt == block_size) {
    592           if (min < 0) break;
    593           cnt = 0;
    594         }
     572        if ((curr = state[e] * red_cost[e]) < min) {
     573          min = curr;
     574          min_edge = e;
     575        }
     576        if (++cnt == block_size) {
     577          if (min < 0) break;
     578          cnt = 0;
     579        }
    595580      }
    596581      if (!(min < 0)) {
    597         for (EdgeIt e(graph); e != next_edge; ++e) {
    598           if ((curr = state[e] * red_cost[e]) < min) {
    599             min = curr;
    600             min_edge = e;
    601           }
    602           if (++cnt == block_size) {
    603             if (min < 0) break;
    604             cnt = 0;
    605           }
    606         }
     582        for (EdgeIt e(graph); e != next_edge; ++e) {
     583          if ((curr = state[e] * red_cost[e]) < min) {
     584            min = curr;
     585            min_edge = e;
     586          }
     587          if (++cnt == block_size) {
     588            if (min < 0) break;
     589            cnt = 0;
     590          }
     591        }
    607592      }
    608593      in_edge = min_edge;
    609594      if ((next_edge = ++min_edge) == INVALID)
    610         next_edge = EdgeIt(graph);
     595        next_edge = EdgeIt(graph);
    611596      return min < 0;
    612597    }
     
    620605
    621606      if (minor_count >= minor_limit || candidates.size() == 0) {
    622         // Major iteration
    623         candidates.clear();
    624         for (EdgeIt e(graph); e != INVALID; ++e) {
    625           if (state[e] * red_cost[e] < 0) {
    626             candidates.push_back(e);
    627             if (candidates.size() == list_length) break;
    628           }
    629         }
    630         if (candidates.size() == 0) return false;
     607        // Major iteration
     608        candidates.clear();
     609        for (EdgeIt e(graph); e != INVALID; ++e) {
     610          if (state[e] * red_cost[e] < 0) {
     611            candidates.push_back(e);
     612            if (candidates.size() == list_length) break;
     613          }
     614        }
     615        if (candidates.size() == 0) return false;
    631616      }
    632617
     
    637622      for (int i = 0; i < candidates.size(); ++i) {
    638623        e = candidates[i];
    639         if (state[e] * red_cost[e] < min) {
    640           min = state[e] * red_cost[e];
    641           in_edge = e;
    642         }
     624        if (state[e] * red_cost[e] < min) {
     625          min = state[e] * red_cost[e];
     626          in_edge = e;
     627        }
    643628      }
    644629      return true;
     
    656641    public:
    657642      SortFunc(const IntEdgeMap &_st, const ReducedCostMap &_rc) :
    658         st(_st), rc(_rc) {}
     643        st(_st), rc(_rc) {}
    659644      bool operator()(const Edge &e1, const Edge &e2) {
    660         return st[e1] * rc[e1] < st[e2] * rc[e2];
     645        return st[e1] * rc[e1] < st[e2] * rc[e2];
    661646      }
    662647    };
     
    669654      // Minor iteration
    670655      while (list_index < candidates.size()) {
    671         in_edge = candidates[list_index++];
    672         if (state[in_edge] * red_cost[in_edge] < 0) return true;
     656        in_edge = candidates[list_index++];
     657        if (state[in_edge] * red_cost[in_edge] < 0) return true;
    673658      }
    674659
     
    677662      Cost curr, min = 0;
    678663      for (EdgeIt e(graph); e != INVALID; ++e) {
    679         if ((curr = state[e] * red_cost[e]) < min / LOWER_DIV) {
    680           candidates.push_back(e);
    681           if (curr < min) min = curr;
    682           if (candidates.size() == list_length) break;
    683         }
     664        if ((curr = state[e] * red_cost[e]) < min / LOWER_DIV) {
     665          candidates.push_back(e);
     666          if (curr < min) min = curr;
     667          if (candidates.size() == list_length) break;
     668        }
    684669      }
    685670      if (candidates.size() == 0) return false;
     
    697682      Node v = graph.target(in_edge);
    698683      while (u != v) {
    699         if (depth[u] == depth[v]) {
    700           u = parent[u];
    701           v = parent[v];
    702         }
    703         else if (depth[u] > depth[v]) u = parent[u];
    704         else v = parent[v];
     684        if (depth[u] == depth[v]) {
     685          u = parent[u];
     686          v = parent[v];
     687        }
     688        else if (depth[u] > depth[v]) u = parent[u];
     689        else v = parent[v];
    705690      }
    706691      return u;
     
    713698      // of the cycle
    714699      if (state[in_edge] == LOWER) {
    715         first = graph.source(in_edge);
    716         second  = graph.target(in_edge);
     700        first = graph.source(in_edge);
     701        second  = graph.target(in_edge);
    717702      } else {
    718         first = graph.target(in_edge);
    719         second  = graph.source(in_edge);
     703        first = graph.target(in_edge);
     704        second  = graph.source(in_edge);
    720705      }
    721706      delta = capacity[in_edge];
     
    727712      // root node
    728713      for (Node u = first; u != join; u = parent[u]) {
    729         e = pred_edge[u];
    730         d = forward[u] ? flow[e] : capacity[e] - flow[e];
    731         if (d < delta) {
    732           delta = d;
    733           u_out = u;
    734           u_in = first;
    735           v_in = second;
    736           result = true;
    737         }
     714        e = pred_edge[u];
     715        d = forward[u] ? flow[e] : capacity[e] - flow[e];
     716        if (d < delta) {
     717          delta = d;
     718          u_out = u;
     719          u_in = first;
     720          v_in = second;
     721          result = true;
     722        }
    738723      }
    739724      // Searching the cycle along the path form the second node to the
    740725      // root node
    741726      for (Node u = second; u != join; u = parent[u]) {
    742         e = pred_edge[u];
    743         d = forward[u] ? capacity[e] - flow[e] : flow[e];
    744         if (d <= delta) {
    745           delta = d;
    746           u_out = u;
    747           u_in = second;
    748           v_in = first;
    749           result = true;
    750         }
     727        e = pred_edge[u];
     728        d = forward[u] ? capacity[e] - flow[e] : flow[e];
     729        if (d <= delta) {
     730          delta = d;
     731          u_out = u;
     732          u_in = second;
     733          v_in = first;
     734          result = true;
     735        }
    751736      }
    752737      return result;
    753738    }
    754739
    755     /// \brief Changes flow and state edge maps.
     740    /// \brief Changes \c flow and \c state edge maps.
    756741    void changeFlows(bool change) {
    757742      // Augmenting along the cycle
    758743      if (delta > 0) {
    759         Capacity val = state[in_edge] * delta;
    760         flow[in_edge] += val;
    761         for (Node u = graph.source(in_edge); u != join; u = parent[u]) {
    762           flow[pred_edge[u]] += forward[u] ? -val : val;
    763         }
    764         for (Node u = graph.target(in_edge); u != join; u = parent[u]) {
    765           flow[pred_edge[u]] += forward[u] ? val : -val;
    766         }
     744        Capacity val = state[in_edge] * delta;
     745        flow[in_edge] += val;
     746        for (Node u = graph.source(in_edge); u != join; u = parent[u]) {
     747          flow[pred_edge[u]] += forward[u] ? -val : val;
     748        }
     749        for (Node u = graph.target(in_edge); u != join; u = parent[u]) {
     750          flow[pred_edge[u]] += forward[u] ? val : -val;
     751        }
    767752      }
    768753      // Updating the state of the entering and leaving edges
    769754      if (change) {
    770         state[in_edge] = TREE;
    771         state[pred_edge[u_out]] =
    772           (flow[pred_edge[u_out]] == 0) ? LOWER : UPPER;
     755        state[in_edge] = TREE;
     756        state[pred_edge[u_out]] =
     757          (flow[pred_edge[u_out]] == 0) ? LOWER : UPPER;
    773758      } else {
    774         state[in_edge] = -state[in_edge];
    775       }
    776     }
    777 
    778     /// \brief Updates thread and parent node maps.
     759        state[in_edge] = -state[in_edge];
     760      }
     761    }
     762
     763    /// \brief Updates \c thread and \c parent node maps.
    779764    void updateThreadParent() {
    780765      Node u;
     
    784769      bool par_first = false;
    785770      if (join == v_out) {
    786         for (u = join; u != u_in && u != v_in; u = thread[u]) ;
    787         if (u == v_in) {
    788           par_first = true;
    789           while (thread[u] != u_out) u = thread[u];
    790           first = u;
    791         }
     771        for (u = join; u != u_in && u != v_in; u = thread[u]) ;
     772        if (u == v_in) {
     773          par_first = true;
     774          while (thread[u] != u_out) u = thread[u];
     775          first = u;
     776        }
    792777      }
    793778
     
    797782      right = thread[u];
    798783      if (thread[v_in] == u_out) {
    799         for (last = u; depth[last] > depth[u_out]; last = thread[last]) ;
    800         if (last == u_out) last = thread[last];
     784        for (last = u; depth[last] > depth[u_out]; last = thread[last]) ;
     785        if (last == u_out) last = thread[last];
    801786      }
    802787      else last = thread[v_in];
     
    806791      par_stem = v_in;
    807792      while (stem != u_out) {
    808         thread[u] = new_stem = parent[stem];
    809 
    810         // Finding the node just before the stem node (u) according to
    811         // the original thread index
    812         for (u = new_stem; thread[u] != stem; u = thread[u]) ;
    813         thread[u] = right;
    814 
    815         // Changing the parent node of stem and shifting stem and
    816         // par_stem nodes
    817         parent[stem] = par_stem;
    818         par_stem = stem;
    819         stem = new_stem;
    820 
    821         // Finding the last successor of stem (u) and the node after it
    822         // (right) according to the thread index
    823         for (u = stem; depth[thread[u]] > depth[stem]; u = thread[u]) ;
    824         right = thread[u];
     793        thread[u] = new_stem = parent[stem];
     794
     795        // Finding the node just before the stem node (u) according to
     796        // the original thread index
     797        for (u = new_stem; thread[u] != stem; u = thread[u]) ;
     798        thread[u] = right;
     799
     800        // Changing the parent node of stem and shifting stem and
     801        // par_stem nodes
     802        parent[stem] = par_stem;
     803        par_stem = stem;
     804        stem = new_stem;
     805
     806        // Finding the last successor of stem (u) and the node after it
     807        // (right) according to the thread index
     808        for (u = stem; depth[thread[u]] > depth[stem]; u = thread[u]) ;
     809        right = thread[u];
    825810      }
    826811      parent[u_out] = par_stem;
     
    828813
    829814      if (join == v_out && par_first) {
    830         if (first != v_in) thread[first] = right;
     815        if (first != v_in) thread[first] = right;
    831816      } else {
    832         for (u = v_out; thread[u] != u_out; u = thread[u]) ;
    833         thread[u] = right;
    834       }
    835     }
    836 
    837     /// \brief Updates pred_edge and forward node maps.
     817        for (u = v_out; thread[u] != u_out; u = thread[u]) ;
     818        thread[u] = right;
     819      }
     820    }
     821
     822    /// \brief Updates \c pred_edge and \c forward node maps.
    838823    void updatePredEdge() {
    839824      Node u = u_out, v;
    840825      while (u != u_in) {
    841         v = parent[u];
    842         pred_edge[u] = pred_edge[v];
    843         forward[u] = !forward[v];
    844         u = v;
     826        v = parent[u];
     827        pred_edge[u] = pred_edge[v];
     828        forward[u] = !forward[v];
     829        u = v;
    845830      }
    846831      pred_edge[u_in] = in_edge;
     
    848833    }
    849834
    850     /// \brief Updates depth and potential node maps.
     835    /// \brief Updates \c depth and \c potential node maps.
    851836    void updateDepthPotential() {
    852837      depth[u_in] = depth[v_in] + 1;
    853838      potential[u_in] = forward[u_in] ?
    854         potential[v_in] + cost[pred_edge[u_in]] :
    855         potential[v_in] - cost[pred_edge[u_in]];
     839        potential[v_in] + cost[pred_edge[u_in]] :
     840        potential[v_in] - cost[pred_edge[u_in]];
    856841
    857842      Node u = thread[u_in], v;
    858843      while (true) {
    859         v = parent[u];
    860         if (v == INVALID) break;
    861         depth[u] = depth[v] + 1;
    862         potential[u] = forward[u] ?
    863           potential[v] + cost[pred_edge[u]] :
    864           potential[v] - cost[pred_edge[u]];
    865         if (depth[u] <= depth[v_in]) break;
    866         u = thread[u];
     844        v = parent[u];
     845        if (v == INVALID) break;
     846        depth[u] = depth[v] + 1;
     847        potential[u] = forward[u] ?
     848          potential[v] + cost[pred_edge[u]] :
     849          potential[v] - cost[pred_edge[u]];
     850        if (depth[u] <= depth[v_in]) break;
     851        u = thread[u];
    867852      }
    868853    }
     
    881866#endif
    882867      {
    883         join = findJoinNode();
    884         bool change = findLeavingEdge();
    885         changeFlows(change);
    886         if (change) {
    887           updateThreadParent();
    888           updatePredEdge();
    889           updateDepthPotential();
    890         }
     868        join = findJoinNode();
     869        bool change = findLeavingEdge();
     870        changeFlows(change);
     871        if (change) {
     872          updateThreadParent();
     873          updatePredEdge();
     874          updateDepthPotential();
     875        }
    891876#ifdef _DEBUG_ITER_
    892         ++iter_num;
     877        ++iter_num;
    893878#endif
    894879      }
     
    896881#ifdef _DEBUG_ITER_
    897882      std::cout << "Network Simplex algorithm finished. " << iter_num
    898                 << " pivot iterations performed." << std::endl;
     883                << " pivot iterations performed." << std::endl;
    899884#endif
    900885
     
    902887      // artificial edges
    903888      for (InEdgeIt e(graph, root); e != INVALID; ++e)
    904         if (flow[e] > 0) return false;
     889        if (flow[e] > 0) return false;
    905890      for (OutEdgeIt e(graph, root); e != INVALID; ++e)
    906         if (flow[e] > 0) return false;
     891        if (flow[e] > 0) return false;
    907892
    908893      // Copying flow values to flow_result
    909894      if (lower) {
    910         for (typename Graph::EdgeIt e(graph_ref); e != INVALID; ++e)
    911           flow_result[e] = (*lower)[e] + flow[edge_ref[e]];
     895        for (typename Graph::EdgeIt e(graph_ref); e != INVALID; ++e)
     896          flow_result[e] = (*lower)[e] + flow[edge_ref[e]];
    912897      } else {
    913         for (typename Graph::EdgeIt e(graph_ref); e != INVALID; ++e)
    914           flow_result[e] = flow[edge_ref[e]];
     898        for (typename Graph::EdgeIt e(graph_ref); e != INVALID; ++e)
     899          flow_result[e] = flow[edge_ref[e]];
    915900      }
    916901      // Copying potential values to potential_result
    917902      for (typename Graph::NodeIt n(graph_ref); n != INVALID; ++n)
    918         potential_result[n] = potential[node_ref[n]];
     903        potential_result[n] = potential[node_ref[n]];
    919904
    920905      return true;
Note: See TracChangeset for help on using the changeset viewer.