marci@762
|
1 |
// -*- C++ -*-
|
marci@762
|
2 |
#ifndef HUGO_AUGMENTING_FLOW_H
|
marci@762
|
3 |
#define HUGO_AUGMENTING_FLOW_H
|
marci@762
|
4 |
|
marci@762
|
5 |
#include <vector>
|
marci@762
|
6 |
#include <iostream>
|
marci@762
|
7 |
|
marci@762
|
8 |
#include <hugo/graph_wrapper.h>
|
marci@762
|
9 |
#include <bfs_dfs.h>
|
marci@762
|
10 |
#include <hugo/invalid.h>
|
marci@762
|
11 |
#include <hugo/maps.h>
|
marci@863
|
12 |
#include <tight_edge_filter_map.h>
|
marci@762
|
13 |
|
marci@762
|
14 |
/// \file
|
marci@762
|
15 |
/// \brief Maximum flow algorithms.
|
marci@762
|
16 |
/// \ingroup galgs
|
marci@762
|
17 |
|
marci@762
|
18 |
namespace hugo {
|
marci@762
|
19 |
|
marci@762
|
20 |
/// \addtogroup galgs
|
marci@762
|
21 |
/// @{
|
marci@862
|
22 |
/// Class for augmenting path flow algorithms.
|
marci@762
|
23 |
|
marci@862
|
24 |
/// This class provides various algorithms for finding a flow of
|
marci@862
|
25 |
/// maximum value in a directed graph. The \e source node, the \e
|
marci@862
|
26 |
/// target node, the \e capacity of the edges and the \e starting \e
|
marci@862
|
27 |
/// flow value of the edges should be passed to the algorithm through the
|
marci@862
|
28 |
/// constructor.
|
marci@862
|
29 |
// /// It is possible to change these quantities using the
|
marci@862
|
30 |
// /// functions \ref resetSource, \ref resetTarget, \ref resetCap and
|
marci@862
|
31 |
// /// \ref resetFlow. Before any subsequent runs of any algorithm of
|
marci@862
|
32 |
// /// the class \ref resetFlow should be called.
|
marci@762
|
33 |
|
marci@862
|
34 |
/// After running an algorithm of the class, the actual flow value
|
marci@862
|
35 |
/// can be obtained by calling \ref flowValue(). The minimum
|
marci@862
|
36 |
/// value cut can be written into a \c node map of \c bools by
|
marci@862
|
37 |
/// calling \ref minCut. (\ref minMinCut and \ref maxMinCut writes
|
marci@862
|
38 |
/// the inclusionwise minimum and maximum of the minimum value
|
marci@862
|
39 |
/// cuts, resp.)
|
marci@762
|
40 |
///\param Graph The directed graph type the algorithm runs on.
|
marci@762
|
41 |
///\param Num The number type of the capacities and the flow values.
|
marci@762
|
42 |
///\param CapMap The capacity map type.
|
marci@762
|
43 |
///\param FlowMap The flow map type.
|
marci@862
|
44 |
///\author Marton Makai
|
marci@762
|
45 |
template <typename Graph, typename Num,
|
marci@762
|
46 |
typename CapMap=typename Graph::template EdgeMap<Num>,
|
marci@762
|
47 |
typename FlowMap=typename Graph::template EdgeMap<Num> >
|
marci@762
|
48 |
class AugmentingFlow {
|
marci@762
|
49 |
protected:
|
marci@762
|
50 |
typedef typename Graph::Node Node;
|
marci@762
|
51 |
typedef typename Graph::NodeIt NodeIt;
|
marci@762
|
52 |
typedef typename Graph::EdgeIt EdgeIt;
|
marci@762
|
53 |
typedef typename Graph::OutEdgeIt OutEdgeIt;
|
marci@762
|
54 |
typedef typename Graph::InEdgeIt InEdgeIt;
|
marci@762
|
55 |
|
marci@762
|
56 |
const Graph* g;
|
marci@762
|
57 |
Node s;
|
marci@762
|
58 |
Node t;
|
marci@762
|
59 |
const CapMap* capacity;
|
marci@762
|
60 |
FlowMap* flow;
|
marci@762
|
61 |
// int n; //the number of nodes of G
|
marci@762
|
62 |
typedef ResGraphWrapper<const Graph, Num, CapMap, FlowMap> ResGW;
|
marci@762
|
63 |
//typedef ExpResGraphWrapper<const Graph, Num, CapMap, FlowMap> ResGW;
|
marci@762
|
64 |
typedef typename ResGW::OutEdgeIt ResGWOutEdgeIt;
|
marci@762
|
65 |
typedef typename ResGW::Edge ResGWEdge;
|
marci@762
|
66 |
//typedef typename ResGW::template NodeMap<bool> ReachedMap;
|
marci@762
|
67 |
typedef typename Graph::template NodeMap<int> ReachedMap;
|
marci@762
|
68 |
|
marci@762
|
69 |
//level works as a bool map in augmenting path algorithms and is
|
marci@762
|
70 |
//used by bfs for storing reached information. In preflow, it
|
marci@762
|
71 |
//shows the levels of nodes.
|
marci@762
|
72 |
ReachedMap level;
|
marci@762
|
73 |
|
marci@762
|
74 |
public:
|
marci@762
|
75 |
///Indicates the property of the starting flow.
|
marci@762
|
76 |
|
marci@762
|
77 |
///Indicates the property of the starting flow. The meanings are as follows:
|
marci@762
|
78 |
///- \c ZERO_FLOW: constant zero flow
|
marci@762
|
79 |
///- \c GEN_FLOW: any flow, i.e. the sum of the in-flows equals to
|
marci@762
|
80 |
///the sum of the out-flows in every node except the \e source and
|
marci@762
|
81 |
///the \e target.
|
marci@762
|
82 |
///- \c PRE_FLOW: any preflow, i.e. the sum of the in-flows is at
|
marci@762
|
83 |
///least the sum of the out-flows in every node except the \e source.
|
marci@762
|
84 |
///- \c NO_FLOW: indicates an unspecified edge map. \ref flow will be
|
marci@762
|
85 |
///set to the constant zero flow in the beginning of the algorithm in this case.
|
marci@762
|
86 |
enum FlowEnum{
|
marci@762
|
87 |
ZERO_FLOW,
|
marci@762
|
88 |
GEN_FLOW,
|
marci@762
|
89 |
PRE_FLOW,
|
marci@762
|
90 |
NO_FLOW
|
marci@762
|
91 |
};
|
marci@762
|
92 |
|
marci@762
|
93 |
enum StatusEnum {
|
marci@762
|
94 |
AFTER_NOTHING,
|
marci@762
|
95 |
AFTER_AUGMENTING,
|
marci@762
|
96 |
AFTER_FAST_AUGMENTING,
|
marci@762
|
97 |
AFTER_PRE_FLOW_PHASE_1,
|
marci@762
|
98 |
AFTER_PRE_FLOW_PHASE_2
|
marci@762
|
99 |
};
|
marci@762
|
100 |
|
marci@762
|
101 |
/// Don not needle this flag only if necessary.
|
marci@762
|
102 |
StatusEnum status;
|
marci@762
|
103 |
int number_of_augmentations;
|
marci@762
|
104 |
|
marci@762
|
105 |
|
marci@762
|
106 |
template<typename IntMap>
|
marci@762
|
107 |
class TrickyReachedMap {
|
marci@762
|
108 |
protected:
|
marci@762
|
109 |
IntMap* map;
|
marci@762
|
110 |
int* number_of_augmentations;
|
marci@762
|
111 |
public:
|
marci@762
|
112 |
TrickyReachedMap(IntMap& _map, int& _number_of_augmentations) :
|
marci@762
|
113 |
map(&_map), number_of_augmentations(&_number_of_augmentations) { }
|
marci@762
|
114 |
void set(const Node& n, bool b) {
|
marci@762
|
115 |
if (b)
|
marci@762
|
116 |
map->set(n, *number_of_augmentations);
|
marci@762
|
117 |
else
|
marci@762
|
118 |
map->set(n, *number_of_augmentations-1);
|
marci@762
|
119 |
}
|
marci@762
|
120 |
bool operator[](const Node& n) const {
|
marci@762
|
121 |
return (*map)[n]==*number_of_augmentations;
|
marci@762
|
122 |
}
|
marci@762
|
123 |
};
|
marci@762
|
124 |
|
marci@762
|
125 |
AugmentingFlow(const Graph& _G, Node _s, Node _t, const CapMap& _capacity,
|
marci@762
|
126 |
FlowMap& _flow) :
|
marci@762
|
127 |
g(&_G), s(_s), t(_t), capacity(&_capacity),
|
marci@762
|
128 |
flow(&_flow), //n(_G.nodeNum()),
|
marci@762
|
129 |
level(_G), //excess(_G,0),
|
marci@762
|
130 |
status(AFTER_NOTHING), number_of_augmentations(0) { }
|
marci@762
|
131 |
|
marci@762
|
132 |
/// Starting from a flow, this method searches for an augmenting path
|
marci@762
|
133 |
/// according to the Edmonds-Karp algorithm
|
marci@762
|
134 |
/// and augments the flow on if any.
|
marci@762
|
135 |
/// The return value shows if the augmentation was succesful.
|
marci@762
|
136 |
bool augmentOnShortestPath();
|
marci@762
|
137 |
bool augmentOnShortestPath2();
|
marci@762
|
138 |
|
marci@762
|
139 |
/// Starting from a flow, this method searches for an augmenting blocking
|
marci@762
|
140 |
/// flow according to Dinits' algorithm and augments the flow on if any.
|
marci@762
|
141 |
/// The blocking flow is computed in a physically constructed
|
marci@762
|
142 |
/// residual graph of type \c Mutablegraph.
|
marci@762
|
143 |
/// The return value show sif the augmentation was succesful.
|
marci@762
|
144 |
template<typename MutableGraph> bool augmentOnBlockingFlow();
|
marci@762
|
145 |
|
marci@762
|
146 |
/// The same as \c augmentOnBlockingFlow<MutableGraph> but the
|
marci@762
|
147 |
/// residual graph is not constructed physically.
|
marci@762
|
148 |
/// The return value shows if the augmentation was succesful.
|
marci@762
|
149 |
bool augmentOnBlockingFlow2();
|
marci@762
|
150 |
|
marci@762
|
151 |
template<typename _CutMap>
|
marci@762
|
152 |
void actMinCut(_CutMap& M) const {
|
marci@762
|
153 |
NodeIt v;
|
marci@762
|
154 |
switch (status) {
|
marci@762
|
155 |
case AFTER_PRE_FLOW_PHASE_1:
|
marci@762
|
156 |
// std::cout << "AFTER_PRE_FLOW_PHASE_1" << std::endl;
|
marci@762
|
157 |
// for(g->first(v); g->valid(v); g->next(v)) {
|
marci@762
|
158 |
// if (level[v] < n) {
|
marci@762
|
159 |
// M.set(v, false);
|
marci@762
|
160 |
// } else {
|
marci@762
|
161 |
// M.set(v, true);
|
marci@762
|
162 |
// }
|
marci@762
|
163 |
// }
|
marci@762
|
164 |
break;
|
marci@762
|
165 |
case AFTER_PRE_FLOW_PHASE_2:
|
marci@762
|
166 |
// std::cout << "AFTER_PRE_FLOW_PHASE_2" << std::endl;
|
marci@762
|
167 |
break;
|
marci@762
|
168 |
case AFTER_NOTHING:
|
marci@762
|
169 |
// std::cout << "AFTER_NOTHING" << std::endl;
|
marci@762
|
170 |
minMinCut(M);
|
marci@762
|
171 |
break;
|
marci@762
|
172 |
case AFTER_AUGMENTING:
|
marci@762
|
173 |
// std::cout << "AFTER_AUGMENTING" << std::endl;
|
marci@775
|
174 |
for(g->first(v); v!=INVALID; ++v) {
|
marci@762
|
175 |
if (level[v]) {
|
marci@762
|
176 |
M.set(v, true);
|
marci@762
|
177 |
} else {
|
marci@762
|
178 |
M.set(v, false);
|
marci@762
|
179 |
}
|
marci@762
|
180 |
}
|
marci@762
|
181 |
break;
|
marci@762
|
182 |
case AFTER_FAST_AUGMENTING:
|
marci@762
|
183 |
// std::cout << "AFTER_FAST_AUGMENTING" << std::endl;
|
marci@775
|
184 |
for(g->first(v); v!=INVALID; ++v) {
|
marci@762
|
185 |
if (level[v]==number_of_augmentations) {
|
marci@762
|
186 |
M.set(v, true);
|
marci@762
|
187 |
} else {
|
marci@762
|
188 |
M.set(v, false);
|
marci@762
|
189 |
}
|
marci@762
|
190 |
}
|
marci@762
|
191 |
break;
|
marci@762
|
192 |
}
|
marci@762
|
193 |
}
|
marci@762
|
194 |
|
marci@762
|
195 |
template<typename _CutMap>
|
marci@762
|
196 |
void minMinCut(_CutMap& M) const {
|
marci@762
|
197 |
std::queue<Node> queue;
|
marci@762
|
198 |
|
marci@762
|
199 |
M.set(s,true);
|
marci@762
|
200 |
queue.push(s);
|
marci@762
|
201 |
|
marci@762
|
202 |
while (!queue.empty()) {
|
marci@762
|
203 |
Node w=queue.front();
|
marci@762
|
204 |
queue.pop();
|
marci@762
|
205 |
|
marci@762
|
206 |
OutEdgeIt e;
|
marci@775
|
207 |
for(g->first(e,w) ; e!=INVALID; ++e) {
|
marci@762
|
208 |
Node v=g->head(e);
|
marci@762
|
209 |
if (!M[v] && (*flow)[e] < (*capacity)[e] ) {
|
marci@762
|
210 |
queue.push(v);
|
marci@762
|
211 |
M.set(v, true);
|
marci@762
|
212 |
}
|
marci@762
|
213 |
}
|
marci@762
|
214 |
|
marci@762
|
215 |
InEdgeIt f;
|
marci@775
|
216 |
for(g->first(f,w) ; f!=INVALID; ++f) {
|
marci@762
|
217 |
Node v=g->tail(f);
|
marci@762
|
218 |
if (!M[v] && (*flow)[f] > 0 ) {
|
marci@762
|
219 |
queue.push(v);
|
marci@762
|
220 |
M.set(v, true);
|
marci@762
|
221 |
}
|
marci@762
|
222 |
}
|
marci@762
|
223 |
}
|
marci@762
|
224 |
}
|
marci@762
|
225 |
|
marci@762
|
226 |
template<typename _CutMap>
|
marci@762
|
227 |
void minMinCut2(_CutMap& M) const {
|
marci@762
|
228 |
ResGW res_graph(*g, *capacity, *flow);
|
marci@762
|
229 |
BfsIterator<ResGW, _CutMap> bfs(res_graph, M);
|
marci@762
|
230 |
bfs.pushAndSetReached(s);
|
marci@762
|
231 |
while (!bfs.finished()) ++bfs;
|
marci@762
|
232 |
}
|
marci@762
|
233 |
|
marci@762
|
234 |
Num flowValue() const {
|
marci@762
|
235 |
Num a=0;
|
marci@777
|
236 |
for (InEdgeIt e(*g, t); e!=INVALID; ++e) a+=(*flow)[e];
|
marci@777
|
237 |
for (OutEdgeIt e(*g, t); e!=INVALID; ++e) a-=(*flow)[e];
|
marci@762
|
238 |
return a;
|
marci@762
|
239 |
//marci figyu: excess[t] epp ezt adja preflow 1. fazisa utan
|
marci@762
|
240 |
}
|
marci@762
|
241 |
|
marci@762
|
242 |
};
|
marci@762
|
243 |
|
marci@762
|
244 |
|
marci@762
|
245 |
|
marci@762
|
246 |
template <typename Graph, typename Num, typename CapMap, typename FlowMap>
|
marci@762
|
247 |
bool AugmentingFlow<Graph, Num, CapMap, FlowMap>::augmentOnShortestPath()
|
marci@762
|
248 |
{
|
marci@762
|
249 |
ResGW res_graph(*g, *capacity, *flow);
|
marci@762
|
250 |
bool _augment=false;
|
marci@762
|
251 |
|
marci@762
|
252 |
//ReachedMap level(res_graph);
|
marci@777
|
253 |
for (typename Graph::NodeIt n(*g); n!=INVALID; ++n) level.set(n, 0);
|
marci@762
|
254 |
BfsIterator<ResGW, ReachedMap> bfs(res_graph, level);
|
marci@762
|
255 |
bfs.pushAndSetReached(s);
|
marci@762
|
256 |
|
marci@762
|
257 |
typename ResGW::template NodeMap<ResGWEdge> pred(res_graph);
|
marci@762
|
258 |
pred.set(s, INVALID);
|
marci@762
|
259 |
|
marci@762
|
260 |
typename ResGW::template NodeMap<Num> free(res_graph);
|
marci@762
|
261 |
|
marci@762
|
262 |
//searching for augmenting path
|
marci@762
|
263 |
while ( !bfs.finished() ) {
|
marci@777
|
264 |
ResGWEdge e=bfs;
|
marci@775
|
265 |
if (e!=INVALID && bfs.isBNodeNewlyReached()) {
|
marci@762
|
266 |
Node v=res_graph.tail(e);
|
marci@762
|
267 |
Node w=res_graph.head(e);
|
marci@762
|
268 |
pred.set(w, e);
|
marci@775
|
269 |
if (pred[v]!=INVALID) {
|
marci@762
|
270 |
free.set(w, std::min(free[v], res_graph.resCap(e)));
|
marci@762
|
271 |
} else {
|
marci@762
|
272 |
free.set(w, res_graph.resCap(e));
|
marci@762
|
273 |
}
|
marci@762
|
274 |
if (res_graph.head(e)==t) { _augment=true; break; }
|
marci@762
|
275 |
}
|
marci@762
|
276 |
|
marci@762
|
277 |
++bfs;
|
marci@762
|
278 |
} //end of searching augmenting path
|
marci@762
|
279 |
|
marci@762
|
280 |
if (_augment) {
|
marci@762
|
281 |
Node n=t;
|
marci@762
|
282 |
Num augment_value=free[t];
|
marci@775
|
283 |
while (pred[n]!=INVALID) {
|
marci@762
|
284 |
ResGWEdge e=pred[n];
|
marci@762
|
285 |
res_graph.augment(e, augment_value);
|
marci@762
|
286 |
n=res_graph.tail(e);
|
marci@762
|
287 |
}
|
marci@762
|
288 |
}
|
marci@762
|
289 |
|
marci@762
|
290 |
status=AFTER_AUGMENTING;
|
marci@762
|
291 |
return _augment;
|
marci@762
|
292 |
}
|
marci@762
|
293 |
|
marci@762
|
294 |
template <typename Graph, typename Num, typename CapMap, typename FlowMap>
|
marci@762
|
295 |
bool AugmentingFlow<Graph, Num, CapMap, FlowMap>::augmentOnShortestPath2()
|
marci@762
|
296 |
{
|
marci@762
|
297 |
ResGW res_graph(*g, *capacity, *flow);
|
marci@762
|
298 |
bool _augment=false;
|
marci@762
|
299 |
|
marci@762
|
300 |
if (status!=AFTER_FAST_AUGMENTING) {
|
marci@777
|
301 |
for (typename Graph::NodeIt n(*g); n!=INVALID; ++n) level.set(n, 0);
|
marci@762
|
302 |
number_of_augmentations=1;
|
marci@762
|
303 |
} else {
|
marci@762
|
304 |
++number_of_augmentations;
|
marci@762
|
305 |
}
|
marci@762
|
306 |
TrickyReachedMap<ReachedMap>
|
marci@762
|
307 |
tricky_reached_map(level, number_of_augmentations);
|
marci@762
|
308 |
//ReachedMap level(res_graph);
|
marci@762
|
309 |
// FOR_EACH_LOC(typename Graph::NodeIt, e, *g) level.set(e, 0);
|
marci@762
|
310 |
BfsIterator<ResGW, TrickyReachedMap<ReachedMap> >
|
marci@762
|
311 |
bfs(res_graph, tricky_reached_map);
|
marci@762
|
312 |
bfs.pushAndSetReached(s);
|
marci@762
|
313 |
|
marci@762
|
314 |
typename ResGW::template NodeMap<ResGWEdge> pred(res_graph);
|
marci@762
|
315 |
pred.set(s, INVALID);
|
marci@762
|
316 |
|
marci@762
|
317 |
typename ResGW::template NodeMap<Num> free(res_graph);
|
marci@762
|
318 |
|
marci@762
|
319 |
//searching for augmenting path
|
marci@762
|
320 |
while ( !bfs.finished() ) {
|
marci@777
|
321 |
ResGWEdge e=bfs;
|
marci@775
|
322 |
if (e!=INVALID && bfs.isBNodeNewlyReached()) {
|
marci@762
|
323 |
Node v=res_graph.tail(e);
|
marci@762
|
324 |
Node w=res_graph.head(e);
|
marci@762
|
325 |
pred.set(w, e);
|
marci@775
|
326 |
if (pred[v]!=INVALID) {
|
marci@762
|
327 |
free.set(w, std::min(free[v], res_graph.resCap(e)));
|
marci@762
|
328 |
} else {
|
marci@762
|
329 |
free.set(w, res_graph.resCap(e));
|
marci@762
|
330 |
}
|
marci@762
|
331 |
if (res_graph.head(e)==t) { _augment=true; break; }
|
marci@762
|
332 |
}
|
marci@762
|
333 |
|
marci@762
|
334 |
++bfs;
|
marci@762
|
335 |
} //end of searching augmenting path
|
marci@762
|
336 |
|
marci@762
|
337 |
if (_augment) {
|
marci@762
|
338 |
Node n=t;
|
marci@762
|
339 |
Num augment_value=free[t];
|
marci@775
|
340 |
while (pred[n]!=INVALID) {
|
marci@762
|
341 |
ResGWEdge e=pred[n];
|
marci@762
|
342 |
res_graph.augment(e, augment_value);
|
marci@762
|
343 |
n=res_graph.tail(e);
|
marci@762
|
344 |
}
|
marci@762
|
345 |
}
|
marci@762
|
346 |
|
marci@762
|
347 |
status=AFTER_FAST_AUGMENTING;
|
marci@762
|
348 |
return _augment;
|
marci@762
|
349 |
}
|
marci@762
|
350 |
|
marci@762
|
351 |
|
marci@762
|
352 |
template <typename Graph, typename Num, typename CapMap, typename FlowMap>
|
marci@762
|
353 |
template<typename MutableGraph>
|
marci@762
|
354 |
bool AugmentingFlow<Graph, Num, CapMap, FlowMap>::augmentOnBlockingFlow()
|
marci@762
|
355 |
{
|
marci@762
|
356 |
typedef MutableGraph MG;
|
marci@762
|
357 |
bool _augment=false;
|
marci@762
|
358 |
|
marci@762
|
359 |
ResGW res_graph(*g, *capacity, *flow);
|
marci@762
|
360 |
|
marci@762
|
361 |
//bfs for distances on the residual graph
|
marci@762
|
362 |
//ReachedMap level(res_graph);
|
marci@777
|
363 |
for (typename Graph::NodeIt n(*g); n!=INVALID; ++n) level.set(n, 0);
|
marci@762
|
364 |
BfsIterator<ResGW, ReachedMap> bfs(res_graph, level);
|
marci@762
|
365 |
bfs.pushAndSetReached(s);
|
marci@762
|
366 |
typename ResGW::template NodeMap<int>
|
marci@762
|
367 |
dist(res_graph); //filled up with 0's
|
marci@762
|
368 |
|
marci@762
|
369 |
//F will contain the physical copy of the residual graph
|
marci@762
|
370 |
//with the set of edges which are on shortest paths
|
marci@762
|
371 |
MG F;
|
marci@762
|
372 |
typename ResGW::template NodeMap<typename MG::Node>
|
marci@762
|
373 |
res_graph_to_F(res_graph);
|
marci@762
|
374 |
{
|
marci@762
|
375 |
typename ResGW::NodeIt n;
|
marci@862
|
376 |
for(res_graph.first(n); n!=INVALID; ++n)
|
marci@762
|
377 |
res_graph_to_F.set(n, F.addNode());
|
marci@762
|
378 |
}
|
marci@762
|
379 |
|
marci@762
|
380 |
typename MG::Node sF=res_graph_to_F[s];
|
marci@762
|
381 |
typename MG::Node tF=res_graph_to_F[t];
|
marci@762
|
382 |
typename MG::template EdgeMap<ResGWEdge> original_edge(F);
|
marci@762
|
383 |
typename MG::template EdgeMap<Num> residual_capacity(F);
|
marci@762
|
384 |
|
marci@762
|
385 |
while ( !bfs.finished() ) {
|
marci@777
|
386 |
ResGWEdge e=bfs;
|
marci@775
|
387 |
if (e!=INVALID) {
|
marci@762
|
388 |
if (bfs.isBNodeNewlyReached()) {
|
marci@762
|
389 |
dist.set(res_graph.head(e), dist[res_graph.tail(e)]+1);
|
marci@762
|
390 |
typename MG::Edge f=F.addEdge(res_graph_to_F[res_graph.tail(e)],
|
marci@762
|
391 |
res_graph_to_F[res_graph.head(e)]);
|
marci@854
|
392 |
//original_edge.update();
|
marci@762
|
393 |
original_edge.set(f, e);
|
marci@854
|
394 |
//residual_capacity.update();
|
marci@762
|
395 |
residual_capacity.set(f, res_graph.resCap(e));
|
marci@762
|
396 |
} else {
|
marci@762
|
397 |
if (dist[res_graph.head(e)]==(dist[res_graph.tail(e)]+1)) {
|
marci@762
|
398 |
typename MG::Edge f=F.addEdge(res_graph_to_F[res_graph.tail(e)],
|
marci@762
|
399 |
res_graph_to_F[res_graph.head(e)]);
|
marci@854
|
400 |
//original_edge.update();
|
marci@762
|
401 |
original_edge.set(f, e);
|
marci@854
|
402 |
//residual_capacity.update();
|
marci@762
|
403 |
residual_capacity.set(f, res_graph.resCap(e));
|
marci@762
|
404 |
}
|
marci@762
|
405 |
}
|
marci@762
|
406 |
}
|
marci@762
|
407 |
++bfs;
|
marci@762
|
408 |
} //computing distances from s in the residual graph
|
marci@762
|
409 |
|
marci@762
|
410 |
bool __augment=true;
|
marci@762
|
411 |
|
marci@762
|
412 |
while (__augment) {
|
marci@762
|
413 |
__augment=false;
|
marci@762
|
414 |
//computing blocking flow with dfs
|
marci@762
|
415 |
DfsIterator< MG, typename MG::template NodeMap<bool> > dfs(F);
|
marci@762
|
416 |
typename MG::template NodeMap<typename MG::Edge> pred(F);
|
marci@762
|
417 |
pred.set(sF, INVALID);
|
marci@762
|
418 |
//invalid iterators for sources
|
marci@762
|
419 |
|
marci@762
|
420 |
typename MG::template NodeMap<Num> free(F);
|
marci@762
|
421 |
|
marci@762
|
422 |
dfs.pushAndSetReached(sF);
|
marci@762
|
423 |
while (!dfs.finished()) {
|
marci@762
|
424 |
++dfs;
|
marci@854
|
425 |
if (typename MG::Edge(dfs)!=INVALID) {
|
marci@762
|
426 |
if (dfs.isBNodeNewlyReached()) {
|
marci@777
|
427 |
typename MG::Node v=F.tail(dfs);
|
marci@777
|
428 |
typename MG::Node w=F.head(dfs);
|
marci@762
|
429 |
pred.set(w, dfs);
|
marci@775
|
430 |
if (pred[v]!=INVALID) {
|
marci@762
|
431 |
free.set(w, std::min(free[v], residual_capacity[dfs]));
|
marci@762
|
432 |
} else {
|
marci@762
|
433 |
free.set(w, residual_capacity[dfs]);
|
marci@762
|
434 |
}
|
marci@762
|
435 |
if (w==tF) {
|
marci@762
|
436 |
__augment=true;
|
marci@762
|
437 |
_augment=true;
|
marci@762
|
438 |
break;
|
marci@762
|
439 |
}
|
marci@762
|
440 |
|
marci@762
|
441 |
} else {
|
marci@854
|
442 |
F.erase(typename MG::Edge(dfs));
|
marci@762
|
443 |
}
|
marci@762
|
444 |
}
|
marci@762
|
445 |
}
|
marci@762
|
446 |
|
marci@762
|
447 |
if (__augment) {
|
marci@762
|
448 |
typename MG::Node n=tF;
|
marci@762
|
449 |
Num augment_value=free[tF];
|
marci@775
|
450 |
while (pred[n]!=INVALID) {
|
marci@762
|
451 |
typename MG::Edge e=pred[n];
|
marci@762
|
452 |
res_graph.augment(original_edge[e], augment_value);
|
marci@762
|
453 |
n=F.tail(e);
|
marci@762
|
454 |
if (residual_capacity[e]==augment_value)
|
marci@762
|
455 |
F.erase(e);
|
marci@762
|
456 |
else
|
marci@762
|
457 |
residual_capacity.set(e, residual_capacity[e]-augment_value);
|
marci@762
|
458 |
}
|
marci@762
|
459 |
}
|
marci@762
|
460 |
|
marci@762
|
461 |
}
|
marci@762
|
462 |
|
marci@762
|
463 |
status=AFTER_AUGMENTING;
|
marci@762
|
464 |
return _augment;
|
marci@762
|
465 |
}
|
marci@762
|
466 |
|
marci@862
|
467 |
/// Blocking flow augmentation without constructing the layered
|
marci@862
|
468 |
/// graph physically in which the blocking flow is computed.
|
marci@762
|
469 |
template <typename Graph, typename Num, typename CapMap, typename FlowMap>
|
marci@762
|
470 |
bool AugmentingFlow<Graph, Num, CapMap, FlowMap>::augmentOnBlockingFlow2()
|
marci@762
|
471 |
{
|
marci@762
|
472 |
bool _augment=false;
|
marci@762
|
473 |
|
marci@762
|
474 |
ResGW res_graph(*g, *capacity, *flow);
|
marci@762
|
475 |
|
marci@862
|
476 |
//Potential map, for distances from s
|
marci@862
|
477 |
typename ResGW::template NodeMap<int> potential(res_graph, 0);
|
marci@862
|
478 |
typedef ConstMap<typename ResGW::Edge, int> Const1Map;
|
marci@862
|
479 |
Const1Map const_1_map(1);
|
marci@862
|
480 |
TightEdgeFilterMap<ResGW, typename ResGW::template NodeMap<int>,
|
marci@862
|
481 |
Const1Map> tight_edge_filter(res_graph, potential, const_1_map);
|
marci@862
|
482 |
|
marci@777
|
483 |
for (typename Graph::NodeIt n(*g); n!=INVALID; ++n) level.set(n, 0);
|
marci@762
|
484 |
BfsIterator<ResGW, ReachedMap> bfs(res_graph, level);
|
marci@862
|
485 |
bfs.pushAndSetReached(s);
|
marci@762
|
486 |
|
marci@862
|
487 |
//computing distances from s in the residual graph
|
marci@762
|
488 |
while ( !bfs.finished() ) {
|
marci@777
|
489 |
ResGWEdge e=bfs;
|
marci@862
|
490 |
if (e!=INVALID && bfs.isBNodeNewlyReached())
|
marci@862
|
491 |
potential.set(res_graph.head(e), potential[res_graph.tail(e)]+1);
|
marci@762
|
492 |
++bfs;
|
marci@862
|
493 |
}
|
marci@762
|
494 |
|
marci@862
|
495 |
//Subgraph containing the edges on some shortest paths
|
marci@862
|
496 |
//(i.e. tight edges)
|
marci@762
|
497 |
ConstMap<typename ResGW::Node, bool> true_map(true);
|
marci@762
|
498 |
typedef SubGraphWrapper<ResGW, ConstMap<typename ResGW::Node, bool>,
|
marci@862
|
499 |
TightEdgeFilterMap<ResGW, typename ResGW::template NodeMap<int>,
|
marci@862
|
500 |
Const1Map> > FilterResGW;
|
marci@862
|
501 |
FilterResGW filter_res_graph(res_graph, true_map, tight_edge_filter);
|
marci@762
|
502 |
|
marci@762
|
503 |
//Subgraph, which is able to delete edges which are already
|
marci@762
|
504 |
//met by the dfs
|
marci@777
|
505 |
typename FilterResGW::template NodeMap<typename FilterResGW::Edge>
|
marci@762
|
506 |
first_out_edges(filter_res_graph);
|
marci@862
|
507 |
for (typename FilterResGW::NodeIt v(filter_res_graph); v!=INVALID; ++v)
|
marci@862
|
508 |
first_out_edges.set
|
marci@862
|
509 |
(v, typename FilterResGW::OutEdgeIt(filter_res_graph, v));
|
marci@862
|
510 |
|
marci@762
|
511 |
typedef ErasingFirstGraphWrapper<FilterResGW, typename FilterResGW::
|
marci@777
|
512 |
template NodeMap<typename FilterResGW::Edge> > ErasingResGW;
|
marci@762
|
513 |
ErasingResGW erasing_res_graph(filter_res_graph, first_out_edges);
|
marci@762
|
514 |
|
marci@762
|
515 |
bool __augment=true;
|
marci@762
|
516 |
|
marci@762
|
517 |
while (__augment) {
|
marci@762
|
518 |
|
marci@762
|
519 |
__augment=false;
|
marci@762
|
520 |
//computing blocking flow with dfs
|
marci@762
|
521 |
DfsIterator< ErasingResGW,
|
marci@762
|
522 |
typename ErasingResGW::template NodeMap<bool> >
|
marci@762
|
523 |
dfs(erasing_res_graph);
|
marci@762
|
524 |
typename ErasingResGW::
|
marci@777
|
525 |
template NodeMap<typename ErasingResGW::Edge> pred(erasing_res_graph);
|
marci@762
|
526 |
pred.set(s, INVALID);
|
marci@762
|
527 |
//invalid iterators for sources
|
marci@762
|
528 |
|
marci@762
|
529 |
typename ErasingResGW::template NodeMap<Num>
|
marci@762
|
530 |
free1(erasing_res_graph);
|
marci@762
|
531 |
|
marci@762
|
532 |
dfs.pushAndSetReached
|
marci@777
|
533 |
/// \bug hugo 0.2
|
marci@762
|
534 |
(typename ErasingResGW::Node
|
marci@762
|
535 |
(typename FilterResGW::Node
|
marci@762
|
536 |
(typename ResGW::Node(s)
|
marci@762
|
537 |
)
|
marci@762
|
538 |
)
|
marci@762
|
539 |
);
|
marci@777
|
540 |
|
marci@762
|
541 |
while (!dfs.finished()) {
|
marci@762
|
542 |
++dfs;
|
marci@862
|
543 |
if (typename ErasingResGW::Edge(dfs)!=INVALID) {
|
marci@862
|
544 |
if (dfs.isBNodeNewlyReached()) {
|
marci@862
|
545 |
|
marci@862
|
546 |
typename ErasingResGW::Node v=erasing_res_graph.tail(dfs);
|
marci@862
|
547 |
typename ErasingResGW::Node w=erasing_res_graph.head(dfs);
|
marci@762
|
548 |
|
marci@862
|
549 |
pred.set(w, typename ErasingResGW::Edge(dfs));
|
marci@862
|
550 |
if (pred[v]!=INVALID) {
|
marci@862
|
551 |
free1.set
|
marci@862
|
552 |
(w, std::min(free1[v], res_graph.resCap
|
marci@862
|
553 |
(typename ErasingResGW::Edge(dfs))));
|
marci@862
|
554 |
} else {
|
marci@862
|
555 |
free1.set
|
marci@862
|
556 |
(w, res_graph.resCap
|
marci@862
|
557 |
(typename ErasingResGW::Edge(dfs)));
|
marci@862
|
558 |
}
|
marci@762
|
559 |
|
marci@862
|
560 |
if (w==t) {
|
marci@862
|
561 |
__augment=true;
|
marci@862
|
562 |
_augment=true;
|
marci@862
|
563 |
break;
|
marci@762
|
564 |
}
|
marci@862
|
565 |
} else {
|
marci@862
|
566 |
erasing_res_graph.erase(dfs);
|
marci@762
|
567 |
}
|
marci@862
|
568 |
}
|
marci@762
|
569 |
}
|
marci@762
|
570 |
|
marci@762
|
571 |
if (__augment) {
|
marci@762
|
572 |
typename ErasingResGW::Node
|
marci@762
|
573 |
n=typename FilterResGW::Node(typename ResGW::Node(t));
|
marci@762
|
574 |
Num augment_value=free1[n];
|
marci@777
|
575 |
while (pred[n]!=INVALID) {
|
marci@777
|
576 |
typename ErasingResGW::Edge e=pred[n];
|
marci@762
|
577 |
res_graph.augment(e, augment_value);
|
marci@762
|
578 |
n=erasing_res_graph.tail(e);
|
marci@762
|
579 |
if (res_graph.resCap(e)==0)
|
marci@762
|
580 |
erasing_res_graph.erase(e);
|
marci@762
|
581 |
}
|
marci@762
|
582 |
}
|
marci@762
|
583 |
|
marci@762
|
584 |
} //while (__augment)
|
marci@762
|
585 |
|
marci@762
|
586 |
status=AFTER_AUGMENTING;
|
marci@762
|
587 |
return _augment;
|
marci@762
|
588 |
}
|
marci@762
|
589 |
|
marci@762
|
590 |
|
marci@762
|
591 |
} //namespace hugo
|
marci@762
|
592 |
|
marci@762
|
593 |
#endif //HUGO_AUGMENTING_FLOW_H
|
marci@762
|
594 |
|
marci@762
|
595 |
|