Binary graph matching (Hungarian algorithm)

Source: Internet
Author: User

"The algorithms on the books are often very complex, and I plan to use a humorous example to describe the process of the algorithm."

The Hungarian algorithm was proposed by the Hungarian mathematician Edmonds in 1965, hence the name. The Hungarian algorithm is based on the idea of sufficiency proof in Hall theorem, it is the most common algorithm of the part graph matching, and the core of the algorithm is to find the augmented path, which is an algorithm for finding the maximal matching of binary graph with the augmented path.


A. first, the basic concept:

two-part diagram: In simple terms, if the midpoint of the diagram can be divided into two groups, and all edges cross the boundary of the group, then this is a two-part graph. To be exact: dividing the vertices of a graph into two disjoint sets u UAnd v V, so that each edge is connected separately u U、 v VVertices in the. If such a division exists, the graph is a two-part graph. An equivalent definition of a binary graph is: A graph that does not contain "rings with odd numbers of edges". Figure 1 is a two-part diagram. In order to be clear, we will later draw it into the form of Figure 2.

match : In graph theory, a "match" (matching) is a set of edges in which any two edges have no public vertices. For example, the red edge in Figure 3, Figure 4, is the match of Figure 2.

We define matching points , matching edges , unmatched points , mismatched edges , and they are very obvious. Example 3, 1, 4, 5, 7 is the matching point, the other vertices are unmatched points, 1-5, 4-7 is the matching edge, the other edges are non-matching edges.

Maximum match : A match with the largest number of matched edges in all matches of a graph, called the maximum match for this graph. Figure 4 is a maximum match that contains 4 matching edges.

Perfect Match : if one of the graphs has a match, all vertices are matching points, then it is a perfect match. Figure 4 is a perfect match. Obviously, the perfect match must be the maximum match (any point of the perfect match has already been matched, adding a new matching edge will certainly conflict with the existing matching edge). But not every diagram has a perfect match.

For example: as shown, if there is a connecting edge between a pair of boys and girls, it means they like each other. Is it possible for all boys and girls to be paired 22 so that each pair likes each other? In graph theory, this is the perfect match problem. If it's a different story: how many boys/girls do you like to pair with each other? This is the maximum matching problem.

Two. Hungarian algorithm:

The idea of the algorithm is to constantly find the augmented path, and increase the number of matches, the augmented path as the name implies is a path that can make a number of matches, in the matching problem, the expression of the augmented path is a "staggered path", that is, the path of the edge of the graph, its first edge is currently not involved in matching, The second side participates in the match, and the third side does not have one. The last edge does not participate in the match, and the start and end points have not been selected. This staggered, and evidently he had an odd number of edges. So for such a path, we can change the first edge to match, the second edge to not match ... And so on That is, the "inverse color" of all the edges, it is easy to find such modifications, the match is still legal, but the number of matches increased by a pair. In addition, an individual edge that joins two unmatched points is clearly an interleaved path. Can prove. When the augmented path can no longer be found, a maximum match is obtained, which is the idea of the Hungarian algorithm.

After reading the above narrative, beginners will definitely feel the scalp numbness, don't worry, let us look at a humorous example:

Through the efforts of several generations, you finally caught up with the tide of the remaining male, assuming you are a glorious new century matchmaker, in your hand there are n left male, m a woman, everyone may have a good impression on multiple heterosexual (-_-| | Temporarily regardless of special sexual orientation), if a pair of men and women, then you can put together the pair, now let us ignore all the unrequited love (a good feeling of sadness), you have is probably the following a diagram, each line is a mutual goodwill.


In the spirit of saving a life, the principle of building a seven-storey pagoda, you want to match as many couples as possible, the Hungarian algorithm working mode will teach you to do this:

===============================================================================

First : try to find the girl number 1th boys, found the first and he connected to the number 1th girls are still flowers, got it, connected to a blue line


===============================================================================

two : then to the 2nd boys to find a sister, found the first and he connected to the 2nd girl is no master, got it


===============================================================================

three : Next is the number 3rd boys, it is very regrettable that the number 1th girls already have the Lord, how to do?

We tried to assign another sister to the boy who was a match for girl number 1th (aka Number 1th).

(yellow means the side is temporarily torn down)

The second girl with number 1th is the number 2nd girl, but the number 2nd girl also has the Lord , how to do? We're going to try to get a girl for number 2nd again. ( Note This step is the same as above, this is a recursive process )


At this time found 2nd boys can still find the number 3rd girl, then the problem solved, go back

Number 2nd Boys can find the number 3rd sister ~ ~ ~                Boy number 1th can find the number 2nd Sister ~ ~ ~ Boy number 3rd can find the number 1th .

So the final result of the third step is:


===============================================================================

four : Next is the number 4th boys, unfortunately, according to the rhythm of the third step we can not give the number 4th boys to free a sister, we really do not have the means.

===============================================================================

This is the Hungarian algorithm process, in which the search for a sister is a recursive process, the most critical word is "Teng"Word

The principle is that there is a chance to create an opportunity at an organic meeting.


Three. Post code:

1. Adjacency Matrix

bool Dfs (int x) {      int i,j;      for (j=1;j<=m;j++) {    //scan each sister          if (Map[x][j] &&! BOOK[J])                //If there is ambiguous and has not been marked (the mark here means that this search has tried to change the problem of the sister's attribution, but did not succeed, so there is no need to blind time)          {              book[j]=1;              if (match[j]==0 | | dfs (MATCH[J])) {                   //flowers are free or can be vacated, here the girl with recursive                  match[j]=x;//numbered J is the boy with the number x.                                  return True;}}      }      return false;  }  for (i=1;i<=n;i++)  //There is a total of N boy, starting from the first to solve the emotional problem {      memset (book,0,sizeof (book));    This clears the      if (Dfs (i)) cnt++ in each step;  }  

2. adjacency table:

bool Dfs (int x) {for (int y=0;y<map[x].size (); y++)//push_back is started from the minus sign; {if (Map[x][y]&&!book[map[x][y]]) { Book[map[x][y]]=1;if (!match[map[x][y]]| | DFS (Match[map[x][y]))//flowers-based, or try to grab a grab {match[map[x][y]]=x; return true;}} return false;} <span style= "White-space:pre" ></span>memset (map,0,sizeof (map)); Memset (match,0,sizeof (match)); ANS=0;SCANF ("%d%d", &n,&m); int x, y; while (m--) {scanf ("%d%d", &x,&y), Map[x].push_back (y);} for (int i=1;i<=n;i++)//From the first boy to solve the emotional problem {memset (book , 0,sizeof (book)); if (Dfs (i)) ans++;} printf ("%d\n", ans);


Binary graph matching (Hungarian algorithm)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.