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