lemon/capacity_scaling.h
changeset 964 7fdaa05a69a1
parent 863 a93f1a27d831
child 1004 1e87c18cf65e
equal deleted inserted replaced
13:b9e6498259df 14:ad23554b3d59
     1 /* -*- C++ -*-
     1 /* -*- mode: C++; indent-tabs-mode: nil; -*-
     2  *
     2  *
     3  * This file is a part of LEMON, a generic C++ optimization library
     3  * This file is a part of LEMON, a generic C++ optimization library.
     4  *
     4  *
     5  * Copyright (C) 2003-2008
     5  * Copyright (C) 2003-2010
     6  * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
     6  * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
     7  * (Egervary Research Group on Combinatorial Optimization, EGRES).
     7  * (Egervary Research Group on Combinatorial Optimization, EGRES).
     8  *
     8  *
     9  * Permission to use, modify and distribute this software is granted
     9  * Permission to use, modify and distribute this software is granted
    10  * provided that this copyright notice appears in all copies. For
    10  * provided that this copyright notice appears in all copies. For
   131       /// on that arc, however, note that it could actually be bounded
   131       /// on that arc, however, note that it could actually be bounded
   132       /// over the feasible flows, but this algroithm cannot handle
   132       /// over the feasible flows, but this algroithm cannot handle
   133       /// these cases.
   133       /// these cases.
   134       UNBOUNDED
   134       UNBOUNDED
   135     };
   135     };
   136   
   136 
   137   private:
   137   private:
   138 
   138 
   139     TEMPLATE_DIGRAPH_TYPEDEFS(GR);
   139     TEMPLATE_DIGRAPH_TYPEDEFS(GR);
   140 
   140 
   141     typedef std::vector<int> IntVector;
   141     typedef std::vector<int> IntVector;
   182     Value _delta;
   182     Value _delta;
   183     int _factor;
   183     int _factor;
   184     IntVector _pred;
   184     IntVector _pred;
   185 
   185 
   186   public:
   186   public:
   187   
   187 
   188     /// \brief Constant for infinite upper bounds (capacities).
   188     /// \brief Constant for infinite upper bounds (capacities).
   189     ///
   189     ///
   190     /// Constant for infinite upper bounds (capacities).
   190     /// Constant for infinite upper bounds (capacities).
   191     /// It is \c std::numeric_limits<Value>::infinity() if available,
   191     /// It is \c std::numeric_limits<Value>::infinity() if available,
   192     /// \c std::numeric_limits<Value>::max() otherwise.
   192     /// \c std::numeric_limits<Value>::max() otherwise.
   209       const CostVector &_cost;
   209       const CostVector &_cost;
   210       const ValueVector &_res_cap;
   210       const ValueVector &_res_cap;
   211       const ValueVector &_excess;
   211       const ValueVector &_excess;
   212       CostVector &_pi;
   212       CostVector &_pi;
   213       IntVector &_pred;
   213       IntVector &_pred;
   214       
   214 
   215       IntVector _proc_nodes;
   215       IntVector _proc_nodes;
   216       CostVector _dist;
   216       CostVector _dist;
   217       
   217 
   218     public:
   218     public:
   219 
   219 
   220       ResidualDijkstra(CapacityScaling& cs) :
   220       ResidualDijkstra(CapacityScaling& cs) :
   221         _node_num(cs._node_num), _geq(cs._sum_supply < 0),
   221         _node_num(cs._node_num), _geq(cs._sum_supply < 0),
   222         _first_out(cs._first_out), _target(cs._target), _cost(cs._cost),
   222         _first_out(cs._first_out), _target(cs._target), _cost(cs._cost),
   437       }
   437       }
   438       _supply[_node_id[s]] =  k;
   438       _supply[_node_id[s]] =  k;
   439       _supply[_node_id[t]] = -k;
   439       _supply[_node_id[t]] = -k;
   440       return *this;
   440       return *this;
   441     }
   441     }
   442     
   442 
   443     /// @}
   443     /// @}
   444 
   444 
   445     /// \name Execution control
   445     /// \name Execution control
   446     /// The algorithm can be executed using \ref run().
   446     /// The algorithm can be executed using \ref run().
   447 
   447 
   573 
   573 
   574       _lower.resize(_res_arc_num);
   574       _lower.resize(_res_arc_num);
   575       _upper.resize(_res_arc_num);
   575       _upper.resize(_res_arc_num);
   576       _cost.resize(_res_arc_num);
   576       _cost.resize(_res_arc_num);
   577       _supply.resize(_node_num);
   577       _supply.resize(_node_num);
   578       
   578 
   579       _res_cap.resize(_res_arc_num);
   579       _res_cap.resize(_res_arc_num);
   580       _pi.resize(_node_num);
   580       _pi.resize(_node_num);
   581       _excess.resize(_node_num);
   581       _excess.resize(_node_num);
   582       _pred.resize(_node_num);
   582       _pred.resize(_node_num);
   583 
   583 
   617         int fi = _arc_idf[a];
   617         int fi = _arc_idf[a];
   618         int bi = _arc_idb[a];
   618         int bi = _arc_idb[a];
   619         _reverse[fi] = bi;
   619         _reverse[fi] = bi;
   620         _reverse[bi] = fi;
   620         _reverse[bi] = fi;
   621       }
   621       }
   622       
   622 
   623       // Reset parameters
   623       // Reset parameters
   624       resetParams();
   624       resetParams();
   625       return *this;
   625       return *this;
   626     }
   626     }
   627 
   627 
   726       _sum_supply = 0;
   726       _sum_supply = 0;
   727       for (int i = 0; i != _root; ++i) {
   727       for (int i = 0; i != _root; ++i) {
   728         _sum_supply += _supply[i];
   728         _sum_supply += _supply[i];
   729       }
   729       }
   730       if (_sum_supply > 0) return INFEASIBLE;
   730       if (_sum_supply > 0) return INFEASIBLE;
   731       
   731 
   732       // Initialize vectors
   732       // Initialize vectors
   733       for (int i = 0; i != _root; ++i) {
   733       for (int i = 0; i != _root; ++i) {
   734         _pi[i] = 0;
   734         _pi[i] = 0;
   735         _excess[i] = _supply[i];
   735         _excess[i] = _supply[i];
   736       }
   736       }
   774             _res_cap[j] = 0;
   774             _res_cap[j] = 0;
   775             _res_cap[_reverse[j]] += rc;
   775             _res_cap[_reverse[j]] += rc;
   776           }
   776           }
   777         }
   777         }
   778       }
   778       }
   779       
   779 
   780       // Handle GEQ supply type
   780       // Handle GEQ supply type
   781       if (_sum_supply < 0) {
   781       if (_sum_supply < 0) {
   782         _pi[_root] = 0;
   782         _pi[_root] = 0;
   783         _excess[_root] = -_sum_supply;
   783         _excess[_root] = -_sum_supply;
   784         for (int a = _first_out[_root]; a != _res_arc_num; ++a) {
   784         for (int a = _first_out[_root]; a != _res_arc_num; ++a) {
   842       // Shift potentials if necessary
   842       // Shift potentials if necessary
   843       Cost pr = _pi[_root];
   843       Cost pr = _pi[_root];
   844       if (_sum_supply < 0 || pr > 0) {
   844       if (_sum_supply < 0 || pr > 0) {
   845         for (int i = 0; i != _node_num; ++i) {
   845         for (int i = 0; i != _node_num; ++i) {
   846           _pi[i] -= pr;
   846           _pi[i] -= pr;
   847         }        
   847         }
   848       }
   848       }
   849       
   849 
   850       return pt;
   850       return pt;
   851     }
   851     }
   852 
   852 
   853     // Execute the capacity scaling algorithm
   853     // Execute the capacity scaling algorithm
   854     ProblemType startWithScaling() {
   854     ProblemType startWithScaling() {