### Graph algorithms 4

```Graph Algorithms
Network flow problem
Graph Algorithms
Maximum Flow - Example
12
a
b
16
s
20
10
4
9
t
7
13
4
c
d
14
Graph Algorithms
Maximum Flow - Example
12/12
a
b
11/16
s
15/20
0/10
1/4
4/9
t
7/7
8/13
4/4
c
d
11/14
Graph Algorithms
Maximum Flow - Example
12/12
a
b
11/16
s
19/20
0/10
1/4
0/9
t
7/7
12/13
4/4
c
d
11/14
Graph Algorithms
Maximum Flow - Flow Network
Flow network - directed graph G = (V,E) in which
each edge (u,v)  E has a non-negative capacity
c(u,v)  0.
If (u,v)  E, we assume c(u,v) = 0.
Graph Algorithms
Maximum Flow - Flow
A Flow in G - a real valued function f : VV  R:
• for all u,v  V :
f(u,v)  c(u,v)
• for all u,v  V :
f(u,v) = – f(v,u)
• for all u  V – {s,t} :
vV f(u,v) = 0
Graph Algorithms
Maximum Flow - Flow
The value of flow :
| f | = vV f(s,v)
Problem
Given a flow network G with source s and sink t,
find a flow f with a maximum possible value | f |.
Graph Algorithms
Maximum Flow - Flow Properties
X,Y - sets of vertices
f(X,Y) = xX yY f(x,y)
Graph Algorithms
Maximum Flow - Flow Properties
Lemma
Let G = (V,E) be a flow network and let f be a flow in G.
Then:
• for X  V :
f(X,X) = 0
• for X,Y  V :
f(X,Y) = – f(Y,X)
• for X,Y,Z  V, XY =  :
f(XY,Z) = f(X,Z) + f(Y,Z)
f(Z,XY) = f(Z,X) + f(Z,Y)
Graph Algorithms
Maximum Flow - Ford-Fulkerson Method
procedure FordFulkerson(graph G, source s, sink t)
initialize flow f to 0
while there exists an augmenting path p do
augment flow f along p
return f
Graph Algorithms
Maximum Flow - Residual Networks
G = (V,E) - flow network
s - source, t - sink
f - flow in G
Residual capacity of (u,v)E:
cf(u,v) = c(u,v) – f(u,v)
Graph Algorithms
Maximum Flow - Residual Networks
G = (V,E) - flow network
f - flow in G
Residual network induced by f:
Gf = (V,Ef),
where Ef = {(u,v)  VV: cf(u,v) > 0}
Graph Algorithms
Maximum Flow - Residual Networks - Example
12/12
a
b
11/16
s
15/20
0/10
1/4
4/9
t
7/7
8/13
4/4
c
d
11/14
Graph Algorithms
Maximum Flow - Residual Networks - Example
12
a
b
15
11
5
s
11
5
3
4
7
5
8
d
11
t
4
3
c
5
Graph Algorithms
Maximum Flow - Residual Networks - Example
12
a
b
15
11
5
s
11
5
3
4
7
5
8
d
11
t
4
3
c
5
Graph Algorithms
Maximum Flow - Residual Networks - Example
12/12
a
b
11/16
s
19/20
0/10
1/4
0/9
t
7/7
12/13
4/4
c
d
11/14
Graph Algorithms
Maximum Flow - Residual Networks - Example
12
a
b
19
11
5
s
11
1
3
7
9
12
d
11
t
4
3
c
1
Graph Algorithms
Maximum Flow - Residual Networks
Lemma
Let G = (V,E) be a flow network with source s and sink t.
Let f be a flow in G. Let Gf be a residual network induced
by f and let f ’ be a flow in Gf. then the flow sum f + f ’
defined by
(f + f ’) (u,v) = f(u,v) + f ’(u,v)
is a flow in G with value | f + f ’| = | f | + | f ’ |
Graph Algorithms
Maximum Flow - Augmenting Paths
G = (V,E) - a flow network with source s and sink t.
f - a flow in G.
An augmenting path p is a simple path from s to t in Gf.
Residual capacity of p :
cf(p) = min{cf(u,v) : (u,v) is on p}
Graph Algorithms
Maximum Flow - Augmenting Paths
Lemma
Let G = (V,E) be a flow network, let f be a flow in G, and
let p be an augmenting path in Gf. Define fp : VV  R by
fp (u,v) =
cf(p), if (u,v) is on p
–cf(p), if (v,u) is on p
0,
otherwise
Then fp is a flow in Gf with value | fp | = cf(p) > 0.
Graph Algorithms
Maximum Flow - Augmenting Paths
Corollary
Let G = (V,E) be a flow network, let f be a flow in G, and
let p be an augmenting path in Gf. Let fp be defined as
above. Define f ’ : VV  R by f ’ = f + fp. Then f ’ is a flow
in G with value | f ’ | = | f | + | fp | > | f |.
Graph Algorithms
Maximum Flow - Cuts
A cut (S,T) of a flow network G = (V,E) is a partition of V
into S and T = V – S such that s  S and t  T.
The net flow across the cut (S,T) is defined to be f(S,T).
The capacity of the cut (S,T) is c(S,T).
Graph Algorithms
Maximum Flow - Cuts - Example
f(S,T) = 19
c(S,T) = 26
12/12
a
b
11/16
s
15/20
0/10
1/4
4/9
8/13
4/4
c
d
11/14
S
t
7/7
T
Graph Algorithms
Maximum Flow - Cuts - Lemmas
Lemma
Let f be a flow in flow network G with source s and sink t
and let (S,T) be a cut of G. Then the flow across (S,T)
is f(S,T) = | f |.
Corollary
The value of any flow f in a flow network G is bounded
from above by the capacity of any cut in G.
Graph Algorithms
Maximum Flow - Cuts - Lemmas
Graph Algorithms
Maximum Flow - Cuts - Lemmas
Graph Algorithms
Maximum Flow - Cuts - Lemmas
Graph Algorithms
Maximum Flow - Cuts - Lemmas
Graph Algorithms
Maximum Flow - Cuts - Lemmas
Graph Algorithms
Maximum Flow - Cuts - Lemmas
Graph Algorithms
Maximum Flow - Max-Flow Theorem
Theorem
If f is a flow network G = (V,E) with source s and sink t,
then the following conditions are equivalent:
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) in G
Graph Algorithms
Maximum Flow - Max-Flow Theorem
Graph Algorithms
Maximum Flow - Max-Flow Theorem
Graph Algorithms
Maximum Flow - Ford-Fulkerson Method
procedure FordFulkerson(graph G = (V,E), source s, sink t)
for (u,v)  E do
f[u,v]  0
f[v,u]  0
while there exists a path p from s to t in Gf do
cf(p)  min{ cf(u,v): (u,v)  p }
for (u,v)  p do
f[u,v]  f[u,v] + cf(p)
f[v,u]   f[u,v]
Graph Algorithms
Maximum Flow - Ford-Fulkerson - Example
12
a
b
16
s
20
10
4
9
t
7
13
4
c
d
14
Graph Algorithms
Maximum Flow - Ford-Fulkerson - Example
12
a
b
16
s
20
10
4
9
t
7
13
4
c
d
14
Graph Algorithms
Maximum Flow - Ford-Fulkerson - Example
4/12
a
b
4/16
s
20
10
4
4/9
t
7
13
4/4
c
d
4/14
Graph Algorithms
Maximum Flow - Ford-Fulkerson - Example
8
a
b
4
4
20
12
s
10
13
4
4
5
4
10
c
d
4
t
7
Graph Algorithms
Maximum Flow - Ford-Fulkerson - Example
8
a
b
4
4
20
12
s
10
13
4
4
5
4
10
c
d
4
t
7
Graph Algorithms
Maximum Flow - Ford-Fulkerson - Example
4/12
a
b
11/16
s
7/20
7/10
4
4/9
t
7/7
13
4/4
c
d
11/14
Graph Algorithms
Maximum Flow - Ford-Fulkerson - Example
8
a
b
7
4
11
5
s
3
13
11
13
4
5
4
3
c
d
11
t
7
Graph Algorithms
Maximum Flow - Ford-Fulkerson - Example
8
a
b
7
4
11
5
s
3
13
11
13
4
5
4
3
c
d
11
t
7
Graph Algorithms
Maximum Flow - Ford-Fulkerson - Example
12/12
a
b
11/16
s
15/20
10
1/4
4/9
t
7/7
8/13
4/4
c
d
11/14
Graph Algorithms
Maximum Flow - Ford-Fulkerson - Example
a
b
15
12
11
5
s
11
5
3
5
4
5
8
4
3
c
d
11
t
7
Graph Algorithms
Maximum Flow - Ford-Fulkerson - Example
a
b
15
12
11
5
s
11
5
3
5
4
5
8
4
3
c
d
11
t
7
Graph Algorithms
Maximum Flow - Ford-Fulkerson - Example
12/12
a
b
11/16
s
19/20
10
1/4
9
t
7/7
12/13
4/4
c
d
11/14
Graph Algorithms
Maximum Flow - Ford-Fulkerson - Example
a
b
19
12
11
5
s
1
11
1
3
9
12
4
3
c
d
11
t
7
Graph Algorithms
Maximum Flow - Ford-Fulkerson - Bad Example
1000000
a
1000000
s
1
1000000
t
1000000
b
Graph Algorithms
Maximum Flow - Ford-Fulkerson - Bad Example
1000000
a
1000000
s
1
1000000
t
1000000
b
Graph Algorithms
Maximum Flow - Ford-Fulkerson - Bad Example
a
999999
1000000
1
s
1
1000000
1
t
999999
b
Graph Algorithms
Maximum Flow - Ford-Fulkerson - Bad Example
a
999999
1000000
1
s
1
1000000
1
t
999999
b
Graph Algorithms
Maximum Flow - Ford-Fulkerson - Bad Example
a
999999
999999
1
s
1
1
1
999999
1
t
999999
b
Graph Algorithms
Maximum Flow - Ford-Fulkerson - Bad Example
a
999999
999999
1
s
1
1
1
999999
1
t
999999
b
Graph Algorithms
Maximum Flow - Ford-Fulkerson - Complexity
procedure FordFulkerson(graph G = (V,E), source s, sink t)
for (u,v)  E do
f[u,v]  0
f[v,u]  0
while there exists a path p from s to t in Gf do
cf(p)  min{ cf(u,v): (u,v)  p }
for (u,v)  p do
f[u,v]  f[u,v] + cf(p)
f[v,u]   f[u,v]
If all capacities are integers and f* is a maximum flow,
then
T(V,E,f*) = O(E | f* |)
Graph Algorithms
Maximum Flow - Ford-Fulkerson - Complexity
Graph Algorithms
Maximum Flow - Edmonds-Karp Algorithm
Algorithm
Apply Ford-Fulkerson method with using BFS for
finding finding the shortest augmenting path in each
residual network.
Graph Algorithms
Maximum Flow - Edmonds-Karp Algorithm - complexity
Lemma
If the Edmonds-Karp algorithm is run on a flow network
G = (V,E) with source s and sink t, then for all vertices
v  V – {s,t} the shortest path distance f(s,v) in the residual
network Gf increases monotonically with each flow
augmentation.
Theorem
If the Edmonds-Karp algorithm is run on a flow network
G = (V,E) with source s and sink t, then the total number
of flow augmentations performed by the algorithm is at
most O(V E).
Graph Algorithms
Maximum Flow - Edmonds-Karp Algorithm - complexity
Graph Algorithms
Maximum Flow - Edmonds-Karp Algorithm - complexity
Graph Algorithms
Maximum Flow - Edmonds-Karp Algorithm - complexity
Graph Algorithms
Maximum Flow - Edmonds-Karp Algorithm - complexity
Graph Algorithms
Maximum Flow - Edmonds-Karp Algorithm - complexity
Graph Algorithms
Maximum Flow - Edmonds-Karp Algorithm - complexity
Graph Algorithms
Maximum Flow - Edmonds-Karp Algorithm - complexity
Graph Algorithms
Maximum Flow - Edmonds-Karp Algorithm - complexity