English translation
Example usage
AI Article Decision Snapshot
Turn the lesson into workflow, model, budget, and security checks before choosing tools.
Use this quick snapshot before leaving the article. It keeps the next search tied to practical AI software, model/API, cost, privacy, and implementation questions.
Workflow fit
Identify the real job behind the article: coding, research, document review, support, analytics, content, or internal automation.
Model or tool decision
Decide whether the next step is a software shortlist, an AI tool comparison, an API platform choice, or a model benchmark.
Budget and usage signal
Estimate seats, API calls, prompt volume, retries, review time, and fallback work before assuming the workflow is cheap.
Security and privacy review
Check whether source code, customer data, private documents, prompts, logs, or embeddings will enter the AI workflow.
In the previous article of the graph algorithms series, we thoroughly explored minimum spanning trees (MSTs), including the widely used Kruskal’s and Prim’s algorithms. In this article, we delve into an important class of graph algorithms—network flow algorithms—and examine their practical applications.
At its core, network flow theory addresses how to optimally route resources from a source node to a sink node (also called the destination) within a directed flow network. Through concrete examples, we will study fundamental network flow problems and learn how to solve them using the Ford–Fulkerson algorithm.
Basic Concepts of Network Flow
In network flow, we work with a directed graph composed of the following elements:
- Nodes: Represent vertices in the graph, including a designated source node and a sink (or target) node .
- Edges: Each directed edge has a non-negative capacity , indicating the maximum amount that can flow across it.
- Flow: A function assigning to each edge a non-negative value representing the amount of flow sent from to , subject to capacity constraints.
Formal Definition of Flow
A flow on a directed graph satisfies the following two conditions:
-
Capacity Constraint: For every edge ,
-
Flow Conservation: For every intermediate node , total inflow equals total outflow:
By convention, the source has zero inflow, and the sink has zero outflow.
The Ford–Fulkerson Algorithm
Next, we introduce the Ford–Fulkerson method—a classic algorithm for computing the maximum flow from source to sink . It iteratively augments flow along augmenting paths in the residual network.
Algorithm Steps
- Initialize Flow: Set for all edges .
- Find Augmenting Path: Use DFS or BFS on the residual network to find a path from to where each edge has positive residual capacity.
- Augment Flow: Determine the bottleneck capacity—the minimum residual capacity along the path—and increase flow by that amount along each forward edge (and decrease it equivalently along reverse edges).
- Update Residual Network: Adjust residual capacities and reverse-edge flows. Repeat until no more augmenting paths exist.
Example Walkthrough
Let’s illustrate the Ford–Fulkerson algorithm with a concrete example.
Sample Network
Consider the following flow network:
10
(s)----->(A)
| /| \
| / | \
| / | \ 10
5| / | \
| / | \
v v v v
(B)----->(C)---->(t)
15 10
Here, is the source, is the sink, and edge labels denote capacities.
Implementation
Below is a Python implementation of the Ford–Fulkerson algorithm using BFS (i.e., the Edmonds–Karp variant):
from collections import defaultdict, deque
class Graph:
def __init__(self):
self.graph = defaultdict(list) # adjacency list representation
self.capacity = {} # edge capacities: (u, v) -> cap
def add_edge(self, u, v, cap):
self.graph[u].append(v)
self.graph[v].append(u) # add reverse edge
self.capacity[(u, v)] = cap
self.capacity[(v, u)] = 0 # reverse edge starts with 0 capacity
def bfs(self, s, t, parent):
visited = set()
queue = deque([s])
visited.add(s)
while queue:
u = queue.popleft()
for v in self.graph[u]:
if v not in visited and self.capacity[(u, v)] > 0:
visited.add(v)
parent[v] = u
queue.append(v)
return t in visited
def ford_fulkerson(self, s, t):
parent = {}
max_flow = 0
while self.bfs(s, t, parent):
# Find bottleneck capacity along the augmenting path
path_flow = float('Inf')
v = t
while v != s:
u = parent[v]
path_flow = min(path_flow, self.capacity[(u, v)])
v = parent[v]
# Update residual capacities
v = t
while v != s:
u = parent[v]
self.capacity[(u, v)] -= path_flow
self.capacity[(v, u)] += path_flow
v = parent[v]
max_flow += path_flow
return max_flow
# Example usage
g = Graph()
g.add_edge('s', 'A', 10)
g.add_edge('s', 'B', 5)
g.add_edge('A', 'C', 10)
g.add_edge('B', 'C', 15)
g.add_edge('C', 't', 10)
max_flow = g.ford_fulkerson('s', 't')
print(f"Maximum flow: {max_flow}")
This code constructs the sample network and computes its maximum flow using the Ford–Fulkerson method. Running it yields the numerical value of the maximum flow.
Summary
Network flow algorithms are essential tools in algorithm design and optimization, enabling solutions to real-world problems such as maximum flow, minimum cut, bipartite matching, transportation logistics, and resource allocation. Mastering the Ford–Fulkerson algorithm lays the groundwork for tackling more advanced flow-related problems—including those solvable via variants like Edmonds–Karp, Dinic’s algorithm, or push-relabel methods.
In the next article, we will explore advanced concepts and applications of dynamic programming—specifically how to apply DP techniques to solve increasingly complex optimization problems. Stay curious, and keep diving deeper!
Apply This Lesson
Turn this article into AI software, model, API, and security decisions.
English Article FAQ
Use this article as evidence before choosing AI tools
How should I use this AI Tutorials article?
Use it as the implementation or learning layer, then connect the idea to AI software buyer guides, tool comparisons, benchmarks, API choices, and security checks before making a production decision.
Is this English article different from the Chinese original?
The English edition is localized for global AI readers while preserving the original diagrams, screenshots, prompts, code examples, and source context from the Chinese article.
What should I read after Example usage?
Continue with AI Software Buyer Guides, AI Tools Workbench, Best AI Coding Agents, AI Model Benchmarks, OpenAI vs Anthropic API, or LLM Security Tools depending on the decision you need to make.
Can this article alone choose an AI product or model?
No. Treat the article as evidence and context, then validate fit with pricing, privacy requirements, integration effort, benchmark results, workflow tests, and fallback planning.
Continue