Tutorialspoint
Problem
Solution
Submissions

Maximum Flow in a Flow Network

Certification: Advanced Level Accuracy: 100% Submissions: 5 Points: 15

Write a Python function that implements the Ford-Fulkerson algorithm to find the maximum flow in a flow network. The flow network is a directed graph where each edge has a capacity, and the algorithm finds the maximum flow from a source vertex to a sink vertex.

Example 1
  • Input: Graph = [
    [0, 16, 13, 0, 0, 0],
    [0, 0, 10, 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
  • Explanation:
    • Step 1: Create a residual graph with given capacities.
    • Step 2: Find augmenting paths from source to sink using BFS or DFS.
    • Step 3: Update residual capacities along each path.
    • Step 4: Repeat until no more augmenting paths exist.
    • Step 5: The maximum flow possible from vertex 0 to vertex 5 is 23 units.
Example 2
  • Input: Graph = [
    [0, 10, 0, 10, 0],
    [0, 0, 4, 2, 8],
    [0, 0, 0, 0, 10],
    [0, 0, 6, 0, 10],
    [0, 0, 0, 0, 0]],
    Source = 0, Sink = 4
  • Output: 19
  • Explanation:
    • Step 1: Create a residual graph with given capacities.
    • Step 2: Find augmenting paths from source to sink using BFS or DFS.
    • Step 3: Update residual capacities along each path.
    • Step 4: Repeat until no more augmenting paths exist.
    • Step 5: The maximum flow possible from vertex 0 to vertex 4 is 19 units.
Constraints
  • 2 ≤ number of vertices ≤ 100
  • 0 ≤ edge capacity ≤ 10^9
  • Graph is represented as an adjacency matrix
  • Time Complexity: O(max_flow * E), where E is the number of edges
  • Space Complexity: O(V²), where V is the number of vertices
GraphFacebookShopify
Editorial

Login to view the detailed solution and explanation for this problem.

My Submissions
All Solutions
Lang Status Date Code
You do not have any submissions for this problem.
User Lang Status Date Code
No submissions found.

Please Login to continue
Solve Problems

 
 
 
Output Window

Don't have an account? Register

Solution Hints

  • Use a residual graph to keep track of remaining capacities
  • Use BFS or DFS to find an augmenting path from source to sink
  • While there is an augmenting path, add the path's bottleneck capacity to the maximum flow
  • Update residual capacities of edges and reverse edges along the path
  • Return the maximum flow when no more augmenting paths exist

Steps to solve by this approach:

 Step 1: Create a residual graph (initially identical to the original graph).
 Step 2: While there exists an augmenting path from source to sink using BFS:
 Step 3: Find the minimum capacity (bottleneck) along the augmenting path.
 Step 4: Update the residual graph by subtracting the bottleneck capacity from forward edges.
 Step 5: Add the bottleneck capacity to backward edges to allow flow cancellation.
 Step 6: Add the bottleneck capacity to the maximum flow.
 Step 7: Return the final maximum flow when no more augmenting paths exist.

Submitted Code :