maximum flow
DESCRIPTION
Maximum Flow. 26.1 流量網路與流量. Flow network( 流量網路 ) G=(V,E) 是一個有向圖,每一邊 (u,v) ∈ E 均有 Capacity( 容量 ) c(u,v)>0 。如 c(u,v)=0 即代表 (u,v) ∉ E 。. c(s,t)=0 ,因 (s,t) ∉ E. v 1. v 3. s. t. v 2. v 4. 流量網路與流量. 令 s 為 Source vertex , t 為 Sink vertex 。一個 Flow( 流量 ) 係一函數 f : V×V R ,對任兩點 u,v 而言滿足下列性質: - PowerPoint PPT PresentationTRANSCRIPT
Maximum Flow
2
26.1 流量網路與流量• Flow network( 流量網路 ) G=(V,E) 是一個有向圖,每一邊 (u,v)∈E 均有 Capacity( 容量 ) c(u,v)>0 。如 c(u,v)=0 即代表 (u,v)∉E 。
s
v1 v3
v2 v4
t
c(s,t)=0 ,因 (s,t) E∉
3
流量網路與流量• 令 s 為 Source vertex , t 為 Sink vertex 。一個
Flow( 流量 ) 係一函數 f : V×VR ,對任兩點 u,v而言滿足下列性質:– Capacity constraint: f(u,v)≤c(u,v)– Skew symmetric: f(u,v)=-f(v,u)– Flow conservation: 若 u∈V-{s,t} ,則
Σw∈Vf(u,w)=0 。• |f|=Σv∈Vf(s,v) 稱作流量 f 的值。
4
最大流量問題• 給一流量網路 G , Source s 以及 Sink t 。求出具有最大值的流量 f 。
s
v1 v3
v2 v4
t
f(u,v)/c(u,v)
11/16
8/13
0/101/4
12/12
11/14
4/9 7/7
15/20
4/4
5
Residue Network與 Augmenting Path
• 由一 Flow network G 及一 Flow f 所導出的 Residue network Gf 為一個 Flow network ,其 Capacity cf(u,v)=c(u,v)-f(u,v) 。
• 一個 Flow network G 及 Flow f 所導出的Augmenting path 即是 Residue network Gf 上一個st 的路徑 p 。
6
Residue Network與 Augmenting Path
• 如果一 Flow network 找的到 Augmenting path 代表可以找到一個 Flow f ,其值大於 0 。• 如一 Flow network G 及一 Flow f 所導出的 Residue
network Gf ,以找不到任何 Augmenting path ,則f 是最大流量。
7
s
v1 v3
v2 v4
tf(u,v)/c(u,v)
11/16
7/13
0/10 1/4
12/12
10/14
4/9 7/7
15/20
3/4
s
v1 v3
v2 v4
t5
6
11 3
12
5 7
15
Flow network
Residue Network
11
7
4 5
10
4
13
Augmenting Path
8
26.2 Ford-Fulkerson演算法• 主要是利用 Residue network 的觀點來找出
Maxium flow 。• 重複下列動作直到找不到 Augmenting path 為止。
– 找出 Augmenting path p 。– 將 Flow f 沿著 p 增加 min{cf(u,v):(u,v) 在 p上 } ,即 residue network Gf 中路徑 p 上最小的
Capacity 。
9
Ford-Fulkerson(G,s,t){ for each edge (u,v)∈E[G] do f[u,v]0 f[v,u]0while ∃ path p from s to t on Gf do cf(p)min{cf(u,v):(u,v) is in p} for each (u,v) in p
do f[u,v]f[u,v]+cf(p) f[v,u]-f[u,v]
return f}
10
s
v4v2
v3v1
t
16
13
10 4 9 7
14
4
20(a)
12
s
v4v2
v3v1
t
4/16
13
10 4 4/9 7
4/14
4/4
20
4/12
11
s
v4v2
v3v1
t
11/16
13
7/10 4 4/9 7/7
11/14
4/4
7/20
4/12
(b)
s
v4v2
v3v1
t
12
13
10 4 5
8
7
4
204
10
4
4
4
12
(c)
s
v4v2
v3v1
t
11/16
8/13
10 1/4 4/9 7/7
11/14
4/4
15/20
12/12
s
v4v2
v3v1
t
5
13
3 11 5
8
7
4
1311 7
3
4
4
11
13
(d)
s
v4v2
v3v1
t
11/16
12/13
10 1/4 9 7/7
11/14
4/4
19/20
12/12
s
v4v2
v3v1
t
5
13
3 3 5
12
7
4
511 15
3
5
4
11
14
s
v4v2
v3v1
t
5
12
11 3 9
12
7
4
1(e)
11 19
3
1
因無 Augmenting path ,故 Maximum flow 如下所示:s
v4v2
v3v1
t
11/16
12/13
10 1/4 9 7/7
11/14
4/4
19/20
12/12
15
Edmonds-Karp演算法• 使用 Breadth-first search 來找 Augmenting path 。• 主要能夠避免下面這種情形發生:
每次找出的 Augmenting path 是 (s,b,a,t) 跟 (s,a,b,t)交錯出現,如此要執行 2M 個 Iteration 才能做完。
s
a
b
t
M M
M M1
16
s
a
b
t
M M
M M1 s
a
b
t
M-1 M
M M-1
1 11
s
a
b
t
M-1 M-1
M-1 M-1
1 11
1
1s
a
b
t
M M
M M1需要 2M 次
17
Maximum flow and minimum cut
• 對一個流量網路 G=(V,E) 而言,一個 Cut (S,T) 是將 點集合 V 分割為 S 跟 T=V-S 兩部份且滿足s∈S 及 t∈T 。
• Cut (S,T) 的容量 (Capacity) , c(S,T) ,定義為:所有滿足 u∈S 及 v∈T 的邊 (u,v) 之容量和。
18
Cut範例
s
v1 v3
v2 v4
t
f(u,v)/c(u,v)
11/16
8/13
0/101/4
12/12
11/14
4/9 7/7
15/20
4/4
S T
c(S,T)=c(v1,v3)+c(v2,v4)=12+14=26
19
• Lemma 5 Let (S,T) be a cut of a flow network G with flow f.Then the net flow across (S,T) is f(S,T)=|f|
f(S,T)=f(S, V)-f(S,S) =f(S, V) =f(s, V)+f(S-s, V) =f(s, V) = | f |
Proof:
20
• Corollary 6
The value of any flow f in a flow network G isbounded from above by the capacity of any cutof G
u S v T u S v T
Let (S,T) be any cut of G and f be any flow.By the above lemma and capacity constraints |f| = f(S,T) = f(u,v) c(u,v) c(S,T)
Proof:
21
Maximum flow=minimum cut
• 最大流量跟容量最小的 Cut 是相等的。• Thm26.6 以下三敘述等價
– (1) f 是流量網路 G=(V,E) 的最大流量– (2) Residue network Gf 找不到 Augmenting path– (3) 存在一個 Cut (S,T) , |f|=c(S,T) 。
22
f
p
(1) (2) By contradiction, suppose f is a maximum flow in G but that G has an augment path p By cor4, the flow sum f+f is a flow in G with value strictly greater than | f |.
},
f f
f
(2) (3) Suppose G has no augmenting path, that is, that G has no path from s to t Define S={v V: a path from s to v in G and T=V-S The partition (S,T) is a cut: we have s S trivi
.
f
f
ally and t S there is no path from to in G For each pair of vertices u S and v T, we have f(u,v)=c(u,v) otherwise (u, v) E and v is in set S. By lemma5, | f | = f(S,T)=c(
s
)
t
S,T
23
(3) (1) By cor6, | f | c(S,T), for all cuts(S,T). The condition | f |=c(S,T) thus implies that f is a maximum flow
24
• Lemma 26.7
f f
If the Edmonds-karp algorithm is run on a flownetwork G=(V,E) with source s and sink t, thendistance (s,v) in the residual network G increases monotonically with each flow augmentation.
fBy contradiction, suppose that for some vertex v V-{s,t},
there is a flow augmentation that cause (s,v) to decrease.
Let f be the flow just before the f 'augmentation, and let be the flow just afterwa
. . ( , ) ( , )
( , ) ( , )
(
( , ) ( , ),( , ) ( , ).
, )(
( , ))
s v s v
s v s ui e ss u s u u s v
s u s u
f
f ' f '
f
f '
f ' f ' f '
f f '
rd. Then
Assume that for all vertices u V-{s,t} such that u V-{s,t},
Pf:
(*)
25
( , ) ( , ) . ( , ) ( , )s u s v s u s u
f '
f f
f
' f ' f '
Consider a shortest path p' in of the form s u v.Then By(*), we have Consider the net flow f from u to v before the augmentationof flow i G
G
n
1
( , ) ( , ) ( , ) ( , )s v s u s u s v f f f ' f '
(1) If f[u,v] < c(u,v), then we have 1 1
f '
f
f
f
' f
(2) Thus f[u,v]=c(u,v), which means (u,v) EThus augmenting path p chosen in G to produce must have the edge(v,u), since (u,v) and (u,v)E E
G
This is augmenting flow along the path p pushes flowback along (u,v). v appears before u on p
( , ) ( , )( , ) ( , )
( , )( , )
( , )
s u s vs v s u
s us vs v
f f
f f
f '
f '
f '
Thus
= <
1112
26
• Thm 26.8If the Edmonds-Karp algorithm is run on a flow networkG=(V,E) with source s and sink t, then the totalnumber of flow augmentations performed by the algorithm is at most O(VE)
( ) )( ,
()
,p u vu v
f
f f
in a residual network G is on an augmenting path p if After each augmentation, any critical edge disappears from
the residual network. Moreover, at least
critic
one e
alc c
dge
( , ) ( , )s v s u
f f
on any augmenting path must be critical Let (u,v) E. Since augmenting paths are shortest
paths, when (u,v) is critical for the first time, we have 1
Pf:
27
( , )u v will reappear later on another augmenting path only if (v,u) appears on an augmenting path
( , ) ( , ) .
( , ) ( , )( , ) ( , ) ( , ) ( , )
( , ) ( ,
)
| )
(
|
s u s v
s v s vs u s v s v s u
s u V u v
by lemma
f ' f
f
f f
f
'
f '
f ' f '
If is the flow in G when this event occurs, then we have Since
we h 26.
f
a7
ve
'
1
1 1 21 can be critical at most O(V) times.
• There are at most O(E) pairs of vertices that can have an edge between them in a residual graph, the total number of critical edges during the entire execution of the Edmonds-Karpalgorithm is O(VE). • Each augmenting path has at least one critical edge, and
hence the theorem follows.
28
• Each iteration of Ford-Fulkerson can be implemented in O(E) time, when the augmenting path is found by BFS.
• Total running time of the Edmonds-Karp algorithm is O(VE2).
Asymptotically fastest to date for maximum-flow :2/3 1/ 2 2(min( , ) lg( / 2) lg ), deterministic.O V E E V E C C: max capaci ty
29
Minimum cut的應用• 可用於決定經營投資策略。如開發產品 A1 需要先購入工具 T1,T2 ,而產品 A2 需要先購入工具
T2T3 ,則同時開發僅需要負擔 T1,T2,T3 的成本。• 可以將此問題一般化,假定產品 Ai 需要先購入 k個工具 Ti
1Ti2…Ti
k 。而產品 Ai 開發完成可獲利Pi ,購入工具 Tj 需要 Qj 的金錢,則該選擇哪些產品開發?
30
利用Minimum cut
s
T1
T2
Tm-1
Tm
……
……
.
A1
A2
An-1
An
……
……
.t
Q1
Q2
Qm-1
Qm
P1
P2
Pn-1
Pn
如 Ai 需要 Tj ,則自 T1 拉一條容量無限大的邊到Ai 。
31
流量網路的建構方式• 將圖如上頁一般的建構出來,有 source s, sink t ,以及每一個產品與工具。• 對每個工具 Tj 自 s 拉一條容量為 Qj 的邊。• 自每一個產品 Ai 拉一條容量為 Pi 的邊到 t 。• 如 Ai 需要 Tj ,則自 T1 拉一條容量無限大的邊到
Ai 。
32
與最大獲利的對應• 所有產品的利潤總和扣掉該圖的 minimum cut 即是最大獲利。• 觀察:能夠獲利的產品,獲利必然比投入的工具成本高,故將此類的產品與工具劃入 T ,其他的劃入 S 。• 理想的狀態是所有的產品利潤全得,沒有投入的產品部份係扣除產品接到 sink 的容量,而有投入的產品需扣除投入的工具成本,故扣除 source 接到工具的容量。所扣除部分即為 cut 的容量。
33
s
T1
T2
Tm-1
Tm
……
……
.
A1
A2
An-1
An
……
……
.t
Q1
Q2
Qm-1
Qm
P1
P2
Pn-1
Pn
T
S
需投入成本
得不到獲利
34
26.3 Maximum Bipartite Matching
• 一個 Bipartite Graph G=(V=L R,E)∪ ,具有下列性質:– V 可以分割成 L 及 R=V-L 兩個集合。– 所有的邊 (u,v) 的兩個端點 u 及 v 不會同在 L 或同在 R 之中。
35
Bipartite Graph範例
L R
36
Matching
• 對一個圖 G=(V,E) 所謂的配對 (Matching) 是一個不共用點的邊子集合。即:– 對任意兩邊 (u1,v1) , (u2,v2) 而言, u1,v1,u2,v2 四點均相異。
• 最大配對 (Maximum matching) 是指具有最多邊的配對。
37
Matching範例
L R
紅色邊所成的即為一個配對(Matching)
38
Maximum Matching範例
L R
紅色邊所成的即為一個最大配對(Maximum matching)
39
利用最大流量求最大配對• 令 G’=(V {s,t},E {(s,u):u∪ ∪ ∈L} {(v,t):v∪ ∈R}) 。即新增 source s 及 sink t 進入圖 G ,並且在 s 與 L 的所有點之間拉一條邊,而在 R 跟 t 之間拉一條邊。• 如果所有邊的容量均設定為 1 ,則最大流量等於最大配對。
40
流量網路圖
L R
s t
41
最大流量與最大配對
L R
s t