Transcript Max-Flow

Maximum Flow
Haidong Xue (Haydon)
10/28/2013
What is a flow network?
β€’ A no-selfloop, weak connected directed graph
(V, E)
β€’ Among vertices in V
V
– There is one and only one source vertex 𝑠
– There is one and only one sink vertex 𝑑
– For each vertex v, there is a path 𝑠 β†’ 𝑣 β†’ 𝑑
β€’ No antiparallel edges in E
E
– if 𝑣1 , 𝑣2 ∈ 𝐸, 𝑣2 , 𝑣1 βˆ‰ 𝐸
β€’ A Capacity is defined on each edge: 𝑐 𝑒, 𝑣
What is a flow network?
β€’ A flow on a flow network
– 𝑓 𝑒, 𝑣
β€’ 𝑒, π‘£οƒŽπ‘‰: 0 ≀ 𝑓(𝑒, 𝑣) ο‚£ 𝑐(𝑒, 𝑣)
– i.e. 𝑓 does not exceed 𝑐 (capasity constraint)
β€’ ο€’π‘’οƒŽπ‘‰ ο€­ 𝑠, 𝑑 : 𝑣 οƒŽπ‘‰ 𝑓 𝑒, 𝑣 = 𝑣 οƒŽπ‘‰ 𝑓 𝑣, 𝑒
– i. e. the net flow is 0 (flow conservation)
Note:
In some definition of flow networks, negative flows are allowed;
in these slides, we keep consistent with the textbook
Red: flow
Green: capacity
What is a flow network?
β€’ Are they flow networks with valid flows?
12/12
15/20
11/16
1/4
17/12
20/20
11/16
6/4
4/9
8/13
4/9
7/7
4/4
13/13
11/14
Not connected
Violate the capacity constraint
12/12
12/12
15/20
15/20
11/16
11/16
1/4
4/9
1/4
7/7
4/4
8/13
11/14
Multiple Sinks
4/9
7/7
4/4
10/13
11/14
Violate the flow conservation rule
Red: flow
Green: capacity
What is a flow network?
β€’ Are they flow networks with valid flows?
0/7
12/12
12/12
15/20
11/16
1/4
4/9
0/10
7/7
4/4
8/13
have a self-loop
15/20
11/16
4/9
t
7/7
4/4
8/13
4/9
7/7
4/4
8/13
have an antiparallel edge
12/12
1/4
1/4
11/14
11/14
s
15/20
11/16
11/14
Page 710 Fig 26.1(b)
1 source; 1 sink;
no self-loop; no antiparallel edge;
no flow exceed capacity;
for all non-source non-sink vertices, total flow is 0.
What if the real problem has antiparallel edges
or multiple sinks or sources?
v1
v1
10
10
4
4
V’
10
Add an intermediate vertex to
eliminate antiparallel edges
v2
s1
10
ο‚₯
ο‚₯
sο‚’
ο‚₯
3
s2
s3
12
5
s4
s5
t1
15
6
8
t2
20
14
ο‚₯
ο‚₯
v2
7
11
13
18
t3
ο‚₯
ο‚₯
tο‚’
ο‚₯
2
Add a super-source and a super sink
Why do we
disallow
antiparallel
edges?
What is the max-flow problem?
12/12
15/20
11/16
s
1/4
4/9
t
7/7
4/4
8/13
11/14
The flow of a flow network, |𝒇|:
the net output of the source
i.e., |𝑓 | = 𝑣 οƒŽπ‘‰ 𝑓(𝑠, 𝑣)-𝑣 οƒŽπ‘‰ 𝑓(𝑣, 𝑠)
In the above example, |𝑓 | = 11+8=19
What is the max-flow problem?
Given a flow network, there could be more than one valid
flows:
10/12
15/20
9/16
s
1/4
2/9
|𝑓 | = 17
t
7/7
2/4
8/13
9/14
12/12
15/20
11/16
s
1/4
4/9
|𝑓 | = 19
t
7/7
The maximum-flow problem:
Given a flow network, find a
valid flow that has the
maximum |𝑓 |
4/4
8/13
11/14
12/12
19/20
12/16
s
4
9
t
7/7
4/4
11/13
11/14
|𝑓 | = 23 (it is a max-flow)
Find a maximum flow
β€’ Input: a flow network
12
20
16
s
4
t
7
9
4
13
14
β€’ Output: a flow net work with max-flow
12/12
19/20
12/16
s
4
9
t
7/7
4/4
11/13
11/14
Find a maximum flow- residual networks
β€’ What are the possible modifications can be made
on an edge that does not break the capacity
constraint?
8/12
4/20
4/16
s
4/4
4/9
t
7
4/4
4/13
4/14
Increase it at most by 10 = c-f=14-4;
Decrease it at most by 4=f.
To represent a possible modifications, residual
networks are introduced
Find a maximum flow - residual networks
12/12
𝑣1
11/16
s
1/4
8/13
11
3
8
5
𝑣2
t
𝑣3
12
15
5
5
4
11
3
t
7
𝑣4
A flow network 𝐺 =
(𝑉, 𝐸) with a valid
flow
4/4
𝑣4
11/14
1
15/20
7/7
4/9
𝑣1
5
s
𝑣2
𝑣3
4
A residual network of 𝐺, 𝐺𝑓 = 𝑉, 𝐸𝑓
where
𝐸𝑓 = 𝑒, 𝑣 ∈ 𝑉 × π‘‰: 𝑐𝑓 𝑒, 𝑣 > 0
𝑐𝑓 𝑒, 𝑣
𝑐 𝑒, 𝑣 βˆ’ 𝑓 𝑒, 𝑣 𝑖𝑓(𝑒, 𝑣) ∈ 𝐸
=
𝑓 𝑣, 𝑒
𝑖𝑓 (𝑣, 𝑒) ∈ 𝐸
0
Find a maximum flow – augmenting paths
11/16
12/12
𝑣1
s
1/4
8/13
𝑣2
𝑣3
t
7/7
4/9
11/14
11
s
4/4
𝑣4
𝑣1
5
15/20
3
8
5
𝑣2
𝑣3
12
1
5
5
4
11
3
𝐺𝑓 π‘œπ‘“ 𝐺
𝐺
An augmenting path is a simple path from s to t in the
residual network 𝐺𝑓
𝑣1
5
11
s
3
8
5
𝑣2
𝑣3
12
1
15
5
5
4
11
3
t
7
𝑣4
4
15
t
7
𝑣4
4
Find a maximum flow – augmenting paths
An augmenting path is a simple path from s to t in the
residual network 𝐺𝑓
𝑣1
5
11
s
3
8
4/5
𝑣3
12
1
𝑣2
15
4/5
5
11
t
7
4/4
𝑣4
4
3
The residual capacity of an augmenting path 𝑝:
𝑐𝑓(𝑝) = min{𝑐𝑓(𝑒, 𝑣): (𝑒, 𝑣) 𝑖𝑠 π‘œπ‘› 𝑝}
The flow defined by 𝒑, 𝒇𝒑 :
𝑓𝑝 𝑒, 𝑣 =
𝑐𝑓 𝑝 ,
0,
𝑐𝑓 𝑝 = 4
𝑖𝑓 (𝑒, 𝑣) 𝑖𝑠 π‘œπ‘› 𝑝
π‘œπ‘‘β„Žπ‘’π‘Ÿπ‘€π‘–π‘ π‘’
Guaranteed by
Corollary 26.3 ,
modifying 𝐺
according to this
flow on 𝐺𝑓 does
not break the flow
conservation rule
Find a maximum flow – augmenting paths
11/16
12/12
𝑣1
s
1/4
8/13
𝑣2
𝑣3
4/9
11/14
t
7/7
11
s
4/4
𝑣4
𝑣1
5
15/20
3
1
8
4/5
𝑣2
𝑣3
12
15
4/5
5
11
𝑣4
3
Modify 𝐺 according to 𝑓𝑝
12/12
𝑣1
s
𝑣3
1/4
19/20
t
7/7
0/9
12/13
4
𝐺𝑓 π‘œπ‘“ 𝐺 π‘€π‘–π‘‘β„Ž π‘π‘Žπ‘‘β„Ž 𝑝 π‘Žπ‘›π‘‘ π‘“π‘™π‘œπ‘€ 𝑓𝑝
𝐺 π‘€π‘–π‘‘β„Ž 𝑓
11/16
t
7
4/4
𝑣2
11/14
𝐺 π‘€π‘–π‘‘β„Ž "𝑓 ↑ 𝑓𝑝 "
𝑣4
4/4
Find a maximum flow
β€’ Corollary 26.3 guarantees that:
– 𝑓 ↑ 𝑓𝑝 is a valid flow in 𝐺
– 𝑓 ↑ 𝑓𝑝 > |𝑓|
β€’ Theorem 26.6 (Max-flow min-cut theorem)
further tells that:
– If there is no augmenting path in 𝐺𝑓 , 𝑓 in 𝐺 is a
maximum flow
Do you have any idea to develop an algorithm to find a
max-flow?
Find a maximum flow – the basic FordFulkerson algorithm
FORD-FULKERSON(𝐺, 𝑠, 𝑑)
Initialize |𝑓| to 0
for each 𝑒, 𝑣 οƒŽ 𝐺. 𝐸
(𝑒, 𝑣). 𝑓 = 0;
while there exist path 𝑝 from 𝑠 to 𝑑 in 𝐺𝑓
𝑐𝑓(𝑝) ∢= min{𝑐𝑓(𝑒, 𝑣): (𝑒, 𝑣) 𝑖𝑠 𝑖𝑛 𝑝};
for each (𝑒, 𝑣) in p
if 𝑒, 𝑣 ∈ 𝐺. 𝐸, 𝑒, 𝑣 . 𝑓 = (𝑒, 𝑣). 𝑓+ 𝑐𝑓 𝑝 ;
else 𝑒, 𝑣 . 𝑓 = 𝑒, 𝑣 . 𝑓 βˆ’ 𝑐𝑓 𝑝 ;
Is it correct? What is the time complexity?
Repeatedly find a
𝑓𝑝 , and apply the
modification until
there is no
augmenting path
Find a maximum flow – Analysis of F-F
β€’ Is it correct in general?
– No!
– A counter example:
http://en.wikipedia.org/wiki/Ford%E2%80%93Fulkerson_algorithm#Nonterminating_example
β€’ Is it correct with integer capacities?
–
–
–
–
Yes.
To find a 𝑝, if using DFS or BFS that is O(|V|+|E|)=O(|E|)
At most, need to it |𝑓 βˆ— | times
Time complexity: 𝑂(|𝐸||𝑓 βˆ— |)
Find a maximum flow – Edmonds-Karp
algorithm
EDMONDS-KARP(𝐺, 𝑠, 𝑑)
for each 𝑒, 𝑣 οƒŽ 𝐺. 𝐸
(𝑒, 𝑣). 𝑓 = 0;
while there exist a BFS path 𝑝 from 𝑠 to 𝑑 in 𝐺𝑓
𝑐𝑓(𝑝) ∢= min{𝑐𝑓(𝑒, 𝑣): (𝑒, 𝑣) 𝑖𝑠 𝑖𝑛 𝑝};
for each (𝑒, 𝑣) in p
if 𝑒, 𝑣 ∈ 𝐺. 𝐸, 𝑒, 𝑣 . 𝑓 = (𝑒, 𝑣). 𝑓+ 𝑐𝑓 𝑝 ;
else 𝑒, 𝑣 . 𝑓 = 𝑒, 𝑣 . 𝑓 βˆ’ 𝑐𝑓 𝑝 ;
Find a maximum flow – Analysis of E-K
β€’ Theorem 26.8
– The total number of flow augmentations performed by the
algorithm is O(|V||E|)
β€’ Time complexity
– 𝑂(|𝐸|) βˆ— 𝑂(|𝑉||𝐸|) = 𝑂(|𝑉||𝐸|2 )
Find a maximum flow – Example
𝐺
𝐺𝑓
12
12
20
16
s
4
9
t
7
4
13
20
16
s
4
4
13
14
14
12/12
12
12/20
12/16
s
4
9
8
4
t
7
12
s
12
4
4
13
4
13
14
12
12/12
12/20
12/16
4
9
4/14
8
4
t
7
4/4
4/13
t
7
9
14
s
t
7
9
12
s
12
4
9
4
9
4
10
t
7
4
Find a maximum flow – Example
𝐺
𝐺𝑓
12
12/12
12/20
12/16
s
4
9
12
s
t
7
12
4
4
9
12/12
12
19/20
12/16
4
4
10
4/14
s
t
7
9
4
4/4
4/13
8
4
9
t
7/7
11/14
A max-flow
12
s
19
4
9
11
4/4
11/13
1
4
11
2
t
7
4
3
𝑑 cannot be reached by 𝑠
Maximum bipartite matching
β€’ A problem can be converted to the max-flow
problem
β€’ Bipartite Graph
– A undirected graph (V, E)
– 𝑉 = 𝐿 βˆͺ 𝑅, 𝐿 ∩ 𝑅 = βˆ…
– All edges in E go between 𝐿 and 𝑅
– Every vertex in V has at least one incident edge
Maximum bipartite matching
L
R
L
R
L
R
Maximum bipartite matching
A matching 𝑀: 𝑀 βŠ† 𝐸,
L
R
𝑀 =1
L
R
𝑀 =2
L
R
𝑀 =3
The maximum bipartite matching problem: find a M with the maximum 𝑀
Maximum bipartite matching
Convert it to a max-flow problem
L
R
L
R
s
t
Set each edge with a capacity of 1
By Lemma 26.9, 𝑓 = |𝑀|
Summary
β€’ Max-flow problem
–
–
–
–
–
–
Flow network 𝐺
The flow of 𝐺
The residual network 𝐺𝑓 π‘œπ‘“ 𝐺
A augmenting path 𝑝 of 𝐺𝑓
𝑐𝑝 , 𝑓𝑝
Ford-Fulkerson, Edmonds-Karps
β€’ Maximum bipartite Matching
– Bipartite graph
– Matching
– Convert a bipartite graph to a flow net work
FYI
β€’ Your next assignment has already been posted
β€’ Chaoyang’s will be holding extended office
hours on Wednesdays from 12:00pm-4:00pm
β€’ ACM has an event tomorrow
Room 460, University Center
Distributed Content Delivery Systems with Real Time Route Optimization
Free knowledge, pizza, soda, donut, and a door price
Pop Quiz
Write down your official name clearly on the right-top corner
in the form: Last Name, First Name. (e.g. Xue, Haidong)