Hungarian algorithm and its template-[binary graph maximum match]

Source: Internet
Author: User

References from https://www.renfei.org/blog/bipartite-matching.html

Two-part diagram: Divides all vertices of a graph G into two disjoint sets L  and R V "> < /span>  , so that each edge in Figure g is connected to L, R  U "> the vertices in V " >

Match: A "match" (matching) is a collection of edges, where any two edges have no public vertices.

Maximum match: In all matches of a graph, there is a match, the number of edges it contains is the largest of all matches, and the match is called the maximum match of the graph.

Alternate path: From an unmatched point, followed by a non-matching edge, matching edge, non-matching edge ... A path formed is called alternating road.

Augmented road: For an alternate road whose end point is an unmatched point (the starting point does not count), this alternate path is called the augmented path (agumenting path).

(Because the alternate path is defined so that there is no unmatched point except the start and end of the path)

The augmented road has an important feature:

A non-matching edge is one more than the matching edge. Therefore, the significance of the study of the augmented path is to improve the matching, augmented path augmentation: As long as the augmented path of matching and non-matching edge of the identity exchange can be.

This does not break the matching character because there are no other matching edges in the middle of the matching node. After the swap, the number of matching edges in the figure is 1 more than the original one.

Then, according to the Augmented road theorem, the maximum matching is reached when the augmented path is not found;

Therefore, the Hungarian algorithm is obtained:

Starting from the 1th vertex of the L set, enumerate the unmatched points I to search for the augmented path.

If you go to an unmatched point, the representative finds an augmented path, immediately augmented, matching the number of sides +1;

Then obviously I point becomes a match point, according to the definition of alternate road, so there is no alternate road from I point, there is no augmented road, so stop the search with I point as the starting point, enter the next point.

If the augmented path has not been found, it is no longer searched from the I point and goes directly to the next point.

Hungarian algorithm templates:

structedge{intu,v;}; Vector<Edge>E;vector<int>G[max];intLn,rn;//the number of vertices of the l,r set of two graphsintMatch[max];intVis[max];BOOLDfsintu) {     for(intI=0, _size=g[u].size (); i<_size;i++)    {        intv=e[g[u][i]].to; if(!Vis[v]) {Vis[v]=1; if(!matching[v] | | DFS (MATCHING[V))//If an unmatched point is encountered, augment it directly, otherwise continue the search.{Matching[v]=u; Matching[u]=v; return 1; }        }    }    return false;}intHungarian () {intret=0; memset (Matching,0,sizeof(matching));  for(intI=1; i<=ln;i++)    {        if(!Matching[i]) {memset (Vis,0,sizeof(VIS)); if(Dfs (i)) ret++; }    }    returnret;} 

Hungarian algorithm and its template-[binary graph maximum match]

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.