ADS
Algorithms and Data Structures
Loading...
Searching...
No Matches
ADS::FlowNetwork Class Reference

a network of flow with integer capacities More...

#include <ADS/graph/MaxFlow.h>

Public member functions

 FlowNetwork (int n)
 the network of the nodes \( 0 , \ldots , n - 1 \), with no edge
int n (void) const
 the number of nodes
int addEdge (int u, int v, long long c)
 the edge \( ( u , v ) \) of capacity \( c \), and its number
int addNode (void)
 a new node, with no edge, and its number
void setCapacity (int e, long long c)
 the capacity of the edge number \( e \) set to \( c \), its flow kept if at most \( c \) and cut to \( c \) otherwise, the difference left as an excess at the tail and a deficit at the head, which the next maxFlow() routes before it increases the flow
long long flow (int e) const
 the flow on the edge number \( e \)
long long capacity (int e) const
 the capacity of the edge number \( e \)
int places (void) const
 the number of the places of the edges: the edge \( e \), an even number, and its residual twin \( e + 1 \)
int tail (int e) const
 the tail of the edge number \( e \)
int head (int e) const
 the head of the edge number \( e \)
void assignFlow (const std::vector< long long > &F)
 the flow of each edge \( e \) set to \( F[ e / 2 ] \), e.g., one that another solver computed, and the excesses and the deficits left by setCapacity() forgotten, in \( O( n + m ) \) time
long long maxFlow (int s, int t, Augment how=Augment::DINIC, long long *paths=nullptr)
 the maximum flow from \( s \) to \( t \), starting from the flow there is, after the excesses and the deficits left by setCapacity() are routed: the change of its value \( | f | \), the net flow into \( t \); the augmenting paths found go to *paths, if given (none for PUSH_RELABEL).
std::vector< bool > minCut (int s) const
 the side \( S \) of a minimum cut, the nodes reached from \( s \) in the residual network, after maxFlow()

Detailed description

a network of flow with integer capacities

Constructor and destructor documentation

◆ FlowNetwork()

ADS::FlowNetwork::FlowNetwork ( int n)
explicit

the network of the nodes \( 0 , \ldots , n - 1 \), with no edge

Precondition
\( n \ge 0 \)

Member function documentation

◆ addEdge()

int ADS::FlowNetwork::addEdge ( int u,
int v,
long long c )

the edge \( ( u , v ) \) of capacity \( c \), and its number

Precondition
\( u \) and \( v \) are nodes, \( c \ge 0 \)

◆ addNode()

int ADS::FlowNetwork::addNode ( void )

a new node, with no edge, and its number

◆ assignFlow()

void ADS::FlowNetwork::assignFlow ( const std::vector< long long > & F)

the flow of each edge \( e \) set to \( F[ e / 2 ] \), e.g., one that another solver computed, and the excesses and the deficits left by setCapacity() forgotten, in \( O( n + m ) \) time

Precondition
\( |F| \) is the number of edges, \( 0 \le F[ e / 2 ] \le c( e ) \)

◆ capacity()

long long ADS::FlowNetwork::capacity ( int e) const
nodiscard

the capacity of the edge number \( e \)

Precondition
\( e \) is the number of an edge

◆ flow()

long long ADS::FlowNetwork::flow ( int e) const
nodiscard

the flow on the edge number \( e \)

Precondition
\( e \) is the number of an edge

◆ head()

int ADS::FlowNetwork::head ( int e) const
inlinenodiscard

the head of the edge number \( e \)

Precondition
\( e \) is the number of an edge or of a twin

◆ maxFlow()

long long ADS::FlowNetwork::maxFlow ( int s,
int t,
Augment how = Augment::DINIC,
long long * paths = nullptr )

the maximum flow from \( s \) to \( t \), starting from the flow there is, after the excesses and the deficits left by setCapacity() are routed: the change of its value \( | f | \), the net flow into \( t \); the augmenting paths found go to *paths, if given (none for PUSH_RELABEL).

PUSH_RELABEL leaves the call to DINIC when the residual capacities out of \( s \) and the excesses left by setCapacity() sum to more than LLONG_MAX, since the excesses of its preflow, which sum to as much, would overflow a long long; the flow itself must fit one

In pseudocode (section 15.2), \( c_f( u , v ) = c( u , v ) - f( u , v ) \) the residual capacity, each edge with its twin of opposite flow:

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{maxFlow}( s , t , \mathit{how} )\quad \triangleright\ c_f \text{ the residual capacity} \cr 2 & \quad \mathrm{rebalance}( s , t ) \cr 3 & \quad \textbf{if}\ \mathit{how} = \mathrm{PUSH\_RELABEL} \textbf{ and } c_f \text{ out of } s \text{ and the excesses sum} \text{ to at most } 2^{63} - 1 \cr & \qquad \textbf{then}\ \mathrm{pushRelabel}( s , t ); \ \textbf{return}\ | f | \cr 4 & \quad \textbf{loop} \cr 5 & \qquad \textbf{if}\ \mathit{how} = \mathrm{DINIC}\ \textbf{then} \cr 6 & \qquad\quad \ell \gets \text{the levels of the nodes in a visit in breadth from } s \text{ along } c_f > 0 \cr 7 & \qquad\quad \textbf{if}\ \ell( t ) = \infty\ \textbf{then return}\ | f |\quad \triangleright\ \text{no augmenting path} \cr 8 & \qquad\quad \text{push a blocking flow on the edges } ( u , v ) \text{ with } \ell( v ) = \ell( u ) + 1;\ \textbf{continue} \cr 9 & \qquad P \gets \text{a path from } s \text{ to } t \text{ along } c_f > 0 \text{: by a visit in depth for } \mathrm{FORD\_FULKERSON} \text{,} \cr & \qquad\quad \text{a shortest one by a visit in breadth for } \mathrm{EDMONDS\_KARP} \cr 10 & \qquad \textbf{if}\ P = \textbf{nil}\ \textbf{then return}\ | f | \cr 11 & \qquad \delta \gets \text{the least } c_f( u , v ) \text{ of an edge of } P \cr 12 & \qquad \textbf{foreach}\ ( u , v ) \in P\ \textbf{do}\ f( u , v ) \gets f( u , v ) + \delta;\ f( v , u ) \gets f( v , u ) - \delta\quad \triangleright\ \text{skew symmetry} \end{array}} \]

Precondition
\( s \) and \( t \) are distinct nodes
Postcondition
no augmenting path from \( s \) to \( t \) (audit)

◆ minCut()

std::vector< bool > ADS::FlowNetwork::minCut ( int s) const
nodiscard

the side \( S \) of a minimum cut, the nodes reached from \( s \) in the residual network, after maxFlow()

In pseudocode (Theorem 15.1): with \( f \) maximum, the nodes reached from \( s \) by edges of positive residual capacity are one side of a cut of capacity \( | f | \).

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{minCut}( s )\quad \triangleright \ \text{after } \mathrm{maxFlow} \text{: no edge with } c_f > 0 \text{ leaves the set} \cr 2 & \quad \textbf{return}\ \text{the nodes that a visit from } s \text{ along the edges with } c_f( u , v ) > 0 \text{ reaches} \end{array}} \]

Precondition
\( s \) is a node

◆ n()

int ADS::FlowNetwork::n ( void ) const
inlinenodiscard

the number of nodes

◆ places()

int ADS::FlowNetwork::places ( void ) const
inlinenodiscard

the number of the places of the edges: the edge \( e \), an even number, and its residual twin \( e + 1 \)

◆ setCapacity()

void ADS::FlowNetwork::setCapacity ( int e,
long long c )

the capacity of the edge number \( e \) set to \( c \), its flow kept if at most \( c \) and cut to \( c \) otherwise, the difference left as an excess at the tail and a deficit at the head, which the next maxFlow() routes before it increases the flow

Precondition
\( e \) is the number of an edge, \( c \ge 0 \)

◆ tail()

int ADS::FlowNetwork::tail ( int e) const
inlinenodiscard

the tail of the edge number \( e \)

Precondition
\( e \) is the number of an edge or of a twin

The documentation for this class was generated from the following files: