Interactive Graph Theory Learning
Interactive Graph Theory Learning
Guest User
Using app without sign in
Maximum flow calculator
Finds maximum flow from source to sink in flow network
Select an algorithm and generate steps to begin visualization
The maximum flow problem asks how much material can be pushed from a source to a sink through a network where each edge has a capacity. It is one of the most versatile models in combinatorial optimization, and by the max-flow min-cut theorem its value equals the capacity of the smallest cut separating source from sink.
The Ford-Fulkerson method repeatedly finds an augmenting path from source to sink in the residual graph, a bookkeeping structure that records remaining capacity and allows undoing flow. Pushing flow along augmenting paths until none remain yields a maximum flow. The Edmonds-Karp refinement always augments along a shortest path found by BFS, guaranteeing O(V E squared) time; Dinic's algorithm improves this further with level graphs and blocking flows.
Max flow models pipeline and traffic throughput, bipartite matching for job assignment, airline crew scheduling, image segmentation in computer vision, baseball elimination, and project selection. It is the standard advanced graph topic in competitive programming and senior interviews.
Every maximum-flow algorithm in this family is the same loop: find a path from source to sink with spare capacity, push as much as it allows, repeat. The algorithms differ only in how they choose that path.
MaxFlow(graph, s, t):
flow = 0
build residual graph: cap(u,v) forward, 0 backward
while there is an augmenting path P from s to t
in the residual graph:
bottleneck = min residual capacity along P
for each edge (u, v) in P:
residual[u][v] -= bottleneck
residual[v][u] += bottleneck // the undo edge
flow += bottleneck
return flow
// Ford-Fulkerson: find P by DFS (any path)
// Edmonds-Karp: find P by BFS (shortest path)The backward residual edge is the part that looks wrong and is in fact essential. It lets a later augmenting path cancel flow pushed earlier, which is how the algorithm escapes a bad early choice without backtracking. Without those undo edges the greedy loop gets stuck at a suboptimal flow.
Run Edmonds-Karp on the standard example where a greedy first choice must later be undone.
Example graph: Directed capacities: S to A (10), S to B (10), A to B (2), A to T (4), B to T (9).
Maximum flow is 13, and the minimum cut is the pair of edges A-T and B-T with total capacity 13. The two numbers being equal is not a coincidence: it is the max-flow min-cut theorem.
Time: O(V·E^2) Edmonds-Karp · Space: O(V + E)
Ford-Fulkerson with arbitrary path selection runs in O(E times maxflow), because each augmentation adds at least one unit but the path search costs O(E). That is pseudo-polynomial and genuinely bad: with capacities of a billion it can take a billion augmentations, and with irrational capacities it may not terminate at all. Edmonds-Karp fixes this by choosing the shortest augmenting path via BFS. The shortest-path distance from source to sink never decreases, and each edge can become the bottleneck at most V/2 times, bounding the number of augmentations at O(VE) and the total at O(V times E squared). Dinic groups augmentations into phases using a level graph and improves this to O(V squared times E), or O(E times sqrt(V)) on unit-capacity graphs.
The choice is mostly about capacity magnitudes and graph size.
| Alternative | Prefer it when | Cost |
|---|---|---|
| Edmonds-Karp | The default. BFS path selection makes the bound independent of capacity values. | O(V·E^2) |
| Dinic | Larger graphs. Level graphs and blocking flows make it substantially faster in practice. | O(V^2·E) |
| Push-relabel | Very large dense graphs where the best asymptotic behaviour matters. | O(V^3) |
| Hopcroft-Karp | The problem is really bipartite matching, a special case of unit-capacity max flow. | O(E·sqrt(V)) |
| Min-cut | You want the bottleneck edges rather than the throughput. Same computation, read differently. | same as max flow |
Related algorithms: Minimum Cut, Bipartite Check, Breadth-First Search