Kuhn-munkres algorithm

Source: Internet
Author: User
Tags natural logarithm

KM algorithm--two-point graph maximum weight matching

We've learned the Hungarian algorithm that matches the two-point chart before. But this algorithm is for a graph that does not have a weighted value.

Certainly someone would like to ask, there is no right value of the Hungarian algorithm, which the right to the value of the graph requires maximum or minimum weight matching it??

This brings us to today's protagonist--km algorithm.

How is this algorithm? In fact, the maximum and minimum right matching are the same problem. As long as the maximum match is asked, if the minimum weight is required to match, then the weights are reversed, then the result is reversed, then the minimum right matching is found out.

But what is the problem in asking for the most right match??

Before we look at this question, let's figure out a few concepts.

One

weighted matching of binary graphs: The weighted matching of binary graphs is to find a matching set, which makes the sum of the weights of the edges of the set is the largest or smallest. And the best match of the binary graph is a complete match , on the basis of which the sum of the Benquan values of the matching is required to be the maximum or minimum. The weighted matching of the binary graphs is not equal to the best match and does not contain each other .

The relationship between the two is poised. My understanding is that the weighted match is not considered complete, only the maximum or minimum right matching. And the best match must find the maximum or minimum right match on the basis of complete match.

km the operational requirements of the algorithm must exist a complete match , if a maximum weight matching (not necessarily complete) how to do? It is still simple to assign a non-existent Benquan value to 0.

KM algorithm the maximum weight matching is the edge weight and maximum, if I want to Benquan the largest, and how to convert? It is not difficult to do, each side of the right to take the natural logarithm, and then seek the maximum and right matching, the result of a and then calculate the E^a is the maximum product matching. There is no better solution to the problem of precision.

Both

The best matching of binary graphs: There is a right (non-negative) for each edge of the binary graph, and a complete matching scheme is required, which makes the right and maximum of all matching edges and the best complete match. (Special, when the right of all sides is 1 o'clock, is the maximum complete matching problem)

Three.

complete Match : defines the g=<v1,v2,e> as a two-part chart, | v1|≤| V2|,m is a maximum match in G, and | m|=| V1|, it is said that M is a complete match of V1 to V2.

In the above definition, if | v2|=| v1|, the complete match is the perfect match, if | v1|<| V2|, the complete match is the maximum match in G.

KM algorithm

KM algorithm by giving each vertex a label (called the Top Mark ) to the problem of finding the maximum right matching to the problem of complete matching. The top mark of the Vertex XI is a[i], the top mark of the vertex Yi is b[i], and the Benquan between the Vertex XI and the YJ is w[i,j]. At any moment during the execution of the algorithm, for any edge (I,J), A[i]+b[j]>=w[i,j] is always established, the initial a[i] is the maximum edge of the edge connected to Xi, b[j]=0. Km the correctness of the algorithm is based on the following theorem:

set G (V,e) to two, G ' (V,e ') is a sub-diagram of the two-part graph. If for any side of G ' <x,y> satisfied, L (x) + L (y) = = Wx,y, we call G ' (V,e ') an equivalent sub-graph of G (v,e) or an equal sub-graph (which is the generation sub-graph of G).

If the sub-graph (i,j) consisting of all the edges (a[i]+b[j]=w[i,j) in the binary graph (called equal sub -graph) has a complete match, then this complete match is the maximum weight matching of the binary graph.

Because for any one of the binary graphs, if it is contained in an equal sub-graph, then its Benquan and equals the top of all vertices, and if its edges are not included in the equal sub-graph, then its Benquan and is less than the top of all vertices and (that is, not the optimal match). So the complete match of the equal sub-graph must be the maximal right match of the binary graph.

Algorithmic flow

(1) Initialize the value of the feasible top

(2) Searching for complete match with Hungarian algorithm

(3) If no complete match is found, modify the value of the feasible top

(4) Repeat (2) (3) until a complete match is found for the equal sub-graph

Small summary:

KM algorithm is used to solve the maximum weight matching problem, in a binary graph, the left vertex is x, the right vertex is Y, now for each group of connected connected Xiyj right Wij, to find a match so that all wij and maximum.

In other words, the maximum match must be a perfect match. (Perfect match: two points on both sides of the graph are the same)

If the points are not the same, we can have the same number of points when the virtual point weights are 0, which makes the graph a perfect match.

Code implementation:

#include <iostream>#include<cstring>#include<cstdio>using namespacestd;Const intMAXN =305;Const intINF =0x3f3f3f3f;intLOVE[MAXN][MAXN];//record the goodwill of every sister and every boyintEX_GIRL[MAXN];//the expectations of every sisterintEX_BOY[MAXN];//the expectations of every boyBOOLVIS_GIRL[MAXN];//record every match of the match GirlBOOLVIS_BOY[MAXN];//keep track of each match, match the boys.intMATCH[MAXN];//record each boy's match to the sister, if not, 1.intSLACK[MAXN];//keep track of the number of expectations that every man needs at least if he can be attracted to a sisterintN;BOOLDfsintgirl) {Vis_girl[girl]=true;  for(intBoy =0; Boy < N; ++Boy ) {        if(Vis_boy[boy])Continue;//each round matches each boy only try once        intGap = Ex_girl[girl] + ex_boy[boy]-Love[girl][boy]; if(Gap = =0) {//If the requirements are metVis_boy[boy] =true; if(Match[boy] = =-1|| DFS (Match[boy])) {//find a guy who doesn't have a match, or the boy's sister can find someone else .Match[boy] =girl; return true; }        } Else{Slack[boy]= Min (Slack[boy], GAP);//slack can be understood as the boy who wants to get a girl's heart. How much will it take to have a minimum spare tire appearance "cover your face        }    }    return false;}intKM () {memset (match,-1,sizeofMatch);//Initially, every guy doesn't have a matching girl.memset (Ex_boy,0,sizeofEx_boy);//the initial expectation for each boy is 0.//The initial expectation of each girl is the greatest affection of the boy who is connected to her .     for(inti =0; i < N; ++i) {Ex_girl[i]= love[i][0];  for(intj =1; J < N; ++j) Ex_girl[i]=Max (Ex_girl[i], love[i][j]); }    //try to solve a problem for every girl     for(inti =0; i < N; ++i) {Fill (slack, slack+ N, INF);//because to take the minimum value to initialize to infinity         while(1)         {            //The way to solve the problem for each girl is to reduce the expectation if it is not found.//keep track of whether the boys and girls are trying to match each round .memset (Vis_girl,false,sizeofvis_girl); memset (Vis_boy,false,sizeofVis_boy); if(Dfs (i)) Break;//find a destination to exit//If you can't find it, lower your expectations .//minimum expected value to reduce            intD =INF;  for(intj =0; J < N; ++j)if(!vis_boy[j]) d =min (d, slack[j]);  for(intj =0; J < N; ++j) {//all girls who have visited lower their expectations                if(Vis_girl[j]) ex_girl[j]-=D; //all the boys visited increased their expectations.                if(Vis_boy[j]) ex_boy[j] + =D; //the boy who has not visited because the expectations of the girl lower, the distance to get a girl fell in a step!                 ElseSLACK[J]-=D; }        }    }    //match completion to find out all pairs of goodwill and    intres =0;  for(inti =0; i < N; ++i) Res+=love[Match[i]][i]; returnRes;}intMain () { while(~SCANF ("%d", &N)) { for(inti =0; i < N; ++i) for(intj =0; J < N; ++j) scanf ("%d", &Love[i][j]); printf ("%d\n", KM ()); }    return 0;}

There's a blog post that's pretty good.

Http://www.cnblogs.com/wenruo/p/5264235.html

Kuhn-munkres 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.