0% found this document useful (0 votes)
14 views

Lecture#7 - Flow Network Algorithm

Uploaded by

Pritom Das
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views

Lecture#7 - Flow Network Algorithm

Uploaded by

Pritom Das
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 40

FLOW NETWORK

CSE-237 : ALGORITHM DESIGN AND


A N A LY S I S
FLOW NETWORK

• A flow network (or just network) N consists of


• A weighted digraph G with nonnegative integer edge weights, where
the weight of an edge e is called the capacity c(e) of e
• Two distinguished vertices, s and t of G, called the source and sink,
respectively, such that s has no incoming edges and t has no outgoing edges.

v 3
• The value of a flow f , denoted |f|, 6
1
7 t
is the total flow from the source, s 3
w
which is the same as the total flow 1 9 5
5
into the sink u z
2

Flow Networks ‹#›


TERMINOLOGY - FLOW

• A flow f for a network N is an assignment of an integer value f(e) to


each edge e that satisfies the following properties:

0 e,
• Capacity Rule: For each edge f (e) c(e)
 f (e)   f (e )
• Conservation Rule: For each vertex evE
s,t

(v) eE 
(v)

where E-(v) and E+(v) are the incoming


and outgoing edges of v,
resp.

Flow Networks ‹#›


TERMINOLOGY - MAXIMUM FLOW

• A flow for a network N is said to be maximum if its value is the largest of all flows
for N
v 1/3 v 3/3
2/6 4/6
1/1 3/7 t 1/1 3/7 t
s 3/3 s 3/3
w w
1/1 2/9 4/5 1/1 2/9 4/5
3/5 3/5
u z u z
2/2 2/2
Flow of value 8 = 2 + 3 + 3 Flow (Maximum) of value 10 = 4 + 3 + 3

• The maximum flow problem consists of finding a maximum flow for a given
network N.
• Applications - Hydraulic Systems, Electrical Circuits, Traffic Movements etc

Flow Networks ‹#›


TERMINOLOGY - CUT

• A cut of a network N with source s and sink t is a partition c = (Vs,Vt) of the


vertices of N such hat s  Vs and t  Vt c v 3
6 1
7 t
• Forward edge of cut c : origin in Vs and destination in Vt s 3
w
• Backward edge of cut c : origin in Vt and destination in Vs 1 9 5
5
u z
c 2
v 1/3
2/6 • Capacity c(c) of a cut c : total capacity of forward
1/1 3/7 t
s 3/3 edges
w
1/1 2/9 4/5 • Flow f(c) across a cut c : total flow of forward edges
3/5
u z minus total flow of backward edges
2/2
• Example : c(c) = 24, f(c) = 8

Flow Networks ‹#›


TERMINOLOGY - FLOW AND CUT

• Lemma : The flow f(c) across any cut c is equal to the flow value |f|
• Lemma : The flow f(c) across a cut c is less than or equal to the capacity c(c)
of the cut

• Theorem : The value of any flow is less c1 c2


than or equal to the capacity of any cut, v 1/3
2/6
i.e., for any flow f and any cut c, we have 1/1
3/7 t
|f|  c(c) s 3/3
w
1/1 2/9 4/5
|f| = 8 3/5
• c(c1) = 6 + 3 + 1 + 2 = 12
u z
• c(c2) = 3 + 7 + 9 + 2 = 21 2/2

Flow Networks ‹#›


TERMINOLOGY - AUGMENTING PATH

• Consider a flow f for a network N the flow valu


|f| = 7
• Let e be an edge from u to v, the residual capacity (RC): p
v 1/3
• RC of forward edge e, from u to v: Df(u, v) = c(e) - f (e)2/6
1/1 t
2/7
• RC of backward edge e, from v to u: Df(v, u) = f (e)s 3/3
w
• Let p be a path from s to t 2/9 4/5
2/5 0/1
• The residual capacity Df(p) of p is the
u z
smallest of the residual capacities of the 2/2
edges of p
Df(s,u) = 3 Df(u,w) = 1 Df(w,v) = 1 Df(v,t) = 2
• A path p from s to t is an augmenting path
if Df(p) > 0 So Df(p) = 1

Flow Networks ‹#›


TERMINOLOGY - RESIDUAL NETWORK

• Given a flow network G=(V,E) and a flow f, the residual network of G


induced by f is Gf=(V,Ef), where Ef = { (u,v) ∈V x V : cf(u,v) >0}

12/12 12
v1 v3 v1 v3

s t s t
1/4
10

11
7/7

7
3
v2 v4 v2 v4
11/14 11
Flow network G and flow f Residual network induced by f

Flow Networks ‹#›


TERMINOLOGY - FLOW AUGMENTATION
p
v 1/3
2/6
• Lemma : Let p be an augmenting path 1/1
2/7 t
for flow f in network N. There exists a s 3/3
w
flow f for N of value | f | = |f | + Df(p) 0/1 2/9 4/5
|f|=7 2/5
u z
2/2
• Proof : We compute flow f by Df(p) = 1
modifying the flow on the edges of p p
v 2/3
2/6
0/1 2/7 t
• Forward edge : f (e) = f(e) + Df(p) s 3/3
w
• Backward edge : f (e) = f(e) - Df(p) 1/1 2/9 4/5
3/5
u z | f | =
2/2 8
Flow Networks ‹#›
TERMINOLOGY - MAX FLOW MIN CUT

• Consider a flow f for a network N the flow valu


|f| = 7
• Let e be an edge from u to v, the residual capacity (RC): p
v 1/3
• RC of forward edge e, from u to v: Df(u, v) = c(e) - f (e)2/6
1/1 t
2/7
• RC of backward edge e, from v to u: Df(v, u) = f (e)s 3/3
w
• Let p be a path from s to t 2/9 4/5
2/5 0/1
• The residual capacity Df(p) of p is the
u z
smallest of the residual capacities of the 2/2
edges of p
Df(s,u) = 3 Df(u,w) = 1 Df(w,v) = 1 Df(v,t) = 2
• A path p from s to t is an augmenting path
if Df(p) > 0 So Df(p) = 1

Flow Networks ‹#›


TERMINOLOGY - RECAP

Source and Sink Capacities and Saturation

S S 7/9
7/10 7/7 T
7/11
T

Flow Conservation Flow Cancellation

14 6
8 3
1

10
1 5 = 0
7

Flow Networks ‹#›


TERMINOLOGY - RECAP ...

Flow of Network Max Flow and Min Cut


12 + 7 = 19 +
saturated 0
A B A B
11/16 12/12 19/2 12/12
0 11/16 19/2
1/4 saturat 1/4 0
S 0/1 T S 0/1 7/7 T
0/9 ed 7/7 0
0/
0 9
12/1 12/1 4/4
4/4
3 3 C D
C D 11/14
11/14 saturat
ed
23/23
12 + 0 + 0 = 1 + 23/29 23/2
11
6
Flow Networks ‹#›
FORD-FULKERSON’S ALGORITHM

Flow Networks
FORD-FULKERSON’S ALGORITHM

• Ford-Fulkerson is the basic technique for finding the maximum flow.


• Further optimizations also exist.

• Characteristics
• Single source, single sink
• Several algorithms are implementations of this method
• Requires integer capacities for steady progress
• Fastest implementation computes max flow in O(V^3) time

Flow Networks ‹#›


FORD-FULKERSON’S ALGORITHM ...

Initially, f(e) = 0 for each edge e


Repeatedly
• Search for an augmenting path p
• Augment by Df(p) the flow along
the edges of p

A specialization of DFS (or BFS)


searches for an augmenting path
• An edge e is traversed from u to v
provided Df(u, v) > 0

Flow Networks ‹#›


EXAMPLE

0/1
A 2 B
0/1 0/2
6 0
0/
S 0/1 4 0/ T
0/
0 7
9
0/1 0/
3 4
C D
0/1
• To begin, it turns out that the initial
4 RN is just the set of capacities of the graph.
• The routine is: Compute RN, find next augmenting path, recompute G and RN,
etc

Flow Networks ‹#›


EXAMPLE - AUGMENTING PATH

• Find the residual network : Flow / Residual Capacity

12
A B
16
20
4
S 10 7 T
9
13 4
C D
14

• Find a augmenting path with DFS or BFS.

Flow Networks ‹#›


EXAMPLE - AUGMENTING PATH

• How much can be sent along the path?

12
A B
16
20
4
S 10 7 T
9
13 4
C D
14

• Minimum flow network through this path is 12

Flow Networks ‹#›


EXAMPLE - RESULTANT FLOW NETWORK

• Resultant flow network through this path is 12

12/12
A B
12/16 12/2
0
0/
S 0/1 0/ T
4 0/
0 7
9
0/1 0/
3 C D 4
0/1
4
• Resultant flow network having net flow 0+12 = 12

Flow Networks ‹#›


EXAMPLE - ANOTHER AUGMENTING PATH

• Residual network on step 2

A B
16- 12 20-
12=4 12=8
12 4
S 10 12 T
7
9

1 4
3 C D
14

• Find another augmenting path with DFS or BFS.

Flow Networks ‹#›


EXAMPLE - ANOTHER AUGMENTING PATH

• How much can be sent along the path?

A B
16- 12 20-
12=4 12=8
12 4 12
S 10 7 T
9

1 4
3 C D
14

• Minimum flow network through this path is 4

Flow Networks ‹#›


EXAMPLE - RESULTANT FLOW NETWORK

• Resultant flow network through this path is 4

12/12
A B
16/16 16/2
0
0/
S 4/1 4/ T
4 0/
0 7
9
0/1 0/
3 C D 4
4/1
4
• Resultant flow network having net flow 12+4 = 16

Flow Networks ‹#›


EXAMPLE - ANOTHER AUGMENTING PATH

• Residual network on step 3

A B
12 20-
16=4
16 8
S 6 16 T
9 4 7-4=3

1 4
4
3 C D
14-
4=10
• Find another augmenting path with DFS or BFS.

Flow Networks ‹#›


EXAMPLE - ANOTHER AUGMENTING PATH

• How much can be sent along the path?

A B
12 20-
16=4
16 8
S 6 16 T
9 4 7-4=3

1 4
4
3 C D
14-
4=10
• Minimum flow network through this path is 3

Flow Networks ‹#›


EXAMPLE - RESULTANT FLOW NETWORK

• Resultant flow network through this path is 3

12/12
A B
16/16 19/2
0
0/
S 4/1 7/ T
4 0/
0 7
9
3/1 0/
3 C D 4
7/1
4
• Resultant flow network having net flow 16+3 = 19

Flow Networks ‹#›


EXAMPLE - ANOTHER AUGMENTING PATH

• Residual network on step 4

A B
12 20-
19=1
16 8
S 6 19 T
9 7
3
1 4
7
0 C D
14-
7=7
• Find another augmenting path with DFS or BFS.

Flow Networks ‹#›


EXAMPLE - ANOTHER AUGMENTING PATH

• How much can be sent along the path?

A B
12 20-
19=1
16 8
S 6 19 T
9 7
3
1 4
7
0 C D
14-
7=7
• Minimum flow network through this path is 4

Flow Networks ‹#›


EXAMPLE - RESULTANT FLOW NETWORK

• Resultant flow network through this path is 4

12/12
A B
16/16 19/2
0
0/
S 4/1 7/ T
4 0/
0 7
9
7/1 4/
3 C D 4
11/1
4
• Resultant flow network having net flow 19+4 = 23

Flow Networks ‹#›


EXAMPLE - ANOTHER AUGMENTING PATH

• Residual network on step 5

A B
12 20-
19=1
16 8
S 6 19 T
9 7
7
13- 4
11
7=6 C D
14-
11=3
• Find another augmenting path with DFS or BFS.

Flow Networks ‹#›


EXAMPLE - ANOTHER AUGMENTING PATH

• There is no available augmenting path!

A B
12 20-
19=1
16 8
S 6 19 T
9 7
7
13- 4
11
7=6 C D
14-
11=3
• Min Cut : 0 Max Flow : 23

Flow Networks ‹#›


EDMONDS-KARP ALGORITHM

Flow Networks
EDMONDS-KARP ALGORITHM

Variation of Ford-Fulkerson’s algorithm


Uses a simple technique for finding good augmenting path in faster
running time.
• It is more greedy in the choice of path
• Chooses an augmenting path p with the smallest number of edges
• This can be done in O(m) by a modified BFS traversal

Complexity O(n m2)


Lemma: The number of flow augmentations in the Edmonds-Karp
algorithm for a network with n vertices and m edges is no more than n m.

Flow Networks ‹#›


MAXIMUM BIPARTITE MATCHING

Flow Networks
MAXIMUM BIPARTITE MATCHING

• Graph G(V,E) is bipartite (denoted G(V1,V2,E) ) if


• The vertices V of G can be partitioned into two disjoint sets, V1 and V2
• Every edge e of G has one endpoint in V1 and the other in V2.
• A matching in G is a set of edges (subset of E) that have no endpoints
in common.
• A matching in bipartite graph is called bipartite matching.

Flow Networks ‹#›


MAXIMUM BIPARTITE MATCHING ...

• We are often interested in maximum matching, that is, the matching


with maximum number of edges.

M=1 M=2

V1 V2

Flow Networks ‹#›


MAXIMUM BIPARTITE MATCHING ...

• Solution is non-unique
• Followings graphs - all are maximum,(M=2) V1 V2

Flow Networks ‹#›


REDUCTION TO MAX FLOW PROBLEM

• Systematic approach to bipartite matching


problem
V1 V2
• Create a network flow N from graph G(V1,V2,E):
• Add a source s and a sink t
• Connect s withV1 and t with V2, using directed t
s
edges
• Direct edges E from V1 to V2 and assign capacities
c(e)=1
• Solve a max flow problem on the network flow N
• max M = max f

Flow Networks ‹#›


REDUCTION TO PROOF OF CORRECTNESS

• f(e) = 1  e  M
• Set M is a matching: rom graph G(V1,V2,E):
• The flow through e is 0 or 1 (capacity is 1)
• Each vertex in V1 has exactly one incoming edge: by flow conservation rule there is at
most one outgoing edge for v V1
• Each vertex in V2 has exactly one outgoing edge: there is at most one incoming edge
for v V2
V V
• Hence, each vertex in V1 will be paired in M by at most one vertex in V2 ., hence M is
1 2
the matching. t
s
• Finally, size of M = |f|.

Flow Networks ‹#›


APPLICATION OF FLOW NETWORK / MATCHING

• Job Scheduling : you have a set of people, and a set of jobs. Each person
is certified to handle 1+ jobs, but can only perform 1 job. Is there a way to
assign jobs to maximize how many jobs get done, e.g. all of them?
• Example: P1  J1; P2  J1, J2; P3  J2, J3; P4  J2, J3; P5  J3, J4, J5
• Solution : Introduce a source and sink vertex. 

• Airline scheduling : Could the following flights all be accomplished by just


2 planes? (Assume all times are in same time zone)
• BOS-DCA 6:00-7:00; PHL-PIT 7:00-8:00; DCA-LAX 8:00-11:00; PHL-SFO 11:00-
14:00; SFO-SEA 14:30-15:30; SLC-SEA 17:00-18:00;

Flow Networks ‹#›


STRING MATCHING
Explore it on NEXT DAY

Flow Networks ‹#›

You might also like