Changeset 2556:74c2c81055e1 in lemon-0.x for lemon/capacity_scaling.h
- Timestamp:
- 01/13/08 11:32:14 (16 years ago)
- Branch:
- default
- Phase:
- public
- Convert:
- svn:c9d7d8f5-90d6-0310-b91f-818b3a526b0e/lemon/trunk@3437
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
lemon/capacity_scaling.h
r2553 r2556 23 23 /// 24 24 /// \file 25 /// \brief The capacity scaling algorithm for finding a minimum cost 26 /// flow. 25 /// \brief The capacity scaling algorithm for finding a minimum cost flow. 27 26 28 27 #include <lemon/graph_adaptor.h> … … 50 49 /// 51 50 /// \warning 52 /// - Edge capacities and costs should be non negative integers.51 /// - Edge capacities and costs should be non-negative integers. 53 52 /// However \c CostMap::Value should be signed type. 54 53 /// - Supply values should be signed integers. … … 67 66 class CapacityScaling 68 67 { 69 typedef typename Graph::Node Node; 70 typedef typename Graph::NodeIt NodeIt; 71 typedef typename Graph::Edge Edge; 72 typedef typename Graph::EdgeIt EdgeIt; 73 typedef typename Graph::InEdgeIt InEdgeIt; 74 typedef typename Graph::OutEdgeIt OutEdgeIt; 68 GRAPH_TYPEDEFS(typename Graph); 75 69 76 70 typedef typename LowerMap::Value Lower; … … 78 72 typedef typename CostMap::Value Cost; 79 73 typedef typename SupplyMap::Value Supply; 80 typedef typename Graph::template EdgeMap<Capacity> Capacity RefMap;81 typedef typename Graph::template NodeMap<Supply> Supply RefMap;74 typedef typename Graph::template EdgeMap<Capacity> CapacityEdgeMap; 75 typedef typename Graph::template NodeMap<Supply> SupplyNodeMap; 82 76 typedef typename Graph::template NodeMap<Edge> PredMap; 83 77 84 78 public: 85 79 86 /// \briefType to enable or disable capacity scaling.80 /// Type to enable or disable capacity scaling. 87 81 enum ScalingEnum { 88 82 WITH_SCALING = 0, … … 90 84 }; 91 85 92 /// \briefThe type of the flow map.93 typedef CapacityRefMapFlowMap;94 /// \briefThe type of the potential map.86 /// The type of the flow map. 87 typedef typename Graph::template EdgeMap<Capacity> FlowMap; 88 /// The type of the potential map. 95 89 typedef typename Graph::template NodeMap<Cost> PotentialMap; 96 90 … … 111 105 protected: 112 106 113 /// \briefThe directed graph the algorithm runs on.107 /// The directed graph the algorithm runs on. 114 108 const Graph &graph; 115 109 116 /// \briefThe flow map.110 /// The flow map. 117 111 const FlowMap &flow; 118 /// \briefThe residual capacity map.119 const Capacity RefMap &res_cap;120 /// \briefThe cost map.112 /// The residual capacity map. 113 const CapacityEdgeMap &res_cap; 114 /// The cost map. 121 115 const CostMap &cost; 122 /// \briefThe excess map.123 const Supply RefMap &excess;124 125 /// \briefThe potential map.116 /// The excess map. 117 const SupplyNodeMap &excess; 118 119 /// The potential map. 126 120 PotentialMap &potential; 127 121 128 /// \briefThe distance map.122 /// The distance map. 129 123 CostNodeMap dist; 130 /// \briefThe map of predecessors edges.124 /// The map of predecessors edges. 131 125 PredMap &pred; 132 /// \briefThe processed (i.e. permanently labeled) nodes.126 /// The processed (i.e. permanently labeled) nodes. 133 127 std::vector<Node> proc_nodes; 134 128 135 129 public: 136 130 137 /// \briefThe constructor of the class.131 /// The constructor of the class. 138 132 ResidualDijkstra( const Graph &_graph, 139 133 const FlowMap &_flow, 140 const Capacity RefMap &_res_cap,134 const CapacityEdgeMap &_res_cap, 141 135 const CostMap &_cost, 142 136 const SupplyMap &_excess, … … 148 142 {} 149 143 150 /// \briefRuns the algorithm from the given source node.144 /// Runs the algorithm from the given source node. 151 145 Node run(Node s, Capacity delta) { 152 146 HeapCrossRef heap_cross_ref(graph, Heap::PRE_HEAP); … … 223 217 protected: 224 218 225 /// \briefThe directed graph the algorithm runs on.219 /// The directed graph the algorithm runs on. 226 220 const Graph &graph; 227 /// \briefThe original lower bound map.221 /// The original lower bound map. 228 222 const LowerMap *lower; 229 /// \briefThe modified capacity map.230 Capacity RefMap capacity;231 /// \briefThe cost map.223 /// The modified capacity map. 224 CapacityEdgeMap capacity; 225 /// The cost map. 232 226 const CostMap &cost; 233 /// \brief The modified supply map. 234 SupplyRefMap supply; 235 /// \brief The sum of supply values equals zero. 227 /// The modified supply map. 228 SupplyNodeMap supply; 236 229 bool valid_supply; 237 230 238 /// \briefThe edge map of the current flow.231 /// The edge map of the current flow. 239 232 FlowMap flow; 240 /// \briefThe potential node map.233 /// The potential node map. 241 234 PotentialMap potential; 242 235 243 /// \briefThe residual capacity map.244 Capacity RefMap res_cap;245 /// \briefThe excess map.246 Supply RefMap excess;247 /// \brief The excess nodes (i.e.nodes with positive excess).236 /// The residual capacity map. 237 CapacityEdgeMap res_cap; 238 /// The excess map. 239 SupplyNodeMap excess; 240 /// The excess nodes (i.e. the nodes with positive excess). 248 241 std::vector<Node> excess_nodes; 249 /// \brief The index of the next excess node.250 int next_node;251 252 /// \briefThe scaling status (enabled or disabled).242 /// The deficit nodes (i.e. the nodes with negative excess). 243 std::vector<Node> deficit_nodes; 244 245 /// The scaling status (enabled or disabled). 253 246 ScalingEnum scaling; 254 /// \brief Thedelta parameter used for capacity scaling.247 /// The \c delta parameter used for capacity scaling. 255 248 Capacity delta; 256 /// \brief The maximum number of phases. 257 Capacity phase_num; 258 /// \brief The deficit nodes. 259 std::vector<Node> deficit_nodes; 249 /// The maximum number of phases. 250 int phase_num; 260 251 261 252 /// \brief Implementation of the \ref Dijkstra algorithm for 262 253 /// finding augmenting shortest paths in the residual network. 263 254 ResidualDijkstra dijkstra; 264 /// \briefThe map of predecessors edges.255 /// The map of predecessors edges. 265 256 PredMap pred; 266 257 … … 286 277 dijkstra(graph, flow, res_cap, cost, excess, potential, pred) 287 278 { 288 // Removing non zero lower bounds279 // Removing non-zero lower bounds 289 280 capacity = subMap(_capacity, _lower); 290 281 res_cap = capacity; … … 336 327 /// \param _t The target node. 337 328 /// \param _flow_value The required amount of flow from node \c _s 338 /// to node \c _t (i.e. the supply of \c _s and the demand of 339 /// \c _t). 329 /// to node \c _t (i.e. the supply of \c _s and the demand of \c _t). 340 330 CapacityScaling( const Graph &_graph, 341 331 const LowerMap &_lower, … … 349 339 dijkstra(graph, flow, res_cap, cost, excess, potential) 350 340 { 351 // Removing non zero lower bounds341 // Removing non-zero lower bounds 352 342 capacity = subMap(_capacity, _lower); 353 343 res_cap = capacity; … … 375 365 /// \param _t The target node. 376 366 /// \param _flow_value The required amount of flow from node \c _s 377 /// to node \c _t (i.e. the supply of \c _s and the demand of 378 /// \c _t). 367 /// to node \c _t (i.e. the supply of \c _s and the demand of \c _t). 379 368 CapacityScaling( const Graph &_graph, 380 369 const CapacityMap &_capacity, … … 392 381 } 393 382 383 /// \brief Runs the algorithm. 384 /// 385 /// Runs the algorithm. 386 /// 387 /// \param scaling_mode The scaling mode. In case of WITH_SCALING 388 /// capacity scaling is enabled in the algorithm (this is the 389 /// default) otherwise it is disabled. 390 /// If the maximum edge capacity and/or the amount of total supply 391 /// is small, the algorithm could be slightly faster without 392 /// scaling. 393 /// 394 /// \return \c true if a feasible flow can be found. 395 bool run(int scaling_mode = WITH_SCALING) { 396 return init(scaling_mode) && start(); 397 } 398 394 399 /// \brief Returns a const reference to the flow map. 395 400 /// … … 425 430 } 426 431 427 /// \brief Runs the algorithm.428 ///429 /// Runs the algorithm.430 ///431 /// \param scaling_mode The scaling mode. In case of WITH_SCALING432 /// capacity scaling is enabled in the algorithm (this is the433 /// default value) otherwise it is disabled.434 /// If the maximum edge capacity and/or the amount of total supply435 /// is small, the algorithm could be faster without scaling.436 ///437 /// \return \c true if a feasible flow can be found.438 bool run(int scaling_mode = WITH_SCALING) {439 return init(scaling_mode) && start();440 }441 442 432 protected: 443 433 444 /// \briefInitializes the algorithm.434 /// Initializes the algorithm. 445 435 bool init(int scaling_mode) { 446 436 if (!valid_supply) return false; … … 466 456 } 467 457 468 /// \briefExecutes the algorithm.458 /// Executes the algorithm. 469 459 bool start() { 470 460 if (delta > 1) … … 507 497 if (excess[n] <= -delta) deficit_nodes.push_back(n); 508 498 } 509 next_node = 0;499 int next_node = 0; 510 500 511 501 // Finding augmenting shortest paths … … 573 563 } 574 564 575 // Handling non zero lower bounds565 // Handling non-zero lower bounds 576 566 if (lower) { 577 567 for (EdgeIt e(graph); e != INVALID; ++e) … … 585 575 bool startWithoutScaling() { 586 576 // Finding excess nodes 587 for (NodeIt n(graph); n != INVALID; ++n) {577 for (NodeIt n(graph); n != INVALID; ++n) 588 578 if (excess[n] > 0) excess_nodes.push_back(n); 589 }590 579 if (excess_nodes.size() == 0) return true; 591 next_node = 0;580 int next_node = 0; 592 581 593 582 // Finding shortest paths … … 632 621 } 633 622 634 // Handling non zero lower bounds623 // Handling non-zero lower bounds 635 624 if (lower) { 636 625 for (EdgeIt e(graph); e != INVALID; ++e)
Note: See TracChangeset
for help on using the changeset viewer.