13 Maxflow
13 Maxflow
Sử dụng một phần tài liệu bài giảng CS161 Stanford University
Khái niệm
• Graphs are directed and edges have “capacities” (weights)
• We have a special “source” vertex s and “sink” vertex t.
• s has only outgoing edges
• t has only incoming edges
3
4
4 2 6
6
2 4
s t
3
2 6
3 4
6
10
Lát cắt
An s-t cut
is a cut which separates s from t
3
4
4 2 6
6
2 4
s t
3
2 6
3 4
6
10
An s-t cut
is a cut which separates s from t
• An edge crosses the cut if it goes from s’s side to t’s side.
• The cost (or capacity) of a cut is the sum of the
capacities of the edges that cross the cut.
This cut has cost
3 4 + 2 + 10 = 16
4
4 2 6
6
2 4
s t
3
2 6
3 4
6
this edge does not
cross the cut; it’s going 10
in the wrong direction.
Lát cắt cực tiểu
A minimum s-t cut is a cut which separates s
from t with minimum cost
• Question: how do we find a minimum s-t cut?
This cut has cost
4 + 3 + 4 = 11
3
4
4 2 6
6
2 4
s t
3
2 6
3 4
6
10
Example where this comes up
• 1955 map of rail
networks from the Soviet
this says
Union to Eastern Europe.
“the bottleneck”
• Declassified in 1999.
• 44 edges, 105 vertices
Schriver 2002
Luồng (Flows)
• In addition to a capacity, each edge has a flow
• (unmarked edges in the picture have flow 0)
• The flow on an edge must be at most its capacity.
• At each vertex, the incoming flows must equal the outgoing
flows.
4 units in, 3
1+1+2 = 4 units out. 2
2 4
4 6
2 1
6 1
4
2 4
s 1 2 t
3
2 1+1 = 2 units in, 6
6 2 units3out. 4 of this as
Think
meaning “send 2
10 units of stuff along
this edge.”
Because of conservation of
Flows flows at vertices,
3
2
2 4
4 6
2 1
6 1
4
2 4
s 1 2 t
3
2 6
3 4
6
The value of 10
this flow is 4.
Luồng cực đại
A maximum flow is a flow of maximum value
• This example flow is pretty wasteful, I’m not utilizing
the capacities very well.
3
2
2 4
4 6
2 1
6 1
4
2 4
s 1 2 t
3
2 6
3 4
6
The value of 10
this flow is 4.
A maximum flow
is a flow of maximum value
• This one is maximum; it has value 11.
3
3
3 4
4 6
4
2 6 1
2 4
s 2 3 4 t
3
5 2 1 6
3 4
6 4
5
10
Định lý
Max-flow min-cut theorem
The value of a max flow from s to t Intuition: in a max flow,
is equal to the min cut better fill up,
and this is the bottleneck.
the cost of a min s-t cut.
3
3
3 4
4 6
4
2 6 1
2 4
s 2 3 4 t
3
5 2 1 6
3 4
6 4
5
10
Proof outline
• Lemma 1: max flow min cut.
• Proof-by-picture
• What we actually want: max flow min cut.
• Proof-by-algorithm…the Ford-Fulkerson algorithm!
• The Ford-Fulkerson algorithm actually finds the max flow and
the min cut.
One half of Min-Cut Max-Flow Theorem
• Lemma 1:
• For ANY s-t flow and ANY s-t cut, the value of the flow is
at most the cost of the cut.
• Hence max flow min cut.
ANY s-t CUT
Proof by picture:
All that stuff has to cross
So x cost of this cut
the cut at some point.
x stuff
comes
out of s
t
s
Ford-Fulkerson
Algorithm
Ford-Fulkerson algorithm
• Outline of algorithm:
• We will be updating a flow
• Start with
• We will maintain a “residual graph”
• A path from s to t in will give us a way to improve
our flow.
• We will continue until there are no s-t paths left in .
2
2 3
3 6 2
4 1 1 5
t
s 8 s 1 t
1 3 2 7 1
Call the flow 1 2
Call the graph Call this graph
claim:
if there is an augmenting path, we can
increase the flow along that path.
• Easy case: every edge on the path in Gf is a forward edge in G
2
2 3
3 6 2
4 1 1 5
t
s 8 s 1 t
1 3 2 7 1
Call the flow 1 2
Call the graph Call this graph
2
2 3
43 6 2
4 21 1 5
t
s 8 s 1 t
1 3 32 7 1
Call the flow 1 2
Call the graph Call this graph
2
2 3
43 6 2
4 21 1 5
t
s 8 s 1 t
1 3 32 7 1
Call the flow 1 2
Call the graph Call this graph
2
2 4
43 6 2
4 21 0 4
t
s 8 s 2 t
1 3 32 7 0
Call the flow 1 3
Call the graph Call this graph
2
2 1
1 6 2
4 1 3 5
t
s 3 s 1 t
1 3 0 2 3
Call the flow 1
Call the graph Call this graph
I changed some of
the weights and
edge directions.
claim:
if there is an augmenting path, we can
increase the flow along that path.
• Harder case: there are backward edges in G in the path.
• Here’s a slightly different example of a flow:
2
2 1
1 6 2
4 1 3 5
t
s 3 s 1 t
1 3 0 2 3
Call the flow 1
Call the graph Call this graph
Now we should NOT increase the flow at
all the edges along the path! I changed some of
• For example, that will mess up the the weights and
conservation of stuff at this vertex. edge directions.
claim:
if there is an augmenting path, we can
increase the flow along that path.
• In this case we do something a bit different:
We will add We will remove flow here,
flow here since our augmenting path
is going backwards along
2 this edge.
2 1
21 6 2
4
01 3 5
t
s 3 s 1 t
1 3 10 2 3
Call the flow 1
Call the graph Call this graph
We will add
flow here
claim:
if there is an augmenting path, we can
increase the flow along that path.
• In this case we do something a bit different:
Then we’ll update the residual graph:
2
2 1
12 6 2
4 10 3 5
t
s 3 s 1 t
1 3 01 2 3
Call the flow 1
Call the graph Call this graph
claim:
if there is an augmenting path, we can
increase the flow along that path.
• In this case we do something a bit different:
Then we’ll update the residual graph:
2
2 2
12 6 2
4 10 2 6
t
s 3 s t
1 3 01 2 2
Call the flow 1 1
Call the graph Call this graph
2 in, 2 out
Before: flow value is 2
2
2 1
1 6 2
4 1 3 5
t
s 3 s 1 t
1 3 0 2 3
Call the flow 1
Call the graph 1 in, 1 out Call this graph
2 flow value is 3
2 2
12 6 2
4 10 2 6
t
s 3 s t
1 3 01 2 2
Call the flow 1 1
Call the graph 1 in, 1 out Call this graph
proof:
• increaseFlow(path P in , flow f ):
• x = min weight on any edge in P
• for (u,v) in P:
• if (u,v) in E, Check that this
always makes a
• if (v,u) in E, bigger (and legit)
• return f’ flow!
Ford-Fulkerson Algorithm
• Ford-Fulkerson(G):
• all zero flow.
•
• while t is reachable from s in
• Find a path P from s to t in // eg, use BFS
• increaseFlow(P,f)
• update
• return f
Example of Ford-Fulkerson
3 0
4
4 0 0
0 6
s 0 2 0 4 0 t
3
0
3 0
6 0 4
0
10
3
4 4
6
3 4
s t
2
3
6 4
10
Example of Ford-Fulkerson
3 0
4
4 0 0
0 6
s 0 2 0 4 0 t
3
0
3 0
6 0 4
0
10
3
4 4
6
3 4
s t
2
3
6 4
10
Example of Ford-Fulkerson
3 0
4
4 4 0
4 6
s 0 2 0 4 4 t
3
0
3 0
6 0 4
0
10
3
4
4 2
4
3 4
s t
2
3
6 4
10
Example of Ford-Fulkerson
3 0
4
4 4 0
4 6
s 0 2 0 4 4 t
3
0
3 0
6 0 4
0
10
3
4
4 2
4
3 4
s t
2
3
6 4
10
Example of Ford-Fulkerson
3 0
4
4 4 0
4 6
s 0 2 0 4 4 t
3
4
3 0
6 4 4
4
10
3
4
4 2
4
3 4
s t
2
4 3 4
2 4
6
Example of Ford-Fulkerson We will remove flow
from this edge.
3 0
4
4 4 0
4 6
s 0 2 0 4 4 t
3
4
3 0
6 4 4
4
10
Notice that we’re
going back along one
3
of the backwards 4
edges we added. 4 2
4
s 3 t
2 4
4 3 4
2 4
6
Example of Ford-Fulkerson We will remove flow
from this edge.
3 2
4
4 2 2
4 6
s 2 2 2 4 4 t
3
4
3 0
6 4 4
4
10
Notice that we’re
going back along one
1
of the backwards
2 2
edges we added. 4 4 2
2 2
s 1 t
4
4 3 2
4
2 4
6
Example of Ford-Fulkerson We will remove flow
from this edge AGAIN.
3 2
4
4 2 2
4 6
s 2 2 2 4 4 t
3
4
3 0
6 4 4
4
10
1
2 2
4 4 2
2
s
2 1 t
4
4 3 2
4
2 4
6
Example of Ford-Fulkerson We will remove flow
from this edge AGAIN.
3 3
4
4 1 3
4 6
s 2 2 3 4 4 t
3
5
3 1
6 4 4
5
10
3 1
4 5 3
1
s
2 t
4
5 2 3
1 4
1 5
5
Example of Ford-Fulkerson
3 3
4
4 1 3
4 6
s 2 2 3 4 4 t
3
5
3 1
6 4 4
5
10
3
1
4 5 3
1
s
2 t
3 4
5 2 4
1
1 5
5
Example of Ford-Fulkerson
3 3
4
4 1 3
4 6
s 2 2 3 4 4 t
3
5
3 1
6 4 4
5
10
3
1
4 5 3
1
s
2 t
3 4
There’s no path 5 2 4
from s to t, and
1
1 5
here’s the cut to
prove it. 5
Example of Ford-Fulkerson
3 3
4
4 1 3
4 6
s 2 2 3 4 4 t
3
5
3 1
6 4 4
5
10
3
1
4 5 3
1
s
2 t
3 4
There’s no path 5 2 4
from s to t, and
1
1 5
here’s the cut to
prove it. 5
Useful corollary
• Using Ford-Fulkerson alg. you can find :
• An s-t cut of cost X
• An s-t flow with value X
• Is it fast?
• Depends on how we pick the augmenting paths!
Why should we be concerned?
Suppose we just picked paths arbitrarily.
Choose a really
a
C big number C.
C
0
0
0
1
t
s
C
0 C 0
a
b
C
C
1 t
s
C C
b
Why should we be concerned?
Suppose we just picked paths arbitrarily.
Choose a really
a
C big number C.
C
0
0
0
1
t
s
C
0 C 0
a
b
C
C
1 t
s
C C
b
Why should we be concerned?
Suppose we just picked paths arbitrarily.
Choose a really
a
C big number C.
C
1
0
1
1
t
s
C
1 C 0
b
a 1
C
C-1
1
The edge (b,a) disappeared s 1 t
from the residual graph!
C
C-1 b
Why should we be concerned?
Suppose we just picked paths arbitrarily.
Choose a really
a
C big number C.
C
1
0
1
1
t
s
C
1 C 0
b
a 1
C
C-1
1
t
s 1
C
C-1 b
Why should we be concerned?
Suppose we just picked paths arbitrarily.
Choose a really
a
C big number C.
C
1
1
0
1
t
s
C
1 C 1
b
a 1
1
C-1 C-1
The edge (b,a) re-appeared 1 t
s 1 1
in the residual graph!
C-1 b
C-1
Why should we be concerned?
Suppose we just picked paths arbitrarily.
Choose a really
a
C big number C.
C
1
1
0
1
t
s
C
1 C 1
b
a 1
1
C-1 C-1
1 t
s 1 1
C-1 b
C-1
Why should we be concerned?
Suppose we just picked paths arbitrarily.
Choose a really
a
C big number C.
C
2
1
1
1
t
s
C
2 C 1
b
a 2
1
C-1 C-2
1
The edge (b,a) disappeared 2 t
s 1
from the residual graph!
C-2 b
C-1
Why should we be concerned?
Suppose we just picked paths arbitrarily.
Choose a really
a
C big number C.
C
2
1 This will go on for C steps,
1
1 adding flow along (b,a) and
t
s
C then subtracting it again.
2 C 1
b
a 2
1
C-1 C-2
1
The edge (b,a) disappeared 2 t
s 1
from the residual graph!
C-2 b
C-1
Edmonds-Karp Algorithm
• If we run the Ford-Fulkerson algorithm, using BFS to
pick augmenting paths, it’s called the Edmonds-Karp
Algorithm.
For more on applications, check out this book chapter from “Algorithms” by Jeff Erickson:
https://jeffe.cs.illinois.edu/teaching/algorithms/book/11-maxflowapps.pdf
Ví dụ:
Maximum matching in bipartite graphs
s t
1
1
1
1
s 1 t
1
1
1
1
1
1
3
3
3
1
1 1 10
5
10 3
10
This person is vegan 2
and not that hungry;
they only want two
2 6
scoops of the sorbet.
Stanford Students Tubs of ice cream
Solution via max flow
1
3
6
4
3 3
3
1
s 1 10
t
1
10
5
3
2
10 6
2
1
1
3 4
4 3 6
4
3 3
3 3
3 3
1
s 1 10
1 1 3 t
1
7 10 1
5
1 3
2 6
2
2 10 6
2 6
1 1
3 4
4 3 6
4
3 3
3 3 3 3
1
s 1 10
1 1 3 t
1
7 10 1
5
1 3
2 6
2
This student can 6
2 10
have flow at most
10 going in, and so 2 6
at most 10 going
out, so at most 10
scoops assigned.
As before, flows correspond to assignments, and
max flows correspond to max assignments.
Tổng kết
• Khái niệm về lát cắt s-t (nguồn - đích) và luồng s-t.
• Định lý Min-Cut Max-Flow: tối thiểu hóa lát cắt đồng
nghĩa với tối đa hóa luồng.
• Thuật toán Ford-Fulkerson:
• Tìm một đường đi “tăng cường”
• Bổ sung lưu lượng của luồng bằng đường đi này
• Lặp lại cho đến khi không còn đường đi nào khác.
• Là cơ sở cho nhiều giải thuật khác
• Ví dụ, bài toán phân công (assignment problems).