SlideShare una empresa de Scribd logo
1 de 70
Descargar para leer sin conexión
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.

Más contenido relacionado

La actualidad más candente

Generalized Nonlinear Models in R
Generalized Nonlinear Models in RGeneralized Nonlinear Models in R
Generalized Nonlinear Models in Rhtstatistics
 
Isomorphic graph
Isomorphic graphIsomorphic graph
Isomorphic graphumair khan
 
Apriori algorithm
Apriori algorithmApriori algorithm
Apriori algorithmGangadhar S
 
Variational worksheets
Variational worksheetsVariational worksheets
Variational worksheetsJuliecadena
 
Branch and bounding : Data structures
Branch and bounding : Data structuresBranch and bounding : Data structures
Branch and bounding : Data structuresKàŕtheek Jåvvàjí
 
Str8ts Weekly Extreme #70 - Step-by-step Solution
Str8ts Weekly Extreme #70 - Step-by-step SolutionStr8ts Weekly Extreme #70 - Step-by-step Solution
Str8ts Weekly Extreme #70 - Step-by-step SolutionSlowThinker
 
No sql 이해 및 활용 공개용
No sql 이해 및 활용 공개용No sql 이해 및 활용 공개용
No sql 이해 및 활용 공개용YOUNGGYU CHUN
 
K Means Clustering Algorithm | K Means Example in Python | Machine Learning A...
K Means Clustering Algorithm | K Means Example in Python | Machine Learning A...K Means Clustering Algorithm | K Means Example in Python | Machine Learning A...
K Means Clustering Algorithm | K Means Example in Python | Machine Learning A...Edureka!
 

La actualidad más candente (12)

Generalized Nonlinear Models in R
Generalized Nonlinear Models in RGeneralized Nonlinear Models in R
Generalized Nonlinear Models in R
 
Isomorphic graph
Isomorphic graphIsomorphic graph
Isomorphic graph
 
Apriori algorithm
Apriori algorithmApriori algorithm
Apriori algorithm
 
Matrix algebra
Matrix algebraMatrix algebra
Matrix algebra
 
Variational worksheets
Variational worksheetsVariational worksheets
Variational worksheets
 
Branch and bounding : Data structures
Branch and bounding : Data structuresBranch and bounding : Data structures
Branch and bounding : Data structures
 
Graph theory
Graph theoryGraph theory
Graph theory
 
Trees
TreesTrees
Trees
 
Str8ts Weekly Extreme #70 - Step-by-step Solution
Str8ts Weekly Extreme #70 - Step-by-step SolutionStr8ts Weekly Extreme #70 - Step-by-step Solution
Str8ts Weekly Extreme #70 - Step-by-step Solution
 
No sql 이해 및 활용 공개용
No sql 이해 및 활용 공개용No sql 이해 및 활용 공개용
No sql 이해 및 활용 공개용
 
Survival analysis
Survival analysisSurvival analysis
Survival analysis
 
K Means Clustering Algorithm | K Means Example in Python | Machine Learning A...
K Means Clustering Algorithm | K Means Example in Python | Machine Learning A...K Means Clustering Algorithm | K Means Example in Python | Machine Learning A...
K Means Clustering Algorithm | K Means Example in Python | Machine Learning A...
 

Destacado

[ACM-ICPC] Dinic's Algorithm
[ACM-ICPC] Dinic's Algorithm[ACM-ICPC] Dinic's Algorithm
[ACM-ICPC] Dinic's AlgorithmChih-Hsuan Kuo
 
[ACM-ICPC] Tree Isomorphism
[ACM-ICPC] Tree Isomorphism[ACM-ICPC] Tree Isomorphism
[ACM-ICPC] Tree IsomorphismChih-Hsuan Kuo
 
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...Kostas Katrinis
 
ICPC 2015 - Welcome from the chairs
ICPC 2015 - Welcome from the chairsICPC 2015 - Welcome from the chairs
ICPC 2015 - Welcome from the chairsRocco Oliveto
 
[ACM-ICPC] Minimum Cut
[ACM-ICPC] Minimum Cut[ACM-ICPC] Minimum Cut
[ACM-ICPC] Minimum CutChih-Hsuan Kuo
 
Ownership System in Rust
Ownership System in RustOwnership System in Rust
Ownership System in RustChih-Hsuan Kuo
 
Graph terminologies & special type graphs
Graph terminologies & special type graphsGraph terminologies & special type graphs
Graph terminologies & special type graphsNabeel Ahsen
 
Knapsack Algorithm www.geekssay.com
Knapsack Algorithm www.geekssay.comKnapsack Algorithm www.geekssay.com
Knapsack Algorithm www.geekssay.comHemant Gautam
 
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...ertekg
 
Greedy Algorithm
Greedy AlgorithmGreedy Algorithm
Greedy AlgorithmWaqar Akram
 
01 knapsack using backtracking
01 knapsack using backtracking01 knapsack using backtracking
01 knapsack using backtrackingmandlapure
 

Destacado (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
 

Más de Chih-Hsuan Kuo

[Mozilla] content-select
[Mozilla] content-select[Mozilla] content-select
[Mozilla] content-selectChih-Hsuan Kuo
 
在開始工作以前,我以為我會寫扣。
在開始工作以前,我以為我會寫扣。在開始工作以前,我以為我會寫扣。
在開始工作以前,我以為我會寫扣。Chih-Hsuan Kuo
 
Effective Modern C++ - Item 35 & 36
Effective Modern C++ - Item 35 & 36Effective Modern C++ - Item 35 & 36
Effective Modern C++ - Item 35 & 36Chih-Hsuan Kuo
 
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 GeckoChih-Hsuan Kuo
 
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 OSChih-Hsuan Kuo
 
Protocol handler in Gecko
Protocol handler in GeckoProtocol handler in Gecko
Protocol handler in GeckoChih-Hsuan Kuo
 
面試面試面試,因為很重要所以要說三次!
面試面試面試,因為很重要所以要說三次!面試面試面試,因為很重要所以要說三次!
面試面試面試,因為很重要所以要說三次!Chih-Hsuan Kuo
 
Windows 真的不好用...
Windows 真的不好用...Windows 真的不好用...
Windows 真的不好用...Chih-Hsuan Kuo
 
[ACM-ICPC] Disjoint Set
[ACM-ICPC] Disjoint Set[ACM-ICPC] Disjoint Set
[ACM-ICPC] Disjoint SetChih-Hsuan Kuo
 
[ACM-ICPC] Efficient Algorithm
[ACM-ICPC] Efficient Algorithm[ACM-ICPC] Efficient Algorithm
[ACM-ICPC] Efficient AlgorithmChih-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-upChih-Hsuan Kuo
 

Más de 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
 

Último

Python Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxPython Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxRamakrishna Reddy Bijjam
 
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...ZurliaSoop
 
SOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning PresentationSOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning Presentationcamerronhm
 
Fostering Friendships - Enhancing Social Bonds in the Classroom
Fostering Friendships - Enhancing Social Bonds  in the ClassroomFostering Friendships - Enhancing Social Bonds  in the Classroom
Fostering Friendships - Enhancing Social Bonds in the ClassroomPooky Knightsmith
 
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...christianmathematics
 
Micro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdfMicro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdfPoh-Sun Goh
 
Application orientated numerical on hev.ppt
Application orientated numerical on hev.pptApplication orientated numerical on hev.ppt
Application orientated numerical on hev.pptRamjanShidvankar
 
How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17Celine George
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfciinovamais
 
Graduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - EnglishGraduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - Englishneillewis46
 
Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)Jisc
 
Unit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptxUnit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptxVishalSingh1417
 
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...pradhanghanshyam7136
 
Understanding Accommodations and Modifications
Understanding  Accommodations and ModificationsUnderstanding  Accommodations and Modifications
Understanding Accommodations and ModificationsMJDuyan
 
Spellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please PractiseSpellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please PractiseAnaAcapella
 
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 17Celine George
 
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdfUGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdfNirmal Dwivedi
 
ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.MaryamAhmad92
 
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptxMaritesTamaniVerdade
 
Salient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functionsSalient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functionsKarakKing
 

Último (20)

Python Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxPython Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docx
 
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
 
SOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning PresentationSOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning Presentation
 
Fostering Friendships - Enhancing Social Bonds in the Classroom
Fostering Friendships - Enhancing Social Bonds  in the ClassroomFostering Friendships - Enhancing Social Bonds  in the Classroom
Fostering Friendships - Enhancing Social Bonds in the Classroom
 
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
 
Micro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdfMicro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdf
 
Application orientated numerical on hev.ppt
Application orientated numerical on hev.pptApplication orientated numerical on hev.ppt
Application orientated numerical on hev.ppt
 
How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdf
 
Graduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - EnglishGraduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - English
 
Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)
 
Unit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptxUnit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptx
 
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
 
Understanding Accommodations and Modifications
Understanding  Accommodations and ModificationsUnderstanding  Accommodations and Modifications
Understanding Accommodations and Modifications
 
Spellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please PractiseSpellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please Practise
 
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
 
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdfUGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
 
ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.
 
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
 
Salient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functionsSalient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functions
 

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