[acm-icpc] bipartite matching
TRANSCRIPT
![Page 1: [ACM-ICPC] Bipartite Matching](https://reader033.vdocuments.net/reader033/viewer/2022061609/555603e0d8b42a3f168b4834/html5/thumbnails/1.jpg)
Bipartite Matching郭至軒(KuoE0)
![Page 2: [ACM-ICPC] Bipartite Matching](https://reader033.vdocuments.net/reader033/viewer/2022061609/555603e0d8b42a3f168b4834/html5/thumbnails/2.jpg)
Bipartite Graphtwo disjoint sets
every edge connects between them
![Page 3: [ACM-ICPC] Bipartite Matching](https://reader033.vdocuments.net/reader033/viewer/2022061609/555603e0d8b42a3f168b4834/html5/thumbnails/3.jpg)
Matching
A subset of edges, no two of which share an endpoint.
![Page 4: [ACM-ICPC] Bipartite Matching](https://reader033.vdocuments.net/reader033/viewer/2022061609/555603e0d8b42a3f168b4834/html5/thumbnails/4.jpg)
Valid Matching
bachelor
not matching edgematching edge
![Page 5: [ACM-ICPC] Bipartite Matching](https://reader033.vdocuments.net/reader033/viewer/2022061609/555603e0d8b42a3f168b4834/html5/thumbnails/5.jpg)
Invalid Matching
shared
not matching edgematching edge
![Page 6: [ACM-ICPC] Bipartite Matching](https://reader033.vdocuments.net/reader033/viewer/2022061609/555603e0d8b42a3f168b4834/html5/thumbnails/6.jpg)
Bipartite Matching
objective: more matching
![Page 7: [ACM-ICPC] Bipartite Matching](https://reader033.vdocuments.net/reader033/viewer/2022061609/555603e0d8b42a3f168b4834/html5/thumbnails/7.jpg)
1
2
84
5
7
9
3
6
not matching edgematching edge
![Page 8: [ACM-ICPC] Bipartite Matching](https://reader033.vdocuments.net/reader033/viewer/2022061609/555603e0d8b42a3f168b4834/html5/thumbnails/8.jpg)
Alternating Path
A path in which the edges belong alternatively to the matching and not to the matching.
![Page 9: [ACM-ICPC] Bipartite Matching](https://reader033.vdocuments.net/reader033/viewer/2022061609/555603e0d8b42a3f168b4834/html5/thumbnails/9.jpg)
1
2
84
5
7
9
3
6
not matching edge
[1 - 5 - 2 - 9] is an alternating path.
matching edge
![Page 10: [ACM-ICPC] Bipartite Matching](https://reader033.vdocuments.net/reader033/viewer/2022061609/555603e0d8b42a3f168b4834/html5/thumbnails/10.jpg)
1
2
84
5
7
9
3
6
not matching edge
[1 - 5 - 2 - 9] is an alternating path.
matching edge
![Page 11: [ACM-ICPC] Bipartite Matching](https://reader033.vdocuments.net/reader033/viewer/2022061609/555603e0d8b42a3f168b4834/html5/thumbnails/11.jpg)
1
2
84
5
7
9
3
6
not matching edgematching edge
[1 - 5 - 2 - 6 - 3] is not an alternating path.
![Page 12: [ACM-ICPC] Bipartite Matching](https://reader033.vdocuments.net/reader033/viewer/2022061609/555603e0d8b42a3f168b4834/html5/thumbnails/12.jpg)
1
2
84
5
7
9
3
6
not matching edgematching edge
[1 - 5 - 2 - 6 - 3] is not an alternating path.
![Page 13: [ACM-ICPC] Bipartite Matching](https://reader033.vdocuments.net/reader033/viewer/2022061609/555603e0d8b42a3f168b4834/html5/thumbnails/13.jpg)
Alternating Cycle
A cycle in which the edges belong alternatively to the matching and not to the matching.
![Page 14: [ACM-ICPC] Bipartite Matching](https://reader033.vdocuments.net/reader033/viewer/2022061609/555603e0d8b42a3f168b4834/html5/thumbnails/14.jpg)
1
2
84
5
7
9
3
6
not matching edge
[1 - 5 - 2 - 9 - 1] is an alternating cycle.
matching edge
![Page 15: [ACM-ICPC] Bipartite Matching](https://reader033.vdocuments.net/reader033/viewer/2022061609/555603e0d8b42a3f168b4834/html5/thumbnails/15.jpg)
1
2
84
5
7
9
3
6
not matching edge
[1 - 5 - 2 - 9 - 1] is an alternating cycle.
matching edge
![Page 16: [ACM-ICPC] Bipartite Matching](https://reader033.vdocuments.net/reader033/viewer/2022061609/555603e0d8b42a3f168b4834/html5/thumbnails/16.jpg)
1
2
84
5
7
9
3
6
not matching edgematching edge
Reverse the alternating cycle, cardinality won’t change.
![Page 17: [ACM-ICPC] Bipartite Matching](https://reader033.vdocuments.net/reader033/viewer/2022061609/555603e0d8b42a3f168b4834/html5/thumbnails/17.jpg)
Augmenting Path AlgorithmMaximum Matching
![Page 18: [ACM-ICPC] Bipartite Matching](https://reader033.vdocuments.net/reader033/viewer/2022061609/555603e0d8b42a3f168b4834/html5/thumbnails/18.jpg)
Augmenting Path
An alternating path that starts from and ends on unmatched vertices.
![Page 19: [ACM-ICPC] Bipartite Matching](https://reader033.vdocuments.net/reader033/viewer/2022061609/555603e0d8b42a3f168b4834/html5/thumbnails/19.jpg)
1
2
84
5
7
9
3
6
not matching edgematching edge
[2 - 5 - 1 - 9] is an augmenting path.
![Page 20: [ACM-ICPC] Bipartite Matching](https://reader033.vdocuments.net/reader033/viewer/2022061609/555603e0d8b42a3f168b4834/html5/thumbnails/20.jpg)
1
2
84
5
7
9
3
6
not matching edgematching edge
[2 - 5 - 1 - 9] is an augmenting path.
![Page 21: [ACM-ICPC] Bipartite Matching](https://reader033.vdocuments.net/reader033/viewer/2022061609/555603e0d8b42a3f168b4834/html5/thumbnails/21.jpg)
1
2
84
5
7
9
3
6
not matching edgematching edge
Reverse the augmenting path, cardinality will increase.
![Page 22: [ACM-ICPC] Bipartite Matching](https://reader033.vdocuments.net/reader033/viewer/2022061609/555603e0d8b42a3f168b4834/html5/thumbnails/22.jpg)
Augmenting Path• The length of an augmenting path
always is odd.
• Reversing an augmenting path will increase the cardinality.
• If there is no augmenting path, the cardinality is maximum.
![Page 23: [ACM-ICPC] Bipartite Matching](https://reader033.vdocuments.net/reader033/viewer/2022061609/555603e0d8b42a3f168b4834/html5/thumbnails/23.jpg)
1.Try to build augmenting paths from all vertices in one side.
2.Travel on graph.
3. If an augmenting path exists, reverse the augmenting path to increase cardinality.
4. If no augmenting path exists, ignore this vertex.
5.Repeat above step until there no augmenting path exists.
Algorithm
![Page 24: [ACM-ICPC] Bipartite Matching](https://reader033.vdocuments.net/reader033/viewer/2022061609/555603e0d8b42a3f168b4834/html5/thumbnails/24.jpg)
1
2
84
5
7
9
3
6
not matching edgematching edge
[1 - 5] is an augmenting path.
current cardinality: 0
![Page 25: [ACM-ICPC] Bipartite Matching](https://reader033.vdocuments.net/reader033/viewer/2022061609/555603e0d8b42a3f168b4834/html5/thumbnails/25.jpg)
1
2
84
5
7
9
3
6
not matching edgematching edge
[1 - 5] is an augmenting path.
current cardinality: 0
![Page 26: [ACM-ICPC] Bipartite Matching](https://reader033.vdocuments.net/reader033/viewer/2022061609/555603e0d8b42a3f168b4834/html5/thumbnails/26.jpg)
1
2
84
5
7
9
3
6
Reverse it!
not matching edgematching edge
current cardinality: 1
![Page 27: [ACM-ICPC] Bipartite Matching](https://reader033.vdocuments.net/reader033/viewer/2022061609/555603e0d8b42a3f168b4834/html5/thumbnails/27.jpg)
1
2
84
5
7
9
3
6
[2 - 5 - 1- 9] is an augmenting path.
not matching edgematching edge
current cardinality: 1
![Page 28: [ACM-ICPC] Bipartite Matching](https://reader033.vdocuments.net/reader033/viewer/2022061609/555603e0d8b42a3f168b4834/html5/thumbnails/28.jpg)
1
2
84
5
7
9
3
6
[2 - 5 - 1- 9] is an augmenting path.
not matching edgematching edge
current cardinality: 1
![Page 29: [ACM-ICPC] Bipartite Matching](https://reader033.vdocuments.net/reader033/viewer/2022061609/555603e0d8b42a3f168b4834/html5/thumbnails/29.jpg)
1
2
84
5
7
9
3
6
not matching edgematching edge
current cardinality: 2
Reverse it!
![Page 30: [ACM-ICPC] Bipartite Matching](https://reader033.vdocuments.net/reader033/viewer/2022061609/555603e0d8b42a3f168b4834/html5/thumbnails/30.jpg)
1
2
84
5
7
9
3
6
not matching edgematching edge
current cardinality: 2
[3 - 6] is an augmenting path.
![Page 31: [ACM-ICPC] Bipartite Matching](https://reader033.vdocuments.net/reader033/viewer/2022061609/555603e0d8b42a3f168b4834/html5/thumbnails/31.jpg)
1
2
84
5
7
9
3
6
not matching edgematching edge
current cardinality: 2
[3 - 6] is an augmenting path.
![Page 32: [ACM-ICPC] Bipartite Matching](https://reader033.vdocuments.net/reader033/viewer/2022061609/555603e0d8b42a3f168b4834/html5/thumbnails/32.jpg)
1
2
84
5
7
9
3
6
not matching edgematching edge
current cardinality: 3
Reverse it!
![Page 33: [ACM-ICPC] Bipartite Matching](https://reader033.vdocuments.net/reader033/viewer/2022061609/555603e0d8b42a3f168b4834/html5/thumbnails/33.jpg)
1
2
84
5
7
9
3
6
not matching edgematching edge
current cardinality: 3
[4 - 5 - 2 - 6 - 3 - 7] is an augmenting path.
![Page 34: [ACM-ICPC] Bipartite Matching](https://reader033.vdocuments.net/reader033/viewer/2022061609/555603e0d8b42a3f168b4834/html5/thumbnails/34.jpg)
1
2
84
5
7
9
3
6
not matching edgematching edge
current cardinality: 3
[4 - 5 - 2 - 6 - 3 - 7] is an augmenting path.
![Page 35: [ACM-ICPC] Bipartite Matching](https://reader033.vdocuments.net/reader033/viewer/2022061609/555603e0d8b42a3f168b4834/html5/thumbnails/35.jpg)
1
2
84
5
7
9
3
6
not matching edgematching edge
current cardinality: 4
Reverse it!
![Page 36: [ACM-ICPC] Bipartite Matching](https://reader033.vdocuments.net/reader033/viewer/2022061609/555603e0d8b42a3f168b4834/html5/thumbnails/36.jpg)
1
2
84
5
7
9
3
6
not matching edgematching edge
Max cardinality is 4.
![Page 37: [ACM-ICPC] Bipartite Matching](https://reader033.vdocuments.net/reader033/viewer/2022061609/555603e0d8b42a3f168b4834/html5/thumbnails/37.jpg)
Time Complexity
O(V × E)
V: number of verticesE: number of edges
![Page 38: [ACM-ICPC] Bipartite Matching](https://reader033.vdocuments.net/reader033/viewer/2022061609/555603e0d8b42a3f168b4834/html5/thumbnails/38.jpg)
// find an augmenting pathbool find_aug_path( int x ) { for ( int i = 0; i < ( int )vertex[ x ].size(); ++i ) { int next = vertex[ x ][ i ]; // not in augmenting path if ( !visit[ next ] ) { // setup this vertex in augmenting path visit[ next ] = 1; /* * If this vertex is a unmatched vertex, reverse augmenting path and return. * If this vector is a matched vertex, try to reverse augmenting path and continue find an unmatched vertex. */ if ( lnk2[ next ] == -1 || find_aug_path( lnk2[ next ] ) ) { lnk1[ x ] = next, lnk2[ next ] = x; return 1; } } } return 0;}
Source Code
![Page 39: [ACM-ICPC] Bipartite Matching](https://reader033.vdocuments.net/reader033/viewer/2022061609/555603e0d8b42a3f168b4834/html5/thumbnails/39.jpg)
UVa 670 - The dog task
Practice Now
![Page 40: [ACM-ICPC] Bipartite Matching](https://reader033.vdocuments.net/reader033/viewer/2022061609/555603e0d8b42a3f168b4834/html5/thumbnails/40.jpg)
Hungarian Algorithm(Kuhn-Munkres Algorithm)
Maximum Weight Matching
![Page 41: [ACM-ICPC] Bipartite Matching](https://reader033.vdocuments.net/reader033/viewer/2022061609/555603e0d8b42a3f168b4834/html5/thumbnails/41.jpg)
1 4
2
3
5
6
532
1
-25
![Page 42: [ACM-ICPC] Bipartite Matching](https://reader033.vdocuments.net/reader033/viewer/2022061609/555603e0d8b42a3f168b4834/html5/thumbnails/42.jpg)
weight adjustment
If add (subtract) some value on all edges connected with vertex X, the maximum matching won’t be effected.
1 4
2
3
5
6
abc
not matching edgematching edge
1 4
2
3
5
6
a+db+dc+d
![Page 43: [ACM-ICPC] Bipartite Matching](https://reader033.vdocuments.net/reader033/viewer/2022061609/555603e0d8b42a3f168b4834/html5/thumbnails/43.jpg)
vertex labeling
For convenient, add a variable on vertices to denote the value add (subtract) on edges connected with them.
1 4
2
3
5
6
abc
not matching edgematching edge
1 4
2
3
5
6
a+db+dc+d
d ≡
![Page 44: [ACM-ICPC] Bipartite Matching](https://reader033.vdocuments.net/reader033/viewer/2022061609/555603e0d8b42a3f168b4834/html5/thumbnails/44.jpg)
vertex labeling
l(x)+l(y)≥w(x,y), for each edge(x,y)
1 4
2
3
5
6
3
532
1
-25
5
5
0
0
0
![Page 45: [ACM-ICPC] Bipartite Matching](https://reader033.vdocuments.net/reader033/viewer/2022061609/555603e0d8b42a3f168b4834/html5/thumbnails/45.jpg)
1 4
2
3
5
6
0
532
1
-25
2
4
1
3
0
minimize vertex labeling
Admissible Edge: l(x)+l(y)=w(x,y)
![Page 46: [ACM-ICPC] Bipartite Matching](https://reader033.vdocuments.net/reader033/viewer/2022061609/555603e0d8b42a3f168b4834/html5/thumbnails/46.jpg)
convertmaximum weight problem
tominimum vertex labeling problem
![Page 47: [ACM-ICPC] Bipartite Matching](https://reader033.vdocuments.net/reader033/viewer/2022061609/555603e0d8b42a3f168b4834/html5/thumbnails/47.jpg)
Augmenting Path with Admissible Edge
[1 - 4] is an augment path with admissible edges.
1 4
2
3
5
6
3
532
1
-25
5
5
0
0
0
![Page 48: [ACM-ICPC] Bipartite Matching](https://reader033.vdocuments.net/reader033/viewer/2022061609/555603e0d8b42a3f168b4834/html5/thumbnails/48.jpg)
Augmenting Path with Admissible Edge
[1 - 4] is an augment path with admissible edges.
1 4
2
3
5
6
3
532
1
-25
5
5
0
0
0
![Page 49: [ACM-ICPC] Bipartite Matching](https://reader033.vdocuments.net/reader033/viewer/2022061609/555603e0d8b42a3f168b4834/html5/thumbnails/49.jpg)
Augmenting Path with Admissible Edge
Reverse it!
1 4
2
3
5
6
3
532
1
-25
5
5
0
0
0
Current Weight = 5
![Page 50: [ACM-ICPC] Bipartite Matching](https://reader033.vdocuments.net/reader033/viewer/2022061609/555603e0d8b42a3f168b4834/html5/thumbnails/50.jpg)
Augmenting Path with Admissible Edge
No augmenting path found start from vertex 2.
1 4
2
3
5
6
3
532
1
-25
5
5
0
0
0
![Page 51: [ACM-ICPC] Bipartite Matching](https://reader033.vdocuments.net/reader033/viewer/2022061609/555603e0d8b42a3f168b4834/html5/thumbnails/51.jpg)
Augmenting Path with Admissible Edge
No augmenting path found start from vertex 2.
1 4
2
3
5
6
3
532
1
-25
5
5
0
0
0
![Page 52: [ACM-ICPC] Bipartite Matching](https://reader033.vdocuments.net/reader033/viewer/2022061609/555603e0d8b42a3f168b4834/html5/thumbnails/52.jpg)
Adjust Vertex Labeling
1 4
2
3
5
6
3
532
1
-25
5
5
0
0
0
relax value d = min(l(x)+l(y)-w(x,y))x: vertex in alternating pathy: vertex y not in alternating path
![Page 53: [ACM-ICPC] Bipartite Matching](https://reader033.vdocuments.net/reader033/viewer/2022061609/555603e0d8b42a3f168b4834/html5/thumbnails/53.jpg)
Adjust Vertex Labeling
1 4
2
3
5
6
3
532
1
-25
5
5
0
0
0
R(1,6)=3R(2,5)=2R(2,6)=5
relax d = 2
![Page 54: [ACM-ICPC] Bipartite Matching](https://reader033.vdocuments.net/reader033/viewer/2022061609/555603e0d8b42a3f168b4834/html5/thumbnails/54.jpg)
Adjust Vertex Labeling
l(x) subtract value d.l(y) add value d.
1 4
2
3
5
6
1
532
1
-25
3
5
0
2
0
![Page 55: [ACM-ICPC] Bipartite Matching](https://reader033.vdocuments.net/reader033/viewer/2022061609/555603e0d8b42a3f168b4834/html5/thumbnails/55.jpg)
Continue to Find Augmenting Path
1 4
2
3
5
6
1
532
1
-25
3
5
0
2
0
[2 - 5] is an augment path with admissible edges.
![Page 56: [ACM-ICPC] Bipartite Matching](https://reader033.vdocuments.net/reader033/viewer/2022061609/555603e0d8b42a3f168b4834/html5/thumbnails/56.jpg)
Continue to Find Augmenting Path
1 4
2
3
5
6
1
532
1
-25
3
5
0
2
0
[2 - 5] is an augment path with admissible edges.
![Page 57: [ACM-ICPC] Bipartite Matching](https://reader033.vdocuments.net/reader033/viewer/2022061609/555603e0d8b42a3f168b4834/html5/thumbnails/57.jpg)
Continue to Find Augmenting Path
1 4
2
3
5
6
1
532
1
-25
3
5
0
2
0
Reverse it!
Current Weight = 6
![Page 58: [ACM-ICPC] Bipartite Matching](https://reader033.vdocuments.net/reader033/viewer/2022061609/555603e0d8b42a3f168b4834/html5/thumbnails/58.jpg)
Continue to Find Augmenting Path
1 4
2
3
5
6
1
532
1
-25
3
5
0
2
0
No augmenting path found start from vertex 3.
![Page 59: [ACM-ICPC] Bipartite Matching](https://reader033.vdocuments.net/reader033/viewer/2022061609/555603e0d8b42a3f168b4834/html5/thumbnails/59.jpg)
Continue to Find Augmenting Path
1 4
2
3
5
6
1
532
1
-25
3
5
0
2
0
No augmenting path found start from vertex 3.
![Page 60: [ACM-ICPC] Bipartite Matching](https://reader033.vdocuments.net/reader033/viewer/2022061609/555603e0d8b42a3f168b4834/html5/thumbnails/60.jpg)
Adjust Vertex Labeling
1 4
2
3
5
6
1
532
1
-25
3
5
0
2
0
R(1,6)=1R(2,6)=3 relax d = 1
![Page 61: [ACM-ICPC] Bipartite Matching](https://reader033.vdocuments.net/reader033/viewer/2022061609/555603e0d8b42a3f168b4834/html5/thumbnails/61.jpg)
Adjust Vertex Labeling
1 4
2
3
5
6
0
532
1
-25
2
4
1
3
0
l(x) subtract value d.l(y) add value d.
![Page 62: [ACM-ICPC] Bipartite Matching](https://reader033.vdocuments.net/reader033/viewer/2022061609/555603e0d8b42a3f168b4834/html5/thumbnails/62.jpg)
Continue to Find Augmenting Path
1 4
2
3
5
6
0
532
1
-25
2
4
1
3
0
[3 - 5 - 2 - 4 - 1 - 6] is an augment path with admissible edges.
![Page 63: [ACM-ICPC] Bipartite Matching](https://reader033.vdocuments.net/reader033/viewer/2022061609/555603e0d8b42a3f168b4834/html5/thumbnails/63.jpg)
Continue to Find Augmenting Path
1 4
2
3
5
6
0
532
1
-25
2
4
1
3
0
[3 - 5 - 2 - 4 - 1 - 6] is an augment path with admissible edges.
![Page 64: [ACM-ICPC] Bipartite Matching](https://reader033.vdocuments.net/reader033/viewer/2022061609/555603e0d8b42a3f168b4834/html5/thumbnails/64.jpg)
Continue to Find Augmenting Path
1 4
2
3
5
6
0
532
1
-25
2
4
1
3
0
Reverse it!
Current Weight = 10
![Page 65: [ACM-ICPC] Bipartite Matching](https://reader033.vdocuments.net/reader033/viewer/2022061609/555603e0d8b42a3f168b4834/html5/thumbnails/65.jpg)
Maximum Weight Matching = 10
1 4
2
3
5
6
0
532
1
-25
2
4
1
3
0
![Page 66: [ACM-ICPC] Bipartite Matching](https://reader033.vdocuments.net/reader033/viewer/2022061609/555603e0d8b42a3f168b4834/html5/thumbnails/66.jpg)
Algorithm1. Initial vertex labeling to fit l(x)+l(y)≥w(x,y)2.Find augmenting paths composed with
admissible edges from all vertices in one side.
3. If no augmenting path exists, adjust vertex labeling until the augmenting path found.
4. If augmenting path exists, continue to find next augmenting path.
5.Repeat above step until there no augmenting path exists.
6.Calculate the sum of weight on matching edges.
![Page 68: [ACM-ICPC] Bipartite Matching](https://reader033.vdocuments.net/reader033/viewer/2022061609/555603e0d8b42a3f168b4834/html5/thumbnails/68.jpg)
Problem ListUVa 670UVa 753
UVa 10080UVa 10092UVa 10243UVa 10418UVa 10984POJ 3565
![Page 69: [ACM-ICPC] Bipartite Matching](https://reader033.vdocuments.net/reader033/viewer/2022061609/555603e0d8b42a3f168b4834/html5/thumbnails/69.jpg)
Reference
• http://en.wikipedia.org/wiki/Bipartite_graph
• http://en.wikipedia.org/wiki/Matching_(graph_theory)
• http://www.flickr.com/photos/marceau_r/5244129689
• http://www.sanfilippo-chianti.it/offerta-svalentino.html
• http://www.csie.ntnu.edu.tw/~u91029/Matching.html
![Page 70: [ACM-ICPC] Bipartite Matching](https://reader033.vdocuments.net/reader033/viewer/2022061609/555603e0d8b42a3f168b4834/html5/thumbnails/70.jpg)
Thank You for Your Listening.