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

unit-4-dynamic programming
unit-4-dynamic programmingunit-4-dynamic programming
unit-4-dynamic programminghodcsencet
 
Graph isomorphism
Graph isomorphismGraph isomorphism
Graph isomorphismCore Condor
 
The Stochastic Simulation Algorithm
The Stochastic Simulation AlgorithmThe Stochastic Simulation Algorithm
The Stochastic Simulation AlgorithmStephen Gilmore
 
Interesting applications of graph theory
Interesting applications of graph theoryInteresting applications of graph theory
Interesting applications of graph theoryTech_MX
 
Graph theory with algorithms and its applications
Graph theory with algorithms and its applicationsGraph theory with algorithms and its applications
Graph theory with algorithms and its applicationsSpringer
 
Understanding quadrilaterals chapter 3 NCERT
Understanding quadrilaterals chapter 3 NCERT Understanding quadrilaterals chapter 3 NCERT
Understanding quadrilaterals chapter 3 NCERT jai3077
 
3.2 Managing Engineering Design.ppt
3.2 Managing Engineering Design.ppt3.2 Managing Engineering Design.ppt
3.2 Managing Engineering Design.pptKrishnaGupta191
 
Classification of matrix
Classification of matrixClassification of matrix
Classification of matrixPRANTO5555
 
ABC-GSX:Hybrid method to solve TSP
ABC-GSX:Hybrid method to solve TSPABC-GSX:Hybrid method to solve TSP
ABC-GSX:Hybrid method to solve TSPgauravon
 
A study on number theory and its applications
A study on number theory and its applicationsA study on number theory and its applications
A study on number theory and its applicationsItishree Dash
 
Graph Theory Introduction
Graph Theory IntroductionGraph Theory Introduction
Graph Theory IntroductionMANISH T I
 
Matrix Representation Of Graph
Matrix Representation Of GraphMatrix Representation Of Graph
Matrix Representation Of GraphAbhishek Pachisia
 

What's hot (20)

Shortest path algorithms
Shortest path algorithmsShortest path algorithms
Shortest path algorithms
 
8 queens problem.pptx
8 queens problem.pptx8 queens problem.pptx
8 queens problem.pptx
 
unit-4-dynamic programming
unit-4-dynamic programmingunit-4-dynamic programming
unit-4-dynamic programming
 
Graph isomorphism
Graph isomorphismGraph isomorphism
Graph isomorphism
 
Tsp branch and bound
Tsp branch and boundTsp branch and bound
Tsp branch and bound
 
The Stochastic Simulation Algorithm
The Stochastic Simulation AlgorithmThe Stochastic Simulation Algorithm
The Stochastic Simulation Algorithm
 
graphtheory
graphtheorygraphtheory
graphtheory
 
Interesting applications of graph theory
Interesting applications of graph theoryInteresting applications of graph theory
Interesting applications of graph theory
 
Graph theory with algorithms and its applications
Graph theory with algorithms and its applicationsGraph theory with algorithms and its applications
Graph theory with algorithms and its applications
 
Understanding quadrilaterals chapter 3 NCERT
Understanding quadrilaterals chapter 3 NCERT Understanding quadrilaterals chapter 3 NCERT
Understanding quadrilaterals chapter 3 NCERT
 
3.2 Managing Engineering Design.ppt
3.2 Managing Engineering Design.ppt3.2 Managing Engineering Design.ppt
3.2 Managing Engineering Design.ppt
 
Classification of matrix
Classification of matrixClassification of matrix
Classification of matrix
 
Graphs
GraphsGraphs
Graphs
 
KMP String Matching Algorithm
KMP String Matching AlgorithmKMP String Matching Algorithm
KMP String Matching Algorithm
 
ABC-GSX:Hybrid method to solve TSP
ABC-GSX:Hybrid method to solve TSPABC-GSX:Hybrid method to solve TSP
ABC-GSX:Hybrid method to solve TSP
 
A study on number theory and its applications
A study on number theory and its applicationsA study on number theory and its applications
A study on number theory and its applications
 
Graph Theory Introduction
Graph Theory IntroductionGraph Theory Introduction
Graph Theory Introduction
 
Quadrilateral
Quadrilateral Quadrilateral
Quadrilateral
 
Matrix Representation Of Graph
Matrix Representation Of GraphMatrix Representation Of Graph
Matrix Representation Of Graph
 
Planar graph
Planar graphPlanar graph
Planar graph
 

Viewers also liked

[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
 

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

[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
 

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

How to Make a Duplicate of Your Odoo 17 Database
How to Make a Duplicate of Your Odoo 17 DatabaseHow to Make a Duplicate of Your Odoo 17 Database
How to Make a Duplicate of Your Odoo 17 DatabaseCeline George
 
Reading and Writing Skills 11 quarter 4 melc 1
Reading and Writing Skills 11 quarter 4 melc 1Reading and Writing Skills 11 quarter 4 melc 1
Reading and Writing Skills 11 quarter 4 melc 1GloryAnnCastre1
 
Unraveling Hypertext_ Analyzing Postmodern Elements in Literature.pptx
Unraveling Hypertext_ Analyzing  Postmodern Elements in  Literature.pptxUnraveling Hypertext_ Analyzing  Postmodern Elements in  Literature.pptx
Unraveling Hypertext_ Analyzing Postmodern Elements in Literature.pptxDhatriParmar
 
31 ĐỀ THI THỬ VÀO LỚP 10 - TIẾNG ANH - FORM MỚI 2025 - 40 CÂU HỎI - BÙI VĂN V...
31 ĐỀ THI THỬ VÀO LỚP 10 - TIẾNG ANH - FORM MỚI 2025 - 40 CÂU HỎI - BÙI VĂN V...31 ĐỀ THI THỬ VÀO LỚP 10 - TIẾNG ANH - FORM MỚI 2025 - 40 CÂU HỎI - BÙI VĂN V...
31 ĐỀ THI THỬ VÀO LỚP 10 - TIẾNG ANH - FORM MỚI 2025 - 40 CÂU HỎI - BÙI VĂN V...Nguyen Thanh Tu Collection
 
Narcotic and Non Narcotic Analgesic..pdf
Narcotic and Non Narcotic Analgesic..pdfNarcotic and Non Narcotic Analgesic..pdf
Narcotic and Non Narcotic Analgesic..pdfPrerana Jadhav
 
ARTERIAL BLOOD GAS ANALYSIS........pptx
ARTERIAL BLOOD  GAS ANALYSIS........pptxARTERIAL BLOOD  GAS ANALYSIS........pptx
ARTERIAL BLOOD GAS ANALYSIS........pptxAneriPatwari
 
ICS2208 Lecture6 Notes for SL spaces.pdf
ICS2208 Lecture6 Notes for SL spaces.pdfICS2208 Lecture6 Notes for SL spaces.pdf
ICS2208 Lecture6 Notes for SL spaces.pdfVanessa Camilleri
 
Concurrency Control in Database Management system
Concurrency Control in Database Management systemConcurrency Control in Database Management system
Concurrency Control in Database Management systemChristalin Nelson
 
Beauty Amidst the Bytes_ Unearthing Unexpected Advantages of the Digital Wast...
Beauty Amidst the Bytes_ Unearthing Unexpected Advantages of the Digital Wast...Beauty Amidst the Bytes_ Unearthing Unexpected Advantages of the Digital Wast...
Beauty Amidst the Bytes_ Unearthing Unexpected Advantages of the Digital Wast...DhatriParmar
 
4.16.24 Poverty and Precarity--Desmond.pptx
4.16.24 Poverty and Precarity--Desmond.pptx4.16.24 Poverty and Precarity--Desmond.pptx
4.16.24 Poverty and Precarity--Desmond.pptxmary850239
 
Using Grammatical Signals Suitable to Patterns of Idea Development
Using Grammatical Signals Suitable to Patterns of Idea DevelopmentUsing Grammatical Signals Suitable to Patterns of Idea Development
Using Grammatical Signals Suitable to Patterns of Idea Developmentchesterberbo7
 
4.9.24 School Desegregation in Boston.pptx
4.9.24 School Desegregation in Boston.pptx4.9.24 School Desegregation in Boston.pptx
4.9.24 School Desegregation in Boston.pptxmary850239
 
Decoding the Tweet _ Practical Criticism in the Age of Hashtag.pptx
Decoding the Tweet _ Practical Criticism in the Age of Hashtag.pptxDecoding the Tweet _ Practical Criticism in the Age of Hashtag.pptx
Decoding the Tweet _ Practical Criticism in the Age of Hashtag.pptxDhatriParmar
 
Grade Three -ELLNA-REVIEWER-ENGLISH.pptx
Grade Three -ELLNA-REVIEWER-ENGLISH.pptxGrade Three -ELLNA-REVIEWER-ENGLISH.pptx
Grade Three -ELLNA-REVIEWER-ENGLISH.pptxkarenfajardo43
 
CHEST Proprioceptive neuromuscular facilitation.pptx
CHEST Proprioceptive neuromuscular facilitation.pptxCHEST Proprioceptive neuromuscular facilitation.pptx
CHEST Proprioceptive neuromuscular facilitation.pptxAneriPatwari
 
4.11.24 Mass Incarceration and the New Jim Crow.pptx
4.11.24 Mass Incarceration and the New Jim Crow.pptx4.11.24 Mass Incarceration and the New Jim Crow.pptx
4.11.24 Mass Incarceration and the New Jim Crow.pptxmary850239
 
4.11.24 Poverty and Inequality in America.pptx
4.11.24 Poverty and Inequality in America.pptx4.11.24 Poverty and Inequality in America.pptx
4.11.24 Poverty and Inequality in America.pptxmary850239
 

Recently uploaded (20)

How to Make a Duplicate of Your Odoo 17 Database
How to Make a Duplicate of Your Odoo 17 DatabaseHow to Make a Duplicate of Your Odoo 17 Database
How to Make a Duplicate of Your Odoo 17 Database
 
Faculty Profile prashantha K EEE dept Sri Sairam college of Engineering
Faculty Profile prashantha K EEE dept Sri Sairam college of EngineeringFaculty Profile prashantha K EEE dept Sri Sairam college of Engineering
Faculty Profile prashantha K EEE dept Sri Sairam college of Engineering
 
Reading and Writing Skills 11 quarter 4 melc 1
Reading and Writing Skills 11 quarter 4 melc 1Reading and Writing Skills 11 quarter 4 melc 1
Reading and Writing Skills 11 quarter 4 melc 1
 
prashanth updated resume 2024 for Teaching Profession
prashanth updated resume 2024 for Teaching Professionprashanth updated resume 2024 for Teaching Profession
prashanth updated resume 2024 for Teaching Profession
 
Unraveling Hypertext_ Analyzing Postmodern Elements in Literature.pptx
Unraveling Hypertext_ Analyzing  Postmodern Elements in  Literature.pptxUnraveling Hypertext_ Analyzing  Postmodern Elements in  Literature.pptx
Unraveling Hypertext_ Analyzing Postmodern Elements in Literature.pptx
 
31 ĐỀ THI THỬ VÀO LỚP 10 - TIẾNG ANH - FORM MỚI 2025 - 40 CÂU HỎI - BÙI VĂN V...
31 ĐỀ THI THỬ VÀO LỚP 10 - TIẾNG ANH - FORM MỚI 2025 - 40 CÂU HỎI - BÙI VĂN V...31 ĐỀ THI THỬ VÀO LỚP 10 - TIẾNG ANH - FORM MỚI 2025 - 40 CÂU HỎI - BÙI VĂN V...
31 ĐỀ THI THỬ VÀO LỚP 10 - TIẾNG ANH - FORM MỚI 2025 - 40 CÂU HỎI - BÙI VĂN V...
 
Narcotic and Non Narcotic Analgesic..pdf
Narcotic and Non Narcotic Analgesic..pdfNarcotic and Non Narcotic Analgesic..pdf
Narcotic and Non Narcotic Analgesic..pdf
 
ARTERIAL BLOOD GAS ANALYSIS........pptx
ARTERIAL BLOOD  GAS ANALYSIS........pptxARTERIAL BLOOD  GAS ANALYSIS........pptx
ARTERIAL BLOOD GAS ANALYSIS........pptx
 
ICS2208 Lecture6 Notes for SL spaces.pdf
ICS2208 Lecture6 Notes for SL spaces.pdfICS2208 Lecture6 Notes for SL spaces.pdf
ICS2208 Lecture6 Notes for SL spaces.pdf
 
Concurrency Control in Database Management system
Concurrency Control in Database Management systemConcurrency Control in Database Management system
Concurrency Control in Database Management system
 
Beauty Amidst the Bytes_ Unearthing Unexpected Advantages of the Digital Wast...
Beauty Amidst the Bytes_ Unearthing Unexpected Advantages of the Digital Wast...Beauty Amidst the Bytes_ Unearthing Unexpected Advantages of the Digital Wast...
Beauty Amidst the Bytes_ Unearthing Unexpected Advantages of the Digital Wast...
 
INCLUSIVE EDUCATION PRACTICES FOR TEACHERS AND TRAINERS.pptx
INCLUSIVE EDUCATION PRACTICES FOR TEACHERS AND TRAINERS.pptxINCLUSIVE EDUCATION PRACTICES FOR TEACHERS AND TRAINERS.pptx
INCLUSIVE EDUCATION PRACTICES FOR TEACHERS AND TRAINERS.pptx
 
4.16.24 Poverty and Precarity--Desmond.pptx
4.16.24 Poverty and Precarity--Desmond.pptx4.16.24 Poverty and Precarity--Desmond.pptx
4.16.24 Poverty and Precarity--Desmond.pptx
 
Using Grammatical Signals Suitable to Patterns of Idea Development
Using Grammatical Signals Suitable to Patterns of Idea DevelopmentUsing Grammatical Signals Suitable to Patterns of Idea Development
Using Grammatical Signals Suitable to Patterns of Idea Development
 
4.9.24 School Desegregation in Boston.pptx
4.9.24 School Desegregation in Boston.pptx4.9.24 School Desegregation in Boston.pptx
4.9.24 School Desegregation in Boston.pptx
 
Decoding the Tweet _ Practical Criticism in the Age of Hashtag.pptx
Decoding the Tweet _ Practical Criticism in the Age of Hashtag.pptxDecoding the Tweet _ Practical Criticism in the Age of Hashtag.pptx
Decoding the Tweet _ Practical Criticism in the Age of Hashtag.pptx
 
Grade Three -ELLNA-REVIEWER-ENGLISH.pptx
Grade Three -ELLNA-REVIEWER-ENGLISH.pptxGrade Three -ELLNA-REVIEWER-ENGLISH.pptx
Grade Three -ELLNA-REVIEWER-ENGLISH.pptx
 
CHEST Proprioceptive neuromuscular facilitation.pptx
CHEST Proprioceptive neuromuscular facilitation.pptxCHEST Proprioceptive neuromuscular facilitation.pptx
CHEST Proprioceptive neuromuscular facilitation.pptx
 
4.11.24 Mass Incarceration and the New Jim Crow.pptx
4.11.24 Mass Incarceration and the New Jim Crow.pptx4.11.24 Mass Incarceration and the New Jim Crow.pptx
4.11.24 Mass Incarceration and the New Jim Crow.pptx
 
4.11.24 Poverty and Inequality in America.pptx
4.11.24 Poverty and Inequality in America.pptx4.11.24 Poverty and Inequality in America.pptx
4.11.24 Poverty and Inequality in America.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.