ADS
Algorithms and Data Structures
Loading...
Searching...
No Matches
PushRelabel.h
Go to the documentation of this file.
1/*--------------------------------------------------------------------------*/
2/*--------------------------- File PushRelabel.h ---------------------------*/
3/*--------------------------------------------------------------------------*/
39/*--------------------------------------------------------------------------*/
40/*----------------------------- DEFINITIONS --------------------------------*/
41/*--------------------------------------------------------------------------*/
42
43#ifndef __ADS_PushRelabel
44 #define __ADS_PushRelabel
45/* self-identification: #endif constant */
46
47/*--------------------------------------------------------------------------*/
48/*------------------------------ INCLUDES ----------------------------------*/
49/*--------------------------------------------------------------------------*/
50
51#include "ADS/graph/MaxFlow.h"
52
53#include <cstddef>
54#include <vector>
55
56/*--------------------------------------------------------------------------*/
57/*--------------------------- NAMESPACE ------------------------------------*/
58/*--------------------------------------------------------------------------*/
59
60namespace ADS
61{
62
63/*--------------------------------------------------------------------------*/
64/*---------------------------- THE PUSH-RELABEL ----------------------------*/
65/*--------------------------------------------------------------------------*/
66
126inline void FlowNetwork::pushRelabel( int s , int t )
127{
128 // on the edges in place, each with its twin e ^ 1, no copy of them
129 const int N = n();
130 long long * const R = f_res.data();
131 std::vector< long long > ex( N , 0 );
132 // the excesses left by setCapacity(), where they are; with none and a
133 // flow already, the flow is maximum unless a residual path leads from s
134 // to t, which a visit of the side of s finds without touching the rest
135 bool any = false;
136 if( f_unbalanced ) {
137 for( const int x : f_imbNodes ) {
138 if( ( x != s ) && ( x != t ) && ( f_imb[ x ] > 0 ) ) {
139 ex[ x ] = f_imb[ x ];
140 any = true;
141 }
142 f_imb[ x ] = 0;
143 }
144 f_imbNodes.clear();
145 f_unbalanced = false;
146 }
147 if( ! any && ( inflow( t ) != 0 ) ) { // a flow already: warm
148 std::vector< char > seen( N , 0 );
149 std::vector< int > S = { s };
150 seen[ s ] = 1;
151 bool reach = false;
152 while( ! S.empty() && ! reach ) {
153 const int u = S.back();
154 S.pop_back();
155 for( const auto & [ e , v ] : f_adj[ u ] )
156 if( ! seen[ v ] && ( R[ e ] > 0 ) ) {
157 seen[ v ] = 1;
158 reach = reach || ( v == t );
159 S.push_back( v );
160 }
161 }
162 if( ! reach )
163 return;
164 }
165 std::vector< int > d( N , N );
166 // the active nodes by label, as stacks, and all the nodes by label, as
167 // doubly linked lists for the gaps
168 std::vector< int > aFirst( 2 * N + 1 , -1 ) , aNext( N );
169 std::vector< char > queued( N , 0 );
170 std::vector< int > bFirst( 2 * N + 1 , -1 ) , bNext( N ) , bPrev( N );
171 int aMax = -1 , bMax = -1;
172 auto aPush = [ & ]( int v ) {
173 if( queued[ v ] )
174 return;
175 queued[ v ] = 1;
176 aNext[ v ] = aFirst[ d[ v ] ];
177 aFirst[ d[ v ] ] = v;
178 if( d[ v ] > aMax )
179 aMax = d[ v ];
180 };
181 auto bAdd = [ & ]( int v ) {
182 bPrev[ v ] = -1;
183 bNext[ v ] = bFirst[ d[ v ] ];
184 if( bNext[ v ] >= 0 )
185 bPrev[ bNext[ v ] ] = v;
186 bFirst[ d[ v ] ] = v;
187 if( d[ v ] > bMax )
188 bMax = d[ v ];
189 };
190 auto bDel = [ & ]( int v ) {
191 if( bPrev[ v ] >= 0 )
192 bNext[ bPrev[ v ] ] = bNext[ v ];
193 else
194 bFirst[ d[ v ] ] = bNext[ v ];
195 if( bNext[ v ] >= 0 )
196 bPrev[ bNext[ v ] ] = bPrev[ v ];
197 };
198
199 // the labels: the distances to the target in the residual network, by a
200 // visit in breadth backwards over the nodes still in the game (those
201 // below the top); the others stay at the top
202 int target = t , top = N;
203 std::vector< int > Q;
204 std::vector< char > inGame( N , 1 );
205 auto relabelAll = [ & ]( bool first ) {
206 int * const D = d.data();
207 const char * const G = inGame.data();
208 int want = 0; // the active ones to label
209 for( int v = 0 ; v < N ; ++v )
210 if( G[ v ] ) {
211 D[ v ] = top;
212 queued[ v ] = 0;
213 want += ( ex[ v ] > 0 ) && ( v != s ) && ( v != t );
214 }
215 for( int k = 0 ; k <= aMax ; ++k )
216 aFirst[ k ] = -1;
217 for( int k = 0 ; k <= bMax ; ++k )
218 bFirst[ k ] = -1;
219 aMax = bMax = -1;
220 // s and t out of the visit, the target at 0 and the other at the top
221 D[ s ] = D[ t ] = -1;
222 D[ target ] = 0;
223 Q.resize( std::size_t( N ) );
224 int * const q = Q.data();
225 std::size_t head = 0 , tail = 0;
226 q[ tail++ ] = target;
227 const int tp = top;
228 const bool all = target == t; // the first phase: no node out
229 std::size_t level = tail; // the end of the level of now
230 int last = 0; // its label
231 while( head < tail ) {
232 if( head == level ) { // a level done: all the
233 if( want == 0 ) // active ones labeled, the
234 break; // others above (the lazy
235 level = tail; // update of HIPR)
236 ++last;
237 }
238 const int v = q[ head++ ];
239 const int dv = D[ v ] + 1;
240 const Incidence * a = f_adj[ v ].data();
241 const Incidence * const z = a + f_adj[ v ].size();
242 // no branch on the data in the first labeling: the node taken by a
243 // conditional write, as its tests are hard to predict
244 if( all && first )
245 for( ; a != z ; ++a ) {
246 const int u = a->to;
247 const bool ok = ( D[ u ] == tp ) & ( R[ a->e ^ 1 ] > 0 );
248 D[ u ] = ok ? dv : D[ u ];
249 q[ tail ] = u;
250 tail += ok;
251 want -= ok & ( ex[ u ] > 0 );
252 }
253 else
254 for( ; a != z ; ++a ) {
255 const int u = a->to;
256 if( D[ u ] != tp ) // labeled, or s or t
257 continue;
258 const int e = a->e;
259 if( G[ u ] && ( R[ e ^ 1 ] > 0 ) ) {
260 D[ u ] = dv;
261 q[ tail++ ] = u;
262 want -= ex[ u ] > 0;
263 }
264 }
265 }
266 D[ target == t ? s : t ] = tp;
267 if( head < tail ) { // stopped early: the nodes not
268 const int above = last + 2; // reached get a lower bound of
269 for( int v = 0 ; v < N ; ++v ) // their distance
270 if( G[ v ] && ( D[ v ] == tp ) && ( v != s ) && ( v != t ) &&
271 ( above < tp ) ) {
272 D[ v ] = above;
273 q[ tail++ ] = v;
274 }
275 }
276 for( std::size_t i = 1 ; i < tail ; ++i ) {
277 const int v = q[ i ];
278 bAdd( v );
279 if( ex[ v ] > 0 )
280 aPush( v );
281 }
282 };
283
284 const long long nm = 6LL * N + static_cast< long long >( f_to.size() ) / 2;
285 long long work = 0;
286 // the discharges of the highest active nodes: each scans its edges once,
287 // pushing to the lower neighbors and taking the least label of the
288 // others, which is its new label if an excess is left; the gaps and the
289 // global relabelings as in Cherkassky and Goldberg
290 auto run = [ & ]( bool global ) {
291 while( aMax >= 0 ) {
292 const int u = aFirst[ aMax ];
293 if( u < 0 ) {
294 --aMax;
295 continue;
296 }
297 aFirst[ aMax ] = aNext[ u ];
298 queued[ u ] = 0;
299 const int du = d[ u ];
300 if( ( du != aMax ) || ( ex[ u ] == 0 ) )
301 continue; // a stale entry
302 long long x = ex[ u ];
303 int nd = top;
304 const auto & A = f_adj[ u ];
305 for( const auto & [ e , v ] : A ) {
306 const int dv = d[ v ];
307 if( ( dv >= du ) && ( dv >= nd ) )
308 continue; // no use, read no edge
309 const long long r = R[ e ];
310 if( r <= 0 )
311 continue;
312 if( dv < du ) { // dv = du - 1, the labels valid
313 const long long y = r < x ? r : x;
314 R[ e ] -= y;
315 R[ e ^ 1 ] += y;
316 if( ( ex[ v ] == 0 ) && ( v != s ) && ( v != t ) )
317 aPush( v );
318 ex[ v ] += y;
319 x -= y;
320 if( x == 0 )
321 break;
322 }
323 else if( dv < nd )
324 nd = dv;
325 }
326 ex[ u ] = x;
327 if( x == 0 )
328 continue;
329 // an excess left: the relabeling of u, or a gap
330 work += 12 + static_cast< long long >( A.size() );
331 bDel( u );
332 if( bFirst[ du ] < 0 ) { // a gap at du: all above it
333 for( int k = du + 1 ; k <= bMax ; ++k ) {
334 for( int v = bFirst[ k ] ; v >= 0 ; v = bNext[ v ] )
335 d[ v ] = top;
336 bFirst[ k ] = -1;
337 }
338 bMax = du - 1;
339 d[ u ] = top;
340 continue;
341 }
342 d[ u ] = nd + 1 < top ? nd + 1 : top;
343 if( d[ u ] == top )
344 continue;
345 bAdd( u );
346 aPush( u );
347 if( global && ( 2 * work > nm ) ) { // a global relabeling
348 work = 0;
349 relabelAll( false );
350 }
351 }
352 };
353
354 // the first phase: the edges out of s saturated, the labels up to the
355 // last node with an excess, and a maximum preflow; the excesses that
356 // cannot reach t end at the top
357 for( const auto & [ e , v ] : f_adj[ s ] ) {
358 const long long r = R[ e ];
359 if( ( r > 0 ) && ( v != s ) ) {
360 R[ e ] = 0;
361 R[ e ^ 1 ] += r;
362 ex[ v ] += r;
363 }
364 }
365 relabelAll( true );
366 run( true );
367
368 // the second phase, only if an excess missed t: the excesses back to s,
369 // on the labels of the distances to s among the nodes out of the game of
370 // the first phase, those at the top
371 bool left = false;
372 for( int v = 0 ; ( v < N ) && ! left ; ++v )
373 left = ( ex[ v ] > 0 ) && ( v != s ) && ( v != t ) && ( d[ v ] == top );
374 if( ! left )
375 return;
376 for( int v = 0 ; v < N ; ++v )
377 inGame[ v ] = ( d[ v ] == top ) || ( v == s );
378 inGame[ t ] = 0;
379 target = s;
380 top = 2 * N;
381 relabelAll( false );
382 run( false );
383 }
384
385/*--------------------------------------------------------------------------*/
386
387} // end( namespace ADS )
388
389/*--------------------------------------------------------------------------*/
390
391#endif /* PushRelabel.h included */
392
393/*--------------------------------------------------------------------------*/
394/*------------------------- End File PushRelabel.h -------------------------*/
395/*--------------------------------------------------------------------------*/
The maximum flow of section 15.2 in a network , with integer capacities, and the minimum cut.
int n(void) const
the number of nodes
Definition MaxFlow.h:135
int tail(int e) const
the tail of the edge number
Definition MaxFlow.h:165
int head(int e) const
the head of the edge number
Definition MaxFlow.h:172
Definition ADT.h:96