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