Network Flow Algorithms PDF
Network Flow Algorithms PDF
Video Series
Max Flow
Ford-Fulkerson
method
William Fiset
Max flow
Q: With an infinite input source, how much “flow” can we push
through the network given that each edge has a certain capacity?
0/2 0/7
Source 0/7 0
0/4
3 6 Sink
0/3 0/1
0/4 0/1
0/2 0/3
s 1 0/5
4 0/8 7 t
0/4
0/8 0/3
0/1 2 0/6 5 8
0/3
Max flow
Q: With an infinite input source, how much “flow” can we push
through the network given that each edge has a certain capacity?
0/2 0/7
Source 0/7 0
0/4
3 6 Sink
0/3 0/1
0/4 0/1
0/2 0/3
s 1 0/5
4 0/8 7 t
0/4
0/8 0/3
0/1 2 0/6 5 8
0/3
Suppose the edges are roads with cars, pipes with water
or wires with electric current. Flow represents the
volume of water allowed to flow through the pipes, the
number of cars the roads can sustain in traffic and net
electric current. Effectively, it’s the “bottleneck”
value for the amount of flow that can pass through the
network from source to sink under all the constraints.
Max flow
Q: With an infinite input source, how much “flow” can we push
through the network given that each edge has a certain capacity?
2/2 1/7
Source 4/7 0
0/4
3 6 Sink
0/3 1/1
2/4 1/1
2/2 2/3
s 1 1/5
4 1/8 7 t
4/4
2/8 1/3
1/1 2 1/6 5 8
3/3
s t
1 3
0/10
s t
0/10
0/10
1 0/15
3
0/10
s t
0/10
0/10
1 0/15
3
0/10
s t
0/10
0/10
1 0/15
3
0/10
s t
0/10
Bottleneck of 6
0/10
1 0/15
3
In the augmenting path above, the bottleneck
is the “smallest” edge on the path. We can
use the bottleneck value to augment the flow
along the path.
6/10
s t
0/10
6/10
1 6/15
3
6/10
s -6/0
t
0/10
-6/0 -6/0
6/10
1 6/15
3
6/10
s -6/0
t
0/10
-6/0 -6/0
6/10
1 6/15
3
0/0 6/10
s -6/0
t
0/10
-6/0 -6/0
6/10
1 6/15
3 0/0
6/10
s -6/0
t
0/10
-6/0 -6/0
6/10
1 6/15
3
Q: Residual edges have a capacity of 0? Isn’t
that forbidden? How does that work?
-6/0
6/10
s -6/0
t
0/10
-6/0 -6/0
6/10
1 6/15
3
Q: Residual edges have a capacity of 0? Isn’t
that forbidden? How does that work?
6/10
s -6/0
t
0/10
-6/0 -6/0
6/10
1 6/15
3
6/10
s -6/0
t
0/10
-6/0 -6/0
6/10
1 6/15
3
-6/0
6/10
s -6/0
t
0/10
-6/0 -6/0
6/10
1 6/15
3
6/10
s -10/0
t
4/10
-10/0 -6/0
10/10
1 10/15
3 -4/0
6/10
s -10/0
t
4/10
-10/0 -6/0
10/10
1 10/15
3 -4/0
-6/0
6/10
s -10/0
t
4/10
-10/0 -6/0
10/10
1 10/15
3 -4/0
-6/0 6/10
s -10/0
t
10/10
-10/0 0/0
10/10
1 10/15
3 -10/0
-6/0 6/10
s -10/0
t
10/10
-10/0 0/0
10/10
1 10/15
3 -10/0
-6/0
-6/0 6/10
s -10/0
t
10/10
-10/0 0/0
10/10
1 10/15
3 -10/0
-6/0
-6/0 6/10
s -10/0
t
10/10
-10/0 0/0
10/10
1 10/15
3 -10/0
-10/0 10/10
s -10/0
t
10/10
-10/0 0/0
10/10
1 10/15
3 -10/0
-10/0 10/10
s -10/0
t
10/10
-10/0 0/0
10/10
1 10/15
3 -10/0
-10/0
-10/0 10/10
s -10/0
t
10/10
-10/0 0/0
10/10
1 10/15
3 -10/0
maximum flow = 6 + 4 + 6 + 4 = 20
bottleneck values
The time complexity of the Ford-Fulkerson
method depends on the algorithm being used
to find the augmenting paths, which is left
unspecified.
Assuming the method of finding augmenting
paths is by using a Depth First Search (DFS),
the algorithm runs in O(fE), where f is the
maximum flow and E is the number of edges.
Assuming the method of finding augmenting
paths is by using a Depth First Search (DFS),
the algorithm runs in O(fE), where f is the
maximum flow and E is the number of edges.
0/100 0
0/0
0/0
0/100
0/0
s t
0/100
0/0
0/100 0/1
0/0
1
Recall that a DFS traversal chooses edges in
a random order, so it is possible to pick the
middle edge every time when finding an
augmenting path.
1/100 0
0/0
-1/0
0/100
-1/0
s t
1/100
0/0
0/100 1/1
-1/0
1
Recall that a DFS traversal chooses edges in
a random order, so it is possible to pick the
middle edge every time when finding an
augmenting path.
1/100 0
-1/0
0/0
1/100
-1/0
s t
1/100
-1/0
1/100 0/1
-1/0
1
This results in flipping back and forth
between the same two alternating paths
for 200 iterations…
2/100 0
-1/0
-1/0
1/100
-2/0
s t
2/100
-1/0
1/100 1/1
-2/0
1
This results in flipping back and forth
between the same two alternating paths
for 200 iterations…
2/100 0
-2/0
0/0
2/100
-2/0
s t
2/100
-2/0
2/100 0/1
-2/0
1
This results in flipping back and forth
between the same two alternating paths
for 200 iterations…
3/100 0
-2/0
-1/0
2/100
-3/0
s t
3/100
-2/0
2/100 1/1
-3/0
1
Other much faster algorithms and heuristics
exist to find the maximum flow.
0/10
2 0/15
5 0/10
8
Then let’s code Ford-Fulkerson and hope to
achieve the following graph (or similar)
with max-flow of 23:
8/10
2 13/15
5 10/10
8
Source Code Link
Implementation source code can
be found at the following link:
github.com/williamfiset/algorithms
0/10
2 0/15
5 0/10
8
0/10 0/15
0/10 0 3 6
0/3
0/15
0/20 0/10
0/5 0/10
s 1 0/15
4 7 t
0/4
0/10
0/10
0/7
0/10
2 0/15
5 0/10
8
0/10 0/15
0/10 0 3 6
0/3
0/15
0/20 0/10
0/5 0/10
s 1 0/15
4 7 t
0/4
0/10
0/10
0/7
0/10
2 0/15
5 0/10
8
Graph Theory
Video Series
Network Flow:
unweighted bipartite
graph matching
William Fiset
Bipartite Graph
A bipartite graph is one whose vertices can be
split into two independent groups U, V such
that every edge connects between U and V.
Unweighted
Edges
Weighted
Edges
Harder
Reference: Competitive Programming 3 The New Lower
Bound of Programming Contests. P 349
Common matching variations
Easier Harder
Weighted
Edges
Harder
Reference: Competitive Programming 3 The New Lower
Bound of Programming Contests. P 349
Common matching variations
Easier Harder
Harder
Reference: Competitive Programming 3 The New Lower
Bound of Programming Contests. P 349
Common matching variations
Easier Harder
Harder
Reference: Competitive Programming 3 The New Lower
Bound of Programming Contests. P 349
Common matching variations
Easier Harder
Harder
Reference: Competitive Programming 3 The New Lower
Bound of Programming Contests. P 349
Suppose there are 5 people + 5 books and some people
express interest in some books, who gets what book?
Suppose there are 5 people + 5 books and some people
express interest in some books, who gets what book?
Let’s try a greedy matching solution
Let’s try a greedy matching solution
Let’s try a greedy matching solution
Let’s try a greedy matching solution
Let’s try a greedy matching solution
Let’s try a greedy matching solution
This book is
already matched!
Let’s try a greedy matching solution
Let’s try a greedy matching solution
Let’s try a greedy matching solution
Let’s try a greedy matching solution
Let’s try a greedy matching solution
Let’s try a greedy matching solution
Let’s try a greedy matching solution
This book is
already matched!
Let’s try a greedy matching solution
Let’s try a greedy matching solution
This book is
already matched!
Let’s try a greedy matching solution
Let’s try a greedy matching solution
The greedy approach found 4 matchings,
but can we do better?
Yes! We can turn this matching problem into a
network flow problem by adding a sink s and a source
t and making each edge have unit capacities.
First make the edges directed and add unit capacities
to each edge. The 0/1 besides each edge means 0 flow
and a maximum capacity of 1.
0/1
0/1
0/1
0/1
0/1
0/1
0/1
0/1
0/1
0/1
0/1
0/1
0/1
Then attach the source s and the sink t nodes
allowing for one unit of capacity.
0/1 0/1
0/1
0/1
0/1 0/1
0/1
0/1
0/1
0/1
s 0/1 0/1 0/1 t
0/1
0/1 0/1
0/1
0/1
0/1
0/1
0/1
0/1
0/1
Once the flow graph is set up, use any max-flow
algorithm to push flow through the network.
0/1 0/1
0/1
0/1
0/1 0/1
0/1
0/1
0/1
0/1
s 0/1 0/1 0/1 t
0/1
0/1 0/1
0/1
0/1
0/1
0/1
0/1
0/1
0/1
Pushing flow through the network to achieve the maximum
flow changes the edge capacities. We can find which
edges were taken by checking if the flow is 1
1/1 1/1
1/1
0/1
1/1 0/1
1/1
0/1
1/1
0/1
s 1/1 0/1 1/1 t
1/1
0/1 1/1
1/1
1/1
1/1
0/1
1/1
0/1
1/1
1/1 1/1
1/1
0/1
1/1 0/1
1/1
0/1
1/1
0/1
s 1/1 0/1 1/1 t
1/1
0/1 1/1
1/1
1/1
1/1
0/1
1/1
0/1
1/1
0/1 0/1
0/1
0/1
0/1 0/1
0/1
0/1
0/1
0/1
s 0/1 0/1 0/1 t
0/1
0/1 0/1
0/1
0/1
0/1
0/1
0/1
0/1
0/1
0/1 0/1
0/1
0/1
0/1 0/1
0/1
0/1
0/1
0/1
s 0/1 0/1 0/1 t
0/1
0/1 0/1
0/1
0/1
0/1
0/1
0/1
0/1
0/1
0/1
0/1
0/1
0/1
0/2 0/1
0/1
0/1
0/1
0/1
s 0/1 0/1 t
0/3
0/1
0/1 0/1
0/1
0/1
0/1 0/1
0/2 0/1
0/1
1/1
1/1
1/1
0/1
2/2 0/1
1/1
0/1
1/1
1/1
s 0/1 1/1 t
2/3
0/1
0/1 1/1
1/1
1/1
0/1 0/1
0/2 1/1
0/1
0/1
0/1
0/1
0/1
0/2 0/1
0/1
0/1
0/1
0/1
s 0/1 0/1 t
0/3
0/1
0/1 0/1
0/1
0/1
0/1 0/1
0/2 0/1
0/1
0/1
0/1
0/1
0/1
0/2 0/1
0/1
0/1
0/1
0/1
s 0/1 0/1
t
0/3
0/1
0/1 0/1
0/1
0/1
0/1 0/1
0/2 0/1
0/1
0/1
0/1
0/1
0/1
0/2 0/1
0/5
0/1
0/1
0/1
s 0/1 0/2
t
0/3
0/1
0/1 0/1
0/3
0/1
0/1 0/1
0/2 0/1
0/1
1/1
1/1
1/1
0/1
2/2 0/1
1/5
0/1
1/1
1/1
s 1/1 2/2
t
3/3
0/1
0/1 1/1
2/3
1/1
0/1 0/1
1/2 1/1
1/1
1/1
0/1
2/2 0/1
1/5
0/1
1/1
1/1
s 1/1 2/2
t
3/3
0/1
0/1 1/1
2/3
1/1
0/1 0/1
1/2 1/1
1/1
1/1
0/1
2/2 0/1
1/5
0/1
1/1
1/1
s 1/1 2/2
t
3/3
0/1
0/1 1/1
2/3
1/1
0/1 0/1
1/2 1/1
1/1
1/1
0/1
2/2 0/1
1/5
0/1
1/1
1/1
s 1/1 2/2
t
3/3
0/1
0/1 1/1
2/3
1/1
0/1 0/1
1/2 1/1
1/1
1/1
0/1
2/2 0/1
1/5
0/1
1/1
1/1
s 1/1 2/2
t
3/3
0/1
0/1 1/1
2/3
1/1
0/1 0/1
1/2 1/1
1/1
1/1
0/1
2/2 0/1
1/5
0/1
1/1
1/1
s 1/1 2/2
t
3/3
0/1
0/1 1/1
2/3
1/1
0/1 0/1
1/2 1/1
1/1
1/1
1/1
1/1
0/1
2/2 0/1
1/5
0/1
1/1
1/1
s 1/1 2/2
t
3/3
0/1
0/1 1/1
2/3
1/1
0/1 0/1
1/2 1/1
1/1
0/1
0/1
0/1
0/1
0/2 0/1
0/5
0/1
0/1
0/1
s 0/1 0/2
t
0/3
0/1
0/1 0/1
0/3
0/1
0/1 0/1
0/2 0/1
0/1
0/1
0/1
0/1
0/1
0/2 0/1
0/5
0/1
0/1
0/1
s 0/1 0/2
t
0/3
0/1
0/1 0/1
0/3
0/1
0/1 0/1
0/2 0/1
0/1
1 1
2
3
2
1
2
Assume every mouse is capable of running a
radius of r before being caught by the owl.
What is the maximum number of mice that can
hide safely?
1 1
2
3
2
1
2
The first step is to figure out which holes
each mouse can reach.
1 1
2
3
2
1
2
1 1
2
3
2
1
2
Which mice should go in which holes to
maximize the overall safety of the group?
1 1
2
3
2
1
2
Which mice should go in which holes to
maximize the overall safety of the group?
1 1
2
3
2
1
2
The key realization to make is that this
graph is bipartite.
1 1
2
3
2
1
2
Flow Graph Setup
Mice Holes
0
… …
M-3
M-2
M-1
Mice Holes
0
1 M
2 M+1
… …
M-3 M+H-2
M-2 M+H-1
M-1
Mice Holes
0
1 0/1 M
0/1
0/1
2 0/1 M+1
0/1
… …
0/1
M-3 M+H-2
0/1
0/1
M-2 M+H-1
0/1
M-1
Mice Holes
0
0/1
1 0/1 M
0/1 0/1
0/1
2 0/1 M+1
0/1 0/1
s … …
0/1
0/1 M-3 M+H-2
0/1
0/1
0/1 M-2 M+H-1
0/1
0/1 M-1
Mice Holes
0
0/1
1 0/1 M
0/1 0/1
0/1
2 0/1 M+1
0/1 0/1 0/h1
s … … 0/h2
t
0/h3
0/1
M-3 0/h4
0/1 M+H-2
0/1
0/1
0/1 M-2 M+H-1
0/1
0/1 M-1
Mice Holes
0
0/1
1 0/1 M
0/1 0/1
0/1
2 0/1 M+1
0/1 0/1 0/h1
s … … 0/h2
t
0/h3
0/1
M-3 0/h4
0/1 M+H-2
0/1
0/1
0/1 M-2 M+H-1
0/1
0/1 M-1
1 5 =
3 3 =
4 5 =
-1 -6 =
Elementary Math
1 * 5 = 5
3 + 3 = 6
4 - 5 = -1
-1 * -6 = 6
Elementary Math
1 * 5 = 5
3 + 3 = 6
Answers must
be unique!
4 - 5 = -1
-1 * -6 = 6
Elementary Math
1 - 5 = -4
3 - 3 = 0
4 + 5 = 9
-1 * -6 = 6
Elementary Math
1 - 5 = -4
3 - 3 = 0
4 + 5 = 9
-1 * -6 = 6
Elementary Math
1 2 =
2 1 =
1 2 =
2 1 =
1 2 =
Elementary Math
1 2 =
2 1 =
No solution
exists! 1 2 =
2 1 =
1 2 =
Elementary Math
This problem presents itself as a network flow
problem even though it might not be obvious at
first. Take a moment and attempt to set up a
flow graph that can solve this problem.
Elementary Math
This problem presents itself as a network flow
problem even though it might not be obvious at
first. Take a moment and attempt to set up a
flow graph that can solve this problem.
2 + 3 = 5
2 3 = 2 - 3 = -1
2 * 3 = 6
This makes it easy to formulate the flow graph
as a bipartite graph with input pairs on one
side and solutions on the other.
Elementary Math
Let’s see an example of how to set up
the flow graph for the following pairs:
1 5 =
3 3 =
-1 -6 =
2 2 =
(a,b) pairs Answers
-4
1,5
6
5
1 - 5 = -4
1 + 5 = 6
1 * 5 = 5
5
3,3
3 - 3 = 0
9
3 + 3 = 6
3 * 3 = 9
0
5
3,3
-1 - -6 = 5
9
-1 + -6 = -7
-1 * -6 = 6
-1,-6 0
-7
5
3,3
2 - 2 = 0
9
2 + 2 = 4
2 * 2 = 4
-1,-6 0
-7
2,2
4
5
3,3
9
-1,-6 0
-7
2,2
4
5
3,3
9
s t
-1,-6 0
-7
2,2
4
5 0/1
3,3 0/1
0/1
9
s 0/1 t
0/1
-1,-6 0 0/1
0/1
-7
2,2
4
1,5
0/1 6
0/1
0/1
0/1
5 0/1
3,3 0/1 0/1
0/1
9
s
0/1
0/1 t
0/1 0/1
-1,-6 0 0/1
0/1 0/1
0/1
-7
2,2
0/1 4
-4
0/1
1,5
0/1
0/1 6
0/1
0/1
0/1
5 0/1
0/1
3,3 0/1 0/1
0/1
9
s
0/1
0/1 t
0/1 0/1
0/1 -1,-6 0 0/1
0/1 0/1
0/1
0/1
-7
2,2
0/1 4
-4
0/1
1,5
0/1
0/1 6
0/1
0/1
0/1
5 0/1
0/1
3,3 0/1 0/1
0/1
9
s
0/1
0/1 t
0/1 0/1
0/1 -1,-6 0 0/1
0/1 0/1
0/1
0/1
-7
2,2
0/1 4
-4
0/1
1,5
1/1
1/1 6
0/1
0/1
0/1
5 0/1
1/1
3,3 0/1 1/1
0/1
9
s
0/1
0/1 t
1/1 1/1
1/1 -1,-6 0 1/1
0/1 1/1
1/1
1/1
-7
2,2
1/1 4
1,5
1/1
1/1 6
0/1
0/1
0/1
5 0/1
1/1
3,3 0/1 1/1
0/1
9
s
0/1
0/1 t
1/1 1/1
1/1 -1,-6 0 1/1
0/1 1/1
1/1
1/1
-7
2,2
1/1 4
-4
0/1
1,5
1/1
1/1 6
0/1
0/1
0/1
5 0/1
1/1
3,3 0/1 1/1
0/1
9
s
0/1
0/1 t
1/1 1/1
1/1 -1,-6 0 1/1
0/1 1/1
1/1
1/1
-7
2,2
1/1 4
-1,-6 0 -1 -6 =
-7
2 2 =
2,2
4
-4
1,5
6 1 5 =
5
3,3 3 3 =
9
-1,-6 0 -1 -6 =
-7
2 2 =
2,2
4
1 + 5 equals 6?
1 - 5 equals 6?
1 * 5 equals 6?
-4
1,5
6 1 + 5 = 6
5
3,3 3 3 =
9
-1,-6 0 -1 -6 =
-7
2 2 =
2,2
4
1 + 5 equals 6?
1 - 5 equals 6?
1 * 5 equals 6?
-4
1,5
6 1 + 5 = 6
5
3,3 3 3 =
9
-1,-6 0 -1 -6 =
-7
2 2 =
2,2
4
3 + 3 equals 0?
3 - 3 equals 0?
3 * 3 equals 0?
-4
1,5
6 1 + 5 = 6
5
3,3 3 - 3 = 0
9
-1,-6 0 -1 -6 =
-7
2 2 =
2,2
4
3 + 3 equals 0?
3 - 3 equals 0?
3 * 3 equals 0?
-4
1,5
6 1 + 5 = 6
5
3,3 3 - 3 = 0
9
-1,-6 0 -1 -6 =
-7
2 2 =
2,2
4
-1 + -6 equals -7?
-1 - -6 equals -7?
-1 * -6 equals -7?
-4
1,5
6 1 + 5 = 6
5
3,3 3 - 3 = 0
9
-1,-6 0 -1 + -6 = -7
-7
2 2 =
2,2
4
-1 + -6 equals -7?
-1 - -6 equals -7?
-1 * -6 equals -7?
-4
1,5
6 1 + 5 = 6
5
3,3 3 - 3 = 0
9
-1,-6 0 -1 + -6 = -7
-7
2 2 =
2,2
4
2 + 2 equals 4?
2 - 2 equals 4?
2 * 2 equals 4?
-4
1,5
6 1 + 5 = 6
5
3,3 3 - 3 = 0
9
-1,-6 0 -1 + -6 = -7
-7
2 2 =
2,2
4
2 + 2 equals 4?
2 - 2 equals 4?
2 * 2 equals 4?
You can pick either operator if
multiple of them work.
-4
1,5
6 1 + 5 = 6
5
3,3 3 - 3 = 0
9
-1,-6 0 -1 + -6 = -7
-7
2 * 2 = 4
2,2
4
2 + 2 equals 4?
2 - 2 equals 4?
2 * 2 equals 4?
-4
1,5
6 1 + 5 = 6
5
3,3 3 - 3 = 0
9
-1,-6 0 -1 + -6 = -7
-7
2 * 2 = 4
2,2
4
1 + 5 = 6
3 - 3 = 0
-1 + -6 = -7
2 * 2 = 4
Graph Theory
Video Series
Network Flow:
Edmonds-Karp
Algorithm
William Fiset
Ford-Fulkerson overview
0/2 0/7
Source 0/7 0
0/4
3 6 Sink
0/3 0/1
0/4 0/1
0/2 0/3
s 1 0/5
4 0/8 7 t
0/4
0/8 0/3
0/1 2 0/6 5 8
0/3
Ford-Fulkerson overview
At a high level, Ford-Fulkerson says that all
we want to do is repeatedly find augmenting
paths from s -> t in the flow graph, augment
the flow and repeat until no more paths exist.
0/2 0/7
Source 0/7 0
0/4
3 6 Sink
0/3 0/1
0/4 0/1
0/2 0/3
s 1 0/5
4 0/8 7 t
0/4
0/8 0/3
0/1 2 0/6 5 8
0/3
Ford-Fulkerson overview
The key takeaway here is that the Ford-Fulkerson
method does not specify how to actually find
augmenting paths. This is where optimizations
come into play.
0/2 0/7
Source 0/7 0
0/4
3 6 Sink
0/3 0/1
0/4 0/1
0/2 0/3
s 1 0/5
4 0/8 7 t
0/4
0/8 0/3
0/1 2 0/6 5 8
0/3
Edmonds-Karp
The Ford-Fulkerson method using a DFS to find
augmenting paths takes O(Ef) where E is the
number of edges and f is the max flow.
ANIMATION HERE
Edmonds-Karp
The Ford-Fulkerson method using a DFS to find
augmenting paths takes O(Ef) where E is the
number of edges and f is the max flow.
0/5
0/0 0/25 0/0
0/0 0/10 0/20 0/30 0/15
S 0/0 0/0 0/0 0/0
T
0/5
0/0
0/0 0/0 0/10
0/0
0/5 0/5 0/5
0/0 0/0
0/10
0/10 0/0
0/10 0/0
0/5
0/0 0/25 0/0
0/0 0/10 0/20 0/30 0/15
S 0/0 0/0 0/0 0/0
T
0/5
0/0
0/0 0/0 0/10
0/0
0/5 0/5 0/5
0/0 0/0
0/10
0/10 0/0
0/10 0/0
0/5
0/0 0/25 0/0
0/0 0/10 0/20 0/30 0/15
S 0/0 0/0 0/0 0/0
T
0/5
0/0
0/0 0/0 0/10
0/0
0/5 0/5 0/5
0/0 0/0
0/10
0/10 0/0
0/10 0/0
0/5
0/0 0/25 0/0
0/0 0/10 0/20 0/30 0/15
S 0/0 0/0 0/0 0/0
T
0/5
0/0
0/0 0/0 0/10
0/0
0/5 0/5 0/5
0/0 0/0
0/10
0/10 0/0
0/10 0/0
0/5
0/0 0/25 0/0
0/0 0/10 0/20 0/30 0/15
S 0/0 0/0 0/0 0/0
T
0/5
0/0
0/0 0/0 0/10
0/0
0/5 0/5 0/5
0/0 0/0
0/10
0/10 0/0
0/10 0/0
0/5
0/0 0/25 0/0
0/0 0/10 0/20 0/30 0/15
S 0/0 0/0 0/0 0/0
T
0/5
0/0
0/0 0/0 0/10
0/0
0/5 0/5 0/5
0/0 0/0
0/10
0/10 0/0
0/10 0/0
0/5
0/0 0/25 0/0
0/0 0/10 0/20 0/30 0/15
S 0/0 0/0 0/0 0/0
T
0/5
0/0
0/0 0/0 0/10
0/0
0/5 0/5 0/5
0/0 0/0
0/10
0/10 0/0
0/10 0/0
0/5
0/0 0/25 0/0
0/0 0/10 0/20 0/30 0/15
S 0/0 0/0 0/0 0/0
T
0/5
0/0
0/0 0/0 0/10
0/0
0/5 0/5 0/5
0/0 0/0
0/10
0/10 0/0
Using a DFS can lead to zigzagging through the flow graph
to find the sink which can cause longer augmenting paths.
Longer paths are generally undesirable because the longer
the path, the higher the chance for a small bottleneck
value which results in a longer runtime.
0/10 0/0
0/5
0/0 0/25 0/0
0/0 0/10 0/20 0/30 0/15
S 0/0 0/0 0/0 0/0
T
0/5
0/0
0/0 0/0 0/10
0/0
0/5 0/5 0/5
0/0 0/0
0/10
0/10 0/0
Using the shortest path (in terms of the number of
edges) as an augmenting path is a great approach to
avoid the DFS worse case and reduce the length of
augmenting paths.
0/10 0/0
0/5
0/0 0/25 0/0
0/0 0/10 0/20 0/30 0/15
S 0/0 0/0 0/0 0/0
T
0/5
0/0
0/0 0/0 0/10
0/0
0/5 0/5 0/5
0/0 0/0
0/10
0/10 0/0
Do a Breadth First Search (BFS) starting at the source
and ending at the sink. While exploring the flow graph,
remember that we can only reach a node if the capacity
of the edge to get there is greater than 0.
0/10 0/0
0/5
0/0 0/25 0/0
0/0 0/10 0/20 0/30 0/15
S 0/0 0/0 0/0 0/0
T
0/5
0/0
0/0 0/0 0/10
0/0
0/5 0/5 0/5
0/0 0/0
0/10
0/10 0/0
Add all reachable neighbours to the queue and proceed…
0/10 0/0
0/5
0/0 0/25 0/0
0/0 0/10 0/20 0/30 0/15
S 0/0 0/0 0/0 0/0
T
0/5
0/0
0/0 0/0 0/10
0/0
0/5 0/5 0/5
0/0 0/0
0/10
0/10 0/0
Add all reachable neighbours to the queue and proceed…
0/10 0/0
0/5
0/0 0/25 0/0
0/0 0/10 0/20 0/30 0/15
S 0/0 0/0 0/0 0/0
T
0/5
0/0
0/0 0/0 0/10
0/0
0/5 0/5 0/5
0/0 0/0
0/10
0/10 0/0
Add all reachable neighbours to the queue and proceed…
0/10 0/0
0/5
0/0 0/25 0/0
0/0 0/10 0/20 0/30 0/15
S 0/0 0/0 0/0 0/0
T
0/5
0/0
0/0 0/0 0/10
0/0
0/5 0/5 0/5
0/0 0/0
0/10
0/10 0/0
Add all reachable neighbours to the queue and proceed…
0/10 0/0
0/5
0/0 0/25 0/0
0/0 0/10 0/20 0/30 0/15
S 0/0 0/0 0/0 0/0
T
0/5
0/0
0/0 0/0 0/10
0/0
0/5 0/5 0/5
0/0 0/0
0/10
0/10 0/0
When a complete path from s -> t is found, get
the augmenting path.
0/10 0/0
0/5
0/0 0/25 0/0
0/0 0/10 0/20 0/30 0/15
S 0/0 0/0 0/0 0/0
T
0/5
0/0
0/0 0/0 0/10
0/0
0/5 0/5 0/5
0/0 0/0
0/10
0/10 0/0
Find the bottleneck value:
min(5-0, 10-0, 10-0, 10-0) = 5
0/10 0/0
0/5
0/0 0/25 0/0
0/0 0/10 0/20 0/30 0/15
S 0/0 0/0 0/0 0/0
T
0/5
0/0
0/0 0/0 0/10
0/0
0/5 0/5 0/5
0/0 0/0
0/10
0/10 0/0
Update (augment) the flow of the forward and residual
edges along the path by the bottleneck value (5).
0/10 0/0
0/5
0/0 0/25 0/0
0/0 0/10 0/20 0/30 0/15
S 0/0 0/0 0/0 0/0
T
0/5
0/0
-5/0 0/0 5/10
-5/0
5/5 0/5 0/5
-5/0 0/0
5/10
5/10 -5/0
Do a Breadth First Search (BFS) starting at the source
and ending at the sink. While exploring the flow graph
remember that we can only reach a node if the capacity
of the edge to get there is greater than 0
0/10 0/0
0/5
0/0 0/25 0/0
0/0 0/10 0/20 0/30 0/15
S 0/0 0/0 0/0 0/0
T
0/5
0/0
-5/0 0/0 5/10
-5/0
5/5 0/5 0/5
-5/0 0/0
5/10
5/10 -5/0
Add all reachable neighbours to the queue and proceed…
The bottom left node here is unreachable because the
edge from the source leading to it has a capacity of 0.
0/10 0/0
0/5
0/0 0/25 0/0
0/0 0/10 0/20 0/30 0/15
S 0/0 0/0 0/0 0/0
T
0/5
0/0
-5/0 0/0 5/10
-5/0
5/5 0/5 0/5
-5/0 0/0
5/10
5/10 -5/0
Add all reachable neighbours to the queue and proceed…
0/10 0/0
0/5
0/0 0/25 0/0
0/0 0/10 0/20 0/30 0/15
S 0/0 0/0 0/0 0/0
T
0/5
0/0
-5/0 0/0 5/10
-5/0
5/5 0/5 0/5
-5/0 0/0
5/10
5/10 -5/0
Add all reachable neighbours to the queue and proceed…
0/10 0/0
0/5
0/0 0/25 0/0
0/0 0/10 0/20 0/30 0/15
S 0/0 0/0 0/0 0/0
T
0/5
0/0
-5/0 0/0 5/10
-5/0
5/5 0/5 0/5
-5/0 0/0
5/10
5/10 -5/0
Add all reachable neighbours to the queue and proceed…
0/10 0/0
0/5
0/0 0/25 0/0
0/0 0/10 0/20 0/30 0/15
S 0/0 0/0 0/0 0/0
T
0/5
0/0
-5/0 0/0 5/10
-5/0
5/5 0/5 0/5
-5/0 0/0
5/10
5/10 -5/0
Reconstruct the augmenting path
0/10 0/0
0/5
0/0 0/25 0/0
0/0 0/10 0/20 0/30 0/15
S 0/0 0/0 0/0 0/0
T
0/5
0/0
-5/0 0/0 5/10
-5/0
5/5 0/5 0/5
-5/0 0/0
5/10
5/10 -5/0
Find the bottleneck value:
min(10-0, 20-0, 30-0, 15-0) = 10
0/10 0/0
0/5
0/0 0/25 0/0
0/0 0/10 0/20 0/30 0/15
S 0/0 0/0 0/0 0/0
T
0/5
0/0
-5/0 0/0 5/10
-5/0
5/5 0/5 0/5
-5/0 0/0
5/10
5/10 -5/0
Update (augment) the flow of the forward and residual
edges along the path by the bottleneck value (10).
0/10 0/0
0/5
0/0 0/25 0/0
0/0 10/10 10/20 10/30 10/15
S -10/0 -10/0 -10/0 -10/0
T
0/5
0/0
-5/0 0/0 5/10
-5/0
5/5 0/5 0/5
-5/0 0/0
5/10
5/10 -5/0
0/10 0/0
0/5
0/0 0/25 0/0
0/0 10/10 10/20 10/30 10/15
S -10/0 -10/0 -10/0 -10/0
T
0/5
0/0
-5/0 0/0 5/10
-5/0
5/5 0/5 0/5
-5/0 0/0
5/10
5/10 -5/0
Do a Breadth First Search (BFS) starting at the source
and ending at the sink. While exploring the flow graph
remember that we can only reach a node if the capacity
of the edge to get there is greater than 0
0/10 0/0
0/5
0/0 0/25 0/0
0/0 10/10 10/20 10/30 10/15
S -10/0 -10/0 -10/0 -10/0
T
0/5
0/0
-5/0 0/0 5/10
-5/0
5/5 0/5 0/5
-5/0 0/0
5/10
5/10 -5/0
Add all reachable neighbours to the queue and proceed…
0/10 0/0
0/5
0/0 0/25 0/0
0/0 10/10 10/20 10/30 10/15
S -10/0 -10/0 -10/0 -10/0
T
0/5
0/0
-5/0 0/0 5/10
-5/0
5/5 0/5 0/5
-5/0 0/0
5/10
5/10 -5/0
Add all reachable neighbours to the queue and proceed…
0/10 0/0
0/5
0/0 0/25 0/0
0/0 10/10 10/20 10/30 10/15
S -10/0 -10/0 -10/0 -10/0
T
0/5
0/0
-5/0 0/0 5/10
-5/0
5/5 0/5 0/5
-5/0 0/0
5/10
5/10 -5/0
Add all reachable neighbours to the queue and proceed…
0/10 0/0
0/5
0/0 0/25 0/0
0/0 10/10 10/20 10/30 10/15
S -10/0 -10/0 -10/0 -10/0
T
0/5
0/0
-5/0 0/0 5/10
-5/0
5/5 0/5 0/5
-5/0 0/0
5/10
5/10 -5/0
Add all reachable neighbours to the queue and proceed…
0/10 0/0
0/5
0/0 0/25 0/0
0/0 10/10 10/20 10/30 10/15
S -10/0 -10/0 -10/0 -10/0
T
0/5
0/0
-5/0 0/0 5/10
-5/0
5/5 0/5 0/5
-5/0 0/0
5/10
5/10 -5/0
Reconstruct the augmenting path
0/10 0/0
0/5
0/0 0/25 0/0
0/0 10/10 10/20 10/30 10/15
S -10/0 -10/0 -10/0 -10/0
T
0/5
0/0
-5/0 0/0 5/10
-5/0
5/5 0/5 0/5
-5/0 0/0
5/10
5/10 -5/0
Find the bottleneck value:
min(5-0, 10-0, 10-0, 5-0) = 5
0/10 0/0
0/5
0/0 0/25 0/0
0/0 10/10 10/20 10/30 10/15
S -10/0 -10/0 -10/0 -10/0
T
0/5
0/0
-5/0 0/0 5/10
-5/0
5/5 0/5 0/5
-5/0 0/0
5/10
5/10 -5/0
Update (augment) the flow of the forward and residual
edges along the path by the bottleneck value (5).
5/10 -5/0
5/5
0/0 0/25 0/0
-5/0 10/10 10/20 10/30 10/15
S -10/0 -10/0 -10/0 -10/0
T
0/5
0/0
-5/0 0/0 5/10
-5/0
5/5 0/5 0/5
-5/0 0/0
5/10
5/10 -5/0
There are no more augmenting paths left to be found
because all the edges leading outwards from the source
have a remaining capacity of 0. However, more generally
we know to stop when there are no more augmenting paths
from s -> t
5/10 -5/0
5/5
0/0 0/25 0/0
-5/0 10/10 10/20 10/30 10/15
S -10/0 -10/0 -10/0 -10/0
T
0/5
0/0
-5/0 0/0 5/10
-5/0
5/5 0/5 0/5
-5/0 0/0
5/10
5/10 -5/0
The max flow from running Edmonds-Karp is the sum
of the bottleneck values:
Max flow = 5 + 10 + 5 = 20
5/10 -5/0
5/5
0/0 0/25 0/0
-5/0 10/10 10/20 10/30 10/15
S -10/0 -10/0 -10/0 -10/0
T
0/5
0/0
-5/0 0/0 5/10
-5/0
5/5 0/5 0/5
-5/0 0/0
5/10
5/10 -5/0
You can also get the max flow by summing the
capacity values going into the sink.
Max flow = 5 + 10 + 5 = 20
5/10 -5/0
5/5
0/0 0/25 0/0
-5/0 10/10 10/20 10/30 10/15
S -10/0 -10/0 -10/0 -10/0
T
0/5
0/0
-5/0 0/0 5/10
-5/0
5/5 0/5 0/5
-5/0 0/0
5/10
5/10 -5/0
Summary
A depth first search will sometimes find long
windy paths from the source to the sink.
0/10 0/0
0/5
0/0 0/25 0/0
0/0 0/10 0/20 0/30 0/15
S 0/0 0/0 0/0 0/0
T
0/5
0/0
0/0 0/0 0/10
0/0
0/5 0/5 0/5
0/0 0/0
0/10
0/10 0/0
Summary
This is usually undesirable because the longer
the path, the smaller the bottleneck value,
which results in a longer runtime.
0/10 0/0
0/5
0/0 0/25 0/0
0/0 0/10 0/20 0/30 0/15
S 0/0 0/0 0/0 0/0
T
0/5
0/0
0/0 0/0 0/10
0/0
0/5 0/5 0/5
0/0 0/0
0/10
0/10 0/0
Summary
Edmonds-Karp tries to resolve this problem
by finding the shortest path from the source
to the sink using a breadth first search.
0/10 0/0
0/5
0/0 0/25 0/0
0/0 0/10 0/20 0/30 0/15
S 0/0 0/0 0/0 0/0
T
0/5
0/0
-5/0 0/0 5/10
-5/0
5/5 0/5 0/5
-5/0 0/0
5/10
5/10 -5/0
Summary
More importantly, the big achievement of
Edmonds-Karp is that its time complexity of
O(VE²) is independent of the max flow.
0/10 0/0
0/5
0/0 0/25 0/0
0/0 0/10 0/20 0/30 0/15
S 0/0 0/0 0/0 0/0
T
0/5
0/0
-5/0 0/0 5/10
-5/0
5/5 0/5 0/5
-5/0 0/0
5/10
5/10 -5/0
Next Video: Edmonds-Karp Source Code
Graph Theory
Video Series
Network Flow:
Edmonds-Karp
source code
William Fiset
Previous video explaining
Edmonds-Karp
Source Code Link
Implementation source code can
be found at the following link:
github.com/williamfiset/algorithms
William Fiset
Let’s quickly revisit finding the maximum
flow using a DFS.
0/5
0/6 0 2
0/0
0/12
0/0
0/1 0/0
0/1
0/0
0/14
1 3
0/10
0/5
0/6 0 2
0/0
0/12
0/0
0/1 0/0
0/1
0/0
0/14
1 3
0/10
0/5
0/6 0 2
0/0
0/12
0/0
0/1 0/0
0/1
0/0
0/14
1 3
0/10
0/5
0/6 0 2
0/0
0/12
0/0
0/1 0/0
0/1
0/0
0/14
1 3
0/10
0/5
0/6 0 2
0/0
0/12
0/0
0/1 0/0
0/1
0/0
0/14
1 3
0/10
0/5
0/6 0 2
0/0
0/12
0/0
0/1 0/0
0/1
0/0
0/14
1 3
0/10
Find the bottleneck (edge with smallest
remaining capacity) along the augmenting path.
min(6-0,1-0,7-0,11-0) = 1
0/5
0/6 0 2
0/0
0/12
0/0
0/1 0/0
0/1
0/0
0/14
1 3
0/10
0/5
1/6 0 2
0/0
0/12
-1/0
1/1 0/0
0/1
0/0
0/14
1 3
0/10
1/6 0 2
0/0
0/12
-1/0
1/1 0/0
0/1
0/0
0/14
1 3
0/10
0/5
1/6 0 2
0/0
0/12
-1/0
1/1 0/0
0/1
0/0
0/14
1 3
0/10
0/5
1/6 0 2
0/0
0/12
-1/0
1/1 0/0
0/1
0/0
0/14
1 3
0/10
0/5
1/6 0 2
0/0
0/12
-1/0
1/1 0/0
0/1
0/0
0/14
1 3
0/10
0/5
1/6 0 2
0/0
0/12
-1/0
1/1 0/0
0/1
0/0
0/14
1 3
0/10
0/5
1/6 0 2
0/0
0/12
-1/0
1/1 0/0
0/1
0/0
0/14
1 3
0/10
Recall that the remaining capacity of an edge is
calculated as: capacity - flow. This allows residual edges
with negative flow to have a positive remaining capacity.
min(14-0,0-(-1),5-0,11-1) = 1
0/5
1/6 0 2
0/0
0/12
-1/0
1/1 0/0
0/1
0/0
0/14
1 3
0/10
1/5
1/6 0 2
-1/0
0/12
-1/0
0/1 0/0
0/1
0/0
1/14
1 3
0/10
1/6 0 2
-1/0
0/12
-1/0
0/1 0/0
0/1
0/0
1/14
1 3
0/10
You could imagine a DFS algorithm repeatedly taking an
edge with a capacity of 1, ultimately limiting how
much flow we can push through the network with each
iteration.
1/5
2/6 0 2
-1/0
0/12
-2/0
1/1 0/0
0/1
0/0
1/14
1 3
0/10
You could imagine a DFS algorithm repeatedly taking an
edge with a capacity of 1, ultimately limiting how
much flow we can push through the network with each
iteration.
2/5
2/6 0 2
-2/0
0/12
-2/0
0/1 0/0
0/1
0/0
2/14
1 3
0/10
You could imagine a DFS algorithm repeatedly taking an
edge with a capacity of 1, ultimately limiting how
much flow we can push through the network with each
iteration.
2/5
3/6 0 2
-2/0
0/12
-3/0
1/1 0/0
0/1
0/0
2/14
1 3
0/10
You could imagine a DFS algorithm repeatedly taking an
edge with a capacity of 1, ultimately limiting how
much flow we can push through the network with each
iteration.
3/5
3/6 0 2
-3/0
0/12
-3/0
0/1 0/0
0/1
0/0
3/14
1 3
0/10
Capacity scaling is the idea that we should prioritize
taking edges with larger capacities to avoid ending up
with a path that has a small bottleneck.
0/5
0/6 0 2
0/0
0/12
0/0
0/1 0/0
0/1
0/0
0/14
1 3
0/10
If we adjust the size of each edge based on its
capacity value, then we can more easily visualize
which edges we should give more attention to.
0/5
0/6 0 2
0/0
0/0
S T
0/0
0/12
0/0
0/1 0/0
0/1
0/0
0/14 1 3
0/10
Capacity Scaling Algorithm
0/5
0/6 0 2
0/0
0/0
S T
0/0
0/12
0/0
0/1 0/0
0/1
0/0
0/14 1 3
0/10
First, compute U as the maximum of all initial capacities.
U = max(6,14,1,5,7,10,1,11,12) = 14
0/5
0/6 0 2
0/0
0/0
S T
0/0
0/12
0/0
0/1 0/0
0/1
0/0
0/14 1 3
0/10
Next, compute the starting value for Δ which is the
smallest power of 2 less than or equal to U (=14).
Δ = 2³ = 8, since the next power of 2 is 2⁴ = 16 > U
0/5
0/6 0 2
0/0
0/0
S T
0/0
0/12
0/0
0/1 0/0
0/1
0/0
0/14 1 3
0/10
Let’s find an augmenting path which has all edges with a
remaining capacity greater than or equal to Δ = 8.
0/5
0/6 0 2
0/0
0/0
S T
0/0
0/12
0/0
0/1 0/0
0/1
0/0
0/14 1 3
0/10
Let’s find an augmenting path which has all edges with a
remaining capacity greater than or equal to Δ = 8.
0/5
0/6 0 2
0/0
0/0
S T
0/0
0/12
0/0
0/1 0/0
0/1
0/0
0/14 1 3
0/10
Let’s find an augmenting path which has all edges with a
remaining capacity greater than or equal to Δ = 8.
0/5
0/6 0 2
0/0
0/0
S T
0/0
0/12
0/0
0/1 0/0
0/1
0/0
0/14 1 3
0/10
Let’s find an augmenting path which has all edges with a
remaining capacity greater than or equal to Δ = 8.
0/5
0/6 0 2
0/0
0/0
S T
0/0
0/12
0/0
0/1 0/0
0/1
0/0
0/14 1 3
0/10
Let’s find an augmenting path which has all edges with a
remaining capacity greater than or equal to Δ = 8.
0/5
0/6 0 2
0/0
0/0
S T
0/0
0/12
0/0
0/1 0/0
0/1
0/0
0/14 1 3
0/10
Let’s find an augmenting path which has all edges with a
remaining capacity greater than or equal to Δ = 8.
0/5
0/6 0 2
0/0
0/0
S T
0/0
0/12
0/0
0/1 0/0
0/1
0/0
0/14 1 3
0/10
0/5
0/6 0 2
0/0
0/0
S T
-10/0
10/12
0/0
0/1 -10/0
0/1
-10/0
10/14
1 3
10/10
0/5
0/6 0 2
0/0
0/0
S T
-10/0
10/12
0/0
0/1 -10/0
0/1
-10/0
10/14
1 3
10/10
There are no more augmenting paths from s -> t which have
a remaining capacity greater than or equal to Δ (=8), so
the new Δ is: Δ = Δ / 2 = 4.
0/5
0/6 0 2
0/0
0/0
S T
-10/0
10/12
0/0
0/1 -10/0
0/1
-10/0
10/14
1 3
10/10
Let’s find an augmenting path which has all edges with a
remaining capacity greater than or equal to Δ = 4.
0/5
0/6 0 2
0/0
0/0
S T
-10/0
10/12
0/0
0/1 -10/0
0/1
-10/0
10/14
1 3
10/10
Let’s find an augmenting path which has all edges with a
remaining capacity greater than or equal to Δ = 4.
0/5
0/6 0 2
0/0
0/0
S T
-10/0
10/12
0/0
0/1 -10/0
0/1
-10/0
10/14
1 3
10/10
Let’s find an augmenting path which has all edges with a
remaining capacity greater than or equal to Δ = 4.
0/5
0/6 0 2
0/0
0/0
S T
-10/0
10/12
0/0
0/1 -10/0
0/1
-10/0
10/14
1 3
10/10
Let’s find an augmenting path which has all edges with a
remaining capacity greater than or equal to Δ = 4.
0/5
0/6 0 2
0/0
0/0
S T
-10/0
10/12
0/0
0/1 -10/0
0/1
-10/0
10/14
1 3
10/10
Let’s find an augmenting path which has all edges with a
remaining capacity greater than or equal to Δ = 4.
0/5
0/6 0 2
0/0
0/0
S T
-10/0
10/12
0/0
0/1 -10/0
0/1
-10/0
10/14
1 3
10/10
Let’s find an augmenting path which has all edges with a
remaining capacity greater than or equal to Δ = 4.
0/5
0/6 0 2
0/0
0/0
S T
-10/0
10/12
0/0
0/1 -10/0
0/1
-10/0
10/14
1 3
10/10
Let’s find an augmenting path which has all edges with a
remaining capacity greater than or equal to Δ = 4.
5/5
5/6 0 2
-5/0
-5/0
S T
-10/0
10/12
0/0
0/1 -10/0
0/1
-10/0
10/14
1 3
10/10
Let’s find an augmenting path which has all edges with a
remaining capacity greater than or equal to Δ = 4.
5/5
5/6 0 2
-5/0
-5/0
S T
-10/0
10/12
0/0
0/1 -10/0
0/1
-10/0
10/14
1 3
10/10
Let’s find an augmenting path which has all edges with a
remaining capacity greater than or equal to Δ = 4.
5/5
5/6 0 2
-5/0
-5/0
S T
-10/0
10/12
0/0
0/1 -10/0
0/1
-10/0
10/14
1 3
10/10
Let’s find an augmenting path which has all edges with a
remaining capacity greater than or equal to Δ = 4.
5/5
5/6 0 2
-5/0
-5/0
S T
-10/0
10/12
0/0
0/1 -10/0
0/1
-10/0
10/14
1 3
10/10
Let’s find an augmenting path which has all edges with a
remaining capacity greater than or equal to Δ = 4.
5/5
5/6 0 2
-5/0
-5/0
S T
-10/0
10/12
0/0
0/1 -10/0
0/1
-10/0
10/14
1 3
10/10
Let’s find an augmenting path which has all edges with a
remaining capacity greater than or equal to Δ = 4.
5/5
5/6 0 2
-5/0
-5/0
S T
-10/0
10/12
0/0
0/1 -10/0
0/1
-10/0
10/14
1 3
10/10
Let’s find an augmenting path which has all edges with a
remaining capacity greater than or equal to Δ = 4.
5/5
5/6 0 2
-5/0
-5/0
S T
-14/0
10/12
-4/0
0/1 -10/0
0/1
-10/0
14/14
1 3
10/10
5/5
5/6 0 2
-5/0
-5/0
S T
-14/0
10/12
-4/0
0/1 -10/0
0/1
-10/0
14/14
1 3
10/10
There are no more augmenting paths from s -> t which have
a remaining capacity greater than or equal to Δ (=4), so
the new Δ is: Δ = Δ / 2 = 2.
However, there are also no more augmenting paths from
s -> t which have a remaining capacity greater than or
equal to Δ (=2), so the new Δ is: Δ = Δ / 2 = 1.
5/5
5/6 0 2
-5/0
-5/0
S T
-14/0
10/12
-4/0
0/1 -10/0
0/1
-10/0
14/14
1 3
10/10
Let’s find an augmenting path which has all edges with a
remaining capacity greater than or equal to Δ = 1.
5/5
5/6 0 2
-5/0
-5/0
S T
-14/0
10/12
-4/0
0/1 -10/0
0/1
-10/0
14/14
1 3
10/10
Let’s find an augmenting path which has all edges with a
remaining capacity greater than or equal to Δ = 1.
5/5
5/6 0 2
-5/0
-5/0
S T
-14/0
10/12
-4/0
0/1 -10/0
0/1
-10/0
14/14
1 3
10/10
Let’s find an augmenting path which has all edges with a
remaining capacity greater than or equal to Δ = 1.
5/5
5/6 0 2
-5/0
-5/0
S T
-14/0
10/12
-4/0
0/1 -10/0
0/1
-10/0
14/14
1 3
10/10
Let’s find an augmenting path which has all edges with a
remaining capacity greater than or equal to Δ = 1.
5/5
5/6 0 2
-5/0
-5/0
S T
-14/0
10/12
-4/0
0/1 -10/0
0/1
-10/0
14/14
1 3
10/10
Let’s find an augmenting path which has all edges with a
remaining capacity greater than or equal to Δ = 1.
5/5
5/6 0 2
-5/0
-5/0
S T
-14/0
10/12
-4/0
0/1 -10/0
0/1
-10/0
14/14
1 3
10/10
Let’s find an augmenting path which has all edges with a
remaining capacity greater than or equal to Δ = 1.
The bottleneck value is 1, because min(6-5, 1-0, 7-4,
11-9) = 1 is the smallest remaining capacity along the
path.
5/5
5/6 0 2
-5/0
-5/0
S T
-14/0
10/12
-4/0
0/1 -10/0
0/1
-10/0
14/14
1 3
10/10
Let’s find an augmenting path which has all edges with a
remaining capacity greater than or equal to Δ = 1.
The bottleneck value is 1, because min(6-5, 1-0, 7-4,
11-9) = 1 is the smallest remaining capacity along the
path.
5/5
6/6 0 2
-5/0
-6/0
S T
-14/0
10/12
-5/0
1/1 -10/0
0/1
-10/0
14/14
1 3
10/10
There are no more augmenting paths from s -> t
which have a remaining capacity greater than or
equal to Δ (=1), so the new Δ is: Δ = Δ / 2 = 0,
which terminates the algorithm.
5/5
6/6 0 2
-5/0
-6/0
S T
-14/0
10/12
-5/0
1/1 -10/0
0/1
-10/0
14/14
1 3
10/10
The max flow value is the sum of the bottleneck
values found during each iteration:
Max flow = 10 + 5 + 4 + 1 = 20
5/5
6/6 0 2
-5/0
-6/0
S T
-14/0
10/12
-5/0
1/1 -10/0
0/1
-10/0
14/14
1 3
10/10
You can also get the max flow by summing the
flow values going into the sink
Max flow = 10 + 10 = 20
5/5
6/6 0 2
-5/0
-6/0
S T
-14/0
10/12
-5/0
1/1 -10/0
0/1
-10/0
14/14
1 3
10/10
Summary
Ford-Fulkerson implemented with a DFS can
result in having a bottleneck value of 1 every
iteration for a complexity of O(fE).
0/5
1/6 0 2
0/0
0/12
-1/0
1/1 0/0
0/1
0/0
0/14
1 3
0/10
Summary
Capacity scaling is when we push flow through
the larger edges first to achieve a better
runtime.
0/6 0 2
0/0
0/0
S T
0/0
0/12
0/0
0/1 0/0
0/1
0/0
0/14 1 3
0/10
Summary
One approach to capacity scaling is to maintain
a decreasing parameter Δ (i.e by halving) which
acts as a threshold for which edges should be
accepted or rejected based on their remaining
capacity.
0/6 0 2
0/0
0/0
S T
0/0
0/12
0/0
0/1 0/0
0/1
0/0
0/14 1 3
0/10
Next Video: Capacity Scaling
Source Code
Graph Theory
Video Series
Network Flow
Capacity Scaling
source code
William Fiset
Previous video explaining
capacity scaling:
Source Code Link
Implementation source code can
be found at the following link:
github.com/williamfiset/algorithms
William Fiset
Dinic’s Algorithm
Dinic’s is a strongly polynomial maximum flow
algorithm with a runtime of O(V2E).
S T
The levels of the graph are those obtained by
doing a BFS from the source.
L = 0 L = 1 L = 2 L = 3 L = 4
1 2 3
S 1 2 3 T
1 2 3
Furthermore, an edge is only part of the level graph if it
makes progress towards the sink. That is, the edge must go
from a node at level L to another at level L+1.
L = 0 L = 1 L = 2 L = 3 L = 4
1 2 3
S 1 2 3 T
1 2 3
The requirement that edges must go from L to
L+1 prunes backwards and “sideways” edges
(grey edges in this slide).
L = 0 L = 1 L = 2 L = 3 L = 4
1 2 3
S 1 2 3 T
1 2 3
Q: If we're trying to go from s -> t as quickly
as possible, does it make sense to take the red
edge going in the backwards direction?
S T
Take this edge?
A: No, taking the red edge doesn’t bring you closer
to the sink, so it should only be taken if a detour
is required. This is why backwards edges are omitted
in the level graph.
S T
Take this edge?
The same thing can be said about edges
which cut across sideways to the sink as
no progress is made.
S T
It’s also worth mentioning that residual (dotted) edges
can be made part of the level graph, but they must have
a remaining capacity (capacity - flow) > 0.
L = 0 L = 1 L = 2 L = 3 L = 4
1 2 3
S 1 2 3 T
1 2 3
Dinic’s Algorithm Steps
Step 1: Construct a level graph by doing a
BFS from the source to label all the levels
of the current flow graph.
Repeat Steps 1 to 3
Let’s run through an example of using Dinic’s
algorithm to find the maximum flow on the
following flow graph.
0/10 0/0
0 3 6
0/5 0/0 0/10
0/0 0/0
0/15 0/15
0/5
0/0 0/25 0/0
0/0 0/10 0/20 0/30 0/15
S 0/0 1 0/0
4 7 T
0/0 0/20 0/0
0/0
0/0 0/0 0/10
0/0
0/15 0/5 0/15
0/0 0/0
0/10
2 0/25
5 8
0/0
Begin by constructing the level graph for
the current flow graph.
0/10 0/0
0/5
0/0 0/25 0/0
0/0 0/10 0/20 0/30 0/15
S 0/0 0/0 0/0 0/0
T
0/20
0/0
0/0 0/0 0/10
0/0
0/15 0/5 0/15
0/0 0/0
0/10
0/25 0/0
Begin by constructing the level graph for
the current flow graph.
0/10 0/0
1
0/5 0/0 0/10
0/0 0/0
0/15 0/15
0/5
0/0 0/25 0/0
0/0 0/10 0/20 0/30 0/15
S 0/0 1 0/0 0/0 0/0
T
0/20
0/0
0/0 0/0 0/10
0/0
0/15 0/5 0/15
0/0 0/0
0/10
1 0/25 0/0
Begin by constructing the level graph for
the current flow graph.
0/10 0/0
1 2
0/5 0/0 0/10
0/0 0/0
0/15 0/15
0/5
0/0 0/25 0/0
0/0 0/10 0/20 0/30 0/15
S 0/0 1 0/0
2 T
0/0 0/20 0/0
0/0
0/0 0/0 0/10
0/0
0/15 0/5 0/15
0/0 0/0
0/10
1 0/25
2
0/0
Begin by constructing the level graph for
the current flow graph.
0/10 0/0
1 2 3
0/5 0/0 0/10
0/0 0/0
0/15 0/15
0/5
0/0 0/25 0/0
0/0 0/10 0/20 0/30 0/15
S 0/0 1 0/0
2 3 T
0/0 0/20 0/0
0/0
0/0 0/0 0/10
0/0
0/15 0/5 0/15
0/0 0/0
0/10
1 0/25
2 3
0/0
Begin by constructing the level graph for
the current flow graph.
0/10 0/0
0
1 3
2 6
3
0/5 0/0 0/10
0/0 0/0
0/15 0/15
0/5
0/0 0/25 0/0
0/0 0/10 0/20 0/30 0/15
S 0/0 1 0/0
4
2 7
3 T
0/0 0/20 0/0
0/0
0/0 0/0 0/10
0/0
0/15 0/5 0/15
0/0 0/0
0/10
2
1 0/25
5
2 8
3
0/0
The level graph consists of all edges which go from
L to L+1 in level and have remaining capacity > 0.
0/10 0/0
0
1 3
2 6
3
0/5 0/0 0/10
0/0 0/0
0/15 0/15
0/5
0/0 0/25 0/0
0/0 0/10 0/20 0/30 0/15
S 0/0 1 0/0
4
2 7
3 T
0/0 0/20 0/0
0/0
0/0 0/0 0/10
0/0
0/15 0/5 0/15
0/0 0/0
0/10
2
1 0/25
5
2 8
3
0/0
Find paths from s -> t until a blocking flow is
reached (i.e. you cannot find any more paths).
0/10 0/0
0
1 3
2 6
3
0/5 0/0 0/10
0/0 0/0
0/15 0/15
0/5
0/0 0/25 0/0
0/0 0/10 0/20 0/30 0/15
S 0/0 1 0/0
4
2 7
3 T
0/0 0/20 0/0
0/0
0/0 0/0 0/10
0/0
0/15 0/5 0/15
0/0 0/0
0/10
2
1 0/25
5
2 8
3
0/0
Find paths from s -> t until a blocking flow is
reached (i.e. you cannot find any more paths).
0/10 0/0
0
1 3
2 6
3
0/5 0/0 0/10
0/0 0/0
0/15 0/15
0/5
0/0 0/25 0/0
0/0 0/10 0/20 0/30 0/15
S 0/0 1 0/0
4
2 7
3 T
0/0 0/20 0/0
0/0
0/0 0/0 0/10
0/0
0/15 0/5 0/15
0/0 0/0
0/10
2
1 0/25
5
2 8
3
0/0
Find paths from s -> t until a blocking flow is
reached (i.e. you cannot find any more paths).
0/10 0/0
1 2 3
0/5 0/0 0/10
0/0 0/0
0/15 0/15
0/5
0/0 0/25 0/0
0/0 0/10 0/20 0/30 0/15
S 0/0 1 0/0
2 3 T
0/0 0/20 0/0
0/0
0/0 0/0 0/10
0/0
0/15 0/5 0/15
0/0 0/0
0/10
1 0/25
2 3
0/0
Find paths from s -> t until a blocking flow is
reached (i.e. you cannot find any more paths).
0/10 0/0
1 2 3
0/5 0/0 0/10
0/0 0/0
0/15 0/15
0/5
0/0 0/25 0/0
0/0 0/10 0/20 0/30 0/15
S 0/0 1 0/0
2 3 T
0/0 0/20 0/0
0/0
0/0 0/0 0/10
0/0
0/15 0/5 0/15
0/0 0/0
0/10
1 0/25
2 3
0/0
The current path has a bottleneck value of 5
since min(5-0,10-0,10-0,5-0) = 5.
0/10 0/0
1 2 3
0/5 0/0 0/10
0/0 0/0
0/15 0/15
0/5
0/0 0/25 0/0
0/0 0/10 0/20 0/30 0/15
S 0/0 1 0/0
2 3 T
0/0 0/20 0/0
0/0
0/0 0/0 0/10
0/0
0/15 0/5 0/15
0/0 0/0
0/10
1 0/25
2 3
0/0
Augment the flow values along the path by 5.
5/10 -5/0
1 2 3
5/5 -5/0 5/10
0/0 -5/0
0/15 0/15
5/5
0/0 0/25 0/0
-5/0 0/10 0/20 0/30 0/15
S 0/0 1 0/0
2 3 T
0/0 0/20 0/0
0/0
0/0 0/0 0/10
0/0
0/15 0/5 0/15
0/0 0/0
0/10
1 0/25
2 3
0/0
The blocking flow has not yet been reached since
there still exists paths from s -> t.
5/10 -5/0
1 2 3
5/5 -5/0 5/10
0/0 -5/0
0/15 0/15
5/5
0/0 0/25 0/0
-5/0 0/10 0/20 0/30 0/15
S 0/0 1 0/0
2 3 T
0/0 0/20 0/0
0/0
0/0 0/0 0/10
0/0
0/15 0/5 0/15
0/0 0/0
0/10
1 0/25
2 3
0/0
5/10 -5/0
1 2 3
5/5 -5/0 5/10
0/0 -5/0
0/15 0/15
5/5
0/0 0/25 0/0
-5/0 0/10 0/20 0/30 0/15
S 0/0 1 0/0
2 3 T
0/0 0/20 0/0
0/0
0/0 0/0 0/10
0/0
0/15 0/5 0/15
0/0 0/0
0/10
1 0/25
2 3
0/0
5/10 -5/0
1 2 3
5/5 -5/0 5/10
0/0 -5/0
0/15 0/15
5/5
0/0 0/25 0/0
-5/0 0/10 0/20 0/30 0/15
S 0/0 1 0/0
2 3 T
0/0 0/20 0/0
0/0
0/0 0/0 0/10
0/0
0/15 0/5 0/15
0/0 0/0
0/10
1 0/25
2 3
0/0
5/10 -5/0
1 2 3
5/5 -5/0 5/10
0/0 -5/0
0/15 0/15
5/5
0/0 0/25 0/0
-5/0 0/10 0/20 0/30 0/15
S 0/0 1 0/0
2 3 T
0/0 0/20 0/0
0/0
0/0 0/0 0/10
0/0
0/15 0/5 0/15
0/0 0/0
0/10
1 0/25
2 3
0/0
The current path has a bottleneck value of 15
since min(15-0,25-0,20-0,15-0) = 15.
5/10 -5/0
1 2 3
5/5 -5/0 5/10
0/0 -5/0
0/15 0/15
5/5
0/0 0/25 0/0
-5/0 0/10 0/20 0/30 0/15
S 0/0 1 0/0
2 3 T
0/0 0/20 0/0
0/0
0/0 0/0 0/10
0/0
0/15 0/5 0/15
0/0 0/0
0/10
1 0/25
2 3
0/0
Augment the flow values along the path by 15.
5/10 -5/0
1 2 3
5/5 -5/0 5/10
0/0 -5/0
0/15 0/15
5/5
0/0 0/25 0/0
-5/0 0/10 15/15
0/20 0/30
S 0/0 1 0/0
2 3 T
0/0 15/20 -15/0
0/0
-15/0 0/0 0/10
5/10
1 2 3
5/5 -5/0 5/10
0/0 -5/0
0/15 0/15
5/5
0/0 0/25 0/0
-5/0 0/10 15/15
0/20 0/30
S 0/0 1 0/0
2 3 T
0/0 15/20 -15/0
0/0
-15/0 0/0 0/10
5/5
0/0 0/25 0/0
-5/0 0/10 15/15
0/20 0/30
S 0/0 1 0/0
2 3 T
0/0 15/20 -15/0
0/0
-15/0 0/0 0/10
5/5
0/0 0/25 0/0
-5/0 0/10 15/15
0/20 0/30
S 0/0 1 0/0
2 3 T
0/0 15/20 -15/0
0/0
-15/0 0/0 0/10
5/5
0/0 0/25 0/0
-5/0 0/10 15/15
0/20 0/30
S 0/0 1 0/0
2 3 T
0/0 15/20 -15/0
0/0
-15/0 0/0 0/10
5/10
1 2 3
5/5 -5/0 5/10
0/0 -5/0
0/15 0/15
5/5
0/0 0/25 0/0
-5/0 0/10 15/15
0/20 0/30
S 0/0 1 0/0
2 3 T
0/0 15/20 -15/0
0/0
-15/0 0/0 0/10
5/5
0/0 0/25 0/0
-5/0 0/10 0/20 0/30 15/15
S 0/0 0/0 0/0 -15/0
T
15/20
0/0
-15/0 0/0 0/10
5/10 -5/0
5/5
0/0 0/25 0/0
-5/0 0/10 0/20 0/30 15/15
S 0/0 0/0 0/0 -15/0
T
15/20
0/0
-15/0 0/0 0/10
5/10 -5/0
0 3 6
5/5 -5/0 5/10
0/0 -5/0
0/15 0/15
5/5
0/0 0/25 0/0
-5/0 0/10 0/20 0/30 15/15
S 0/0 1 0/0
4 7 T
0/0 15/20 -15/0
0/0
-15/0 0/0 0/10
5/10 -5/0
5/5
0/0 0/25 0/0
-5/0 0/10 0/20 0/30 15/15
S 0/0 1 0/0 0/0 -15/0
T
15/20
0/0
-15/0 0/0 0/10
5/10 -5/0
2
5/5 -5/0 5/10
0/0 -5/0
0/15 0/15
5/5
0/0 0/25 0/0
-5/0 0/10 0/20 0/30 15/15
S 0/0 1 0/0
2 T
0/0 15/20 -15/0
0/0
-15/0 0/0 0/10
5/10 -5/0
2 3
5/5 -5/0 5/10
0/0 -5/0
0/15 0/15
5/5
0/0 0/25 0/0
-5/0 0/10 0/20 0/30 15/15
S 0/0 1 0/0
2 3 T
0/0 15/20 -15/0
0/0
-15/0 0/0 0/10
5/10 -5/0
2 3 4
5/5 -5/0 5/10
0/0 -5/0
0/15 0/15
5/5
0/0 0/25 0/0
-5/0 0/10 0/20 0/30 15/15
S 0/0 1 0/0
2 3 T
0/0 15/20 -15/0
0/0
-15/0 0/0 0/10
5/10 -5/0
2 3 4
5/5 -5/0 5/10
0/0 -5/0
0/15 0/15
5/5
0/0 0/25 0/0
-5/0 0/10 0/20 0/30 15/15
S 0/0 1 0/0
2 3 T
0/0 15/20 -15/0
0/0
-15/0 0/0 0/10
5/10 -5/0
2 3 4
5/5 -5/0 5/10
0/0 -5/0
0/15 0/15
5/5
0/0 0/25 0/0
-5/0 0/10 0/20 0/30 15/15
S 0/0 1 0/0
2 3 T
0/0 15/20 -15/0
0/0
-15/0 0/0 0/10
5/10 -5/0
2 3 4
5/5 -5/0 5/10
0/0 -5/0
0/15 0/15
5/5
0/0 0/25 0/0
-5/0 0/10 0/20 0/30 15/15
S 0/0 1 0/0
2 3 T
0/0 15/20 -15/0
0/0
-15/0 0/0 0/10
5/10 -5/0
2 3 4
5/5 -5/0 5/10
0/0 -5/0
0/15 0/15
5/5
0/0 0/25 0/0
-5/0 0/10 0/20 0/30 15/15
S 0/0 1 0/0
2 3 T
0/0 15/20 -15/0
0/0
-15/0 0/0 0/10
5/10 -5/0
2 3 4
5/5 -5/0 5/10
0/0 -5/0
0/15 0/15
5/5
0/0 0/25 0/0
-5/0 0/10 0/20 0/30 15/15
S 0/0 1 0/0
2 3 T
0/0 15/20 -15/0
0/0
-15/0 0/0 0/10
5/10 -5/0
2 3 4
5/5 -5/0 5/10
0/0 -5/0
0/15 0/15
5/5
0/0 0/25 0/0
-5/0 0/10 0/20 0/30 15/15
S 0/0 1 0/0
2 3 T
0/0 15/20 -15/0
0/0
-15/0 0/0 0/10
5/10 -5/0
2 3 4
5/5 -5/0 5/10
0/0 -5/0
0/15 0/15
5/5
0/0 0/25 0/0
-5/0 0/10 0/20 0/30 15/15
S 0/0 1 0/0
2 3 T
0/0 15/20 -15/0
0/0
-15/0 0/0 0/10
5/5
0/0 0/25 0/0
-5/0 0/10 0/20 0/30 15/15
S 0/0 1 0/0
2 3 T
0/0 15/20 -15/0
0/0
-15/0 0/0 0/10
5/5
0/0 0/25 0/0
-5/0 0/10 0/20 0/30 15/15
S 0/0 1 0/0
2 3 T
0/0 15/20 -15/0
0/0
-15/0 0/0 0/10
5/5
0/0 0/25 0/0
-5/0 0/10 0/20 0/30 15/15
S 0/0 1 0/0
2 3 T
0/0 15/20 -15/0
0/0
-15/0 0/0 0/10
5/5
0/0 0/25 0/0
-5/0 0/10 0/20 0/30 15/15
S 0/0 1 0/0
2 3 T
0/0 15/20 -15/0
0/0
-15/0 0/0 0/10
5/5
0/0 0/25 0/0
-5/0 0/10 0/20 0/30 15/15
S 0/0 1 0/0
2 3 T
0/0 15/20 -15/0
0/0
-15/0 0/0 0/10
5/10 -5/0
2 3 4
5/5 -5/0 5/10
0/0 -5/0
0/15 0/15
5/5
0/0 0/25 0/0
-5/0 0/10 0/20 0/30 15/15
S 0/0 1 0/0
2 3 T
0/0 15/20 -15/0
0/0
-15/0 0/0 0/10
5/10 -5/0
2 3 4
5/5 -5/0 5/10
0/0 -5/0
0/15 0/15
5/5
0/0 0/25 0/0
-5/0 10/10 10/20 10/30 15/15
S -10/0 1 2 3 T
-10/0 -10/0 15/20 -15/0
0/0 -10/0
-15/0 10/10
-10/0
15/15 0/5 10/15
-15/0 -15/0
0/10
3 15/25
4 4
0/0
5/10 -5/0
2 3 4
5/5 -5/0 5/10
0/0 -5/0
0/15 0/15
5/5
0/0 0/25 0/0
-5/0 10/10 10/20 10/30 15/15
S -10/0 1 2 3 T
-10/0 -10/0 15/20 -15/0
0/0 -10/0
-15/0 10/10
-10/0
15/15 0/5 10/15
-15/0 -15/0
0/10
3 15/25
4 4
0/0
The blocking flow has been reached and no
more flow can be pushed through the
network, so the algorithm terminates.
5/10 -5/0
2 3 4
5/5 -5/0 5/10
0/0 -5/0
0/15 0/15
5/5
0/0 0/25 0/0
-5/0 10/10 10/20 10/30 15/15
S -10/0 1 2 3 T
-10/0 -10/0 15/20 -15/0
0/0 -10/0
-15/0 10/10
-10/0
15/15 0/5 10/15
-15/0 -15/0
0/10
3 15/25
4 4
0/0
The max flow is the sum of the bottleneck values:
Max flow = 5 + 15 + 10 = 30
5/10 -5/0
2 3 4
5/5 -5/0 5/10
0/0 -5/0
0/15 0/15
5/5
0/0 0/25 0/0
-5/0 10/10 10/20 10/30 15/15
S -10/0 1 2 3 T
-10/0 -10/0 15/20 -15/0
0/0 -10/0
-15/0 10/10
-10/0
15/15 0/5 10/15
-15/0 -15/0
0/10
3 15/25
4 4
0/0
The max flow can also be calculated by looking at
the flow values of the edges leading into the sink.
Max flow = 5 + 15 + 10 = 30
5/10 -5/0
2 3 4
5/5 -5/0 5/10
0/0 -5/0
0/15 0/15
5/5
0/0 0/25 0/0
-5/0 10/10 10/20 10/30 15/15
S -10/0 1 2 3 T
-10/0 -10/0 15/20 -15/0
0/0 -10/0
-15/0 10/10
-10/0
15/15 0/5 10/15
-15/0 -15/0
0/10
3 15/25
4 4
0/0
History & Optimizations
One of the pitfalls of the current
implementation of Dinic’s algorithm at the
moment is that the algorithm may encounter
multiple dead ends during the DFS phase.
2 3 4
S 1 2 3 T
3 4 4
History & Optimizations
Dead ends are highly undesirable because they
extend the length of the journey from the
source to the sink, especially if the same
dead end is taken multiple times during a
blocking flow iteration.
2 3 4
S 1 2 3 T
3 4 4
History & Optimizations
Coffee shop
You
Summary
Dinic’s algorithm uses a BFS to construct
a level graph which directs edges towards
the sink.
0/10 0/0
0/5
0
1 0/0
3
2 6
3
0/10
0/0 0/0
0/15 0/15
0/5
0/25 0/0
0/0
0/0 0/10
0/20 0/30 0/15
S 0/0 1 0/0
4
2 0/0
7
3 0/0
T
0/20
0/0
0/0 0/0 0/10
0/0
0/15 0/5 0/15
0/0
0/0 0/10
2
1 0/25
5
2 8
3
0/0
Summary
In the next phase of the algorithm,
multiple DFSs are run on the level graph
until a blocking flow is reached.
0/10 0/0
0/5
0
1 0/0
3
2 6
3
0/10
0/0 0/0
0/15 0/15
0/5
0/25 0/0
0/0
0/0 0/10
0/20 0/30 0/15
S 0/0 1 0/0
4
2 0/0
7
3 0/0
T
0/20
0/0
0/0 0/0 0/10
0/0
0/15 0/5 0/15
0/0
0/0 0/10
2
1 0/25
5
2 8
3
0/0
Summary
The process of rebuilding the level graph
and finding the blocking flow is repeated
until no more augmenting paths exist and the
max flow is found.
0/10 0/0
0/5
0
1 0/0
3
2 6
3
0/10
0/0 0/0
0/15 0/15
0/5
0/25 0/0
0/0
0/0 0/10
0/20 0/30 0/15
S 0/0 1 0/0
4
2 0/0
7
3 0/0
T
0/20
0/0
0/0 0/0 0/10
0/0
0/15 0/5 0/15
0/0
0/0 0/10
2
1 0/25
5
2 8
3
0/0
Summary
A critical optimization of Dinitz’s
algorithm is pruning dead ends so that you
do not encounter them again.
2 3 4
S 1 2 3 T
3 4 4
Graph Theory
Video Series
Network Flow:
Dinic’s Algorithm
source code
William Fiset
Previous video explaining
Dinic’s algorithm:
Source Code Link
Implementation source code can
be found at the following link:
github.com/williamfiset/algorithms
0/10
0 3 6
0/5 0/10
0/15 0/15
0/5
0/25
0/10 0/15
0/20 0/30
S 1 4 7 T
0/20
0/10
William Fiset
What’s
TODO list
up with negative cycles?
6 0/9
0/4
0/20 1 0/15 t
s
0/14 0/22
8
4
0/60
0/5
0/10 0/11
0/3
9
3
2 0/4
0/11
Would have been a good example but it’s too
large and might confuse some folks
1 4 7 T
88/88 88/88
88/88
88/88
88/88
0 3 0/0
6 9
S 2 5 8
0/10 0/0
0 3 6
0/5 0/0 0/10
0/0 0/0
0/15 0/15
0/5
0/0 0/25 0/0
0/0 0/10 0/20 0/30 0/15
S 0/0 1 0/0
4 7 T
0/0 0/0
0/5
0/0
0/0 0/0 0/10
0/0
0/5 0/5 0/5
0/0 0/0
0/10
2 0/10
5 8
0/0