The Hungarian algorithm solves the binary graph matching

Source: Internet
Author: User

The Hungarian algorithm solves the binary graph matching

"The algorithms in the book are often very complex, and my friends and I plan to use some simple examples 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.

-------, wait, look at the big head? Then see the following version:

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 3rd sister, ~ ~ 1th Boys can find 2nd sister, ~ ~ ~ 3rd Boys can find the number 1th sister

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. Fragrant custard classmate walk well.

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

This is the Hungarian algorithm of the process, in which looking for a sister is a recursive process, the most critical word is "teng" word

The principle is: there is a chance, no chance to create a chance to go on

1 BOOLFindintx) {2     inti,j;3      for(j=1; j<=m;j++) {//Scan every sister4         if(line[x][j]==true&& used[j]==false)      5         //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 go blind)6         {7used[j]=1;8             if(girl[j]==0||find (Girl[j])) { 9                 //no master or can make a place, use recursion hereTengirl[j]=x; One                 return true; A             } -         } -     } the     return false; -}

In the main program we do this: each step corresponds to one of the 1234 steps we described above

1  for (i=1; i<=n;i++) 2 {3     memset (used,0,sizeof(used));    // This is emptied at every step. 4     if find (i) all+=1; 5 }

Reference: http://blog.csdn.net/dark_scope/article/details/8880547

The procedure described above and the specific process of code in memory:

The Hungarian algorithm solves the binary graph matching

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.