Poj 2195 going home (Maximum Weight Matching and KM Algorithm for bipartite graphs)

Source: Internet
Author: User

Here is a picture of N people and N houses. Each person needs to return to a house, and the sum of distance is the smallest.
... H ....
... H ....
... H ....
Mmmhmmmm
... H ....
... H ....
... H ....

Question: In fact, the question is to find the least weighted matching. How can this problem be converted to the maximum weighted matching? Method 1 can take the opposite number for each value. Method 2: Subtract each value from the upper bound.

Basic Principles

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 }.

Improvement:

The above is the basic idea of the KM algorithm. However, in a simple implementation method, the time complexity is O (n ^ 4). You need to find the O (n) Increment path. You need to modify the O (n) top mark at most for each increment, each time you modify the top tag, the complexity is O (n ^ 2) because the enumeration edge is required to evaluate the D value ). In fact, the complexity of the KM algorithm can be O (N ^ 3. Each y vertex is given a "relaxation amount" function slack, Which is initialized to an infinite number every time the augmented path is searched. When you check the edge (I, j) in the process of searching for the augmented path, if it is not in the same subgraph, the slack [J] is converted into a smaller value of the original value and a [I] + B [J]-W [I, j. In this way, the minimum value of the slack value of all y vertices not in the staggered tree can be used as the D value when the top mark is modified. Note that after the top mark is modified, the slack value of all y vertices not in the staggered tree must be subtracted from D.

Kuhn-munkras algorithm flow:

(1) initialize the value of the feasible top mark;

(2) Search for complete matching using the Hungary algorithm;

(3) If a complete match is not found, modify the value of the feasible top mark;

(4) Repeat (2) (3) until a complete matching of an equal subgraph is found;

# Include <cstdio> # include <cmath> # include <cstring> # include <algorithm> using namespace STD; # define Max 105 # define INF 9999999 struct house {int R, c;} House [Max]; struct man {int R, C;} Man [Max]; int H, m, n, m; int A [Max], B [Max]; int visa [Max], visb [Max]; int match [Max], slack [Max], map [Max] [Max]; bool find_path (int I) {visa [I] = true; For (Int J = 0; j 

Related Article

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.