A series of interesting writing algorithms--Hungarian algorithm

Source: Internet
Author: User

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

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

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.

BOOLFindintx) {    inti,j;  for(j=1; j<=m;j++) {//Scan every sister        if(line[x][j]==true&& used[j]==false)              //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){Used[j]=1; if(girl[j]==0||find (Girl[j])) {                 //no master or can make a place, use recursion heregirl[j]=x; return true; }        }    }    return false;}

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

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

A series of interesting writing algorithms--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.