[2440] | 1 | /* -*- C++ -*- |
---|
| 2 | * |
---|
| 3 | * This file is a part of LEMON, a generic C++ optimization library |
---|
| 4 | * |
---|
[2553] | 5 | * Copyright (C) 2003-2008 |
---|
[2440] | 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 | |
---|
| 22 | /// \ingroup min_cost_flow |
---|
| 23 | /// |
---|
| 24 | /// \file |
---|
[2574] | 25 | /// \brief Capacity scaling algorithm for finding a minimum cost flow. |
---|
| 26 | |
---|
| 27 | #include <vector> |
---|
[2440] | 28 | |
---|
[2535] | 29 | #include <lemon/graph_adaptor.h> |
---|
| 30 | #include <lemon/bin_heap.h> |
---|
[2457] | 31 | |
---|
[2440] | 32 | namespace lemon { |
---|
| 33 | |
---|
| 34 | /// \addtogroup min_cost_flow |
---|
| 35 | /// @{ |
---|
| 36 | |
---|
[2574] | 37 | /// \brief Implementation of the capacity scaling algorithm for |
---|
| 38 | /// finding a minimum cost flow. |
---|
[2440] | 39 | /// |
---|
[2535] | 40 | /// \ref CapacityScaling implements the capacity scaling version |
---|
| 41 | /// of the successive shortest path algorithm for finding a minimum |
---|
| 42 | /// cost flow. |
---|
[2440] | 43 | /// |
---|
[2574] | 44 | /// \tparam Graph The directed graph type the algorithm runs on. |
---|
| 45 | /// \tparam LowerMap The type of the lower bound map. |
---|
| 46 | /// \tparam CapacityMap The type of the capacity (upper bound) map. |
---|
| 47 | /// \tparam CostMap The type of the cost (length) map. |
---|
| 48 | /// \tparam SupplyMap The type of the supply map. |
---|
[2440] | 49 | /// |
---|
| 50 | /// \warning |
---|
[2574] | 51 | /// - Edge capacities and costs should be \e non-negative \e integers. |
---|
| 52 | /// - Supply values should be \e signed \e integers. |
---|
| 53 | /// - \c LowerMap::Value must be convertible to \c CapacityMap::Value. |
---|
| 54 | /// - \c CapacityMap::Value and \c SupplyMap::Value must be |
---|
| 55 | /// convertible to each other. |
---|
| 56 | /// - All value types must be convertible to \c CostMap::Value, which |
---|
| 57 | /// must be signed type. |
---|
[2440] | 58 | /// |
---|
| 59 | /// \author Peter Kovacs |
---|
| 60 | |
---|
[2533] | 61 | template < typename Graph, |
---|
[2535] | 62 | typename LowerMap = typename Graph::template EdgeMap<int>, |
---|
[2574] | 63 | typename CapacityMap = typename Graph::template EdgeMap<int>, |
---|
[2535] | 64 | typename CostMap = typename Graph::template EdgeMap<int>, |
---|
[2574] | 65 | typename SupplyMap = typename Graph::template NodeMap<int> > |
---|
[2440] | 66 | class CapacityScaling |
---|
| 67 | { |
---|
[2556] | 68 | GRAPH_TYPEDEFS(typename Graph); |
---|
[2440] | 69 | |
---|
| 70 | typedef typename CapacityMap::Value Capacity; |
---|
| 71 | typedef typename CostMap::Value Cost; |
---|
| 72 | typedef typename SupplyMap::Value Supply; |
---|
[2556] | 73 | typedef typename Graph::template EdgeMap<Capacity> CapacityEdgeMap; |
---|
| 74 | typedef typename Graph::template NodeMap<Supply> SupplyNodeMap; |
---|
[2535] | 75 | typedef typename Graph::template NodeMap<Edge> PredMap; |
---|
[2440] | 76 | |
---|
| 77 | public: |
---|
| 78 | |
---|
[2556] | 79 | /// The type of the flow map. |
---|
| 80 | typedef typename Graph::template EdgeMap<Capacity> FlowMap; |
---|
| 81 | /// The type of the potential map. |
---|
[2440] | 82 | typedef typename Graph::template NodeMap<Cost> PotentialMap; |
---|
| 83 | |
---|
[2574] | 84 | private: |
---|
[2440] | 85 | |
---|
[2535] | 86 | /// \brief Special implementation of the \ref Dijkstra algorithm |
---|
[2574] | 87 | /// for finding shortest paths in the residual network. |
---|
| 88 | /// |
---|
| 89 | /// \ref ResidualDijkstra is a special implementation of the |
---|
| 90 | /// \ref Dijkstra algorithm for finding shortest paths in the |
---|
| 91 | /// residual network of the graph with respect to the reduced edge |
---|
| 92 | /// costs and modifying the node potentials according to the |
---|
| 93 | /// distance of the nodes. |
---|
[2535] | 94 | class ResidualDijkstra |
---|
[2440] | 95 | { |
---|
[2535] | 96 | typedef typename Graph::template NodeMap<Cost> CostNodeMap; |
---|
| 97 | typedef typename Graph::template NodeMap<Edge> PredMap; |
---|
[2440] | 98 | |
---|
[2535] | 99 | typedef typename Graph::template NodeMap<int> HeapCrossRef; |
---|
| 100 | typedef BinHeap<Cost, HeapCrossRef> Heap; |
---|
| 101 | |
---|
[2574] | 102 | private: |
---|
[2535] | 103 | |
---|
[2574] | 104 | // The directed graph the algorithm runs on |
---|
| 105 | const Graph &_graph; |
---|
[2535] | 106 | |
---|
[2574] | 107 | // The main maps |
---|
| 108 | const FlowMap &_flow; |
---|
| 109 | const CapacityEdgeMap &_res_cap; |
---|
| 110 | const CostMap &_cost; |
---|
| 111 | const SupplyNodeMap &_excess; |
---|
| 112 | PotentialMap &_potential; |
---|
[2535] | 113 | |
---|
[2574] | 114 | // The distance map |
---|
| 115 | CostNodeMap _dist; |
---|
| 116 | // The pred edge map |
---|
| 117 | PredMap &_pred; |
---|
| 118 | // The processed (i.e. permanently labeled) nodes |
---|
| 119 | std::vector<Node> _proc_nodes; |
---|
[2440] | 120 | |
---|
| 121 | public: |
---|
| 122 | |
---|
[2556] | 123 | /// The constructor of the class. |
---|
[2574] | 124 | ResidualDijkstra( const Graph &graph, |
---|
| 125 | const FlowMap &flow, |
---|
| 126 | const CapacityEdgeMap &res_cap, |
---|
| 127 | const CostMap &cost, |
---|
| 128 | const SupplyMap &excess, |
---|
| 129 | PotentialMap &potential, |
---|
| 130 | PredMap &pred ) : |
---|
| 131 | _graph(graph), _flow(flow), _res_cap(res_cap), _cost(cost), |
---|
| 132 | _excess(excess), _potential(potential), _dist(graph), |
---|
| 133 | _pred(pred) |
---|
[2535] | 134 | {} |
---|
[2440] | 135 | |
---|
[2556] | 136 | /// Runs the algorithm from the given source node. |
---|
[2535] | 137 | Node run(Node s, Capacity delta) { |
---|
[2574] | 138 | HeapCrossRef heap_cross_ref(_graph, Heap::PRE_HEAP); |
---|
[2535] | 139 | Heap heap(heap_cross_ref); |
---|
| 140 | heap.push(s, 0); |
---|
[2574] | 141 | _pred[s] = INVALID; |
---|
| 142 | _proc_nodes.clear(); |
---|
[2535] | 143 | |
---|
| 144 | // Processing nodes |
---|
[2574] | 145 | while (!heap.empty() && _excess[heap.top()] > -delta) { |
---|
[2535] | 146 | Node u = heap.top(), v; |
---|
[2574] | 147 | Cost d = heap.prio() + _potential[u], nd; |
---|
| 148 | _dist[u] = heap.prio(); |
---|
[2535] | 149 | heap.pop(); |
---|
[2574] | 150 | _proc_nodes.push_back(u); |
---|
[2535] | 151 | |
---|
| 152 | // Traversing outgoing edges |
---|
[2574] | 153 | for (OutEdgeIt e(_graph, u); e != INVALID; ++e) { |
---|
| 154 | if (_res_cap[e] >= delta) { |
---|
| 155 | v = _graph.target(e); |
---|
[2535] | 156 | switch(heap.state(v)) { |
---|
| 157 | case Heap::PRE_HEAP: |
---|
[2574] | 158 | heap.push(v, d + _cost[e] - _potential[v]); |
---|
| 159 | _pred[v] = e; |
---|
[2535] | 160 | break; |
---|
| 161 | case Heap::IN_HEAP: |
---|
[2574] | 162 | nd = d + _cost[e] - _potential[v]; |
---|
[2535] | 163 | if (nd < heap[v]) { |
---|
| 164 | heap.decrease(v, nd); |
---|
[2574] | 165 | _pred[v] = e; |
---|
[2535] | 166 | } |
---|
| 167 | break; |
---|
| 168 | case Heap::POST_HEAP: |
---|
| 169 | break; |
---|
| 170 | } |
---|
| 171 | } |
---|
| 172 | } |
---|
| 173 | |
---|
| 174 | // Traversing incoming edges |
---|
[2574] | 175 | for (InEdgeIt e(_graph, u); e != INVALID; ++e) { |
---|
| 176 | if (_flow[e] >= delta) { |
---|
| 177 | v = _graph.source(e); |
---|
[2535] | 178 | switch(heap.state(v)) { |
---|
| 179 | case Heap::PRE_HEAP: |
---|
[2574] | 180 | heap.push(v, d - _cost[e] - _potential[v]); |
---|
| 181 | _pred[v] = e; |
---|
[2535] | 182 | break; |
---|
| 183 | case Heap::IN_HEAP: |
---|
[2574] | 184 | nd = d - _cost[e] - _potential[v]; |
---|
[2535] | 185 | if (nd < heap[v]) { |
---|
| 186 | heap.decrease(v, nd); |
---|
[2574] | 187 | _pred[v] = e; |
---|
[2535] | 188 | } |
---|
| 189 | break; |
---|
| 190 | case Heap::POST_HEAP: |
---|
| 191 | break; |
---|
| 192 | } |
---|
| 193 | } |
---|
| 194 | } |
---|
| 195 | } |
---|
| 196 | if (heap.empty()) return INVALID; |
---|
| 197 | |
---|
| 198 | // Updating potentials of processed nodes |
---|
| 199 | Node t = heap.top(); |
---|
[2574] | 200 | Cost t_dist = heap.prio(); |
---|
| 201 | for (int i = 0; i < int(_proc_nodes.size()); ++i) |
---|
| 202 | _potential[_proc_nodes[i]] += _dist[_proc_nodes[i]] - t_dist; |
---|
[2535] | 203 | |
---|
| 204 | return t; |
---|
[2440] | 205 | } |
---|
| 206 | |
---|
[2535] | 207 | }; //class ResidualDijkstra |
---|
[2440] | 208 | |
---|
[2574] | 209 | private: |
---|
[2440] | 210 | |
---|
[2574] | 211 | // The directed graph the algorithm runs on |
---|
| 212 | const Graph &_graph; |
---|
| 213 | // The original lower bound map |
---|
| 214 | const LowerMap *_lower; |
---|
| 215 | // The modified capacity map |
---|
| 216 | CapacityEdgeMap _capacity; |
---|
| 217 | // The original cost map |
---|
| 218 | const CostMap &_cost; |
---|
| 219 | // The modified supply map |
---|
| 220 | SupplyNodeMap _supply; |
---|
| 221 | bool _valid_supply; |
---|
[2440] | 222 | |
---|
[2574] | 223 | // Edge map of the current flow |
---|
| 224 | FlowMap _flow; |
---|
| 225 | // Node map of the current potentials |
---|
| 226 | PotentialMap _potential; |
---|
[2440] | 227 | |
---|
[2574] | 228 | // The residual capacity map |
---|
| 229 | CapacityEdgeMap _res_cap; |
---|
| 230 | // The excess map |
---|
| 231 | SupplyNodeMap _excess; |
---|
| 232 | // The excess nodes (i.e. nodes with positive excess) |
---|
| 233 | std::vector<Node> _excess_nodes; |
---|
| 234 | // The deficit nodes (i.e. nodes with negative excess) |
---|
| 235 | std::vector<Node> _deficit_nodes; |
---|
[2440] | 236 | |
---|
[2574] | 237 | // The delta parameter used for capacity scaling |
---|
| 238 | Capacity _delta; |
---|
| 239 | // The maximum number of phases |
---|
| 240 | int _phase_num; |
---|
[2440] | 241 | |
---|
[2574] | 242 | // The pred edge map |
---|
| 243 | PredMap _pred; |
---|
| 244 | // Implementation of the Dijkstra algorithm for finding augmenting |
---|
| 245 | // shortest paths in the residual network |
---|
| 246 | ResidualDijkstra _dijkstra; |
---|
[2440] | 247 | |
---|
| 248 | public : |
---|
| 249 | |
---|
| 250 | /// \brief General constructor of the class (with lower bounds). |
---|
| 251 | /// |
---|
| 252 | /// General constructor of the class (with lower bounds). |
---|
| 253 | /// |
---|
[2574] | 254 | /// \param graph The directed graph the algorithm runs on. |
---|
| 255 | /// \param lower The lower bounds of the edges. |
---|
| 256 | /// \param capacity The capacities (upper bounds) of the edges. |
---|
| 257 | /// \param cost The cost (length) values of the edges. |
---|
| 258 | /// \param supply The supply values of the nodes (signed). |
---|
| 259 | CapacityScaling( const Graph &graph, |
---|
| 260 | const LowerMap &lower, |
---|
| 261 | const CapacityMap &capacity, |
---|
| 262 | const CostMap &cost, |
---|
| 263 | const SupplyMap &supply ) : |
---|
| 264 | _graph(graph), _lower(&lower), _capacity(graph), _cost(cost), |
---|
| 265 | _supply(graph), _flow(graph, 0), _potential(graph, 0), |
---|
| 266 | _res_cap(graph), _excess(graph), _pred(graph), |
---|
| 267 | _dijkstra(_graph, _flow, _res_cap, _cost, _excess, _potential, _pred) |
---|
[2440] | 268 | { |
---|
[2556] | 269 | // Removing non-zero lower bounds |
---|
[2574] | 270 | _capacity = subMap(capacity, lower); |
---|
| 271 | _res_cap = _capacity; |
---|
[2440] | 272 | Supply sum = 0; |
---|
[2574] | 273 | for (NodeIt n(_graph); n != INVALID; ++n) { |
---|
| 274 | Supply s = supply[n]; |
---|
| 275 | for (InEdgeIt e(_graph, n); e != INVALID; ++e) |
---|
| 276 | s += lower[e]; |
---|
| 277 | for (OutEdgeIt e(_graph, n); e != INVALID; ++e) |
---|
| 278 | s -= lower[e]; |
---|
| 279 | _supply[n] = s; |
---|
[2535] | 280 | sum += s; |
---|
[2440] | 281 | } |
---|
[2574] | 282 | _valid_supply = sum == 0; |
---|
[2440] | 283 | } |
---|
| 284 | |
---|
| 285 | /// \brief General constructor of the class (without lower bounds). |
---|
| 286 | /// |
---|
| 287 | /// General constructor of the class (without lower bounds). |
---|
| 288 | /// |
---|
[2574] | 289 | /// \param graph The directed graph the algorithm runs on. |
---|
| 290 | /// \param capacity The capacities (upper bounds) of the edges. |
---|
| 291 | /// \param cost The cost (length) values of the edges. |
---|
| 292 | /// \param supply The supply values of the nodes (signed). |
---|
| 293 | CapacityScaling( const Graph &graph, |
---|
| 294 | const CapacityMap &capacity, |
---|
| 295 | const CostMap &cost, |
---|
| 296 | const SupplyMap &supply ) : |
---|
| 297 | _graph(graph), _lower(NULL), _capacity(capacity), _cost(cost), |
---|
| 298 | _supply(supply), _flow(graph, 0), _potential(graph, 0), |
---|
| 299 | _res_cap(capacity), _excess(graph), _pred(graph), |
---|
| 300 | _dijkstra(_graph, _flow, _res_cap, _cost, _excess, _potential, _pred) |
---|
[2440] | 301 | { |
---|
| 302 | // Checking the sum of supply values |
---|
| 303 | Supply sum = 0; |
---|
[2574] | 304 | for (NodeIt n(_graph); n != INVALID; ++n) sum += _supply[n]; |
---|
| 305 | _valid_supply = sum == 0; |
---|
[2440] | 306 | } |
---|
| 307 | |
---|
| 308 | /// \brief Simple constructor of the class (with lower bounds). |
---|
| 309 | /// |
---|
| 310 | /// Simple constructor of the class (with lower bounds). |
---|
| 311 | /// |
---|
[2574] | 312 | /// \param graph The directed graph the algorithm runs on. |
---|
| 313 | /// \param lower The lower bounds of the edges. |
---|
| 314 | /// \param capacity The capacities (upper bounds) of the edges. |
---|
| 315 | /// \param cost The cost (length) values of the edges. |
---|
| 316 | /// \param s The source node. |
---|
| 317 | /// \param t The target node. |
---|
| 318 | /// \param flow_value The required amount of flow from node \c s |
---|
| 319 | /// to node \c t (i.e. the supply of \c s and the demand of \c t). |
---|
| 320 | CapacityScaling( const Graph &graph, |
---|
| 321 | const LowerMap &lower, |
---|
| 322 | const CapacityMap &capacity, |
---|
| 323 | const CostMap &cost, |
---|
| 324 | Node s, Node t, |
---|
| 325 | Supply flow_value ) : |
---|
| 326 | _graph(graph), _lower(&lower), _capacity(graph), _cost(cost), |
---|
| 327 | _supply(graph), _flow(graph, 0), _potential(graph, 0), |
---|
| 328 | _res_cap(graph), _excess(graph), _pred(graph), |
---|
| 329 | _dijkstra(_graph, _flow, _res_cap, _cost, _excess, _potential, _pred) |
---|
[2440] | 330 | { |
---|
[2556] | 331 | // Removing non-zero lower bounds |
---|
[2574] | 332 | _capacity = subMap(capacity, lower); |
---|
| 333 | _res_cap = _capacity; |
---|
| 334 | for (NodeIt n(_graph); n != INVALID; ++n) { |
---|
| 335 | Supply sum = 0; |
---|
| 336 | if (n == s) sum = flow_value; |
---|
| 337 | if (n == t) sum = -flow_value; |
---|
| 338 | for (InEdgeIt e(_graph, n); e != INVALID; ++e) |
---|
| 339 | sum += lower[e]; |
---|
| 340 | for (OutEdgeIt e(_graph, n); e != INVALID; ++e) |
---|
| 341 | sum -= lower[e]; |
---|
| 342 | _supply[n] = sum; |
---|
[2440] | 343 | } |
---|
[2574] | 344 | _valid_supply = true; |
---|
[2440] | 345 | } |
---|
| 346 | |
---|
| 347 | /// \brief Simple constructor of the class (without lower bounds). |
---|
| 348 | /// |
---|
| 349 | /// Simple constructor of the class (without lower bounds). |
---|
| 350 | /// |
---|
[2574] | 351 | /// \param graph The directed graph the algorithm runs on. |
---|
| 352 | /// \param capacity The capacities (upper bounds) of the edges. |
---|
| 353 | /// \param cost The cost (length) values of the edges. |
---|
| 354 | /// \param s The source node. |
---|
| 355 | /// \param t The target node. |
---|
| 356 | /// \param flow_value The required amount of flow from node \c s |
---|
| 357 | /// to node \c t (i.e. the supply of \c s and the demand of \c t). |
---|
| 358 | CapacityScaling( const Graph &graph, |
---|
| 359 | const CapacityMap &capacity, |
---|
| 360 | const CostMap &cost, |
---|
| 361 | Node s, Node t, |
---|
| 362 | Supply flow_value ) : |
---|
| 363 | _graph(graph), _lower(NULL), _capacity(capacity), _cost(cost), |
---|
| 364 | _supply(graph, 0), _flow(graph, 0), _potential(graph, 0), |
---|
| 365 | _res_cap(capacity), _excess(graph), _pred(graph), |
---|
| 366 | _dijkstra(_graph, _flow, _res_cap, _cost, _excess, _potential, _pred) |
---|
[2440] | 367 | { |
---|
[2574] | 368 | _supply[s] = flow_value; |
---|
| 369 | _supply[t] = -flow_value; |
---|
| 370 | _valid_supply = true; |
---|
[2440] | 371 | } |
---|
| 372 | |
---|
[2556] | 373 | /// \brief Runs the algorithm. |
---|
| 374 | /// |
---|
| 375 | /// Runs the algorithm. |
---|
| 376 | /// |
---|
[2574] | 377 | /// \param scaling Enable or disable capacity scaling. |
---|
[2556] | 378 | /// If the maximum edge capacity and/or the amount of total supply |
---|
[2574] | 379 | /// is rather small, the algorithm could be slightly faster without |
---|
[2556] | 380 | /// scaling. |
---|
| 381 | /// |
---|
| 382 | /// \return \c true if a feasible flow can be found. |
---|
[2574] | 383 | bool run(bool scaling = true) { |
---|
| 384 | return init(scaling) && start(); |
---|
[2556] | 385 | } |
---|
| 386 | |
---|
[2574] | 387 | /// \brief Returns a const reference to the edge map storing the |
---|
| 388 | /// found flow. |
---|
[2440] | 389 | /// |
---|
[2574] | 390 | /// Returns a const reference to the edge map storing the found flow. |
---|
[2440] | 391 | /// |
---|
| 392 | /// \pre \ref run() must be called before using this function. |
---|
| 393 | const FlowMap& flowMap() const { |
---|
[2574] | 394 | return _flow; |
---|
[2440] | 395 | } |
---|
| 396 | |
---|
[2574] | 397 | /// \brief Returns a const reference to the node map storing the |
---|
| 398 | /// found potentials (the dual solution). |
---|
[2440] | 399 | /// |
---|
[2574] | 400 | /// Returns a const reference to the node map storing the found |
---|
| 401 | /// potentials (the dual solution). |
---|
[2440] | 402 | /// |
---|
| 403 | /// \pre \ref run() must be called before using this function. |
---|
| 404 | const PotentialMap& potentialMap() const { |
---|
[2574] | 405 | return _potential; |
---|
[2440] | 406 | } |
---|
| 407 | |
---|
| 408 | /// \brief Returns the total cost of the found flow. |
---|
| 409 | /// |
---|
| 410 | /// Returns the total cost of the found flow. The complexity of the |
---|
| 411 | /// function is \f$ O(e) \f$. |
---|
| 412 | /// |
---|
| 413 | /// \pre \ref run() must be called before using this function. |
---|
| 414 | Cost totalCost() const { |
---|
| 415 | Cost c = 0; |
---|
[2574] | 416 | for (EdgeIt e(_graph); e != INVALID; ++e) |
---|
| 417 | c += _flow[e] * _cost[e]; |
---|
[2440] | 418 | return c; |
---|
| 419 | } |
---|
| 420 | |
---|
[2574] | 421 | private: |
---|
[2440] | 422 | |
---|
[2556] | 423 | /// Initializes the algorithm. |
---|
[2574] | 424 | bool init(bool scaling) { |
---|
| 425 | if (!_valid_supply) return false; |
---|
| 426 | _excess = _supply; |
---|
[2440] | 427 | |
---|
| 428 | // Initilaizing delta value |
---|
[2574] | 429 | if (scaling) { |
---|
[2535] | 430 | // With scaling |
---|
| 431 | Supply max_sup = 0, max_dem = 0; |
---|
[2574] | 432 | for (NodeIt n(_graph); n != INVALID; ++n) { |
---|
| 433 | if ( _supply[n] > max_sup) max_sup = _supply[n]; |
---|
| 434 | if (-_supply[n] > max_dem) max_dem = -_supply[n]; |
---|
[2535] | 435 | } |
---|
| 436 | if (max_dem < max_sup) max_sup = max_dem; |
---|
[2574] | 437 | _phase_num = 0; |
---|
| 438 | for (_delta = 1; 2 * _delta <= max_sup; _delta *= 2) |
---|
| 439 | ++_phase_num; |
---|
[2535] | 440 | } else { |
---|
| 441 | // Without scaling |
---|
[2574] | 442 | _delta = 1; |
---|
[2440] | 443 | } |
---|
| 444 | return true; |
---|
| 445 | } |
---|
| 446 | |
---|
[2535] | 447 | bool start() { |
---|
[2574] | 448 | if (_delta > 1) |
---|
[2535] | 449 | return startWithScaling(); |
---|
| 450 | else |
---|
| 451 | return startWithoutScaling(); |
---|
| 452 | } |
---|
| 453 | |
---|
[2574] | 454 | /// Executes the capacity scaling algorithm. |
---|
[2535] | 455 | bool startWithScaling() { |
---|
| 456 | // Processing capacity scaling phases |
---|
| 457 | Node s, t; |
---|
| 458 | int phase_cnt = 0; |
---|
| 459 | int factor = 4; |
---|
| 460 | while (true) { |
---|
| 461 | // Saturating all edges not satisfying the optimality condition |
---|
[2574] | 462 | for (EdgeIt e(_graph); e != INVALID; ++e) { |
---|
| 463 | Node u = _graph.source(e), v = _graph.target(e); |
---|
| 464 | Cost c = _cost[e] + _potential[u] - _potential[v]; |
---|
| 465 | if (c < 0 && _res_cap[e] >= _delta) { |
---|
| 466 | _excess[u] -= _res_cap[e]; |
---|
| 467 | _excess[v] += _res_cap[e]; |
---|
| 468 | _flow[e] = _capacity[e]; |
---|
| 469 | _res_cap[e] = 0; |
---|
[2535] | 470 | } |
---|
[2574] | 471 | else if (c > 0 && _flow[e] >= _delta) { |
---|
| 472 | _excess[u] += _flow[e]; |
---|
| 473 | _excess[v] -= _flow[e]; |
---|
| 474 | _flow[e] = 0; |
---|
| 475 | _res_cap[e] = _capacity[e]; |
---|
[2535] | 476 | } |
---|
| 477 | } |
---|
| 478 | |
---|
| 479 | // Finding excess nodes and deficit nodes |
---|
[2574] | 480 | _excess_nodes.clear(); |
---|
| 481 | _deficit_nodes.clear(); |
---|
| 482 | for (NodeIt n(_graph); n != INVALID; ++n) { |
---|
| 483 | if (_excess[n] >= _delta) _excess_nodes.push_back(n); |
---|
| 484 | if (_excess[n] <= -_delta) _deficit_nodes.push_back(n); |
---|
[2535] | 485 | } |
---|
[2556] | 486 | int next_node = 0; |
---|
[2535] | 487 | |
---|
| 488 | // Finding augmenting shortest paths |
---|
[2574] | 489 | while (next_node < int(_excess_nodes.size())) { |
---|
[2535] | 490 | // Checking deficit nodes |
---|
[2574] | 491 | if (_delta > 1) { |
---|
[2535] | 492 | bool delta_deficit = false; |
---|
[2574] | 493 | for (int i = 0; i < int(_deficit_nodes.size()); ++i) { |
---|
| 494 | if (_excess[_deficit_nodes[i]] <= -_delta) { |
---|
[2535] | 495 | delta_deficit = true; |
---|
| 496 | break; |
---|
| 497 | } |
---|
| 498 | } |
---|
| 499 | if (!delta_deficit) break; |
---|
| 500 | } |
---|
| 501 | |
---|
| 502 | // Running Dijkstra |
---|
[2574] | 503 | s = _excess_nodes[next_node]; |
---|
| 504 | if ((t = _dijkstra.run(s, _delta)) == INVALID) { |
---|
| 505 | if (_delta > 1) { |
---|
[2535] | 506 | ++next_node; |
---|
| 507 | continue; |
---|
| 508 | } |
---|
| 509 | return false; |
---|
| 510 | } |
---|
| 511 | |
---|
| 512 | // Augmenting along a shortest path from s to t. |
---|
[2574] | 513 | Capacity d = _excess[s] < -_excess[t] ? _excess[s] : -_excess[t]; |
---|
[2535] | 514 | Node u = t; |
---|
| 515 | Edge e; |
---|
[2574] | 516 | if (d > _delta) { |
---|
| 517 | while ((e = _pred[u]) != INVALID) { |
---|
[2535] | 518 | Capacity rc; |
---|
[2574] | 519 | if (u == _graph.target(e)) { |
---|
| 520 | rc = _res_cap[e]; |
---|
| 521 | u = _graph.source(e); |
---|
[2535] | 522 | } else { |
---|
[2574] | 523 | rc = _flow[e]; |
---|
| 524 | u = _graph.target(e); |
---|
[2535] | 525 | } |
---|
| 526 | if (rc < d) d = rc; |
---|
| 527 | } |
---|
| 528 | } |
---|
| 529 | u = t; |
---|
[2574] | 530 | while ((e = _pred[u]) != INVALID) { |
---|
| 531 | if (u == _graph.target(e)) { |
---|
| 532 | _flow[e] += d; |
---|
| 533 | _res_cap[e] -= d; |
---|
| 534 | u = _graph.source(e); |
---|
[2535] | 535 | } else { |
---|
[2574] | 536 | _flow[e] -= d; |
---|
| 537 | _res_cap[e] += d; |
---|
| 538 | u = _graph.target(e); |
---|
[2535] | 539 | } |
---|
| 540 | } |
---|
[2574] | 541 | _excess[s] -= d; |
---|
| 542 | _excess[t] += d; |
---|
[2535] | 543 | |
---|
[2574] | 544 | if (_excess[s] < _delta) ++next_node; |
---|
[2535] | 545 | } |
---|
| 546 | |
---|
[2574] | 547 | if (_delta == 1) break; |
---|
| 548 | if (++phase_cnt > _phase_num / 4) factor = 2; |
---|
| 549 | _delta = _delta <= factor ? 1 : _delta / factor; |
---|
[2535] | 550 | } |
---|
| 551 | |
---|
[2556] | 552 | // Handling non-zero lower bounds |
---|
[2574] | 553 | if (_lower) { |
---|
| 554 | for (EdgeIt e(_graph); e != INVALID; ++e) |
---|
| 555 | _flow[e] += (*_lower)[e]; |
---|
[2535] | 556 | } |
---|
| 557 | return true; |
---|
| 558 | } |
---|
| 559 | |
---|
[2574] | 560 | /// Executes the successive shortest path algorithm. |
---|
[2535] | 561 | bool startWithoutScaling() { |
---|
[2440] | 562 | // Finding excess nodes |
---|
[2574] | 563 | for (NodeIt n(_graph); n != INVALID; ++n) |
---|
| 564 | if (_excess[n] > 0) _excess_nodes.push_back(n); |
---|
| 565 | if (_excess_nodes.size() == 0) return true; |
---|
[2556] | 566 | int next_node = 0; |
---|
[2440] | 567 | |
---|
[2457] | 568 | // Finding shortest paths |
---|
[2535] | 569 | Node s, t; |
---|
[2574] | 570 | while ( _excess[_excess_nodes[next_node]] > 0 || |
---|
| 571 | ++next_node < int(_excess_nodes.size()) ) |
---|
[2440] | 572 | { |
---|
[2535] | 573 | // Running Dijkstra |
---|
[2574] | 574 | s = _excess_nodes[next_node]; |
---|
| 575 | if ((t = _dijkstra.run(s, 1)) == INVALID) |
---|
[2535] | 576 | return false; |
---|
[2440] | 577 | |
---|
[2535] | 578 | // Augmenting along a shortest path from s to t |
---|
[2574] | 579 | Capacity d = _excess[s] < -_excess[t] ? _excess[s] : -_excess[t]; |
---|
[2535] | 580 | Node u = t; |
---|
| 581 | Edge e; |
---|
[2574] | 582 | while ((e = _pred[u]) != INVALID) { |
---|
[2535] | 583 | Capacity rc; |
---|
[2574] | 584 | if (u == _graph.target(e)) { |
---|
| 585 | rc = _res_cap[e]; |
---|
| 586 | u = _graph.source(e); |
---|
[2535] | 587 | } else { |
---|
[2574] | 588 | rc = _flow[e]; |
---|
| 589 | u = _graph.target(e); |
---|
[2535] | 590 | } |
---|
| 591 | if (rc < d) d = rc; |
---|
| 592 | } |
---|
| 593 | u = t; |
---|
[2574] | 594 | while ((e = _pred[u]) != INVALID) { |
---|
| 595 | if (u == _graph.target(e)) { |
---|
| 596 | _flow[e] += d; |
---|
| 597 | _res_cap[e] -= d; |
---|
| 598 | u = _graph.source(e); |
---|
[2535] | 599 | } else { |
---|
[2574] | 600 | _flow[e] -= d; |
---|
| 601 | _res_cap[e] += d; |
---|
| 602 | u = _graph.target(e); |
---|
[2535] | 603 | } |
---|
| 604 | } |
---|
[2574] | 605 | _excess[s] -= d; |
---|
| 606 | _excess[t] += d; |
---|
[2440] | 607 | } |
---|
| 608 | |
---|
[2556] | 609 | // Handling non-zero lower bounds |
---|
[2574] | 610 | if (_lower) { |
---|
| 611 | for (EdgeIt e(_graph); e != INVALID; ++e) |
---|
| 612 | _flow[e] += (*_lower)[e]; |
---|
[2440] | 613 | } |
---|
| 614 | return true; |
---|
| 615 | } |
---|
| 616 | |
---|
| 617 | }; //class CapacityScaling |
---|
| 618 | |
---|
| 619 | ///@} |
---|
| 620 | |
---|
| 621 | } //namespace lemon |
---|
| 622 | |
---|
| 623 | #endif //LEMON_CAPACITY_SCALING_H |
---|