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 |
*
|
alpar@2553
|
5 |
* Copyright (C) 2003-2008
|
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
|
kpeter@2556
|
25 |
/// \brief The capacity scaling algorithm for finding a minimum cost flow.
|
deba@2440
|
26 |
|
kpeter@2535
|
27 |
#include <lemon/graph_adaptor.h>
|
kpeter@2535
|
28 |
#include <lemon/bin_heap.h>
|
deba@2440
|
29 |
#include <vector>
|
deba@2457
|
30 |
|
deba@2440
|
31 |
namespace lemon {
|
deba@2440
|
32 |
|
deba@2440
|
33 |
/// \addtogroup min_cost_flow
|
deba@2440
|
34 |
/// @{
|
deba@2440
|
35 |
|
deba@2440
|
36 |
/// \brief Implementation of the capacity scaling version of the
|
kpeter@2509
|
37 |
/// successive shortest path algorithm for finding a minimum cost
|
kpeter@2509
|
38 |
/// flow.
|
deba@2440
|
39 |
///
|
kpeter@2535
|
40 |
/// \ref CapacityScaling implements the capacity scaling version
|
kpeter@2535
|
41 |
/// of the successive shortest path algorithm for finding a minimum
|
kpeter@2535
|
42 |
/// cost flow.
|
deba@2440
|
43 |
///
|
deba@2440
|
44 |
/// \param Graph The directed graph type the algorithm runs on.
|
deba@2440
|
45 |
/// \param LowerMap The type of the lower bound map.
|
deba@2440
|
46 |
/// \param CapacityMap The type of the capacity (upper bound) map.
|
deba@2440
|
47 |
/// \param CostMap The type of the cost (length) map.
|
deba@2440
|
48 |
/// \param SupplyMap The type of the supply map.
|
deba@2440
|
49 |
///
|
deba@2440
|
50 |
/// \warning
|
kpeter@2556
|
51 |
/// - Edge capacities and costs should be non-negative integers.
|
kpeter@2535
|
52 |
/// However \c CostMap::Value should be signed type.
|
kpeter@2509
|
53 |
/// - Supply values should be signed integers.
|
deba@2440
|
54 |
/// - \c LowerMap::Value must be convertible to
|
kpeter@2535
|
55 |
/// \c CapacityMap::Value and \c CapacityMap::Value must be
|
kpeter@2535
|
56 |
/// convertible to \c SupplyMap::Value.
|
deba@2440
|
57 |
///
|
deba@2440
|
58 |
/// \author Peter Kovacs
|
deba@2440
|
59 |
|
kpeter@2533
|
60 |
template < typename Graph,
|
kpeter@2535
|
61 |
typename LowerMap = typename Graph::template EdgeMap<int>,
|
kpeter@2535
|
62 |
typename CapacityMap = LowerMap,
|
kpeter@2535
|
63 |
typename CostMap = typename Graph::template EdgeMap<int>,
|
kpeter@2535
|
64 |
typename SupplyMap = typename Graph::template NodeMap
|
kpeter@2535
|
65 |
<typename CapacityMap::Value> >
|
deba@2440
|
66 |
class CapacityScaling
|
deba@2440
|
67 |
{
|
kpeter@2556
|
68 |
GRAPH_TYPEDEFS(typename Graph);
|
deba@2440
|
69 |
|
deba@2440
|
70 |
typedef typename LowerMap::Value Lower;
|
deba@2440
|
71 |
typedef typename CapacityMap::Value Capacity;
|
deba@2440
|
72 |
typedef typename CostMap::Value Cost;
|
deba@2440
|
73 |
typedef typename SupplyMap::Value Supply;
|
kpeter@2556
|
74 |
typedef typename Graph::template EdgeMap<Capacity> CapacityEdgeMap;
|
kpeter@2556
|
75 |
typedef typename Graph::template NodeMap<Supply> SupplyNodeMap;
|
kpeter@2535
|
76 |
typedef typename Graph::template NodeMap<Edge> PredMap;
|
deba@2440
|
77 |
|
deba@2440
|
78 |
public:
|
deba@2440
|
79 |
|
kpeter@2556
|
80 |
/// Type to enable or disable capacity scaling.
|
kpeter@2535
|
81 |
enum ScalingEnum {
|
kpeter@2535
|
82 |
WITH_SCALING = 0,
|
kpeter@2535
|
83 |
WITHOUT_SCALING = -1
|
kpeter@2535
|
84 |
};
|
kpeter@2535
|
85 |
|
kpeter@2556
|
86 |
/// The type of the flow map.
|
kpeter@2556
|
87 |
typedef typename Graph::template EdgeMap<Capacity> FlowMap;
|
kpeter@2556
|
88 |
/// The type of the potential map.
|
deba@2440
|
89 |
typedef typename Graph::template NodeMap<Cost> PotentialMap;
|
deba@2440
|
90 |
|
deba@2440
|
91 |
protected:
|
deba@2440
|
92 |
|
kpeter@2535
|
93 |
/// \brief Special implementation of the \ref Dijkstra algorithm
|
kpeter@2535
|
94 |
/// for finding shortest paths in the residual network of the graph
|
kpeter@2535
|
95 |
/// with respect to the reduced edge costs and modifying the
|
kpeter@2535
|
96 |
/// node potentials according to the distance of the nodes.
|
kpeter@2535
|
97 |
class ResidualDijkstra
|
deba@2440
|
98 |
{
|
kpeter@2535
|
99 |
typedef typename Graph::template NodeMap<Cost> CostNodeMap;
|
kpeter@2535
|
100 |
typedef typename Graph::template NodeMap<Edge> PredMap;
|
deba@2440
|
101 |
|
kpeter@2535
|
102 |
typedef typename Graph::template NodeMap<int> HeapCrossRef;
|
kpeter@2535
|
103 |
typedef BinHeap<Cost, HeapCrossRef> Heap;
|
kpeter@2535
|
104 |
|
kpeter@2535
|
105 |
protected:
|
kpeter@2535
|
106 |
|
kpeter@2556
|
107 |
/// The directed graph the algorithm runs on.
|
kpeter@2535
|
108 |
const Graph &graph;
|
kpeter@2535
|
109 |
|
kpeter@2556
|
110 |
/// The flow map.
|
kpeter@2535
|
111 |
const FlowMap &flow;
|
kpeter@2556
|
112 |
/// The residual capacity map.
|
kpeter@2556
|
113 |
const CapacityEdgeMap &res_cap;
|
kpeter@2556
|
114 |
/// The cost map.
|
kpeter@2535
|
115 |
const CostMap &cost;
|
kpeter@2556
|
116 |
/// The excess map.
|
kpeter@2556
|
117 |
const SupplyNodeMap &excess;
|
kpeter@2535
|
118 |
|
kpeter@2556
|
119 |
/// The potential map.
|
kpeter@2535
|
120 |
PotentialMap &potential;
|
kpeter@2535
|
121 |
|
kpeter@2556
|
122 |
/// The distance map.
|
kpeter@2535
|
123 |
CostNodeMap dist;
|
kpeter@2556
|
124 |
/// The map of predecessors edges.
|
kpeter@2535
|
125 |
PredMap &pred;
|
kpeter@2556
|
126 |
/// The processed (i.e. permanently labeled) nodes.
|
kpeter@2535
|
127 |
std::vector<Node> proc_nodes;
|
deba@2440
|
128 |
|
deba@2440
|
129 |
public:
|
deba@2440
|
130 |
|
kpeter@2556
|
131 |
/// The constructor of the class.
|
kpeter@2535
|
132 |
ResidualDijkstra( const Graph &_graph,
|
kpeter@2535
|
133 |
const FlowMap &_flow,
|
kpeter@2556
|
134 |
const CapacityEdgeMap &_res_cap,
|
kpeter@2535
|
135 |
const CostMap &_cost,
|
kpeter@2535
|
136 |
const SupplyMap &_excess,
|
kpeter@2535
|
137 |
PotentialMap &_potential,
|
kpeter@2535
|
138 |
PredMap &_pred ) :
|
kpeter@2535
|
139 |
graph(_graph), flow(_flow), res_cap(_res_cap), cost(_cost),
|
kpeter@2535
|
140 |
excess(_excess), potential(_potential), dist(_graph),
|
kpeter@2535
|
141 |
pred(_pred)
|
kpeter@2535
|
142 |
{}
|
deba@2440
|
143 |
|
kpeter@2556
|
144 |
/// Runs the algorithm from the given source node.
|
kpeter@2535
|
145 |
Node run(Node s, Capacity delta) {
|
kpeter@2535
|
146 |
HeapCrossRef heap_cross_ref(graph, Heap::PRE_HEAP);
|
kpeter@2535
|
147 |
Heap heap(heap_cross_ref);
|
kpeter@2535
|
148 |
heap.push(s, 0);
|
kpeter@2535
|
149 |
pred[s] = INVALID;
|
kpeter@2535
|
150 |
proc_nodes.clear();
|
kpeter@2535
|
151 |
|
kpeter@2535
|
152 |
// Processing nodes
|
kpeter@2535
|
153 |
while (!heap.empty() && excess[heap.top()] > -delta) {
|
kpeter@2535
|
154 |
Node u = heap.top(), v;
|
kpeter@2535
|
155 |
Cost d = heap.prio() - potential[u], nd;
|
kpeter@2535
|
156 |
dist[u] = heap.prio();
|
kpeter@2535
|
157 |
heap.pop();
|
kpeter@2535
|
158 |
proc_nodes.push_back(u);
|
kpeter@2535
|
159 |
|
kpeter@2535
|
160 |
// Traversing outgoing edges
|
kpeter@2535
|
161 |
for (OutEdgeIt e(graph, u); e != INVALID; ++e) {
|
kpeter@2535
|
162 |
if (res_cap[e] >= delta) {
|
kpeter@2535
|
163 |
v = graph.target(e);
|
kpeter@2535
|
164 |
switch(heap.state(v)) {
|
kpeter@2535
|
165 |
case Heap::PRE_HEAP:
|
kpeter@2535
|
166 |
heap.push(v, d + cost[e] + potential[v]);
|
kpeter@2535
|
167 |
pred[v] = e;
|
kpeter@2535
|
168 |
break;
|
kpeter@2535
|
169 |
case Heap::IN_HEAP:
|
kpeter@2535
|
170 |
nd = d + cost[e] + potential[v];
|
kpeter@2535
|
171 |
if (nd < heap[v]) {
|
kpeter@2535
|
172 |
heap.decrease(v, nd);
|
kpeter@2535
|
173 |
pred[v] = e;
|
kpeter@2535
|
174 |
}
|
kpeter@2535
|
175 |
break;
|
kpeter@2535
|
176 |
case Heap::POST_HEAP:
|
kpeter@2535
|
177 |
break;
|
kpeter@2535
|
178 |
}
|
kpeter@2535
|
179 |
}
|
kpeter@2535
|
180 |
}
|
kpeter@2535
|
181 |
|
kpeter@2535
|
182 |
// Traversing incoming edges
|
kpeter@2535
|
183 |
for (InEdgeIt e(graph, u); e != INVALID; ++e) {
|
kpeter@2535
|
184 |
if (flow[e] >= delta) {
|
kpeter@2535
|
185 |
v = graph.source(e);
|
kpeter@2535
|
186 |
switch(heap.state(v)) {
|
kpeter@2535
|
187 |
case Heap::PRE_HEAP:
|
kpeter@2535
|
188 |
heap.push(v, d - cost[e] + potential[v]);
|
kpeter@2535
|
189 |
pred[v] = e;
|
kpeter@2535
|
190 |
break;
|
kpeter@2535
|
191 |
case Heap::IN_HEAP:
|
kpeter@2535
|
192 |
nd = d - cost[e] + potential[v];
|
kpeter@2535
|
193 |
if (nd < heap[v]) {
|
kpeter@2535
|
194 |
heap.decrease(v, nd);
|
kpeter@2535
|
195 |
pred[v] = e;
|
kpeter@2535
|
196 |
}
|
kpeter@2535
|
197 |
break;
|
kpeter@2535
|
198 |
case Heap::POST_HEAP:
|
kpeter@2535
|
199 |
break;
|
kpeter@2535
|
200 |
}
|
kpeter@2535
|
201 |
}
|
kpeter@2535
|
202 |
}
|
kpeter@2535
|
203 |
}
|
kpeter@2535
|
204 |
if (heap.empty()) return INVALID;
|
kpeter@2535
|
205 |
|
kpeter@2535
|
206 |
// Updating potentials of processed nodes
|
kpeter@2535
|
207 |
Node t = heap.top();
|
kpeter@2535
|
208 |
Cost dt = heap.prio();
|
kpeter@2535
|
209 |
for (int i = 0; i < proc_nodes.size(); ++i)
|
kpeter@2535
|
210 |
potential[proc_nodes[i]] -= dist[proc_nodes[i]] - dt;
|
kpeter@2535
|
211 |
|
kpeter@2535
|
212 |
return t;
|
deba@2440
|
213 |
}
|
deba@2440
|
214 |
|
kpeter@2535
|
215 |
}; //class ResidualDijkstra
|
deba@2440
|
216 |
|
deba@2440
|
217 |
protected:
|
deba@2440
|
218 |
|
kpeter@2556
|
219 |
/// The directed graph the algorithm runs on.
|
deba@2440
|
220 |
const Graph &graph;
|
kpeter@2556
|
221 |
/// The original lower bound map.
|
deba@2440
|
222 |
const LowerMap *lower;
|
kpeter@2556
|
223 |
/// The modified capacity map.
|
kpeter@2556
|
224 |
CapacityEdgeMap capacity;
|
kpeter@2556
|
225 |
/// The cost map.
|
deba@2440
|
226 |
const CostMap &cost;
|
kpeter@2556
|
227 |
/// The modified supply map.
|
kpeter@2556
|
228 |
SupplyNodeMap supply;
|
deba@2440
|
229 |
bool valid_supply;
|
deba@2440
|
230 |
|
kpeter@2556
|
231 |
/// The edge map of the current flow.
|
deba@2440
|
232 |
FlowMap flow;
|
kpeter@2556
|
233 |
/// The potential node map.
|
deba@2440
|
234 |
PotentialMap potential;
|
deba@2440
|
235 |
|
kpeter@2556
|
236 |
/// The residual capacity map.
|
kpeter@2556
|
237 |
CapacityEdgeMap res_cap;
|
kpeter@2556
|
238 |
/// The excess map.
|
kpeter@2556
|
239 |
SupplyNodeMap excess;
|
kpeter@2556
|
240 |
/// The excess nodes (i.e. the nodes with positive excess).
|
kpeter@2535
|
241 |
std::vector<Node> excess_nodes;
|
kpeter@2556
|
242 |
/// The deficit nodes (i.e. the nodes with negative excess).
|
kpeter@2556
|
243 |
std::vector<Node> deficit_nodes;
|
deba@2440
|
244 |
|
kpeter@2556
|
245 |
/// The scaling status (enabled or disabled).
|
kpeter@2535
|
246 |
ScalingEnum scaling;
|
kpeter@2556
|
247 |
/// The \c delta parameter used for capacity scaling.
|
deba@2440
|
248 |
Capacity delta;
|
kpeter@2556
|
249 |
/// The maximum number of phases.
|
kpeter@2556
|
250 |
int phase_num;
|
deba@2440
|
251 |
|
kpeter@2535
|
252 |
/// \brief Implementation of the \ref Dijkstra algorithm for
|
kpeter@2535
|
253 |
/// finding augmenting shortest paths in the residual network.
|
kpeter@2535
|
254 |
ResidualDijkstra dijkstra;
|
kpeter@2556
|
255 |
/// The map of predecessors edges.
|
kpeter@2535
|
256 |
PredMap pred;
|
deba@2440
|
257 |
|
deba@2440
|
258 |
public :
|
deba@2440
|
259 |
|
deba@2440
|
260 |
/// \brief General constructor of the class (with lower bounds).
|
deba@2440
|
261 |
///
|
deba@2440
|
262 |
/// General constructor of the class (with lower bounds).
|
deba@2440
|
263 |
///
|
deba@2440
|
264 |
/// \param _graph The directed graph the algorithm runs on.
|
deba@2440
|
265 |
/// \param _lower The lower bounds of the edges.
|
deba@2440
|
266 |
/// \param _capacity The capacities (upper bounds) of the edges.
|
deba@2440
|
267 |
/// \param _cost The cost (length) values of the edges.
|
deba@2440
|
268 |
/// \param _supply The supply values of the nodes (signed).
|
deba@2440
|
269 |
CapacityScaling( const Graph &_graph,
|
kpeter@2535
|
270 |
const LowerMap &_lower,
|
kpeter@2535
|
271 |
const CapacityMap &_capacity,
|
kpeter@2535
|
272 |
const CostMap &_cost,
|
kpeter@2535
|
273 |
const SupplyMap &_supply ) :
|
deba@2440
|
274 |
graph(_graph), lower(&_lower), capacity(_graph), cost(_cost),
|
deba@2440
|
275 |
supply(_graph), flow(_graph, 0), potential(_graph, 0),
|
kpeter@2535
|
276 |
res_cap(_graph), excess(_graph), pred(_graph),
|
kpeter@2535
|
277 |
dijkstra(graph, flow, res_cap, cost, excess, potential, pred)
|
deba@2440
|
278 |
{
|
kpeter@2556
|
279 |
// Removing non-zero lower bounds
|
deba@2440
|
280 |
capacity = subMap(_capacity, _lower);
|
kpeter@2535
|
281 |
res_cap = capacity;
|
deba@2440
|
282 |
Supply sum = 0;
|
deba@2440
|
283 |
for (NodeIt n(graph); n != INVALID; ++n) {
|
kpeter@2535
|
284 |
Supply s = _supply[n];
|
kpeter@2535
|
285 |
for (InEdgeIt e(graph, n); e != INVALID; ++e)
|
kpeter@2535
|
286 |
s += _lower[e];
|
kpeter@2535
|
287 |
for (OutEdgeIt e(graph, n); e != INVALID; ++e)
|
kpeter@2535
|
288 |
s -= _lower[e];
|
kpeter@2535
|
289 |
supply[n] = s;
|
kpeter@2535
|
290 |
sum += s;
|
deba@2440
|
291 |
}
|
deba@2440
|
292 |
valid_supply = sum == 0;
|
deba@2440
|
293 |
}
|
deba@2440
|
294 |
|
deba@2440
|
295 |
/// \brief General constructor of the class (without lower bounds).
|
deba@2440
|
296 |
///
|
deba@2440
|
297 |
/// General constructor of the class (without lower bounds).
|
deba@2440
|
298 |
///
|
deba@2440
|
299 |
/// \param _graph The directed graph the algorithm runs on.
|
deba@2440
|
300 |
/// \param _capacity The capacities (upper bounds) of the edges.
|
deba@2440
|
301 |
/// \param _cost The cost (length) values of the edges.
|
deba@2440
|
302 |
/// \param _supply The supply values of the nodes (signed).
|
deba@2440
|
303 |
CapacityScaling( const Graph &_graph,
|
kpeter@2535
|
304 |
const CapacityMap &_capacity,
|
kpeter@2535
|
305 |
const CostMap &_cost,
|
kpeter@2535
|
306 |
const SupplyMap &_supply ) :
|
deba@2440
|
307 |
graph(_graph), lower(NULL), capacity(_capacity), cost(_cost),
|
deba@2440
|
308 |
supply(_supply), flow(_graph, 0), potential(_graph, 0),
|
kpeter@2535
|
309 |
res_cap(_capacity), excess(_graph), pred(_graph),
|
kpeter@2535
|
310 |
dijkstra(graph, flow, res_cap, cost, excess, potential)
|
deba@2440
|
311 |
{
|
deba@2440
|
312 |
// Checking the sum of supply values
|
deba@2440
|
313 |
Supply sum = 0;
|
deba@2440
|
314 |
for (NodeIt n(graph); n != INVALID; ++n) sum += supply[n];
|
deba@2440
|
315 |
valid_supply = sum == 0;
|
deba@2440
|
316 |
}
|
deba@2440
|
317 |
|
deba@2440
|
318 |
/// \brief Simple constructor of the class (with lower bounds).
|
deba@2440
|
319 |
///
|
deba@2440
|
320 |
/// Simple constructor of the class (with lower bounds).
|
deba@2440
|
321 |
///
|
deba@2440
|
322 |
/// \param _graph The directed graph the algorithm runs on.
|
deba@2440
|
323 |
/// \param _lower The lower bounds of the edges.
|
deba@2440
|
324 |
/// \param _capacity The capacities (upper bounds) of the edges.
|
deba@2440
|
325 |
/// \param _cost The cost (length) values of the edges.
|
deba@2440
|
326 |
/// \param _s The source node.
|
deba@2440
|
327 |
/// \param _t The target node.
|
deba@2440
|
328 |
/// \param _flow_value The required amount of flow from node \c _s
|
kpeter@2556
|
329 |
/// to node \c _t (i.e. the supply of \c _s and the demand of \c _t).
|
deba@2440
|
330 |
CapacityScaling( const Graph &_graph,
|
kpeter@2535
|
331 |
const LowerMap &_lower,
|
kpeter@2535
|
332 |
const CapacityMap &_capacity,
|
kpeter@2535
|
333 |
const CostMap &_cost,
|
kpeter@2535
|
334 |
Node _s, Node _t,
|
kpeter@2535
|
335 |
Supply _flow_value ) :
|
deba@2440
|
336 |
graph(_graph), lower(&_lower), capacity(_graph), cost(_cost),
|
deba@2440
|
337 |
supply(_graph), flow(_graph, 0), potential(_graph, 0),
|
kpeter@2535
|
338 |
res_cap(_graph), excess(_graph), pred(_graph),
|
kpeter@2535
|
339 |
dijkstra(graph, flow, res_cap, cost, excess, potential)
|
deba@2440
|
340 |
{
|
kpeter@2556
|
341 |
// Removing non-zero lower bounds
|
deba@2440
|
342 |
capacity = subMap(_capacity, _lower);
|
kpeter@2535
|
343 |
res_cap = capacity;
|
deba@2440
|
344 |
for (NodeIt n(graph); n != INVALID; ++n) {
|
kpeter@2535
|
345 |
Supply s = 0;
|
kpeter@2535
|
346 |
if (n == _s) s = _flow_value;
|
kpeter@2535
|
347 |
if (n == _t) s = -_flow_value;
|
kpeter@2535
|
348 |
for (InEdgeIt e(graph, n); e != INVALID; ++e)
|
kpeter@2535
|
349 |
s += _lower[e];
|
kpeter@2535
|
350 |
for (OutEdgeIt e(graph, n); e != INVALID; ++e)
|
kpeter@2535
|
351 |
s -= _lower[e];
|
kpeter@2535
|
352 |
supply[n] = s;
|
deba@2440
|
353 |
}
|
deba@2440
|
354 |
valid_supply = true;
|
deba@2440
|
355 |
}
|
deba@2440
|
356 |
|
deba@2440
|
357 |
/// \brief Simple constructor of the class (without lower bounds).
|
deba@2440
|
358 |
///
|
deba@2440
|
359 |
/// Simple constructor of the class (without lower bounds).
|
deba@2440
|
360 |
///
|
deba@2440
|
361 |
/// \param _graph The directed graph the algorithm runs on.
|
deba@2440
|
362 |
/// \param _capacity The capacities (upper bounds) of the edges.
|
deba@2440
|
363 |
/// \param _cost The cost (length) values of the edges.
|
deba@2440
|
364 |
/// \param _s The source node.
|
deba@2440
|
365 |
/// \param _t The target node.
|
deba@2440
|
366 |
/// \param _flow_value The required amount of flow from node \c _s
|
kpeter@2556
|
367 |
/// to node \c _t (i.e. the supply of \c _s and the demand of \c _t).
|
deba@2440
|
368 |
CapacityScaling( const Graph &_graph,
|
kpeter@2535
|
369 |
const CapacityMap &_capacity,
|
kpeter@2535
|
370 |
const CostMap &_cost,
|
kpeter@2535
|
371 |
Node _s, Node _t,
|
kpeter@2535
|
372 |
Supply _flow_value ) :
|
deba@2440
|
373 |
graph(_graph), lower(NULL), capacity(_capacity), cost(_cost),
|
deba@2440
|
374 |
supply(_graph, 0), flow(_graph, 0), potential(_graph, 0),
|
kpeter@2535
|
375 |
res_cap(_capacity), excess(_graph), pred(_graph),
|
kpeter@2535
|
376 |
dijkstra(graph, flow, res_cap, cost, excess, potential)
|
deba@2440
|
377 |
{
|
deba@2440
|
378 |
supply[_s] = _flow_value;
|
deba@2440
|
379 |
supply[_t] = -_flow_value;
|
deba@2440
|
380 |
valid_supply = true;
|
deba@2440
|
381 |
}
|
deba@2440
|
382 |
|
kpeter@2556
|
383 |
/// \brief Runs the algorithm.
|
kpeter@2556
|
384 |
///
|
kpeter@2556
|
385 |
/// Runs the algorithm.
|
kpeter@2556
|
386 |
///
|
kpeter@2556
|
387 |
/// \param scaling_mode The scaling mode. In case of WITH_SCALING
|
kpeter@2556
|
388 |
/// capacity scaling is enabled in the algorithm (this is the
|
kpeter@2556
|
389 |
/// default) otherwise it is disabled.
|
kpeter@2556
|
390 |
/// If the maximum edge capacity and/or the amount of total supply
|
kpeter@2556
|
391 |
/// is small, the algorithm could be slightly faster without
|
kpeter@2556
|
392 |
/// scaling.
|
kpeter@2556
|
393 |
///
|
kpeter@2556
|
394 |
/// \return \c true if a feasible flow can be found.
|
kpeter@2556
|
395 |
bool run(int scaling_mode = WITH_SCALING) {
|
kpeter@2556
|
396 |
return init(scaling_mode) && start();
|
kpeter@2556
|
397 |
}
|
kpeter@2556
|
398 |
|
deba@2440
|
399 |
/// \brief Returns a const reference to the flow map.
|
deba@2440
|
400 |
///
|
deba@2440
|
401 |
/// Returns a const reference to the flow map.
|
deba@2440
|
402 |
///
|
deba@2440
|
403 |
/// \pre \ref run() must be called before using this function.
|
deba@2440
|
404 |
const FlowMap& flowMap() const {
|
deba@2440
|
405 |
return flow;
|
deba@2440
|
406 |
}
|
deba@2440
|
407 |
|
deba@2440
|
408 |
/// \brief Returns a const reference to the potential map (the dual
|
deba@2440
|
409 |
/// solution).
|
deba@2440
|
410 |
///
|
deba@2440
|
411 |
/// Returns a const reference to the potential map (the dual
|
deba@2440
|
412 |
/// solution).
|
deba@2440
|
413 |
///
|
deba@2440
|
414 |
/// \pre \ref run() must be called before using this function.
|
deba@2440
|
415 |
const PotentialMap& potentialMap() const {
|
deba@2440
|
416 |
return potential;
|
deba@2440
|
417 |
}
|
deba@2440
|
418 |
|
deba@2440
|
419 |
/// \brief Returns the total cost of the found flow.
|
deba@2440
|
420 |
///
|
deba@2440
|
421 |
/// Returns the total cost of the found flow. The complexity of the
|
deba@2440
|
422 |
/// function is \f$ O(e) \f$.
|
deba@2440
|
423 |
///
|
deba@2440
|
424 |
/// \pre \ref run() must be called before using this function.
|
deba@2440
|
425 |
Cost totalCost() const {
|
deba@2440
|
426 |
Cost c = 0;
|
deba@2440
|
427 |
for (EdgeIt e(graph); e != INVALID; ++e)
|
kpeter@2535
|
428 |
c += flow[e] * cost[e];
|
deba@2440
|
429 |
return c;
|
deba@2440
|
430 |
}
|
deba@2440
|
431 |
|
deba@2440
|
432 |
protected:
|
deba@2440
|
433 |
|
kpeter@2556
|
434 |
/// Initializes the algorithm.
|
kpeter@2535
|
435 |
bool init(int scaling_mode) {
|
deba@2440
|
436 |
if (!valid_supply) return false;
|
kpeter@2535
|
437 |
excess = supply;
|
deba@2440
|
438 |
|
deba@2440
|
439 |
// Initilaizing delta value
|
kpeter@2535
|
440 |
if (scaling_mode == WITH_SCALING) {
|
kpeter@2535
|
441 |
// With scaling
|
kpeter@2535
|
442 |
Supply max_sup = 0, max_dem = 0;
|
kpeter@2535
|
443 |
for (NodeIt n(graph); n != INVALID; ++n) {
|
kpeter@2535
|
444 |
if ( supply[n] > max_sup) max_sup = supply[n];
|
kpeter@2535
|
445 |
if (-supply[n] > max_dem) max_dem = -supply[n];
|
kpeter@2535
|
446 |
}
|
kpeter@2535
|
447 |
if (max_dem < max_sup) max_sup = max_dem;
|
kpeter@2535
|
448 |
phase_num = 0;
|
kpeter@2535
|
449 |
for (delta = 1; 2 * delta <= max_sup; delta *= 2)
|
kpeter@2535
|
450 |
++phase_num;
|
kpeter@2535
|
451 |
} else {
|
kpeter@2535
|
452 |
// Without scaling
|
kpeter@2535
|
453 |
delta = 1;
|
deba@2440
|
454 |
}
|
deba@2440
|
455 |
return true;
|
deba@2440
|
456 |
}
|
deba@2440
|
457 |
|
kpeter@2556
|
458 |
/// Executes the algorithm.
|
kpeter@2535
|
459 |
bool start() {
|
kpeter@2535
|
460 |
if (delta > 1)
|
kpeter@2535
|
461 |
return startWithScaling();
|
kpeter@2535
|
462 |
else
|
kpeter@2535
|
463 |
return startWithoutScaling();
|
kpeter@2535
|
464 |
}
|
kpeter@2535
|
465 |
|
kpeter@2535
|
466 |
/// \brief Executes the capacity scaling version of the successive
|
kpeter@2535
|
467 |
/// shortest path algorithm.
|
kpeter@2535
|
468 |
bool startWithScaling() {
|
kpeter@2535
|
469 |
// Processing capacity scaling phases
|
kpeter@2535
|
470 |
Node s, t;
|
kpeter@2535
|
471 |
int phase_cnt = 0;
|
kpeter@2535
|
472 |
int factor = 4;
|
kpeter@2535
|
473 |
while (true) {
|
kpeter@2535
|
474 |
// Saturating all edges not satisfying the optimality condition
|
kpeter@2535
|
475 |
for (EdgeIt e(graph); e != INVALID; ++e) {
|
kpeter@2535
|
476 |
Node u = graph.source(e), v = graph.target(e);
|
kpeter@2535
|
477 |
Cost c = cost[e] - potential[u] + potential[v];
|
kpeter@2535
|
478 |
if (c < 0 && res_cap[e] >= delta) {
|
kpeter@2535
|
479 |
excess[u] -= res_cap[e];
|
kpeter@2535
|
480 |
excess[v] += res_cap[e];
|
kpeter@2535
|
481 |
flow[e] = capacity[e];
|
kpeter@2535
|
482 |
res_cap[e] = 0;
|
kpeter@2535
|
483 |
}
|
kpeter@2535
|
484 |
else if (c > 0 && flow[e] >= delta) {
|
kpeter@2535
|
485 |
excess[u] += flow[e];
|
kpeter@2535
|
486 |
excess[v] -= flow[e];
|
kpeter@2535
|
487 |
flow[e] = 0;
|
kpeter@2535
|
488 |
res_cap[e] = capacity[e];
|
kpeter@2535
|
489 |
}
|
kpeter@2535
|
490 |
}
|
kpeter@2535
|
491 |
|
kpeter@2535
|
492 |
// Finding excess nodes and deficit nodes
|
kpeter@2535
|
493 |
excess_nodes.clear();
|
kpeter@2535
|
494 |
deficit_nodes.clear();
|
kpeter@2535
|
495 |
for (NodeIt n(graph); n != INVALID; ++n) {
|
kpeter@2535
|
496 |
if (excess[n] >= delta) excess_nodes.push_back(n);
|
kpeter@2535
|
497 |
if (excess[n] <= -delta) deficit_nodes.push_back(n);
|
kpeter@2535
|
498 |
}
|
kpeter@2556
|
499 |
int next_node = 0;
|
kpeter@2535
|
500 |
|
kpeter@2535
|
501 |
// Finding augmenting shortest paths
|
kpeter@2535
|
502 |
while (next_node < excess_nodes.size()) {
|
kpeter@2535
|
503 |
// Checking deficit nodes
|
kpeter@2535
|
504 |
if (delta > 1) {
|
kpeter@2535
|
505 |
bool delta_deficit = false;
|
kpeter@2535
|
506 |
for (int i = 0; i < deficit_nodes.size(); ++i) {
|
kpeter@2535
|
507 |
if (excess[deficit_nodes[i]] <= -delta) {
|
kpeter@2535
|
508 |
delta_deficit = true;
|
kpeter@2535
|
509 |
break;
|
kpeter@2535
|
510 |
}
|
kpeter@2535
|
511 |
}
|
kpeter@2535
|
512 |
if (!delta_deficit) break;
|
kpeter@2535
|
513 |
}
|
kpeter@2535
|
514 |
|
kpeter@2535
|
515 |
// Running Dijkstra
|
kpeter@2535
|
516 |
s = excess_nodes[next_node];
|
kpeter@2535
|
517 |
if ((t = dijkstra.run(s, delta)) == INVALID) {
|
kpeter@2535
|
518 |
if (delta > 1) {
|
kpeter@2535
|
519 |
++next_node;
|
kpeter@2535
|
520 |
continue;
|
kpeter@2535
|
521 |
}
|
kpeter@2535
|
522 |
return false;
|
kpeter@2535
|
523 |
}
|
kpeter@2535
|
524 |
|
kpeter@2535
|
525 |
// Augmenting along a shortest path from s to t.
|
kpeter@2535
|
526 |
Capacity d = excess[s] < -excess[t] ? excess[s] : -excess[t];
|
kpeter@2535
|
527 |
Node u = t;
|
kpeter@2535
|
528 |
Edge e;
|
kpeter@2535
|
529 |
if (d > delta) {
|
kpeter@2535
|
530 |
while ((e = pred[u]) != INVALID) {
|
kpeter@2535
|
531 |
Capacity rc;
|
kpeter@2535
|
532 |
if (u == graph.target(e)) {
|
kpeter@2535
|
533 |
rc = res_cap[e];
|
kpeter@2535
|
534 |
u = graph.source(e);
|
kpeter@2535
|
535 |
} else {
|
kpeter@2535
|
536 |
rc = flow[e];
|
kpeter@2535
|
537 |
u = graph.target(e);
|
kpeter@2535
|
538 |
}
|
kpeter@2535
|
539 |
if (rc < d) d = rc;
|
kpeter@2535
|
540 |
}
|
kpeter@2535
|
541 |
}
|
kpeter@2535
|
542 |
u = t;
|
kpeter@2535
|
543 |
while ((e = pred[u]) != INVALID) {
|
kpeter@2535
|
544 |
if (u == graph.target(e)) {
|
kpeter@2535
|
545 |
flow[e] += d;
|
kpeter@2535
|
546 |
res_cap[e] -= d;
|
kpeter@2535
|
547 |
u = graph.source(e);
|
kpeter@2535
|
548 |
} else {
|
kpeter@2535
|
549 |
flow[e] -= d;
|
kpeter@2535
|
550 |
res_cap[e] += d;
|
kpeter@2535
|
551 |
u = graph.target(e);
|
kpeter@2535
|
552 |
}
|
kpeter@2535
|
553 |
}
|
kpeter@2535
|
554 |
excess[s] -= d;
|
kpeter@2535
|
555 |
excess[t] += d;
|
kpeter@2535
|
556 |
|
kpeter@2535
|
557 |
if (excess[s] < delta) ++next_node;
|
kpeter@2535
|
558 |
}
|
kpeter@2535
|
559 |
|
kpeter@2535
|
560 |
if (delta == 1) break;
|
kpeter@2535
|
561 |
if (++phase_cnt > phase_num / 4) factor = 2;
|
kpeter@2535
|
562 |
delta = delta <= factor ? 1 : delta / factor;
|
kpeter@2535
|
563 |
}
|
kpeter@2535
|
564 |
|
kpeter@2556
|
565 |
// Handling non-zero lower bounds
|
kpeter@2535
|
566 |
if (lower) {
|
kpeter@2535
|
567 |
for (EdgeIt e(graph); e != INVALID; ++e)
|
kpeter@2535
|
568 |
flow[e] += (*lower)[e];
|
kpeter@2535
|
569 |
}
|
kpeter@2535
|
570 |
return true;
|
kpeter@2535
|
571 |
}
|
kpeter@2535
|
572 |
|
deba@2440
|
573 |
/// \brief Executes the successive shortest path algorithm without
|
deba@2440
|
574 |
/// capacity scaling.
|
kpeter@2535
|
575 |
bool startWithoutScaling() {
|
deba@2440
|
576 |
// Finding excess nodes
|
kpeter@2556
|
577 |
for (NodeIt n(graph); n != INVALID; ++n)
|
kpeter@2535
|
578 |
if (excess[n] > 0) excess_nodes.push_back(n);
|
deba@2440
|
579 |
if (excess_nodes.size() == 0) return true;
|
kpeter@2556
|
580 |
int next_node = 0;
|
deba@2440
|
581 |
|
deba@2457
|
582 |
// Finding shortest paths
|
kpeter@2535
|
583 |
Node s, t;
|
kpeter@2535
|
584 |
while ( excess[excess_nodes[next_node]] > 0 ||
|
kpeter@2535
|
585 |
++next_node < excess_nodes.size() )
|
deba@2440
|
586 |
{
|
kpeter@2535
|
587 |
// Running Dijkstra
|
kpeter@2535
|
588 |
s = excess_nodes[next_node];
|
kpeter@2535
|
589 |
if ((t = dijkstra.run(s, 1)) == INVALID)
|
kpeter@2535
|
590 |
return false;
|
deba@2440
|
591 |
|
kpeter@2535
|
592 |
// Augmenting along a shortest path from s to t
|
kpeter@2535
|
593 |
Capacity d = excess[s] < -excess[t] ? excess[s] : -excess[t];
|
kpeter@2535
|
594 |
Node u = t;
|
kpeter@2535
|
595 |
Edge e;
|
kpeter@2535
|
596 |
while ((e = pred[u]) != INVALID) {
|
kpeter@2535
|
597 |
Capacity rc;
|
kpeter@2535
|
598 |
if (u == graph.target(e)) {
|
kpeter@2535
|
599 |
rc = res_cap[e];
|
kpeter@2535
|
600 |
u = graph.source(e);
|
kpeter@2535
|
601 |
} else {
|
kpeter@2535
|
602 |
rc = flow[e];
|
kpeter@2535
|
603 |
u = graph.target(e);
|
kpeter@2535
|
604 |
}
|
kpeter@2535
|
605 |
if (rc < d) d = rc;
|
kpeter@2535
|
606 |
}
|
kpeter@2535
|
607 |
u = t;
|
kpeter@2535
|
608 |
while ((e = pred[u]) != INVALID) {
|
kpeter@2535
|
609 |
if (u == graph.target(e)) {
|
kpeter@2535
|
610 |
flow[e] += d;
|
kpeter@2535
|
611 |
res_cap[e] -= d;
|
kpeter@2535
|
612 |
u = graph.source(e);
|
kpeter@2535
|
613 |
} else {
|
kpeter@2535
|
614 |
flow[e] -= d;
|
kpeter@2535
|
615 |
res_cap[e] += d;
|
kpeter@2535
|
616 |
u = graph.target(e);
|
kpeter@2535
|
617 |
}
|
kpeter@2535
|
618 |
}
|
kpeter@2535
|
619 |
excess[s] -= d;
|
kpeter@2535
|
620 |
excess[t] += d;
|
deba@2440
|
621 |
}
|
deba@2440
|
622 |
|
kpeter@2556
|
623 |
// Handling non-zero lower bounds
|
deba@2440
|
624 |
if (lower) {
|
kpeter@2535
|
625 |
for (EdgeIt e(graph); e != INVALID; ++e)
|
kpeter@2535
|
626 |
flow[e] += (*lower)[e];
|
deba@2440
|
627 |
}
|
deba@2440
|
628 |
return true;
|
deba@2440
|
629 |
}
|
deba@2440
|
630 |
|
deba@2440
|
631 |
}; //class CapacityScaling
|
deba@2440
|
632 |
|
deba@2440
|
633 |
///@}
|
deba@2440
|
634 |
|
deba@2440
|
635 |
} //namespace lemon
|
deba@2440
|
636 |
|
deba@2440
|
637 |
#endif //LEMON_CAPACITY_SCALING_H
|