Maximum matching, perfect matching and Hungarian algorithm for binary graphs

Source: Internet
Author: User

Original: http://www.renfei.org/blog/bipartite-matching.html

Maximum matching, perfect matching and Hungarian algorithm for binary graphs

This article speaks of the maximum matching (maximum matching) and perfect match (perfect matching) of the No-power binary graph (unweighted bipartite graph), and the Hungarian algorithm for solving the match (Hungarian algorithm); Do not talk about the best match with the weighted binary graph.

binary diagram : Simply put, if the midpoint of the graph can be divided into two groups, and all edges cross the boundary of the group, then this is a two-part graph. To be exact: dividing the vertices of a graph into two disjoint sets $U $ and $v$, so that each edge is connected to $u$, $V $ vertices respectively. If such a division exists, the graph is a two-part graph. An equivalent definition of a binary graph is: A graph that does not contain "rings with odd numbers of edges". Figure 1 is a two-part diagram. In order to be clear, we will later draw it into the form of Figure 2.

match : In graph theory, a "match" (matching) is a set of edges in which any two edges have no public vertices. For example, the red edge in Figure 3, Figure 4, is the match of Figure 2.

We define matching points , matching edges , unmatched points , mismatched edges , and they are very obvious. Example 3, 1, 4, 5, 7 is the matching point, the other vertices are unmatched points, 1-5, 4-7 is the matching edge, the other edges are non-matching edges.

Maximum match : A match with the largest number of matched edges in all matches of a graph, called the maximum match for this graph. Figure 4 is a maximum match that contains 4 matching edges.

Perfect Match : if one of the graphs has a match, all vertices are matching points, then it is a perfect match. Figure 4 is a perfect match. Obviously, the perfect match must be the maximum match (any point of the perfect match has already been matched, adding a new matching edge will certainly conflict with the existing matching edge). But not every diagram has a perfect match.

For example: as shown, if there is a connecting edge between a pair of boys and girls, it means they like each other. Is it possible for all boys and girls to be paired 22 so that each pair likes each other? In graph theory, this is the perfect match problem. If it's a different story: how many boys/girls do you like to pair with each other? This is the maximum matching problem.

The basic concept is finished. One of the algorithms for solving the maximum matching problem is the Hungarian algorithm , and the concepts below are for this algorithm service.

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

Augmented Road : From an unmatched point, take the alternate road, if the way another unmatched point (the point of departure does not count), then this alternate road is called the augmented path (agumenting path). For example, Figure 5 shows an augmented path of 6 (the matching points in the figure are marked in red):

There is an important feature of the augmented path: a non-matching edge is more than a matching edge. Therefore, the significance of studying the augmented path is to improve the matching . Simply swap the identities of the matching and non-matching edges in the augmented path. 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.

We can add matching edges and matching points in the match by constantly looking for the augmented path. When the augmented path is not found, the maximum match is reached (this is the augmented path theorem). This is exactly what the Hungarian algorithm does. Before you give the Hungarian algorithm DFS and BFS versions of the code, first talk about the Hungarian tree.

The Hungarian tree is generally constructed from BFS (similar to the BFS tree). Run BFS from an unmatched point (the only limitation is that you must take the alternate path) until you can no longer expand. For example, from Figure 7, you can get 8 of a BFS tree:

This tree has a leaf node as a non-matching point (number 7th), but the Hungarian tree requires all leaf nodes to be the matching points, so this is not a Hungarian tree. If the original image does not contain the number 7th node, then a Hungarian tree will be obtained from node 2nd. This scenario is shown in 9 (by the way, in Figure 8, the root node 2 to the non-matching leaf node 7 is obviously an augmented path that will be a perfect match along the augmented path).

The following is the code for the DFS and BFS versions of the Hungarian algorithm :

The number of vertices and edges starts at 0adjacency Table StoragestructEdge{IntFrom;IntTo;IntWeight;Edge(IntF,IntT,IntW):From(F),To(T),Weight(W){}};vector<int> G [__maxnodes/* g[i] storage vertex i departure edge number */vector<edge > edges; typedef vector<int>:: Iterator iterator_t;num_nodes; int num_left; int num_right; int num_edges           
IntMatching[__maxnodes];/* Store Solution Results */IntCheck[__maxnodes];boolDfs(IntU){For(iterator_tI=G[U].Begin();I!=G[U].End();++I){For each adjacency point of uIntV=Edges[*I].To;If(!Check[V]){Requirements are not on alternate routesCheck[V]=True;Put on Alternate RoadIf(Matching[V]==-1||Dfs(Matching[V])){If it is an open point, it indicates that the alternate route is an augmented path, then the route is switched and the success is returnedMatching[V]=U;Matching[U]=V;ReturnTrue;}}}ReturnFalse;There is no augmented path, return failure}IntHungarian(){IntAns=0;Memset(Matching,-1,sizeof(Matching));For(IntU=0;U<Num_left;++u) {if  (matching [u == -1) {memset (check 0sizeof (checkif  (dfs (u++ans} } return ans;}    
Queue<Int>Q;IntPrev[__maxnodes];IntHungarian(){IntAns=0;Memset(Matching,-1,sizeof(Matching));Memset(Check,-1,sizeof(Check));For(IntI=0;I<Num_left;++I){If(Matching[I]==-1){While(!Q.Empty())Q.Pop();Q.Push(I);Prev[I]=-1;Set I as the starting point for the pathboolFlag=False;The augmented path has not been foundWhile(!Q.Empty()&&!Flag){IntU=Q.Front();For(iterator_tIx=G[U].Begin();Ix!=G[U].End()&&!Flag;++Ix){IntV=Edges[*Ix].To;If(Check[V]!=I){Check[V]=I;Q.Push(Matching[V]);If(Matching[V]>=0){This point is a match pointPrev[Matching[V]]=U;}Else{Find an unmatched point and alternate path becomes an augmented pathFlag=True;IntD=U,E=V;While(D!=-1){IntT=Matching[D];Matching[D]=E;Matching[E]=Dd = prev[d; span class= "n" >e = t} } } } q.pop (); } if  (matching[i ] != -1) ++ans} } return ans;}    

The key points of the Hungarian algorithm are as follows

    1. Starting with the 1th vertex on the left, select an unmatched point to search for the augmented path.

      1. If an unmatched point is passed, the search succeeds. Update the path information, match the number of edges +1, stop the search.
      2. If the augmentation path has not been found, the search is no longer started from this point. In fact, a Hungarian tree is formed after the search. We can delete it permanently without affecting the result.
    2. Because we need to update the match along the path after we find the augmentation path, we need a structure to record the points on the path. The DFS version implicitly uses a stack through function calls, while the BFS version uses prev an array.

Performance comparison

Two versions have a time complexity of $o\big (V \cdot e\big) $. DFS has the advantage of clear thinking, less code, but performance is inferior to BFS. I tested the performance of the two algorithms. For sparse graphs, the BFS version is significantly faster than the DFS version, while for dense graphs the two are comparable. In the total random data 9,000 vertices 4, 0000 edges of the former lead the latter about 97.6%, 9,000 vertices 100,0000 edge when the former lead the latter 8.6%, while reaching the 500,0000 edge, BFS only lead 0.85%.

Supplemental Definitions and theorems:

Maximum matches: the number of matching edges that match the maximum

minimum Point coverage : Select a minimum point so that at least one end of any edge is selected

maximum independent number : Select the most points so that any selected two points are not connected

Minimum path coverage : For a DAG (directed acyclic graph), select the fewest paths so that each vertex belongs to and belongs to only one path. The path length can be 0 (that is, a single point).

Theorem 1: Maximum number of matches = minimum point coverage (this is the Konig theorem)

Theorem 2: Maximum number of matches = maximum Independent number

Theorem 3: Minimum path coverage = number of vertices-maximum number of matches

Maximum matching, perfect matching and Hungarian algorithm for binary graphs

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.