Network Flow Algorithm

Правка en2, от deinepapa, 2025-10-21 21:23:10

Hey everyone!

We were taught basic network flow in our algorithms course in university and I started to learn it. Found this useful blog blog by Zhtluo link

Target Audience.

This post is for anyone who has some free time :)

Help if you can spare some time

Basic

What is network flow

Bipartite Matching Example

Suppose we have two sets:
Set A = people Set B = books

Each person likes certain books, and we want to find the maximum number of unique person-book matches, where each person gets exactly one book they like.

Here’s how Network Flow helps: Connect the source to all people. Connect each person to the books they like. Connect all books to the sink.

Then, when we run a maximum flow algorithm, the maximum flow value will tell us the number of unique person-book pairs that can be matched.
This is basically the maximum bipartite matching problem.

The Ford–Fulkerson Algorithm

We keep finding paths from the source to the sink that still have some remaining capacity, and push as much flow as possible through them — until no such paths exist.

Steps

  1. Use DFS to find a path from source to sink.

  2. Find the smallest capacity (bottleneck) along that path.

  3. Send that much “water” (flow) through the path.

  4. Add a reverse edge to allow undoing flow later.

  5. Repeat until no more augmenting paths exist.

Time Complexity

O(E × f) where f is the value of the maximum flow.

Code

Code

It’s conceptually simple but can be slow if the capacities are large because runtime depends directly on the flow value.

The Edmonds–Karp Algorithm

This is an optimized version of Ford–Fulkerson that uses BFS instead of DFS to find augmenting paths.

Why BFS? Using DFS can lead to zig-zagging paths that push very little flow each time, making the algorithm slow. BFS ensures we always find the shortest path (in terms of number of edges), which makes the process much more efficient.

Steps

  1. Use BFS to find the shortest augmenting path.

  2. Find the bottleneck capacity on that path.

  3. Push flow along the path and update reverse edges.

  4. Repeat until no more augmenting paths exist.

Time Complexity

O(V × E²)

This guarantees a polynomial runtime

Code

Code

PRACTICE PROBLEMS

  1. Katis Elementary math
  2. Katis Gopher2
  3. CF Delivery Bears
  4. ICPC regionals I. Magic Potions

Again, if possible, please provide new perspectives and interesting problems to explore using this algorithm

Теги network flow, beginner

История

 
 
 
 
Правки
 
 
  Rev. Язык Кто Когда Δ Комментарий
en2 Английский deinepapa 2025-10-21 21:23:10 197 (published)
en1 Английский deinepapa 2025-10-21 21:19:44 6519 Initial revision (saved to drafts)