Module 2
Module 2
3 4
1
2
1 2 3 4
1 2
6 7
1 0 ∞ 3 ∞
3 2 2 0 ∞ ∞
𝐷0 = 3 ∞ 7 0 1
3 4
1 4 6 ∞ ∞ 0
1 2 2 0 5 ∞
𝐷 =3 ∞ 7 0
D1[4,2] =min {D0[4,2], D0[4,1]+D0[1,2] }
1 = min {∞, 6+ ∞}
4 6 ∞ 9 0 =∞
3 4
1
0 10 3 4
2 0 5 6
𝐷4 = 7 7 0 1
6 16 9 0
O(n3)
Flow Value:
1/2
2/2
b c
2/3 |f| = V(f) = fout(s)
= fin (t)
=3
1/7 2/6
S ?/6 t
?/4
9/11
b c
10/15
4/4
a d
3/4 8/10
1/7 2/6
S 6/6 t
4/4
9/11
b c
10/15
a d a 6 d
1
1 7 2 21
c c
3 2
2 21
c
2 21
c
t1 t1
∞ s1 ∞
s1
Internal Internal
s ∞
Node t2 Node t2 t
∞
s2 s2 ∞
t3 t3
𝑐 𝑢, 𝑣 − 𝑓 𝑢, 𝑣 , 𝑖𝑓 (𝑢, 𝑣) ∈ 𝐸
𝑐𝑓 𝑢, 𝑣 = 𝑓 𝑣, 𝑢 , 𝑖𝑓(𝑣, 𝑢) ∈ 𝐸
0, 𝑂𝑡ℎ𝑒𝑟𝑤𝑖𝑠𝑒
Prepared By, Dr. Kovendan AKP., Assistant Professor Senior,
SCOPE, VIT, Vellore.
Residual Graph Example 1
b b
1 1
1/1 1
a d a 1 1 d
1/2
1 1
1 1/1 c
c
5/7
a d
3/3 5/8
2/2 1/1
S 1/1 t
2/3
4/5
b c
3/3
Path in Residual
Graph
a a Augment the Path in
3 C(P’)=min{3,3,1}=1 1/3 Flow Graph
3 1/3
1 4 1 1/5 2/2
s b c t 1/1
s b c t
1 1
4 3 0/4 0/3
d e d e
2 Prepared By, Dr. Kovendan AKP., Assistant Professor Senior, 0/2
SCOPE, VIT, Vellore.
Maximum Flow - The Ford-Fulkerson method
• We try to find out the max flow value of a graph.
• Max(V(f))
a
0/3 Flow initialized to
0/3
Zero
0/1 0/5 0/2
s b c t
0/4 0/3
d e
0/2
Path in Residual
Graph
a Augment the Path in
a C(P’)=min{1,5,5}=1 0/3 Flow Graph
3 0/3
3
1/1 1/5 2/2
1 s b c t
5 2
s b c t
0/4 0/3
4 3
d e
d e Prepared By, Dr. Kovendan AKP., Assistant Professor Senior, 0/2
2 SCOPE, VIT, Vellore.
Flow graph a Residual graph
a 3
3
0/3
0/3
1 4 1
1/5 1/2 s b c t
1/1
s b c t 1 1
4 3
0/4 0/3
d e
d e 2
0/2
Path in Residual
Graph
a a Augment the Path in
3 C(P’)=min{3,3,1}=1 1/3 Flow Graph
3 1/3
1 4 1 1/5 2/2
s b c t 1/1
s b c t
1 1
4 3 0/4 0/3
d e d e
2 Prepared By, Dr. Kovendan AKP., Assistant Professor Senior, 0/2
SCOPE, VIT, Vellore.
a Flow graph a Residual graph
1/3 1 1
1/3 2 2
1/5 2/2 1 4
1/1 s b c t
s b c t
1 2
0/4 0/3 4 3
d e d e
0/2 2
Path in Residual
Graph
a a Augment the Path in
1 C(P’)=min{2,2,1,4,2,3}=1 2/3 Flow Graph
1 2/3
2 2
1 4 1/1 0/5 2/2
s b c t s b c t
1 2
4 3 1/4 1/3
d e d e
2 Prepared By, Dr. Kovendan AKP., Assistant Professor Senior, 1/2
SCOPE, VIT, Vellore.
a Flow graph a Residual graph
2 2
2/3 1 1
2/3
0/5 2/2 1 5
1/1 s b c t
s b c t 2
1 1
3 2
1/4 1/3 1
d e d e
1/2 1
Path in Residual
Graph
a Final Flow graph
a
Not a valid path !!! 2/3 Maximum Flow
2 2 2/3
1 1 V(f)=3
1 1 1/3
3 2 1/4
1
d e d e
1 Prepared By, Dr. Kovendan AKP., Assistant Professor Senior, 1/2
SCOPE, VIT, Vellore.
Homework
• Apply Ford Fulkerson method to find the maximum flow in the given
graph.
Question
Solution
1 5 2
s b c t
4 3
d e T
2
Prepared By, Dr. Kovendan AKP., Assistant Professor Senior,
SCOPE, VIT, Vellore.
a S
Capacity of a Cut 3
3
1 5 2
s b c t
• The capacity of a S-T cut is denoted by C(S,T).
4 3
𝐶 𝑆, 𝑇 = 𝐶(𝑢, 𝑣) d e T
𝑢∈𝑆 2
𝑣∈𝑇
• Summation of capacity of outgoing edges from S to T.
• Eg: S={s,a,c} T={b,d,e,t}
Outgoing edges={(s,b),(c,t)}
C(S,T) = 1+2
C(S,T) = 3
• A min cut in a S-T cut is defined as a cut with minimum capacity.
Prepared By, Dr. Kovendan AKP., Assistant Professor Senior,
SCOPE, VIT, Vellore.
Flow across the cut
• It is also referred to a the net flow.
• If f is the flow in the graph G, then the net flow f(S,T) across the cut
(S,T) is defined as follows,
=2 0/4 0/3
d e T
0/2
f(v) = |f| = 2
f(S,T) = |f|
• Capacity
O(E .|f*|)
0/8
0/2 0/6
S t
0/10
0/10
b c
0/9
1
20 18 8
15 3
S 2 t
10
12
3
1
0/20 0/8
0/18
0/15 0/3
S 2 t
0/10
0/12
3
0/10 Min{20,8} = 8
0/12
3
Prepared By, Dr. Kovendan AKP., Assistant Professor Senior,
SCOPE, VIT, Vellore.
Flow graph Residual graph
1 1
8/20 8/8 8
0/18 8
12 18
0/15 0/3 15
S 2 t 3
S 2 t
0/10 10
0/12 12
3 3
Path:
Final Flow graph 1. S
1 2. S-1;S-2
3. S-2; S-1-2;
8/20 8/8 Maximum Flow = 21
0/18
No outgoing edges from 2.
13/15 3/3
S 2 t
So No path in Residual graph.
10/10 So Stop.
10/12
3
Prepared By, Dr. Kovendan AKP., Assistant Professor Senior,
SCOPE, VIT, Vellore.
Comparison of Edmond Karp and Ford
Fulkerson Algorithm
100
1 3
100
100 1 Find the maximum flow for the given
graph using Edmond Karp Algorithm.
S 1 1 t
100 1
100
2 4
100
S 1 1 t S 1 1 t
100 1 100 1
100 100
2 4 2 4
100 100
s 6 t
8 5
b
8 5
h(a) = 0 ; e(a) = 0
b h(b) = 0 ; e(b) = 0
h(t) = 0 ; e(t) = 0
h(b)=0
e(b)=0
a a
5 4 5/5 4
h(t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0 h(t)=0
h(s)=4 s 0/6 t h(s)=4 s 0/6 t
e(t)=9 e(t)=9
h(b)=3 h(b)=4
e(b)=3 e(b)=3
Active Node: b
Neighbours : {s,a,t}
a a
5/5 4/4 5/5 4/4
h(t)=0 h(t)=0
h(s)=4 s 0/6 t h(s)=4 s 0/6 t
e(t)=9 e(t)=9
h(b)=4 h(b)=5
e(b)=3 e(b)=0
a a
5/5 4/4 4/5 4/4
h(t)=0 h(t)=0
h(s)=4 s 0/6 t h(s)=4 s 0/6 t
e(t)=9 e(t)=9
h(b)=5 h(b)=5
e(b)=0 e(b)=0
a
4/5 4/4
No excess in the intermediate
h(t)=0
h(s)=4 s 0/6 t nodes !!!
e(t)=9
So, Stop.
5/8 5/5
b
h(b)=5
e(b)=0
s 5 8 t
12 17
c d
6
5 8 h(t)=0
h(s)=6 s t
e(t)=0
12 17
c d
6
h(c)=0 h(d)=0
e(c)=0 e(d)=0
5 8 h(t)=0 h(t)=0
h(s)=6 s t h(s)=6 s 5 8 t
e(t)=0 e(t)=0
12 17 12/12 17
c d c d
6 6
h(c)=0 h(d)=0 h(c)=0 h(d)=0
e(c)=0 e(d)=0 e(c)=12 e(d)=0
17 12/12 17
12/12 c d
c d 6/6
6
h(c)=0 h(c)=1 h(d)=0
h(d)=0
e(c)=12 e(c)=6 e(d)=6
e(d)=0
12/12 17 12/12 17
c d c d
6/6 6/6
h(c)=1 h(d)=0 h(c)=1 h(d)=0
e(c)=6 e(d)=6 e(c)=6 e(d)=6
12/12 17 12/12 17
c d c d
6/6 6/6
h(c)=1 h(d)=0 h(c)=1 h(d)=0
e(c)=6 e(d)=6 e(c)=6 e(d)=14
0/4 0/3
d e
0/2