Optimal Matching KM Algorithm for weighted bipartite graphs

Source: Internet
Author: User

[Cpp]/********************************** * ********************* algorithm introduction: given a completely bipartite graph G = (X ∪ Y, X × Y), the edge (x, y) has the right to w (x, y ); find a matching M with the maximum weight from X to Y, that is, the optimal matching problem of the Bipartite Graph. The KM (Kuhn_Munkras) algorithm is used to obtain the maximum weight matching in a complete match; algorithm idea: the KM 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 Yi to B [I], 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 initial A [I] is the maximum edge weight of the edge connected to Xi, and B [j] = 0. the correctness of the KM algorithm is based on the following theorem: Set G (V, E) it is a bipartite graph, and G' (V, E ') is the subgraph of the Bipartite Graph; If any edge in G' is <x, y> satisfied, A (x) + B (y) = W [x, y]; then G' (V, E ') is an equivalent subgraph of G (V, E) or an equal subgraph (G-generated subgraph ); 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, because any matching of the Bipartite Graph, if it contains an equal subgraph; the edge weight is equal to the top Mark sum of all vertices. If some of its edges are not contained in an equal subgraph, then its edge weight is smaller than the top Mark sum of all vertices (that is, it is not the optimal match). Therefore, a complete match of an equal subgraph must be the maximum weight match of a bipartite graph; an equal subgraph contains all vertices of the source image. A perfect match can be found for an equal subgraph. A perfect match can be expanded by adding some virtual vertices (marked as M ); the perfect match includes the matching of all vertices, so the vertex label values of all vertices are included. Although some vertices are 0, in this state, the numbers of equal subgraphs are mapped to the source image one by one; Any matching of the source image can only contain all vertices of the source image. That is, the sum of all matching rights cannot exceed the sum of all labels. Therefore, M and M must be optimal. Algorithm Improvement: each Y vertex is assigned a "relaxation quantity" function slack. Each time the augmented path is searched for, it is initially infinite. During the search for augmented paths, when I, j is checked, if it is not in an equal subgraph, let slack [j] = min (original value, A [I] + B [j]-W [I, j]); in this way, the minimum value of all the slack values of Y vertex not in the staggered tree can be used as the D value when the top mark is modified. algorithm process: ① initialize the feasible top Mark value; ② use the Hungary algorithm to find a complete match; ③ if a complete match is not found, modify the value of the feasible top mark; ④ repeat ② until a complete match of an equal subgraph is found; **************************************** * *****************/# include <iostream> # include <cstring> # include <cstdlib> # Include <cstdio> # include <climits> # include <algorithm> using namespace std; const int N = 1000; const int INF = 0 xffffff; int w [N] [N]; // weight int lx [N], ly [N]; // top Mark int linky [N]; // record the vertex int visx [N], visy [N]; int slack [N]; // The relaxation quantity int nx, ny; // The number of vertices on both sides of the Bipartite Graph void init () {memset (linky,-1, sizeof (linky); // records the vertex memset (ly, 0, sizeof (ly); // initialize the top Mark y to 0 for (int I = 0; I <nx; I ++) for (int j = 0, lx [I] =-INF; j <Ny; j ++) {if (w [I] [j]> lx [I]) lx [I] = w [I] [j]; /// initialize top x as the maximum edge weight associated with vertex Xi} bool find (int x) // Hungarian algorithm {visx [x] = true; for (int y = 0; y <ny; y ++) {if (visy [y]) continue; int t = lx [x] + ly [y]-w [x] [y]; // If t = 0, the maximum weight is matched; if (t = 0) {visy [y] = true; if (linky [y] =-1 | find (linky [y]) {linky [y] = x; return true; // find the augmented rail} else if (slack [y]> t) slack [y] = t;} return false; // The augmented rail is not found (indicating that vertex x does not match Complete match (complete match of equal subgraphs) does not match)} int KM () // returns the optimal matched value {init (); for (int x = 0; x <nx; x ++) {for (int I = 0; I <ny; I ++) slack [I] = INF; // The relaxation function is initialized to infinity while (1) {memset (visx, 0, sizeof (visx); memset (visy, 0, sizeof (visy); if (find (x) // find the augmented orbit, exit break; int d = INF; for (int I = 0; I <ny; I ++) // not found, adjust l (this will increase the edge of the equal subgraph) and find {if (! Visy [I] & d> slack [I]) d = slack [I] ;}for (int I = 0; I <nx; I ++) // modify the top mark of x {if (visx [I]) lx [I]-= d ;}for (int I = 0; I <ny; I ++) // modify the top mark of y {if (visy [I]) ly [I] + = d; else slack [I]-= d; // After modifying the top mark, d ;}} int result = 0; for (int I = 0; I <ny; I ++) {if (linky [I]>-1) result + = w [linky [I] [I];} return result;} int main () {www.2cto.com // freopen ("C: \ Users \ Administrator \ Desktop \ Kd.txt "," r ", stdin); while (~ Scanf ("% d", & nx, & ny) {if (! Nx |! Ny) break; int a, B, c; while (scanf ("% d", & a, & B, & c), a + B + c) {w [a] [B] = c;} 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.