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