Binary graph matching-Hungarian algorithm

Source: Internet
Author: User

The so-called dichotomy chart: refers to the same part without edges, then this graph is a binary graph.

Note: The following transfer from http://blog.csdn.net/dark_scope/article/details/8880547

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 left, 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 (surprised-_-| | Temporarily do not consider the special sexual orientation), if a pair of men and women each other, then you can put together the pair, now let us ignore all the unrequited love (good sad feeling fast cry), 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


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

Second: Then to the 2nd boys to find a sister, found the first and he connected to the number 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? Let's try to get a girl for number 2nd (angry) and find a sister again (note that 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 out of a sister, we are helpless ... 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

HDU 2063

Test instructions: These men and women are fond of each other, as far as possible to match them together, ask the maximum number of pairs can be paired?

Code:

/* * Author:ry * Created time:2017/10/31 8:53:47 * File name:1.cpp */#include <iostream> #include <cstdio&gt , #include <cstdlib> #include <cstring> #include <cmath> #include <algorithm> #include < string> #include <vector> #include <stack> #include <queue> #include <set> #include <map > #include <time.h>using namespace std;const int eps = 1e6+5;const double pi = ACOs ( -1.0); const int inf = 0X3F3F3F 3f; #define MAX (A, B) a>b?a:b#define Min (A, B) a>b?b:a#define ll long Longint K, M, N;int edge[505][505];int pt[505];in T Used[505];bool find (int x) {for (int i = 1; I <= n; i++) {if (Edge[x][i] &&!used[i]) {US            Ed[i] = 1;                if (pt[i] = = 0 | | find (pt[i)) {pt[i] = x;            return true; }}} return false;}        int main () {int A, B;        while (~SCANF ("%d", &k) && k) {scanf ("%d%d", &m, &n); memset (Edge, 0, sIzeof (Edge));        memset (PT, 0, sizeof (PT));                    while (k--) {scanf ("%d%d", &a, &b);        EDGE[A][B] = 1;        } int ans = 0;            for (int i = 1; I <= m; i++) {memset (used, 0, sizeof (used));            if (find (i)) {ans++;    }} printf ("%d\n", ans); } return 0;}

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.