Maximum matching of binary graphs (Hungarian algorithm) HDU1083

Source: Internet
Author: User

Two-part diagram: The two-part graph, also known as two-part graph, is a special model in graph theory. Set g= (V,e) is a graph, if vertex V can be divided into two disjoint subsets (A, b), and each edge (I,J) in the diagram is associated with two vertices I and J respectively belong to these two different vertex sets (I in A,j in B), it is said that figure G is a two-part graph. The sufficient and necessary condition for the   graph G to be two points is that G has at least two vertices and that all of its loops are of an even length.    Maximum match: given a binary graph G, in a sub-figure m of G, any two edges in M's edge set are not attached to the same vertex, then M is a match. Selecting the largest subset of such edges is called the maximum matching problem of graphs, and if each vertex in the graph is associated with an edge in the graph, it is called an exact match, also known as a complete match.   Minimum Coverage: Minimum coverage requires a minimum number of points (either x or Y sets) to have each edge associated with at least one of the points. It can be proved that the fewest points (i.e. the number of overlays) = maximum number of matches   minimum path coverage:  cover all nodes with a direction-free graph G with as few disjoint simple paths as possible. To solve such problems, a binary graph model can be established. Divide all vertices I into two: I in the X node set and the Y node concentration i ', if there is an edge i->j, then in the binary graph to introduce the Edge I->j ', set two graph maximum match to M, then the result is n-m.   Augmented path (augmented orbit): if P is a path in Figure G that connects two unmatched vertices, and an edge that belongs to M and an edge that does not belong to M (that is, the matched and matched edges) appears alternately on p, then p is an augmented path relative to M (for example, a, B set, A point in the augmented route a leads to a point in B, and this point in B leads to a point in a ... alternately).   The nature of the augmented path: 1 has an odd number of edges. 2 beginnings in the left half of the dichotomy, ending at the right half. 3 The point on the path must be one on the left half, one in the right half, alternating. (In fact, the nature of the binary chart determines this point, because the two points of the same side of the point is not connected to the side, do not forget oh.) 4 There are no duplicate points on the entire path. 5 The starting and ending points are not currently paired, and all the other points are well-matched. All the odd edges on the 6 path are not in the original match, and all the even-odd bars appear in the original match. 7 Finally, and most importantly, adding all the odd bars on the augmented path to the original match, and removing all the odd bars in the augmented path from the original match (this operation is called the inverse of the augmented path), the new match number is 1 more than the original match number. After understanding the definition and nature of the augmented path, we carefully understand the nature of the 7th article. Since the length of the augmented path is odd, we might as well set it to 2*k+1, and because the first is a non-matching edge, and the matching and non-matching edges alternately appear, so the non-matching edge has k+1 bar, matching side has K bar. At this point, we do reverse operation, then the number of matching edges will be +1 on the original basis. The "Hungarian algorithm", which is the best match, is the idea: whichever match starts, each operation makes the match number +1, expanding until the augmented path is found, and the maximum match is obtained. Hungarian algorithm: The core of the algorithm is to find an augmented path based on an initial match until there is no augmented path. The nature of the Hungarian algorithm is actually similar to the maximum flow algorithm based on the augmented path feature, with only two points to be noted: (a) Each X node has to do a maximum of the starting point of the augmentation path; (b) If a Y-node has been matched, So the only way to get here is to go to the matching point of the Y node (you can recall the back edge of the maximum flow algorithm, which can be augmented by the back edge). When looking for an augmented path, either DFS or BFS can be used, both of which can guarantee the complexity of O (nm), because the complexity of finding an augmented path is O (m), which is increased by up to N times, and DFS is more concise in its actual implementation. The basic mode of the Hungarian algorithm: 1, the initial maximum match is empty 2, while (find the augmented path) 3, do   Add the augmented path to the maximum match. If the left half of the binary graph has n points, up to n increments, if there are M-bars in the graph, each augmented path iterates through the edges, so the time complexity is O (n*m); the idea of:  algorithm is to find the augmented orbit continuously, and increase the number of matches. Augmented orbit, as the name implies, is a path that can make the number of matches more, in the matching problem, the expression of augmented orbit is a "staggered track", that is, the edge of the graph is composed of the path, its first edge is currently not involved in matching, the second side participated in the match, the third side did not. The last edge does not participate in the match, and the start and end points have not been selected. So staggered, he obviously had odd edges. So for such a path, we can change the first edge to match, and the second edge to an unmatched ... And so on. That is, the "inverse" of all the edges, it is easy to find such modifications, the match is still legal, but the number of matches increased by one pair. In addition, a separate edge that connects two unmatched points is clearly a staggered track. It can be proved that when the augmented orbit is no longer found, a Maximum match. This is the idea of the Hungarian algorithm. The three most important formulas in the binary graph matching are: The minimum vertex coverage of the binary graph = two the maximum matching; the minimum path overlay of the DAG graph = number of nodes (n)-the maximum number of matches; The maximum independent set of two graphs = number of nodes (n)-Maximum matching number;  Example HDU 1083 courses 
1#include <iostream>2#include <cstdio>3#include <cstring>4 using namespacestd;5 Const intn= -;6 BOOLFlg,visit[n];7 BOOLMat[n][n];8 intMatch[n];9 intans,p,n,t;Ten  One BOOLDfsintv) {//Search for augmented paths A      for(intI=1; i<=n; i++) { -         if(Mat[v][i] &&!Visit[i]) { -  thevisit[i]=true; -             if(match[i]==-1||DFS (Match[i])) { -match[i]=v; -                 return true; +  -             } +  A  at         } -     } -     return false; - } -  - intMain () { inFreopen ("C:\\code\\in.txt","R", stdin); -     //freopen ("C:\\code\\out.txt", "w", stdout); toscanf"%d",&T); +      while(t--) { -scanf"%d%d",&p,&n); the          for(intI=1; i<=p; i++) { *              for(intj=1; j<=n; J + +) { $mat[i][j]=false;Panax Notoginseng             } -         } the          for(intI=1; i<=n; i++) { +match[i]=-1; A         } theflg=true; +          for(intI=1; i<=p; i++) { -             intT; $scanf"%d",&t); $             if(t==0) { -flg=false; -             } the              while(t--) { -                 inttmp;Wuyiscanf"%d",&tmp); themat[i][tmp]=true; -             } Wu         } -ans=0; About         if(FLG) { $              for(intI=1; i<=p; i++) { -memset (Visit,false,sizeof(visit)); -                 if(Dfs (i)) { -ans++; A                 } +             } the             if(ans==p) -Puts"YES"); $             Else thePuts"NO"); the}Else { thePuts"NO"); the         } -  in  the     } the  About fclose (stdin); the     return 0; the}

Maximum matching of binary graphs (Hungarian algorithm) HDU1083

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.