KM algorithm (binary graph maximum weight Matching) learning

Source: Internet
Author: User

It's a la la!

KM algorithm by giving each vertex a label (called the top mark) to the problem of maximum weight 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 point in the algorithm execution, a[i]+b[j]>=w[i,j] is always true for either edge (i,j).

Km the correctness of the algorithm is based on the following theorem:

*

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.

*

This theorem is Obvious. Because for any one of the binary graphs, if it is contained in an equal sub-graph, then its Benquan and equals the top label of all vertices, and if it has an edge that is not included in the equal sub-graph, then its Benquan and is smaller than the top of all vertices and. So the complete match of the equal sub-graph must be the maximal right match of the binary Graph. Initially in order for a[i]+b[j]>=w[i,j] to be established, the a[i] is the maximum weight of all the edges associated with vertex xi, b[j]=0. If the current equality Sub-diagram does not have a complete match, modify the top label as follows to enlarge the equal sub-graph until the equal sub-graph has a complete match.

We're asking for a complete match for the current equality sub-graph to fail because we can't find a staggered path from it for an X Vertex. At this point we get a staggered tree whose leaf nodes are all x vertices. Now we're going to reduce the top of the x vertex in the staggered tree by a certain value. the top of the d,y vertex all increases the same value d, then we find:

1. The value of a[i]+b[j] does not change at both ends of the edge (i,j) in the interlaced Tree. That is, it originally belonged to an equal sub-graph, which is still part of the equal CHILD.

2. Neither side (i,j), a[i] and b[j] in the interlaced tree have Changed. That is, it originally belonged to (or does not belong to) an equal sub-graph, and now still belongs to (or does not belong to) an equal sub-graph.

The 3.X end is not in the interlaced tree, and the y-end is in the edge of the interleaved tree (i,j), and its a[i]+b[j] value Increases. It originally does not belong to the equality sub-graph, and is still not part of the equality Sub-graph.

4.X End in the interleaved tree, the Y end is not on the edge of the interlaced tree (i,j), and its a[i]+b[j] value Decreases. It is also said that it originally did not belong to the equal sub-graph, it may now enter the equal sub-graph, so that the equal sub-graph has been expanded.

Now the problem is to ask for D Value.

For a[i]+b[j]>=w[i,j] to always be true, and at least one edge into the equal sub-graph, D should be equal to min{a[i]+b[j]-w[i,j]| Xi in the staggered tree, Yi is not in the interlaced tree}.

the above is the basic idea of the KM ALGORITHM. But the simple implementation method, the time complexity of O (n4)-need to find O (n) secondary augmentation path, Each augmentation needs to modify the O (n) sub-index, each time the top is modified to enumerate the edges to find D value, the complexity of O (n2). In fact, the complexity of the KM algorithm can be achieved o (n3). We give each y vertex a "slack" function slack, which is initialized to infinity each time we start looking for an augmented path. While looking for an augmented path, when checking for edges (i,j), if it is not in the equal sub-graph, let slack[j] be the smaller value of the original value and a[i]+b[j]-w[i,j]. This way, when you modify the top label, take all the minimum values in the slack value of the y vertices that are not in the interleaved tree as the D value. however, It is important to note that all slack values are subtracted from D when the top label is Modified.

So the KM algorithm process:

1, the initial feasibility of the top standard lx[], ly[].

2, the idea of similar hungry algorithm to find an exact match.

3, can not find the adjustment lx[], ly[] value, and then go back to 1.

4, If the binary map is already exactly matching the exit, ans =∑ (lx[i] + ly[i]);

If (not read) give another explanation http://www.cnblogs.com/wenruo/p/5264235.html

Let's look at an example

hdu2255 ben off to make a lot of money

Time limit:1000/1000 MS (java/others) Memory limit:32768/32768 K (java/others)
Total submission (s): 8309 Accepted submission (s): 3692

Problem description legend in a faraway place there is a very wealthy village, one day the village chief decided to reform the System: redistribute the House.
This is a big event, related to People's housing problems ah. There are n rooms in the village, just there are n common people, considering every house to have a room to live (if There are people do not live, easy to cause instability factors), each family must be assigned to a house and can only get a house.
On the other hand, village chiefs and other village leaders hope to get the most benefit, so that the Village's institutions will be rich. because people are richer, they can have a certain price for each house in their economy, for example, there are 3 houses, and a common man can give 100,000 to the first, 20,000 to the 2nd. 200,000 for the 3rd Room. (in their economy, of course). the question now is how village leaders can allocate their homes to make the most of their income. (villagers who have the money to buy a house but not necessarily can buy it depends on what the village leader assigns).

Input data contains multiple sets of test cases, the first row of each set of data input n, indicating the number of houses (also the number of people home), followed by n rows, the number of n per row represents the price of the room of the second village name (n<=300).

Output make the maximum revenue value for each set of data, one row for each set of Outputs.

Sample Input2100 1015 Output123
//km algorithm template O (n^3)#include <iostream>#include<cstdio>#include<cstring>#defineMAXN 301#defineINF 9999185using namespacestd;intw[maxn][maxn],lx[maxn],ly[maxn],visx[maxn],visy[maxn];intlinky[maxn],slack[maxn],nx,ny,n;BOOLFindintx) {visx[x]=true;  for(inty=1; y<=n;y++)    {        intt=lx[x]+ly[y]-w[x][y]; if(!visy[y]) {            if(t==0) {visy[y]=true; if(linky[y]==-1||Find (linky[y])) {linky[y]=x; return true;//Find the Augmented road                }            }            Else if(slack[y]>t)//no augmented orbit found (indicates that vertex x does not have a corresponding match and is not matched to a complete match (the complete match of the equal Sub-graph )slack[y]=t; }    }    return false;}intKM ()//returns the value of the best match{memset (linky,-1,sizeoflinky); Memset (ly,0,sizeofly); Memset (lx,-127/3,sizeoflx);  for(intI=1; i<=n; i++)         for(intj=1; j<=n;j++)            if(w[i][j]>lx[i]) lx[i]=w[i][j];  for(intx=1; x<=n; X + +)    {         for(intI=1; i<=n; i++) slack[i]=INF;  while(1) {memset (visx,0,sizeofvisx); Memset (visy,0,sizeofvisy); if(find (x)) break;//find the augmented Road end            intD=INF;  for(intI=1; i<=n; I++)//not found, make adjustments to L (this will increase the edges of the equal sub-graph), Re-find            {                if(!visy[i]&&d>slack[i]) D=slack[i]; }             for(intI=1; i<=n; i++)                if(visx[i]) lx[i]-=d;  for(intI=1; i<=n; i++)                if(visy[i]) ly[i]+=d; Elseslack[i]-=d; }    }    intans=0;  for(intI=1; i<=n; i++)        if(linky[i]>-1) ans+=w[linky[i]][i]; returnans;}intmain () {intb;  while(SCANF ("%d", &n)! =EOF) {         for(intI=1; i<=n;i++)        {             for(intj=1; j<=n;j++) scanf ("%d", &a), w[i][j]=a; } printf ("%d\n", KM ()); }    return 0;}

KM algorithm (binary graph maximum weight Matching) learning

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.