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.
Maximum matching of binary graphs:
Hungarian Algorithm
Perfect match: If all points are on the matching edge, the maximum match is said to be the perfect 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) = The maximum number of matches
Minimum path overrides:
Cover all nodes of the 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.
Maximum independent set problem:
The M points are selected in Figure g of n points so that there is no edge between the M points 22. To find the M maximum value.
If the graph G satisfies the binary graph condition, it can be done by the binary graph matching. Maximum independent set point = N-Maximum number of matches
The Hungarian algorithm for the maximum matching problem of binary graphs:
There are two ways to find the maximum match for a graph, the first is the maximum flow (I assume that the reader already has the knowledge of the network flow), and the second is the Hungarian algorithm I'm going to talk about now. This algorithm is plainly the maximum flow algorithm, but it is based on the binary map matching the characteristics of the problem, the maximum flow algorithm to simplify, improve efficiency.
The core problem of the maximum flow algorithm is to find the augmented path (augment path). The Hungarian algorithm is no exception, its basic pattern is:
Initial maximum match is empty While the augmented path can be found Do add the augmented path to the maximum match |
The visible and maximum flow algorithms are the same. But here the augmented path has its certain particularity, let me analyze it.
(Note: Although the Hungarian algorithm is essentially the maximum flow algorithm, it does not need to build a network model, so the source and sink points are no longer needed in the diagram, just a two-part diagram.) Each edge does not need to have a direction. )
Figure 1 shows a match in the binary diagram I gave: [1,5] and [2,6]. Figure 2 is an augmented path found on the basis of this match:3->6->2->5->1->4.Let's take a look at the nature of the augmented path in the binary diagram:
(1) There is an odd number of edges.
(2) The starting point is in the left half of the dichotomy, ending at the right half.
(3) The point on the path must be one in the left half, and 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. (1, Figure 2, [1,5] and [2,6] in Figure 1 are two pairs already paired with the point, and the beginning 3 and end of 4 are not currently matched with other points. )
(6) All the odd-number edges on the path are not in the original match, and all of the even-odd bars appear in the original match. (1, Figure 2 shows that the original match is [1,5] and [2,6], the two side of the edge in Figure 2 given in the augmented path of the edge is the 2nd and 4th edge. The 1th, 3, and 5 edges of the augmented path do not appear in the match shown in Figure 1. )
(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 an augmented pathTake counter), the new match number is increased by 1 compared to the original match number. (2 shows that the new match is all blue edges, and all red edges are removed from the original match.) The new match number is 3. )
It is not difficult to figure out that, at the beginning, when there is no match, the two gray edges in Fig. 1 are themselves augmented paths. So the process of finding the maximum match in this binary chart may be as follows:
(1) Find the augmented path 1->5 and reverse it, then the match number increases to 1.
(2) Find the augmented path 2->6 and reverse it, then the match number increases to 2.
(3) Find the augmented path 3->6->2->5->1->4 and reverse it, then the match number increases to 3.
(4) Can not find the augmented path, end.
Of course, this is just a possible process. There may also be other ways to find the augmented path, or to find different augmented paths, and the final matching scheme may not be the same. But the maximum number of matches must be the same.
The augmented path can also be described in a recursive way. This description is not necessarily the most accurate, but it reveals a general way to find an augmented path:
"Augmented path from point a" must first connect to a point B that is not paired with point A in the original match. If point B is not paired with any point in the original match, it is the end point of the augmented path, whereas if point B is paired with point C, the augmented path is from a to B, then from B to C, plus the "augmented path from point C". Also, the augmented path from C does not have a duplicate point with the augmented paths of the first half.
Compared to 2, we are looking for an augmented path from 3 to do the following 3 steps:
(1) First starting from 3, it can connect to the point of only 6, and 6 in Figure 1 has been paired with 2, so the current augmented path is 3->6->2 plus the augmented path from 2.
(2) Starting from 2, it can be connected to the first half of the path is not repeat the point of only 5, and 5 does not match the original matching with 2. So from 2 even to 5. However, 5 is already paired with 1 in Figure 1, so the current augmented path is 3->6->2->5->1 plus the augmented path from 1.
(3) Starting from 1, the point that can not be connected to self-pairing and not with the first half of the path repeats only 4. Since 4 is not paired with any point in Figure 1, it is the end point. So the final augmented path is 3->6->2->5->1->4.
But strictly speaking, the augmented path (2->5->1->4) from 2 and the augmented path from 1 (1->4) in the above process are not really augmented paths. Since they do not conform to the 5th nature of the augmented path previously spoken, their starting point is the point at which they have been fitted. We call them "augmented paths" here just to make it easier to explain the entire search process. The two paths themselves are only two of the returned results of sub-processes not known to the outside world.
Obviously, the above example shows that the method of searching for an augmented path is DFS, which can be written as a recursive function. Of course, using BFS can also be fully realized.
At this point, the theoretical basis of the part is finished. But to complete the Hungarian algorithm, an important theorem is also needed:
If you start from a point A and do not find an augmented path, then no augmented path will ever be found from a, no matter how many augmented paths are found from another point to change the current match.
To prove that this theorem is very complicated by words, it is hard to say, or I have to draw a picture, I will save it here. In fact, you draw a few pictures yourself, trying to give two counter-examples, this theorem is not difficult to figure out. (Give a hint.) If you try to give a counter example to show that you can find an augmented path from a when you find another augmented path and change the existing match. Then, in this case, it is certain that the augmented path can be found from a before the other augmented paths are found. This is inconsistent with the hypothesis. )
With this theorem, the Hungarian algorithm is formed. As follows:
Initial maximum match is empty Each point of the left half of the for binary diagram I Do from point I to find the augmented path. If found, it is reversed (that is, the total number of matches is increased). |
If there is a total of n points in the left half of the binary graph, then the maximum number of n-augmented paths is found. If the graph has m-bars, then it is necessary to traverse all the edges in each search for an augmented path (DFS or BFS), which takes the time that is M. So the total time is probably O (n * m).
[CPP]View Plaincopy
- #define N 202
- int useif[n]; //Record y whether the node uses 0 to indicate no access, 1 for access
- int link[n]; //Record the node of the X currently connected to the Y node
- int mat[n][n]; //Record the edges of the connection x and Y, if there is an edge between I and J is 1, otherwise 0
- int gn,gm; //Two number of points in X and Y in the chart
- int can (int t)
- {
- int i;
- For (i=1;i<=gm;i++)
- {
- if (useif[i]==0 && mat[t][i])
- {
- Useif[i]=1;
- if (Link[i]==-1 | | can (LINK[I]))
- {
- link[i]=t;
- return 1;
- }
- }
- }
- return 0;
- }
- int Maxmatch ()
- {
- int i,num;
- num=0;
- memset (Link,0xff,sizeof (link));
- For (i=1;i<=gn;i++)
- {
- memset (useif,0,sizeof (Useif));
- if (Can (i)) num++;
- }
- return num;
- }
The problem of really finding the maximum matching of binary graphs is very few and often makes some simple changes
Variant 1: minimum vertex coverage for binary graphs
The least point in the binary diagram is that each edge is associated with at least one of the points, which is the "minimum vertex overlay for the binary graph".
hdoj1150
The minimum vertex cover number of a binary graph = Two maximum match number of the graph
Variant 2:Minimum path overlay for DAG graph (no loop directed graph)
Overwrite all vertices of the directed acyclic graph (DAG) with as few disjoint simple paths as possible, which is the minimal path overlay problem for the DAG graph.
hdoj1151
Minimum path coverage for dag graphs = number of nodes (n)-Maximum number of matches (m)
Key: Finding the maximum matching number of binary graphs
Variant 3: maximum independent set of two-part graphs
hdoj1068
Maximum number of independent sets of binary graphs = number of nodes (n)-Maximum number of matches (m)
Key: Finding the maximum matching number of binary graphs
Resources:
1,http://imlazy.ycool.com/post.1603708.html
2, Hangzhou electric ACM PPT
The maximum matching problem of binary graph and its extension