[ACM-ICPC] Matching

Preview:

Citation preview

Matching

陳鵬宇 (float)<float.tw@gmail.com>

2

Rights to CopyAttribution-ShareAlike 3.0

You are free:

– to Share — to copy, distribute and transmit the work

– to Remix — to adapt the work

– to make commercial use of the work

● Under the following conditions:

– Attribution — You must attribute the work in the manner specified by the author or licensor (but not in any way that suggests that they endorse you or your use of the work).

– Share Alike — If you alter, transform, or build upon this work, you may distribute the resulting work only under the same or similar license to this one.

– License text: http://creativecommons.org/licenses/by-sa/3.0/legalcode

3

Matching

● 匹配● 圖上各點僅連接零或一條邊,所構成的集合

1 2

3 4

Graph

1 2

3 4

Matching

4

Cardinality

● 匹配數

1 2

3 4

0

1 2

3 4

1

1 2

3 4

1 2

3 4

2

1 2

3 4

5

Cardinality

● Maximal matching– 無法在增加匹配數

● Maximum matching– 匹配數最多

● Perfect matching– 所有點都完成配對

6

Weight

1 2

3 4

-35

2

8

1 1 2

3 4

-35

2

8

1 1 2

3 4

-35

2

8

1

3 -3 13

7

Weight

● Maximum weight matching

– 權重最大的匹配● Maximum weight maximum cardinality matching

– 在匹配數最大的情況下,權重最大的匹配● Maximum weight perfect matching

– 所有點都完成配對的情況下,權重最大的匹配

8

Bipartite Matching

● Bipartite Graph– 二分圖,圖上點被分為兩群,群內沒有邊

0

1

2

3

4

5

6

9

Bipartite Matching

● 化簡資料結構

0

1

2

0

1

2

3

10

Bipartite Matching

● 利用 flow 解 Bipartite Matching 問題

0

1

2

0

1

2

3

s t

11

Bipartite Matching

● Alternating Path– 交錯路徑,一條路徑上,匹配、未匹配邊交互出現

0

1

2

0

1

2

3

12

Bipartite Matching

● Augmenting Path– 擴充路徑,起始點和終點都是未匹配點的交錯路徑

0

1

2

0

1

2

3

13

Bipartite Matching

● Augmenting Path– 特性:交換匹配、未匹配邊可增加匹配數

0

1

2

0

1

2

3

0

1

2

0

1

2

3

14

Bipartite Matching

● 經過一些證明(有興趣者可以參考演算法筆記)– 如果任取一未匹配點,找不到以此點作為起點的擴充路徑,

那麼最大匹配就不會包含此點。● 找最大匹配的演算法

– 1. 起始圖上皆為未匹配點

– 2. 把圖上各點當作起點嘗試尋找擴充路徑● 成功:修改匹配增加匹配數● 失敗:刪掉他

15

Bipartite Matching

● Bipartite Graph 的好處

– 擴充路徑會在兩側來回

0

1

2

0

1

2

3

16

Bipartite Matching

17

Hungarian Algorithm(Kuhn-Munkres Algorithm)

● 解最大權完美二分匹配● 修改後也可以求

– 最大全最大二分匹配– 最大二分匹配

18

Hungarian Algorithm(Kuhn-Munkres Algorithm)

● 預備知識● 權重調整

– 對一個點連接的所有邊,等量加減權重,不影響最大權最大匹配

0

1

2

0

1

2

3

1 + D

3 + D

19

Hungarian Algorithm(Kuhn-Munkres Algorithm)

● 權重調整– 將每個點加上一個變數 (label) ,用來取代在邊上調

整,減少調整時間

0

1

2

0

1

2

3

1

3

+D

0

0

0

0

0

0

20

Hungarian Algorithm(Kuhn-Munkres Algorithm)

● 問題轉換– 使所選邊兩端 label 和大於等於邊的權重,當 label

值從上限往下降低,便可以碰到最大權重

0

1

2

0

1

2

1

2

3

2

7

3

2

7

0

0

0

21

Hungarian Algorithm(Kuhn-Munkres Algorithm)

● 等邊– 兩端 label 和等於邊的權重– 在最大權二分匹配的情況下, label 和最低時,所

有匹配邊皆為等邊0

1

2

0

1

2

1

2

3

2

7

3

2

7

0

0

0

22

Hungarian Algorithm(Kuhn-Munkres Algorithm)

● 演算法:結合等邊和 Augmenting path 概念– 一直以等邊組成的擴充路徑擴充,最後就可以得到

最大權匹配– 1. 起始所有點皆為未匹配點 (X, Y 兩群 )– 2. 嘗試用 X 中的每個點作為擴充路徑的起點,並建

立交錯樹● 有擴充路徑:沿路徑修改匹配,增加匹配數● 找不到:調整 label

23

Hungarian Algorithm(Kuhn-Munkres Algorithm)

● 調整 label

0

1

2

3

4

5

3

2

1

2

7

3

2

7

0

0

0

2

24

Hungarian Algorithm(Kuhn-Munkres Algorithm)

● 調整 label

0

1

2

3

4

5

3

2

1

2

7

3

2

7

0

0

0

2

25

Hungarian Algorithm(Kuhn-Munkres Algorithm)

● 調整 label

0

1

3

4

5

3

2

1

3

2

0

0

0

2

交錯樹

26

Hungarian Algorithm(Kuhn-Munkres Algorithm)

● 調整 label ,找減小幅度最小者(以免小於最大值)

0

1

3

4

5

3

2

1

3

2

0

0

0

2

幅度計算, label 和減去權重

d = min((3 + 0 – 1 = 2), (3 + 0 – 2 = 1)) = 1

27

Hungarian Algorithm(Kuhn-Munkres Algorithm)

● 調整 label ,找減小幅度最小者(以免小於最大值)

0

1

3

4

5

3

2

1

3 - d

2 - d

0 + d

0

0

2

28

Hungarian Algorithm(Kuhn-Munkres Algorithm)

● 調整 label ,找減小幅度最小者(以免小於最大值)

0

1

3

4

5

3

2

1

2

1

1

0

0

2

等邊出現

29

30

Practice

● UVa11138● UVa 663

● http://140.113.210.149/gdoj/problemset/problem/253

● http://140.122.185.166/ZeroJudge/ShowProblem?problemid=d191&contentlanguage=en_US

● ACM-ICPC Live 4102

31

Thank You for Your Listening.