SlideShare a Scribd company logo
1 of 70
Download to read offline
Bipartite Matching
      郭至軒(KuoE0)
     KuoE0.tw@gmail.com
          KuoE0.ch
Bipartite Graph
                 two disjoint sets




every edge connects between them
Matching
A 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         8

not matching edge   9
matching edge
Alternating Path
A 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                     7

          4                     8

not matching edge               9
matching edge
[1 - 5 - 2 - 9] is an alternating path.

          1                     5

          2                     6

          3                     7

          4                     8

not matching edge               9
matching edge
[1 - 5 - 2 - 6 - 3] is not an alternating path.

           1                     5

           2                     6

           3                     7

           4                     8

not matching edge                9
matching edge
[1 - 5 - 2 - 6 - 3] is not an alternating path.

           1                     5

           2                     6

           3                     7

           4                     8

not matching edge                9
matching edge
Alternating Cycle
A 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

           3                     7

           4                     8

not matching edge                9
matching edge
[1 - 5 - 2 - 9 - 1] is an alternating cycle.

           1                     5

           2                     6

           3                     7

           4                     8

not matching edge                9
matching edge
Reverse the alternating cycle, cardinality won’t change.

                1                    5

                2                    6

                3                    7

                4                    8

     not matching edge               9
     matching edge
Maximum Matching
Augmenting Path Algorithm
Augmenting Path
An alternating path that starts from and ends on
              unmatched vertices.
[2 - 5 - 1 - 9] is an augmenting path.

          1                   5

          2                   6

          3                   7

          4                   8

not matching edge             9
matching edge
[2 - 5 - 1 - 9] is an augmenting path.

          1                   5

          2                   6

          3                   7

          4                   8

not matching edge             9
matching edge
Reverse the augmenting path, cardinality will increase.

                1                    5

                2                    6

                3                    7

                4                    8

     not matching edge               9
     matching edge
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.
Algorithm
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.
[1 - 5] is an augmenting path.

               1                     5

               2                     6

               3                     7

               4                     8

current cardinality: 0
                                     9
    not matching edge
    matching edge
[1 - 5] is an augmenting path.

               1                     5

               2                     6

               3                     7

               4                     8

current cardinality: 0
                                     9
    not matching edge
    matching edge
Reverse it!

               1                   5

               2                   6

               3                   7

               4                   8

current cardinality: 1
                                   9
    not matching edge
    matching edge
[2 - 5 - 1- 9] is an augmenting path.

               1                      5

               2                      6

               3                      7

               4                      8

current cardinality: 1
                                      9
    not matching edge
    matching edge
[2 - 5 - 1- 9] is an augmenting path.

               1                      5

               2                      6

               3                      7

               4                      8

current cardinality: 1
                                      9
    not matching edge
    matching edge
Reverse it!

               1                   5

               2                   6

               3                   7

               4                   8

current cardinality: 2
                                   9
    not matching edge
    matching edge
[3 - 6] is an augmenting path.

               1                     5

               2                     6

               3                     7

               4                     8

current cardinality: 2
                                     9
    not matching edge
    matching edge
[3 - 6] is an augmenting path.

               1                     5

               2                     6

               3                     7

               4                     8

current cardinality: 2
                                     9
    not matching edge
    matching edge
Reverse it!

               1                   5

               2                   6

               3                   7

               4                   8

current cardinality: 3
                                   9
    not matching edge
    matching edge
[4 - 5 - 2 - 6 - 3 - 7] is an augmenting path.

                1                      5

                2                      6

                3                      7

                4                      8

current cardinality: 3
                                       9
    not matching edge
    matching edge
[4 - 5 - 2 - 6 - 3 - 7] is an augmenting path.

                1                      5

                2                      6

                3                      7

                4                      8

current cardinality: 3
                                       9
    not matching edge
    matching edge
Reverse it!

               1                   5

               2                   6

               3                   7

               4                   8

current cardinality: 4
                                   9
    not matching edge
    matching edge
Max cardinality is 4.
          1                  5

          2                  6

          3                  7

          4                  8

                             9
not matching edge
matching edge
Time Complexity
      O(V × E)

  V: number of vertices
  E: number of edges
Source Code
// find an augmenting path
bool 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;
}
Practice Now
 UVa 670 - The dog task
Maximum Weight Matching
Hungarian Algorithm
         (Kuhn-Munkres Algorithm)
5
1                4
    2   3
            1
2                5

3   5       -2   6
weight adjustment
If 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
vertex labeling
For 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
vertex labeling
l(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
minimize vertex labeling
Admissible 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
convert
   maximum weight problem
              to
minimum vertex labeling problem
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.
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.
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
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.
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.
Adjust Vertex Labeling

                    5
    5   1                         4   0
              2     3
                           1
   3    2                         5   0

    5   3     5            -2     6   0

relax value d = min(l(x)+l(y)-w(x,y))
    x: vertex in alternating path
    y: vertex y not in alternating path
Adjust Vertex Labeling

                   5
5    1                          4    0
           2       3
                         1
3    2                          5    0

5    3         5         -2     6    0

    R(1,6)=3
    R(2,5)=2           relax d = 2
    R(2,6)=5
Adjust Vertex Labeling

                  5
3   1                            4   2
            2     3
                        1
1   2                            5   0

5   3       5           -2       6   0

        l(x) subtract value d.
        l(y) add value d.
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.
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.
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
Continue to Find Augmenting Path

                     5
    3   1                          4   2
               2     3
                            1
    1   2                          5   0

    5   3      5           -2      6   0


No augmenting path found start from vertex 3.
Continue to Find Augmenting Path

                     5
    3   1                          4   2
               2     3
                            1
    1   2                          5   0

    5   3      5           -2      6   0


No augmenting path found start from vertex 3.
Adjust Vertex Labeling

                   5
3    1                          4    2
           2       3
                         1
1    2                          5    0

5    3         5         -2     6    0

    R(1,6)=1
                       relax d = 1
    R(2,6)=3
Adjust Vertex Labeling

                  5
2   1                            4   3
            2     3
                        1
0   2                            5   1

4   3       5           -2       6   0

        l(x) subtract value d.
        l(y) add value d.
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.
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.
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
Maximum Weight Matching = 10

              5
  2   1                4   3
          2   3
                  1
  0   2                5   1

  4   3   5       -2   6   0
Algorithm
1. 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.
Practice Now
POJ 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://www.flickr.com/photos/marceau_r/5244129689

•   http://www.sanfilippo-chianti.it/offerta-svalentino.html

•   http://www.csie.ntnu.edu.tw/~u91029/Matching.html
Thank You for Your
    Listening.

More Related Content

What's hot

Principles of soft computing-Associative memory networks
Principles of soft computing-Associative memory networksPrinciples of soft computing-Associative memory networks
Principles of soft computing-Associative memory networks
Sivagowry Shathesh
 

What's hot (20)

Graph coloring problem(DAA).pptx
Graph coloring problem(DAA).pptxGraph coloring problem(DAA).pptx
Graph coloring problem(DAA).pptx
 
Genetic Algorithms
Genetic AlgorithmsGenetic Algorithms
Genetic Algorithms
 
C++ integer arithmetic
C++ integer arithmeticC++ integer arithmetic
C++ integer arithmetic
 
Adaptive Resonance Theory (ART)
Adaptive Resonance Theory (ART)Adaptive Resonance Theory (ART)
Adaptive Resonance Theory (ART)
 
Pca ankita dubey
Pca ankita dubeyPca ankita dubey
Pca ankita dubey
 
Machine Learning with Applications in Categorization, Popularity and Sequence...
Machine Learning with Applications in Categorization, Popularity and Sequence...Machine Learning with Applications in Categorization, Popularity and Sequence...
Machine Learning with Applications in Categorization, Popularity and Sequence...
 
Graph colouring
Graph colouringGraph colouring
Graph colouring
 
Lecture 23 alpha beta pruning
Lecture 23 alpha beta pruningLecture 23 alpha beta pruning
Lecture 23 alpha beta pruning
 
Minimum spanning tree
Minimum spanning treeMinimum spanning tree
Minimum spanning tree
 
Dmitry Larko, H2O.ai - Kaggle Airbus Ship Detection Challenge - H2O World San...
Dmitry Larko, H2O.ai - Kaggle Airbus Ship Detection Challenge - H2O World San...Dmitry Larko, H2O.ai - Kaggle Airbus Ship Detection Challenge - H2O World San...
Dmitry Larko, H2O.ai - Kaggle Airbus Ship Detection Challenge - H2O World San...
 
Principles of soft computing-Associative memory networks
Principles of soft computing-Associative memory networksPrinciples of soft computing-Associative memory networks
Principles of soft computing-Associative memory networks
 
Local search algorithm
Local search algorithmLocal search algorithm
Local search algorithm
 
Graph Based Clustering
Graph Based ClusteringGraph Based Clustering
Graph Based Clustering
 
Dynamic programming
Dynamic programmingDynamic programming
Dynamic programming
 
AMBA 5 COHERENT HUB INTERFACE.pptx
AMBA 5 COHERENT HUB INTERFACE.pptxAMBA 5 COHERENT HUB INTERFACE.pptx
AMBA 5 COHERENT HUB INTERFACE.pptx
 
Alpha beta
Alpha betaAlpha beta
Alpha beta
 
Genetic Algorithm by Example
Genetic Algorithm by ExampleGenetic Algorithm by Example
Genetic Algorithm by Example
 
Lecture 24 iterative improvement algorithm
Lecture 24 iterative improvement algorithmLecture 24 iterative improvement algorithm
Lecture 24 iterative improvement algorithm
 
Verification of amba axi bus protocol implementing incr and wrap burst using ...
Verification of amba axi bus protocol implementing incr and wrap burst using ...Verification of amba axi bus protocol implementing incr and wrap burst using ...
Verification of amba axi bus protocol implementing incr and wrap burst using ...
 
System verilog assertions
System verilog assertionsSystem verilog assertions
System verilog assertions
 

Viewers also liked

[ACM-ICPC] Dinic's Algorithm
[ACM-ICPC] Dinic's Algorithm[ACM-ICPC] Dinic's Algorithm
[ACM-ICPC] Dinic's Algorithm
Chih-Hsuan Kuo
 
[ACM-ICPC] Tree Isomorphism
[ACM-ICPC] Tree Isomorphism[ACM-ICPC] Tree Isomorphism
[ACM-ICPC] Tree Isomorphism
Chih-Hsuan Kuo
 
[ACM-ICPC] Minimum Cut
[ACM-ICPC] Minimum Cut[ACM-ICPC] Minimum Cut
[ACM-ICPC] Minimum Cut
Chih-Hsuan Kuo
 
01 knapsack using backtracking
01 knapsack using backtracking01 knapsack using backtracking
01 knapsack using backtracking
mandlapure
 

Viewers also liked (20)

[ACM-ICPC] Dinic's Algorithm
[ACM-ICPC] Dinic's Algorithm[ACM-ICPC] Dinic's Algorithm
[ACM-ICPC] Dinic's Algorithm
 
[ACM-ICPC] Tree Isomorphism
[ACM-ICPC] Tree Isomorphism[ACM-ICPC] Tree Isomorphism
[ACM-ICPC] Tree Isomorphism
 
Teaching Graph Algorithms in the Field - Bipartite Matching in optical datace...
Teaching Graph Algorithms in the Field - Bipartite Matching in optical datace...Teaching Graph Algorithms in the Field - Bipartite Matching in optical datace...
Teaching Graph Algorithms in the Field - Bipartite Matching in optical datace...
 
[ACM-ICPC] Traversal
[ACM-ICPC] Traversal[ACM-ICPC] Traversal
[ACM-ICPC] Traversal
 
ICPC 2015 - Welcome from the chairs
ICPC 2015 - Welcome from the chairsICPC 2015 - Welcome from the chairs
ICPC 2015 - Welcome from the chairs
 
[ACM-ICPC] Minimum Cut
[ACM-ICPC] Minimum Cut[ACM-ICPC] Minimum Cut
[ACM-ICPC] Minimum Cut
 
Ownership System in Rust
Ownership System in RustOwnership System in Rust
Ownership System in Rust
 
Graphs
GraphsGraphs
Graphs
 
Graph terminologies & special type graphs
Graph terminologies & special type graphsGraph terminologies & special type graphs
Graph terminologies & special type graphs
 
Knapsack Algorithm www.geekssay.com
Knapsack Algorithm www.geekssay.comKnapsack Algorithm www.geekssay.com
Knapsack Algorithm www.geekssay.com
 
Application of local search methods for solving a quadratic assignment proble...
Application of local search methods for solving a quadratic assignment proble...Application of local search methods for solving a quadratic assignment proble...
Application of local search methods for solving a quadratic assignment proble...
 
Knapsack problem
Knapsack problemKnapsack problem
Knapsack problem
 
Greedy Algorithm
Greedy AlgorithmGreedy Algorithm
Greedy Algorithm
 
01 knapsack using backtracking
01 knapsack using backtracking01 knapsack using backtracking
01 knapsack using backtracking
 
Greedy
GreedyGreedy
Greedy
 
Greedy Algorihm
Greedy AlgorihmGreedy Algorihm
Greedy Algorihm
 
Knapsack
KnapsackKnapsack
Knapsack
 
Greedy algorithm
Greedy algorithmGreedy algorithm
Greedy algorithm
 
Greedy Algorithms
Greedy AlgorithmsGreedy Algorithms
Greedy Algorithms
 
Knapsack Problem
Knapsack ProblemKnapsack Problem
Knapsack Problem
 

More from Chih-Hsuan Kuo

[ACM-ICPC] Disjoint Set
[ACM-ICPC] Disjoint Set[ACM-ICPC] Disjoint Set
[ACM-ICPC] Disjoint Set
Chih-Hsuan Kuo
 
[ACM-ICPC] Efficient Algorithm
[ACM-ICPC] Efficient Algorithm[ACM-ICPC] Efficient Algorithm
[ACM-ICPC] Efficient Algorithm
Chih-Hsuan Kuo
 
[ACM-ICPC] Top-down & Bottom-up
[ACM-ICPC] Top-down & Bottom-up[ACM-ICPC] Top-down & Bottom-up
[ACM-ICPC] Top-down & Bottom-up
Chih-Hsuan Kuo
 

More from Chih-Hsuan Kuo (20)

Rust
RustRust
Rust
 
[Mozilla] content-select
[Mozilla] content-select[Mozilla] content-select
[Mozilla] content-select
 
在開始工作以前,我以為我會寫扣。
在開始工作以前,我以為我會寫扣。在開始工作以前,我以為我會寫扣。
在開始工作以前,我以為我會寫扣。
 
Effective Modern C++ - Item 35 & 36
Effective Modern C++ - Item 35 & 36Effective Modern C++ - Item 35 & 36
Effective Modern C++ - Item 35 & 36
 
Use C++ to Manipulate mozSettings in Gecko
Use C++ to Manipulate mozSettings in GeckoUse C++ to Manipulate mozSettings in Gecko
Use C++ to Manipulate mozSettings in Gecko
 
Pocket Authentication with OAuth on Firefox OS
Pocket Authentication with OAuth on Firefox OSPocket Authentication with OAuth on Firefox OS
Pocket Authentication with OAuth on Firefox OS
 
Necko walkthrough
Necko walkthroughNecko walkthrough
Necko walkthrough
 
Protocol handler in Gecko
Protocol handler in GeckoProtocol handler in Gecko
Protocol handler in Gecko
 
面試面試面試,因為很重要所以要說三次!
面試面試面試,因為很重要所以要說三次!面試面試面試,因為很重要所以要說三次!
面試面試面試,因為很重要所以要說三次!
 
應徵軟體工程師
應徵軟體工程師應徵軟體工程師
應徵軟體工程師
 
面試心得分享
面試心得分享面試心得分享
面試心得分享
 
Windows 真的不好用...
Windows 真的不好用...Windows 真的不好用...
Windows 真的不好用...
 
Python @Wheel Lab
Python @Wheel LabPython @Wheel Lab
Python @Wheel Lab
 
Introduction to VP8
Introduction to VP8Introduction to VP8
Introduction to VP8
 
Python @NCKU CSIE
Python @NCKU CSIEPython @NCKU CSIE
Python @NCKU CSIE
 
[ACM-ICPC] Disjoint Set
[ACM-ICPC] Disjoint Set[ACM-ICPC] Disjoint Set
[ACM-ICPC] Disjoint Set
 
[ACM-ICPC] UVa-10245
[ACM-ICPC] UVa-10245[ACM-ICPC] UVa-10245
[ACM-ICPC] UVa-10245
 
[ACM-ICPC] Sort
[ACM-ICPC] Sort[ACM-ICPC] Sort
[ACM-ICPC] Sort
 
[ACM-ICPC] Efficient Algorithm
[ACM-ICPC] Efficient Algorithm[ACM-ICPC] Efficient Algorithm
[ACM-ICPC] Efficient Algorithm
 
[ACM-ICPC] Top-down & Bottom-up
[ACM-ICPC] Top-down & Bottom-up[ACM-ICPC] Top-down & Bottom-up
[ACM-ICPC] Top-down & Bottom-up
 

Recently uploaded

QUATER-1-PE-HEALTH-LC2- this is just a sample of unpacked lesson
QUATER-1-PE-HEALTH-LC2- this is just a sample of unpacked lessonQUATER-1-PE-HEALTH-LC2- this is just a sample of unpacked lesson
QUATER-1-PE-HEALTH-LC2- this is just a sample of unpacked lesson
httgc7rh9c
 

Recently uploaded (20)

How to Add New Custom Addons Path in Odoo 17
How to Add New Custom Addons Path in Odoo 17How to Add New Custom Addons Path in Odoo 17
How to Add New Custom Addons Path in Odoo 17
 
How to setup Pycharm environment for Odoo 17.pptx
How to setup Pycharm environment for Odoo 17.pptxHow to setup Pycharm environment for Odoo 17.pptx
How to setup Pycharm environment for Odoo 17.pptx
 
COMMUNICATING NEGATIVE NEWS - APPROACHES .pptx
COMMUNICATING NEGATIVE NEWS - APPROACHES .pptxCOMMUNICATING NEGATIVE NEWS - APPROACHES .pptx
COMMUNICATING NEGATIVE NEWS - APPROACHES .pptx
 
NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...
NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...
NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...
 
Economic Importance Of Fungi In Food Additives
Economic Importance Of Fungi In Food AdditivesEconomic Importance Of Fungi In Food Additives
Economic Importance Of Fungi In Food Additives
 
AIM of Education-Teachers Training-2024.ppt
AIM of Education-Teachers Training-2024.pptAIM of Education-Teachers Training-2024.ppt
AIM of Education-Teachers Training-2024.ppt
 
Our Environment Class 10 Science Notes pdf
Our Environment Class 10 Science Notes pdfOur Environment Class 10 Science Notes pdf
Our Environment Class 10 Science Notes pdf
 
How to Add a Tool Tip to a Field in Odoo 17
How to Add a Tool Tip to a Field in Odoo 17How to Add a Tool Tip to a Field in Odoo 17
How to Add a Tool Tip to a Field in Odoo 17
 
How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17
 
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdf
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdfUnit 3 Emotional Intelligence and Spiritual Intelligence.pdf
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdf
 
Graduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - EnglishGraduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - English
 
PANDITA RAMABAI- Indian political thought GENDER.pptx
PANDITA RAMABAI- Indian political thought GENDER.pptxPANDITA RAMABAI- Indian political thought GENDER.pptx
PANDITA RAMABAI- Indian political thought GENDER.pptx
 
How to Manage Call for Tendor in Odoo 17
How to Manage Call for Tendor in Odoo 17How to Manage Call for Tendor in Odoo 17
How to Manage Call for Tendor in Odoo 17
 
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
 
QUATER-1-PE-HEALTH-LC2- this is just a sample of unpacked lesson
QUATER-1-PE-HEALTH-LC2- this is just a sample of unpacked lessonQUATER-1-PE-HEALTH-LC2- this is just a sample of unpacked lesson
QUATER-1-PE-HEALTH-LC2- this is just a sample of unpacked lesson
 
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptxHMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
 
Tatlong Kwento ni Lola basyang-1.pdf arts
Tatlong Kwento ni Lola basyang-1.pdf artsTatlong Kwento ni Lola basyang-1.pdf arts
Tatlong Kwento ni Lola basyang-1.pdf arts
 
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
 
Understanding Accommodations and Modifications
Understanding  Accommodations and ModificationsUnderstanding  Accommodations and Modifications
Understanding Accommodations and Modifications
 
Interdisciplinary_Insights_Data_Collection_Methods.pptx
Interdisciplinary_Insights_Data_Collection_Methods.pptxInterdisciplinary_Insights_Data_Collection_Methods.pptx
Interdisciplinary_Insights_Data_Collection_Methods.pptx
 

[ACM-ICPC] Bipartite Matching

  • 1. Bipartite Matching 郭至軒(KuoE0) KuoE0.tw@gmail.com KuoE0.ch
  • 2. Bipartite Graph two disjoint sets every edge connects between them
  • 3. Matching A subset of edges, no two of which share an endpoint.
  • 4. Valid Matching not matching edge matching edge bachelor
  • 5. Invalid Matching not matching edge matching edge shared
  • 6. Bipartite Matching objective: more matching
  • 7. 1 5 2 6 3 7 4 8 not matching edge 9 matching edge
  • 8. Alternating Path A path in which the edges belong alternatively to the matching and not to the matching.
  • 9. [1 - 5 - 2 - 9] is an alternating path. 1 5 2 6 3 7 4 8 not matching edge 9 matching edge
  • 10. [1 - 5 - 2 - 9] is an alternating path. 1 5 2 6 3 7 4 8 not matching edge 9 matching edge
  • 11. [1 - 5 - 2 - 6 - 3] is not an alternating path. 1 5 2 6 3 7 4 8 not matching edge 9 matching edge
  • 12. [1 - 5 - 2 - 6 - 3] is not an alternating path. 1 5 2 6 3 7 4 8 not matching edge 9 matching edge
  • 13. Alternating Cycle A cycle in which the edges belong alternatively to the matching and not to the matching.
  • 14. [1 - 5 - 2 - 9 - 1] is an alternating cycle. 1 5 2 6 3 7 4 8 not matching edge 9 matching edge
  • 15. [1 - 5 - 2 - 9 - 1] is an alternating cycle. 1 5 2 6 3 7 4 8 not matching edge 9 matching edge
  • 16. Reverse the alternating cycle, cardinality won’t change. 1 5 2 6 3 7 4 8 not matching edge 9 matching edge
  • 18. Augmenting Path An alternating path that starts from and ends on unmatched vertices.
  • 19. [2 - 5 - 1 - 9] is an augmenting path. 1 5 2 6 3 7 4 8 not matching edge 9 matching edge
  • 20. [2 - 5 - 1 - 9] is an augmenting path. 1 5 2 6 3 7 4 8 not matching edge 9 matching edge
  • 21. Reverse the augmenting path, cardinality will increase. 1 5 2 6 3 7 4 8 not matching edge 9 matching edge
  • 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. Algorithm 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.
  • 24. [1 - 5] is an augmenting path. 1 5 2 6 3 7 4 8 current cardinality: 0 9 not matching edge matching edge
  • 25. [1 - 5] is an augmenting path. 1 5 2 6 3 7 4 8 current cardinality: 0 9 not matching edge matching edge
  • 26. Reverse it! 1 5 2 6 3 7 4 8 current cardinality: 1 9 not matching edge matching edge
  • 27. [2 - 5 - 1- 9] is an augmenting path. 1 5 2 6 3 7 4 8 current cardinality: 1 9 not matching edge matching edge
  • 28. [2 - 5 - 1- 9] is an augmenting path. 1 5 2 6 3 7 4 8 current cardinality: 1 9 not matching edge matching edge
  • 29. Reverse it! 1 5 2 6 3 7 4 8 current cardinality: 2 9 not matching edge matching edge
  • 30. [3 - 6] is an augmenting path. 1 5 2 6 3 7 4 8 current cardinality: 2 9 not matching edge matching edge
  • 31. [3 - 6] is an augmenting path. 1 5 2 6 3 7 4 8 current cardinality: 2 9 not matching edge matching edge
  • 32. Reverse it! 1 5 2 6 3 7 4 8 current cardinality: 3 9 not matching edge matching edge
  • 33. [4 - 5 - 2 - 6 - 3 - 7] is an augmenting path. 1 5 2 6 3 7 4 8 current cardinality: 3 9 not matching edge matching edge
  • 34. [4 - 5 - 2 - 6 - 3 - 7] is an augmenting path. 1 5 2 6 3 7 4 8 current cardinality: 3 9 not matching edge matching edge
  • 35. Reverse it! 1 5 2 6 3 7 4 8 current cardinality: 4 9 not matching edge matching edge
  • 36. Max cardinality is 4. 1 5 2 6 3 7 4 8 9 not matching edge matching edge
  • 37. Time Complexity O(V × E) V: number of vertices E: number of edges
  • 38. Source Code // find an augmenting path bool 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; }
  • 39. Practice Now UVa 670 - The dog task
  • 40. Maximum Weight Matching Hungarian Algorithm (Kuhn-Munkres Algorithm)
  • 41. 5 1 4 2 3 1 2 5 3 5 -2 6
  • 42. weight adjustment If 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. vertex labeling For 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. vertex labeling l(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. minimize vertex labeling Admissible 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. convert maximum weight problem to minimum vertex labeling problem
  • 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. 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. 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. 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. 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. Adjust Vertex Labeling 5 5 1 4 0 2 3 1 3 2 5 0 5 3 5 -2 6 0 relax value d = min(l(x)+l(y)-w(x,y)) x: vertex in alternating path y: vertex y not in alternating path
  • 53. Adjust Vertex Labeling 5 5 1 4 0 2 3 1 3 2 5 0 5 3 5 -2 6 0 R(1,6)=3 R(2,5)=2 relax d = 2 R(2,6)=5
  • 54. Adjust Vertex Labeling 5 3 1 4 2 2 3 1 1 2 5 0 5 3 5 -2 6 0 l(x) subtract value d. l(y) add value d.
  • 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. 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. 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. Continue to Find Augmenting Path 5 3 1 4 2 2 3 1 1 2 5 0 5 3 5 -2 6 0 No augmenting path found start from vertex 3.
  • 59. Continue to Find Augmenting Path 5 3 1 4 2 2 3 1 1 2 5 0 5 3 5 -2 6 0 No augmenting path found start from vertex 3.
  • 60. Adjust Vertex Labeling 5 3 1 4 2 2 3 1 1 2 5 0 5 3 5 -2 6 0 R(1,6)=1 relax d = 1 R(2,6)=3
  • 61. Adjust Vertex Labeling 5 2 1 4 3 2 3 1 0 2 5 1 4 3 5 -2 6 0 l(x) subtract value d. l(y) add value d.
  • 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. 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. 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. Maximum Weight Matching = 10 5 2 1 4 3 2 3 1 0 2 5 1 4 3 5 -2 6 0
  • 66. Algorithm 1. 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. Practice Now POJ 2195 - Going Home
  • 68. Problem List UVa 670 UVa 753 UVa 10080 UVa 10092 UVa 10243 UVa 10418 UVa 10984 POJ 3565
  • 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. Thank You for Your Listening.