Network flow is rare in standard interviews but appears at senior levels or when a problem secretly reduces to matching/assignment. Aim for awareness: recognize the reduction, know Edmonds-Karp exists.
| Algorithm | Time | Note |
|---|---|---|
| Ford-Fulkerson (DFS) | O(E · maxflow) | Only integral capacities; can be slow |
| Edmonds-Karp (BFS) | O(V · E²) | Shortest augmenting path; standard to cite |
| Dinic's | O(V² · E) | Level graph + blocking flow; fast in practice |
Edmonds-Karp skeleton (BFS augmenting paths on a capacity matrix).
import java.util.*;
/** Edmonds-Karp max flow. cap[u][v] = capacity. Returns max flow s→t. */
public int maxFlow(int[][] cap, int s, int t, int n) {
int flow = 0;
while (true) {
int[] parent = new int[n];
Arrays.fill(parent, -1);
parent[s] = s;
Queue<Integer> q = new ArrayDeque<>();
q.offer(s);
while (!q.isEmpty() && parent[t] == -1) { // BFS for augmenting path
int u = q.poll();
for (int v = 0; v < n; v++)
if (parent[v] == -1 && cap[u][v] > 0) {
parent[v] = u;
q.offer(v);
}
}
if (parent[t] == -1) break; // no path ⇒ done
int bottleneck = Integer.MAX_VALUE; // min residual on path
for (int v = t; v != s; v = parent[v])
bottleneck = Math.min(bottleneck, cap[parent[v]][v]);
for (int v = t; v != s; v = parent[v]) { // update residuals
cap[parent[v]][v] -= bottleneck;
cap[v][parent[v]] += bottleneck; // reverse edge
}
flow += bottleneck;
}
return flow;
}
| Disguise / phrasing | Reduces to | Twist to handle |
|---|---|---|
| "Maximum bipartite matching" | Max flow (unit capacities) | Source→left→right→sink, cap 1 |
| "Max # of edge/vertex-disjoint paths" | Max flow (Menger) | Unit caps; split vertices for vertex-disjoint |
| "Min cut to separate / sabotage network" | Min cut = max flow | Compute max flow, read the cut |
| "Assign tasks/workers with limits" | Flow with capacities | Capacities encode the limits |
| "Project selection / max profit with prereqs" | Min-cut (closure) | Classic max-flow reduction |
| "Escape the grid (multiple exits, no overlap)" | Vertex-capacity flow | Split each cell into in/out with cap 1 |
The capstone cheat-sheet — map any graph problem to its algorithm in seconds.
| Signal | Algorithm | Day |
|---|---|---|
| Fewest steps, unweighted | BFS | 2 |
| Reachability, enumerate paths, region shape | DFS | 3 |
| Count groups (undirected) | Connected components | 4 |
| Shortest path, non-negative weights | Dijkstra | 7 |
| Negative edges / bounded hops | Bellman-Ford | 6 |
| All-pairs shortest, small V | Floyd-Warshall | 7 |
| Weights ∈ {0,1} | 0-1 BFS | 6 |
| Dynamic connectivity / merging groups | Union-Find | 8–10 |
| Dependency ordering (DAG) | Topological sort | 11–12 |
| Directed cycle detection | 3-color DFS / Kahn count | 12 |
| Connect all at min cost | MST (Kruskal / Prim) | 13–14 |
| Two-group split / conflicts apart | Bipartite check | 16 |
| Directed mutual reachability / 2-SAT | Tarjan's SCC | 17 |
| Single points of failure (undirected) | Bridges / articulation | 18 |
| Goal-directed search / heuristic | A* | 19 |
| Use every edge once / itinerary | Eulerian (Hierholzer) | 19 |
| Matching / min cut / capacity limits | Max flow | 20 |