ADS
Algorithms and Data Structures
Loading...
Searching...
No Matches
MaxFlow.h
Go to the documentation of this file.
1/*--------------------------------------------------------------------------*/
2/*----------------------------- File MaxFlow.h -----------------------------*/
3/*--------------------------------------------------------------------------*/
86/*--------------------------------------------------------------------------*/
87/*----------------------------- DEFINITIONS --------------------------------*/
88/*--------------------------------------------------------------------------*/
89
90#ifndef __ADS_MaxFlow
91 #define __ADS_MaxFlow
92/* self-identification: #endif constant */
93
94/*--------------------------------------------------------------------------*/
95/*------------------------------ INCLUDES ----------------------------------*/
96/*--------------------------------------------------------------------------*/
97
98#include "ADS/Contract.h"
99
100#include <algorithm>
101#include <cstddef>
102#include <limits>
103#include <vector>
104
105/*--------------------------------------------------------------------------*/
106/*--------------------------- NAMESPACE ------------------------------------*/
107/*--------------------------------------------------------------------------*/
108
109namespace ADS
110{
111
119
120/*--------------------------------------------------------------------------*/
121/*--------------------------- CLASS FlowNetwork ----------------------------*/
122/*--------------------------------------------------------------------------*/
123
126{
127
128/*--------------------------------------------------------------------------*/
129 public:
132 explicit FlowNetwork( int n );
133
135 [[nodiscard]] int n( void ) const { return( int( f_adj.size() ) ); }
136
139 int addEdge( int u , int v , long long c );
140
142 int addNode( void );
143
149 void setCapacity( int e , long long c );
150
153 [[nodiscard]] long long flow( int e ) const;
154
157 [[nodiscard]] long long capacity( int e ) const;
158
161 [[nodiscard]] int places( void ) const { return( int( f_to.size() ) ); }
162
165 [[nodiscard]] int tail( int e ) const {
166 ADS_PRE( ( e >= 0 ) && ( e < places() ) );
167 return( f_to[ std::size_t( e ^ 1 ) ] );
168 }
169
172 [[nodiscard]] int head( int e ) const {
173 ADS_PRE( ( e >= 0 ) && ( e < places() ) );
174 return( f_to[ std::size_t( e ) ] );
175 }
176
182 void assignFlow( const std::vector< long long > & F );
183
232 long long maxFlow( int s , int t , Augment how = Augment::DINIC ,
233 long long * paths = nullptr );
234
251 [[nodiscard]] std::vector< bool > minCut( int s ) const;
252
253/*--------------------------------------------------------------------------*/
254 private:
256 static constexpr long long INF = std::numeric_limits< long long >::max();
257
259 long long residual( int e ) const;
260
263 void push( int e , long long d );
264
266 [[nodiscard]] long long inflow( int t ) const;
267
301 void rebalance( int s , int t , bool excesses = true );
302
306 void pushRelabel( int s , int t );
307
312 [[nodiscard]] bool preflowFits( int s ) const;
313
315 long long bfsAugment( int s , int t );
316
318 long long dfsAugment( int s , int t );
319
322 bool levels( int s , int t );
323
353 long long blocking( int u , int t , long long d );
354
356 [[nodiscard]] bool skewSymmetric( void ) const;
357
360 struct Incidence
361 {
362 int e;
363 int to;
364 };
365
366 std::vector< std::vector< Incidence > > f_adj;
368 std::vector< int > f_to;
370 std::vector< long long > f_cap;
371 std::vector< long long > f_res;
374 std::vector< int > f_level;
375 std::vector< std::size_t > f_next;
376 std::vector< long long > f_imb;
378 bool f_unbalanced = false;
379 std::vector< int > f_imbNodes;
380
381 }; // end( class FlowNetwork )
382
385{
386 int u ,
388 long long low ,
390 };
391
423inline bool feasibleCirculation( int n , const std::vector< BoundedEdge > & E ,
424 std::vector< long long > * flows = nullptr );
425
426/*--------------------------------------------------------------------------*/
427
428} // end( namespace ADS )
429
430/*--------------------------------------------------------------------------*/
431/*-------------------------- IMPLEMENTATION --------------------------------*/
432/*--------------------------------------------------------------------------*/
433
434#include "ADS/graph/impl/MaxFlow.tpp"
435
436/*--------------------------------------------------------------------------*/
437/*--------------------- THE ENGINEERED PUSH-RELABEL ------------------------*/
438/*--------------------------------------------------------------------------*/
439
441
442/*--------------------------------------------------------------------------*/
443
444#endif /* MaxFlow.h included */
445
446/*--------------------------------------------------------------------------*/
447/*--------------------------- End File MaxFlow.h ---------------------------*/
448/*--------------------------------------------------------------------------*/
The contracts of ADS: the preconditions, the postconditions and the invariants of the operations,...
The push-relabel of Goldberg and Tarjan, the way PUSH_RELABEL of FlowNetwork::maxFlow() [see MaxFlow....
void assignFlow(const std::vector< long long > &F)
the flow of each edge set to , e.g., one that another solver computed, and the excesses and the defi...
std::vector< bool > minCut(int s) const
the side of a minimum cut, the nodes reached from in the residual network, after maxFlow()
int n(void) const
the number of nodes
Definition MaxFlow.h:135
FlowNetwork(int n)
the network of the nodes , with no edge
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
long long capacity(int e) const
the capacity of the edge number
long long maxFlow(int s, int t, Augment how=Augment::DINIC, long long *paths=nullptr)
the maximum flow from to , starting from the flow there is, after the excesses and the deficits left...
int addEdge(int u, int v, long long c)
the edge of capacity , and its number
long long flow(int e) const
the flow on the edge number
void setCapacity(int e, long long c)
the capacity of the edge number set to , its flow kept if at most and cut to otherwise,...
int places(void) const
the number of the places of the edges: the edge , an even number, and its residual twin
Definition MaxFlow.h:161
int addNode(void)
a new node, with no edge, and its number
#define ADS_PRE(cond)
[check]
Definition Contract.h:121
Augment
the way of increasing the flow
Definition MaxFlow.h:113
bool feasibleCirculation(int n, const std::vector< BoundedEdge > &E, std::vector< long long > *flows=nullptr)
whether the edges admit a circulation, a flow conserved at every node with ; then the flows in *flows...
@ DINIC
a blocking flow on the levels of a visit in breadth
Definition MaxFlow.h:116
@ EDMONDS_KARP
a shortest augmenting path, by a visit in breadth
Definition MaxFlow.h:115
@ FORD_FULKERSON
any augmenting path, by a visit in depth
Definition MaxFlow.h:114
@ PUSH_RELABEL
the pushes and the relabelings of a preflow
Definition MaxFlow.h:117
Definition ADT.h:96
an edge with lower and upper bounds on its flow
Definition MaxFlow.h:385
int v
the head
Definition MaxFlow.h:387
long long high
the largest flow on the edge
Definition MaxFlow.h:389
int u
the tail
Definition MaxFlow.h:386
long long low
the least flow on the edge
Definition MaxFlow.h:388