Reprinted Baidu Library
Algorithm-Art
Analysis of Bipartite Graph Matching
Many people say that algorithms are an art. But for beginners, I do not have a deep understanding of algorithms, but occasionally feel his strong charm and vitality.
This makes it impossible for me to stop pursuing algorithms. Next, I will analyze the Hungarian algorithm and common graph creation methods to enjoy the beauty of the algorithm.
Hungary Algorithm
The Hungarian algorithm is used to solve the problem of maximum Bipartite Graph Matching. The so-called bipartite graph means that "a set of point sets can be divided into two parts, and each part has different points, the vertices in the two parts can have edges ". The so-called maximum bipartite graph matching is "to find a subset of all edges of a bipartite graph. This subset meets two conditions,
1: Any two edges do not depend on the same vertex.
2: Make the sub-set as many sides as possible when condition 1 is met.
First, we can find all the edge sets that meet the preceding conditions by searching, and then select the set with the most number of edges from all edge sets, however, we can feel that the time complexity of this algorithm is an exponential function of the number of edges. Therefore, we need to find a more efficient method. Currently, the most effective methods are the Hungary algorithm and the network stream algorithm by adding the sink point and source point. For data with the number of points between 200 and 300, we adopt
The Hungarian algorithm is easier to implement than the network stream. The following describes the Hungarian algorithm:
Before introducing Hungary, let's talk about "Augmented rail" first ".
Definition:
If P is the path connecting two unmatched vertices in graph G, and is the edge of m in the largest matching edge set and is not the edge of M (that is, the edge that has been matched and to be matched) when P appears alternately, P is a augmented rail definition relative to m, which is always abstracted and understood in the following diagram.
The line segment (2-> 3, 3-> 1, 1-> 4) in the figure is the P path mentioned above,
We assume that the edge () is a matched edge, and () is an unmatched edge, then the side () and the side) the path P appears alternately, so P is an augmented orbit relative to M, in this way, we can replace edge 1 and 4 with edge 2 and 3. Then we can add 1 to the number of matched edge sets.
The Hungarian algorithm is the same as the constant pursuit of augmented rail implementation. Obviously, if the two vertices in the bipartite graph are N and m, the maximum number of matching values should be less than or equal to min (n, m ); therefore, we can enumerate every vertex in the first part (the second part can also be used). We will start from every vertex to find the augmented orbit. Finally, after finding the vertex in the first part, we can find the maximum number of matches. Of course, we can also find these edges by recording them.
The following two theorem about the maximum matching of bipartite graphs are given.
1: Maximum number of matches + maximum independent set = N + m
2: The minimum overwrite count of the Bipartite Graph = the maximum number of matching
3: Minimum path overwrite = Maximum Independent Set
The largest independent set refers to finding the largest point set in a bipartite graph. The points in the set are not connected to each other.
The minimum vertex overwrite refers to the use of the least vertex in a bipartite graph to associate all edges with at least one vertex.
The minimum path overwrite refers to a directed graph G without loops. A path overwrite of G is a set of non-intersecting paths p, each node in the figure contains only one path in P. The path can start and end from any node, and the length is also any value, including 0
The maximum matching pseudocode of the Bipartite Graph is given below:
/* Use [I] indicates whether vertices J in the second part have been used in the array. The point where Match [I] matches vertex I in the second part is match [I] */INT getaugmentpath (number) // find the augmented orbit through the number point, if 1 is found, 0 is not found. P-> related [number] is returned. next; // The vertex connected to the number; while (P! = NULL) {if not used [p] Then // P point has not been used if match [p] = 0 or getaugmentpath (Match [p]) // The point P is not paired with another vertex. // or the point P is paired with another vertex. Other vertices and other vertices can be found. (The augmented orbit is found.) return 1; p-> P. next // The next vertex that is connected to number} return 0;} end getaugmentpath; // we only need to find the augmented orbit for each vertex of a certain part of the point set in the main program; int main {ans <-0 for I = 1-> N // enumerate the N points in the first part; for j = 1-> M use [J] <-false // indicates the M points in the second part as unused. If getaugmentpath (I) Then ans <-ans + 1 // you can add one to the number of edges if you find the augmented orbit ;}
Every time we try to find a unique point P that matches the point I above, there are two strategies. One is to find a point P directly in the following points, he has not matched the above vertex (that is, Match [p] = 0 ). Second, when P matches a point above, that is, (Match [p]), we start from match [p] and find another matching point for him, leave P points to vertex I. So we won't find one more?
However, for the Hungary algorithm, the difficulty is not with the program itself, but how to convert the actual problem into a maximum Bipartite Graph Matching Model, and then use the Hungary algorithm to solve the problem.
The following describes several common graph creation models.
Common graph creation Models
Method 1
Row-column matching
Poj3041
Solution report
It is a 3*3 matrix. 1 in the square indicates that there are enemies in this place, and 0 indicates that there are no enemies. Now we have many arrows, each arrow can kill one or more enemies. The problem is that we want to kill all the enemies for use.
How many arrows?
At first glance, it seems that there is no correlation with the maximum bipartite graph. However, if we change our perspective, we think: we want to kill an enemy, you only need to let an arrow go through his position. That is, all the positions are covered by arrows.
That is, the minimum coverage of vertices. Since it is the minimum coverage of vertices, and we want to kill the enemy, our points should be the enemy's seat, that is, the (row and column) for the figure above, can I create the following model? The coordinates of the person are (, 2, 3, and 1). We can use the horizontal and vertical coordinates of these points to create a graph.
(The abscissa of each point is the first part, and the ordinate is the other part. Two numbers connected by edges represent a point)
As we have said above, the minimum vertex overwrite of a bipartite graph is to find the least edge to overwrite all the vertices, which meets the requirements of this question. The above also gives a property, that is, the minimum vertex overwrite of a bipartite graph is equal to the maximum matching of a bipartite graph. Therefore, we only need to match the above Bipartite Graph with the maximum value. This makes it easy to change the original problem.
Method 2
Black and white Staining
Poj2246
Solution report
Another graph requires that all 1 in the square be changed to zero. Only two adjacent ones can be modified at a time. How many times does it need to be modified at least? There is a problem with finding the most worthwhile, but it seems to be used to find the most worthwhile algorithm (greedy, dynamic planning ......)
It is useless. Since it is proposed here, he will certainly be able to solve the problem with the maximum matching of a bipartite graph. The key is how to create a graph?
Since we can only get two adjacent ones at a time, we can also find two matching results when matching them. Is this where the question matches the biggest bipartite graph? Right. But each point can match the four points around him. How can we divide all the points into that part? Right: place the I vertex in the first part, and the four vertices around the I vertex in the second part, then we put the 16 points around these four points in part 1st. With this idea, we only need to make the changes to the source image: black and white dyeing makes the color of the surrounding area and the middle area different.
Black and white in the figure means to classify the points. The values of 1, 2, 3, 4, 5, and 6 represent the number of 1 in the 0, 1 graph above.
Create a graph to connect adjacent points, such as 1 and 2 2 and 3 .......
Then, we need to change all vertices to zero, that is, we need to operate on each vertex and each vertex must have it. Isn't that the minimum vertex overwrite? Yes, this problem has been solved.
Fa 3
Anti-Construction Method
Poj2771
Solution report
Problem Background: an extremely feudal teacher wants to bring his classmates out to play, but he is afraid of having a relationship between his classmates. The teacher studied and found that, two students meeting the following conditions are unlikely to have an affair.
1 "height difference> 40
2> same gender
3. Different interests in music
4> similar sports
Obviously, if we use the Edge building between students who meet the above conditions, the final building is not a bipartite graph. Let's take a look at it. Boys and girls share the same gender. Therefore, we can
Men and women are divided into two parts. How do men and women build edges? If we connect men and women to meet their needs without having a relationship, the greatest matching is not representative and we cannot get the desired result. Therefore, we use the anti-construction method to establish the sides of a possible relationship between men and women. That is to say, the height difference is equal to or equal to 40. Then find the largest independent set. The principle of the largest independent set is not to find a point set,
So that the points in the set are not connected to each other and there are as many points as possible? We connect men and women who may have a relationship. Isn't the largest set of independence the set of people we are looking for that cannot have a relationship? So, this problem has been solved!
Fa 4
Split Point Method
The split-point method is used to solve the problem of least path coverage.
To find several paths, you can pass through all the points, and the paths cannot cross. Our practice is to split the vertex into two parts (split point 1 into X1, split point 2 into X2, y2 ......)
If we find that the greatest matching of a bipartite graph corresponds to a path Overwrite for each matching, the maximum matching of the Bipartite Graph is: the minimum path in the source image overwrites the number of edges (the path consists of 0, 1, or multiple edges ). Then, the minimum path overwrites of the source image = the number of source image vertices-the number of edges on the minimum path, that is, the minimum path overwrites of the source image = the number of source image vertices-the maximum number of matching for the bipartite graph.
France 5
One row changes to multiple rows, and one column changes to multiple columns
The above is a 4*4 square, with ### in the square representing the wall. We need to build a bunker where there is no wall in the table, in addition, to ensure that no two bunker blocks can attack each other, ask how many bunker blocks can be built at most? Is it like the first question? If we create a graph like the first question, the final maximum match is the matching of rows and columns. In addition, this match satisfies that all the matches are different columns in different rows (the matching itself is that each vertex or multiple columns belong to a matching edge ). But what should we do if we build a graph like this? A wall is equivalent to dividing this row and this column into two rows and two columns.
For example
One row is changed to two rows.
That's the point.
Then we can create a graph based on the number.
Find the maximum matching value for the bipartite graph. This problem has also been solved!
An algorithm used to find Binary Graph Matching-the Hungary algorithm, cleverly applied the method of finding the augmented track, making it efficient and easy to understand to find the maximum matching of binary graphs, this has the artistic feeling. However, an operation applied to a bipartite graph can use clever methods to take things that seem irrelevant to a bipartite graph, how wonderful it is to convert an artistic hand into a bipartite graph. Algorithms are amazing and confusing. When you think you are familiar with an algorithm, you will immediately find that you only have a little understanding of it, this is the algorithm.