[Graph theory] binary graph matching (Hungarian algorithm)

Source: Internet
Author: User

Introduction Partial reprint Wikimedia Encyclopedia:

The Hungarian algorithm is one of the many algorithms used to solve the problem of linear task assignment, and it is a classical algorithm to solve the problem of the maximal matching of the binary graph, which can solve the problem in polynomial time, which was put forward by American mathematician Harold Kuhn in 1955. This algorithm is called the Hungarian algorithm because a large part of the algorithm is based on the former Hungarian mathematician Dénes K?nig and Jen? Created on top of Egerváry's work.

Problem Description:

Set g= (v,e) is an no-map. such as Vertex set V can be partitioned into two disjoint subsets of the v1,v2, and each edge of the graph two vertices attached to each of these two different subsets. It is said that figure G is a two-point figure. The dichotomy chart can also be recorded as g= (V1,v2,e).


Given a binary graph G, in a sub-figure m of G, any two edges in M's edge set {E} are not attached to the same vertex, then M is a match. Selecting a subset with the largest number of edges in such a subset is called the maximum matching problem for graphs (maximal matching problem)


If all vertices of the graph are associated with an edge in a match, the match is called an exact match, also known as a complete, perfect match.


Algorithm Description:


An obvious algorithm for finding the maximum match is to find out all the matches first, and then keep the most matching numbers. But the time complexity of the algorithm is the exponential function of the number of edges. Therefore, it is necessary to seek a more efficient algorithm. The following is a description of the method for finding the maximum match using the augmented path (called the Hungarian algorithm, proposed by mathematician Harold Kuhn in 1955).

Definition of the augmented road (also called augmented rail or staggered rail):

If P is a path in Figure G that is connected to two unmatched vertices, and an edge that belongs to M and an edge that does not belong to M (that is, a matched and a matched edge) appears alternately on p, then p is an augmented path relative to M. (M is a match)

The following three conclusions can be drawn from the definition of an augmented road:

The path length of the 1-p must be odd, and the last edge of the first Bien Hoa does not belong to M.

2-The M and P xor (go with the XOR) can get a larger match m '.

3-m is the maximum match for G when and only if there is no augmented path to M.

Algorithm outline:

(1) m is empty

(2) Find an augmented path p, by XOR or operation to obtain a larger match m ' instead of M

(3) Repeat (2) operation until the augmented path is not found

Complexity of time space:
Time Complexity adjacency matrix: The Worst O (n^3) adjacency table: O (MN) spatial complexity adjacency matrix: O (n^2) adjacency table: O (m+n)


More intuitive to understand the matching process can refer to the blog: http://blog.csdn.net/dark_scope/article/details/8880547

Personal memo: In the dichotomy chart, the number of vertices on the left is assumed to be UN, the number of vertices on the right is assumed to be vn,g[i][j] to indicate that there is an edge between the left fixed point I and the right vertex J, with Linked[i] to indicate which vertex on the left is matched to the first vertex of the right. First, go to the left point, for each left vertex to match the right vertex, if a feasible match is found, mark the right vertex has been matched, the left point of the matching process to this end, and then go through the next left vertex, if the left vertex with the edge of the right vertex has been matched, Assuming that the left vertex that matches it is x, then you need to re-match the x to see if you can give the right vertex to the left point that needs to be matched. A little bit around. For example, left vertex A and right c,d have edges, left vertex B and right vertex C have edge, first match is a,a and C match, a match end, then go to match B, find and B have edge connected to right vertex C has been matched, and matching vertex is left vertex A, then re-match a , see if a to match the other vertex, and the C vertex "free space", found that a can also be matched with D, you can vacate an empty to make B and C match. This is where the Hungarian algorithm is more critical.


Template: Hdu 2063

/* Hungarian algorithm solves the problem of the maximum matching of the binary graph using the form of adjacency matrix, G[I][J]MAXN represents the maximum number of vertices on one side, UN represents the left vertex number, the VN represents the right vertex number Linked[i] represents the right of the first point and which point to the left match * * #include <iostream> #include <stdio.h> #include <algorithm> #include <string.h>using namespace std; const int Maxn=502;int un,vn;//left vertex number, right vertex number int g[maxn][maxn];//adjacency matrix to store edge int linked[maxn];//point to the right and which point to the left matches bool VIS[MAXN] ; bool Dfs (int u) {for (int v=1;v<=vn;v++)//Traverse right vertex {if (G[u][v]&&!vis[v]) {Vis[v]            = 1; if (!linked[v]| |                DFS (LINKED[V])//The right vertex has not been matched, or a matching front left vertex can go to find another match and the right vertex "vacated" to the current left vertex u {linked[v]=u;            return true; }}} return false;}    int Hungary () {int ans=0;    memset (linked,0,sizeof (linked));        for (int u=1;u<=un;u++)//traverse the left vertex to find the matching right vertex {memset (vis,0,sizeof (VIS));    if (DFS (U))//Go to find u can match, if can match, ans++ ans++; } return ans; int k,m,n;//m is the number of left vertices required to be entered in the topic, n is the right vertex number required for input in the topic int main () {while (scanf ("%d", &k)!=eof& &k) {scanf ("%d%d", &m,&n);        Un=m;vn=n;        memset (G,0,sizeof (g));        int u,v;        for (int i=1;i<=k;i++) {scanf ("%d%d", &u,&v),//u and V have side g[u][v]=1;    } printf ("%d\n", Hungary ()); } return 0;}



[Graph theory] binary graph matching (Hungarian 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.