Published by Dimitris Kougioumtzis · Updated
Find shortest augmenting paths in the residual network. Source is 0; sink is the highest numbered node. Edge weights are capacities.
Use BFS to find a residual augmenting path, push its bottleneck, and maintain reverse capacities for later corrections.
When to use Edmonds–Karp maximum flow
Maximum throughput from source to sink.
Choosing an alternative
General Ford-Fulkerson may use arbitrary augmenting paths; Edmonds-Karp chooses BFS paths.
Worked example: input and state changes
Example input
0,1,4; 0,2,2; 2,1,1; 1,3,5; 2,3,8; 3,4,2
These checkpoints follow the actual example timeline. State details show the stored values and decisions; use the visualization to inspect every intervening step.
Step 0: Residual capacity matrix. Parallel capacities are added.
State at this checkpoint
{ "values": [ 0, 4, 2, 0, 0, 0, 0, 0, 5, 0, 0, 1, 0, 8, 0, 0, 0, 0, 0, 2, 0, 0, 0, 0, 0 ], "active": [], "kind": "matrix", "columns": 5 }Step 1: Augment along 0 → 1 → 3 → 4 by 2.
State at this checkpoint
{ "values": [ 0, 4, 2, 0, 0, 0, 0, 0, 5, 0, 0, 1, 0, 8, 0, 0, 0, 0, 0, 2, 0, 0, 0, 0, 0 ], "active": [], "kind": "matrix", "columns": 5 }Step 3: Update forward and reverse capacities for 1 → 3.
State at this checkpoint
{ "values": [ 0, 4, 2, 0, 0, 0, 0, 0, 3, 0, 0, 1, 0, 8, 0, 0, 2, 0, 0, 0, 0, 0, 0, 2, 0 ], "active": [ 8, 16 ], "kind": "matrix", "columns": 5 }Step 4: Update forward and reverse capacities for 0 → 1.
State at this checkpoint
{ "values": [ 0, 2, 2, 0, 0, 2, 0, 0, 3, 0, 0, 1, 0, 8, 0, 0, 2, 0, 0, 0, 0, 0, 0, 2, 0 ], "active": [ 1, 5 ], "kind": "matrix", "columns": 5 }Step 5: Maximum flow = 2. No augmenting path remains.
State at this checkpoint
{ "values": [ 0, 2, 2, 0, 0, 2, 0, 0, 3, 0, 0, 1, 0, 8, 0, 0, 2, 0, 0, 0, 0, 0, 0, 2, 0 ], "active": [], "kind": "matrix", "columns": 5 }
Expected final state
Maximum flow = 2. No augmenting path remains.
Complexity and input bounds
Family: Graphs. Time complexity: O(VE²).
Implementation detail: O(V E squared) is the standard adjacency-list algorithm bound. The reference uses a residual matrix, giving an O(V cubed times E + V squared) conservative bound including matrix scans.
Space complexity: O(V squared) residual matrix plus O(V) BFS state.
Working-space bounds describe algorithm state and any explicitly noted output, excluding the visualization's saved timeline. Recording frames can use more memory than running the function alone. Here n is sequence length, V and E are vertex and edge counts, and other parameters refer to the named input dimensions.
Directed edges as source,target,weight; separated by semicolons. Nodes 0–7, positive weights 1–99. Start node is 0.
The playground validates these limits before starting. Downloaded functions are callable implementations, with parameter and output-buffer contracts in their comments; they do not all repeat the playground's input validation.
Common mistakes and limitations
Reverse residual edges are essential for revising earlier flow decisions.
Further reading
For background on algorithm analysis and the JavaScript function interface:
- MIT OpenCourseWare: algorithm design and analysis foundations
- MDN: JavaScript function parameters and return values
Editorial and testing approach · Browse all algorithm guides
Complete JavaScript implementation
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 | // Directed edge arrays from, to, weight; nodes 0..vertices-1; source 0. Up to 8 vertices, 16 edges. Allocate output with at least 256 integers. Weights are positive capacities; parallel capacities are added. Source 0, sink vertices-1. Returns max flow; output receives final residual capacities. One-vertex networks return 0. Reference snippets assume validated playground inputs. Fixed scratch buffers have 256 slots. Sorting variants may use a different traversal strategy than the animation. function algorithm(from, to, weight, edges, vertices, output) { for (let i = 0; i < vertices * vertices; i++) { output[i] = 0; } for (let i = 0; i < edges; i++) { output[from[i] * vertices + to[i]] += weight[i]; } let parent = Array(256).fill(0); let queue = Array(256).fill(0); let flow = 0; while (vertices > 1) { for (let v = 0; v < vertices; v++) { parent[v] = -1; } let head = 0; let tail = 1; queue[0] = 0; parent[0] = 0; while (head < tail && parent[vertices - 1] < 0) { let u = queue[head++]; for (let v = 0; v < vertices; v++) { if (parent[v] < 0 && output[u * vertices + v] > 0) { parent[v] = u; queue[tail++] = v; } } } if (parent[vertices - 1] < 0) { break; } let amount = 1000000; for (let v = vertices - 1; v != 0; v = parent[v]) { let capacity = output[parent[v] * vertices + v]; if (capacity < amount) { amount = capacity; } } for (let v = vertices - 1; v != 0; v = parent[v]) { let u = parent[v]; output[u * vertices + v] -= amount; output[v * vertices + u] += amount; } flow += amount; } return flow; } |
Source
Primary research source for the algorithm or its underlying method. The playground is a bounded teaching implementation, not the publication's original program.
- Theoretical Improvements in Algorithmic Efficiency for Network Flow Problems
Jack Edmonds; Richard M. Karp (1972). Research paper.