Network Flow
Introduction
Many optimization problems reduce to routing as much "stuff" as possible through a network subject to capacity limits, such as traffic through roads, data through pipes, and goods through supply chains. Network flow formalizes this and gives efficient algorithms with broad applications (e.g. bipartite matching).
The central problem: given a directed graph with edge capacities, a source and sink node, find the maximum amount of flow that can be routed from source to sink without violating any capacity or conservation constraints.
Definitions
Flow Network. A flow network is a directed graph where each edge has capacity , a source node and sink node . We assume no edge enters and no edge leaves .
Flow. A flow is a function satisfiying the following two conditions:
- Capacity constraint. For every edge : .
- Conservation constraint. For every node :
That is, the flow in equals flow out at every internal node.
Value of a flow. The value of a flow is the total flow leaving the source:
Maximum flow problem. Given a flow network , find a flow maximizing .
Residual graph. Given a flow , the residual graph captures remaining capacity. For each edge .
- Add a forward edge with residual capacity if and .
- Add a backwards edge with residual capacity if if and .
These backward edges allow us to "cancel" or undo any previously routed flow.
Augmenting path. An augmenting path is any path from to in the residual graph . We say the bottleneck capacity of path is:
In other words, it is the maximum flow we can add/subtract onto any foward/backward edge along without violating any capacity constraints (without exceed or going below 0).
Ford-Fulkerson
Ford-Fulkerson repeatedly finds an augmenting path in the residual graph and pushes flow along it equal to its bottleneck capacity, until no augmenting path remains. After finding an augmenting path with flow and bottleneck , we use the following augment operation.
- For each forward edge : set .
- For each backward edge (reversing an original edge ): set .
Algorithm
Ford-Fulkerson(G, s, t, c):
f(e) = 0 for all edges
build residual graph G_f
while there is an s-t path P in G_f:
b = bottleneck(P)
f = augment(f, P, b)
update G_f
return flow f
Lemma: Augmenting preserves capacity and conservation constraints.
Let be a valid flow, be a simple path in and . Let be the flow after augmenting with path .
Recall that the augment performs the following operation to forward/backward edges:
- Forward edge with with ; we set .
- Backward edge with with ; we set .
For our bottleneck ,
- for every forward edge .
- for every backward edge .
We show the capacity constraint, i.e. for every .
Case 1: . This means that the edge was not augmented, so . Because is a valid flow, we have as required.
Case 2: , is used as a forward edge. Then, . We have:
where the upperbound uses .
Case 3: , is used as a backward edge. Then, .
The lower bound uses , and the upper bound uses .
In all cases , so satisfies the capacity constraint.
Next, we show that the conservation constraint is satisfied.
We fix an internal node . Since satisfies conservation at , it is enough to show the augmentation changes the total inflow and outflow at by the same amount. We denote these changes as and .
If , no incident edge to is modified, so and conservation is preserved.
Otherwise, is an interval vertex of our path , so there is exactly one incoming edge and one outgoing edge in . Each contributes to the flow on its underlying original edge.
Consider the case where is a forward edge and is a backward edge.
- Since is a forward edge, we contribute to .
- Since is a backward edge (original edge is ), we contribute to .
In total, we contribute to and leave unchanged. In this case, , so conservation is preserved. As an exercise, extend this reasoning to the rest of the cases:
- is a forward edge and is a forward edge.
- is a backward edge and is a forward edge.
- is a backward edge and is a backward edge.
Thus, in every case . Since held for , adding equal changes to both sides preserves the equality for . As was an arbitrary internal node, satisfies conservation everywhere.
Together, these establish that is a valid flow.
Termination. If all , then every residual capacity stays integral, so each bottleneck is a positive integer and value increase by at least 1 per iteration. Since value, the loop runs at most times.
Runtime Analysis. Suppose for flow network graph , let . We can find augmenting paths in time and augments in time (each path has at most nodes). This gives time overall, since the loop can run at most times. This is only pseudo-polynomial: the runtime depends on the capacities, not just on and .
Since the algorithm doesn't specify which augmenting path to pick at each step, a poor choice of paths can be extremely slow. Consider a network where an internal edge has capacity 1 but every other edge has a much larger capacity . If Ford-Fulkerson happens to alternate between two augmenting paths that both route through this narrow edge, forward through it in one path and backward (canceling flow) in the next, each round trip only increases the total flow by 1, even though a smarter choice of paths would saturate the network in just two augmentations.
Visualization Demo
Ford-Fulkerson Visualizer
Start with zero flow
Every edge begins at 0 flow.
Properties
Applications
Bipartite Matching
Max-flow Reduction
Min-cut Reduction
Practice
Problem 1.