DSA Maximum Flow

DSA Tutorial

DSA – Maximum Flow (Complete Beginner Guide)
(Concept • Terminology • Ford–Fulkerson • Edmonds–Karp • Example • Code • Complexity • Interview Tips)

Maximum Flow is a classic graph problem where we find the maximum possible flow from a source to a sink without violating capacity constraints.

 One-line idea:
Push as much flow as possible from Source (S) to Sink (T).


1️⃣ What is the Maximum Flow Problem?

Given

  • A directed graph

  • Each edge has a capacity

  • A source (S) and a sink (T)

Find

  • The maximum flow from S to T

  • Such that:

    • Flow on edge ≤ capacity

    • Flow conservation holds at intermediate vertices


2️⃣ Key Terminology

Term Meaning
Flow Amount sent through an edge
Capacity Max flow an edge can carry
Residual Capacity Remaining capacity
Augmenting Path Path with available capacity
Residual Graph Graph showing remaining capacities

3️⃣ Real-Life Applications

✔ Network bandwidth optimization
✔ Traffic / water / pipeline systems
✔ Bipartite matching
✔ Image segmentation
✔ Project scheduling


4️⃣ Flow Network Example

S → A (10)
S → C (10)
AB (4)
A → C (2)
C → D (9)
D → B (6)
B → T (10)
D → T (10)

5️⃣ Core Idea (Augmenting Path)

  1. Find a path from S → T

  2. Minimum capacity on that path = bottleneck

  3. Push that flow

  4. Update residual graph

  5. Repeat until no path exists


6️⃣ Ford–Fulkerson Algorithm

 Idea

  • Repeatedly find any augmenting path

  • Add flow until no path exists

 Path search can be DFS or BFS

 Complexity

O(E × MaxFlow) (can be slow)

7️⃣ Edmonds–Karp Algorithm

Improved Ford–Fulkerson

 Difference

  • Uses BFS to find shortest augmenting path

  • Guarantees polynomial time

 Complexity

O(V × E²)

Most commonly asked in interviews


8️⃣ Step-by-Step Flow (Edmonds–Karp)

  1. Initialize all flows = 0

  2. Run BFS on residual graph to find S → T

  3. Find bottleneck capacity

  4. Augment flow

  5. Update residual capacities

  6. Repeat until BFS fails


9️⃣ Edmonds–Karp C++ Implementation


 


🔟 Time & Space Complexity

Algorithm Time Complexity
Ford–Fulkerson O(E × MaxFlow)
Edmonds–Karp O(V × E²)

Space: O(V²) (adjacency matrix)


1️⃣1️⃣ Maximum Flow vs Minimum Cut

Max-Flow Min-Cut Theorem

Maximum flow value = Minimum cut capacity

 If you find max flow → you also found min cut


1️⃣2️⃣ Common Interview Questions

Q1. Why residual graph is needed?
👉 To track remaining capacity and reverse flow

Q2. Difference between Ford–Fulkerson & Edmonds–Karp?
👉 Path selection (DFS vs BFS)

Q3. Can edges have negative capacity?
👉 No

Q4. Is Maximum Flow applicable to undirected graphs?
👉 Convert edges to bidirectional directed edges


Final Summary

✔ Works on directed graphs
✔ Uses augmenting paths
✔ Residual graph is key
✔ Edmonds–Karp is interview-friendly
✔ Foundation for matching & min-cut problems