Network Flow Problems
Network Flow Problems
Introduction of: network, max-flow problem capacity, flow Ford-Fulkerson method pseudo code, residual networks, augmenting paths cuts of networks max-flow min-cut theorem example of an execution analysis, running time, variations of the max-flow problem
Introduction network
Practical examples of a network - liquids flowing through pipes - parts through assembly lines - current through electrical network - information through communication network - goods transported on the road
Introduction - network
Representation
Flow network: directed graph G=(V,E)
v1 v3 v1 v3
v2
v4
source
v2
v4
sink
v2
v4
source
v2
v4
sink
Informal definition of the max-flow problem: What is the greatest rate at which material can be shipped from the source to the sink without violating any capacity contraints?
Introduction - capacity
Representation
Flow network: directed graph G=(V,E)
3 v1 8 S 3 6 v2 6 12 v4 8 3 v3 6 S v1 v3
v2
v4
c(u,v)=12
u 6 v
Big pipe
c(u,v)=6
Small pipe
Introduction - capacity
Representation
Flow network: directed graph G=(V,E)
3 v1 8 S 3 6 v2 6 v4 8 3 v3 6 S v1 v3
v2
v4
If (u,v) E c(u,v) = 0
6 v2 0 0 v4 0 v3 v4
Introduction flow
Representation
Flow network: directed graph G=(V,E)
3 v1 8 S 3 6 v2 6 6/12 v4 8 3 v3 6 S v1 v3
v2
v4
f(u,v)=6
u 6/6 v
f(u,v)=6
Maximum flow
Introduction flow
Representation
Flow network: directed graph G=(V,E)
3 v1 8 S 3 6 v2 6 v4 8 3 v3 6 S v1 v3
v2
v4
Introduction flow
Representation
Flow network: directed graph G=(V,E)
3 v1 8 S 3 6/6 v2 6/6 v4 6/8 3 v3 6 S v1 v3
v2
v4
Introduction flow
Representation
Flow network: directed graph G=(V,E)
3 v1 8 S 3 6/6 v2 6/6 v4 6/8 3 v3 6 S v1 v3
v2
v4
Introduction flow
Representation
Flow network: directed graph G=(V,E)
3/3 v1 3/8 S 3 6/6 v2 6/6 v4 6/8 3 v3 3/6 S v1 v3
v2
v4
Introduction flow
Representation
Flow network: directed graph G=(V,E)
3/3 v1 3/8 S 3 6/6 v2 6/6 v4 6/8 3 v3 3/6 S v1 v3
v2
v4
Introduction flow
Representation
Flow network: directed graph G=(V,E)
3/3 v1 5/8 S 3 6/6 v2 6/6 v4 8/8 2/3 v3 3/6 S v1 v3
v2
v4
Introduction cancellation
Representation
Flow network: directed graph G=(V,E)
3/3 v1 5/8 S 3 6/6 v2 6/6 v4 8/8 2/3 v3 3/6 S v1 v3
v2
v4
Introduction cancellation
Representation
Flow network: directed graph G=(V,E)
3/3 v1 6/8 S 1/3 6/6 v2 5/6 v4 8/8 3/3 v3 4/6 S v1 v3
v2
v4
u 10 4 8/10
u 4 8/10
u 3/4 5/10
u 4
Flow properties
Flow in G = (V,E): f: V x V R with 3 properties: 1) Capacity constraint: For all u,v V : f(u,v) < c(u,v) 2) Skew symmetry: For all u,v V : f(u,v) = - f(v,u)
vV
f(u,v) = 0
Flow properties
Flow in G = (V,E): f: V x V R with 3 properties: 1) Capacity constraint: For all u,v V : f(u,v) < c(u,v) 2) Skew symmetry: For all u,v V : f(u,v) = - f(v,u)
vV
f(u,v) = 0
v1
12/12
v3
15 /20
t
1/4
4/9
7/7
10
8/1
v2
11/14
v4
4/4
f(u,v) = 5 f(v,u) = -5
v3
4/6
t 8/8
v4
Formal definition of the max-flow problem: The max-flow problem is to find a valid flow for a given weighted directed graph G, that has the maximum value over all valid flows.
This method contains 3 important ideas: 1) residual networks 2) augmenting paths 3) cuts of flow networks
1 initialize flow f to 0 2 while there exits an augmenting path p 3 do augment flow f along p 4 return f
12/12
v3
5
5 8
v1
v3
1/4
7/7
10
11
v2 v4
4/9
8/1 3
v2
11/14
v4
4/4
12/12
v3
12
5
11
11
v3
5 15
t
1/4
4/9
7/7
10
8/1 3
v2
11/14
v4
4/4
v2
3 11
v4
12/12
v3
12
5
11
11
v3
5 15
t
1/4
4/9
7/7
10
8/1 3
v2
11/14
v4
4/4
v2
3 11
v4
12/12
v3
12
5
11
11
v3
5 15
t
1/4
4/9
7/7
10
8/1 3
v2
11/14
v4
4/4
v2
3 11
v4
Augmenting path
12/12
v3
15 /20
t S
5
11
11
v1
12
v3
5 15
t
1/4
4/9
7/7
10
8/1 3
v2
11/14
v4
4/4
v2
3 11
v4
12/12
v3
12
4/4
5
11
11
v3
4/5 -4/ 1 5
t
1/4
4/5 -4/ 8
8/1 3
v2
11/14
v4
4/4
-4 /5
4/9
7/7
10
7
v4
v2
3 11
12/12
v3
12
4/4
/20
t S
5
11
11
v3
4/5 15
t
1/4
0/9
7/7
10
4/5 8
12/ 1
v2
11/14
v4
4/4
v2
3 11
v4
New flow:
f: V x V R : f=f + fp
Capacity constraint: cf(u,v) = c(u,v) f(u,v) For all u,v V, we require f(u,v) < c(u,v) Proof: fp (u ,v) < cf (u ,v) = c (u ,v) f (u ,v) (f + fp) (u ,v) = f (u ,v) + fp (u ,v) < c (u ,v)
Skew symmetry: For all u,v V, we require f(u,v) = - f(v,u) Proof: (f + fp)(u ,v) = f (u ,v) + fp (u ,v) = - f (v ,u) fp (v ,u) = - (f (v ,u) + fp (v ,u)) = - (f + fp) (v ,u)
f(u,v) = 0
v V
1/4
0/9
7/7
12/ 12
v3
10
19 20 /
4/4
12/ 13
In the example: S = {s,v1,v2) , T = {v3,v4,t} Net flow f(S ,T) = f(v1,v3) + f(v2,v4) + f(v2,v3) = 12 + 11 + (-0) = 23 Capacity c(S,T) = c(v1,v3) + c(v2,v4) = 12 + 14 = 26
v2
11/ S14 T
v4
vT
f (u, v)
1
S
6 1/1
v1
19 /20
t
1/4
0/9
7/7
v4
10
12/ 1
v2
4/4
11/14
proof
16 11/
S
12/12
v3
19 /20
t
1/4
0/9
7/7
v4
10
12/ 1
v2
4/4
11/14
(2):
We assume for the sake of contradiction that f is a maximum flow in G but that there still exists an augmenting path p in Gf. Then as we know from above, we can augment the flow in G according to the formula: f= f + fp. That would create a flow fthat is strictly greater than the former flow f which is in contradiction to our assumption that f is a maximum flow.
residual network Gf
3 v1 2 6 t S 2 6 v2 1 5 v4 8 3 1 4 t v3 2
residual network Gf
3 v3 2
original network G
3/3 v1 6/8 S 1/3 6/6 v2 1 5/6 v4 8/8 3/3 v3 4/6
12
v3
16
S
20
t
13
v2
14
v4
for each edge (u, v) E [G] do f [u, v] = 0 f [v, u] = 0 while there exists a path p from s to t in the residual network Gf do cf(p) = min{cf(u, v) | (u, v) p} for each edge (u, v) in p do f [u, v] = f [u, v] + cf(p) f [v, u] = - f [u, v]
10
(residual) network Gf
6 0 /1
v1
0/12
v3
0/2 0
0/9
0/1
v2
0/14
v4
0/4
5 6 7 8
for each edge (u, v) E [G] do f [u, v] = 0 f [v, u] = 0 while there exists a path p from s to t in the residual network Gf do cf(p) = min{cf(u, v) | (u, v) p} for each edge (u, v) in p do f [u, v] = f [u, v] + cf(p) f [v, u] = - f [u, v]
0/10
0/4
(residual) network Gf
v1
12
v3
16
S
20
13
v2
14
v4
5 6 7 8
for each edge (u, v) E [G] do f [u, v] = 0 f [v, u] = 0 while there exists a path p from s to t in the residual network Gf do cf(p) = min{cf(u, v) | (u, v) p} for each edge (u, v) in p do f [u, v] = f [u, v] + cf(p) f [v, u] = - f [u, v]
10
(residual) network Gf
v1
12
v3
16
S
20
13
v2
14
v4
5 6 7 8
for each edge (u, v) E [G] do f [u, v] = 0 f [v, u] = 0 while there exists a path p from s to t in the residual network Gf do cf(p) = min{cf(u, v) | (u, v) p} for each edge (u, v) in p do f [u, v] = f [u, v] + cf(p) f [v, u] = - f [u, v]
temporary variable: cf (p) = 12
10
(residual) network Gf
v1
12
v3
16
S
20
13
v2
14
v4
5 6 7 8
for each edge (u, v) E [G] do f [u, v] = 0 f [v, u] = 0 while there exists a path p from s to t in the residual network Gf do cf(p) = min{cf(u, v) | (u, v) p} for each edge (u, v) in p do f [u, v] = f [u, v] + cf(p) f [v, u] = - f [u, v]
temporary variable:
10
12/12
9
v3
12 /20
t
10
13
cf (p) = 12
v2
14
v4
(residual) network Gf
v1
12
v3
4
S
8 12
10
12
13
v2
14
v4
5 6 7 8
for each edge (u, v) E [G] do f [u, v] = 0 f [v, u] = 0 while there exists a path p from s to t in the residual network Gf do cf(p) = min{cf(u, v) | (u, v) p} for each edge (u, v) in p do f [u, v] = f [u, v] + cf(p) f [v, u] = - f [u, v]
12/12
9
v3
12 /20
t
10
13
v2
14
v4
(residual) network Gf
v1
12
v3
4
S
8 12
10
12
13
v2
14
v4
5 6 7 8
for each edge (u, v) E [G] do f [u, v] = 0 f [v, u] = 0 while there exists a path p from s to t in the residual network Gf do cf(p) = min{cf(u, v) | (u, v) p} for each edge (u, v) in p do f [u, v] = f [u, v] + cf(p) f [v, u] = - f [u, v]
12/12
9
v3
12 /20
t
10
13
v2
14
v4
(residual) network Gf
v1
12
v3
4
S
8 12
10
12
13
v2
14
v4
5 6 7 8
for each edge (u, v) E [G] do f [u, v] = 0 f [v, u] = 0 while there exists a path p from s to t in the residual network Gf do cf(p) = min{cf(u, v) | (u, v) p} for each edge (u, v) in p do f [u, v] = f [u, v] + cf(p) f [v, u] = - f [u, v]
12/12
9
v3
12 /20
t
10
13
v2
14
v4
(residual) network Gf
v1
12
v3
4
S
8 12
10
12
13
v2
14
v4
5 6 7 8
for each edge (u, v) E [G] do f [u, v] = 0 f [v, u] = 0 while there exists a path p from s to t in the residual network Gf do cf(p) = min{cf(u, v) | (u, v) p} for each edge (u, v) in p do f [u, v] = f [u, v] + cf(p) f [v, u] = - f [u, v]
12/12
9
v3
12 /20
t
10
13
v2
14
v4
(residual) network Gf
v1
12
v3
4
S
8 12
10
12
13
v2
14
v4
5 6 7 8
for each edge (u, v) E [G] do f [u, v] = 0 f [v, u] = 0 while there exists a path p from s to t in the residual network Gf do cf(p) = min{cf(u, v) | (u, v) p} for each edge (u, v) in p do f [u, v] = f [u, v] + cf(p) f [v, u] = - f [u, v]
temporary variable:
12/12
9
v3
12 /20
t
10
cf (p) = 4
4/1 3
v2
4/14
v4
4/4
(residual) network Gf
v1
12
v3
4
S
8 12
10
12
4 9
4
v2
10
v4
5 6 7 8
for each edge (u, v) E [G] do f [u, v] = 0 f [v, u] = 0 while there exists a path p from s to t in the residual network Gf do cf(p) = min{cf(u, v) | (u, v) p} for each edge (u, v) in p do f [u, v] = f [u, v] + cf(p) f [v, u] = - f [u, v]
12/12
9
v3
12 /20
t
10
4/1 3
v2
4/14
v4
4/4
(residual) network Gf
v1
12
v3
4
S
8 12
10
12
4 9
4
v2
10
v4
5 6 7 8
for each edge (u, v) E [G] do f [u, v] = 0 f [v, u] = 0 while there exists a path p from s to t in the residual network Gf do cf(p) = min{cf(u, v) | (u, v) p} for each edge (u, v) in p do f [u, v] = f [u, v] + cf(p) f [v, u] = - f [u, v]
12/12
9
v3
12 /20
t
10
4/1 3
v2
4/14
v4
4/4
(residual) network Gf
v1
12
v3
4
S
8 12
10
12
4 9
4
v2
10
v4
5 6 7 8
for each edge (u, v) E [G] do f [u, v] = 0 f [v, u] = 0 while there exists a path p from s to t in the residual network Gf do cf(p) = min{cf(u, v) | (u, v) p} for each edge (u, v) in p do f [u, v] = f [u, v] + cf(p) f [v, u] = - f [u, v]
12/12
9
v3
12 /20
t
10
4/1 3
v2
4/14
v4
4/4
(residual) network Gf
v1
12
v3
4
S
8 12
10
12
4 9
4
v2
10
v4
5 6 7 8
for each edge (u, v) E [G] do f [u, v] = 0 f [v, u] = 0 while there exists a path p from s to t in the residual network Gf do cf(p) = min{cf(u, v) | (u, v) p} for each edge (u, v) in p do f [u, v] = f [u, v] + cf(p) f [v, u] = - f [u, v]
temporary variable:
12/12
9
v3
12 /20
t
10
cf (p) = 7
4/1 3
v2
4/14
v4
4/4
(residual) network Gf
v1
12
v3
4
S
8 12
10
12
4 9
4
v2
10
v4
5 6 7 8
for each edge (u, v) E [G] do f [u, v] = 0 f [v, u] = 0 while there exists a path p from s to t in the residual network Gf do cf(p) = min{cf(u, v) | (u, v) p} for each edge (u, v) in p do f [u, v] = f [u, v] + cf(p) f [v, u] = - f [u, v]
temporary variable:
12/12
9
v3
19 /20
t
7/7
10
cf (p) = 7
11/ 1
v2
11/14
v4
4/4
(residual) network Gf
v1
12
v3
4
S
1 19
10
12
11 2
11
v2
v4
5 6 7 8
for each edge (u, v) E [G] do f [u, v] = 0 f [v, u] = 0 while there exists a path p from s to t in the residual network Gf do cf(p) = min{cf(u, v) | (u, v) p} for each edge (u, v) in p do f [u, v] = f [u, v] + cf(p) f [v, u] = - f [u, v]
12/12
9
v3
19 /20
t
7/7
v4
10
11/ 1
v2
4/4
11/14
(residual) network Gf
v1
12
v3
4
S
1 19
10
12
11 2
11
v2
v4
5 6 7 8
for each edge (u, v) E [G] do f [u, v] = 0 f [v, u] = 0 while there exists a path p from s to t in the residual network Gf do cf(p) = min{cf(u, v) | (u, v) p} for each edge (u, v) in p do f [u, v] = f [u, v] + cf(p) f [v, u] = - f [u, v]
12/12
9
v3
19 /20
t
7/7
10
4
9
11/ 1
v2
11/14
v4
4/4
The running time depends on how the augmenting path p in line 4 is determined.
O(|E| | fmax|)
v1
00
,00
s
1, 0 0 0, 00 0
t
00 0,0 0
1
v2
1,0
running time: O ( |E| | fmax| ) with fmax as maximum (1) The augmenting flow is chosen arbitrarily and all capacities are integers path
residual network Gf
99 9 ,9 1
v1
00
, 00
99
v1
1,0
00 , 00
s
1, 0 00 , 000
1/1
t
0 1,0 0 0,0 0
s
1,0 00 , 000
t
1 9 9,9 9
1
v2
99
v2
1/
running time: O ( |E| | fmax| ) with fmax as maximum (1) The augmenting flow is chosen arbitrarily and all capacities are integers path
residual network Gf
,9 999 99 1
v1
1,0
00
,00
v1
99 1
9, 9 99
s
1/ 1,0 00 , 0 00
t
0 1,0 0 0 ,00
s
99 9 1 ,99 9
t
1 9 9,9 9
1
v2
99
v2
1/
running time: O ( |E| | fmax| ) with fmax as maximum (1) The augmenting flow is chosen arbitrarily and all capacities are integers path
for all vertices v V\{s,t}: the shortest path distance f(s,v) in Gf increases monotonically with each flow augmentation f (s,v) < f (s,v)
S
u1
4
2
v3
6
t
u2
v4
running time: O ( |V| |E| ) (2) Edmonds-Karp algorithm augmenting path is found by breath-first search and has to be a shortest path from p to t
for all vertices v V\{s,t}: the shortest path distance f(s,v) in Gf increases monotonically with each flow augmentation . f(s,v) < f(s,v)
S
u1
4
2
v3
6
t
u2
v4
f(s,u2) = 1
running time: O ( |V| |E| ) (2) Edmonds-Karp algorithm augmenting path is found by breath-first search and has to be a shortest path from p to t
for all vertices v V\{s,t}: the shortest path distance f(s,v) in Gf increases monotonically with each flow augmentation . f(s,v) < f(s,v)
4
2
v3
4 2
t
u2
v4
f(s,u2) = 3
running time: O ( |V| |E| ) (2) Edmonds-Karp algorithm augmenting path is found by breath-first search and has to be a shortest path from p to t
The total number of flow augmentations performed by the algorithm is at most O(|V| |E|)
S
u1
4
2
v3
6
t
u2
v4
running time: O ( |V| |E| ) (2) Edmonds-Karp algorithm augmenting path is found by breath-first search and has to be a shortest path from p to t
Def: critical edge (u,v) if cf(p) = cf(u,v) At least one edge on p must be critical. How many times can (u,v) be critical during an execution of Edmonds-Karp algorithm?
S u1
4
2
v3
6
t
u2
v4
Critical edges on the path p running time: O ( |V| |E| ) (2) Edmonds-Karp algorithm augmenting path is found by breath-first search and has to be a shortest path from p to t
Idea: (u,v) can become critical at most O(V) times (After beeing critical it dissappears in Gf and can only reappear after net flow is decreased)
S u1
4
2
v3
6
t
u2
v4
our example: edge (u2,v4) running time: O ( |V| |E| ) (2) Edmonds-Karp algorithm augmenting path is found by breath-first search and has to be a shortest path from p to t
Idea: (u,v) can become critical at most O(V) times (After beeing critical it dissappears in Gf and can only reappear after net flow is decreased)
S
4
2
v3
4 2
t
u2
v4
running time: O ( |V| |E| ) (2) Edmonds-Karp algorithm augmenting path is found by breath-first search and has to be a shortest path from p to t
Idea: (u,v) can become critical at most O(V) times (After beeing critical it disappears in Gf and can only reappear after net flow is decreased)
S
4
2
v3
u2
2 2
v4
our example: edge (u2,v4) reappeared but is now unreachable from s running time: O ( |V| |E| ) (2) Edmonds-Karp algorithm augmenting path is found by breath-first search and has to be a shortest path from p to t
Idea: (u,v) can become critical at most O(|V|) times O(|E|) pairs of vertices exist in Gf All critical edges in O(|V| |E|) O(|E|) for breath first search for p total running time O(|V| |E|) running time: O ( |V| |E| ) (2) Edmonds-Karp algorithm augmenting path is found by breath-first search and has to be a shortest path from p to t
8
s2 t1
supersource
supersink
8 8
s3 t2
8
s4
f (x, y)
x f (X, X) = X y Y 0
f (X, Y) = - f (Y, X) f (u, V) = 0 for all u V \ {s, t} f (X Y, Z) = f (X, Z) + f (Y, Z) f (Z, X Y) = f (Z, X) + f (Z, Y)