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