Algorithms · Visualized
Max Flow (Edmonds-Karp)
Graph Algorithms
Traverse, order, and find shortest paths and spanning trees on graphs — the machinery behind maps, networks, schedulers and dependency resolvers.
Maximum flow asks how much can be pushed from a source to a sink through capacitated directed edges without exceeding any capacity. Time: O(V·E²) average, O(V·E²) worst; space: O(V + E) for the residual graph, BFS queue and predecessor arrays.
Big-O: Traversals O(V + E); Dijkstra O(E log V) with a heap; Bellman–Ford O(V·E); Floyd–Warshall O(V³); MST O(E log E).
Max Flow (Edmonds-Karp)
Graph AlgorithmsMaximum flow asks how much can be pushed from a source to a sink through capacitated directed edges without exceeding any capacity. The visualizer keeps a residual capacity for every edge — capacity minus current flow — and repeatedly finds an augmenting path from S to T in that residual graph. The bottleneck edge on the path limits how much can be added, and pushing flow there reduces the residual capacity forwards while creating residual capacity backwards, which is what allows a later path to undo a bad earlier choice. Edmonds-Karp makes the choice deterministic by always taking the shortest augmenting path, found with a breadth-first search, instead of an arbitrary one.
How Max Flow (Edmonds-Karp) Works
- Start with zero flow on every edge, so residual capacity equals capacity.
- Run BFS from the source over edges with positive residual capacity to find the shortest S→T path.
- Record the bottleneck: the smallest residual capacity along that path.
- Add the bottleneck to every forward edge on the path and subtract it from the matching reverse edges.
- Repeat the BFS until no S→T path remains in the residual graph.
- The total pushed is the maximum flow, and the saturated edges crossing the final reachable set form the minimum cut.
Max Flow (Edmonds-Karp) Worked Example
Input: {source: 0, sink: 5}
The default network is S→A 10, S→B 8, A→B 2, A→C 6, B→D 8, C→D 3, C→T 8 and D→T 10. BFS finds S→A→C→T with bottleneck min(10, 6, 8) = 6, so C→T drops to 2 residual and flow is 6. The next BFS finds S→B→D→T with bottleneck min(8, 8, 10) = 8, taking the total to 14; D→T keeps 2 residual. A further BFS reaches A but A→C and A→B are both saturated, so no S→T path exists and the algorithm stops at 14 — S→A ends at 6/10, S→B at 8/8, A→C at 6/6, B→D at 8/8, C→T at 6/8 and D→T at 8/10.
from collections import deque
def edmonds_karp(cap, s, t):
n = len(cap)
flow = [[0] * n for _ in range(n)]
total = 0
while True:
prev = [-1] * n # BFS residual path
prev[s] = s
q = deque([s])
while q and prev[t] == -1:
u = q.popleft()
for v in range(n):
residual = cap[u][v] - flow[u][v]
if residual > 0 and prev[v] == -1:
prev[v] = u
q.append(v)
if prev[t] == -1:
break # no path: flow is max
bottle = min(cap[prev[v]][v] - flow[prev[v]][v]
for v in walk(prev, s, t))
for v in walk(prev, s, t):
u = prev[v]
flow[u][v] += bottle
flow[v][u] -= bottle
total += bottle
return total # O(V·E²)Behind the Scenes Execution
Press ▶ Run in the visualizer — the call stack, registers and memory will replay here in sync with the code, line by line.
Max Flow (Edmonds-Karp) Time & Space Complexity
When to Use Max Flow (Edmonds-Karp)
- Bipartite assignment: workers to shifts, ad slots to campaigns, servers to requests.
- Network throughput and pipeline capacity planning, where edges are pipes and capacities are limits.
- Image segmentation and project-selection problems that reduce to a minimum cut.
Recommended resourcesAffiliate links · may earn us a commission
Recommended for learners
Keep the momentum going
Step through a related structure, pattern or course — the ideas reinforce each other and stick faster.