Title: MS 101: Algorithms
1MS 101 Algorithms
- Instructor
- Neelima Gupta
- ngupta_at_cs.du.ac.in
2Network Flows
3DOMAIN
- Depict flow of a material (liquid, gas, current)
from source to sink. - Source Material generator.
- Sink Final destination to receive material.
- Flow Rate of flow of material.
- Conduit Medium for material transfer (pipe,
wires). - Capacity Maximum rate at which material can flow
through conduit.
4Goal
- To solve the Maximum Flow problem
- Compute greatest rate at which material can flow
from source to sink subject to specified
constraints.
5TECHNIQUE
- Flow Network
- A directed graph depicting source, conduit, sink
modeling the flow. - Each directed edge represents conduit.
- Vertices are conduit conjunctions through which
material flows but does not collect . - At a vertex , Rate of inflow rate of outflow.
6NOTATIONS AND CONSTRAINTS
- Flow Network G(V,E)
- Edge (u,v) ? E
- Capacity of edge c(u, v) 0
- If (u,v) does not belong to E the c(u,v) 0
- Source s
- Sink t
- Graph connected ie. E V - 1
7- Flow in G a real valued function
- f V X V ? R satisfying constraints
- Capacity Constraint For all u,v ? V, f(u,v)
c(u,v) - Flow from one vertex to another must not
exceed given capacity. - Skew Symmetry For all u,v ? V, f(u,v) -f(v,u)
- Flow from a vertex u to a vertex is the
negative of the flow in the reverse direction. - Flow Conservation For all u ? V- s,t
- Sf(u,v) 0
- v ? V
8- f(u,v) Flow from u to v.
- It can be either positive , negative or zero.
- Value of flow f Sf(u,v) 0 Total flow
out of source. v ? V - Total flow out of a vertex V ? u, v 0
- By Skew symmetry Sf(u,v) 0
-
u ? V - for all v ? V- s,t ie.. total flow into
vertex 0. - Total net flow at a vertex
- Total positive flow leaving a vertex Total
positive flow entering a vertex . - So by Flow Conservation property total net
flow at a vertex 0
9Flow Network
SINGLE SOURCE SINGLE SINK
14
1
2
19
18
T
S
5
4
15
20
3
4
16
S
SOURCE
SINK
I
J
K
K is Capacity
T
10MULTIPLE SOURCE MULTIPLE SINK
10
s1
12
3
15
s2
t1
5
8
6
14
s3
20
t2
7
13
s4
11
18
t3
2
s5
11 To solve multiple source multiple sink problem
- Add a supersource s and directed edge (s,si) with
c(s,si) 8 for each i1,2,.,m - Add a supersink t and directed edge (ti,t) with
c(ti,t) 8 for each i 1,2,.,n
12s1
10
8
3
12
t1
s2
15
5
8
8
6
8
T
8
S
8
t2
s3
20
14
8
8
13
7
8
t3
s4
18
11
2
s5
13Ford Fulkerson Method
- Instructor Ms Neelima Gupta
14- Ford_Fulkerson(G,s,t)
- 1. initialize flow f to 0
- 2. while there exists augmenting path p
- 3. do augment f along p
- 4. return f
15RESIDUAL NETWORK
- RESIDUAL CAPACITY OF (u,v) cf
- The amount of additional flow that can be
pushed from u to v before exceeding c(u,v) . - cf c(u,v) f(u,v)
-
- Eg - c(u,v)10, f(u,v)5 then flow can
be increased by 5 units ie.. residual capacity
5. - RESIDUAL NETWORK OF G INDUCED BY f , Gf
- Gf (V,Ef)
- where Ef (u,v) ? V X V cf(u,v) gt 0
- ie. Each edge of the residual network or
residual edge can admit flow gt 0. -
16AUGMENTING PATHS
- AUGMENTING PATH ( P)
-
- Path from s to t in residual network Gf .
- The augmenting path admits additional
positive flow from u to v within capacity
constraint. - The maximum amount by which flow can be
increased on each edge in P is residual capacity
of P, cf mincf (u,v) (u,v) is on P. - Example if cf (u,v)3 then the flow through each
edge of p can be increased by 3 without violating
capacity constraint.
17ALGORITHM
- FORD_FULKERSON(G,s,t)
- for each edge (u,v) ? EG
- do fu,v? 0
- fv,u? 0
- while there exists a path from s to t in Gf
- do cf (p)?minc(u,v) (u,v) is in p
- for each edge (u,v) in p
- do fu,v? fu,v cf (p)
- fu,v? - fu,v
18- DESCRIPTION OF ALGORITHM
- 1-3 initialise flow f to 0.
- 4-8 finds augmenting path p in residual network
and augments flow f along p by residual capacity
cf (p). - When no augmenting path exists flow f is maximum.
19EXECUTION OF BASIC FORD-FULKERSON ALGORITHM
14
1
2
19
18
T
S
5
4
15
20
3
4
16
S
SOURCE
SINK
I
J
K
T
K is Capacity
2014
1
2
19
18
T
S
5
4
15
20
3
4
16
MINIMUM CAPACITY IN THE PATH 4
21RESIDUAL GRAPH
C / F Capacity / Flow
14
1
2
19
14 / 4
4
4
T
S
5
0
11 / 4
4
12 / 4
20
3
4
4
a/b
a original capacity - minimum capacity of the
selected path b minimum capacity of the
selected path c b
c
22PATH CHOSENS 1 2 4 T
14
1
2
19
14 / 4
T
S
5
0
11 / 4
12 / 4
20
3
4
MINIMUM CAPACITY IN THE PATH 5
23RESIDUAL GRAPH
C / F Capacity / Flow
5
1
2
19
9 / 5
9 / 9
9
T
5
S
0/ 5
0/4
6 / 9
9
12 / 4
20
3
4
24PATH CHOSEN S 1 2 T
1
2
19
9 / 5
9 / 9
T
S
0/ 5
0 / 4
6 / 9
12 / 4
20
3
4
MINIMUM CAPACITY IN THE PATH 9
25RESIDUAL GRAPH
C / F Capacity / Flow
14
1
2
9
0/ 14
0 / 18
10 / 9
18
T
S
0/ 5
0/4
6 / 9
12 / 4
20
3
4
26PATH CHOSEN S 3 4 T
1
2
10 / 9
0 / 14
0 \ 18
T
S
0/ 5
0 / 4
6 / 9
12 / 4
20
3
4
MINIMUM CAPACITY IN THE PATH 6
27RESIDUAL GRAPH
C / F Capacity / Flow
1
2
10 / 9
0 / 14
0 \ 18
T
S
0/ 5
0 / 4
6
0 / 15
15
6 / 10
14 / 6
3
4
10
28PATH CHOSEN S 3 4 2
T (BACKTRACKING)
1
2
10 / 9
0 / 14
0 \ 18
T
5
S
0/ 5
0 / 4
0 / 15
14 / 6
6 / 10
3
4
MINIMUM CAPACITY IN THE PATH 5
29RESIDUAL GRAPH
C / F Capacity / Flow
1
2
0 / 14
5 / 14
0 \ 18
14
T
S
0
0 / 4
5
11
0 / 15
1 / 15
9 / 11
3
4
15 / 0
Now the graph has no augmenting path ,
therefore the flow shown is the maximum flow.
Maximum Flow - 1415 29
30CUTS OF FLOW NETWORK
- Flow is maximum iff its residual network has no
augmenting path. - CUT(s,t) OF FLOW NETWORK G A partition of V
into S and - TV - S such that s ? S and t ? T .
- It is a collection of edges separating the
network into S and T. - If f is flow ,then the net flow across the cut is
f(S,T) and capacity is c(S,T). - A minimum cut of network is cut with minimum
capacity over all cuts of the network. -
31CUT
1
2
0 / 14
5 / 14
0 \ 18
14
T
S
0
0 / 4
5
11
0 / 15
1 / 15
9 / 11
3
4
32OPTIMALITY
- MAX FLOW MIN CUT THEOREM
- 1.f is maximum flow in G
- 2.Gf contains no augmenting paths
- 3.f c(S,T) for some cut(S,T) of G
- If the flow f reported by the algorithm is
optimal then there exists a cut C on which
residual capacity of cut 0. - The initial capacity of C must have been f and
must have been utilised completely and hence
residual capacity 0. - C is a minimum cut since if another cuts
capacity is less than Cs then it would not be
possible to send f flow. - Optimality is achieved since otherwise some
augmenting path would be present with capacity at
least one.
33ANALYSIS OF THE ALGORITHM
- Running time of the algorithm is O(f X time to
compute augmenting path). - 1-3 take O(E) time.
- While loop is executed at most f times since
flow value increases by at least one unit in each
iteration. - Time to compute an augmenting path using BFS or
DFS is O(E). - Each iteration of the while loop hence takes O(E)
time. - Hence total running time of FORD FULKERSON is O(E
f).
34A DEEPER SIGHT INTO ANALYSIS
- f can be very big as it depends on capacities.
- f could be as big as Sc(u,v).
- Since it depends on the values of capacities so
it is not polynomial in input size which is Slog
ci E V - where cis could be large.
35- Running Time depends on how the augmenting path p
is determined. - If capacities are small and f is small running
time is good. - Example illustrates effect on time with large
f.
36- Here max flow 400.
- But if the first augmenting path is
- s-gtv1-gtv2-gtt, flow would be one and then
choosing s-gt v2--gt v1- gtt in even numbered
iterations and s-gtu-gtv-gtt in odd ones increases
flow by just one in each iteration(total of 400
iterations) and perform a total of 400
augmentations.
37Edmonds-Karp Algorithm
38 Edmonds Karp Algorithm
- Choose the shortest augmenting path from s to t
in residual network. - Path is shortest in terms of no. of edges.
- Critical edge - minimum weight edge of an
augmenting path.
u
t
s
v
39Notations
- Initial graph - G
- Flow in G form s to u f
- Residual graph Gf
- f(u,v) - shortest path distance from u to v in
Gf - Flow in residual graph f1 (computed as shortest
path over Gf )
40 f1
u
- As f is the shortest distance from s to u so
- f lt f1 and
- f(s,u) lt f1(s,u) V u.
t
s
u
v
f
41Critical Edge
- If (u,v) is critical edge along augmenting path
then - f(s,v) f(s,u) 1 (edge)
- f(s,v) in Gf increases monotonically with each
flow augmentation. - Once the flow is augmented, edge (u,v) disappears
from the Gf.
s
t
u
v
42When (u,v) is not present in augmenting path
u
s
t
v
43For having (u,v) back in augmenting path
- edge (v,u) must be present on augmenting path
- (v,u) need not to be critical
u
s
t
v
44- So,
- f1(s,u) f1(s,v) 1
-
- f(s,v) f(s,u) 1
- f(s,v) lt f1(s,v)
- Therefore, f1(s,u) f1(s,v) 1
- gt(f(s,v) 1)
- f(s,u) 2
45Analysis
- In a critical edge (u,v) , Distance of u from s
initially is at least 0. - Every time (u,v) edge becomes critical again
distance of u from s increased by at least 2. - Intermediate vertices on shortest path from s to
u cant contain s,u,t. - (since (u,v) on the critical path means s !
t). - So until u becomes unreachable from s its
distance is at most IVI-2. - So every (u,v) edge can become critical at most
(IVI-2)/2 times.
46- Pair of vertices that can have edge in Gf O(E)
- Each edge can become critical O(V) time.
- Total no. of critical edges O(VE).
- Each iteration of Ford Fulkerson can be
implemented in O(E) time when augmenting path is
found by BFS so - total running time of Edmonds Karp O(VE2).
47Maximum bipartite matching
48- Problem- There is a set T of teachers with a set
C of courses. A teacher can
teach only some set of courses. Assign 1
teacher to 1 course. -
- T
C -
t1 t2 . . . . . . tm
c1 c2 .c3 . . . . . cn
49- Each teacher is to be assigned at most one course
and each course is to be assigned at most one
teacher. - This is a bipartite graph
- Let teachers be 2 and courses be 3.
-
t1 t2
c1 c2 c3
50- Convert the problem into flow network problem in
which flows corresponds to matchings. - Let source s and sink t be new vertices. Draw an
edge from source s to each vertices in T and from
each vertices in C to sink t. - Capacity of each edge from s to T, T to C, C to t
is 1. - In the end, each unit of flow will be equivalent
to a match between a teacher and a course, so
each edge will be assigned a capacity of 1.
1
t
51 The corresponding flow network
1
c1 c2 c3
1
1
t1 t2
1
t
1
s
1
1
1
1
52- Now if we assign t1 to c1, then t2 will not get
any course and c2, c3 are also not assigned to
any teachers. - Therefore we select a subset E of edges such
that no two edges resides on same vertex. We want
to find the maximum edges. - Q- Why capacity from s to T cannot be taken
more than 1? - Ans- If capacity from s to T is more than 1
then any vertex in T would be selected more than
1 time and matched more than once.
53- Select the path s-t1-c1-t.
1
0/1
c1 c2 c3
0/1
t1 t2
0/1
1
1
t
1
s
1
1
1
54- Now t2 is not having any course. But capacity
from s to t2 is 1. Therefore we do backtracking. - Now the new path is s-t2-c1-t1-c2-t.
1
1
c1 c2 c3
0/1
0/1
t1 t2
t
0/1
0/1
s
1
0/1
1
0/1
55- Now t2 is assigned c1 and t1 is assigned to c2.
- Flow from t1to c2 is 1, t2 to c1 is 1.
- But residual capacity from t1 to c2 is 0 and t2
to c1 is 0. - Total network flow is 2.
- Therefore teacher t1 is matched with course c2
and teacher t2 is matched with course c1.