Maximum Flow (Ford-Fulkerson) - Problem

Implement the Ford-Fulkerson algorithm to find the maximum flow in a flow network using BFS (Edmonds-Karp implementation).

Given a flow network represented as an adjacency matrix where capacity[u][v] represents the capacity of the edge from vertex u to vertex v, find the maximum flow from source s to sink t.

The Ford-Fulkerson method uses BFS to find augmenting paths and increases the flow along these paths until no more augmenting paths exist.

Key concepts:

  • Flow Network: A directed graph where each edge has a capacity
  • Augmenting Path: A path from source to sink with available capacity
  • Residual Graph: Graph showing remaining capacities after current flow
  • Max Flow Min Cut Theorem: Maximum flow equals minimum cut capacity

Input & Output

Example 1 — Basic Flow Network
$ Input: capacity = [[0,16,13,0,0,0],[0,0,0,12,0,0],[0,4,0,0,14,0],[0,0,9,0,0,20],[0,0,0,7,0,4],[0,0,0,0,0,0]], source = 0, sink = 5
Output: 23
💡 Note: Maximum flow from vertex 0 to vertex 5 is 23. Path 0→1→3→5 gives flow 12, path 0→2→4→5 gives flow 4, path 0→2→4→3→5 gives flow 7.
Example 2 — Simple Network
$ Input: capacity = [[0,10,10,0],[0,0,0,10],[0,0,0,10],[0,0,0,0]], source = 0, sink = 3
Output: 20
💡 Note: Two paths: 0→1→3 with flow 10 and 0→2→3 with flow 10. Total maximum flow is 20.
Example 3 — Bottleneck Network
$ Input: capacity = [[0,100,0,100],[0,0,1,0],[0,0,0,100],[0,0,0,0]], source = 0, sink = 3
Output: 101
💡 Note: Direct path 0→3 gives flow 100, path 0→1→2→3 gives flow 1. Bottleneck at edge 1→2 limits this path.

Constraints

  • 1 ≤ vertices ≤ 100
  • 0 ≤ capacity[i][j] ≤ 1000
  • 0 ≤ source, sink < vertices
  • source ≠ sink

Visualization

Tap to expand
INPUTFlow Network0Source12345Sink1613121420capacity[u][v] = edge capacitysource = 0, sink = 5ALGORITHM STEPSFord-Fulkerson BFS1BFS finds augmenting pathSearch source to sink2Find bottleneck capacityMin capacity along path3Update residual graphForward/backward edges4Repeat until no pathNo augmenting path existsResidual GraphForward: -flowBackward: +flowEnables flow reversalFINAL RESULTMaximum Flow23Max Flow ValuePaths found:0→1→3→5: flow 120→2→4→5: flow 40→2→4→3→5: flow 7Total: 12+4+7=23Key Insight:The Ford-Fulkerson algorithm uses BFS to find shortest augmenting paths, then pushes maximumflow through each path while maintaining a residual graph that tracks remaining capacities andenables flow reversal, continuing until no path from source to sink has available capacity.TutorialsPoint - Maximum Flow (Ford-Fulkerson) | BFS (Edmonds-Karp)
Asked in
Google 45 Microsoft 38 Amazon 32 Facebook 28 Netflix 22
75.0K Views
Medium Frequency
~35 min Avg. Time
1.9K Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen