22 Network Flow

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 27

Design and Analysis of Algorithms

Network Flow Problem - Maximum Flow


(Ford-Fulkerson Algorithm)

Dr. D. P. Acharjya
Professor, SCOPE
SJT Annex – 201 E
2/4/2024 Dr. D. P. Acharjya 1
Flow Network
 A Flow network can be considered as a
graph theoretic optimization problem.
 A flow network is a directed graph G = (V,
E), in which every edge (u, v)E is having a
capacity c(u, v). If (u, v)E, then c(u, v)=0.
 Therefore, c(u, v)  0.
 Let s be the source vertex and t be the sink
vertex in the flow network.
2/4/2023 Dr. D. P. Acharjya 2
An Illustration
 The source vertex is 0
 The sink vertex is 5
 c(0, 1) = 16
 c(0, 2) = 13
 c(3, 5) = 20

2/4/2023 Dr. D. P. Acharjya 3


Flow
 Let G = (V, E), be flow network.
 A flow, f , in G is a real valued function f:
(VV)  R that satisfies three conditions:
 Capacity constraint
 f(u, v)  c(u, v) for all (u, v )  E
 Skew symmetry
 f(u, v) = - f(v, u) for all (u, v )  E
 Flow conservation
  f(u, v ) = 0 for all u  V – {s, t} and v  V.
2/4/2023 Dr. D. P. Acharjya 4
Total Flow and Flow Problem
 The value of flow f(u, v) can be positive, zero,
or negative.
 The net flow is defined as:
 f | =  f(s, v) for v  V

 Objective
 Given a flow network, our objective is to find a
maximum flow value that a flow network may
have.
2/4/2023 Dr. D. P. Acharjya 5
Continued…
 It means that total flow out of the source is
equal to the total flow into the sink.
  f(s, v) = f | =  f(v, t) for v  V
 An example of flow network with capacity
and flow. The edges are labelled with f(u, v)
/ c(u, v).

2/4/2023 Dr. D. P. Acharjya 6


Residual Network
 If we increase the flow amount from one source to
another source, then better result will be provided. The
resultant network is residual network.
 Given a network flow G with flow f, the residual
network is a network that consists of edges that admits
more flow.
 Let G = (V, E) be a flow network with source s and
sink t. Let f be the flow. The additional flow that the
edge (u, v)  E is given as cf (u,v) and is defined as
 cf (u, v) = c(u, v) – f(u, v) [Residual capacity]
2/4/2023 Dr. D. P. Acharjya 7
Continued…
 Let G = (V, E) be a flow network with source s and
sink t. The residual network of G induced by the flow f
is given as Gf = (V, Ef) where
Ef = {(v, v)  (VV) | cf (u, v) > 0}

 The residual capacity cf (u, v) is defined as:


1. If f(u, v) < c(u, v), then
cf (u, v) = c(u, v) - f(u, v) > 0
2. If f(u, v) > 0 for any (u, v)  E, then f(v, u) < 0.
cf (v, u) = c(v, u) - f(v, u)
2/4/2023 Dr. D. P. Acharjya 8
Augmented Path
 Let G = (V, E) be a flow network with source s and
sink t. The simple path from source s to sink t in the
residual network Gf = (V, Ef) is known as augmented
path P. We define it as cf(P), where
cf (P) = Min{cf (u, v) | (u, v)  P}
 Maximum Flow Problem
 Finding the maximum flow in a given flow network G =
(V, E) from source s to sink t is known as maximum
flow problem.
 Ford-Fulkerson Algorithm
2/4/2023 Dr. D. P. Acharjya 9
Ford-Fulkerson Algorithm
 Finding the maximum flow in a given flow network G = (V,
E) from source s to sink t is known as maximum flow
problem.
1. For every edge (u, v)  E
2. Do f(u, v) = 0
3. f(v, u) = 0
4. While there exists an augmenting path P in residual network Gf
5. Do cf (P) = Min{cf (u, v) | (u, v)  P}
6. For (u, v)  P
7. Do f(u, v) = f(u, v) + cf (P)
8. f(v, u) = - f(u, v)

2/4/2023 Dr. D. P. Acharjya 10


Numerical Illustration
 Finding the maximum flow in a given flow network G = (V,
E) from source 0 to sink 5.

Initialization:
f(0, 1) = 0, f(0, 2) = 0, f(1, 2) = 0, f(2, 1) = 0, f(1, 3) = 0,
f(3, 2) = 0, f(2, 4) = 0, f(4, 3) = 0, f(3, 5) = 0, f(4, 5) = 0

2/4/2023 Dr. D. P. Acharjya 11


Continued …
Construct the residual network.
cf(0, 1) = c(0, 1) – f(0,1)
= 16 – 0 = 16
cf(1, 0) = c(1, 0) – f(1,0)
= 0 – (– f(0,1))
=0+0=0
Initial iteration G = Gf
Augmented path P:
0–1–3–2–4–5
cf(P) = Min {16, 12, 9, 14, 4} = 4
2/4/2023 Dr. D. P. Acharjya 12
Continued …
f(0,1) = f(0,1) + cf(P)
=0+4=4
f(1,3) = f(1,3) + cf(P)
=0+4=4
f(3,2) = f(3,2) + cf(P)
=0+4=4
f(2,4) = f(2,4) + cf(P)
=0+4=4
f(4,5) = f(4,5) + cf(P)
=0+4=4
2/4/2023 Dr. D. P. Acharjya 13
Continued …
The residual network.
cf(0, 1) = c(0, 1) – f(0,1)
= 16 – 4 = 12
cf(1, 0) = c(1, 0) – f(1,0)
= 0 – (– f(0,1))
=0+4=4
cf(1, 3) = c(1, 3) – f(1,3) = 12 – 4 = 8
cf(3, 1) = c(3, 1) – f(3,1) = 0 + 4 = 4
cf(3, 2) = c(3, 2) – f(3,2) = 9 – 4 = 5
cf(2, 3) = c(2, 3) – f(2,3) = 0 + 4 = 4
2/4/2023 Dr. D. P. Acharjya 14
Continued …
cf(2, 4) = c(2, 4) – f(2,4)
= 14 – 4 = 10
cf(4, 2) = c(4, 2) – f(4,2)
=0+4=4
cf(4, 5) = c(4, 5) – f(4,5)
=4–4=0
cf(5, 4) = c(5, 4) – f(5,4)
=0+4=4
The residual network:

2/4/2023 Dr. D. P. Acharjya 15


Continued …
Augmented path P:
0–1–2–4–3–5
cf(P)=Min{12,10,10,7,20}
=7
f(0,1) = f(0,1) + cf(P)
= 4 + 7 = 11
f(1,2) = f(1,2) + cf(P) = 0 + 7 = 7
f(2,4) = f(2,4) + cf(P) = 4 + 7 = 11
f(4,3) = f(4,3) + cf(P) = 0 + 7 = 7
f(3,5) = f(3,5) + cf(P) = 0 + 7 = 7
2/4/2023 Dr. D. P. Acharjya 16
Continued …
The residual network.
cf(0, 1) = c(0, 1) – f(0,1)
= 16 – 11 = 5
cf(1, 0) = c(1, 0) – f(1,0)
= 0 – (– f(0,1))
= 0 + 11 = 11
cf(1, 3) = c(1, 3) – f(1,3) = 12 – 4 = 8
cf(3, 1) = c(3, 1) – f(3,1) = 0 + 4 = 4
cf(3, 2) = c(3, 2) – f(3,2) = 9 – 4 = 5
cf(2, 3) = c(2, 3) – f(2,3) = 0 + 4 = 4
2/4/2023 Dr. D. P. Acharjya 17
Continued …
cf(1, 2)=c(1, 2) – f(1,2)=10 – 7 = 3
cf(2, 1) = c(2, 1) – f(2,1)
= 4 – (– f(1,2))= 4 + 7 = 11
cf(2, 4) = c(2, 4) – f(2,4) = 14 – 11
=3
cf(4, 2) = c(4, 2) – f(4,2) = 0 + 11 = 11
cf(4, 3) = c(4, 3) – f(4,3) = 7 – 7 = 0
cf(3, 4) = c(3, 4) – f(3,4) = 0 + 7 = 7
cf(3, 5) = c(3, 5) – f(3,5) = 20 – 7 = 13
cf(5, 3) = c(5, 3) – f(5,3) = 0 + 7 = 7
2/4/2023 Dr. D. P. Acharjya 18
Continued …
cf(4, 5) = c(4, 5) – f(4,5)
=4–4=0
cf(5, 4) = c(5, 4) – f(5,4)
= 0 – (– f(4,5))= 4
The augmented graph

2/4/2023 Dr. D. P. Acharjya 19


Continued …
Augmented path P:
0–2–1–3–5
cf(P)=Min{13,11,8,13}=8
f(0,2) = f(0,2) + cf(P)
=0+8=8
f(2,1) = f(2,1) + cf(P)
= - f(1,2) + 8 = -7 + 8 =1
f(1,3) = f(1,3) + cf(P) = 4 + 8 = 12
f(3,5) = f(3,5) + cf(P) = 7 + 8 = 15
The graph becomes
2/4/2023 Dr. D. P. Acharjya 20
Continued …
Augmented path P:
0–2–1–3–5
cf(P)=Min{13,11,8,13}=8
f(0,2) = f(0,2) + cf(P)
=0+8=8
f(2,1) = f(2,1) + cf(P)
= - f(1,2) + 8 = -7 + 8 =1
f(1,3) = f(1,3) + cf(P) = 4 + 8 = 12
f(3,5) = f(3,5) + cf(P) = 7 + 8 = 15
The graph becomes
2/4/2023 Dr. D. P. Acharjya 21
Continued …
The residual network.
cf(0, 1) = c(0, 1) – f(0,1)
= 16 – 11 = 5
cf(1, 0) = c(1, 0) – f(1,0)
= 0 – (– f(0,1))
= 0 + 11 = 11
cf(0, 2) = c(0, 2) – f(0,2) = 13 – 8 = 5
cf(2, 0) = c(2, 0) – f(2,0) = 0 + 8 = 8
cf(1, 3) = c(1, 3) – f(1,3) = 12 – 12 = 0
cf(3, 1) = c(3, 1) – f(3,1) = 0 + 12 = 12
2/4/2023 Dr. D. P. Acharjya 22
Continued …
cf(1, 2)=c(1, 2) – f(1,2)
=10 +1 = 11
cf(2, 1)=c(2, 1) – f(2,1)=4–1=3
cf(2, 4) = c(2, 4) – f(2,4)
= 14 – 11= 3
cf(3, 2) = c(3, 2) – f(3,2) = 9 – 4 = 5
cf(2, 3) = c(2, 3) – f(2,3) = 0 + 4 = 4
cf(4, 2) = c(4, 2) – f(4,2) = 0 + 11 = 11
cf(4, 3) = c(4, 3) – f(4,3) = 7 – 7 = 0
cf(3, 4) = c(3, 4) – f(3,4) = 0 + 7 = 7
2/4/2023 Dr. D. P. Acharjya 23
Continued …
cf(4, 5)=c(4, 5) – f(4,5)
=4 – 4 = 0
cf(5, 4)=c(5, 4) – f(5, 4)=0–4=4
cf(3, 5)=c(3, 5)–f(3,5)=20–15=5
cf(5, 3)=c(5, 3)–f(5,3)=0+15= 15
The residual network is given as
Augmented path P:
0–2–3–5
cf(P)=Min{5,4,5}=4

2/4/2023 Dr. D. P. Acharjya 24


Continued …
f(0,2) = f(0,2) + cf(P)
= 8 + 4 = 12
f(2,3) = f(2,3) + cf(P) = - 4+4=0
f(3,5) = f(3,5) + cf(P)=15 + 4=19

The flow graph becomes

2/4/2023 Dr. D. P. Acharjya 25


Continued …
The residual network.
cf(0, 1) = c(0, 1) – f(0,1) = 5
cf(1, 0) = c(1, 0) – f(1,0)= 11
cf(0, 2) = c(0, 2) – f(0,2) = 1
cf(2, 0) = c(2, 0) – f(2,0)= 12
cf(1, 3) = c(1, 3) – f(1,3) = 0
cf(3, 1) = c(3, 1) – f(3,1) = 12
cf(1, 2)=c(1, 2) – f(1,2) = 11
cf(2, 1)=c(2, 1) – f(2,1) = 3
cf(2, 4) = c(2, 4) – f(2,4) = 3
2/4/2023 Dr. D. P. Acharjya 26
Continued …
cf(4, 2) = c(4, 2) – f(4,2) = 11
cf(4, 3) = c(4, 3) – f(4,3) = 0
cf(3, 4) = c(3, 4) – f(3,4) = 7
cf(3, 5) = c(3, 5) – f(3,5) = 1
cf(5, 3) = c(5, 3) – f(5,3) = 19
cf(4, 5)=c(4, 5) – f(4,5) = 0
cf(5, 4)=c(5, 4) – f(5, 4) = 4
Since there is no path from
source to sink, the process
terminates.
2/4/2023 Dr. D. P. Acharjya 27

You might also like