Use the KM algorithm to find the maximum matching value in the case of full matching

Source: Internet
Author: User

Algorithm Description: 1. There must be a full match

2. Find the maximum (small) weight match, that is, the minimum consumption or maximum benefit required for various matching conditions.

Before the algorithm, describe what is a staggered tree: the vertices traversed by the Hungarian algorithm are all nodes in the staggered tree each time an augmented path is added or an increase fails. If we say a bipartite graph (one side is X vertex, if X vertices match y vertices on the other side, all leaf nodes in the staggered tree are X vertices. (Thank you for your understanding)

Algorithm Description: This algorithm converts the question of finding the maximum weight matching to the question of finding a complete match by giving each vertex a label (called the top mark. Set vertex XI to a [I], vertex YJ to B [J], and edge weight between vertex XI and YJ to W [I, j]. A [I] + B [J]> = W [I, j] is always valid for any edge (I, j) during Algorithm Execution.

The correctness of the KM algorithm is based on the following theorem:

If all the subgraphs (called equal subgraphs) that meet the conditions of a [I] + B [J] = W [I, j] edge (I, j) if there is a complete match, this complete match is the maximum weight match of the Bipartite Graph.

First, we will explain what a complete match is. The so-called complete match means that in a two-part graph, all vertices in the X-point set have matched or

If all vertices in the Y-point set match, the match is called complete match.

This theorem is clear. For any matching of a bipartite graph, if it contains an equal subgraph, its edge weight is equal to the top mark and sum of all vertices. If its edge does not contain an equal subgraph, the edge weight is smaller than the top sum of all vertices. Therefore, the full match of an equal subgraph must be the maximum weight match of a bipartite graph.

In the initial stage, to make a [I] + B [J]> = W [I, j] Always stand, make a [I] the maximum weight of all edges associated with vertex XI. B [J] = 0. If the current equality subgraph does not have a complete match, modify the top mark as follows to expand the equality subgraph until the equality subgraph has a complete match.

We fail to find the complete matching of the current equi-th subgraph because we cannot find an staggered path starting from an X vertex. In this case, we get an interleaved tree with all its leaf nodes being X vertices. Now, we can reduce all the top labels of X vertices in the staggered tree to a certain value D, and all the top labels of Y vertices Add the same value D. Then we will find that:

1) The value of the edge (I, j) and a [I] + B [J] In the staggered tree does not change. That is to say, it originally belongs to an equal subgraph and still belongs to an equal subgraph.

2) the two ends are not in the edge (I, j) of the staggered tree, And neither a [I] nor B [J] is changed. That is to say, it originally belongs to (or does not belong to) equal subgraph, And now it still belongs to (or does not belong to) equal subgraph.

3) The X end is not in the staggered tree, and the Y end is in the edge (I, j) of the staggered tree. The value of a [I] + B [J] increases. It does not belong to an equal subgraph and does not belong to an equal subgraph.

4) The X end is in the staggered tree, and the Y end is not in the edge (I, j) of the staggered tree. The value of a [I] + B [J] is reduced. That is to say, it originally does not belong to an equal subgraph, And now it may have entered an equal subgraph, thus expanding the equal subgraph.

The problem now is to evaluate the value of D. To ensure that a [I] + B [J]> = W [I, j] is always valid, and at least one edge enters an equal subgraph, D should be equal:

Min {A [I] + B [J]-W [I, j] | Xi is in the staggered tree, Yi is not in the staggered tree }.

 

Recommendation

Poj 2195: http://poj.org/problem? Id = 2195

My code:

# Include <stdio. h>
# Include <string. h>
Int row, Col; // provides the rows and columns of the graph.
Char STR [201] [201]; // save Graph
Int dis [201] [201]; // distance between each person and house
Int men [201] [2], House [201] [2], hnum, mnum; // coordinates of each house
Int line [201], usedy [201], usedx [201], point [201] [2]; // match the X vertex in the usedx staggered tree obtained by line, usedy stores y vertices in the staggered tree. Point is two-dimensional. The first dimension stores X (I .e., person) vertices and the second dimension stores one (I .e., house) vertices.
Int find (int x)
{
Usedx [x] = 1;
For (INT I = 1; I <= hnum; I ++)
If (! Usedy [I] & point [x] [0] + point [I] [1]-Dis [x] [I] = 0) // Tag: optimized
{
Usedy [I] = 1;
If (! Line [I] | find (line [I])
{
Line [I] = X;
Return 1;
}
}
Return 0;
}
Int count ()
{
Int res = 0;
For (INT I = 1; I <= mnum; I ++)
Res + = point [I] [0] + point [I] [1];
Return res;
}
Int km () // The maximum number of matching conditions obtained by the KM Algorithm
{
Int I, j, Max;
For (I = 1; I <= mnum; I ++)
{
Max = 0x8fffffff;
For (j = 1; j <= hnum; j ++)
{
If (DIS [I] [J]> MAX)
{
Max = dis [I] [J];
}
}
Point [I] [0] = max;
Point [I] [1] = 0;
}
Memset (line, 0, sizeof (line ));
For (I = 1; I <= mnum; I ++)
{
While (1)
{
Memset (usedx, 0, sizeof (usedx); // pay attention to the initialization of used. The two are the most useful: used and staggered tree storage in Hungary.
Memset (usedy, 0, sizeof (usedy ));
If (find (I ))
Break;
For (j = 1; j <= mnum; j ++)
{
If (usedx [J])
Point [J] [0] --; // because the data is small, 1 can be checked each time, and the minimum value of some marked above can be subtracted.
}
For (j = 1; j <= hnum; j ++)
{
If (usedy [J])
Point [J] [1] ++;
}
}
}
Return count ();
}
Int ABS (int x)
{
Return x> 0? X:-X;
}
Int main ()
{
Int I, J;
While (scanf ("% d", & Row, & col )! = EOF &&! (ROW = 0 & Col = 0 ))
{
Hnum = 0;
Mnum = 0;
For (I = 1; I <= row; I ++)
{
Scanf ("% s", STR [I] + 1 );
For (j = 1; j <= Col; j ++)
{
If (STR [I] [J] = 'H ')
{
House [++ hnum] [0] = I;
House [hnum] [1] = J;
}
If (STR [I] [J] = 'M ')
{
Men [++ mnum] [0] = I;
Men [mnum] [1] = J;
}
}
}
For (I = 1; I <= mnum; I ++)
For (j = 1; j <= hnum; j ++)
{
Dis [I] [J] =-(ABS (MEN [I] [0]-House [J] [0]) + ABS (MEN [I] [1]-House [J] [1]); // obtain a negative value and change the minimum value to the maximum value.
}
Printf ("% d \ n",-km ());
}
Return 0;
}

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.