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