Professional Documents
Culture Documents
Discrete Mathematics
PART 2
GRAPH THEORY
(Lý thuyết đồ thị) 2
Content of Part 2
Chapter 1. Fundamental concepts
Chapter 2. Graph representation
Chapter 3. Graph Traversal
Chapter 4. Tree and Spanning tree
Chapter 5. Shortest path problem
Chapter 6. Maximum flow problem
Chapter 6
maximum flow problem
(Bài toán luồng cực đại trong mạng)
c
v 3/3
4/6
1/1 4/7 t
s 3/3
w
1/1 1/9 3/5
3/5
u z
2/2
Contents
1. Problem description and applications
2. Cut (Lát cắt)
3. Residual graph and Augmenting path
4. Ford-Fulkerson algorithm
5. Edmond-Karp algorithm
6. Some applications
Network (Mạng)
Network is a directed graph G = (V, E):
– There is only one vertex s without any incoming arcs,
called source vertex (đỉnh phát/nguồn) and only vertex t
without any outgoing arcs called target vertex (đỉnh
thu/đích).
– Each edge e of G is assigned a nonnegative value c(e)
which is called capacity of e.
Example:
v 3
6
1
7 t
s 3
w
1 9 5
5
u z
2
Flow in network (Luồng trong mạng)
Definition. Flow f in network G=(V,E) is to assign value f(e) on each edge e
(f(e) is flow on edge e) so that following conditions are satisfied:
1) Capacity rule:
E-(v) and E+(v) are sets of arcs entering and leaving vertex v, respectively.
v 1/ 3
2/ 6
Conditions 1) and 2) are satisfied 1/ 1
??/ t
3/ 7
s 3/ 3
=> f is flow in network w
1/1 2/ 9
??/ 4/ 5
??/
3/5
u z
2/ 2
??/
Flow in network (Luồng trong mạng)
Definition. Flow f in network G=(V,E) is to assign value f(e) on each edge e
(f(e) is flow on edge e) so that following conditions are satisfied:
1) Capacity rule:
Goods,
Traffic airport, station highways, flights
customers
cap ( S , T )
eS T
c (e),
where S T : {(v, w) E : v S , w T }
Min cut is the cut having minimum capacity.
S = {s}
Example 1 2 9 5
T = {t,2,3,4,5,6,7}
10 Cap(S, T) = 10+5+15=30
4 15 15 10
S
s 5 3 8 6 10 t
4 6 15 10
15
4 30 7
Cut
Cut is the way to partition vertex set of the network into 2 disjoint sets S and T with s S, t T.
– Capacity (khả năng thông qua) cap(S,T) of the cut (S, T) is:
cap ( S , T )
eS T
c (e),
where S T : {(v, w) E : v S , w T }
S = {s, 2, 3, 4}
Example 2 2 9 5
T = {t, 5, 6, 7}
Cap(S, T) = 62
10 15 15 10
S 4
s 5 3 8 6 10 t
4 6 15 10
15
4 30 7
Cut
Example 3:
Cut (S2, T2), S2={s,3,4,7}, T2={2,5,6,t}
has the capacity = 28
2 9 5
10 15 15 10
4
s 5 3 8 6 10 t
4 6 15 10
15
cap(S2,T2) = 28
4 30 7
Flow through the cut
Definition. Assume f is a flow in network and (S,T) is a cut. We call value of flow
through the cut (S,T) is the value
eS T
f (e )
eT S
f (e ) 6+0+8+10 0
0 10
4 0 6 15 0 10
15
10
4 30 7
10
Flow through the cut (Luồng chảy qua lát cắt)
Definition. Assume f is a flow in network and (S,T) is a cut. We call value of flow
through the cut (S,T) is the value
eS T
f (e )
eT S
f (e ) 10+8+10 4
0 10
4 0 6 15 0 10
15
10
4 30 7
10
What is Cap(S,T) and Flow(S,T)
c f i
20/20 10/10
Flow and cut
Lemma 1. Assume f is flow, and (S, T) is a cut. Then
Flow through this cut is equal to the value of flow f:
f (e ) f (e ) f (e ) f (e) val ( f )
eS T eT S eE (s ) eE (t )
Flow and cut
Lemma 1. Assume f is flow, and (S, T) is a cut. Then flow through this
cut is equal to the value of flow f:
f (e ) f (e ) f (e ) f (e) val ( f )
eS T eT S eE (s ) eE (t )
Example 1: 6 S = {s,2, 3, 4}
2 9 5 T = {5, 6, t}
6+0+8+10 - 0
10 6
Value = 24 0
10 15 15 0 10
4 4
4 8 8
s 5 3 8 6 10 t
0 10
4 0 6 15 0 10
15
10 =10+4+10
4 30 7 =6+8+10
10
Value = 24
Flow and cut
Lemma 1. Assume f is flow, and (S, T) is a cut. Then flow through this
cut is equal to the value of flow f:
f (e ) f (e ) f (e ) f (e) val ( f )
eS T eT S eE (s ) eE (t )
Example 2 6
2 9 5 S = {s,3,4,7}
10 T = {2,5,6,t}
0 6
Value = 24 10 15 15 0 10
4 4
4 8 8
s 5 3 8 6 10 t
0 10
4 0 6 15 0 10
15
10
4 30 7
10
Flow and cut
Lemma 1. Assume f is flow, and (S, T) is a cut. Then flow through this
cut is equal to the value of flow f:
f (e) f (e) f (e) val ( f )
eS T eT S eE (s )
Proof: Sum of all flow conservation condition for all vertex vS:
0 ( f (e) f (e))
vS
eE ( v ) eE ( v )
w
f ( e ) f ( e) f ( e )
eS T u v t
eE ( s ) eT S
eS T
c (e )
s
7
cap(S , T ) 6
Max Flow and Min Cut (Luồng cực đại và lát cắt nhỏ nhất)
Corollary. Assume f is a flow, (S, T) is a cut. If val( f ) = cap(S, T), then
f is maximum flow and (S, T) is minimum cut.
The flow in network G is called maximum flow if among all Minimum cut: the cut with minimum capacity
flows in G, its value is maximum
f ’ is arbitrary
Lemma flowf is a flow, and (S, T) is a cut. Then
2. Assume f is max flow
val( f’ ) val (f ) val( f ) cap(S, T)
9
2 9 5
10 9
1
10 15 15 0 10
4 0
4 8 9
s 5 3 8 6 10 t
4 10
4 0 6 15 0 10
15
14
4 30 7
14 Flow value = 28
Cut capacity = 28
val( f ) = 10+4+14 = 9+9+10=28
Value of flow f is val ( f )
f ( e) f (e )
eE ( s ) eE ( t )
Cut is the way to partition vertex set into 2 disjoint sets S and T with s S, t T.
– Capacity cap(S,T) of cut (S, T) is value: Min cut is the cut with min capacity.
cap ( S , T )
eS T
c(e),
9
where S T : {(v, w) E : v S , w T } 2 9 5
10 9
- Flow through the cut (S,T) is the value 151 15
0 10
10 40
eS T
f (e )
eT S
f ( e) s 5 3
8
8 6
9
10 t
4
4 0
where: S T = {(v,w)E: vS, wT} 15 40 6 15 10
10
T S = {(v,w)E: vT, w S}
14 4 30 7
14
Lemma 1. Amount of low through the cut is equal to value of flow:
where S T : {(v, w) E : v S , w T } 9
2 9 5
10 9
- Flow through the cut (S,T) is the value 151 15
0 10
10 40
eS T
f (e )
eT S
f ( e) s 5 3
8
8 6
9
10 t
4
4 0
where: S T = {(v,w)E: vS, wT} 15 40 6 15 10
10
T S = {(v,w)E: vT, w S}
14 4 30 7
14
Lemma 1. Amount of low through the cut is equal to value of flow:
where S T : {(v, w) E : v S , w T } 9
2 9 5
10 9
- Flow through the cut (S,T) is the value 151 15
0 10
10 40
= 28
eS T
f (e )
eT S
f ( e) s 5 3
8
8 6
9
10 t
4
4 0
where: S T = {(v,w)E: vS, wT} 15 40 6 15 10
10
T S = {(v,w)E: vT, w S}
14 4 30 7
14
Lemma 1. Amount of low through the cut is equal to value of flow:
Cut is the way to partition vertex set into 2 disjoint sets S and T with s S, t T.
– Capacity cap(S,T) of cut (S, T) is value: Min cut is the cut with min capacity.
cap ( S , T )
eS T
c(e),
where S T : {(v, w) E : v S , w T } 6
2 5
9
- Flow through the cut (S,T) is the value 10 6
10 15
0 15 10
44 0
f (e ) f ( e)
s
4
5 3
8
8
8
t
eS T eT S 6 10
60 0
where: S T = {(v,w)E: vS, wT} 15 40 15 10
10 10
T S = {(v,w)E: vT, w S} 4 7
30
10
Lemma 1. Amount of low through the cut is equal to value of flow:
where S T : {(v, w) E : v S , w T } 6
2 5
9
- Flow through the cut (S,T) is the value 10 6
10 15
0 15 10
44 0
=10+8+10 -4 f (e ) f ( e) 4
5 3
8
8
8
=24 eS T eT S s 6 10 t
60 0
where: S T = {(v,w)E: vS, wT} 15 40 15 10
10 10
T S = {(v,w)E: vT, w S} 4 7
30
10
Lemma 1. Amount of low through the cut is equal to value of flow:
4 5
0
0 4 0
4
0 4
0 10 4 0 10
0 10
s 10 2 13 3 10 t
Value
Valueof
offlow
flow==10
0
Greedy algorithm
Greedy algorithm does not provide optimal solution.
4 5
0 Greedy algorithm:
0 4 0
4
Value of flow = 10
0 4
10 4
10 10
s 10 2 13 3 10 t
Optimal:
4 5 Value of flow = 14
4
4 4 4
4
4 4
10 4
6 10
s 10 2 13 3 10 t
Residual graph (Đồ thị tăng luồng)
capacity
Network G = (V, E).
– Arc e = (v, w) E v 17 w
– Flow f (e) 6
flow
– Capacity c(e)
e = (u,v) eR = (v,u)
Residual graph: Gf = (V, Ef ).
– Ef = {e: f(e) < c(e) } {eR : f(e) > 0 } capacity
– Capacity of each arc e
v 11 w
c(e) f (e) if e E 6
c f (e)
f (e) if e R E flow
Residual graph - Example
Residual graph: Gf = (V, Ef ). c(e) f (e) if e E
c f ( e)
– Ef = {e : f(e) < c(e)} {eR : f(e) > 0}. f ( e) if e R E
– cf(e) indicates the maximum amount that can increase flow on arc e.
– cf(eR) indicates the maximum amount that can decrease flow on arc e. (s,4)
4 5 (s,2)
0
0 (2,3)
4 0
G 4 (4,3)
0 4
(2,5)
10 4 10
10 (3,t)
s 10 2 13 3 10 t
(5,t)
4 5
4
Gf 4
4
4
s 10 2 10 3 10 t
3
Residual graph - Example
Residual graph: Gf = (V, Ef ).
– Ef = {e : f(e) < c(e)} {eR : f(e) > 0}.
– cf(e) indicates the maximum amount that can increase flow on arc e.
– cf(eR) indicates the maximum amount that can decrease flow on arc e.
(s,4)
Augmenting path = path from s to t on the residual graph Gf.
(s,2)
Capacity of the path: cf (P) = min {cf (e): e P }.
(2,3)
Build new Gf 4 5
Find augmenting path
4 X
0 (4,3)
4 X
0 4 (2,5)
On new Gf 0 4
X
G 4 (3,t)
4 X
0 4
10 4 (5,t)
X 6
10 10
s 10 2 13 3 10 t
4
Gf 4
4
4
s 10 2 6 3 10 t
7
Theorem about maximum flow and minimum cut
Augmenting path Theorem (Ford-Fulkerson, 1956): Flow is maximum if and
only if there does not exist augmenting path on network.
Max flow and min cut Theorem (Ford-Fulkerson, 1956): the maximum
possible flow in a network (from source to sink) is exactly equal to the
minimum capacity of all possible cuts.
– Assume (iii) f is flow and Gf does not contain any path from s to t.
– Let S the set of vertices reached from s in Gf
– From this definition about s S, and assumption (iii) t S
– Therefore, network flow f on each each eG must be
f(e) = 0, e TS,
S T
f(e) = c(e), e ST
– Then t
val ( f )
eS T
f (e)
eT S
f ( e)
s
eS T
c ( e)
The following example shows how the algorithm can take a lot of
iteration
Ford – Fulkerson algorithm is not polynomial algoirthm
G 4 Gf 4
1 0
X 0 1
X
109 109 109 109
s 1 0
X X
1 0 t s 1 t
s
X X
X
1 1
X
X X
t
There are 6 arcs with capacity = X, 2 arcs with capacity = 1 and one
arc with capacity = = ( -1)/2 0.618034...
Assumption: All capacities on arcs are integers in the range [0, C].
Immutability: Each value of flow value f(e) and capacity cf (e) are always
integer during algorithm execution.
Theorem: F-F algorithm stops after not more than val( f *) nC
iterations.
Proof.
After each augmenting flow, value of flow is augmented by at least 1
0 0 0
s 10 3 9 4 10 t
Value of flow = 0
Build the residual graph Gf
53
Edmonds-Karp Algorithm
0
flow
2 4 5
capacity
G: 8 X
0 0 8
X 0
10 2 0 8 60 10
0 0 8 X
0
s 10 3 9 4 10 t
Value
Giá trịof flow=
luồng = 08
Augmenting path P: s, 2, 4, t
Augment flow along P 2 4 5
capacity
Gf:
10 2 8 6 10
s 10 3 9 4 10 t
Edmonds-Karp Algorithm
X 2
0
2 4 5
G: 10 8
X X
0 2
8
10 2 0 8 60 10
0 0 8
s 10 3 9 4 10 t
(2, 3) Gf: 8
(2, 5) 2 2 8 6 10
(2, 4)
(3, 4) s 10 3 9 4 2 t
(5, t) 8
(4, 5)
(4, t)
Edmonds-Karp Algorithm
2
2 4 5
G: 10 8 2
10 2 0 8 60 10
0
X 2 0 2
X 10 8
X
s 10 3 9 4 10 t
Gf: 2
10 2 8 6 8
s 10 3 9 4 2 t
8
Edmonds-Karp Algorithm
2
2 4 5
G: 10 8 2 8
X
10 2 0 8 6X
0 10
6
2 8
X X 8
2 10
s 10 3 9 4 10 t
Gf: 2
10 2 8 6 8
s 8 3 7 4 10 t
2 2
Edmonds-Karp Algorithm
X
2 3
2 4 5
G: 10 X 7
8 X
8 9
10 2 0 8 66 10
X
8 9 8 9
X 10
s 10 3 9 4 10 t
Gf: 8
10 2 8 6 2
s 2 3 1 4 10 t
8 8
Conclusion: In graph Gf, S = set of vertices reached from s by BFS(s)
- Min cut (S, T): S = {s, 3}; T = {2,4,5,t}
- Max flow: val ( f ) = 10 + 9 = 19
3 val ( f ) f (e ) f (e)
2 4 5
eE ( s )
eE ( t )
G: 10 7 9
10 2 0 8 66 10
9 9 10
s 10 3 9 4 10 t
Gf: 9
1
10 2 7 6 1
s 1 3 9 4 10 t
9
Exercise
Find the max flow by using Edmonds-Karp algorithm
2 9 5
10 4 15 15 10
s 5 3 8 6 t
10
source sink
4 6 15 10
15
4 30 7
Applications
• Some general flow problems:
– The problem with multiple sources and sinks
– The problem with limited capacities at nodes
• Airline scheduling
• Image segmentation
• Bipartite matching
• Data mining
• Project selection
• Network connectivity
• …
Network with multi-sources and multi-sinks
Network G has p sources s1, s2,..., sp with capacities a1, a2, ..., ap
and q sinks t1, t2,..., tq with capacities là b1, b2, ..., bq
Assume that flow can go from any source to all sinks
Find the max flow from sources to sinks
s1 t1
s2 t2
sp tq
Network with multi-sources and multi-sinks
• Add a supersource s and a supersink t and edges connecting s to all
sources, edges connecting all sinks to t
• Capacity of arc (s, si) is equal to capacity of si
• Capacity of arc (ti, t) is equal to capacity of ti
The problem reduces to the single source single sink network
s1 t1
a1 b1
s2 t2 t
s a2 b2
ap bq
sp tq
The problem with limited capacity at node
Network G, besides capacity on each arc c(u, v), at each vertex vV: there is a
capacity of vertex, denoted as d(v), and require that sum of ingoing flow in each
vertex v is not exceed the value d(v):
f(w,v) d(v)
wV
du
5
u 4 dt
ds
s 1
t
2 3
v
dv
Find max flow from s to t in network G
The problem with limited capacity at node
Contruct network such that:
each vertex v of G corresponds to 2 vertices and of ,
each arc (u,v) of G corresponds to arc ( , ) of ,
each arc ( , ) of has capacity of d(v), which is equal to capacity of vertex v in G.
(s,u) (s,v) (u,t) (v,t) (u,v)
du
du u+ u-
5 4
5
u 4 dt
ds ds dt
1 t+ t-
s 1
t s+ s-
2 3 dv
v 2
v+ v-
3
dv
The original problem reduces to the maximum flow problem in
The delivery problem
warehouses Retail agents
6 1
5 6 6
2
7 7
4 3
8 6
5 4
9 5
4 5
Is there a way to delivery goods from the warehouses that meet the
requirements of the retailers?
Reduces to maximum flow problem
warehouses retailers
6 1
6
5 6 6 The total demands
5 2
of all retailers is 24
4 7 7
s 4 3
t
5
8 6
5 4
4
9 5
4 5
• We can use a single plane for flight i and later for flight j if:
– Di = Oj and there is enough time to perform maintenance on the plane between the two
flights, or
– We can add a flight that take plane from Di to Oj if there is enough time for maintenance
Flight j is called reachable from flight i if these two flights can use the same plane and one of
two constraints above is satisfied.
Input: graph with directed arc (i, j) if flight j is reachable from flight i
Directed Acyclic graph
Goal: schedule all m flights using at most k planes
Image segmentation
• In the image segmentation problem, the input is an image, and we would
like to partition it into background and foreground.
Example:
Image segmentation
• The input is a bitmap on a grid where every grid node represents a pixel.
We covert this grid into a directed graph G, by interpreting every edge of
the grid as two directed edges. See the figure below to see how the
resulting graph looks like.
Image segmentation
The input for the problem is as follows:
• A bitmap of size N × N, with an associated directed graph G = (V, E).
• For every pixel i, we have a value fi ≥ 0, which is an estimate of the likelihood
of this pixel to be in foreground (i.e., the larger fi is the more probable that it is
in the foreground)
• For every pixel i, we have (similarly) an estimate bi of the likelihood of pixel i
to be in background.
• We want the foreground/background boundary to be smooth:
– For every two adjacent pixels i and j we have a separation penalty pij , which
is the “price” of separating i from j (for placing one of them in the
foreground and the other in the background). This quantity is defined only
for adjacent pixels in the bitmap.
Image segmentation
Input: Pixel graphs G(V, E), likelihood functions f, b : V → , penalty
function p : E →
Output: Optimum labelling: partition V( the set of the pixels) into two
sets F and B that maximize
Project selection
You have a small company which can carry out some projects out of a set
of projects P:
• Associated with each project i P is a revenue pi , where pi > 0 is a
profitable project and pi < 0 is a losing project.
• There is dependency between projects: one has to complete some
“infrastructure” projects before one is able to do other projects.
Namely, you are provided with a graph G = (P, E) such that (i → j)
E if and only j is a prerequisite for i.
Goal: Select a feasible set of projects maximizing the overall profit.