deba@2440
|
1 |
/* -*- C++ -*-
|
deba@2440
|
2 |
*
|
deba@2440
|
3 |
* This file is a part of LEMON, a generic C++ optimization library
|
deba@2440
|
4 |
*
|
deba@2440
|
5 |
* Copyright (C) 2003-2007
|
deba@2440
|
6 |
* Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
|
deba@2440
|
7 |
* (Egervary Research Group on Combinatorial Optimization, EGRES).
|
deba@2440
|
8 |
*
|
deba@2440
|
9 |
* Permission to use, modify and distribute this software is granted
|
deba@2440
|
10 |
* provided that this copyright notice appears in all copies. For
|
deba@2440
|
11 |
* precise terms see the accompanying LICENSE file.
|
deba@2440
|
12 |
*
|
deba@2440
|
13 |
* This software is provided "AS IS" with no warranty of any kind,
|
deba@2440
|
14 |
* express or implied, and with no claim as to its suitability for any
|
deba@2440
|
15 |
* purpose.
|
deba@2440
|
16 |
*
|
deba@2440
|
17 |
*/
|
deba@2440
|
18 |
|
deba@2440
|
19 |
#ifndef LEMON_CAPACITY_SCALING_H
|
deba@2440
|
20 |
#define LEMON_CAPACITY_SCALING_H
|
deba@2440
|
21 |
|
deba@2440
|
22 |
/// \ingroup min_cost_flow
|
deba@2440
|
23 |
///
|
deba@2440
|
24 |
/// \file
|
deba@2440
|
25 |
/// \brief The capacity scaling algorithm for finding a minimum cost
|
deba@2440
|
26 |
/// flow.
|
deba@2440
|
27 |
|
deba@2440
|
28 |
#include <vector>
|
deba@2440
|
29 |
#include <lemon/dijkstra.h>
|
deba@2440
|
30 |
#include <lemon/graph_adaptor.h>
|
deba@2440
|
31 |
|
deba@2440
|
32 |
#define WITH_SCALING
|
deba@2440
|
33 |
|
kpeter@2471
|
34 |
#ifdef WITH_SCALING
|
kpeter@2471
|
35 |
#define SCALING_FACTOR 2
|
kpeter@2471
|
36 |
#endif
|
kpeter@2471
|
37 |
|
deba@2457
|
38 |
//#define _DEBUG_ITER_
|
deba@2457
|
39 |
|
deba@2440
|
40 |
namespace lemon {
|
deba@2440
|
41 |
|
deba@2440
|
42 |
/// \addtogroup min_cost_flow
|
deba@2440
|
43 |
/// @{
|
deba@2440
|
44 |
|
deba@2440
|
45 |
/// \brief Implementation of the capacity scaling version of the
|
deba@2457
|
46 |
/// successive shortest path algorithm for finding a minimum cost flow.
|
deba@2440
|
47 |
///
|
deba@2440
|
48 |
/// \ref lemon::CapacityScaling "CapacityScaling" implements a
|
deba@2440
|
49 |
/// capacity scaling algorithm for finding a minimum cost flow.
|
deba@2440
|
50 |
///
|
deba@2440
|
51 |
/// \param Graph The directed graph type the algorithm runs on.
|
deba@2440
|
52 |
/// \param LowerMap The type of the lower bound map.
|
deba@2440
|
53 |
/// \param CapacityMap The type of the capacity (upper bound) map.
|
deba@2440
|
54 |
/// \param CostMap The type of the cost (length) map.
|
deba@2440
|
55 |
/// \param SupplyMap The type of the supply map.
|
deba@2440
|
56 |
///
|
deba@2440
|
57 |
/// \warning
|
deba@2440
|
58 |
/// - Edge capacities and costs should be nonnegative integers.
|
deba@2440
|
59 |
/// However \c CostMap::Value should be signed type.
|
deba@2440
|
60 |
/// - Supply values should be integers.
|
deba@2440
|
61 |
/// - \c LowerMap::Value must be convertible to
|
deba@2440
|
62 |
/// \c CapacityMap::Value and \c CapacityMap::Value must be
|
deba@2440
|
63 |
/// convertible to \c SupplyMap::Value.
|
deba@2440
|
64 |
///
|
deba@2440
|
65 |
/// \author Peter Kovacs
|
deba@2440
|
66 |
|
deba@2440
|
67 |
template < typename Graph,
|
deba@2440
|
68 |
typename LowerMap = typename Graph::template EdgeMap<int>,
|
deba@2440
|
69 |
typename CapacityMap = LowerMap,
|
deba@2440
|
70 |
typename CostMap = typename Graph::template EdgeMap<int>,
|
deba@2440
|
71 |
typename SupplyMap = typename Graph::template NodeMap
|
deba@2440
|
72 |
<typename CapacityMap::Value> >
|
deba@2440
|
73 |
class CapacityScaling
|
deba@2440
|
74 |
{
|
deba@2440
|
75 |
typedef typename Graph::Node Node;
|
deba@2440
|
76 |
typedef typename Graph::NodeIt NodeIt;
|
deba@2440
|
77 |
typedef typename Graph::Edge Edge;
|
deba@2440
|
78 |
typedef typename Graph::EdgeIt EdgeIt;
|
deba@2440
|
79 |
typedef typename Graph::InEdgeIt InEdgeIt;
|
deba@2440
|
80 |
typedef typename Graph::OutEdgeIt OutEdgeIt;
|
deba@2440
|
81 |
|
deba@2440
|
82 |
typedef typename LowerMap::Value Lower;
|
deba@2440
|
83 |
typedef typename CapacityMap::Value Capacity;
|
deba@2440
|
84 |
typedef typename CostMap::Value Cost;
|
deba@2440
|
85 |
typedef typename SupplyMap::Value Supply;
|
deba@2440
|
86 |
typedef typename Graph::template EdgeMap<Capacity> CapacityRefMap;
|
deba@2440
|
87 |
typedef typename Graph::template NodeMap<Supply> SupplyRefMap;
|
deba@2440
|
88 |
|
deba@2440
|
89 |
typedef ResGraphAdaptor< const Graph, Capacity,
|
deba@2440
|
90 |
CapacityRefMap, CapacityRefMap > ResGraph;
|
deba@2440
|
91 |
typedef typename ResGraph::Node ResNode;
|
deba@2440
|
92 |
typedef typename ResGraph::NodeIt ResNodeIt;
|
deba@2440
|
93 |
typedef typename ResGraph::Edge ResEdge;
|
deba@2440
|
94 |
typedef typename ResGraph::EdgeIt ResEdgeIt;
|
deba@2440
|
95 |
|
deba@2440
|
96 |
public:
|
deba@2440
|
97 |
|
deba@2440
|
98 |
/// \brief The type of the flow map.
|
deba@2440
|
99 |
typedef CapacityRefMap FlowMap;
|
deba@2440
|
100 |
/// \brief The type of the potential map.
|
deba@2440
|
101 |
typedef typename Graph::template NodeMap<Cost> PotentialMap;
|
deba@2440
|
102 |
|
deba@2440
|
103 |
protected:
|
deba@2440
|
104 |
|
deba@2440
|
105 |
/// \brief Map adaptor class for handling reduced edge costs.
|
deba@2440
|
106 |
class ReducedCostMap : public MapBase<ResEdge, Cost>
|
deba@2440
|
107 |
{
|
deba@2440
|
108 |
private:
|
deba@2440
|
109 |
|
deba@2440
|
110 |
const ResGraph &gr;
|
deba@2440
|
111 |
const CostMap &cost_map;
|
deba@2440
|
112 |
const PotentialMap &pot_map;
|
deba@2440
|
113 |
|
deba@2440
|
114 |
public:
|
deba@2440
|
115 |
|
deba@2440
|
116 |
typedef typename MapBase<ResEdge, Cost>::Value Value;
|
deba@2440
|
117 |
typedef typename MapBase<ResEdge, Cost>::Key Key;
|
deba@2440
|
118 |
|
deba@2440
|
119 |
ReducedCostMap( const ResGraph &_gr,
|
deba@2440
|
120 |
const CostMap &_cost,
|
deba@2440
|
121 |
const PotentialMap &_pot ) :
|
deba@2440
|
122 |
gr(_gr), cost_map(_cost), pot_map(_pot) {}
|
deba@2440
|
123 |
|
deba@2440
|
124 |
Value operator[](const Key &e) const {
|
deba@2440
|
125 |
return ResGraph::forward(e) ?
|
deba@2440
|
126 |
cost_map[e] - pot_map[gr.source(e)] + pot_map[gr.target(e)] :
|
deba@2440
|
127 |
-cost_map[e] - pot_map[gr.source(e)] + pot_map[gr.target(e)];
|
deba@2440
|
128 |
}
|
deba@2440
|
129 |
|
deba@2440
|
130 |
}; //class ReducedCostMap
|
deba@2440
|
131 |
|
deba@2440
|
132 |
/// \brief Map adaptor for \ref lemon::Dijkstra "Dijkstra" class to
|
deba@2440
|
133 |
/// update node potentials.
|
deba@2440
|
134 |
class PotentialUpdateMap : public MapBase<ResNode, Cost>
|
deba@2440
|
135 |
{
|
deba@2440
|
136 |
private:
|
deba@2440
|
137 |
|
deba@2440
|
138 |
PotentialMap *pot;
|
deba@2440
|
139 |
typedef std::pair<ResNode, Cost> Pair;
|
deba@2440
|
140 |
std::vector<Pair> data;
|
deba@2440
|
141 |
|
deba@2440
|
142 |
public:
|
deba@2440
|
143 |
|
deba@2440
|
144 |
typedef typename MapBase<ResNode, Cost>::Value Value;
|
deba@2440
|
145 |
typedef typename MapBase<ResNode, Cost>::Key Key;
|
deba@2440
|
146 |
|
deba@2440
|
147 |
void potentialMap(PotentialMap &_pot) {
|
deba@2440
|
148 |
pot = &_pot;
|
deba@2440
|
149 |
}
|
deba@2440
|
150 |
|
deba@2440
|
151 |
void init() {
|
deba@2440
|
152 |
data.clear();
|
deba@2440
|
153 |
}
|
deba@2440
|
154 |
|
deba@2440
|
155 |
void set(const Key &n, const Value &v) {
|
deba@2440
|
156 |
data.push_back(Pair(n, v));
|
deba@2440
|
157 |
}
|
deba@2440
|
158 |
|
deba@2440
|
159 |
void update() {
|
deba@2440
|
160 |
Cost val = data[data.size()-1].second;
|
deba@2440
|
161 |
for (int i = 0; i < data.size()-1; ++i)
|
deba@2440
|
162 |
(*pot)[data[i].first] += val - data[i].second;
|
deba@2440
|
163 |
}
|
deba@2440
|
164 |
|
deba@2440
|
165 |
}; //class PotentialUpdateMap
|
deba@2440
|
166 |
|
deba@2440
|
167 |
#ifdef WITH_SCALING
|
deba@2440
|
168 |
/// \brief Map adaptor class for identifing deficit nodes.
|
deba@2440
|
169 |
class DeficitBoolMap : public MapBase<ResNode, bool>
|
deba@2440
|
170 |
{
|
deba@2440
|
171 |
private:
|
deba@2440
|
172 |
|
deba@2440
|
173 |
const SupplyRefMap &imb;
|
deba@2440
|
174 |
const Capacity δ
|
deba@2440
|
175 |
|
deba@2440
|
176 |
public:
|
deba@2440
|
177 |
|
deba@2440
|
178 |
DeficitBoolMap(const SupplyRefMap &_imb, const Capacity &_delta) :
|
deba@2440
|
179 |
imb(_imb), delta(_delta) {}
|
deba@2440
|
180 |
|
deba@2440
|
181 |
bool operator[](const ResNode &n) const {
|
deba@2440
|
182 |
return imb[n] <= -delta;
|
deba@2440
|
183 |
}
|
deba@2440
|
184 |
|
deba@2440
|
185 |
}; //class DeficitBoolMap
|
deba@2440
|
186 |
|
deba@2440
|
187 |
/// \brief Map adaptor class for filtering edges with at least
|
deba@2440
|
188 |
/// \c delta residual capacity
|
deba@2440
|
189 |
class DeltaFilterMap : public MapBase<ResEdge, bool>
|
deba@2440
|
190 |
{
|
deba@2440
|
191 |
private:
|
deba@2440
|
192 |
|
deba@2440
|
193 |
const ResGraph &gr;
|
deba@2440
|
194 |
const Capacity δ
|
deba@2440
|
195 |
|
deba@2440
|
196 |
public:
|
deba@2440
|
197 |
|
deba@2440
|
198 |
typedef typename MapBase<ResEdge, Cost>::Value Value;
|
deba@2440
|
199 |
typedef typename MapBase<ResEdge, Cost>::Key Key;
|
deba@2440
|
200 |
|
deba@2440
|
201 |
DeltaFilterMap(const ResGraph &_gr, const Capacity &_delta) :
|
deba@2440
|
202 |
gr(_gr), delta(_delta) {}
|
deba@2440
|
203 |
|
deba@2440
|
204 |
Value operator[](const Key &e) const {
|
deba@2440
|
205 |
return gr.rescap(e) >= delta;
|
deba@2440
|
206 |
}
|
deba@2440
|
207 |
|
deba@2440
|
208 |
}; //class DeltaFilterMap
|
deba@2440
|
209 |
|
deba@2440
|
210 |
typedef EdgeSubGraphAdaptor<const ResGraph, const DeltaFilterMap>
|
deba@2440
|
211 |
DeltaResGraph;
|
deba@2440
|
212 |
|
deba@2440
|
213 |
/// \brief Traits class for \ref lemon::Dijkstra "Dijkstra" class.
|
deba@2440
|
214 |
class ResDijkstraTraits :
|
deba@2440
|
215 |
public DijkstraDefaultTraits<DeltaResGraph, ReducedCostMap>
|
deba@2440
|
216 |
{
|
deba@2440
|
217 |
public:
|
deba@2440
|
218 |
|
deba@2440
|
219 |
typedef PotentialUpdateMap DistMap;
|
deba@2440
|
220 |
|
deba@2440
|
221 |
static DistMap *createDistMap(const DeltaResGraph&) {
|
deba@2440
|
222 |
return new DistMap();
|
deba@2440
|
223 |
}
|
deba@2440
|
224 |
|
deba@2440
|
225 |
}; //class ResDijkstraTraits
|
deba@2440
|
226 |
|
deba@2440
|
227 |
#else //WITHOUT_CAPACITY_SCALING
|
deba@2440
|
228 |
/// \brief Map adaptor class for identifing deficit nodes.
|
deba@2440
|
229 |
class DeficitBoolMap : public MapBase<ResNode, bool>
|
deba@2440
|
230 |
{
|
deba@2440
|
231 |
private:
|
deba@2440
|
232 |
|
deba@2440
|
233 |
const SupplyRefMap &imb;
|
deba@2440
|
234 |
|
deba@2440
|
235 |
public:
|
deba@2440
|
236 |
|
deba@2440
|
237 |
DeficitBoolMap(const SupplyRefMap &_imb) : imb(_imb) {}
|
deba@2440
|
238 |
|
deba@2440
|
239 |
bool operator[](const ResNode &n) const {
|
deba@2440
|
240 |
return imb[n] < 0;
|
deba@2440
|
241 |
}
|
deba@2440
|
242 |
|
deba@2440
|
243 |
}; //class DeficitBoolMap
|
deba@2440
|
244 |
|
deba@2440
|
245 |
/// \brief Traits class for \ref lemon::Dijkstra "Dijkstra" class.
|
deba@2440
|
246 |
class ResDijkstraTraits :
|
deba@2440
|
247 |
public DijkstraDefaultTraits<ResGraph, ReducedCostMap>
|
deba@2440
|
248 |
{
|
deba@2440
|
249 |
public:
|
deba@2440
|
250 |
|
deba@2440
|
251 |
typedef PotentialUpdateMap DistMap;
|
deba@2440
|
252 |
|
deba@2440
|
253 |
static DistMap *createDistMap(const ResGraph&) {
|
deba@2440
|
254 |
return new DistMap();
|
deba@2440
|
255 |
}
|
deba@2440
|
256 |
|
deba@2440
|
257 |
}; //class ResDijkstraTraits
|
deba@2440
|
258 |
#endif
|
deba@2440
|
259 |
|
deba@2440
|
260 |
protected:
|
deba@2440
|
261 |
|
deba@2440
|
262 |
/// \brief The directed graph the algorithm runs on.
|
deba@2440
|
263 |
const Graph &graph;
|
deba@2440
|
264 |
/// \brief The original lower bound map.
|
deba@2440
|
265 |
const LowerMap *lower;
|
deba@2440
|
266 |
/// \brief The modified capacity map.
|
deba@2440
|
267 |
CapacityRefMap capacity;
|
deba@2440
|
268 |
/// \brief The cost map.
|
deba@2440
|
269 |
const CostMap &cost;
|
deba@2440
|
270 |
/// \brief The modified supply map.
|
deba@2440
|
271 |
SupplyRefMap supply;
|
deba@2440
|
272 |
/// \brief The sum of supply values equals zero.
|
deba@2440
|
273 |
bool valid_supply;
|
deba@2440
|
274 |
|
deba@2440
|
275 |
/// \brief The edge map of the current flow.
|
deba@2440
|
276 |
FlowMap flow;
|
deba@2440
|
277 |
/// \brief The potential node map.
|
deba@2440
|
278 |
PotentialMap potential;
|
deba@2440
|
279 |
/// \brief The residual graph.
|
deba@2440
|
280 |
ResGraph res_graph;
|
deba@2440
|
281 |
/// \brief The reduced cost map.
|
deba@2440
|
282 |
ReducedCostMap red_cost;
|
deba@2440
|
283 |
|
deba@2440
|
284 |
/// \brief The imbalance map.
|
deba@2440
|
285 |
SupplyRefMap imbalance;
|
deba@2440
|
286 |
/// \brief The excess nodes.
|
deba@2440
|
287 |
std::vector<ResNode> excess_nodes;
|
deba@2440
|
288 |
/// \brief The index of the next excess node.
|
deba@2440
|
289 |
int next_node;
|
deba@2440
|
290 |
|
deba@2440
|
291 |
#ifdef WITH_SCALING
|
deba@2440
|
292 |
typedef Dijkstra<DeltaResGraph, ReducedCostMap, ResDijkstraTraits>
|
deba@2440
|
293 |
ResDijkstra;
|
deba@2440
|
294 |
/// \brief \ref lemon::Dijkstra "Dijkstra" class for finding
|
deba@2440
|
295 |
/// shortest paths in the residual graph with respect to the
|
deba@2440
|
296 |
/// reduced edge costs.
|
deba@2440
|
297 |
ResDijkstra dijkstra;
|
deba@2440
|
298 |
|
deba@2440
|
299 |
/// \brief The delta parameter used for capacity scaling.
|
deba@2440
|
300 |
Capacity delta;
|
deba@2440
|
301 |
/// \brief Edge filter map.
|
deba@2440
|
302 |
DeltaFilterMap delta_filter;
|
deba@2440
|
303 |
/// \brief The delta residual graph.
|
deba@2440
|
304 |
DeltaResGraph dres_graph;
|
deba@2440
|
305 |
/// \brief Map for identifing deficit nodes.
|
deba@2440
|
306 |
DeficitBoolMap delta_deficit;
|
deba@2440
|
307 |
|
deba@2457
|
308 |
/// \brief The deficit nodes.
|
deba@2457
|
309 |
std::vector<ResNode> deficit_nodes;
|
deba@2457
|
310 |
|
deba@2440
|
311 |
#else //WITHOUT_CAPACITY_SCALING
|
deba@2440
|
312 |
typedef Dijkstra<ResGraph, ReducedCostMap, ResDijkstraTraits>
|
deba@2440
|
313 |
ResDijkstra;
|
deba@2440
|
314 |
/// \brief \ref lemon::Dijkstra "Dijkstra" class for finding
|
deba@2440
|
315 |
/// shortest paths in the residual graph with respect to the
|
deba@2440
|
316 |
/// reduced edge costs.
|
deba@2440
|
317 |
ResDijkstra dijkstra;
|
deba@2440
|
318 |
/// \brief Map for identifing deficit nodes.
|
deba@2440
|
319 |
DeficitBoolMap has_deficit;
|
deba@2440
|
320 |
#endif
|
deba@2440
|
321 |
|
deba@2440
|
322 |
/// \brief Pred map for the \ref lemon::Dijkstra "Dijkstra" class.
|
deba@2440
|
323 |
typename ResDijkstra::PredMap pred;
|
deba@2440
|
324 |
/// \brief Dist map for the \ref lemon::Dijkstra "Dijkstra" class to
|
deba@2440
|
325 |
/// update node potentials.
|
deba@2440
|
326 |
PotentialUpdateMap updater;
|
deba@2440
|
327 |
|
deba@2440
|
328 |
public :
|
deba@2440
|
329 |
|
deba@2440
|
330 |
/// \brief General constructor of the class (with lower bounds).
|
deba@2440
|
331 |
///
|
deba@2440
|
332 |
/// General constructor of the class (with lower bounds).
|
deba@2440
|
333 |
///
|
deba@2440
|
334 |
/// \param _graph The directed graph the algorithm runs on.
|
deba@2440
|
335 |
/// \param _lower The lower bounds of the edges.
|
deba@2440
|
336 |
/// \param _capacity The capacities (upper bounds) of the edges.
|
deba@2440
|
337 |
/// \param _cost The cost (length) values of the edges.
|
deba@2440
|
338 |
/// \param _supply The supply values of the nodes (signed).
|
deba@2440
|
339 |
CapacityScaling( const Graph &_graph,
|
deba@2440
|
340 |
const LowerMap &_lower,
|
deba@2440
|
341 |
const CapacityMap &_capacity,
|
deba@2440
|
342 |
const CostMap &_cost,
|
deba@2440
|
343 |
const SupplyMap &_supply ) :
|
deba@2440
|
344 |
graph(_graph), lower(&_lower), capacity(_graph), cost(_cost),
|
deba@2440
|
345 |
supply(_graph), flow(_graph, 0), potential(_graph, 0),
|
deba@2440
|
346 |
res_graph(_graph, capacity, flow),
|
deba@2440
|
347 |
red_cost(res_graph, cost, potential), imbalance(_graph),
|
deba@2440
|
348 |
#ifdef WITH_SCALING
|
deba@2440
|
349 |
delta(0), delta_filter(res_graph, delta),
|
deba@2440
|
350 |
dres_graph(res_graph, delta_filter),
|
deba@2440
|
351 |
dijkstra(dres_graph, red_cost), pred(dres_graph),
|
deba@2440
|
352 |
delta_deficit(imbalance, delta)
|
deba@2440
|
353 |
#else //WITHOUT_CAPACITY_SCALING
|
deba@2440
|
354 |
dijkstra(res_graph, red_cost), pred(res_graph),
|
deba@2440
|
355 |
has_deficit(imbalance)
|
deba@2440
|
356 |
#endif
|
deba@2440
|
357 |
{
|
deba@2440
|
358 |
// Removing nonzero lower bounds
|
deba@2440
|
359 |
capacity = subMap(_capacity, _lower);
|
deba@2440
|
360 |
Supply sum = 0;
|
deba@2440
|
361 |
for (NodeIt n(graph); n != INVALID; ++n) {
|
deba@2440
|
362 |
Supply s = _supply[n];
|
deba@2440
|
363 |
for (InEdgeIt e(graph, n); e != INVALID; ++e)
|
deba@2440
|
364 |
s += _lower[e];
|
deba@2440
|
365 |
for (OutEdgeIt e(graph, n); e != INVALID; ++e)
|
deba@2440
|
366 |
s -= _lower[e];
|
deba@2440
|
367 |
supply[n] = imbalance[n] = s;
|
deba@2440
|
368 |
sum += s;
|
deba@2440
|
369 |
}
|
deba@2440
|
370 |
valid_supply = sum == 0;
|
deba@2440
|
371 |
}
|
deba@2440
|
372 |
|
deba@2440
|
373 |
/// \brief General constructor of the class (without lower bounds).
|
deba@2440
|
374 |
///
|
deba@2440
|
375 |
/// General constructor of the class (without lower bounds).
|
deba@2440
|
376 |
///
|
deba@2440
|
377 |
/// \param _graph The directed graph the algorithm runs on.
|
deba@2440
|
378 |
/// \param _capacity The capacities (upper bounds) of the edges.
|
deba@2440
|
379 |
/// \param _cost The cost (length) values of the edges.
|
deba@2440
|
380 |
/// \param _supply The supply values of the nodes (signed).
|
deba@2440
|
381 |
CapacityScaling( const Graph &_graph,
|
deba@2440
|
382 |
const CapacityMap &_capacity,
|
deba@2440
|
383 |
const CostMap &_cost,
|
deba@2440
|
384 |
const SupplyMap &_supply ) :
|
deba@2440
|
385 |
graph(_graph), lower(NULL), capacity(_capacity), cost(_cost),
|
deba@2440
|
386 |
supply(_supply), flow(_graph, 0), potential(_graph, 0),
|
deba@2440
|
387 |
res_graph(_graph, capacity, flow),
|
deba@2440
|
388 |
red_cost(res_graph, cost, potential), imbalance(_graph),
|
deba@2440
|
389 |
#ifdef WITH_SCALING
|
deba@2440
|
390 |
delta(0), delta_filter(res_graph, delta),
|
deba@2440
|
391 |
dres_graph(res_graph, delta_filter),
|
deba@2440
|
392 |
dijkstra(dres_graph, red_cost), pred(dres_graph),
|
deba@2440
|
393 |
delta_deficit(imbalance, delta)
|
deba@2440
|
394 |
#else //WITHOUT_CAPACITY_SCALING
|
deba@2440
|
395 |
dijkstra(res_graph, red_cost), pred(res_graph),
|
deba@2440
|
396 |
has_deficit(imbalance)
|
deba@2440
|
397 |
#endif
|
deba@2440
|
398 |
{
|
deba@2440
|
399 |
// Checking the sum of supply values
|
deba@2440
|
400 |
Supply sum = 0;
|
deba@2440
|
401 |
for (NodeIt n(graph); n != INVALID; ++n) sum += supply[n];
|
deba@2440
|
402 |
valid_supply = sum == 0;
|
deba@2440
|
403 |
}
|
deba@2440
|
404 |
|
deba@2440
|
405 |
/// \brief Simple constructor of the class (with lower bounds).
|
deba@2440
|
406 |
///
|
deba@2440
|
407 |
/// Simple constructor of the class (with lower bounds).
|
deba@2440
|
408 |
///
|
deba@2440
|
409 |
/// \param _graph The directed graph the algorithm runs on.
|
deba@2440
|
410 |
/// \param _lower The lower bounds of the edges.
|
deba@2440
|
411 |
/// \param _capacity The capacities (upper bounds) of the edges.
|
deba@2440
|
412 |
/// \param _cost The cost (length) values of the edges.
|
deba@2440
|
413 |
/// \param _s The source node.
|
deba@2440
|
414 |
/// \param _t The target node.
|
deba@2440
|
415 |
/// \param _flow_value The required amount of flow from node \c _s
|
deba@2440
|
416 |
/// to node \c _t (i.e. the supply of \c _s and the demand of
|
deba@2440
|
417 |
/// \c _t).
|
deba@2440
|
418 |
CapacityScaling( const Graph &_graph,
|
deba@2440
|
419 |
const LowerMap &_lower,
|
deba@2440
|
420 |
const CapacityMap &_capacity,
|
deba@2440
|
421 |
const CostMap &_cost,
|
deba@2440
|
422 |
Node _s, Node _t,
|
deba@2440
|
423 |
Supply _flow_value ) :
|
deba@2440
|
424 |
graph(_graph), lower(&_lower), capacity(_graph), cost(_cost),
|
deba@2440
|
425 |
supply(_graph), flow(_graph, 0), potential(_graph, 0),
|
deba@2440
|
426 |
res_graph(_graph, capacity, flow),
|
deba@2440
|
427 |
red_cost(res_graph, cost, potential), imbalance(_graph),
|
deba@2440
|
428 |
#ifdef WITH_SCALING
|
deba@2440
|
429 |
delta(0), delta_filter(res_graph, delta),
|
deba@2440
|
430 |
dres_graph(res_graph, delta_filter),
|
deba@2440
|
431 |
dijkstra(dres_graph, red_cost), pred(dres_graph),
|
deba@2440
|
432 |
delta_deficit(imbalance, delta)
|
deba@2440
|
433 |
#else //WITHOUT_CAPACITY_SCALING
|
deba@2440
|
434 |
dijkstra(res_graph, red_cost), pred(res_graph),
|
deba@2440
|
435 |
has_deficit(imbalance)
|
deba@2440
|
436 |
#endif
|
deba@2440
|
437 |
{
|
deba@2440
|
438 |
// Removing nonzero lower bounds
|
deba@2440
|
439 |
capacity = subMap(_capacity, _lower);
|
deba@2440
|
440 |
for (NodeIt n(graph); n != INVALID; ++n) {
|
deba@2440
|
441 |
Supply s = 0;
|
deba@2440
|
442 |
if (n == _s) s = _flow_value;
|
deba@2440
|
443 |
if (n == _t) s = -_flow_value;
|
deba@2440
|
444 |
for (InEdgeIt e(graph, n); e != INVALID; ++e)
|
deba@2440
|
445 |
s += _lower[e];
|
deba@2440
|
446 |
for (OutEdgeIt e(graph, n); e != INVALID; ++e)
|
deba@2440
|
447 |
s -= _lower[e];
|
deba@2440
|
448 |
supply[n] = imbalance[n] = s;
|
deba@2440
|
449 |
}
|
deba@2440
|
450 |
valid_supply = true;
|
deba@2440
|
451 |
}
|
deba@2440
|
452 |
|
deba@2440
|
453 |
/// \brief Simple constructor of the class (without lower bounds).
|
deba@2440
|
454 |
///
|
deba@2440
|
455 |
/// Simple constructor of the class (without lower bounds).
|
deba@2440
|
456 |
///
|
deba@2440
|
457 |
/// \param _graph The directed graph the algorithm runs on.
|
deba@2440
|
458 |
/// \param _capacity The capacities (upper bounds) of the edges.
|
deba@2440
|
459 |
/// \param _cost The cost (length) values of the edges.
|
deba@2440
|
460 |
/// \param _s The source node.
|
deba@2440
|
461 |
/// \param _t The target node.
|
deba@2440
|
462 |
/// \param _flow_value The required amount of flow from node \c _s
|
deba@2440
|
463 |
/// to node \c _t (i.e. the supply of \c _s and the demand of
|
deba@2440
|
464 |
/// \c _t).
|
deba@2440
|
465 |
CapacityScaling( const Graph &_graph,
|
deba@2440
|
466 |
const CapacityMap &_capacity,
|
deba@2440
|
467 |
const CostMap &_cost,
|
deba@2440
|
468 |
Node _s, Node _t,
|
deba@2440
|
469 |
Supply _flow_value ) :
|
deba@2440
|
470 |
graph(_graph), lower(NULL), capacity(_capacity), cost(_cost),
|
deba@2440
|
471 |
supply(_graph, 0), flow(_graph, 0), potential(_graph, 0),
|
deba@2440
|
472 |
res_graph(_graph, capacity, flow),
|
deba@2440
|
473 |
red_cost(res_graph, cost, potential), imbalance(_graph),
|
deba@2440
|
474 |
#ifdef WITH_SCALING
|
deba@2440
|
475 |
delta(0), delta_filter(res_graph, delta),
|
deba@2440
|
476 |
dres_graph(res_graph, delta_filter),
|
deba@2440
|
477 |
dijkstra(dres_graph, red_cost), pred(dres_graph),
|
deba@2440
|
478 |
delta_deficit(imbalance, delta)
|
deba@2440
|
479 |
#else //WITHOUT_CAPACITY_SCALING
|
deba@2440
|
480 |
dijkstra(res_graph, red_cost), pred(res_graph),
|
deba@2440
|
481 |
has_deficit(imbalance)
|
deba@2440
|
482 |
#endif
|
deba@2440
|
483 |
{
|
deba@2440
|
484 |
supply[_s] = _flow_value;
|
deba@2440
|
485 |
supply[_t] = -_flow_value;
|
deba@2440
|
486 |
valid_supply = true;
|
deba@2440
|
487 |
}
|
deba@2440
|
488 |
|
deba@2440
|
489 |
/// \brief Returns a const reference to the flow map.
|
deba@2440
|
490 |
///
|
deba@2440
|
491 |
/// Returns a const reference to the flow map.
|
deba@2440
|
492 |
///
|
deba@2440
|
493 |
/// \pre \ref run() must be called before using this function.
|
deba@2440
|
494 |
const FlowMap& flowMap() const {
|
deba@2440
|
495 |
return flow;
|
deba@2440
|
496 |
}
|
deba@2440
|
497 |
|
deba@2440
|
498 |
/// \brief Returns a const reference to the potential map (the dual
|
deba@2440
|
499 |
/// solution).
|
deba@2440
|
500 |
///
|
deba@2440
|
501 |
/// Returns a const reference to the potential map (the dual
|
deba@2440
|
502 |
/// solution).
|
deba@2440
|
503 |
///
|
deba@2440
|
504 |
/// \pre \ref run() must be called before using this function.
|
deba@2440
|
505 |
const PotentialMap& potentialMap() const {
|
deba@2440
|
506 |
return potential;
|
deba@2440
|
507 |
}
|
deba@2440
|
508 |
|
deba@2440
|
509 |
/// \brief Returns the total cost of the found flow.
|
deba@2440
|
510 |
///
|
deba@2440
|
511 |
/// Returns the total cost of the found flow. The complexity of the
|
deba@2440
|
512 |
/// function is \f$ O(e) \f$.
|
deba@2440
|
513 |
///
|
deba@2440
|
514 |
/// \pre \ref run() must be called before using this function.
|
deba@2440
|
515 |
Cost totalCost() const {
|
deba@2440
|
516 |
Cost c = 0;
|
deba@2440
|
517 |
for (EdgeIt e(graph); e != INVALID; ++e)
|
deba@2440
|
518 |
c += flow[e] * cost[e];
|
deba@2440
|
519 |
return c;
|
deba@2440
|
520 |
}
|
deba@2440
|
521 |
|
deba@2457
|
522 |
/// \brief Runs the algorithm.
|
deba@2440
|
523 |
///
|
deba@2457
|
524 |
/// Runs the algorithm.
|
deba@2440
|
525 |
///
|
deba@2440
|
526 |
/// \return \c true if a feasible flow can be found.
|
deba@2440
|
527 |
bool run() {
|
deba@2440
|
528 |
return init() && start();
|
deba@2440
|
529 |
}
|
deba@2440
|
530 |
|
deba@2440
|
531 |
protected:
|
deba@2440
|
532 |
|
deba@2440
|
533 |
/// \brief Initializes the algorithm.
|
deba@2440
|
534 |
bool init() {
|
deba@2440
|
535 |
if (!valid_supply) return false;
|
deba@2440
|
536 |
|
deba@2440
|
537 |
// Initalizing Dijkstra class
|
deba@2440
|
538 |
updater.potentialMap(potential);
|
deba@2440
|
539 |
dijkstra.distMap(updater).predMap(pred);
|
deba@2440
|
540 |
|
deba@2440
|
541 |
#ifdef WITH_SCALING
|
deba@2440
|
542 |
// Initilaizing delta value
|
deba@2457
|
543 |
Supply max_sup = 0, max_dem = 0;
|
deba@2457
|
544 |
for (NodeIt n(graph); n != INVALID; ++n) {
|
deba@2457
|
545 |
if (supply[n] > max_sup) max_sup = supply[n];
|
deba@2457
|
546 |
if (supply[n] < -max_dem) max_dem = -supply[n];
|
deba@2440
|
547 |
}
|
deba@2457
|
548 |
if (max_dem < max_sup) max_sup = max_dem;
|
kpeter@2471
|
549 |
for ( delta = 1; SCALING_FACTOR * delta < max_sup;
|
kpeter@2471
|
550 |
delta *= SCALING_FACTOR ) ;
|
deba@2440
|
551 |
#endif
|
deba@2440
|
552 |
return true;
|
deba@2440
|
553 |
}
|
deba@2440
|
554 |
|
deba@2440
|
555 |
#ifdef WITH_SCALING
|
deba@2440
|
556 |
/// \brief Executes the capacity scaling version of the successive
|
deba@2440
|
557 |
/// shortest path algorithm.
|
deba@2440
|
558 |
bool start() {
|
deba@2440
|
559 |
typedef typename DeltaResGraph::EdgeIt DeltaResEdgeIt;
|
deba@2440
|
560 |
typedef typename DeltaResGraph::Edge DeltaResEdge;
|
deba@2457
|
561 |
#ifdef _DEBUG_ITER_
|
deba@2457
|
562 |
int dijk_num = 0;
|
deba@2457
|
563 |
#endif
|
deba@2440
|
564 |
|
deba@2440
|
565 |
// Processing capacity scaling phases
|
deba@2440
|
566 |
ResNode s, t;
|
kpeter@2471
|
567 |
for ( ; delta >= 1; delta = delta < SCALING_FACTOR && delta > 1 ?
|
kpeter@2471
|
568 |
1 : delta / SCALING_FACTOR )
|
deba@2440
|
569 |
{
|
deba@2440
|
570 |
// Saturating edges not satisfying the optimality condition
|
deba@2440
|
571 |
Capacity r;
|
deba@2440
|
572 |
for (DeltaResEdgeIt e(dres_graph); e != INVALID; ++e) {
|
deba@2440
|
573 |
if (red_cost[e] < 0) {
|
deba@2440
|
574 |
res_graph.augment(e, r = res_graph.rescap(e));
|
deba@2440
|
575 |
imbalance[dres_graph.target(e)] += r;
|
deba@2440
|
576 |
imbalance[dres_graph.source(e)] -= r;
|
deba@2440
|
577 |
}
|
deba@2440
|
578 |
}
|
deba@2440
|
579 |
|
deba@2440
|
580 |
// Finding excess nodes
|
deba@2440
|
581 |
excess_nodes.clear();
|
deba@2457
|
582 |
deficit_nodes.clear();
|
deba@2440
|
583 |
for (ResNodeIt n(res_graph); n != INVALID; ++n) {
|
deba@2457
|
584 |
if (imbalance[n] >= delta) excess_nodes.push_back(n);
|
deba@2457
|
585 |
if (imbalance[n] <= -delta) deficit_nodes.push_back(n);
|
deba@2440
|
586 |
}
|
deba@2440
|
587 |
next_node = 0;
|
deba@2440
|
588 |
|
deba@2457
|
589 |
// Finding shortest paths
|
deba@2440
|
590 |
while (next_node < excess_nodes.size()) {
|
deba@2457
|
591 |
// Checking deficit nodes
|
deba@2457
|
592 |
if (delta > 1) {
|
deba@2457
|
593 |
bool delta_def = false;
|
deba@2457
|
594 |
for (int i = 0; i < deficit_nodes.size(); ++i) {
|
deba@2457
|
595 |
if (imbalance[deficit_nodes[i]] <= -delta) {
|
deba@2457
|
596 |
delta_def = true;
|
deba@2457
|
597 |
break;
|
deba@2457
|
598 |
}
|
deba@2457
|
599 |
}
|
deba@2457
|
600 |
if (!delta_def) break;
|
deba@2457
|
601 |
}
|
deba@2457
|
602 |
|
deba@2440
|
603 |
// Running Dijkstra
|
deba@2440
|
604 |
s = excess_nodes[next_node];
|
deba@2440
|
605 |
updater.init();
|
deba@2440
|
606 |
dijkstra.init();
|
deba@2440
|
607 |
dijkstra.addSource(s);
|
deba@2457
|
608 |
#ifdef _DEBUG_ITER_
|
deba@2457
|
609 |
++dijk_num;
|
deba@2457
|
610 |
#endif
|
deba@2440
|
611 |
if ((t = dijkstra.start(delta_deficit)) == INVALID) {
|
deba@2440
|
612 |
if (delta > 1) {
|
deba@2440
|
613 |
++next_node;
|
deba@2440
|
614 |
continue;
|
deba@2440
|
615 |
}
|
deba@2440
|
616 |
return false;
|
deba@2440
|
617 |
}
|
deba@2440
|
618 |
|
deba@2440
|
619 |
// Updating node potentials
|
deba@2440
|
620 |
updater.update();
|
deba@2440
|
621 |
|
deba@2440
|
622 |
// Augment along a shortest path from s to t
|
deba@2440
|
623 |
Capacity d = imbalance[s] < -imbalance[t] ?
|
deba@2440
|
624 |
imbalance[s] : -imbalance[t];
|
deba@2440
|
625 |
ResNode u = t;
|
deba@2440
|
626 |
ResEdge e;
|
deba@2440
|
627 |
if (d > delta) {
|
deba@2440
|
628 |
while ((e = pred[u]) != INVALID) {
|
deba@2440
|
629 |
if (res_graph.rescap(e) < d) d = res_graph.rescap(e);
|
deba@2440
|
630 |
u = dres_graph.source(e);
|
deba@2440
|
631 |
}
|
deba@2440
|
632 |
}
|
deba@2440
|
633 |
u = t;
|
deba@2440
|
634 |
while ((e = pred[u]) != INVALID) {
|
deba@2440
|
635 |
res_graph.augment(e, d);
|
deba@2440
|
636 |
u = dres_graph.source(e);
|
deba@2440
|
637 |
}
|
deba@2440
|
638 |
imbalance[s] -= d;
|
deba@2440
|
639 |
imbalance[t] += d;
|
deba@2440
|
640 |
if (imbalance[s] < delta) ++next_node;
|
deba@2440
|
641 |
}
|
deba@2440
|
642 |
}
|
deba@2457
|
643 |
#ifdef _DEBUG_ITER_
|
deba@2457
|
644 |
std::cout << "Cost Scaling algorithm finished with running Dijkstra algorithm "
|
deba@2457
|
645 |
<< dijk_num << " times." << std::endl;
|
deba@2457
|
646 |
#endif
|
deba@2440
|
647 |
|
deba@2440
|
648 |
// Handling nonzero lower bounds
|
deba@2440
|
649 |
if (lower) {
|
deba@2440
|
650 |
for (EdgeIt e(graph); e != INVALID; ++e)
|
deba@2440
|
651 |
flow[e] += (*lower)[e];
|
deba@2440
|
652 |
}
|
deba@2440
|
653 |
return true;
|
deba@2440
|
654 |
}
|
deba@2440
|
655 |
|
deba@2440
|
656 |
#else //WITHOUT_CAPACITY_SCALING
|
deba@2440
|
657 |
/// \brief Executes the successive shortest path algorithm without
|
deba@2440
|
658 |
/// capacity scaling.
|
deba@2440
|
659 |
bool start() {
|
deba@2440
|
660 |
// Finding excess nodes
|
deba@2440
|
661 |
for (ResNodeIt n(res_graph); n != INVALID; ++n) {
|
deba@2440
|
662 |
if (imbalance[n] > 0) excess_nodes.push_back(n);
|
deba@2440
|
663 |
}
|
deba@2440
|
664 |
if (excess_nodes.size() == 0) return true;
|
deba@2440
|
665 |
next_node = 0;
|
deba@2440
|
666 |
|
deba@2457
|
667 |
// Finding shortest paths
|
deba@2440
|
668 |
ResNode s, t;
|
deba@2440
|
669 |
while ( imbalance[excess_nodes[next_node]] > 0 ||
|
deba@2440
|
670 |
++next_node < excess_nodes.size() )
|
deba@2440
|
671 |
{
|
deba@2440
|
672 |
// Running Dijkstra
|
deba@2440
|
673 |
s = excess_nodes[next_node];
|
deba@2440
|
674 |
updater.init();
|
deba@2440
|
675 |
dijkstra.init();
|
deba@2440
|
676 |
dijkstra.addSource(s);
|
deba@2440
|
677 |
if ((t = dijkstra.start(has_deficit)) == INVALID)
|
deba@2440
|
678 |
return false;
|
deba@2440
|
679 |
|
deba@2440
|
680 |
// Updating node potentials
|
deba@2440
|
681 |
updater.update();
|
deba@2440
|
682 |
|
deba@2440
|
683 |
// Augmenting along a shortest path from s to t
|
deba@2440
|
684 |
Capacity delta = imbalance[s] < -imbalance[t] ?
|
deba@2440
|
685 |
imbalance[s] : -imbalance[t];
|
deba@2440
|
686 |
ResNode u = t;
|
deba@2440
|
687 |
ResEdge e;
|
deba@2440
|
688 |
while ((e = pred[u]) != INVALID) {
|
deba@2440
|
689 |
if (res_graph.rescap(e) < delta) delta = res_graph.rescap(e);
|
deba@2440
|
690 |
u = res_graph.source(e);
|
deba@2440
|
691 |
}
|
deba@2440
|
692 |
u = t;
|
deba@2440
|
693 |
while ((e = pred[u]) != INVALID) {
|
deba@2440
|
694 |
res_graph.augment(e, delta);
|
deba@2440
|
695 |
u = res_graph.source(e);
|
deba@2440
|
696 |
}
|
deba@2440
|
697 |
imbalance[s] -= delta;
|
deba@2440
|
698 |
imbalance[t] += delta;
|
deba@2440
|
699 |
}
|
deba@2440
|
700 |
|
deba@2440
|
701 |
// Handling nonzero lower bounds
|
deba@2440
|
702 |
if (lower) {
|
deba@2440
|
703 |
for (EdgeIt e(graph); e != INVALID; ++e)
|
deba@2440
|
704 |
flow[e] += (*lower)[e];
|
deba@2440
|
705 |
}
|
deba@2440
|
706 |
return true;
|
deba@2440
|
707 |
}
|
deba@2440
|
708 |
#endif
|
deba@2440
|
709 |
|
deba@2440
|
710 |
}; //class CapacityScaling
|
deba@2440
|
711 |
|
deba@2440
|
712 |
///@}
|
deba@2440
|
713 |
|
deba@2440
|
714 |
} //namespace lemon
|
deba@2440
|
715 |
|
deba@2440
|
716 |
#endif //LEMON_CAPACITY_SCALING_H
|