Network Flow & Matching

Maximum flow, the max-flow min-cut theorem, Ford-Fulkerson / Edmonds-Karp, and bipartite matching.

Advanced 2 min readLesson 15 of 18 Interview Q&A

A flow network is a directed graph where each edge has a capacity, with a source s and a sink t. The maximum flow problem asks: how much can flow from s to t without exceeding any capacity, with flow conserved at every intermediate vertex? Think water pipes, network bandwidth, or traffic.

10852107310sABCDt
Edge labels are capacities. The maximum s → t flow here is 15.

Ford-Fulkerson and the residual graph

  1. Start with zero flow.
  2. Find an augmenting path from s to t in the residual graph — edges with leftover capacity, plus reverse edges carrying the flow already sent (which lets the algorithm "undo" earlier bad choices).
  3. Push the bottleneck amount along it; update residual capacities.
  4. Repeat until no augmenting path exists.

Edmonds-Karp finds augmenting paths with BFS (shortest first), guaranteeing O(V · E²). Dinic's algorithm (BFS level graph + blocking flows) achieves O(V² · E) and is the competitive-programming favourite.

Edmonds-Karp
int maxFlow(int[][] cap, int s, int t) {          // adjacency-matrix capacities
    int n = cap.length, flow = 0;
    int[][] res = new int[n][];
    for (int i = 0; i < n; i++) res[i] = cap[i].clone();
    int[] parent = new int[n];
    while (true) {
        Arrays.fill(parent, -1);
        parent[s] = s;
        Deque<Integer> q = new ArrayDeque<>(List.of(s));
        while (!q.isEmpty() && parent[t] == -1) {
            int u = q.poll();
            for (int v = 0; v < n; v++)
                if (parent[v] == -1 && res[u][v] > 0) { parent[v] = u; q.offer(v); }
        }
        if (parent[t] == -1) return flow;              // no augmenting path
        int push = Integer.MAX_VALUE;
        for (int v = t; v != s; v = parent[v]) push = Math.min(push, res[parent[v]][v]);
        for (int v = t; v != s; v = parent[v]) {
            res[parent[v]][v] -= push;
            res[v][parent[v]] += push;                 // reverse edge
        }
        flow += push;
    }
}

Max-flow min-cut theorem

An s-t cut splits the vertices into a side containing s and a side containing t; its capacity is the total capacity of edges crossing from the s-side to the t-side. The theorem:

The maximum flow equals the minimum cut capacity.

When Ford-Fulkerson stops, the vertices reachable from s in the residual graph form the s-side of a minimum cut. So max flow also answers "what's the cheapest set of links to cut to disconnect s from t?" — network reliability, image segmentation (graph cuts), and project selection.

Bipartite matching

Match workers to jobs (each at most once, only compatible pairs). Add a source connected to every worker and a sink connected from every job, all capacity 1; the max flow is the maximum matching. Simpler dedicated algorithms: augmenting-path DFS (Kuhn's, O(V·E)) and Hopcroft-Karp (O(E√V)). Weighted assignment is solved by the Hungarian algorithm.

Key takeaways
  • Max flow pushes as much as possible from source to sink under capacity limits.
  • Augment along paths in the residual graph; reverse edges allow undoing flow.
  • Edmonds-Karp (BFS paths) is O(VE²); Dinic is faster in practice.
  • Max flow = min cut. Bipartite matching reduces to max flow with unit capacities.
Interview questions
QWhy are reverse edges necessary in the residual graph?hard

A greedy first path might route flow through an edge that blocks a better overall solution. Reverse edges let a later augmenting path cancel some of that flow and reroute it, which is what makes the algorithm reach the true maximum.