Network Flow Algorithms Explained | Generated by AI
Understanding Network Flow Algorithms
Network flow algorithms are a fundamental topic in graph theory and computer science, used to model and solve problems involving the flow of resources through a network (like pipes, roads, or data streams). They’re essential for optimization tasks such as maximum flow (e.g., how much water can flow through a system of pipes), minimum cut (e.g., finding bottlenecks), and applications in transportation, scheduling, and matching.
I’ll break this down step-by-step: key concepts, a simple example, main algorithms, and tips for deeper learning. We’ll focus on the max-flow problem, as it’s the core.
1. Key Concepts
- Graph Representation: A network is a directed graph \( G = (V, E) \) with vertices \( V \) (nodes) and edges \( E \) (connections). Each edge has a capacity \( c(u, v) \), the maximum flow it can carry from node \( u \) to \( v \).
- Source and Sink: One node is the source \( s \) (where flow starts) and one is the sink \( t \) (where it ends).
- Flow: A function \( f(u, v) \) assigning how much flow goes along each edge, satisfying:
- Capacity constraint: \( 0 \leq f(u, v) \leq c(u, v) \).
- Conservation: For any node that’s not \( s \) or \( t \), inflow = outflow (no accumulation).
- Net Flow: Flow is antisymmetric: \( f(u, v) = -f(v, u) \).
- Residual Graph: Tracks remaining capacity after sending flow. If you send \( f \) on an edge with capacity \( c \), residual forward is \( c - f \), and backward is \( f \) (to “undo” flow).
- Goals:
- Maximum Flow: Maximize total flow from \( s \) to \( t \).
- Minimum Cut: Partition nodes into \( S \) (with \( s \)) and \( T \) (with \( t \)); minimize sum of capacities from \( S \) to \( T \). By max-flow min-cut theorem, max flow = min cut capacity.
2. A Simple Example
Imagine a tiny network for shipping goods:
- Nodes: \( s \) (source), A, B, \( t \) (sink).
- Edges:
- \( s \to A \): capacity 10
- \( s \to B \): capacity 10
- \( A \to B \): capacity 2
- \( A \to t \): capacity 8
- \( B \to t \): capacity 9
ASCII visualization:
s
/ \
10 10
A B
| \ / |
8 2 9
\ /
t
What’s the max flow? Intuitively, send 10 to A and 10 to B, but A can only push 8 to t (2 goes to B, which helps B push 9+2=11, but B’s limit is 9? Wait, let’s compute properly.
Using an algorithm (below), max flow is 17:
- Path 1: s→A→t (flow 8), residual updates.
- Path 2: s→B→t (flow 9), residual updates.
- Path 3: s→A→B→t (flow 0? Wait, after first, A has 2 left to B, but B to t has 0 left—actually, adjust.
Better: Total from s is 20, but bottlenecks limit to 17 (8 direct from A + 9 from B, with 2 rerouted? No—run algo for precision.
3. Main Algorithms
Start with basics; build to efficient ones. All augment flow along paths in the residual graph until no more augmenting paths exist.
- Ford-Fulkerson Method (1956, foundational):
- Repeatedly find any path from s to t in residual graph (e.g., via DFS/BFS).
- Augment flow by the min residual capacity on that path.
- Repeat until no path.
-
Time: Depends on implementation; can be slow if capacities are irrational (but integers: O( E * max_flow)). - Pros: Simple. Cons: Inefficient for large graphs.
- Pseudocode:
while there is a path P from s to t in residual graph: bottleneck = min residual capacity on P augment flow along P by bottleneck update residuals return total flow
- Edmonds-Karp (1972, BFS variant of Ford-Fulkerson):
- Use BFS to find shortest augmenting path (avoids long paths).
-
Time: O( V * E ^2) — polynomial, practical for small graphs. - Great for learning; implementable in ~50 lines of code.
- Dinic’s Algorithm (1970, faster):
- Builds level graph via BFS (layers by distance from s).
- Uses DFS to find blocking flows (multiple paths per level).
-
Time: O( V ^2 * E ) worst-case, but O( V * E ) for unit capacities; very fast in practice. - When to use: Medium-large graphs.
- Push-Relabel (or Preflow-Push) (1980s, Goldberg-Tarjan):
- “Pushes” excess flow from nodes toward sink using heuristics.
-
Time: O( V ^3) or better with FIFO/gap heuristics. - Pros: Handles huge graphs (e.g., internet routing).
For min-cut: After max-flow, the cut is nodes reachable from s in final residual graph vs. others.
4. Implementation Tips
- Use adjacency lists for graphs.
- In code (Python-like): Represent residual capacities as a dict of dicts.
- Libraries: NetworkX (Python) has built-ins like
maximum_flow. - Test on small examples; visualize with tools like Graphviz.
5. Common Pitfalls & Extensions
- Cycles: Residuals handle “backflow” to adjust.
- Multiple Sources/Sinks: Add super-source/sink.
- Costs: For min-cost flow, add edge costs and use successive shortest paths.
- Applications: Airline scheduling, image segmentation (vision), bipartite matching (set flow=1).
Learning Path
- Read CLRS (Cormen et al.) Chapter 26.
- Practice on LeetCode/HackerRank (e.g., “Minimum Cost to Make at Least One Valid Path”).
- Visualize: Draw residuals after each augmentation.
Introduction to Network Flows
Max Flow Min Cut Theorem
NetworkX Tutorial