alpar@726
|
1 |
// -*- C++ -*-
|
jacint@749
|
2 |
#ifndef HUGO_MAX_FLOW_H
|
jacint@749
|
3 |
#define HUGO_MAX_FLOW_H
|
alpar@726
|
4 |
|
alpar@726
|
5 |
#include <vector>
|
alpar@726
|
6 |
#include <queue>
|
alpar@726
|
7 |
|
alpar@726
|
8 |
#include <hugo/graph_wrapper.h>
|
alpar@726
|
9 |
#include <hugo/invalid.h>
|
alpar@726
|
10 |
#include <hugo/maps.h>
|
alpar@726
|
11 |
|
alpar@726
|
12 |
/// \file
|
alpar@758
|
13 |
/// \ingroup flowalgs
|
alpar@726
|
14 |
|
alpar@726
|
15 |
namespace hugo {
|
alpar@726
|
16 |
|
alpar@758
|
17 |
/// \addtogroup flowalgs
|
alpar@758
|
18 |
/// @{
|
alpar@758
|
19 |
|
alpar@726
|
20 |
///Maximum flow algorithms class.
|
alpar@726
|
21 |
|
alpar@726
|
22 |
///This class provides various algorithms for finding a flow of
|
alpar@726
|
23 |
///maximum value in a directed graph. The \e source node, the \e
|
alpar@726
|
24 |
///target node, the \e capacity of the edges and the \e starting \e
|
alpar@726
|
25 |
///flow value of the edges should be passed to the algorithm through the
|
alpar@726
|
26 |
///constructor. It is possible to change these quantities using the
|
alpar@757
|
27 |
///functions \ref setSource, \ref setTarget, \ref setCap and
|
alpar@757
|
28 |
///\ref setFlow. Before any subsequent runs of any algorithm of
|
alpar@757
|
29 |
///the class \ref setFlow should be called.
|
alpar@758
|
30 |
///
|
alpar@726
|
31 |
///After running an algorithm of the class, the actual flow value
|
alpar@726
|
32 |
///can be obtained by calling \ref flowValue(). The minimum
|
alpar@726
|
33 |
///value cut can be written into a \c node map of \c bools by
|
alpar@726
|
34 |
///calling \ref minCut. (\ref minMinCut and \ref maxMinCut writes
|
alpar@726
|
35 |
///the inclusionwise minimum and maximum of the minimum value
|
alpar@758
|
36 |
///cuts, resp.)
|
alpar@758
|
37 |
///
|
alpar@726
|
38 |
///\param Graph The directed graph type the algorithm runs on.
|
alpar@726
|
39 |
///\param Num The number type of the capacities and the flow values.
|
alpar@726
|
40 |
///\param CapMap The capacity map type.
|
alpar@758
|
41 |
///\param FlowMap The flow map type.
|
alpar@758
|
42 |
///
|
alpar@726
|
43 |
///\author Marton Makai, Jacint Szabo
|
alpar@726
|
44 |
template <typename Graph, typename Num,
|
alpar@726
|
45 |
typename CapMap=typename Graph::template EdgeMap<Num>,
|
alpar@726
|
46 |
typename FlowMap=typename Graph::template EdgeMap<Num> >
|
alpar@726
|
47 |
class MaxFlow {
|
alpar@726
|
48 |
protected:
|
alpar@726
|
49 |
typedef typename Graph::Node Node;
|
alpar@726
|
50 |
typedef typename Graph::NodeIt NodeIt;
|
alpar@726
|
51 |
typedef typename Graph::EdgeIt EdgeIt;
|
alpar@726
|
52 |
typedef typename Graph::OutEdgeIt OutEdgeIt;
|
alpar@726
|
53 |
typedef typename Graph::InEdgeIt InEdgeIt;
|
alpar@726
|
54 |
|
alpar@726
|
55 |
typedef typename std::vector<Node> VecFirst;
|
alpar@726
|
56 |
typedef typename Graph::template NodeMap<Node> NNMap;
|
alpar@726
|
57 |
typedef typename std::vector<Node> VecNode;
|
alpar@726
|
58 |
|
alpar@726
|
59 |
const Graph* g;
|
alpar@726
|
60 |
Node s;
|
alpar@726
|
61 |
Node t;
|
alpar@726
|
62 |
const CapMap* capacity;
|
alpar@726
|
63 |
FlowMap* flow;
|
alpar@726
|
64 |
int n; //the number of nodes of G
|
alpar@726
|
65 |
typedef ResGraphWrapper<const Graph, Num, CapMap, FlowMap> ResGW;
|
alpar@726
|
66 |
//typedef ExpResGraphWrapper<const Graph, Num, CapMap, FlowMap> ResGW;
|
alpar@726
|
67 |
typedef typename ResGW::OutEdgeIt ResGWOutEdgeIt;
|
alpar@726
|
68 |
typedef typename ResGW::Edge ResGWEdge;
|
alpar@726
|
69 |
typedef typename Graph::template NodeMap<int> ReachedMap;
|
alpar@726
|
70 |
|
alpar@726
|
71 |
|
alpar@726
|
72 |
//level works as a bool map in augmenting path algorithms and is
|
alpar@726
|
73 |
//used by bfs for storing reached information. In preflow, it
|
alpar@726
|
74 |
//shows the levels of nodes.
|
alpar@726
|
75 |
ReachedMap level;
|
alpar@726
|
76 |
|
alpar@726
|
77 |
//excess is needed only in preflow
|
alpar@726
|
78 |
typename Graph::template NodeMap<Num> excess;
|
alpar@726
|
79 |
|
alpar@726
|
80 |
// constants used for heuristics
|
alpar@726
|
81 |
static const int H0=20;
|
alpar@726
|
82 |
static const int H1=1;
|
alpar@726
|
83 |
|
alpar@726
|
84 |
public:
|
alpar@726
|
85 |
|
alpar@726
|
86 |
///Indicates the property of the starting flow.
|
alpar@726
|
87 |
|
alpar@726
|
88 |
///Indicates the property of the starting flow. The meanings are as follows:
|
alpar@726
|
89 |
///- \c ZERO_FLOW: constant zero flow
|
alpar@726
|
90 |
///- \c GEN_FLOW: any flow, i.e. the sum of the in-flows equals to
|
alpar@726
|
91 |
///the sum of the out-flows in every node except the \e source and
|
alpar@726
|
92 |
///the \e target.
|
alpar@726
|
93 |
///- \c PRE_FLOW: any preflow, i.e. the sum of the in-flows is at
|
alpar@726
|
94 |
///least the sum of the out-flows in every node except the \e source.
|
alpar@726
|
95 |
///- \c NO_FLOW: indicates an unspecified edge map. \ref flow will be
|
alpar@726
|
96 |
///set to the constant zero flow in the beginning of the algorithm in this case.
|
alpar@726
|
97 |
enum FlowEnum{
|
alpar@726
|
98 |
ZERO_FLOW,
|
alpar@726
|
99 |
GEN_FLOW,
|
alpar@726
|
100 |
PRE_FLOW,
|
alpar@726
|
101 |
NO_FLOW
|
alpar@726
|
102 |
};
|
alpar@726
|
103 |
|
alpar@726
|
104 |
enum StatusEnum {
|
alpar@726
|
105 |
AFTER_NOTHING,
|
alpar@726
|
106 |
AFTER_AUGMENTING,
|
alpar@726
|
107 |
AFTER_FAST_AUGMENTING,
|
alpar@726
|
108 |
AFTER_PRE_FLOW_PHASE_1,
|
alpar@726
|
109 |
AFTER_PRE_FLOW_PHASE_2
|
alpar@726
|
110 |
};
|
alpar@726
|
111 |
|
jacint@749
|
112 |
/// Do not needle this flag only if necessary.
|
alpar@726
|
113 |
StatusEnum status;
|
alpar@726
|
114 |
|
alpar@726
|
115 |
// int number_of_augmentations;
|
alpar@726
|
116 |
|
alpar@726
|
117 |
|
alpar@726
|
118 |
// template<typename IntMap>
|
alpar@726
|
119 |
// class TrickyReachedMap {
|
alpar@726
|
120 |
// protected:
|
alpar@726
|
121 |
// IntMap* map;
|
alpar@726
|
122 |
// int* number_of_augmentations;
|
alpar@726
|
123 |
// public:
|
alpar@726
|
124 |
// TrickyReachedMap(IntMap& _map, int& _number_of_augmentations) :
|
alpar@726
|
125 |
// map(&_map), number_of_augmentations(&_number_of_augmentations) { }
|
alpar@726
|
126 |
// void set(const Node& n, bool b) {
|
alpar@726
|
127 |
// if (b)
|
alpar@726
|
128 |
// map->set(n, *number_of_augmentations);
|
alpar@726
|
129 |
// else
|
alpar@726
|
130 |
// map->set(n, *number_of_augmentations-1);
|
alpar@726
|
131 |
// }
|
alpar@726
|
132 |
// bool operator[](const Node& n) const {
|
alpar@726
|
133 |
// return (*map)[n]==*number_of_augmentations;
|
alpar@726
|
134 |
// }
|
alpar@726
|
135 |
// };
|
alpar@726
|
136 |
|
alpar@726
|
137 |
///Constructor
|
alpar@726
|
138 |
|
alpar@726
|
139 |
///\todo Document, please.
|
alpar@726
|
140 |
///
|
alpar@726
|
141 |
MaxFlow(const Graph& _G, Node _s, Node _t,
|
marci@745
|
142 |
const CapMap& _capacity, FlowMap& _flow) :
|
alpar@726
|
143 |
g(&_G), s(_s), t(_t), capacity(&_capacity),
|
alpar@726
|
144 |
flow(&_flow), n(_G.nodeNum()), level(_G), excess(_G,0),
|
alpar@726
|
145 |
status(AFTER_NOTHING) { }
|
alpar@726
|
146 |
|
alpar@726
|
147 |
///Runs a maximum flow algorithm.
|
alpar@726
|
148 |
|
alpar@726
|
149 |
///Runs a preflow algorithm, which is the fastest maximum flow
|
alpar@726
|
150 |
///algorithm up-to-date. The default for \c fe is ZERO_FLOW.
|
alpar@726
|
151 |
///\pre The starting flow must be
|
alpar@726
|
152 |
/// - a constant zero flow if \c fe is \c ZERO_FLOW,
|
alpar@726
|
153 |
/// - an arbitary flow if \c fe is \c GEN_FLOW,
|
alpar@726
|
154 |
/// - an arbitary preflow if \c fe is \c PRE_FLOW,
|
alpar@726
|
155 |
/// - any map if \c fe is NO_FLOW.
|
alpar@726
|
156 |
void run(FlowEnum fe=ZERO_FLOW) {
|
alpar@726
|
157 |
preflow(fe);
|
alpar@726
|
158 |
}
|
alpar@726
|
159 |
|
alpar@726
|
160 |
|
alpar@726
|
161 |
///Runs a preflow algorithm.
|
alpar@726
|
162 |
|
alpar@726
|
163 |
///Runs a preflow algorithm. The preflow algorithms provide the
|
alpar@726
|
164 |
///fastest way to compute a maximum flow in a directed graph.
|
alpar@726
|
165 |
///\pre The starting flow must be
|
alpar@726
|
166 |
/// - a constant zero flow if \c fe is \c ZERO_FLOW,
|
alpar@726
|
167 |
/// - an arbitary flow if \c fe is \c GEN_FLOW,
|
alpar@726
|
168 |
/// - an arbitary preflow if \c fe is \c PRE_FLOW,
|
alpar@726
|
169 |
/// - any map if \c fe is NO_FLOW.
|
alpar@726
|
170 |
///
|
alpar@726
|
171 |
///\todo NO_FLOW should be the default flow.
|
alpar@726
|
172 |
void preflow(FlowEnum fe) {
|
alpar@726
|
173 |
preflowPhase1(fe);
|
alpar@726
|
174 |
preflowPhase2();
|
alpar@726
|
175 |
}
|
alpar@726
|
176 |
// Heuristics:
|
alpar@726
|
177 |
// 2 phase
|
alpar@726
|
178 |
// gap
|
alpar@726
|
179 |
// list 'level_list' on the nodes on level i implemented by hand
|
alpar@726
|
180 |
// stack 'active' on the active nodes on level i
|
alpar@726
|
181 |
// runs heuristic 'highest label' for H1*n relabels
|
alpar@726
|
182 |
// runs heuristic 'bound decrease' for H0*n relabels, starts with 'highest label'
|
alpar@726
|
183 |
// Parameters H0 and H1 are initialized to 20 and 1.
|
alpar@726
|
184 |
|
alpar@726
|
185 |
///Runs the first phase of the preflow algorithm.
|
alpar@726
|
186 |
|
alpar@726
|
187 |
///The preflow algorithm consists of two phases, this method runs the
|
alpar@726
|
188 |
///first phase. After the first phase the maximum flow value and a
|
alpar@726
|
189 |
///minimum value cut can already be computed, though a maximum flow
|
jacint@749
|
190 |
///is not yet obtained. So after calling this method \ref flowValue
|
alpar@726
|
191 |
///and \ref actMinCut gives proper results.
|
alpar@726
|
192 |
///\warning: \ref minCut, \ref minMinCut and \ref maxMinCut do not
|
alpar@726
|
193 |
///give minimum value cuts unless calling \ref preflowPhase2.
|
alpar@726
|
194 |
///\pre The starting flow must be
|
alpar@726
|
195 |
/// - a constant zero flow if \c fe is \c ZERO_FLOW,
|
alpar@726
|
196 |
/// - an arbitary flow if \c fe is \c GEN_FLOW,
|
alpar@726
|
197 |
/// - an arbitary preflow if \c fe is \c PRE_FLOW,
|
alpar@726
|
198 |
/// - any map if \c fe is NO_FLOW.
|
alpar@726
|
199 |
void preflowPhase1(FlowEnum fe)
|
alpar@726
|
200 |
{
|
alpar@726
|
201 |
|
alpar@726
|
202 |
int heur0=(int)(H0*n); //time while running 'bound decrease'
|
alpar@726
|
203 |
int heur1=(int)(H1*n); //time while running 'highest label'
|
alpar@726
|
204 |
int heur=heur1; //starting time interval (#of relabels)
|
alpar@726
|
205 |
int numrelabel=0;
|
alpar@726
|
206 |
|
alpar@726
|
207 |
bool what_heur=1;
|
alpar@726
|
208 |
//It is 0 in case 'bound decrease' and 1 in case 'highest label'
|
alpar@726
|
209 |
|
alpar@726
|
210 |
bool end=false;
|
alpar@726
|
211 |
//Needed for 'bound decrease', true means no active nodes are above bound
|
alpar@726
|
212 |
//b.
|
alpar@726
|
213 |
|
alpar@726
|
214 |
int k=n-2; //bound on the highest level under n containing a node
|
alpar@726
|
215 |
int b=k; //bound on the highest level under n of an active node
|
alpar@726
|
216 |
|
alpar@726
|
217 |
VecFirst first(n, INVALID);
|
alpar@726
|
218 |
NNMap next(*g, INVALID); //maybe INVALID is not needed
|
alpar@726
|
219 |
|
alpar@726
|
220 |
NNMap left(*g, INVALID);
|
alpar@726
|
221 |
NNMap right(*g, INVALID);
|
alpar@726
|
222 |
VecNode level_list(n,INVALID);
|
alpar@726
|
223 |
//List of the nodes in level i<n, set to n.
|
alpar@726
|
224 |
|
marci@745
|
225 |
preflowPreproc(fe, next, first, level_list, left, right);
|
alpar@726
|
226 |
//End of preprocessing
|
alpar@726
|
227 |
|
alpar@726
|
228 |
//Push/relabel on the highest level active nodes.
|
alpar@726
|
229 |
while ( true ) {
|
alpar@726
|
230 |
if ( b == 0 ) {
|
alpar@726
|
231 |
if ( !what_heur && !end && k > 0 ) {
|
alpar@726
|
232 |
b=k;
|
alpar@726
|
233 |
end=true;
|
alpar@726
|
234 |
} else break;
|
alpar@726
|
235 |
}
|
alpar@726
|
236 |
|
marci@745
|
237 |
if ( !g->valid(first[b]) ) --b;
|
alpar@726
|
238 |
else {
|
alpar@726
|
239 |
end=false;
|
alpar@726
|
240 |
Node w=first[b];
|
alpar@726
|
241 |
first[b]=next[w];
|
marci@745
|
242 |
int newlevel=push(w, next, first);
|
marci@745
|
243 |
if ( excess[w] > 0 ) relabel(w, newlevel, next, first, level_list,
|
alpar@726
|
244 |
left, right, b, k, what_heur);
|
alpar@726
|
245 |
|
alpar@726
|
246 |
++numrelabel;
|
alpar@726
|
247 |
if ( numrelabel >= heur ) {
|
alpar@726
|
248 |
numrelabel=0;
|
alpar@726
|
249 |
if ( what_heur ) {
|
alpar@726
|
250 |
what_heur=0;
|
alpar@726
|
251 |
heur=heur0;
|
alpar@726
|
252 |
end=false;
|
alpar@726
|
253 |
} else {
|
alpar@726
|
254 |
what_heur=1;
|
alpar@726
|
255 |
heur=heur1;
|
alpar@726
|
256 |
b=k;
|
alpar@726
|
257 |
}
|
alpar@726
|
258 |
}
|
alpar@726
|
259 |
}
|
alpar@726
|
260 |
}
|
alpar@726
|
261 |
|
alpar@726
|
262 |
status=AFTER_PRE_FLOW_PHASE_1;
|
alpar@726
|
263 |
}
|
alpar@726
|
264 |
|
alpar@726
|
265 |
|
alpar@726
|
266 |
///Runs the second phase of the preflow algorithm.
|
alpar@726
|
267 |
|
alpar@726
|
268 |
///The preflow algorithm consists of two phases, this method runs
|
alpar@726
|
269 |
///the second phase. After calling \ref preflowPhase1 and then
|
alpar@726
|
270 |
///\ref preflowPhase2 the methods \ref flowValue, \ref minCut,
|
alpar@726
|
271 |
///\ref minMinCut and \ref maxMinCut give proper results.
|
alpar@726
|
272 |
///\pre \ref preflowPhase1 must be called before.
|
alpar@726
|
273 |
void preflowPhase2()
|
alpar@726
|
274 |
{
|
alpar@726
|
275 |
|
alpar@726
|
276 |
int k=n-2; //bound on the highest level under n containing a node
|
alpar@726
|
277 |
int b=k; //bound on the highest level under n of an active node
|
alpar@726
|
278 |
|
alpar@726
|
279 |
|
alpar@726
|
280 |
VecFirst first(n, INVALID);
|
alpar@726
|
281 |
NNMap next(*g, INVALID); //maybe INVALID is not needed
|
alpar@726
|
282 |
level.set(s,0);
|
alpar@726
|
283 |
std::queue<Node> bfs_queue;
|
alpar@726
|
284 |
bfs_queue.push(s);
|
alpar@726
|
285 |
|
alpar@726
|
286 |
while (!bfs_queue.empty()) {
|
alpar@726
|
287 |
|
alpar@726
|
288 |
Node v=bfs_queue.front();
|
alpar@726
|
289 |
bfs_queue.pop();
|
alpar@726
|
290 |
int l=level[v]+1;
|
alpar@726
|
291 |
|
alpar@726
|
292 |
InEdgeIt e;
|
alpar@726
|
293 |
for(g->first(e,v); g->valid(e); g->next(e)) {
|
alpar@726
|
294 |
if ( (*capacity)[e] <= (*flow)[e] ) continue;
|
alpar@726
|
295 |
Node u=g->tail(e);
|
alpar@726
|
296 |
if ( level[u] >= n ) {
|
alpar@726
|
297 |
bfs_queue.push(u);
|
alpar@726
|
298 |
level.set(u, l);
|
alpar@726
|
299 |
if ( excess[u] > 0 ) {
|
alpar@726
|
300 |
next.set(u,first[l]);
|
alpar@726
|
301 |
first[l]=u;
|
alpar@726
|
302 |
}
|
alpar@726
|
303 |
}
|
alpar@726
|
304 |
}
|
alpar@726
|
305 |
|
alpar@726
|
306 |
OutEdgeIt f;
|
alpar@726
|
307 |
for(g->first(f,v); g->valid(f); g->next(f)) {
|
alpar@726
|
308 |
if ( 0 >= (*flow)[f] ) continue;
|
alpar@726
|
309 |
Node u=g->head(f);
|
alpar@726
|
310 |
if ( level[u] >= n ) {
|
alpar@726
|
311 |
bfs_queue.push(u);
|
alpar@726
|
312 |
level.set(u, l);
|
alpar@726
|
313 |
if ( excess[u] > 0 ) {
|
alpar@726
|
314 |
next.set(u,first[l]);
|
alpar@726
|
315 |
first[l]=u;
|
alpar@726
|
316 |
}
|
alpar@726
|
317 |
}
|
alpar@726
|
318 |
}
|
alpar@726
|
319 |
}
|
alpar@726
|
320 |
b=n-2;
|
alpar@726
|
321 |
|
alpar@726
|
322 |
while ( true ) {
|
alpar@726
|
323 |
|
alpar@726
|
324 |
if ( b == 0 ) break;
|
alpar@726
|
325 |
|
marci@745
|
326 |
if ( !g->valid(first[b]) ) --b;
|
alpar@726
|
327 |
else {
|
alpar@726
|
328 |
|
alpar@726
|
329 |
Node w=first[b];
|
alpar@726
|
330 |
first[b]=next[w];
|
alpar@726
|
331 |
int newlevel=push(w,next, first/*active*/);
|
alpar@726
|
332 |
|
alpar@726
|
333 |
//relabel
|
alpar@726
|
334 |
if ( excess[w] > 0 ) {
|
alpar@726
|
335 |
level.set(w,++newlevel);
|
alpar@726
|
336 |
next.set(w,first[newlevel]);
|
alpar@726
|
337 |
first[newlevel]=w;
|
alpar@726
|
338 |
b=newlevel;
|
alpar@726
|
339 |
}
|
jacint@749
|
340 |
}
|
alpar@726
|
341 |
} // while(true)
|
alpar@726
|
342 |
|
alpar@726
|
343 |
status=AFTER_PRE_FLOW_PHASE_2;
|
alpar@726
|
344 |
}
|
alpar@726
|
345 |
|
alpar@726
|
346 |
|
marci@761
|
347 |
/// Returns the value of the maximum flow.
|
alpar@726
|
348 |
|
marci@761
|
349 |
/// Returns the excess of the target node \ref t.
|
marci@761
|
350 |
/// After running \ref preflowPhase1, this is the value of
|
marci@761
|
351 |
/// the maximum flow.
|
alpar@726
|
352 |
/// It can be called already after running \ref preflowPhase1.
|
alpar@726
|
353 |
Num flowValue() const {
|
marci@761
|
354 |
// Num a=0;
|
marci@761
|
355 |
// for(InEdgeIt e(*g,t);g->valid(e);g->next(e)) a+=(*flow)[e];
|
marci@761
|
356 |
// for(OutEdgeIt e(*g,t);g->valid(e);g->next(e)) a-=(*flow)[e];
|
marci@761
|
357 |
// return a;
|
marci@761
|
358 |
return excess[t];
|
alpar@726
|
359 |
//marci figyu: excess[t] epp ezt adja preflow 1. fazisa utan
|
alpar@726
|
360 |
}
|
jacint@749
|
361 |
|
alpar@726
|
362 |
|
alpar@726
|
363 |
///Returns a minimum value cut after calling \ref preflowPhase1.
|
alpar@726
|
364 |
|
alpar@726
|
365 |
///After the first phase of the preflow algorithm the maximum flow
|
alpar@726
|
366 |
///value and a minimum value cut can already be computed. This
|
alpar@726
|
367 |
///method can be called after running \ref preflowPhase1 for
|
alpar@726
|
368 |
///obtaining a minimum value cut.
|
alpar@726
|
369 |
/// \warning Gives proper result only right after calling \ref
|
alpar@726
|
370 |
/// preflowPhase1.
|
alpar@726
|
371 |
/// \todo We have to make some status variable which shows the
|
alpar@726
|
372 |
/// actual state
|
alpar@726
|
373 |
/// of the class. This enables us to determine which methods are valid
|
alpar@726
|
374 |
/// for MinCut computation
|
alpar@726
|
375 |
template<typename _CutMap>
|
alpar@726
|
376 |
void actMinCut(_CutMap& M) const {
|
alpar@726
|
377 |
NodeIt v;
|
alpar@726
|
378 |
switch (status) {
|
alpar@726
|
379 |
case AFTER_PRE_FLOW_PHASE_1:
|
alpar@726
|
380 |
for(g->first(v); g->valid(v); g->next(v)) {
|
alpar@726
|
381 |
if (level[v] < n) {
|
alpar@726
|
382 |
M.set(v, false);
|
alpar@726
|
383 |
} else {
|
alpar@726
|
384 |
M.set(v, true);
|
alpar@726
|
385 |
}
|
alpar@726
|
386 |
}
|
alpar@726
|
387 |
break;
|
alpar@726
|
388 |
case AFTER_PRE_FLOW_PHASE_2:
|
alpar@726
|
389 |
case AFTER_NOTHING:
|
marci@745
|
390 |
case AFTER_AUGMENTING:
|
marci@745
|
391 |
case AFTER_FAST_AUGMENTING:
|
alpar@726
|
392 |
minMinCut(M);
|
alpar@726
|
393 |
break;
|
alpar@726
|
394 |
}
|
alpar@726
|
395 |
}
|
alpar@726
|
396 |
|
alpar@726
|
397 |
///Returns the inclusionwise minimum of the minimum value cuts.
|
alpar@726
|
398 |
|
alpar@726
|
399 |
///Sets \c M to the characteristic vector of the minimum value cut
|
alpar@726
|
400 |
///which is inclusionwise minimum. It is computed by processing
|
alpar@726
|
401 |
///a bfs from the source node \c s in the residual graph.
|
alpar@726
|
402 |
///\pre M should be a node map of bools initialized to false.
|
alpar@726
|
403 |
///\pre \c flow must be a maximum flow.
|
alpar@726
|
404 |
template<typename _CutMap>
|
alpar@726
|
405 |
void minMinCut(_CutMap& M) const {
|
alpar@726
|
406 |
std::queue<Node> queue;
|
alpar@726
|
407 |
|
alpar@726
|
408 |
M.set(s,true);
|
alpar@726
|
409 |
queue.push(s);
|
alpar@726
|
410 |
|
alpar@726
|
411 |
while (!queue.empty()) {
|
alpar@726
|
412 |
Node w=queue.front();
|
alpar@726
|
413 |
queue.pop();
|
alpar@726
|
414 |
|
alpar@726
|
415 |
OutEdgeIt e;
|
alpar@726
|
416 |
for(g->first(e,w) ; g->valid(e); g->next(e)) {
|
alpar@726
|
417 |
Node v=g->head(e);
|
alpar@726
|
418 |
if (!M[v] && (*flow)[e] < (*capacity)[e] ) {
|
alpar@726
|
419 |
queue.push(v);
|
alpar@726
|
420 |
M.set(v, true);
|
alpar@726
|
421 |
}
|
alpar@726
|
422 |
}
|
alpar@726
|
423 |
|
alpar@726
|
424 |
InEdgeIt f;
|
alpar@726
|
425 |
for(g->first(f,w) ; g->valid(f); g->next(f)) {
|
alpar@726
|
426 |
Node v=g->tail(f);
|
alpar@726
|
427 |
if (!M[v] && (*flow)[f] > 0 ) {
|
alpar@726
|
428 |
queue.push(v);
|
alpar@726
|
429 |
M.set(v, true);
|
alpar@726
|
430 |
}
|
alpar@726
|
431 |
}
|
alpar@726
|
432 |
}
|
alpar@726
|
433 |
}
|
alpar@726
|
434 |
|
alpar@726
|
435 |
///Returns the inclusionwise maximum of the minimum value cuts.
|
alpar@726
|
436 |
|
alpar@726
|
437 |
///Sets \c M to the characteristic vector of the minimum value cut
|
alpar@726
|
438 |
///which is inclusionwise maximum. It is computed by processing a
|
alpar@726
|
439 |
///backward bfs from the target node \c t in the residual graph.
|
alpar@726
|
440 |
///\pre M should be a node map of bools initialized to false.
|
alpar@726
|
441 |
///\pre \c flow must be a maximum flow.
|
alpar@726
|
442 |
template<typename _CutMap>
|
alpar@726
|
443 |
void maxMinCut(_CutMap& M) const {
|
alpar@726
|
444 |
|
alpar@726
|
445 |
NodeIt v;
|
alpar@726
|
446 |
for(g->first(v) ; g->valid(v); g->next(v)) {
|
alpar@726
|
447 |
M.set(v, true);
|
alpar@726
|
448 |
}
|
alpar@726
|
449 |
|
alpar@726
|
450 |
std::queue<Node> queue;
|
alpar@726
|
451 |
|
alpar@726
|
452 |
M.set(t,false);
|
alpar@726
|
453 |
queue.push(t);
|
alpar@726
|
454 |
|
alpar@726
|
455 |
while (!queue.empty()) {
|
alpar@726
|
456 |
Node w=queue.front();
|
alpar@726
|
457 |
queue.pop();
|
alpar@726
|
458 |
|
alpar@726
|
459 |
InEdgeIt e;
|
alpar@726
|
460 |
for(g->first(e,w) ; g->valid(e); g->next(e)) {
|
alpar@726
|
461 |
Node v=g->tail(e);
|
alpar@726
|
462 |
if (M[v] && (*flow)[e] < (*capacity)[e] ) {
|
alpar@726
|
463 |
queue.push(v);
|
alpar@726
|
464 |
M.set(v, false);
|
alpar@726
|
465 |
}
|
alpar@726
|
466 |
}
|
alpar@726
|
467 |
|
alpar@726
|
468 |
OutEdgeIt f;
|
alpar@726
|
469 |
for(g->first(f,w) ; g->valid(f); g->next(f)) {
|
alpar@726
|
470 |
Node v=g->head(f);
|
alpar@726
|
471 |
if (M[v] && (*flow)[f] > 0 ) {
|
alpar@726
|
472 |
queue.push(v);
|
alpar@726
|
473 |
M.set(v, false);
|
alpar@726
|
474 |
}
|
alpar@726
|
475 |
}
|
alpar@726
|
476 |
}
|
alpar@726
|
477 |
}
|
alpar@726
|
478 |
|
alpar@726
|
479 |
///Returns a minimum value cut.
|
alpar@726
|
480 |
|
alpar@726
|
481 |
///Sets \c M to the characteristic vector of a minimum value cut.
|
alpar@726
|
482 |
///\pre M should be a node map of bools initialized to false.
|
alpar@726
|
483 |
///\pre \c flow must be a maximum flow.
|
alpar@726
|
484 |
template<typename CutMap>
|
alpar@726
|
485 |
void minCut(CutMap& M) const { minMinCut(M); }
|
alpar@726
|
486 |
|
alpar@757
|
487 |
///Sets the source node to \c _s.
|
alpar@726
|
488 |
|
alpar@757
|
489 |
///Sets the source node to \c _s.
|
alpar@726
|
490 |
///
|
alpar@757
|
491 |
void setSource(Node _s) { s=_s; status=AFTER_NOTHING; }
|
alpar@726
|
492 |
|
alpar@757
|
493 |
///Sets the target node to \c _t.
|
alpar@726
|
494 |
|
alpar@757
|
495 |
///Sets the target node to \c _t.
|
alpar@726
|
496 |
///
|
alpar@757
|
497 |
void setTarget(Node _t) { t=_t; status=AFTER_NOTHING; }
|
alpar@726
|
498 |
|
alpar@757
|
499 |
/// Sets the edge map of the capacities to _cap.
|
alpar@726
|
500 |
|
alpar@757
|
501 |
/// Sets the edge map of the capacities to _cap.
|
alpar@726
|
502 |
///
|
alpar@757
|
503 |
void setCap(const CapMap& _cap)
|
alpar@726
|
504 |
{ capacity=&_cap; status=AFTER_NOTHING; }
|
alpar@726
|
505 |
|
alpar@757
|
506 |
/// Sets the edge map of the flows to _flow.
|
alpar@726
|
507 |
|
alpar@757
|
508 |
/// Sets the edge map of the flows to _flow.
|
alpar@726
|
509 |
///
|
alpar@757
|
510 |
void setFlow(FlowMap& _flow) { flow=&_flow; status=AFTER_NOTHING; }
|
alpar@726
|
511 |
|
alpar@726
|
512 |
|
alpar@726
|
513 |
private:
|
alpar@726
|
514 |
|
alpar@726
|
515 |
int push(Node w, NNMap& next, VecFirst& first) {
|
alpar@726
|
516 |
|
alpar@726
|
517 |
int lev=level[w];
|
alpar@726
|
518 |
Num exc=excess[w];
|
alpar@726
|
519 |
int newlevel=n; //bound on the next level of w
|
alpar@726
|
520 |
|
alpar@726
|
521 |
OutEdgeIt e;
|
alpar@726
|
522 |
for(g->first(e,w); g->valid(e); g->next(e)) {
|
alpar@726
|
523 |
|
alpar@726
|
524 |
if ( (*flow)[e] >= (*capacity)[e] ) continue;
|
alpar@726
|
525 |
Node v=g->head(e);
|
alpar@726
|
526 |
|
alpar@726
|
527 |
if( lev > level[v] ) { //Push is allowed now
|
alpar@726
|
528 |
|
alpar@726
|
529 |
if ( excess[v]<=0 && v!=t && v!=s ) {
|
alpar@726
|
530 |
next.set(v,first[level[v]]);
|
alpar@726
|
531 |
first[level[v]]=v;
|
alpar@726
|
532 |
}
|
alpar@726
|
533 |
|
alpar@726
|
534 |
Num cap=(*capacity)[e];
|
alpar@726
|
535 |
Num flo=(*flow)[e];
|
alpar@726
|
536 |
Num remcap=cap-flo;
|
alpar@726
|
537 |
|
alpar@726
|
538 |
if ( remcap >= exc ) { //A nonsaturating push.
|
alpar@726
|
539 |
|
alpar@726
|
540 |
flow->set(e, flo+exc);
|
alpar@726
|
541 |
excess.set(v, excess[v]+exc);
|
alpar@726
|
542 |
exc=0;
|
alpar@726
|
543 |
break;
|
alpar@726
|
544 |
|
alpar@726
|
545 |
} else { //A saturating push.
|
alpar@726
|
546 |
flow->set(e, cap);
|
alpar@726
|
547 |
excess.set(v, excess[v]+remcap);
|
alpar@726
|
548 |
exc-=remcap;
|
alpar@726
|
549 |
}
|
alpar@726
|
550 |
} else if ( newlevel > level[v] ) newlevel = level[v];
|
alpar@726
|
551 |
} //for out edges wv
|
alpar@726
|
552 |
|
alpar@726
|
553 |
if ( exc > 0 ) {
|
alpar@726
|
554 |
InEdgeIt e;
|
alpar@726
|
555 |
for(g->first(e,w); g->valid(e); g->next(e)) {
|
alpar@726
|
556 |
|
alpar@726
|
557 |
if( (*flow)[e] <= 0 ) continue;
|
alpar@726
|
558 |
Node v=g->tail(e);
|
alpar@726
|
559 |
|
alpar@726
|
560 |
if( lev > level[v] ) { //Push is allowed now
|
alpar@726
|
561 |
|
alpar@726
|
562 |
if ( excess[v]<=0 && v!=t && v!=s ) {
|
alpar@726
|
563 |
next.set(v,first[level[v]]);
|
alpar@726
|
564 |
first[level[v]]=v;
|
alpar@726
|
565 |
}
|
alpar@726
|
566 |
|
alpar@726
|
567 |
Num flo=(*flow)[e];
|
alpar@726
|
568 |
|
alpar@726
|
569 |
if ( flo >= exc ) { //A nonsaturating push.
|
alpar@726
|
570 |
|
alpar@726
|
571 |
flow->set(e, flo-exc);
|
alpar@726
|
572 |
excess.set(v, excess[v]+exc);
|
alpar@726
|
573 |
exc=0;
|
alpar@726
|
574 |
break;
|
alpar@726
|
575 |
} else { //A saturating push.
|
alpar@726
|
576 |
|
alpar@726
|
577 |
excess.set(v, excess[v]+flo);
|
alpar@726
|
578 |
exc-=flo;
|
alpar@726
|
579 |
flow->set(e,0);
|
alpar@726
|
580 |
}
|
alpar@726
|
581 |
} else if ( newlevel > level[v] ) newlevel = level[v];
|
alpar@726
|
582 |
} //for in edges vw
|
alpar@726
|
583 |
|
alpar@726
|
584 |
} // if w still has excess after the out edge for cycle
|
alpar@726
|
585 |
|
alpar@726
|
586 |
excess.set(w, exc);
|
alpar@726
|
587 |
|
alpar@726
|
588 |
return newlevel;
|
alpar@726
|
589 |
}
|
alpar@726
|
590 |
|
alpar@726
|
591 |
|
jacint@749
|
592 |
|
alpar@726
|
593 |
void preflowPreproc(FlowEnum fe, NNMap& next, VecFirst& first,
|
alpar@726
|
594 |
VecNode& level_list, NNMap& left, NNMap& right)
|
alpar@726
|
595 |
{
|
jacint@749
|
596 |
switch (fe) { //setting excess
|
jacint@749
|
597 |
case NO_FLOW:
|
jacint@749
|
598 |
{
|
jacint@749
|
599 |
EdgeIt e;
|
jacint@749
|
600 |
for(g->first(e); g->valid(e); g->next(e)) flow->set(e,0);
|
jacint@749
|
601 |
|
jacint@749
|
602 |
NodeIt v;
|
jacint@749
|
603 |
for(g->first(v); g->valid(v); g->next(v)) excess.set(v,0);
|
jacint@749
|
604 |
break;
|
jacint@749
|
605 |
}
|
jacint@749
|
606 |
case ZERO_FLOW:
|
jacint@749
|
607 |
{
|
jacint@749
|
608 |
NodeIt v;
|
jacint@749
|
609 |
for(g->first(v); g->valid(v); g->next(v)) excess.set(v,0);
|
jacint@749
|
610 |
break;
|
jacint@749
|
611 |
}
|
jacint@749
|
612 |
case GEN_FLOW:
|
jacint@749
|
613 |
{
|
jacint@749
|
614 |
NodeIt v;
|
jacint@749
|
615 |
for(g->first(v); g->valid(v); g->next(v)) excess.set(v,0);
|
jacint@749
|
616 |
|
jacint@749
|
617 |
Num exc=0;
|
jacint@749
|
618 |
InEdgeIt e;
|
jacint@749
|
619 |
for(g->first(e,t); g->valid(e); g->next(e)) exc+=(*flow)[e];
|
jacint@749
|
620 |
OutEdgeIt f;
|
jacint@749
|
621 |
for(g->first(f,t); g->valid(f); g->next(f)) exc-=(*flow)[f];
|
jacint@749
|
622 |
excess.set(t,exc);
|
jacint@749
|
623 |
break;
|
jacint@749
|
624 |
}
|
jacint@749
|
625 |
default: break;
|
jacint@749
|
626 |
}
|
jacint@749
|
627 |
|
jacint@749
|
628 |
NodeIt v;
|
jacint@749
|
629 |
for(g->first(v); g->valid(v); g->next(v)) level.set(v,n);
|
jacint@749
|
630 |
//setting each node to level n
|
jacint@749
|
631 |
|
alpar@726
|
632 |
std::queue<Node> bfs_queue;
|
alpar@726
|
633 |
|
jacint@749
|
634 |
|
alpar@726
|
635 |
switch (fe) {
|
jacint@749
|
636 |
case NO_FLOW: //flow is already set to const zero
|
alpar@726
|
637 |
case ZERO_FLOW:
|
alpar@726
|
638 |
{
|
alpar@726
|
639 |
//Reverse_bfs from t, to find the starting level.
|
alpar@726
|
640 |
level.set(t,0);
|
alpar@726
|
641 |
bfs_queue.push(t);
|
alpar@726
|
642 |
|
alpar@726
|
643 |
while (!bfs_queue.empty()) {
|
alpar@726
|
644 |
|
alpar@726
|
645 |
Node v=bfs_queue.front();
|
alpar@726
|
646 |
bfs_queue.pop();
|
alpar@726
|
647 |
int l=level[v]+1;
|
alpar@726
|
648 |
|
alpar@726
|
649 |
InEdgeIt e;
|
alpar@726
|
650 |
for(g->first(e,v); g->valid(e); g->next(e)) {
|
alpar@726
|
651 |
Node w=g->tail(e);
|
alpar@726
|
652 |
if ( level[w] == n && w != s ) {
|
alpar@726
|
653 |
bfs_queue.push(w);
|
alpar@726
|
654 |
Node z=level_list[l];
|
alpar@726
|
655 |
if ( g->valid(z) ) left.set(z,w);
|
alpar@726
|
656 |
right.set(w,z);
|
alpar@726
|
657 |
level_list[l]=w;
|
alpar@726
|
658 |
level.set(w, l);
|
alpar@726
|
659 |
}
|
alpar@726
|
660 |
}
|
alpar@726
|
661 |
}
|
alpar@726
|
662 |
|
alpar@726
|
663 |
//the starting flow
|
alpar@726
|
664 |
OutEdgeIt e;
|
alpar@726
|
665 |
for(g->first(e,s); g->valid(e); g->next(e))
|
alpar@726
|
666 |
{
|
alpar@726
|
667 |
Num c=(*capacity)[e];
|
alpar@726
|
668 |
if ( c <= 0 ) continue;
|
alpar@726
|
669 |
Node w=g->head(e);
|
alpar@726
|
670 |
if ( level[w] < n ) {
|
jacint@749
|
671 |
if ( excess[w] <= 0 && w!=t ) //putting into the stack
|
jacint@749
|
672 |
{
|
alpar@726
|
673 |
next.set(w,first[level[w]]);
|
alpar@726
|
674 |
first[level[w]]=w;
|
alpar@726
|
675 |
}
|
alpar@726
|
676 |
flow->set(e, c);
|
alpar@726
|
677 |
excess.set(w, excess[w]+c);
|
alpar@726
|
678 |
}
|
alpar@726
|
679 |
}
|
alpar@726
|
680 |
break;
|
alpar@726
|
681 |
}
|
alpar@726
|
682 |
|
alpar@726
|
683 |
case GEN_FLOW:
|
jacint@749
|
684 |
{
|
jacint@749
|
685 |
//Reverse_bfs from t in the residual graph,
|
jacint@749
|
686 |
//to find the starting level.
|
jacint@749
|
687 |
level.set(t,0);
|
jacint@749
|
688 |
bfs_queue.push(t);
|
jacint@749
|
689 |
|
jacint@749
|
690 |
while (!bfs_queue.empty()) {
|
jacint@749
|
691 |
|
jacint@749
|
692 |
Node v=bfs_queue.front();
|
jacint@749
|
693 |
bfs_queue.pop();
|
jacint@749
|
694 |
int l=level[v]+1;
|
jacint@749
|
695 |
|
jacint@749
|
696 |
InEdgeIt e;
|
jacint@749
|
697 |
for(g->first(e,v); g->valid(e); g->next(e)) {
|
jacint@749
|
698 |
if ( (*capacity)[e] <= (*flow)[e] ) continue;
|
jacint@749
|
699 |
Node w=g->tail(e);
|
jacint@749
|
700 |
if ( level[w] == n && w != s ) {
|
jacint@749
|
701 |
bfs_queue.push(w);
|
jacint@749
|
702 |
Node z=level_list[l];
|
jacint@749
|
703 |
if ( g->valid(z) ) left.set(z,w);
|
jacint@749
|
704 |
right.set(w,z);
|
jacint@749
|
705 |
level_list[l]=w;
|
jacint@749
|
706 |
level.set(w, l);
|
jacint@749
|
707 |
}
|
jacint@749
|
708 |
}
|
jacint@749
|
709 |
|
jacint@749
|
710 |
OutEdgeIt f;
|
jacint@749
|
711 |
for(g->first(f,v); g->valid(f); g->next(f)) {
|
jacint@749
|
712 |
if ( 0 >= (*flow)[f] ) continue;
|
jacint@749
|
713 |
Node w=g->head(f);
|
jacint@749
|
714 |
if ( level[w] == n && w != s ) {
|
jacint@749
|
715 |
bfs_queue.push(w);
|
jacint@749
|
716 |
Node z=level_list[l];
|
jacint@749
|
717 |
if ( g->valid(z) ) left.set(z,w);
|
jacint@749
|
718 |
right.set(w,z);
|
jacint@749
|
719 |
level_list[l]=w;
|
jacint@749
|
720 |
level.set(w, l);
|
jacint@749
|
721 |
}
|
jacint@749
|
722 |
}
|
jacint@749
|
723 |
}
|
jacint@749
|
724 |
|
jacint@749
|
725 |
//the starting flow
|
jacint@749
|
726 |
OutEdgeIt e;
|
jacint@749
|
727 |
for(g->first(e,s); g->valid(e); g->next(e))
|
jacint@749
|
728 |
{
|
jacint@749
|
729 |
Num rem=(*capacity)[e]-(*flow)[e];
|
jacint@749
|
730 |
if ( rem <= 0 ) continue;
|
jacint@749
|
731 |
Node w=g->head(e);
|
jacint@749
|
732 |
if ( level[w] < n ) {
|
jacint@749
|
733 |
if ( excess[w] <= 0 && w!=t ) //putting into the stack
|
jacint@749
|
734 |
{
|
jacint@749
|
735 |
next.set(w,first[level[w]]);
|
jacint@749
|
736 |
first[level[w]]=w;
|
jacint@749
|
737 |
}
|
jacint@749
|
738 |
flow->set(e, (*capacity)[e]);
|
jacint@749
|
739 |
excess.set(w, excess[w]+rem);
|
jacint@749
|
740 |
}
|
jacint@749
|
741 |
}
|
jacint@749
|
742 |
|
jacint@749
|
743 |
InEdgeIt f;
|
jacint@749
|
744 |
for(g->first(f,s); g->valid(f); g->next(f))
|
jacint@749
|
745 |
{
|
jacint@749
|
746 |
if ( (*flow)[f] <= 0 ) continue;
|
jacint@749
|
747 |
Node w=g->tail(f);
|
jacint@749
|
748 |
if ( level[w] < n ) {
|
jacint@749
|
749 |
if ( excess[w] <= 0 && w!=t )
|
jacint@749
|
750 |
{
|
jacint@749
|
751 |
next.set(w,first[level[w]]);
|
jacint@749
|
752 |
first[level[w]]=w;
|
jacint@749
|
753 |
}
|
jacint@749
|
754 |
excess.set(w, excess[w]+(*flow)[f]);
|
jacint@749
|
755 |
flow->set(f, 0);
|
jacint@749
|
756 |
}
|
jacint@749
|
757 |
}
|
jacint@749
|
758 |
break;
|
jacint@749
|
759 |
} //case GEN_FLOW
|
jacint@749
|
760 |
|
alpar@726
|
761 |
case PRE_FLOW:
|
alpar@726
|
762 |
{
|
alpar@726
|
763 |
//Reverse_bfs from t in the residual graph,
|
alpar@726
|
764 |
//to find the starting level.
|
alpar@726
|
765 |
level.set(t,0);
|
alpar@726
|
766 |
bfs_queue.push(t);
|
alpar@726
|
767 |
|
alpar@726
|
768 |
while (!bfs_queue.empty()) {
|
alpar@726
|
769 |
|
alpar@726
|
770 |
Node v=bfs_queue.front();
|
alpar@726
|
771 |
bfs_queue.pop();
|
alpar@726
|
772 |
int l=level[v]+1;
|
alpar@726
|
773 |
|
alpar@726
|
774 |
InEdgeIt e;
|
alpar@726
|
775 |
for(g->first(e,v); g->valid(e); g->next(e)) {
|
alpar@726
|
776 |
if ( (*capacity)[e] <= (*flow)[e] ) continue;
|
alpar@726
|
777 |
Node w=g->tail(e);
|
alpar@726
|
778 |
if ( level[w] == n && w != s ) {
|
alpar@726
|
779 |
bfs_queue.push(w);
|
alpar@726
|
780 |
Node z=level_list[l];
|
alpar@726
|
781 |
if ( g->valid(z) ) left.set(z,w);
|
alpar@726
|
782 |
right.set(w,z);
|
alpar@726
|
783 |
level_list[l]=w;
|
alpar@726
|
784 |
level.set(w, l);
|
alpar@726
|
785 |
}
|
alpar@726
|
786 |
}
|
alpar@726
|
787 |
|
alpar@726
|
788 |
OutEdgeIt f;
|
alpar@726
|
789 |
for(g->first(f,v); g->valid(f); g->next(f)) {
|
alpar@726
|
790 |
if ( 0 >= (*flow)[f] ) continue;
|
alpar@726
|
791 |
Node w=g->head(f);
|
alpar@726
|
792 |
if ( level[w] == n && w != s ) {
|
alpar@726
|
793 |
bfs_queue.push(w);
|
alpar@726
|
794 |
Node z=level_list[l];
|
alpar@726
|
795 |
if ( g->valid(z) ) left.set(z,w);
|
alpar@726
|
796 |
right.set(w,z);
|
alpar@726
|
797 |
level_list[l]=w;
|
alpar@726
|
798 |
level.set(w, l);
|
alpar@726
|
799 |
}
|
alpar@726
|
800 |
}
|
alpar@726
|
801 |
}
|
alpar@726
|
802 |
|
alpar@726
|
803 |
|
alpar@726
|
804 |
//the starting flow
|
alpar@726
|
805 |
OutEdgeIt e;
|
alpar@726
|
806 |
for(g->first(e,s); g->valid(e); g->next(e))
|
alpar@726
|
807 |
{
|
alpar@726
|
808 |
Num rem=(*capacity)[e]-(*flow)[e];
|
alpar@726
|
809 |
if ( rem <= 0 ) continue;
|
alpar@726
|
810 |
Node w=g->head(e);
|
alpar@726
|
811 |
if ( level[w] < n ) {
|
alpar@726
|
812 |
flow->set(e, (*capacity)[e]);
|
alpar@726
|
813 |
excess.set(w, excess[w]+rem);
|
alpar@726
|
814 |
}
|
alpar@726
|
815 |
}
|
alpar@726
|
816 |
|
alpar@726
|
817 |
InEdgeIt f;
|
alpar@726
|
818 |
for(g->first(f,s); g->valid(f); g->next(f))
|
alpar@726
|
819 |
{
|
alpar@726
|
820 |
if ( (*flow)[f] <= 0 ) continue;
|
alpar@726
|
821 |
Node w=g->tail(f);
|
alpar@726
|
822 |
if ( level[w] < n ) {
|
alpar@726
|
823 |
excess.set(w, excess[w]+(*flow)[f]);
|
alpar@726
|
824 |
flow->set(f, 0);
|
alpar@726
|
825 |
}
|
alpar@726
|
826 |
}
|
jacint@749
|
827 |
|
jacint@749
|
828 |
NodeIt w; //computing the excess
|
jacint@749
|
829 |
for(g->first(w); g->valid(w); g->next(w)) {
|
jacint@749
|
830 |
Num exc=0;
|
jacint@749
|
831 |
|
jacint@749
|
832 |
InEdgeIt e;
|
jacint@749
|
833 |
for(g->first(e,w); g->valid(e); g->next(e)) exc+=(*flow)[e];
|
jacint@749
|
834 |
OutEdgeIt f;
|
jacint@749
|
835 |
for(g->first(f,w); g->valid(f); g->next(f)) exc-=(*flow)[f];
|
jacint@749
|
836 |
|
jacint@749
|
837 |
excess.set(w,exc);
|
jacint@749
|
838 |
|
jacint@749
|
839 |
//putting the active nodes into the stack
|
jacint@749
|
840 |
int lev=level[w];
|
jacint@749
|
841 |
if ( exc > 0 && lev < n && w != t )
|
jacint@749
|
842 |
{
|
jacint@749
|
843 |
next.set(w,first[lev]);
|
jacint@749
|
844 |
first[lev]=w;
|
jacint@749
|
845 |
}
|
jacint@749
|
846 |
}
|
alpar@726
|
847 |
break;
|
alpar@726
|
848 |
} //case PRE_FLOW
|
alpar@726
|
849 |
}
|
alpar@726
|
850 |
} //preflowPreproc
|
alpar@726
|
851 |
|
alpar@726
|
852 |
|
alpar@726
|
853 |
void relabel(Node w, int newlevel, NNMap& next, VecFirst& first,
|
alpar@726
|
854 |
VecNode& level_list, NNMap& left,
|
alpar@726
|
855 |
NNMap& right, int& b, int& k, bool what_heur )
|
alpar@726
|
856 |
{
|
alpar@726
|
857 |
|
alpar@726
|
858 |
Num lev=level[w];
|
alpar@726
|
859 |
|
alpar@726
|
860 |
Node right_n=right[w];
|
alpar@726
|
861 |
Node left_n=left[w];
|
alpar@726
|
862 |
|
alpar@726
|
863 |
//unlacing starts
|
alpar@726
|
864 |
if ( g->valid(right_n) ) {
|
alpar@726
|
865 |
if ( g->valid(left_n) ) {
|
alpar@726
|
866 |
right.set(left_n, right_n);
|
alpar@726
|
867 |
left.set(right_n, left_n);
|
alpar@726
|
868 |
} else {
|
alpar@726
|
869 |
level_list[lev]=right_n;
|
alpar@726
|
870 |
left.set(right_n, INVALID);
|
alpar@726
|
871 |
}
|
alpar@726
|
872 |
} else {
|
alpar@726
|
873 |
if ( g->valid(left_n) ) {
|
alpar@726
|
874 |
right.set(left_n, INVALID);
|
alpar@726
|
875 |
} else {
|
alpar@726
|
876 |
level_list[lev]=INVALID;
|
alpar@726
|
877 |
}
|
alpar@726
|
878 |
}
|
alpar@726
|
879 |
//unlacing ends
|
alpar@726
|
880 |
|
alpar@726
|
881 |
if ( !g->valid(level_list[lev]) ) {
|
alpar@726
|
882 |
|
alpar@726
|
883 |
//gapping starts
|
alpar@726
|
884 |
for (int i=lev; i!=k ; ) {
|
alpar@726
|
885 |
Node v=level_list[++i];
|
alpar@726
|
886 |
while ( g->valid(v) ) {
|
alpar@726
|
887 |
level.set(v,n);
|
alpar@726
|
888 |
v=right[v];
|
alpar@726
|
889 |
}
|
alpar@726
|
890 |
level_list[i]=INVALID;
|
alpar@726
|
891 |
if ( !what_heur ) first[i]=INVALID;
|
alpar@726
|
892 |
}
|
alpar@726
|
893 |
|
alpar@726
|
894 |
level.set(w,n);
|
alpar@726
|
895 |
b=lev-1;
|
alpar@726
|
896 |
k=b;
|
alpar@726
|
897 |
//gapping ends
|
alpar@726
|
898 |
|
alpar@726
|
899 |
} else {
|
alpar@726
|
900 |
|
alpar@726
|
901 |
if ( newlevel == n ) level.set(w,n);
|
alpar@726
|
902 |
else {
|
alpar@726
|
903 |
level.set(w,++newlevel);
|
alpar@726
|
904 |
next.set(w,first[newlevel]);
|
alpar@726
|
905 |
first[newlevel]=w;
|
alpar@726
|
906 |
if ( what_heur ) b=newlevel;
|
alpar@726
|
907 |
if ( k < newlevel ) ++k; //now k=newlevel
|
alpar@726
|
908 |
Node z=level_list[newlevel];
|
alpar@726
|
909 |
if ( g->valid(z) ) left.set(z,w);
|
alpar@726
|
910 |
right.set(w,z);
|
alpar@726
|
911 |
left.set(w,INVALID);
|
alpar@726
|
912 |
level_list[newlevel]=w;
|
alpar@726
|
913 |
}
|
alpar@726
|
914 |
}
|
alpar@726
|
915 |
} //relabel
|
jacint@749
|
916 |
|
jacint@749
|
917 |
void printexcess() {////
|
jacint@749
|
918 |
std::cout << "Excesses:" <<std::endl;
|
jacint@749
|
919 |
|
jacint@749
|
920 |
NodeIt v;
|
jacint@749
|
921 |
for(g->first(v); g->valid(v); g->next(v)) {
|
jacint@749
|
922 |
std::cout << 1+(g->id(v)) << ":" << excess[v]<<std::endl;
|
jacint@749
|
923 |
}
|
jacint@749
|
924 |
}
|
jacint@749
|
925 |
|
jacint@749
|
926 |
void printlevel() {////
|
jacint@749
|
927 |
std::cout << "Levels:" <<std::endl;
|
jacint@749
|
928 |
|
jacint@749
|
929 |
NodeIt v;
|
jacint@749
|
930 |
for(g->first(v); g->valid(v); g->next(v)) {
|
jacint@749
|
931 |
std::cout << 1+(g->id(v)) << ":" << level[v]<<std::endl;
|
jacint@749
|
932 |
}
|
jacint@749
|
933 |
}
|
jacint@749
|
934 |
|
jacint@749
|
935 |
void printactive() {////
|
jacint@749
|
936 |
std::cout << "Levels:" <<std::endl;
|
jacint@749
|
937 |
|
jacint@749
|
938 |
NodeIt v;
|
jacint@749
|
939 |
for(g->first(v); g->valid(v); g->next(v)) {
|
jacint@749
|
940 |
std::cout << 1+(g->id(v)) << ":" << level[v]<<std::endl;
|
jacint@749
|
941 |
}
|
jacint@749
|
942 |
}
|
jacint@749
|
943 |
|
jacint@749
|
944 |
|
alpar@726
|
945 |
}; //class MaxFlow
|
alpar@726
|
946 |
} //namespace hugo
|
alpar@726
|
947 |
|
alpar@726
|
948 |
#endif //HUGO_MAX_FLOW_H
|
alpar@726
|
949 |
|
alpar@726
|
950 |
|
alpar@726
|
951 |
|
alpar@726
|
952 |
|