8/12/2019 LN10_MaxFlow.pdf
1/23
1NTUEEAlgori thms
Algorithms
Professor Chien-Mo James Li Graduate Insti tute of Electronics Engineering
National Taiwan University
Graphs (4)
Maximum Flow
2NTUEEAlgori thms
Outline
Elementary Graph Algorithms, CH22 Minimum Spanning Trees, CH23 Single Source Shortest Paths, CH24 Al l-pairs Shortest Paths, CH25 Maximum Flow, CH26*
Flow Networks, 26.1
Ford-Fulkerson Method 26.2
Edmond-Karp Algorithm
Maximum Biparti te Matching 26.3
*CH 26 is different from 2nd edition
8/12/2019 LN10_MaxFlow.pdf
2/23
3NTUEEAlgori thms 3
Flow Networks
Flow Network G = (V,E) is a directed graph Each edge (u,v) has a capacity c(u,v) 0 If (u,v)E, then c(u,v) = 0. If (u,v) E, then reverse edge (v,u) E Two special vertices: source vertex s, sink vertex t , each vertex lies on a path from source to sink
s ~> v ~> t for all vV Imagine: vertices are junctions; edges are conduit of di fferent sizes
capacity is an upper bound on the flow rate = units/time
V ~ junct ionE ~ condui t
C ~ capacity
4NTUEEAlgori thms
Flow* different from 2nd ed.
Flow f: V x V , must satisfy Capacity constraint: For all u,vV, 0 f(u,v) c(u,v) Flow conservation: For all uV - {s, t}
total flow into u = total flow out of u
Value of flow |f| = net flow out of source
Maximum f low problem Given G, s, t , and c, find a flow whose value is maximum
Example: Fig 26.1 max. value of flow= |f| = 19
4342143421
uofoutflowtotaluintoflowtotal
),(),(
=VvVv
vufuvf
=VvVv
svfvsff ),(),(
Flow/capacity
8/12/2019 LN10_MaxFlow.pdf
3/23
5NTUEEAlgori thms
FFT
Is max flow unique?
6NTUEEAlgori thms
Outline
Elementary Graph Algorithms, CH22 Minimum Spanning Trees, CH23 Single Source Shortest Paths, CH24 Al l-pairs Shortest Paths, CH25 Maximum Flow, CH26
Flow Networks, 26.1
Ford-Fulkerson Method 26.2
Edmond-Karp Algorithm
Maximum Biparti te Matching 26.3
8/12/2019 LN10_MaxFlow.pdf
4/23
7NTUEEAlgori thms
Ford-Fulkerson Method
FF method contains three concepts Residual Network
Augment Path
Cut
FORD-FULKERSON-METHOD(G,s,t)1 initialize flowf to 02 while there exists an augmenting pathp in the residual network Gf3 augment flowfalongp
4 returnf
8NTUEEAlgori thms
Residual Capacity
Given a flow fin network G =(V,E). Consider a pair of vertices u,v V Residual capacity = additional flow we can push directly from u to v
sending flow back is equivalent to decreasing the flow
Example: Fig 26.4 cf(v3, v2) = 9-4=5
cf(v2, v3) = 4, why?
No (v1, v3) , why?
.),(
,),(
0
),(
),(),(
),(otherwise
E,uvif
Evuif
uvf
vufvuc
vucf
=
f/c
G Gf
residual
capacity
8/12/2019 LN10_MaxFlow.pdf
5/23
9NTUEEAlgori thms
Residual Network
Residual network Gf = (V, Ef)
Similar to a flow network,
except that it may contain antiparallel edges (u, v) and (v, u) Every edge (u, v) Ef corresponds to
an edge (u, v) E, or an edge (v, u) E, or both therefore
Example: Fig. 26.4 |E| = 9
|Ef|= 15
}0),(:),{( >= vucVVvuE ff
Ef 2E
G Gf
10NTUEEAlgori thms
Augmentation of Flow, ff Given a flow fin G and a flow f in Gf
(ff)= augmentation of fby f equation 26.4
Cancellation : pushing flow on reverse edge in Gf decreases f low in G
otherwise
),(if,
0
),('),('),(),)('( oncancellatiincrease
Evuuvfvufvufvuff
+
= 4342143421
f
f/c
f f(v2, v3)
cancellation
0/9
GfFig. 26.4
8/12/2019 LN10_MaxFlow.pdf
6/23
11NTUEEAlgori thms
Value of |ff| (Lemma 26.1) Given a flow network G and a flow f.
Let f be a flow in Gf .
Then f f is a flow in G with value |ff | = |f|+|f | Example: Fig 26.4
|f| = 19 |f| = 4
|ff| = 19+4 =23
f
f/c
f f0/9
Gf
12NTUEEAlgori thms
Proof of Lemma 26.1(1)
prove ff is a flow so it obeys capacity constraint
why?)),,(),('(because
(26.4))equation(by
.0),('
),(),('),(
),('),('),(
),)('(
vufuvf
vuf
vufvufvuf
uvfvufvufvuff
+
+
=
=
)ofn(definitio
)constraint(capacity
e)nonnegativareflows(because
(26.4))equation(by
),(
),(),(),(
),(),(
),('),(
),('),('),(
),)('(
f
f
c
vuc
vufvucvuf
vucvuf
vufvuf
uvfvufvufvuff
+
+
+
+
=
=
=
8/12/2019 LN10_MaxFlow.pdf
7/23
13NTUEEAlgori thms
Proof of Lemma 26.1(2)
prove ff is a flow so it obeys flow conservation
( )
( )
),)('(
),('),('),(
),('),('),(
),('),('),(
),('),('),(
),)('(
+
+
+
+
=
=
=
=
=
Vv
Vv
VvVvVv
VvVvVv
VvVv
uvff
vufuvfuvf
vufuvfuvf
uvfvufvuf
uvfvufvufvuff
u
14NTUEEAlgori thms
Proof of Lemma 26.1(3)
prove |ff| = |f|+|f|
=
=
21 ),)('(),)('(
),)('(),)('(
'
VvVv
VvVv
svffvsff
svffvsffff
( ) ( )
+=
++
=
+
+=
++=
212121
1 1 22
21
2 22
1 11
1 2
),('),('),(),(
),('),('),('),('
),(),(
),('),('),(
),('),('),(
),('),('),(),('),('),(
VvVv
Vv Vv VvVv
VvVv
Vv VvVv
Vv VvVv
VVvVVv
Vv Vv
svfvsfsvfvsf
svfsvfvsfvsf
svfvsf
vsfsvfsvf
svfvsfvsf
vsfsvfsvfsvfvsfvsf
s V1
V2
8/12/2019 LN10_MaxFlow.pdf
8/23
15NTUEEAlgori thms
Contd
exercise 26.2-1
+=212121
),('),('),(),(||VvVv VVvVVv
svfvsfsvfvsff'f
'
),('),('),(),(
ff
svfvsfsvfvsfVvVvVvVv
+
+
=
=
16NTUEEAlgori thms
G Gf
Augmenting Path
Augmenting path p is a simple path from s ~> t in Gf p admits more flow along each edge
a sequence of pipes through which we can push more flow
How much more flow can we push from s to t along p? residual capacity of p
smallest residual capacity of all edges on p
(u, v) is called critical on augmenting path p if cf(u, v) = cf(p)
Example: Fig 26.4 Augmenting Path p =
cf(p) = 4 (v2, v3) is cri tical
}onis),(:),(min{)( pvuvucpcff
=
(v2, v3) is critical
8/12/2019 LN10_MaxFlow.pdf
9/23
17NTUEEAlgori thms
Lemma 26.2 ,corollary 26.3
(Lemma 26.2)Given G and f, let p be an augmenting path in Gf Then fp is a flow in Gf with value |fp| = cf(p) > 0
(corol lary 26.3) Given G and f, and augmenting path p in Gf ffp is a flow in G with value |ffp| = |f| + |fp| > |f|
=
otherwise
26.8eq.,onis),(if
0
)(),(
pvupcvuf
f
p
fp
f
f fp0/9
18NTUEEAlgori thms
CUT
Cut (S, T) of flow network G =(V,E) is a parti tion of V into S and T T = V-S such that sS and tT
Capacity of cut (S,T) is
Net flow across cut (S, T) is
Minimum cut of G is a cut whose capacity is minimum over all cuts Example: Fig 26.5 cut {s, v1, v2} {v3, v4, t}
capacity of cut = 12+14 = 26; net flow cross cut = 12+11-4=19
what is the minimum cut of G? what is the capacity of cut?
=
Su TvSu Tv uvfvufTSf ),(),(),(
=Su Tv
vucTSc ),(),(
8/12/2019 LN10_MaxFlow.pdf
10/23
19NTUEEAlgori thms
Lemma 26.4* not in exam For any cut (S, T), the net flow across cut f(S, T) = |f|
Proof
0),(),( = VvVv
uvfvuf
=
+
+=
+=
Vv SuVv Su
Vv sSuVv sSu
Vv Vv sSu sSu VvVv
uvfvuf
uvfsvfvufvsf
uvfvufsvfvsff
),(),(
),(),(),(),(
),(),(),(),(
}{}{
}{ }{
44444 344444 214444 34444 21
0126 =
+=
}s{Su VvVv
..eq
VvVv
)u,v(f)v,u(f)s,v(f)v,s(ff
flow conservation, uV {s,t}
20NTUEEAlgori thms
Lemma 26.4 (2)* not in exam Proof (contd) because V = ST, and ST =
split summation over V into summation over S and T
44444 344444 21
0
=
+=
+=
Sv SuSv SuTv SuTv Su
Tv SuSv SuTv SuSv Su
)u,v(f)v,u(f)u,v(f)v,u(f
)u,v(f)u,v(f)v,u(f)v,u(ff
),(
),(),(
TSf
uvfvuffSu TvSu Tv
=
=
8/12/2019 LN10_MaxFlow.pdf
11/23
21NTUEEAlgori thms
Corollary 26.5* not in exam The value of any flow capacity of any cut
Proof
Therefore, maximum flow capacity of minimum cut
),(
),(
),(
),(),(),(
TSc
vuc
vuf
uvfvufTSff
Su Tv
Su Tv
Su TvSu Tv
=
==
22NTUEEAlgori thms
Max-flow Min-Cut Theorem (1)
(Theorem 26.6) The following are equivalent:1. fis a maximum f low
2. Gf has no augmenting path
3. |f|= c(S, T) for some cut (S, T)
Proof: 12 contrapositive assume Gf has an augmenting path, and fis a maximum f low
by corol lary 26.3, ffp is a flow in G with value |f|+|fp| > |f| so f is not a maximum flow, conflict!
Proof: 31 (corollary 26.5) |f| c(S, T) so |f| = c(S, T) means fis a max flow
8/12/2019 LN10_MaxFlow.pdf
12/23
23NTUEEAlgori thms
Max-flow Min-Cut Theorem (2)
Proof 23 Suppose Gf has no augmenting path
Let S = {v V: there exists a path s ~> v in Gf}, T = V-S Must have t T ; otherwise there is an augmenting path
Therefore, (S, T) is a cut
Consider uS and vT If (v,u)E, then cf(u,v)= f(v,u)= 0
otherwise, cf(u,v)=f(v,u)>0 (u,v)Ef v S If (u,v)E, then cf(u,v)=0 f(u,v)= c(u,v)
otherwise, (u,v)Ef vS If both (u,v) (v,u) E, must have f(u,v)= f(v,u)= 0
therefore
),(
0),(
),(),(),(||
TSc
vuc
uvfvufTSff
Tv SuSu Tv
Tv SuSu Tv
=
=
==
S T
us tv
24NTUEEAlgori thms
Basic Ford Fulkerson
Keep augmenting f low along an augmenting path unti l there is no augmenting path
FORD-FULKERSON(G,s,t)1 for each edge (u,v) G.E // initialize2 (u,v).f= 03 while there exists a pathp froms totin the residual network Gf4 cf (p) = min {cf (u,v) : (u,v) is inp}5 for each edge (u,v) inp6 if(u,v) E7 (u,v).f= (u,v).f+ cf (p) // increase8 else (v,u).f= (v,u).fcf (p) // cancellation
8/12/2019 LN10_MaxFlow.pdf
13/23
25NTUEEAlgori thms
Example
Fig 26.6
initially, |f|=0
Gf
=G
GGf
fp=4
fp=?
fp=?
26NTUEEAlgori thms
Example (contd)
Fig 26.6 (contd) Q1: maximum flow =? Q2: can you find the min CUT? Q3: please debug this f igure
no more AP
fp=?
fp=?
8/12/2019 LN10_MaxFlow.pdf
14/23
27NTUEEAlgori thms
Time Complexity
line 3: finding Gf using BFS or DFS O(V+E) = O(E)
E = {(u,v): (u,v)E or (v,u)E} line 3~8: while loop
Assume capacities are integers. Assume max f low is f *
each i teration increase flow by at least 1
needs |f*| iterations
Time complexity = O(E |f*|)
FORD-FULKERSON(G,s,t)1 for each edge (u,v) G.E2 (u,v).f= 03 while there exists a pathp froms totin the residual network Gf4 cf (p) = min {cf (u,v) : (u,v) is inp}5 for each edge (u,v) inp6 if(u,v) E7 (u,v).f= (u,v).f+ cf (p)8 else (v,u).f= (v,u).fcf (p)
O(E)
28NTUEEAlgori thms
Problem
FF running time is NOT polynomial in input size It depends on |f*|, which is not a function of V and E
called pseudo polynomial
worst case example: Fig 26.7 need 2,000,000 times augmentations!
can we do better?
Gf GfG=Gf
8/12/2019 LN10_MaxFlow.pdf
15/23
29NTUEEAlgori thms
Outline
Elementary Graph Algorithms, CH22 Minimum Spanning Trees, CH23 Single Source Shortest Paths, CH24
Al l-pairs Shortest Paths, CH25 Maximum Flow, CH26 Flow Networks, 26.1
Ford-Fulkerson Method 26.2
Edmond-Karp Algor ithm 26.2
Maximum Biparti te Matching 26.3
30NTUEEAlgori thms
Edmonds-Karp Algorithm
Do FORD-FULKERSON, but compute augmenting paths by BFS AP are shortest paths s ~> t in Gf , with unit edge weights
time complexity O(VE2) (Theorem 26.8)
push-relabel algorithm is even better O(V2E) 26.5 26.6 *not in exam
Exercise 1 show Edmonds-Karp is better than Ford-Fulkerson
8/12/2019 LN10_MaxFlow.pdf
16/23
31NTUEEAlgori thms
Exercise 2
Use Edmonds-Karp to find max flow Q1: how many iterations do we need?
Q2: what is the max flow?
13
9
2016
4 7
4
Gf
32NTUEEAlgori thms
Lemma 26.7 (1)* not in exam Let f(u, v)= shortest path distance from u to v in Gf
assume unit edge weights
For all vV- {s, t}, f(u, v) increases monotonically with each flowaugmentation.
Proof by contradiction
Suppose there exists vV- {s, t} such that some flowaugmentation causes shortest path distance s ~>v to decrease
Let f= flow before the first augmentation that causes a shortest-
path distance to decrease. Let f = the flow afterward
Let v be a vertex with minimum f(s, v) whose distance wasdecreased by the augmentation
Let a shortest path s to v in Gf be s~>u v
so (u, v)Ef Because of how we chose v, we know the distance from s to u
does not decrease
(26.12)1),(),( '' = vsus ff
(26.13)),(),(' usus ff
),(),(' vsvs ff
8/12/2019 LN10_MaxFlow.pdf
17/23
33NTUEEAlgori thms
Lemma 26.7 (2)* not in exam Claim (u, v) Ef why? if (u, v) Ef then
contradict assumption
f(s, v) < f(s, v) How can (u, v) Ef but (u, v) Ef ?
The augmentation must have increased flow v to u
Since Edmonds-Karp augments along shortest paths, the
shortest path s to u in Gf has (v, u) as its last edge
contradict assumption f(s, v) < f(s, v) Therefore no v exist such that f(s, v) decreases
(26.12))equation(by
(26.13))inequality(by1
)inequalitytrianglethe24.10,Lemma(by1
)v,s(
)u,s(
)u,s()v,s(
'f
'f
ff
=
+
+
(26.12))equation(by2),(
(26.13))inequality(by1),(inpathshortestonedgelastis),(1),(),(
'
'
=
=
vs
us
Guvusvs
f
f
fff
34NTUEEAlgori thms
Theorem 26.8 (1)* not in exam Edmonds-Karp performs O(VE) augmentations
Proof:
Let p be an augmenting path and (u, v) is cri tical
it d isappears from residual network after augmenting along p
claim: each of the |E| edges can become crit ical |V|/2 times Consider u,vV such that either (u, v)E or (v, u)E or both when (u, v) becomes crit ical first t ime
Af ter augmentation, (u, v) disappears from residual network
(u, v) wont reappear in Gf until flow from u to v decreases, which
happens only i f (v, u) is on an augmenting path in Gf
Let f be the flow when this occurs
1),(),( += usvs ff
1),(),( '' += vsus ff
8/12/2019 LN10_MaxFlow.pdf
18/23
35NTUEEAlgori thms
Theorem 26.8 (2)* not in exam by Lemma 26.7
Each time, f(s, u) increases by at least 2 Initially, f(s, u) = 0, eventually, f(s, u) |V|-2
augmenting path cant have s, and t as intermediate vertices
u can become crit ical less than (|V|-2) /2 =|V|/2 - 1 times
totally, u can become crit ical less than |V|/2 times
Since O(E) pairs of vertices have an edge between them in Gf Each AP has at least 1 critical edge
total O(VE) augmentations
Use BFS to find each AP in O(E) time Edmons-Karp is O(VE2)
2),(
1),(
1),(),( ''
+=
+
+=
us
vs
vsus
f
f
ff
36NTUEEAlgori thms
Outline
Elementary Graph Algorithms, CH22 Minimum Spanning Trees, CH23 Single Source Shortest Paths, CH24 Al l-pairs Shortest Paths, CH25 Maximum Flow, CH26
Flow Networks, 26.1
Ford-Fulkerson Method 26.2
Edmond-Karp Algorithm
Maximum Biparti te Matching 26.3
8/12/2019 LN10_MaxFlow.pdf
19/23
37NTUEEAlgori thms
Bipartite Matching
Undirected G = (V, E) is bipartite if we can partition V = LR such that all edges go between L and R
A matching is a subset of edges ME such that for all vV , at most one edge of M is incident on v cardinality = size of M = |M|
Vertex v is matched if an edge of M is incident on it otherwise unmatched
Example: Fig 26.8
(a) cardinality =2
(b) cardinality =3
38NTUEEAlgori thms
Maximum Bipartite Matching
Maximum bipartite matching: a matching of maximum cardinality M is a maximum matching i f |M| |M| for all matching M
Appl icat ions: machine-task matching L = machines, R = tasks
edge (u, v) means machine u is capable of performing a task v
MBM find maximum number of tasks
Example (b) is MBM
8/12/2019 LN10_MaxFlow.pdf
20/23
39NTUEEAlgori thms
Corresponding Flow Network
Corresponding flow network G=(V, E) V= V {s, t} c(u,v) = 1 for all (u, v) E |E| = |E|+|V|
Each vertex in V has at least one incident edge, |E| |V|/2 |E| = |E| + |V| 3|E|. therefore, |E|=|E|
Idea: a flow in G correspond to a matching in G solve MBM using Ford-Fulkerson method
Example Fig 26.8c max flow = 3
MBM cardinality =3
44 344 21444 3444 2144 344 21
ts
RvtvEvuvuLuusE
toedgesedgesoriginalfromedges
}:),{(}),(:),{(}:),{(' =
40NTUEEAlgori thms
Lemma 26.9
Assume integer-valued flow : f(u,v) is integer are for all edges (u, v) If M is a matching in G, then there is an fin G with value |f| = |M| Conversely, if fis a flow in G, then there is a matching with |M| = |f|
Proof 1: M corresponds to f
if f(u,v) M, then f(s,u) = f(u,v) = f(v,t) = 1 other edges, f(u,v) = 0
(u,v) M corresponds to one unit of flow in G suvt net flow across cut (L{S}, R {T}) = |M| by Lemma 26.4, the value of the flow is |f|=|M|
Proof 2: fcorresponds to M
Let
for each uL, one unit flow enters u if and only i f one vertexvR such that f(u,v) = 1
for every matched vertex uL, we have f(u,v) = 1 net flow across cut (L{S}, R {T}) = |M| by Lemma 26.4, the value of the flow is |M|=|f|
}0),(and,,:),{( >= vufRvLuvuM
8/12/2019 LN10_MaxFlow.pdf
21/23
41NTUEEAlgori thms
Contd * not in exam (Theorem 26.10) If the capacity function c takes on only integral
values, then maximum flow f produced by FF method |f| is integer.
Moreover, for all vertices u and v, f(u,v) is integer
exercise: 26.3-2
(corollary 26.11) The cardinality of maximum matching M in abipartite graph G equals the value of a maximum f low fin its
corresponding flow network G
42NTUEEAlgori thms
Conclusion
How to solve MBM? create corresponding flow network G
run FF method, O(|f*| E) time
obtain the MBM
MBM Time complexity |f*| = O(V)
|E| = (E)
totally, O(VE)
8/12/2019 LN10_MaxFlow.pdf
22/23
43NTUEEAlgori thms
Antiparallel Edges
(v1,v2) (v2, v1) are antiparallel edges violate our assumption
how to model this ?
choose one edge, say (v1, v2) create v
replace (v1, v2) by two new edges (v1, v ) and (v , v2)
Example Fig 26.2
v10
10
44NTUEEAlgori thms
Multiple Sources and Sinks
What if more than one sources and sinks? Add a supersource s, add a supersink t
Capacity from supersource to source are Capacity from sinks to supersink are
Fig 26.3
8/12/2019 LN10_MaxFlow.pdf
23/23
45NTUEEAlgori thms
Reading
CH 26.1-26.3