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.
two-part diagram: In simple terms, if the midpoint of the diagram 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< Span id= "mathjax-span-3" class= "Mi" >u and v , so that each edge is connected separately u , v . 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 :
Focus on understanding the above algorithm principle, the implementation of code online similar to a lot of
The key points of the Hungarian algorithm are as follows
- Starting with the 1th vertex on the left, select an unmatched point to search for the augmented path. 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.
- If an unmatched point is passed, the search succeeds. Update the path information, match the number of edges +1, stop the search.
- 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.
Performance comparison
Two versions have a time complexity of O(V-E). 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
(turn) The maximum matching, perfect match and Hungarian algorithm for binary graphs