0% found this document useful (0 votes)
29 views38 pages

Week12 Chap5 MaxFlow Max Matching Hungarian

Uploaded by

Ha Minh Duc
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
29 views38 pages

Week12 Chap5 MaxFlow Max Matching Hungarian

Uploaded by

Ha Minh Duc
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 38

APPLIED ALGORITHMS

APPLIED ALGORITHMS
Maximum Flow and Maximum Matching on the Bipartite Graph

3
CONTENTS

• Maximum flow (Luồng cực đại)


• Maximum matching on the Bipartite Graph (Cặp ghép cực đại trên đồ
thị hai phía)

4
MAXIMUM FLOW

• Definition of network
• Flow (luồng) and maximum flow problem (bài toán luồng cực đại)
• Cut (lát cắt) and minimum cut problem (bài toán lát cắt cực tiểu)
• The relationship between the maximum flow and minimum cut
problems
• Ford-Fulkerson algorithm
• Edmond-Karp algorithm

5
Network (Mạng)

• Network (mạng): Network is a directed graph 𝐺 = (𝑉, 𝐸) :


• There is only one vertex s without an incoming arc called the source vertex and
only one vertex t without an outgoing arc called the destination vertex.
• Each arc e of G is associated with a non-negative number c(e) called the
capacity of e.
• Example:
v 3
6
1
7 t
s 3
w
1 9 5
5
u z
2

6
Flow (luồng)

• Flow 𝑓 in the nework 𝐺 = (𝑉, 𝐸) is the assignment of the number 𝑓(𝑒) to


each edge 𝑒 (value 𝑓(𝑒) (giá trị 𝑓(𝑒) is called a flow on edge 𝑒 that
satisfies the following two conditions:
• 1) Capacity rule (Hạn chế về khả năng thông qua):
0  𝑓 𝑒  𝑐 𝑒 , ∀𝑒 ∈ 𝐸 v 1/ 3
2/ 6
1/ 1
??/
• 2) Conservation rule (Điều kiện cân bằng luồng): 3/ 7 t
s 3/ 3
w
෍ 𝑓 𝑒 = ෍ 𝑓 𝑒 , ∀ 𝑣 ∈ 𝑉, 𝑣 ≠ 𝑠, 𝑣 ≠ 𝑡 1/1 2/ 9
??/ 4/ 5
??/
3/5
𝑒∈𝐸 − (𝑣) 𝑒∈𝐸 + (𝑒)
u z
where 𝐸 − (v) và 𝐸 + (𝑣) are the set of arcs entering and leaving vertex 2/ 2
??/
𝑣, respectively.
• The value of flow f is σ𝑒∈𝐸+ (𝑠) 𝑓 𝑒 = σ𝑒∈𝐸+ (𝑡) 𝑓(𝑒)

7
The maximum flow problem

v
• Maximum flow problem: On a network 2/6
1/3

with many flows, find the one with the 1/1


3/7 t
largest value among the flows. s 3/3
w
1/1 2/9 4/5
3/5
u z
2/2
Flow with value of 8 = 2 + 3 + 3 = 1 + 3 + 4
v 3/3
4/6
1/1 t
3/7
s 3/3
w
1/1 2/9 4/5
3/5
u z
2/2
Maximum flow with value of 10 = 4 + 3 + 3 = 3 + 3 + 4

8
Cut

• Cut is a way of partitioning the vertex set of a graph into two sets S and T such
that s  S, t  T.
• The capacity (khả năng thông qua) 𝐶𝑎𝑝(𝑆, 𝑇) of the cut 𝑆, 𝑇 is defined 𝐶𝑎𝑝 𝑆, 𝑇 =
σ 𝑢,𝑣 ∈𝐸,𝑢∈𝑆,𝑣∈𝑇 𝑓(𝑢, 𝑣)

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

9
Minimum cut (Lát cắt cực tiểu)

• Cut is a way of partitioning the vertex set of a graph into two sets S and T such that s  S, t  T.
• Capacity 𝐶𝑎𝑝(𝑆, 𝑇) of the cut 𝑆, 𝑇 is defined as 𝐶𝑎𝑝 𝑆, 𝑇 = σ 𝑢,𝑣 ∈𝐸,𝑢∈𝑆,𝑣∈𝑇 𝑓(𝑢, 𝑣)
• The minimum cut problem: Among the cuts, find the cut with the smallest capacity

2 9 5

Example 2
10 15 15 10
4

s 5 3 8 6 10 t

4 6 15 10
15

cap(S2,T2) = 28
4 30 7

10
The relationship between max flow and min cut problems

• Lemma 1: Suppose f is a flow and (S, T) is the cut. Then


eS →T
f (e) − 
eT → S
f (e) =  +
f (e) = val ( f )
eE (s )

• Lemma 2. Suppose f is flow, and (S, T) is the cut. Then


𝑣𝑎𝑙 𝑓 ≤ 𝑐𝑎𝑝(𝑆, 𝑇)
• Corollary. Suppose f is flow, and (S, T) is the cut. If val( f ) = cap(S, T), then f is the
max flow and (S, T) is the min cut.
• Theorem (Ford-Fulkerson, 1956): In any network, the value of the maximum flow
is always equal to the capacity of the min cut.

11
Residual graph (đồ thị tăng luồng)

Given network G = (V, E). cap


• Edge e = (v, w)  E
• Flow f (e) v 17 w
• Capacity c(e) 6
flow

e = (u,v)  eR = (v,u)
Residual graph: Gf = (V, Ef ).
• Ef = {e: f(e) < c(e) }  {eR : f(e) > 0 }
cap
• Capacity of edge

c(e) − f (e) if e  E v 11 w
c f ( e) =  6
 f (e) if e R  E
cap

12
Residual graph - Example
Residual graph: Gf = (V, Ef ). c(e) − f (e) if e  E
c f ( e) = 
– Ef = {e : f(e) < c(e)}  {e : f(e) > 0}.
R
 f (e) if e R  E
– cf(e) indicates the maximum amount that can increase flow on edge e.
– cf(eR) indicates the maximum amount that can decrease flow on edge e.
4 5
0 (s,4)
0 4 0 (s,2)
G 4
0 4 (2,3)
10 4
10 10 (4,3)
s 10 2 13 3 10 t (2,5)
(3,t)
4 5
(5,t)
4
Gf 4
4
4
s 10 2 10 3 10 t
3

13
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): the maximum amount that can increase flow on edge e.
– cf(eR): the maximum amount that can decrease flow on edge 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 0 X
on new Gf 4 0 4
X (2,5)
G 4
X
4 0 4 (3,t)
4
10 X 6
10 10 (5,t)
s 10 2 13 3 10 t

Example: Augmenting path P: 4 5


cf (P) = 4
s, 4, 3, 2, 5, t 4
Gf 4
4
4
s 10 2 10 3 10 t
3 14
Residual graph - Example

Augmenting path = path from s to t on the residual graph. (s,4)


• Max flow  could not find any augmenting path??? (s,2)
(2,3)
Value of flow = 14 4 5 (4,3)
4
4 (2,5)
4 4
G 4 (3,t)
4 4
4 (5,t)
10 6 10
s 10 2 13 3 10 t

Find augmenting path P??? 4 5

4
Gf 4
4
4
s 10 2 6 3 10 t
7

15
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.

Theorem. Assume f is a flow in network. The following three statements are equivalent
(i) Can find the cut (S, T) such that val( f ) = cap(S, T).
(ii) f is maximum flow.
(iii) Could not find augmenting path to augment value flow f.

16
Ford – Fulkerson algorithm

Ford-Fulkerson algorithm Augmenting flow f along path P


float Ford_Fulkerson(G,c,s,t) float augment(f,P)
{ {
FOR e  E // initialize flow f to 0 b  cf(P)
f(e)  0 FOR e  P
Gf  residual graph for f IF (e  E)
f(e)  f(e) + b
WHILE (there exists an augmenting path P) ELSE
{ f(eR)  f(e) – b
f  augment(f, P) RETURN f
Update residual graph Gf }
}
RETURN f
}

17
Computation time

Question: Is the Ford-Fulkerson algorithm a polynomial algorithm? (algorithm with


computation time is bounded by a fixed degree polynomial of the input length)
• Answer: Not at all. If the maximum capacity is C then the algorithm may have to
do C iterations.
If the capacity of edges is real number, there is an example showing that the Ford-
Fulkerson algorithm is non-stopped.
Zwick built an example to show that the algorithm may not stop, if the capacity is an
irrational number

18
Example: Ford – Fulkerson algorithm does not terminate

▪ Zwick constructs examples showing that the algorithm may not stop, if the
capacity of edges is irrational
s
X X
X
1 1 

X
X X
t

▪ There are 6 arcs with capacity = X, 2 edges with capacity = 1 and one edge with
capacity  = ( 5-1)/2  0.618034...

19
Edmond-Karp algorithm

Be careful when select augmenting path, because


• Several options may lead to an exponential algorithm.
• Smart choice leads to polynomial algorithm.
• If capacity is irrational number, the algorithm may not stop
The goal: select augmenting path such that:
• Can find the augmenting path effectively.
• The algorithm requires as few iterations as possible..

Select augmenting path with


• Maximum capacity. (fat path - đường béo)
• Capacity is large enough. (capacity scaling)
• The number of edges along the way is the least. (shortest path)
Edmond-Karp algorithm Apply BFS

20
CONTENTS

• Maximum flow (Luồng cực đại)


• Maximum matching on the Bipartite Graph (Cặp ghép cực đại trên đồ
thị hai phía)

21
Maximum matching on the Bipartite Graph

• Given a bipartite graph, each side consists of n vertices, find a way to select n
edges of the graph so that each vertex of the graph appears in exactly one selected
edge and the total weight of the selected edges is the largest.

22
Equality Graph (Đồ thị bù) và Augmenting path (Đường mở)

• Given a bipartite graph 𝐺 = (𝑉, 𝐸), where 𝑉 = 𝑆, 𝑇 , 𝑆 = 𝑇 , 𝑆 ∪ 𝑇 = ∅, each


edge 𝑢, 𝑣 ∈ 𝐸 connects a vertex 𝑢 ∈ 𝑆 with a vertex 𝑣 ∈ 𝑇 and has the weight
w 𝑢, 𝑣 , each vertex 𝑢 ∈ 𝑉 has the weight 𝑙𝑢 ; and a matching 𝑀 ⊂ 𝐸.
• Equality graph 𝐺𝑡 = 𝑉, 𝐸𝑡 corresponding to 𝐺 contains all vertices of graph 𝐺, the
edge set 𝐸𝑡 contains edges whose weight is equal to the sum of the weights of the
2 vertices in the edge.
𝐸𝑡 = 𝑥, 𝑦 𝑥, 𝑦 ∈ 𝐸, 𝑙𝑥 + 𝑙𝑦 = 𝑤 𝑥, 𝑦

23
Equality Graph (Đồ thị bù) và Augmenting path (Đường mở)

• Given a bipartite graph 𝐺 = (𝑉, 𝐸), where 𝑉 = 𝑆, 𝑇 , 𝑆 = 𝑇 , 𝑆 ∪ 𝑇 = ∅, each edge 𝑢, 𝑣 ∈ 𝐸


connects a vertex 𝑢 ∈ 𝑆 with a vertex 𝑣 ∈ 𝑇 and has the weight w 𝑢, 𝑣 , each vertex 𝑢 ∈ 𝑉 has the
weight 𝑙𝑢 ; and a matching 𝑀 ⊂ 𝐸.
• Equality graph 𝐺𝑡 = 𝑉, 𝐸𝑡 corresponding to 𝐺 contains all vertices of graph 𝐺, the edge set 𝐸𝑡
contains edges whose weight is equal to the sum of the weights of the 2 vertices in the edge.
𝐸𝑡 = 𝑥, 𝑦 𝑥, 𝑦 ∈ 𝐸, 𝑙𝑥 + 𝑙𝑦 = 𝑤 𝑥, 𝑦
• Alternative path (đường xen kẽ) is a simple path in 𝐺𝑡 containing edges in 𝑀 and not in 𝑀 (𝐸∖𝑀). An
Augmenting path (đường mở) is an alternating path whose start and end vertices are not within 𝑀.

(f, e, d) is (f, e, d, c, b, a) is
alternative augmenting path
path

24
Hungary algorithm

BEGIN
initialize labels on nodes
WHILE matching is not complete DO
find a root of an alternating path
WHILE augmenting path not found DO
try to find augmenting path in equality graph
IF augmenting path not found
THEN update labels
increase matching
END

25
Hungary algorithm- Initialize the weight on the vertex

• Equality graph 𝐺𝑡 = 𝑉, 𝐸𝑡 corresponding to 𝐺 contains all vertices of graph 𝐺, the


edge set 𝐸𝑡 contains edges whose weight is equal to the sum of the weights of the
2 vertices in the edge.
• To calculate the equality graph, the algorithm finds the edges with the highest
weight for each vertex, assigning the weight value of this edge to the vertex.

26
Hungary algorithm – Procedure to Update labels

• Suppose we are trying to find an augmenting path 𝑝 but are unsuccessful, we need
to add edges to the graph 𝐺𝑡 to find an ougmenting path. Suppose 𝑝 is containing 𝑆
which is the set of vertices in 𝑋 and 𝑇 is the set of vertices in 𝑌. Calculate Δ
according to the following formular:

• Update the weights on the vertices according to the following formula before
finding the corresponding equality graph and finding the augmenting path:

27
Example

28
Example

29
Example

30
Example

31
Example

32
Example

33
Example

34
Example

35
Example

36
Reference

• https://algorithms.discrete.ma.tum.de/graph-algorithms/matchings-
hungarian-method/index_en.html

37
THANK YOU !

38

You might also like