Implement Edmonds-Karp
Standard · Write code · about 20 min
Return the maximum flow from source to sink. graph.edges lists [u, v, capacity] for directed edges u → v. Parallel edges add their capacities.
Use BFS to find each augmenting path, as Edmonds-Karp does.
Example 1
Input:
[{"n":6,"edges":[[0,1,3],[0,3,2],[1,4,2],[1,2,2],[3,4,2],[4,5,2],[2,5,3]],"directed":true},0,5]
Output:
4
The third path uses the backward edge 4 → 1.
Example 2
Input:
[{"n":4,"edges":[[0,1,3],[1,3,2],[0,2,2],[2,3,4]],"directed":true},0,3]
Output:
4
Two separate paths carry 2 each.
Constraints
- 2 ≤ n ≤ 100
- 0 ≤ number of edges ≤ 300
- 1 ≤ capacity ≤ 100
- source ≠ sink
Learn the idea first: Maximum flow: Ford-Fulkerson and Edmonds-Karp, a visual lesson.
More Maximum flow: Ford-Fulkerson and Edmonds-Karp problems
- Why backward edges Warm-up
- Augment by hand Warm-up
- Bottleneck edge Standard