Network Flow
This note is part of my learning notes on Data Structures & Algorithms.
Network Flow
Network flow algorithms are used to find the flow with maximum value (maximum flow) in a network, which can be modeled as a graph with nodes and edges representing flow capacities between nodes.
A flow network is a connected, directed graph .
- Each edge has a non-negative, integer capacity
- Two distinguished vertices
- A single source
- A single sink
- No edge enters the source and no edge leaves the sink
- No self-loop edges are allowed
- A flow in G is a function
Definition of a Flow
The value of a flow , is denoted by , is given by
1. Capacity constraint
flow capacity
2. Flow Conservation
number of flow into the node = number of flow out of the node (nodes that are not or )
3. Skew Symmetry
4. Simple Properties of Flow
- (because no self-loop nodes)
- , if
Theorem
The value of a flow pushed out from the source, eventually goes into the sink.
Cuts
A cut of a flow network is a partition of such that it creates two disjoint set of vertices: and . is the set that includes the source node, while is the set that includes the sink node. The only rule is that the source and the sink cannot be in the same set.
Capacity of a cut is .
If is a flow on , then the flow across the cut is .
The flow across the cut is as the sum of the flows corresponding to each pair of vertices, such that the source vertex is part of and the destination vertex is part of .
Lemma
Doesn’t matter what cut you choose, you have a flow on the network. The flow on the network is going to be equal to the flow on the cut.
For any flow and any cut , we have
Theorem (Upper Bound of Maximum Flow Value)
The value of any flow is bounded above by the capacity of a cut.
Max-Flow Min-Cut Theorem
In this theorem, the following are equivalent:
- for some cut
- is a maximum flow
- admits no augmenting paths
This theorem states that in any flow network, the maximum amount of flow passing from the source node to the sink node is equal to the minimum capacity of a cut (partitioning the nodes into two sets) in the network.
Residual Graph
Residual capacity of a directed edge is its capacity minus its flow. We can create a residual graph from all these types of edges, which has the same vertices and edges, but use the residual capacity as its new capacity.
Let be a flow on . The residual graph is the graph with strictly positive residual capacities
- Forward edges: For each edge of for which , include an edge in with capacity .
- Backward edges: For each edge in with , we include an edge in with capacity .
- If , add edge to with capacity . (remaining capacity left)
- If , add reverse edge with capacity . (can erase up to f(e) capacity)
Edges in admit more flow.
Augmenting Path
An augmenting path is a simple path from source to sink in the residual graph, which do not include any cycles and that pass only through positive weighted edges (residual capacity is positive). Any path from to in is an augmenting path in with respect to . The flow value can be increased along an augmenting path by
Maximum Bipartite Matching
A bipartite graph is a graph in which the vertex set can be divided into two disjoint subsets and such that every edge has one point in and the other end point in .
A maximum matching is a matching that contains the largest possible number of edges. Every maximum matching is a maximal matching.
Maximum bipartite matching deals with finding the maximum number of edges that can be added to a bipartite graph such that no two edges share a common endpoint.
Given a bipartite graph , find an that is matching and is as large as possible. is perfect matching if every vertex is matched.
Example: Ford-Fulkerson Algorithm
Ford-Fulkerson algorithm is used to solve the maximum flow problem. It iteratively finds augmenting paths from the source to the sink and increases the flow along these paths until no more augmenting paths exist. The maximum flow is reached when there are no more augmenting paths.
Algorithm:
- set the flow of each edge to zero
- look for an augmenting path from to
- while there is such an augmenting path that runs along edges with positive residual capacity
- do augment by
Pseudocode:
flow = 0 |
Running Time
The algorithm terminates in iterations of the while loop.
If has edges, has edges. We find a path from source to sink node in with time with depth-first search or breadth-first search.
Since (every node is adjacent to some edge),
Hence, the running time for Ford-Fulkerson algorithm to find max flow is
- where is the value of the maximum flow.
Example: Edmonds-Karp Algorithm
Edmonds-Karp algorithm is a variation of the Ford-Fulkerson algorithm. It is also used to solve the max-flow min-cut problem.
Ford-Fulkerson algorithm VS Edmonds-Karp algorithm
FF algorithm doesn’t specify how augmenting paths are found (but it mostly uses depth-first search); while EK algorithm clearly states that they are found using breadth-first search.
FF algorithm finds any augmenting path until there are none left, while EK finds ‘good’ augmenting paths and shortest edges to improve running time.
Algorithm
Pseudocode
def EdmondsKarp(E, C, s, t): |
Running Time
Each iterations of Edmonds-Karp runs in time, and that there are at most . So for iterations, the running time is
Space complexity is .
Blocking Flow
A blocking flow in a flow network is a flow that saturates at least one edge in every path in . It uses all the available capacity of every edge in the network. In other words, a blocking flow is a maximum flow that cannot be increased any further.
Every maximum flow is a blocking flow, but the reverse is not true!
Level Graph
A level graph is a special type of graph used to represent a network flow problem during the execution of Dinic’s algorithm. It is one where value of each node is its shortest distance from source, and is created by iteratively assigning levels to nodes in the network.
How to construct a level graph:
- Initialize all nodes to level
- If an augmenting path is found, the nodes in the path are assigned levels incrementally. The source node has level 1, and the sink node has the highest level.
- For every node with level k, all the edges leaving the node with level k are considered to have capacity remaining equal to the difference between the current level of the node and the level of the source node (k-1).
Example: Dinic’s Algorithm
Dinic’s Algorithm: is a faster way to calculate maximum flow over the networks. It includes construction of level graphs and residual graphs and finding of augmenting paths along with blocking flow.
Dinic’s Algorithm VS Edmonds Karp Algorithm
Dinic’s algorithm is based on depth-first search, and uses level graphs, while EK algorithm is based on breadth-first search and using augmenting paths.
check if more flow is possible and to construct level graph. In level graph, we assign levels to all nodes, level of a node is shortest distance (in terms of number of edges) of the node from source. Once level graph is constructed, we send multiple flows using this level graph.
Algorithm
- Create the level graph by performing breadth-first search from the source vertex
- Locate blocking flows
- Update the network flow
Pseudocode
function: DinicMaxFlow(Graph G,Node S,Node T): |
Running Time
Space complexity is , in which to store the level array and to store the graph’s adjacency list.
References
- MIT 6.046J Spring 2015: Network flow
- MIT 6.046J Spring 2012: Max-Flow Min-Cut
- Lecture 13. Incremental Improvement
- CMSC 451: Network Flows
- CMSC 451: Maximum Bipartite Matching
- HackerEarth: Maximum Flow Problem
- Baeldung: Dinic’s Algorithm
- CP Algorithms: Edmonds-Karp Algorithm
- Brilliant: Edmonds Karp Algorithm
- 知乎