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