[877] | 1 | /* -*- mode: C++; indent-tabs-mode: nil; -*- |
---|
[805] | 2 | * |
---|
[877] | 3 | * This file is a part of LEMON, a generic C++ optimization library. |
---|
[805] | 4 | * |
---|
[877] | 5 | * Copyright (C) 2003-2010 |
---|
[805] | 6 | * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport |
---|
| 7 | * (Egervary Research Group on Combinatorial Optimization, EGRES). |
---|
| 8 | * |
---|
| 9 | * Permission to use, modify and distribute this software is granted |
---|
| 10 | * provided that this copyright notice appears in all copies. For |
---|
| 11 | * precise terms see the accompanying LICENSE file. |
---|
| 12 | * |
---|
| 13 | * This software is provided "AS IS" with no warranty of any kind, |
---|
| 14 | * express or implied, and with no claim as to its suitability for any |
---|
| 15 | * purpose. |
---|
| 16 | * |
---|
| 17 | */ |
---|
| 18 | |
---|
| 19 | #ifndef LEMON_CAPACITY_SCALING_H |
---|
| 20 | #define LEMON_CAPACITY_SCALING_H |
---|
| 21 | |
---|
[806] | 22 | /// \ingroup min_cost_flow_algs |
---|
[805] | 23 | /// |
---|
| 24 | /// \file |
---|
[806] | 25 | /// \brief Capacity Scaling algorithm for finding a minimum cost flow. |
---|
[805] | 26 | |
---|
| 27 | #include <vector> |
---|
[806] | 28 | #include <limits> |
---|
| 29 | #include <lemon/core.h> |
---|
[805] | 30 | #include <lemon/bin_heap.h> |
---|
| 31 | |
---|
| 32 | namespace lemon { |
---|
| 33 | |
---|
[807] | 34 | /// \brief Default traits class of CapacityScaling algorithm. |
---|
| 35 | /// |
---|
| 36 | /// Default traits class of CapacityScaling algorithm. |
---|
| 37 | /// \tparam GR Digraph type. |
---|
[812] | 38 | /// \tparam V The number type used for flow amounts, capacity bounds |
---|
[807] | 39 | /// and supply values. By default it is \c int. |
---|
[812] | 40 | /// \tparam C The number type used for costs and potentials. |
---|
[807] | 41 | /// By default it is the same as \c V. |
---|
| 42 | template <typename GR, typename V = int, typename C = V> |
---|
| 43 | struct CapacityScalingDefaultTraits |
---|
| 44 | { |
---|
| 45 | /// The type of the digraph |
---|
| 46 | typedef GR Digraph; |
---|
| 47 | /// The type of the flow amounts, capacity bounds and supply values |
---|
| 48 | typedef V Value; |
---|
| 49 | /// The type of the arc costs |
---|
| 50 | typedef C Cost; |
---|
| 51 | |
---|
| 52 | /// \brief The type of the heap used for internal Dijkstra computations. |
---|
| 53 | /// |
---|
| 54 | /// The type of the heap used for internal Dijkstra computations. |
---|
| 55 | /// It must conform to the \ref lemon::concepts::Heap "Heap" concept, |
---|
| 56 | /// its priority type must be \c Cost and its cross reference type |
---|
| 57 | /// must be \ref RangeMap "RangeMap<int>". |
---|
| 58 | typedef BinHeap<Cost, RangeMap<int> > Heap; |
---|
| 59 | }; |
---|
| 60 | |
---|
[806] | 61 | /// \addtogroup min_cost_flow_algs |
---|
[805] | 62 | /// @{ |
---|
| 63 | |
---|
[806] | 64 | /// \brief Implementation of the Capacity Scaling algorithm for |
---|
| 65 | /// finding a \ref min_cost_flow "minimum cost flow". |
---|
[805] | 66 | /// |
---|
| 67 | /// \ref CapacityScaling implements the capacity scaling version |
---|
[806] | 68 | /// of the successive shortest path algorithm for finding a |
---|
[813] | 69 | /// \ref min_cost_flow "minimum cost flow" \ref amo93networkflows, |
---|
| 70 | /// \ref edmondskarp72theoretical. It is an efficient dual |
---|
[806] | 71 | /// solution method. |
---|
[805] | 72 | /// |
---|
[1003] | 73 | /// This algorithm is typically slower than \ref CostScaling and |
---|
| 74 | /// \ref NetworkSimplex, but in special cases, it can be more |
---|
| 75 | /// efficient than them. |
---|
| 76 | /// (For more information, see \ref min_cost_flow_algs "the module page".) |
---|
| 77 | /// |
---|
[806] | 78 | /// Most of the parameters of the problem (except for the digraph) |
---|
| 79 | /// can be given using separate functions, and the algorithm can be |
---|
| 80 | /// executed using the \ref run() function. If some parameters are not |
---|
| 81 | /// specified, then default values will be used. |
---|
[805] | 82 | /// |
---|
[806] | 83 | /// \tparam GR The digraph type the algorithm runs on. |
---|
[812] | 84 | /// \tparam V The number type used for flow amounts, capacity bounds |
---|
[825] | 85 | /// and supply values in the algorithm. By default, it is \c int. |
---|
[812] | 86 | /// \tparam C The number type used for costs and potentials in the |
---|
[825] | 87 | /// algorithm. By default, it is the same as \c V. |
---|
| 88 | /// \tparam TR The traits class that defines various types used by the |
---|
| 89 | /// algorithm. By default, it is \ref CapacityScalingDefaultTraits |
---|
| 90 | /// "CapacityScalingDefaultTraits<GR, V, C>". |
---|
| 91 | /// In most cases, this parameter should not be set directly, |
---|
| 92 | /// consider to use the named template parameters instead. |
---|
[805] | 93 | /// |
---|
[921] | 94 | /// \warning Both \c V and \c C must be signed number types. |
---|
[985] | 95 | /// \warning Capacity bounds and supply values must be integer, but |
---|
| 96 | /// arc costs can be arbitrary real numbers. |
---|
[919] | 97 | /// \warning This algorithm does not support negative costs for |
---|
| 98 | /// arcs having infinite upper bound. |
---|
[807] | 99 | #ifdef DOXYGEN |
---|
| 100 | template <typename GR, typename V, typename C, typename TR> |
---|
| 101 | #else |
---|
| 102 | template < typename GR, typename V = int, typename C = V, |
---|
| 103 | typename TR = CapacityScalingDefaultTraits<GR, V, C> > |
---|
| 104 | #endif |
---|
[805] | 105 | class CapacityScaling |
---|
| 106 | { |
---|
[806] | 107 | public: |
---|
[805] | 108 | |
---|
[807] | 109 | /// The type of the digraph |
---|
| 110 | typedef typename TR::Digraph Digraph; |
---|
[806] | 111 | /// The type of the flow amounts, capacity bounds and supply values |
---|
[807] | 112 | typedef typename TR::Value Value; |
---|
[806] | 113 | /// The type of the arc costs |
---|
[807] | 114 | typedef typename TR::Cost Cost; |
---|
| 115 | |
---|
| 116 | /// The type of the heap used for internal Dijkstra computations |
---|
| 117 | typedef typename TR::Heap Heap; |
---|
| 118 | |
---|
| 119 | /// The \ref CapacityScalingDefaultTraits "traits class" of the algorithm |
---|
| 120 | typedef TR Traits; |
---|
[805] | 121 | |
---|
| 122 | public: |
---|
| 123 | |
---|
[806] | 124 | /// \brief Problem type constants for the \c run() function. |
---|
| 125 | /// |
---|
| 126 | /// Enum type containing the problem type constants that can be |
---|
| 127 | /// returned by the \ref run() function of the algorithm. |
---|
| 128 | enum ProblemType { |
---|
| 129 | /// The problem has no feasible solution (flow). |
---|
| 130 | INFEASIBLE, |
---|
| 131 | /// The problem has optimal solution (i.e. it is feasible and |
---|
| 132 | /// bounded), and the algorithm has found optimal flow and node |
---|
| 133 | /// potentials (primal and dual solutions). |
---|
| 134 | OPTIMAL, |
---|
| 135 | /// The digraph contains an arc of negative cost and infinite |
---|
| 136 | /// upper bound. It means that the objective function is unbounded |
---|
[812] | 137 | /// on that arc, however, note that it could actually be bounded |
---|
[806] | 138 | /// over the feasible flows, but this algroithm cannot handle |
---|
| 139 | /// these cases. |
---|
| 140 | UNBOUNDED |
---|
| 141 | }; |
---|
[877] | 142 | |
---|
[806] | 143 | private: |
---|
| 144 | |
---|
| 145 | TEMPLATE_DIGRAPH_TYPEDEFS(GR); |
---|
| 146 | |
---|
| 147 | typedef std::vector<int> IntVector; |
---|
| 148 | typedef std::vector<Value> ValueVector; |
---|
| 149 | typedef std::vector<Cost> CostVector; |
---|
[839] | 150 | typedef std::vector<char> BoolVector; |
---|
| 151 | // Note: vector<char> is used instead of vector<bool> for efficiency reasons |
---|
[805] | 152 | |
---|
| 153 | private: |
---|
| 154 | |
---|
[806] | 155 | // Data related to the underlying digraph |
---|
| 156 | const GR &_graph; |
---|
| 157 | int _node_num; |
---|
| 158 | int _arc_num; |
---|
| 159 | int _res_arc_num; |
---|
| 160 | int _root; |
---|
| 161 | |
---|
| 162 | // Parameters of the problem |
---|
| 163 | bool _have_lower; |
---|
| 164 | Value _sum_supply; |
---|
| 165 | |
---|
| 166 | // Data structures for storing the digraph |
---|
| 167 | IntNodeMap _node_id; |
---|
| 168 | IntArcMap _arc_idf; |
---|
| 169 | IntArcMap _arc_idb; |
---|
| 170 | IntVector _first_out; |
---|
| 171 | BoolVector _forward; |
---|
| 172 | IntVector _source; |
---|
| 173 | IntVector _target; |
---|
| 174 | IntVector _reverse; |
---|
| 175 | |
---|
| 176 | // Node and arc data |
---|
| 177 | ValueVector _lower; |
---|
| 178 | ValueVector _upper; |
---|
| 179 | CostVector _cost; |
---|
| 180 | ValueVector _supply; |
---|
| 181 | |
---|
| 182 | ValueVector _res_cap; |
---|
| 183 | CostVector _pi; |
---|
| 184 | ValueVector _excess; |
---|
| 185 | IntVector _excess_nodes; |
---|
| 186 | IntVector _deficit_nodes; |
---|
| 187 | |
---|
| 188 | Value _delta; |
---|
[810] | 189 | int _factor; |
---|
[806] | 190 | IntVector _pred; |
---|
| 191 | |
---|
| 192 | public: |
---|
[877] | 193 | |
---|
[806] | 194 | /// \brief Constant for infinite upper bounds (capacities). |
---|
[805] | 195 | /// |
---|
[806] | 196 | /// Constant for infinite upper bounds (capacities). |
---|
| 197 | /// It is \c std::numeric_limits<Value>::infinity() if available, |
---|
| 198 | /// \c std::numeric_limits<Value>::max() otherwise. |
---|
| 199 | const Value INF; |
---|
| 200 | |
---|
| 201 | private: |
---|
| 202 | |
---|
| 203 | // Special implementation of the Dijkstra algorithm for finding |
---|
| 204 | // shortest paths in the residual network of the digraph with |
---|
| 205 | // respect to the reduced arc costs and modifying the node |
---|
| 206 | // potentials according to the found distance labels. |
---|
[805] | 207 | class ResidualDijkstra |
---|
| 208 | { |
---|
| 209 | private: |
---|
| 210 | |
---|
[806] | 211 | int _node_num; |
---|
[811] | 212 | bool _geq; |
---|
[806] | 213 | const IntVector &_first_out; |
---|
| 214 | const IntVector &_target; |
---|
| 215 | const CostVector &_cost; |
---|
| 216 | const ValueVector &_res_cap; |
---|
| 217 | const ValueVector &_excess; |
---|
| 218 | CostVector &_pi; |
---|
| 219 | IntVector &_pred; |
---|
[877] | 220 | |
---|
[806] | 221 | IntVector _proc_nodes; |
---|
| 222 | CostVector _dist; |
---|
[877] | 223 | |
---|
[805] | 224 | public: |
---|
| 225 | |
---|
[806] | 226 | ResidualDijkstra(CapacityScaling& cs) : |
---|
[811] | 227 | _node_num(cs._node_num), _geq(cs._sum_supply < 0), |
---|
| 228 | _first_out(cs._first_out), _target(cs._target), _cost(cs._cost), |
---|
| 229 | _res_cap(cs._res_cap), _excess(cs._excess), _pi(cs._pi), |
---|
| 230 | _pred(cs._pred), _dist(cs._node_num) |
---|
[805] | 231 | {} |
---|
| 232 | |
---|
[806] | 233 | int run(int s, Value delta = 1) { |
---|
[807] | 234 | RangeMap<int> heap_cross_ref(_node_num, Heap::PRE_HEAP); |
---|
[805] | 235 | Heap heap(heap_cross_ref); |
---|
| 236 | heap.push(s, 0); |
---|
[806] | 237 | _pred[s] = -1; |
---|
[805] | 238 | _proc_nodes.clear(); |
---|
| 239 | |
---|
[806] | 240 | // Process nodes |
---|
[805] | 241 | while (!heap.empty() && _excess[heap.top()] > -delta) { |
---|
[806] | 242 | int u = heap.top(), v; |
---|
| 243 | Cost d = heap.prio() + _pi[u], dn; |
---|
[805] | 244 | _dist[u] = heap.prio(); |
---|
[806] | 245 | _proc_nodes.push_back(u); |
---|
[805] | 246 | heap.pop(); |
---|
| 247 | |
---|
[806] | 248 | // Traverse outgoing residual arcs |
---|
[811] | 249 | int last_out = _geq ? _first_out[u+1] : _first_out[u+1] - 1; |
---|
| 250 | for (int a = _first_out[u]; a != last_out; ++a) { |
---|
[806] | 251 | if (_res_cap[a] < delta) continue; |
---|
| 252 | v = _target[a]; |
---|
| 253 | switch (heap.state(v)) { |
---|
[805] | 254 | case Heap::PRE_HEAP: |
---|
[806] | 255 | heap.push(v, d + _cost[a] - _pi[v]); |
---|
| 256 | _pred[v] = a; |
---|
[805] | 257 | break; |
---|
| 258 | case Heap::IN_HEAP: |
---|
[806] | 259 | dn = d + _cost[a] - _pi[v]; |
---|
| 260 | if (dn < heap[v]) { |
---|
| 261 | heap.decrease(v, dn); |
---|
| 262 | _pred[v] = a; |
---|
[805] | 263 | } |
---|
| 264 | break; |
---|
| 265 | case Heap::POST_HEAP: |
---|
| 266 | break; |
---|
| 267 | } |
---|
| 268 | } |
---|
| 269 | } |
---|
[806] | 270 | if (heap.empty()) return -1; |
---|
[805] | 271 | |
---|
[806] | 272 | // Update potentials of processed nodes |
---|
| 273 | int t = heap.top(); |
---|
| 274 | Cost dt = heap.prio(); |
---|
| 275 | for (int i = 0; i < int(_proc_nodes.size()); ++i) { |
---|
| 276 | _pi[_proc_nodes[i]] += _dist[_proc_nodes[i]] - dt; |
---|
| 277 | } |
---|
[805] | 278 | |
---|
| 279 | return t; |
---|
| 280 | } |
---|
| 281 | |
---|
| 282 | }; //class ResidualDijkstra |
---|
| 283 | |
---|
| 284 | public: |
---|
| 285 | |
---|
[807] | 286 | /// \name Named Template Parameters |
---|
| 287 | /// @{ |
---|
| 288 | |
---|
| 289 | template <typename T> |
---|
| 290 | struct SetHeapTraits : public Traits { |
---|
| 291 | typedef T Heap; |
---|
| 292 | }; |
---|
| 293 | |
---|
| 294 | /// \brief \ref named-templ-param "Named parameter" for setting |
---|
| 295 | /// \c Heap type. |
---|
| 296 | /// |
---|
| 297 | /// \ref named-templ-param "Named parameter" for setting \c Heap |
---|
| 298 | /// type, which is used for internal Dijkstra computations. |
---|
| 299 | /// It must conform to the \ref lemon::concepts::Heap "Heap" concept, |
---|
| 300 | /// its priority type must be \c Cost and its cross reference type |
---|
| 301 | /// must be \ref RangeMap "RangeMap<int>". |
---|
| 302 | template <typename T> |
---|
| 303 | struct SetHeap |
---|
| 304 | : public CapacityScaling<GR, V, C, SetHeapTraits<T> > { |
---|
| 305 | typedef CapacityScaling<GR, V, C, SetHeapTraits<T> > Create; |
---|
| 306 | }; |
---|
| 307 | |
---|
| 308 | /// @} |
---|
| 309 | |
---|
[863] | 310 | protected: |
---|
| 311 | |
---|
| 312 | CapacityScaling() {} |
---|
| 313 | |
---|
[807] | 314 | public: |
---|
| 315 | |
---|
[806] | 316 | /// \brief Constructor. |
---|
[805] | 317 | /// |
---|
[806] | 318 | /// The constructor of the class. |
---|
[805] | 319 | /// |
---|
[806] | 320 | /// \param graph The digraph the algorithm runs on. |
---|
| 321 | CapacityScaling(const GR& graph) : |
---|
| 322 | _graph(graph), _node_id(graph), _arc_idf(graph), _arc_idb(graph), |
---|
| 323 | INF(std::numeric_limits<Value>::has_infinity ? |
---|
| 324 | std::numeric_limits<Value>::infinity() : |
---|
| 325 | std::numeric_limits<Value>::max()) |
---|
[805] | 326 | { |
---|
[812] | 327 | // Check the number types |
---|
[806] | 328 | LEMON_ASSERT(std::numeric_limits<Value>::is_signed, |
---|
| 329 | "The flow type of CapacityScaling must be signed"); |
---|
| 330 | LEMON_ASSERT(std::numeric_limits<Cost>::is_signed, |
---|
| 331 | "The cost type of CapacityScaling must be signed"); |
---|
| 332 | |
---|
[830] | 333 | // Reset data structures |
---|
[806] | 334 | reset(); |
---|
[805] | 335 | } |
---|
| 336 | |
---|
[806] | 337 | /// \name Parameters |
---|
| 338 | /// The parameters of the algorithm can be specified using these |
---|
| 339 | /// functions. |
---|
| 340 | |
---|
| 341 | /// @{ |
---|
| 342 | |
---|
| 343 | /// \brief Set the lower bounds on the arcs. |
---|
[805] | 344 | /// |
---|
[806] | 345 | /// This function sets the lower bounds on the arcs. |
---|
| 346 | /// If it is not used before calling \ref run(), the lower bounds |
---|
| 347 | /// will be set to zero on all arcs. |
---|
[805] | 348 | /// |
---|
[806] | 349 | /// \param map An arc map storing the lower bounds. |
---|
| 350 | /// Its \c Value type must be convertible to the \c Value type |
---|
| 351 | /// of the algorithm. |
---|
| 352 | /// |
---|
| 353 | /// \return <tt>(*this)</tt> |
---|
| 354 | template <typename LowerMap> |
---|
| 355 | CapacityScaling& lowerMap(const LowerMap& map) { |
---|
| 356 | _have_lower = true; |
---|
| 357 | for (ArcIt a(_graph); a != INVALID; ++a) { |
---|
| 358 | _lower[_arc_idf[a]] = map[a]; |
---|
| 359 | _lower[_arc_idb[a]] = map[a]; |
---|
[805] | 360 | } |
---|
| 361 | return *this; |
---|
| 362 | } |
---|
| 363 | |
---|
[806] | 364 | /// \brief Set the upper bounds (capacities) on the arcs. |
---|
[805] | 365 | /// |
---|
[806] | 366 | /// This function sets the upper bounds (capacities) on the arcs. |
---|
| 367 | /// If it is not used before calling \ref run(), the upper bounds |
---|
| 368 | /// will be set to \ref INF on all arcs (i.e. the flow value will be |
---|
[812] | 369 | /// unbounded from above). |
---|
[805] | 370 | /// |
---|
[806] | 371 | /// \param map An arc map storing the upper bounds. |
---|
| 372 | /// Its \c Value type must be convertible to the \c Value type |
---|
| 373 | /// of the algorithm. |
---|
| 374 | /// |
---|
| 375 | /// \return <tt>(*this)</tt> |
---|
| 376 | template<typename UpperMap> |
---|
| 377 | CapacityScaling& upperMap(const UpperMap& map) { |
---|
| 378 | for (ArcIt a(_graph); a != INVALID; ++a) { |
---|
| 379 | _upper[_arc_idf[a]] = map[a]; |
---|
[805] | 380 | } |
---|
| 381 | return *this; |
---|
| 382 | } |
---|
| 383 | |
---|
[806] | 384 | /// \brief Set the costs of the arcs. |
---|
| 385 | /// |
---|
| 386 | /// This function sets the costs of the arcs. |
---|
| 387 | /// If it is not used before calling \ref run(), the costs |
---|
| 388 | /// will be set to \c 1 on all arcs. |
---|
| 389 | /// |
---|
| 390 | /// \param map An arc map storing the costs. |
---|
| 391 | /// Its \c Value type must be convertible to the \c Cost type |
---|
| 392 | /// of the algorithm. |
---|
| 393 | /// |
---|
| 394 | /// \return <tt>(*this)</tt> |
---|
| 395 | template<typename CostMap> |
---|
| 396 | CapacityScaling& costMap(const CostMap& map) { |
---|
| 397 | for (ArcIt a(_graph); a != INVALID; ++a) { |
---|
| 398 | _cost[_arc_idf[a]] = map[a]; |
---|
| 399 | _cost[_arc_idb[a]] = -map[a]; |
---|
| 400 | } |
---|
| 401 | return *this; |
---|
| 402 | } |
---|
| 403 | |
---|
| 404 | /// \brief Set the supply values of the nodes. |
---|
| 405 | /// |
---|
| 406 | /// This function sets the supply values of the nodes. |
---|
| 407 | /// If neither this function nor \ref stSupply() is used before |
---|
| 408 | /// calling \ref run(), the supply of each node will be set to zero. |
---|
| 409 | /// |
---|
| 410 | /// \param map A node map storing the supply values. |
---|
| 411 | /// Its \c Value type must be convertible to the \c Value type |
---|
| 412 | /// of the algorithm. |
---|
| 413 | /// |
---|
| 414 | /// \return <tt>(*this)</tt> |
---|
| 415 | template<typename SupplyMap> |
---|
| 416 | CapacityScaling& supplyMap(const SupplyMap& map) { |
---|
| 417 | for (NodeIt n(_graph); n != INVALID; ++n) { |
---|
| 418 | _supply[_node_id[n]] = map[n]; |
---|
| 419 | } |
---|
| 420 | return *this; |
---|
| 421 | } |
---|
| 422 | |
---|
| 423 | /// \brief Set single source and target nodes and a supply value. |
---|
| 424 | /// |
---|
| 425 | /// This function sets a single source node and a single target node |
---|
| 426 | /// and the required flow value. |
---|
| 427 | /// If neither this function nor \ref supplyMap() is used before |
---|
| 428 | /// calling \ref run(), the supply of each node will be set to zero. |
---|
| 429 | /// |
---|
| 430 | /// Using this function has the same effect as using \ref supplyMap() |
---|
[919] | 431 | /// with a map in which \c k is assigned to \c s, \c -k is |
---|
[806] | 432 | /// assigned to \c t and all other nodes have zero supply value. |
---|
| 433 | /// |
---|
| 434 | /// \param s The source node. |
---|
| 435 | /// \param t The target node. |
---|
| 436 | /// \param k The required amount of flow from node \c s to node \c t |
---|
| 437 | /// (i.e. the supply of \c s and the demand of \c t). |
---|
| 438 | /// |
---|
| 439 | /// \return <tt>(*this)</tt> |
---|
| 440 | CapacityScaling& stSupply(const Node& s, const Node& t, Value k) { |
---|
| 441 | for (int i = 0; i != _node_num; ++i) { |
---|
| 442 | _supply[i] = 0; |
---|
| 443 | } |
---|
| 444 | _supply[_node_id[s]] = k; |
---|
| 445 | _supply[_node_id[t]] = -k; |
---|
| 446 | return *this; |
---|
| 447 | } |
---|
[877] | 448 | |
---|
[806] | 449 | /// @} |
---|
| 450 | |
---|
[805] | 451 | /// \name Execution control |
---|
[807] | 452 | /// The algorithm can be executed using \ref run(). |
---|
[805] | 453 | |
---|
| 454 | /// @{ |
---|
| 455 | |
---|
| 456 | /// \brief Run the algorithm. |
---|
| 457 | /// |
---|
| 458 | /// This function runs the algorithm. |
---|
[806] | 459 | /// The paramters can be specified using functions \ref lowerMap(), |
---|
| 460 | /// \ref upperMap(), \ref costMap(), \ref supplyMap(), \ref stSupply(). |
---|
| 461 | /// For example, |
---|
| 462 | /// \code |
---|
| 463 | /// CapacityScaling<ListDigraph> cs(graph); |
---|
| 464 | /// cs.lowerMap(lower).upperMap(upper).costMap(cost) |
---|
| 465 | /// .supplyMap(sup).run(); |
---|
| 466 | /// \endcode |
---|
| 467 | /// |
---|
[830] | 468 | /// This function can be called more than once. All the given parameters |
---|
| 469 | /// are kept for the next call, unless \ref resetParams() or \ref reset() |
---|
| 470 | /// is used, thus only the modified parameters have to be set again. |
---|
| 471 | /// If the underlying digraph was also modified after the construction |
---|
| 472 | /// of the class (or the last \ref reset() call), then the \ref reset() |
---|
| 473 | /// function must be called. |
---|
[805] | 474 | /// |
---|
[810] | 475 | /// \param factor The capacity scaling factor. It must be larger than |
---|
| 476 | /// one to use scaling. If it is less or equal to one, then scaling |
---|
| 477 | /// will be disabled. |
---|
[805] | 478 | /// |
---|
[806] | 479 | /// \return \c INFEASIBLE if no feasible flow exists, |
---|
| 480 | /// \n \c OPTIMAL if the problem has optimal solution |
---|
| 481 | /// (i.e. it is feasible and bounded), and the algorithm has found |
---|
| 482 | /// optimal flow and node potentials (primal and dual solutions), |
---|
| 483 | /// \n \c UNBOUNDED if the digraph contains an arc of negative cost |
---|
| 484 | /// and infinite upper bound. It means that the objective function |
---|
[812] | 485 | /// is unbounded on that arc, however, note that it could actually be |
---|
[806] | 486 | /// bounded over the feasible flows, but this algroithm cannot handle |
---|
| 487 | /// these cases. |
---|
| 488 | /// |
---|
| 489 | /// \see ProblemType |
---|
[830] | 490 | /// \see resetParams(), reset() |
---|
[810] | 491 | ProblemType run(int factor = 4) { |
---|
| 492 | _factor = factor; |
---|
| 493 | ProblemType pt = init(); |
---|
[806] | 494 | if (pt != OPTIMAL) return pt; |
---|
| 495 | return start(); |
---|
| 496 | } |
---|
| 497 | |
---|
| 498 | /// \brief Reset all the parameters that have been given before. |
---|
| 499 | /// |
---|
| 500 | /// This function resets all the paramaters that have been given |
---|
| 501 | /// before using functions \ref lowerMap(), \ref upperMap(), |
---|
| 502 | /// \ref costMap(), \ref supplyMap(), \ref stSupply(). |
---|
| 503 | /// |
---|
[830] | 504 | /// It is useful for multiple \ref run() calls. Basically, all the given |
---|
| 505 | /// parameters are kept for the next \ref run() call, unless |
---|
| 506 | /// \ref resetParams() or \ref reset() is used. |
---|
| 507 | /// If the underlying digraph was also modified after the construction |
---|
| 508 | /// of the class or the last \ref reset() call, then the \ref reset() |
---|
| 509 | /// function must be used, otherwise \ref resetParams() is sufficient. |
---|
[806] | 510 | /// |
---|
| 511 | /// For example, |
---|
| 512 | /// \code |
---|
| 513 | /// CapacityScaling<ListDigraph> cs(graph); |
---|
| 514 | /// |
---|
| 515 | /// // First run |
---|
| 516 | /// cs.lowerMap(lower).upperMap(upper).costMap(cost) |
---|
| 517 | /// .supplyMap(sup).run(); |
---|
| 518 | /// |
---|
[830] | 519 | /// // Run again with modified cost map (resetParams() is not called, |
---|
[806] | 520 | /// // so only the cost map have to be set again) |
---|
| 521 | /// cost[e] += 100; |
---|
| 522 | /// cs.costMap(cost).run(); |
---|
| 523 | /// |
---|
[830] | 524 | /// // Run again from scratch using resetParams() |
---|
[806] | 525 | /// // (the lower bounds will be set to zero on all arcs) |
---|
[830] | 526 | /// cs.resetParams(); |
---|
[806] | 527 | /// cs.upperMap(capacity).costMap(cost) |
---|
| 528 | /// .supplyMap(sup).run(); |
---|
| 529 | /// \endcode |
---|
| 530 | /// |
---|
| 531 | /// \return <tt>(*this)</tt> |
---|
[830] | 532 | /// |
---|
| 533 | /// \see reset(), run() |
---|
| 534 | CapacityScaling& resetParams() { |
---|
[806] | 535 | for (int i = 0; i != _node_num; ++i) { |
---|
| 536 | _supply[i] = 0; |
---|
| 537 | } |
---|
| 538 | for (int j = 0; j != _res_arc_num; ++j) { |
---|
| 539 | _lower[j] = 0; |
---|
| 540 | _upper[j] = INF; |
---|
| 541 | _cost[j] = _forward[j] ? 1 : -1; |
---|
| 542 | } |
---|
| 543 | _have_lower = false; |
---|
| 544 | return *this; |
---|
[805] | 545 | } |
---|
| 546 | |
---|
[830] | 547 | /// \brief Reset the internal data structures and all the parameters |
---|
| 548 | /// that have been given before. |
---|
| 549 | /// |
---|
| 550 | /// This function resets the internal data structures and all the |
---|
| 551 | /// paramaters that have been given before using functions \ref lowerMap(), |
---|
| 552 | /// \ref upperMap(), \ref costMap(), \ref supplyMap(), \ref stSupply(). |
---|
| 553 | /// |
---|
| 554 | /// It is useful for multiple \ref run() calls. Basically, all the given |
---|
| 555 | /// parameters are kept for the next \ref run() call, unless |
---|
| 556 | /// \ref resetParams() or \ref reset() is used. |
---|
| 557 | /// If the underlying digraph was also modified after the construction |
---|
| 558 | /// of the class or the last \ref reset() call, then the \ref reset() |
---|
| 559 | /// function must be used, otherwise \ref resetParams() is sufficient. |
---|
| 560 | /// |
---|
| 561 | /// See \ref resetParams() for examples. |
---|
| 562 | /// |
---|
| 563 | /// \return <tt>(*this)</tt> |
---|
| 564 | /// |
---|
| 565 | /// \see resetParams(), run() |
---|
| 566 | CapacityScaling& reset() { |
---|
| 567 | // Resize vectors |
---|
| 568 | _node_num = countNodes(_graph); |
---|
| 569 | _arc_num = countArcs(_graph); |
---|
| 570 | _res_arc_num = 2 * (_arc_num + _node_num); |
---|
| 571 | _root = _node_num; |
---|
| 572 | ++_node_num; |
---|
| 573 | |
---|
| 574 | _first_out.resize(_node_num + 1); |
---|
| 575 | _forward.resize(_res_arc_num); |
---|
| 576 | _source.resize(_res_arc_num); |
---|
| 577 | _target.resize(_res_arc_num); |
---|
| 578 | _reverse.resize(_res_arc_num); |
---|
| 579 | |
---|
| 580 | _lower.resize(_res_arc_num); |
---|
| 581 | _upper.resize(_res_arc_num); |
---|
| 582 | _cost.resize(_res_arc_num); |
---|
| 583 | _supply.resize(_node_num); |
---|
[877] | 584 | |
---|
[830] | 585 | _res_cap.resize(_res_arc_num); |
---|
| 586 | _pi.resize(_node_num); |
---|
| 587 | _excess.resize(_node_num); |
---|
| 588 | _pred.resize(_node_num); |
---|
| 589 | |
---|
| 590 | // Copy the graph |
---|
| 591 | int i = 0, j = 0, k = 2 * _arc_num + _node_num - 1; |
---|
| 592 | for (NodeIt n(_graph); n != INVALID; ++n, ++i) { |
---|
| 593 | _node_id[n] = i; |
---|
| 594 | } |
---|
| 595 | i = 0; |
---|
| 596 | for (NodeIt n(_graph); n != INVALID; ++n, ++i) { |
---|
| 597 | _first_out[i] = j; |
---|
| 598 | for (OutArcIt a(_graph, n); a != INVALID; ++a, ++j) { |
---|
| 599 | _arc_idf[a] = j; |
---|
| 600 | _forward[j] = true; |
---|
| 601 | _source[j] = i; |
---|
| 602 | _target[j] = _node_id[_graph.runningNode(a)]; |
---|
| 603 | } |
---|
| 604 | for (InArcIt a(_graph, n); a != INVALID; ++a, ++j) { |
---|
| 605 | _arc_idb[a] = j; |
---|
| 606 | _forward[j] = false; |
---|
| 607 | _source[j] = i; |
---|
| 608 | _target[j] = _node_id[_graph.runningNode(a)]; |
---|
| 609 | } |
---|
| 610 | _forward[j] = false; |
---|
| 611 | _source[j] = i; |
---|
| 612 | _target[j] = _root; |
---|
| 613 | _reverse[j] = k; |
---|
| 614 | _forward[k] = true; |
---|
| 615 | _source[k] = _root; |
---|
| 616 | _target[k] = i; |
---|
| 617 | _reverse[k] = j; |
---|
| 618 | ++j; ++k; |
---|
| 619 | } |
---|
| 620 | _first_out[i] = j; |
---|
| 621 | _first_out[_node_num] = k; |
---|
| 622 | for (ArcIt a(_graph); a != INVALID; ++a) { |
---|
| 623 | int fi = _arc_idf[a]; |
---|
| 624 | int bi = _arc_idb[a]; |
---|
| 625 | _reverse[fi] = bi; |
---|
| 626 | _reverse[bi] = fi; |
---|
| 627 | } |
---|
[877] | 628 | |
---|
[830] | 629 | // Reset parameters |
---|
| 630 | resetParams(); |
---|
| 631 | return *this; |
---|
| 632 | } |
---|
| 633 | |
---|
[805] | 634 | /// @} |
---|
| 635 | |
---|
| 636 | /// \name Query Functions |
---|
| 637 | /// The results of the algorithm can be obtained using these |
---|
| 638 | /// functions.\n |
---|
[806] | 639 | /// The \ref run() function must be called before using them. |
---|
[805] | 640 | |
---|
| 641 | /// @{ |
---|
| 642 | |
---|
[806] | 643 | /// \brief Return the total cost of the found flow. |
---|
[805] | 644 | /// |
---|
[806] | 645 | /// This function returns the total cost of the found flow. |
---|
| 646 | /// Its complexity is O(e). |
---|
| 647 | /// |
---|
| 648 | /// \note The return type of the function can be specified as a |
---|
| 649 | /// template parameter. For example, |
---|
| 650 | /// \code |
---|
| 651 | /// cs.totalCost<double>(); |
---|
| 652 | /// \endcode |
---|
| 653 | /// It is useful if the total cost cannot be stored in the \c Cost |
---|
| 654 | /// type of the algorithm, which is the default return type of the |
---|
| 655 | /// function. |
---|
[805] | 656 | /// |
---|
| 657 | /// \pre \ref run() must be called before using this function. |
---|
[806] | 658 | template <typename Number> |
---|
| 659 | Number totalCost() const { |
---|
| 660 | Number c = 0; |
---|
| 661 | for (ArcIt a(_graph); a != INVALID; ++a) { |
---|
| 662 | int i = _arc_idb[a]; |
---|
| 663 | c += static_cast<Number>(_res_cap[i]) * |
---|
| 664 | (-static_cast<Number>(_cost[i])); |
---|
| 665 | } |
---|
| 666 | return c; |
---|
[805] | 667 | } |
---|
| 668 | |
---|
[806] | 669 | #ifndef DOXYGEN |
---|
| 670 | Cost totalCost() const { |
---|
| 671 | return totalCost<Cost>(); |
---|
[805] | 672 | } |
---|
[806] | 673 | #endif |
---|
[805] | 674 | |
---|
| 675 | /// \brief Return the flow on the given arc. |
---|
| 676 | /// |
---|
[806] | 677 | /// This function returns the flow on the given arc. |
---|
[805] | 678 | /// |
---|
| 679 | /// \pre \ref run() must be called before using this function. |
---|
[806] | 680 | Value flow(const Arc& a) const { |
---|
| 681 | return _res_cap[_arc_idb[a]]; |
---|
[805] | 682 | } |
---|
| 683 | |
---|
[1003] | 684 | /// \brief Copy the flow values (the primal solution) into the |
---|
| 685 | /// given map. |
---|
[805] | 686 | /// |
---|
[806] | 687 | /// This function copies the flow value on each arc into the given |
---|
| 688 | /// map. The \c Value type of the algorithm must be convertible to |
---|
| 689 | /// the \c Value type of the map. |
---|
[805] | 690 | /// |
---|
| 691 | /// \pre \ref run() must be called before using this function. |
---|
[806] | 692 | template <typename FlowMap> |
---|
| 693 | void flowMap(FlowMap &map) const { |
---|
| 694 | for (ArcIt a(_graph); a != INVALID; ++a) { |
---|
| 695 | map.set(a, _res_cap[_arc_idb[a]]); |
---|
| 696 | } |
---|
[805] | 697 | } |
---|
| 698 | |
---|
[806] | 699 | /// \brief Return the potential (dual value) of the given node. |
---|
[805] | 700 | /// |
---|
[806] | 701 | /// This function returns the potential (dual value) of the |
---|
| 702 | /// given node. |
---|
[805] | 703 | /// |
---|
| 704 | /// \pre \ref run() must be called before using this function. |
---|
[806] | 705 | Cost potential(const Node& n) const { |
---|
| 706 | return _pi[_node_id[n]]; |
---|
| 707 | } |
---|
| 708 | |
---|
[1003] | 709 | /// \brief Copy the potential values (the dual solution) into the |
---|
| 710 | /// given map. |
---|
[806] | 711 | /// |
---|
| 712 | /// This function copies the potential (dual value) of each node |
---|
| 713 | /// into the given map. |
---|
| 714 | /// The \c Cost type of the algorithm must be convertible to the |
---|
| 715 | /// \c Value type of the map. |
---|
| 716 | /// |
---|
| 717 | /// \pre \ref run() must be called before using this function. |
---|
| 718 | template <typename PotentialMap> |
---|
| 719 | void potentialMap(PotentialMap &map) const { |
---|
| 720 | for (NodeIt n(_graph); n != INVALID; ++n) { |
---|
| 721 | map.set(n, _pi[_node_id[n]]); |
---|
| 722 | } |
---|
[805] | 723 | } |
---|
| 724 | |
---|
| 725 | /// @} |
---|
| 726 | |
---|
| 727 | private: |
---|
| 728 | |
---|
[806] | 729 | // Initialize the algorithm |
---|
[810] | 730 | ProblemType init() { |
---|
[821] | 731 | if (_node_num <= 1) return INFEASIBLE; |
---|
[805] | 732 | |
---|
[806] | 733 | // Check the sum of supply values |
---|
| 734 | _sum_supply = 0; |
---|
| 735 | for (int i = 0; i != _root; ++i) { |
---|
| 736 | _sum_supply += _supply[i]; |
---|
[805] | 737 | } |
---|
[806] | 738 | if (_sum_supply > 0) return INFEASIBLE; |
---|
[877] | 739 | |
---|
[811] | 740 | // Initialize vectors |
---|
[806] | 741 | for (int i = 0; i != _root; ++i) { |
---|
| 742 | _pi[i] = 0; |
---|
| 743 | _excess[i] = _supply[i]; |
---|
[805] | 744 | } |
---|
| 745 | |
---|
[806] | 746 | // Remove non-zero lower bounds |
---|
[811] | 747 | const Value MAX = std::numeric_limits<Value>::max(); |
---|
| 748 | int last_out; |
---|
[806] | 749 | if (_have_lower) { |
---|
| 750 | for (int i = 0; i != _root; ++i) { |
---|
[811] | 751 | last_out = _first_out[i+1]; |
---|
| 752 | for (int j = _first_out[i]; j != last_out; ++j) { |
---|
[806] | 753 | if (_forward[j]) { |
---|
| 754 | Value c = _lower[j]; |
---|
| 755 | if (c >= 0) { |
---|
[811] | 756 | _res_cap[j] = _upper[j] < MAX ? _upper[j] - c : INF; |
---|
[806] | 757 | } else { |
---|
[811] | 758 | _res_cap[j] = _upper[j] < MAX + c ? _upper[j] - c : INF; |
---|
[806] | 759 | } |
---|
| 760 | _excess[i] -= c; |
---|
| 761 | _excess[_target[j]] += c; |
---|
| 762 | } else { |
---|
| 763 | _res_cap[j] = 0; |
---|
| 764 | } |
---|
| 765 | } |
---|
| 766 | } |
---|
| 767 | } else { |
---|
| 768 | for (int j = 0; j != _res_arc_num; ++j) { |
---|
| 769 | _res_cap[j] = _forward[j] ? _upper[j] : 0; |
---|
| 770 | } |
---|
| 771 | } |
---|
[805] | 772 | |
---|
[806] | 773 | // Handle negative costs |
---|
[811] | 774 | for (int i = 0; i != _root; ++i) { |
---|
| 775 | last_out = _first_out[i+1] - 1; |
---|
| 776 | for (int j = _first_out[i]; j != last_out; ++j) { |
---|
| 777 | Value rc = _res_cap[j]; |
---|
| 778 | if (_cost[j] < 0 && rc > 0) { |
---|
| 779 | if (rc >= MAX) return UNBOUNDED; |
---|
| 780 | _excess[i] -= rc; |
---|
| 781 | _excess[_target[j]] += rc; |
---|
| 782 | _res_cap[j] = 0; |
---|
| 783 | _res_cap[_reverse[j]] += rc; |
---|
[806] | 784 | } |
---|
| 785 | } |
---|
| 786 | } |
---|
[877] | 787 | |
---|
[806] | 788 | // Handle GEQ supply type |
---|
| 789 | if (_sum_supply < 0) { |
---|
| 790 | _pi[_root] = 0; |
---|
| 791 | _excess[_root] = -_sum_supply; |
---|
| 792 | for (int a = _first_out[_root]; a != _res_arc_num; ++a) { |
---|
[811] | 793 | int ra = _reverse[a]; |
---|
| 794 | _res_cap[a] = -_sum_supply + 1; |
---|
| 795 | _res_cap[ra] = 0; |
---|
[806] | 796 | _cost[a] = 0; |
---|
[811] | 797 | _cost[ra] = 0; |
---|
[806] | 798 | } |
---|
| 799 | } else { |
---|
| 800 | _pi[_root] = 0; |
---|
| 801 | _excess[_root] = 0; |
---|
| 802 | for (int a = _first_out[_root]; a != _res_arc_num; ++a) { |
---|
[811] | 803 | int ra = _reverse[a]; |
---|
[806] | 804 | _res_cap[a] = 1; |
---|
[811] | 805 | _res_cap[ra] = 0; |
---|
[806] | 806 | _cost[a] = 0; |
---|
[811] | 807 | _cost[ra] = 0; |
---|
[806] | 808 | } |
---|
| 809 | } |
---|
| 810 | |
---|
| 811 | // Initialize delta value |
---|
[810] | 812 | if (_factor > 1) { |
---|
[805] | 813 | // With scaling |
---|
[839] | 814 | Value max_sup = 0, max_dem = 0, max_cap = 0; |
---|
| 815 | for (int i = 0; i != _root; ++i) { |
---|
[811] | 816 | Value ex = _excess[i]; |
---|
| 817 | if ( ex > max_sup) max_sup = ex; |
---|
| 818 | if (-ex > max_dem) max_dem = -ex; |
---|
[839] | 819 | int last_out = _first_out[i+1] - 1; |
---|
| 820 | for (int j = _first_out[i]; j != last_out; ++j) { |
---|
| 821 | if (_res_cap[j] > max_cap) max_cap = _res_cap[j]; |
---|
| 822 | } |
---|
[805] | 823 | } |
---|
| 824 | max_sup = std::min(std::min(max_sup, max_dem), max_cap); |
---|
[810] | 825 | for (_delta = 1; 2 * _delta <= max_sup; _delta *= 2) ; |
---|
[805] | 826 | } else { |
---|
| 827 | // Without scaling |
---|
| 828 | _delta = 1; |
---|
| 829 | } |
---|
| 830 | |
---|
[806] | 831 | return OPTIMAL; |
---|
[805] | 832 | } |
---|
| 833 | |
---|
[806] | 834 | ProblemType start() { |
---|
| 835 | // Execute the algorithm |
---|
| 836 | ProblemType pt; |
---|
[805] | 837 | if (_delta > 1) |
---|
[806] | 838 | pt = startWithScaling(); |
---|
[805] | 839 | else |
---|
[806] | 840 | pt = startWithoutScaling(); |
---|
| 841 | |
---|
| 842 | // Handle non-zero lower bounds |
---|
| 843 | if (_have_lower) { |
---|
[811] | 844 | int limit = _first_out[_root]; |
---|
| 845 | for (int j = 0; j != limit; ++j) { |
---|
[806] | 846 | if (!_forward[j]) _res_cap[j] += _lower[j]; |
---|
| 847 | } |
---|
| 848 | } |
---|
| 849 | |
---|
| 850 | // Shift potentials if necessary |
---|
| 851 | Cost pr = _pi[_root]; |
---|
| 852 | if (_sum_supply < 0 || pr > 0) { |
---|
| 853 | for (int i = 0; i != _node_num; ++i) { |
---|
| 854 | _pi[i] -= pr; |
---|
[877] | 855 | } |
---|
[806] | 856 | } |
---|
[877] | 857 | |
---|
[806] | 858 | return pt; |
---|
[805] | 859 | } |
---|
| 860 | |
---|
[806] | 861 | // Execute the capacity scaling algorithm |
---|
| 862 | ProblemType startWithScaling() { |
---|
[807] | 863 | // Perform capacity scaling phases |
---|
[806] | 864 | int s, t; |
---|
| 865 | ResidualDijkstra _dijkstra(*this); |
---|
[805] | 866 | while (true) { |
---|
[806] | 867 | // Saturate all arcs not satisfying the optimality condition |
---|
[811] | 868 | int last_out; |
---|
[806] | 869 | for (int u = 0; u != _node_num; ++u) { |
---|
[811] | 870 | last_out = _sum_supply < 0 ? |
---|
| 871 | _first_out[u+1] : _first_out[u+1] - 1; |
---|
| 872 | for (int a = _first_out[u]; a != last_out; ++a) { |
---|
[806] | 873 | int v = _target[a]; |
---|
| 874 | Cost c = _cost[a] + _pi[u] - _pi[v]; |
---|
| 875 | Value rc = _res_cap[a]; |
---|
| 876 | if (c < 0 && rc >= _delta) { |
---|
| 877 | _excess[u] -= rc; |
---|
| 878 | _excess[v] += rc; |
---|
| 879 | _res_cap[a] = 0; |
---|
| 880 | _res_cap[_reverse[a]] += rc; |
---|
| 881 | } |
---|
[805] | 882 | } |
---|
| 883 | } |
---|
| 884 | |
---|
[806] | 885 | // Find excess nodes and deficit nodes |
---|
[805] | 886 | _excess_nodes.clear(); |
---|
| 887 | _deficit_nodes.clear(); |
---|
[806] | 888 | for (int u = 0; u != _node_num; ++u) { |
---|
[811] | 889 | Value ex = _excess[u]; |
---|
| 890 | if (ex >= _delta) _excess_nodes.push_back(u); |
---|
| 891 | if (ex <= -_delta) _deficit_nodes.push_back(u); |
---|
[805] | 892 | } |
---|
| 893 | int next_node = 0, next_def_node = 0; |
---|
| 894 | |
---|
[806] | 895 | // Find augmenting shortest paths |
---|
[805] | 896 | while (next_node < int(_excess_nodes.size())) { |
---|
[806] | 897 | // Check deficit nodes |
---|
[805] | 898 | if (_delta > 1) { |
---|
| 899 | bool delta_deficit = false; |
---|
| 900 | for ( ; next_def_node < int(_deficit_nodes.size()); |
---|
| 901 | ++next_def_node ) { |
---|
| 902 | if (_excess[_deficit_nodes[next_def_node]] <= -_delta) { |
---|
| 903 | delta_deficit = true; |
---|
| 904 | break; |
---|
| 905 | } |
---|
| 906 | } |
---|
| 907 | if (!delta_deficit) break; |
---|
| 908 | } |
---|
| 909 | |
---|
[806] | 910 | // Run Dijkstra in the residual network |
---|
[805] | 911 | s = _excess_nodes[next_node]; |
---|
[806] | 912 | if ((t = _dijkstra.run(s, _delta)) == -1) { |
---|
[805] | 913 | if (_delta > 1) { |
---|
| 914 | ++next_node; |
---|
| 915 | continue; |
---|
| 916 | } |
---|
[806] | 917 | return INFEASIBLE; |
---|
[805] | 918 | } |
---|
| 919 | |
---|
[806] | 920 | // Augment along a shortest path from s to t |
---|
| 921 | Value d = std::min(_excess[s], -_excess[t]); |
---|
| 922 | int u = t; |
---|
| 923 | int a; |
---|
[805] | 924 | if (d > _delta) { |
---|
[806] | 925 | while ((a = _pred[u]) != -1) { |
---|
| 926 | if (_res_cap[a] < d) d = _res_cap[a]; |
---|
| 927 | u = _source[a]; |
---|
[805] | 928 | } |
---|
| 929 | } |
---|
| 930 | u = t; |
---|
[806] | 931 | while ((a = _pred[u]) != -1) { |
---|
| 932 | _res_cap[a] -= d; |
---|
| 933 | _res_cap[_reverse[a]] += d; |
---|
| 934 | u = _source[a]; |
---|
[805] | 935 | } |
---|
| 936 | _excess[s] -= d; |
---|
| 937 | _excess[t] += d; |
---|
| 938 | |
---|
| 939 | if (_excess[s] < _delta) ++next_node; |
---|
| 940 | } |
---|
| 941 | |
---|
| 942 | if (_delta == 1) break; |
---|
[810] | 943 | _delta = _delta <= _factor ? 1 : _delta / _factor; |
---|
[805] | 944 | } |
---|
| 945 | |
---|
[806] | 946 | return OPTIMAL; |
---|
[805] | 947 | } |
---|
| 948 | |
---|
[806] | 949 | // Execute the successive shortest path algorithm |
---|
| 950 | ProblemType startWithoutScaling() { |
---|
| 951 | // Find excess nodes |
---|
| 952 | _excess_nodes.clear(); |
---|
| 953 | for (int i = 0; i != _node_num; ++i) { |
---|
| 954 | if (_excess[i] > 0) _excess_nodes.push_back(i); |
---|
| 955 | } |
---|
| 956 | if (_excess_nodes.size() == 0) return OPTIMAL; |
---|
[805] | 957 | int next_node = 0; |
---|
| 958 | |
---|
[806] | 959 | // Find shortest paths |
---|
| 960 | int s, t; |
---|
| 961 | ResidualDijkstra _dijkstra(*this); |
---|
[805] | 962 | while ( _excess[_excess_nodes[next_node]] > 0 || |
---|
| 963 | ++next_node < int(_excess_nodes.size()) ) |
---|
| 964 | { |
---|
[806] | 965 | // Run Dijkstra in the residual network |
---|
[805] | 966 | s = _excess_nodes[next_node]; |
---|
[806] | 967 | if ((t = _dijkstra.run(s)) == -1) return INFEASIBLE; |
---|
[805] | 968 | |
---|
[806] | 969 | // Augment along a shortest path from s to t |
---|
| 970 | Value d = std::min(_excess[s], -_excess[t]); |
---|
| 971 | int u = t; |
---|
| 972 | int a; |
---|
[805] | 973 | if (d > 1) { |
---|
[806] | 974 | while ((a = _pred[u]) != -1) { |
---|
| 975 | if (_res_cap[a] < d) d = _res_cap[a]; |
---|
| 976 | u = _source[a]; |
---|
[805] | 977 | } |
---|
| 978 | } |
---|
| 979 | u = t; |
---|
[806] | 980 | while ((a = _pred[u]) != -1) { |
---|
| 981 | _res_cap[a] -= d; |
---|
| 982 | _res_cap[_reverse[a]] += d; |
---|
| 983 | u = _source[a]; |
---|
[805] | 984 | } |
---|
| 985 | _excess[s] -= d; |
---|
| 986 | _excess[t] += d; |
---|
| 987 | } |
---|
| 988 | |
---|
[806] | 989 | return OPTIMAL; |
---|
[805] | 990 | } |
---|
| 991 | |
---|
| 992 | }; //class CapacityScaling |
---|
| 993 | |
---|
| 994 | ///@} |
---|
| 995 | |
---|
| 996 | } //namespace lemon |
---|
| 997 | |
---|
| 998 | #endif //LEMON_CAPACITY_SCALING_H |
---|