Detailed drawings of the binary chart

Source: Internet
Author: User

Go to: http://blog.csdn.net/pi9nc/article/details/11848327

The maximum matching, perfect match of binary graph and Hungarian algorithm 2013-08-01 algorithms binary graph matching, graph theory, algorithm

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: divide the vertex of a graph into two disjoint sets U and V, so that each edge is connected to the vertices in U and V, 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. For example, in Figure 3, 1, 4, 5, 7 are matching points, the other vertices are mismatched points, 1-5, 4-7 are matching edges, and 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 in the figure below, 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. In other words: The maximum number of boys/girls who love each other can pair. 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, one of the augmented paths in Figure 5 is shown in Figure 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 a BFS tree as shown in Figure 8:

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 situation is shown in Figure 9 (by the way, the root node 2 to the non-matching leaf node 7 in Figure 8 is obviously an augmented path, and a perfect match will be obtained along the augmented path).

Due to the compact time, the code is excerpted from http://dsqiu.iteye.com/blog/1689505 #define MAXN 10//represents the maximum number of vertices in the X and Y sets.  
The number of vertices in the int nx,ny;//x collection and y collection int edge[maxn][maxn];//edge[i][j] to 1 means that IJ can match the int cx[maxn],cy[maxn];//to record which of the matching y elements in the X collection.  
The int visited[maxn];//is used to record whether the vertex has been accessed.  
    int path (int u) {int V;  
           for (v=0;v<ny;v++) {if (Edge[u][v]&&!visited[v]) {visited[v]=1; if (cy[v]==-1| |  
                Path (Cy[v])//If the V element in the Y-set does not match or V has matched, but can find an augmented path {cx[u]=v from cy[v];//Find the augmented path, modify the matching m  
                Cy[v]=u;  
            return 1;  
}}} return 0;  
    } int Maxmatch () {int res=0;  
    memset (cx,0xff,sizeof (CX));//The initial value of 1 means that there are no matching elements in the two collection.  
    memset (cy,0xff,sizeof (CY)); for (int i=0;i<=nx;i++) {if (cx[i]==-1)//is not yet matched, execute internal code {memset (visited,0,size  Of (visitited));   Reset is marked for access Res+=path (i);  
    Start with I as the starting point to find the augmented path, return true, match number +1    }} return res; }



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.