Bipartite Matching      郭至軒(KuoE0)     KuoE0.tw@gmail.com          KuoE0.ch
Bipartite Graph                 two disjoint setsevery edge connects between them
MatchingA subset of edges, no two of which share an                   endpoint.
Valid Matching           not matching edge           matching edge              bachelor
Invalid Matching            not matching edge            matching edge                       shared
Bipartite Matching     objective: more matching
1         5          2         6          3         7          4         8not matching edge   9matching edge
Alternating PathA path in which the edges belong alternatively to the         matching and not to the matching.
[1 - 5 - 2 - 9] is an alternating path.          1                     5          2                     6          3      ...
[1 - 5 - 2 - 9] is an alternating path.          1                     5          2                     6          3      ...
[1 - 5 - 2 - 6 - 3] is not an alternating path.           1                     5           2                     6       ...
[1 - 5 - 2 - 6 - 3] is not an alternating path.           1                     5           2                     6       ...
Alternating CycleA cycle in which the edges belong alternatively to    the matching and not to the matching.
[1 - 5 - 2 - 9 - 1] is an alternating cycle.           1                     5           2                     6          ...
[1 - 5 - 2 - 9 - 1] is an alternating cycle.           1                     5           2                     6          ...
Reverse the alternating cycle, cardinality won’t change.                1                    5                2           ...
Maximum MatchingAugmenting Path Algorithm
Augmenting PathAn alternating path that starts from and ends on              unmatched vertices.
[2 - 5 - 1 - 9] is an augmenting path.          1                   5          2                   6          3           ...
[2 - 5 - 1 - 9] is an augmenting path.          1                   5          2                   6          3           ...
Reverse the augmenting path, cardinality will increase.                1                    5                2            ...
Augmenting Path• The length of an augmenting path  always is odd.• Reversing an augmenting path will  increase the cardina...
Algorithm1. Try to build augmenting paths from all   vertices in one side.2. Travel on graph.3. If an augmenting path exis...
[1 - 5] is an augmenting path.               1                     5               2                     6               3...
[1 - 5] is an augmenting path.               1                     5               2                     6               3...
Reverse it!               1                   5               2                   6               3                   7   ...
[2 - 5 - 1- 9] is an augmenting path.               1                      5               2                      6       ...
[2 - 5 - 1- 9] is an augmenting path.               1                      5               2                      6       ...
Reverse it!               1                   5               2                   6               3                   7   ...
[3 - 6] is an augmenting path.               1                     5               2                     6               3...
[3 - 6] is an augmenting path.               1                     5               2                     6               3...
Reverse it!               1                   5               2                   6               3                   7   ...
[4 - 5 - 2 - 6 - 3 - 7] is an augmenting path.                1                      5                2                   ...
[4 - 5 - 2 - 6 - 3 - 7] is an augmenting path.                1                      5                2                   ...
Reverse it!               1                   5               2                   6               3                   7   ...
Max cardinality is 4.          1                  5          2                  6          3                  7          4...
Time Complexity      O(V × E)  V: number of vertices  E: number of edges
Source Code// find an augmenting pathbool find_aug_path( int x ) {	   for ( int i = 0; i < ( int )vertex[ x ].size(); ++i ...
Practice Now UVa 670 - The dog task
Maximum Weight MatchingHungarian Algorithm         (Kuhn-Munkres Algorithm)
51                4    2   3            12                53   5       -2   6
weight adjustmentIf add (subtract) some value on all edges connected with   vertex X, the maximum matching won’t be effect...
vertex labelingFor convenient, add a variable on vertices to denote the  value add (subtract) on edges connected with them...
vertex labelingl(x)+l(y)≥w(x,y), for each edge(x,y)                  5   5   1                     4   0             2    ...
minimize vertex labelingAdmissible Edge: l(x)+l(y)=w(x,y)                 5  2    1                   4     3            2...
convert   maximum weight problem              tominimum vertex labeling problem
Augmenting Path with Admissible Edge                       5       5   1                        4   0                 2   ...
Augmenting Path with Admissible Edge                       5       5   1                        4   0                 2   ...
Augmenting Path with Admissible Edge                     5     5   1                        4   0               2     3   ...
Augmenting Path with Admissible Edge                       5      5   1                          4   0                 2  ...
Augmenting Path with Admissible Edge                       5      5   1                          4   0                 2  ...
Adjust Vertex Labeling                    5    5   1                         4   0              2     3                   ...
Adjust Vertex Labeling                   55    1                          4    0           2       3                      ...
Adjust Vertex Labeling                  53   1                            4   2            2     3                        ...
Continue to Find Augmenting Path                       5       3   1                        4   2                 2     3 ...
Continue to Find Augmenting Path                       5       3   1                        4   2                 2     3 ...
Continue to Find Augmenting Path                   5   3   1                        4   2             2     3             ...
Continue to Find Augmenting Path                     5    3   1                          4   2               2     3      ...
Continue to Find Augmenting Path                     5    3   1                          4   2               2     3      ...
Adjust Vertex Labeling                   53    1                          4    2           2       3                      ...
Adjust Vertex Labeling                  52   1                            4   3            2     3                        ...
Continue to Find Augmenting Path                      5     2   1                          4   3                2     3   ...
Continue to Find Augmenting Path                      5     2   1                          4   3                2     3   ...
Continue to Find Augmenting Path                   5   2   1                         4   3              2    3            ...
Maximum Weight Matching = 10              5  2   1                4   3          2   3                  1  0   2          ...
Algorithm1. Initial vertex labeling to fit l(x)+l(y)≥w(x,y)2. Find augmenting paths composed with   admissible edges from a...
Practice NowPOJ 2195 - Going Home
Problem List     UVa 670     UVa 753    UVa 10080    UVa 10092    UVa 10243    UVa 10418    UVa 10984    POJ 3565
Reference•   http://en.wikipedia.org/wiki/Bipartite_graph•   http://en.wikipedia.org/wiki/Matching_(graph_theory)•   http:...
Thank You for Your    Listening.
Upcoming SlideShare
Loading in...5
×

[ACM-ICPC] Bipartite Matching

1,053

Published on

Published in: Education
0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
1,053
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
104
Comments
0
Likes
1
Embeds 0
No embeds

No notes for slide

[ACM-ICPC] Bipartite Matching

  1. 1. Bipartite Matching 郭至軒(KuoE0) KuoE0.tw@gmail.com KuoE0.ch
  2. 2. Bipartite Graph two disjoint setsevery edge connects between them
  3. 3. MatchingA subset of edges, no two of which share an endpoint.
  4. 4. Valid Matching not matching edge matching edge bachelor
  5. 5. Invalid Matching not matching edge matching edge shared
  6. 6. Bipartite Matching objective: more matching
  7. 7. 1 5 2 6 3 7 4 8not matching edge 9matching edge
  8. 8. Alternating PathA path in which the edges belong alternatively to the matching and not to the matching.
  9. 9. [1 - 5 - 2 - 9] is an alternating path. 1 5 2 6 3 7 4 8not matching edge 9matching edge
  10. 10. [1 - 5 - 2 - 9] is an alternating path. 1 5 2 6 3 7 4 8not matching edge 9matching edge
  11. 11. [1 - 5 - 2 - 6 - 3] is not an alternating path. 1 5 2 6 3 7 4 8not matching edge 9matching edge
  12. 12. [1 - 5 - 2 - 6 - 3] is not an alternating path. 1 5 2 6 3 7 4 8not matching edge 9matching edge
  13. 13. Alternating CycleA cycle in which the edges belong alternatively to the matching and not to the matching.
  14. 14. [1 - 5 - 2 - 9 - 1] is an alternating cycle. 1 5 2 6 3 7 4 8not matching edge 9matching edge
  15. 15. [1 - 5 - 2 - 9 - 1] is an alternating cycle. 1 5 2 6 3 7 4 8not matching edge 9matching edge
  16. 16. Reverse the alternating cycle, cardinality won’t change. 1 5 2 6 3 7 4 8 not matching edge 9 matching edge
  17. 17. Maximum MatchingAugmenting Path Algorithm
  18. 18. Augmenting PathAn alternating path that starts from and ends on unmatched vertices.
  19. 19. [2 - 5 - 1 - 9] is an augmenting path. 1 5 2 6 3 7 4 8not matching edge 9matching edge
  20. 20. [2 - 5 - 1 - 9] is an augmenting path. 1 5 2 6 3 7 4 8not matching edge 9matching edge
  21. 21. Reverse the augmenting path, cardinality will increase. 1 5 2 6 3 7 4 8 not matching edge 9 matching edge
  22. 22. 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.
  23. 23. Algorithm1. 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.
  24. 24. [1 - 5] is an augmenting path. 1 5 2 6 3 7 4 8current cardinality: 0 9 not matching edge matching edge
  25. 25. [1 - 5] is an augmenting path. 1 5 2 6 3 7 4 8current cardinality: 0 9 not matching edge matching edge
  26. 26. Reverse it! 1 5 2 6 3 7 4 8current cardinality: 1 9 not matching edge matching edge
  27. 27. [2 - 5 - 1- 9] is an augmenting path. 1 5 2 6 3 7 4 8current cardinality: 1 9 not matching edge matching edge
  28. 28. [2 - 5 - 1- 9] is an augmenting path. 1 5 2 6 3 7 4 8current cardinality: 1 9 not matching edge matching edge
  29. 29. Reverse it! 1 5 2 6 3 7 4 8current cardinality: 2 9 not matching edge matching edge
  30. 30. [3 - 6] is an augmenting path. 1 5 2 6 3 7 4 8current cardinality: 2 9 not matching edge matching edge
  31. 31. [3 - 6] is an augmenting path. 1 5 2 6 3 7 4 8current cardinality: 2 9 not matching edge matching edge
  32. 32. Reverse it! 1 5 2 6 3 7 4 8current cardinality: 3 9 not matching edge matching edge
  33. 33. [4 - 5 - 2 - 6 - 3 - 7] is an augmenting path. 1 5 2 6 3 7 4 8current cardinality: 3 9 not matching edge matching edge
  34. 34. [4 - 5 - 2 - 6 - 3 - 7] is an augmenting path. 1 5 2 6 3 7 4 8current cardinality: 3 9 not matching edge matching edge
  35. 35. Reverse it! 1 5 2 6 3 7 4 8current cardinality: 4 9 not matching edge matching edge
  36. 36. Max cardinality is 4. 1 5 2 6 3 7 4 8 9not matching edgematching edge
  37. 37. Time Complexity O(V × E) V: number of vertices E: number of edges
  38. 38. Source Code// 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, reverseaugmenting path and return. * If this vector is a matched vertex, try to reverseaugmenting 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;}
  39. 39. Practice Now UVa 670 - The dog task
  40. 40. Maximum Weight MatchingHungarian Algorithm (Kuhn-Munkres Algorithm)
  41. 41. 51 4 2 3 12 53 5 -2 6
  42. 42. weight adjustmentIf add (subtract) some value on all edges connected with vertex X, the maximum matching won’t be effected. 1 4 1 4 a a+d b b+d 2 5 2 5 c c+d 3 6 3 6 matching edge not matching edge
  43. 43. vertex labelingFor convenient, add a variable on vertices to denote the value add (subtract) on edges connected with them. 1 4 1 4 a a+d b b+d d 2 c 5 ≡ 2 c+d 5 3 6 3 6 matching edge not matching edge
  44. 44. vertex labelingl(x)+l(y)≥w(x,y), for each edge(x,y) 5 5 1 4 0 2 3 1 3 2 5 0 5 3 5 -2 6 0
  45. 45. minimize vertex labelingAdmissible Edge: l(x)+l(y)=w(x,y) 5 2 1 4 3 2 3 1 0 2 5 1 4 3 5 -2 6 0
  46. 46. convert maximum weight problem tominimum vertex labeling problem
  47. 47. Augmenting Path with Admissible Edge 5 5 1 4 0 2 3 1 3 2 5 0 5 3 5 -2 6 0[1 - 4] is an augment path with admissible edges.
  48. 48. Augmenting Path with Admissible Edge 5 5 1 4 0 2 3 1 3 2 5 0 5 3 5 -2 6 0[1 - 4] is an augment path with admissible edges.
  49. 49. Augmenting Path with Admissible Edge 5 5 1 4 0 2 3 1 3 2 5 0 5 3 5 -2 6 0 Reverse it! Current Weight = 5
  50. 50. Augmenting Path with Admissible Edge 5 5 1 4 0 2 3 1 3 2 5 0 5 3 5 -2 6 0 No augmenting path found start from vertex 2.
  51. 51. Augmenting Path with Admissible Edge 5 5 1 4 0 2 3 1 3 2 5 0 5 3 5 -2 6 0 No augmenting path found start from vertex 2.
  52. 52. Adjust Vertex Labeling 5 5 1 4 0 2 3 1 3 2 5 0 5 3 5 -2 6 0relax value d = min(l(x)+l(y)-w(x,y)) x: vertex in alternating path y: vertex y not in alternating path
  53. 53. Adjust Vertex Labeling 55 1 4 0 2 3 13 2 5 05 3 5 -2 6 0 R(1,6)=3 R(2,5)=2 relax d = 2 R(2,6)=5
  54. 54. Adjust Vertex Labeling 53 1 4 2 2 3 11 2 5 05 3 5 -2 6 0 l(x) subtract value d. l(y) add value d.
  55. 55. Continue to Find Augmenting Path 5 3 1 4 2 2 3 1 1 2 5 0 5 3 5 -2 6 0[2 - 5] is an augment path with admissible edges.
  56. 56. Continue to Find Augmenting Path 5 3 1 4 2 2 3 1 1 2 5 0 5 3 5 -2 6 0[2 - 5] is an augment path with admissible edges.
  57. 57. Continue to Find Augmenting Path 5 3 1 4 2 2 3 1 1 2 5 0 5 3 5 -2 6 0 Reverse it! Current Weight = 6
  58. 58. Continue to Find Augmenting Path 5 3 1 4 2 2 3 1 1 2 5 0 5 3 5 -2 6 0No augmenting path found start from vertex 3.
  59. 59. Continue to Find Augmenting Path 5 3 1 4 2 2 3 1 1 2 5 0 5 3 5 -2 6 0No augmenting path found start from vertex 3.
  60. 60. Adjust Vertex Labeling 53 1 4 2 2 3 11 2 5 05 3 5 -2 6 0 R(1,6)=1 relax d = 1 R(2,6)=3
  61. 61. Adjust Vertex Labeling 52 1 4 3 2 3 10 2 5 14 3 5 -2 6 0 l(x) subtract value d. l(y) add value d.
  62. 62. Continue to Find Augmenting Path 5 2 1 4 3 2 3 1 0 2 5 1 4 3 5 -2 6 0[3 - 5 - 2 - 4 - 1 - 6] is an augment path with admissible edges.
  63. 63. Continue to Find Augmenting Path 5 2 1 4 3 2 3 1 0 2 5 1 4 3 5 -2 6 0[3 - 5 - 2 - 4 - 1 - 6] is an augment path with admissible edges.
  64. 64. Continue to Find Augmenting Path 5 2 1 4 3 2 3 1 0 2 5 1 4 3 5 -2 6 0 Reverse it! Current Weight = 10
  65. 65. Maximum Weight Matching = 10 5 2 1 4 3 2 3 1 0 2 5 1 4 3 5 -2 6 0
  66. 66. 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.
  67. 67. Practice NowPOJ 2195 - Going Home
  68. 68. Problem List UVa 670 UVa 753 UVa 10080 UVa 10092 UVa 10243 UVa 10418 UVa 10984 POJ 3565
  69. 69. 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
  70. 70. Thank You for Your Listening.
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×