Design & Analysis of Algorithms COMP 482 / ELEC 420
John Greiner
Flow networks
What if weights in a graph are maximum capacities of some flow of material?
• Pipe network to transport fluid/gas (e.g., water, oil, natural gas, CO2)– Edges – pipes– Vertices – junctions of pipes
• Data communication network – Edges – network connections of different capacity– Vertices – routers (do not produce or consume data just move it)
• Also used in resource planning, economics, ecosystem network analysis
2
To do:[CLRS] 26#7
Flow Network Definitions
Directed graph G=(V,E)
3
Each vertex is on some path from s to t.
One source and one sink.
Each edge has capacity c(u,v)
0.
13
11
54
15
10
14
19
3
9s
a b
c d
te5
Flow Definitions
How much is currently flowing – f : V V ®
4
Vv
vu,fVu,f
Must satisfy 3 properties:• Capacity constraint: u,v V: f(u,v) c(u,v)• Skew symmetry: u,v V: f(u,v) = –f(v,u)• Flow conservation: u V–{s,t}: f(u,V) = f(V,u) = 0 What goes in must go out.
Total value of flow f:|f| = f(s,V) = f(V,t)
Vv
uv,fuV,f 2/13
3/11
1/51/4
3/15
0/10
2/14
3/19
2/3
0/9s
a b
c d
te1/5
Example flows
Valid or invalid? Why?
5
0/13
0/11
0/50/4
0/15
0/10
0/14
0/19
0/3
0/9s
a b
c d
te0/5
2/13
5/11
0/50/4
2/15
2/10
4/14
1/19
5/3
0/9s
a b
c d
te0/5
1/13
1/11
1/51/4
1/15
1/10
1/14
1/19
1/3
1/9s
a b
c d
te1/5
Maximize the flow
6
13
11
54
15
10
14
19
3
9s
a b
c d
te5
Maximum flow: Algorithm idea• If we have some flow, …
• …and can find an augmenting path can add a constant amount of flow along path: a>0, (u,v)p, f(u,v) + a c(u,v)
• Then just do it, to get a better flow!
7
2/13
3/11
1/51/4
3/15
1/10
1/14
2/19
2/3
0/9s
a b
c d
te1/5
s tp
Ford-Fulkerson method
8
Ford-Fulkerson(G,s,t) 1 initialize flow f to 0 everywhere2 while there is an augmenting path p do3 augment flow f along p4 return f
• How do we find/choose an augmenting path?• How much additional flow can we send through that
path?• Does the algorithm always find the maximum flow?
Augmenting
Augmenting path – any path in the residual network:– Residual network: Gf=(V,Ef)
Ef = {(u,v) V V : cf(u,v) > 0}– Residual capacities: cf(u,v) = c(u,v) – f(u,v)
– Residual capacity of path p:
9 Observe – Edges in Ef are either edges in E or their reversals: |Ef| 2|E|.
Residual ? ?
vu,cminpc fpu,vf
Residual capacity of path (s,c,d,t)?
2/13
3/11
1/51/4
3/15
1/10
1/14
2/19
2/3
0/9s
a b
c d
te1/5
Ford-Fulkerson method, with details
10
Ford-Fulkerson(G,s,t) 1 for each edge (u,v)G.E do 2 f(u,v) = f(v,u) = 0 3 while path p from s to t in residual network Gf do4 cf = min{cf(u,v): (u,v)p} 5 for each edge (u,v) in p do6 f(u,v) = f(u,v) + cf
7 f(v,u) = -f(u,v)8 return f
Algorithms based on this method differ in how they choose p.
Does it always find a maximum flow?
Cut – a partition of V into S,T such that sS, tT
Minimum cut – a cut with minimum capacity
11
TS,vu
vu,fTS,f
TS,vu
vu,cTS,c
|f| = f(S,T)
S T2/13
3/11
1/51/4
3/15
1/10
1/14
2/19
2/3
0/9s
a b
c d
te1/5
Does it always find a maximum flow?
Max-flow min-cut theorem:
The following are equivalent statements:1. f is a maximum flow in G.2. The residual network Gf contains no augmenting paths.3. |f| = c(S,T), for some cut (S,T) of G.
12
We will prove three parts: From this we have 2.®1., which means that the Ford-
Fulkerson method always correctly finds a maximum flow.
1.2.3.
What is the worst-case running time?• Augmentation = ?• How many augmentations?
– Let’s assume integer flows.– Each increases the value of the flow by some integer.– O(|f*|), where f* is the max-flow.
• Total worst-case = O(E|f*|).
– How an augmenting path is chosen is very important! 13
O(E)
1
100
100
100
100
ts
b
a
Edmonds-Karp Algorithm
Use shortest augmenting path (in #edges).
Run algorithm on our example:
14
13
11
54
15
10
14
19
3
9s
a b
c d
te5
Edmonds-Karp algorithm analysis: 1
Augmentation = O(E) – Breadth-first search
Will prove: #augmentations = O(VE).Let d(v) be distance from s to v in residual network. Will prove: Every |E| iterations, d(t) increases by 1.d(t) can increase at most |V| times ® O(VE) iterations
Total = O(VE2)
15
Edmonds-Karp algorithm analysis: 2Will prove: Every |E| iterations, d(t) increases by 1.
Consider the residual network in levels according to d(v):
As long as d(t) doesn't change, the paths found will only use forward edges.• Each iteration saturates & removes at least 1 forward edge, and adds only
backward edges (so no distance ever drops).• After removing |E| - d(t) +1 forward edges, t will be disconnected from s.
So, within |E| iterations, either• t is disconnected, & algorithm terminates, or• A non-forward edge used, & d(t) increased.
16
0 1 2 311
8
43
12
9
13
17
1
9s
a b
c d
te43
13
2
112
2
1
1
2/13
3/11
1/51/4
3/15
1/10
1/14
2/19
2/3
0/9s
a b
c d
te1/5
Antiparallel edges
Allow:
Flows cancel.
Residual (multi)graph can have parallel edges.
Disallow:
17
59 9
5
5
What if we have multiple sources or sinks?
18
t1s1
t2
13
11
5410
s2
s3
117
4
6
8
s
¥
t¥
¥
¥
¥
t1s1
t2
13
11
5410
s2
s3
117
4
6
8
Maximum Flow: Another Algorithm Idea
Greedily fill outgoing edges to capacity. Later edges’ capacity constraints can lead us to reduce those flows.
19
13
11
54
15
10
14
19
3
9s
a b
c d
t
Push-Relabel Example – Goldberg & Tarjan (1986)
20
0/13 0/11
0/5
0/4
14/14
0/10
15/15
0/19
0/3
0/9
s
ta b c d
Relabel a
21
0/13 0/11
0/5
0/4
0/15
0/10
0/14
0/19
0/3
0/9
s
t
a
b c d
Push Preflow from a to b & c
22
13/13 0/11
0/5
2/4
14/14
0/10
15/15
0/19
0/3
0/9
s
t
a
b c d
Relabel b
23
13/13
0/11
0/5
2/4
14/14
0/10
15/15
0/19
0/3
0/9
s
t
a b
c d
Push Preflow from b
24
13/13
0/11
0/5
2/4
14/14
0/10
15/15
13/19
0/3
0/9
s
t
a b
c d
Relabel c
25
13/13
0/11
0/5
2/4
14/14
0/10
15/15
13/19
0/30/9
s
t
a b c
d
Push Preflow from c to d
26
11/11
0/5
14/14
0/10
15/15
13/19
0/30/9
s
t
a b c
d
13/13
2/4
Relabel c
27
13/13 11/110/5
2/4
14/14
0/10
15/15
13/19
0/30/9
s
t
a b
c
d
Push Preflow from c to a
28
11/110/5
14/14
0/10
15/15
13/19
0/30/9
s
t
a b
c
d
13/13
0/4
Relabel a & Push Preflow from a to c
29
11/110/5
14/14
0/10
15/15
13/190/3
0/9
s
t
a
b
c
d
13/13
2/4
Relabel & Push c, Relabel & Push a, Relabel & Push c
30
11/11
0/5
14/14
0/10
15/15
13/190/3
0/9
s
t
a
b
c
d
13/13
0/46
5
1
0
Relabel a & Push Preflow from a to s
31
11/11
0/5
14/14
0/10
13/15
13/190/3
0/9
s
t
a
b
c
d
13/13
0/4
6
7
1
0
Relabel c & Push Preflow from c to s
32
11/11
0/5
11/14
0/10
13/15
13/190/3
0/9
s
t
a
b
c
d
13/13
0/4
6
7
1
0
Relabel d & Push Preflow from d to t
33
11/11
0/5
11/14
0/10
13/15
13/19
3/30/9
s
t
a
b
c
d
13/13
0/4
6
7
1
0
Relabel d & Push Preflow from d to b
34
5/5
11/14
13/15
3/30/9
s
t
a
b
c
d
0/4
6
7
1
0
11/110/1013/13
2
13/19
Push Preflow from b to t
35
5/5
11/14
13/15
3/30/9
s
t
a
b
c
d
0/4
6
7
1
0
11/110/1013/13
2
18/19
Relabel d & Push Preflow from d to c
36
5/511/14
13/15
3/30/9
s
t
a
b
c
d
0/4
6
7
1
0
8/11
0/1013/13
8
18/19
Push Preflow from c to s
37
5/58/14
13/15
3/30/9
s
t
a
b
c
d
0/4
6
7
1
0
8/11
0/1013/13
8
18/19
Correctness Overview
38
5/58/14
13/15
3/30/9s
t
a
b
c
d
0/4
6
7
1
0
8/11
0/1013/13
8
18/19
Is this a flow?Is it maximum? – What is the residual graph?
Correctness Depends on Height Invariant
Invariant: Residual edges go down in height by at most one.
But, height(s) – height(t) = |V|, longer than any s-t path.
39
Running Time Overview
Max height = .
Overall algorithm is .– Requires amortized analysis.– See text & homework.
Choosing operation order wisely, can reduce to .
40
Two Other Approaches
Blocking flows:– Each s-t path in blocking flow contains a saturated edge.– Dinitz/Dinic (1970)
O(VE log V)– Goldberg & Rao (1997)
O(min(V2/3,E1/2) E log(V2/E) log (max(u,v)E c(u,v)))
Combinatorial game:– Cheriyan, Hagerup (1989)
O(VE + V2 log2 V) expected– King, Rao, & Tarjan (1994)
O(VE logE/(V log V) V)
41
An Application of Max-Flow
Maximum bipartite matching problem
Matching in a graph is a subset M of edges such that each vertex has at most one edge of M incident on it. It puts vertices in pairs.
42
E.g.?
A maximumBipartite: V = L+R E L×R
E.g., dating agency
Maximum Bipartite Matching
• How can we reformulate as a max-flow problem?
• What is the running time, using Edmonds-Karp?
43
V = L+RE L×R