Algorithm explanation: Binary graph matching
Binary map matching, natural to start with the definition, then what is the two-part diagram?
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.
Simply put, a graph is divided into two parts, the same part has no edge, then this figure is a binary graph, the dichotomy is a special figure.
The:
Given a binary graph G, in a sub-figure m of G, any two edges in M's edge set {E} are not attached to the same vertex, then M is a match.
The maximum match (maximal Matching) is the number of matched edges that can no longer be increased by increasing the unmatched matching edge, under the currently completed match. The maximum match (maximum matching) is a match of the largest number of sides in any maximal match. Selecting a subset of such a number of sides is called the maximum matching problem for graphs.
If one match, each vertex in the diagram is associated with an edge in the graph, it is said to be an exact match, also known as a complete match.
Binary graph matching can be used with maximum flow (maximal flow) or Hungarian algorithm (Hungarian algorithm)
Note that the Hungarian algorithm can be used in addition to binary matching of two graphs in the external binary map matching.
Note: There is also a HK algorithm in the binary graph matching, the complexity is O (sqrt (n) *e) because of the lower complexity, the code volume soared and most of the circumstances no one will be idle card sqrt complexity. In this first not to say, have the interest can own Baidu, looks like card this algorithm's only hdu2389
Well, first, let's explain the Hungarian algorithm process:
Hungarian algorithm:
The Hungarian algorithm is almost the core algorithm of binary graph matching, which can be used in addition to the two-graph multi-match.
The Hungarian algorithm is actually a kind of network flow thought, its core is seeks the augmentation road. The specific operation is, um. Pull Lang with
Note: The following transfer from http://blog.csdn.net/dark_scope/article/details/8880547
The Hungarian algorithm was proposed by the Hungarian mathematician Edmonds in 1965, hence the name. The Hungarian algorithm is based on the idea of sufficiency proof in Hall theorem, it is the most common algorithm of the part graph matching, and the core of the algorithm is to find the augmented path, which is an algorithm for finding the maximal matching of binary graph with the augmented path.
-------, wait, look at the big head? Then see the following version:
Through the efforts of several generations, you finally caught up with the tide of the remaining male, assuming you are a glorious new century matchmaker, in your hand there are n left male, m a woman, everyone may have a good impression on multiple heterosexual (-_-| | Temporarily regardless of special sexual orientation), if a pair of men and women, then you can put together the pair, now let us ignore all the unrequited love (a good feeling of sadness), you have is probably the following a diagram, each line is a mutual goodwill.
In the spirit of saving a life, the principle of building a seven-storey pagoda, you want to match as many couples as possible, the Hungarian algorithm working mode will teach you to do this:
===============================================================================
First : try to find the girl number 1th boys, found the first and he connected to the number 1th girls are still flowers, got it, connected to a blue line
===============================================================================
two : then to the 2nd boys to find a sister, found the first and he connected to the 2nd girl is no master, got it
===============================================================================
Three : Next is the number 3rd boys, it is very regrettable that the number 1th girls already have the Lord, how to do?
We tried to assign another sister to the boy who was a match for girl number 1th (aka Number 1th).
(yellow means the side is temporarily torn down)
The second girl with number 1th is the number 2nd girl, but the number 2nd girl also has the Lord, how to do? We're going to try to get a girl for number 2nd again. (Note this step is the same as above, this is a recursive process)
At this time found 2nd boys can still find the number 3rd girl, then the problem solved, go back
Number 2nd boys can find 3rd sister, ~ ~ 1th Boys can find 2nd sister, ~ ~ ~ 3rd Boys can find the number 1th sister
===============================================================================
Four : Next is the number 4th boys, unfortunately, according to the rhythm of the third step we can not give the number 4th boys to free a sister, we really do not have the means. Fragrant custard classmate walk well.
===============================================================================
This is the Hungarian algorithm of the process, in which looking for a sister is a recursive process, the most critical word is "teng" word
The principle is: there is a chance, no chance to create a chance to go on
hdu2063:
hdu2063 is a bare binary graph maximum matching problem, directly on the Hungarian algorithm can be.
- #include <cstdio>
- #include <cstring>
- #include <iostream>
- #include <queue>
- #include <vector>
- #include <cmath>
- #include <algorithm>
- Using namespace std;
- const int n=505;
- int line[n][n];
- int girl[n],used[n];
- int k,m,n;
- BOOL Found (int x)
- {
- For (int i=1; i<=n; i++)
- {
- if (Line[x][i]&&!used[i])
- {
- Used[i]=1;
- if (girl[i]==0| | Found (Girl[i]))
- {
- Girl[i]=x;
- return 1;
- }
- }
- }
- return 0;
- }
- int main ()
- {
- int x, y;
- while (scanf ("%d", &k) &&k)
- {
- scanf ("%d%d", &m,&n);
- memset (line,0,sizeof (line));
- memset (girl,0,sizeof (girl));
- For (int i=0; i<k; i++)
- {
- scanf ("%d%d", &x,&y);
- Line[x][y]=1;
- }
- int sum=0;
- For (int i=1; i<=m; i++)
- {
- memset (used,0,sizeof (used));
- if (found (i)) sum++;
- }
- printf ("%d\n", sum);
- }
- return 0;
- }
Optimal matching of binary graphs:
Next is the best matching km algorithm of the binary graph.
KM algorithm is really difficult to understand, I can only follow the code to speak, otherwise I simply do not understand. But listen to a senior said to understand the idea and not the code ... Then try to talk about it empty.
The general description of the KM algorithm can basically be summed up in the following several steps:
(1) Initialization of feasible benchmarks
(2) Searching for complete match with Hungarian algorithm
(3) Modify the feasible benchmark if no complete match is found
(4) Repeat (2) (3) until a complete match is found for the equal sub-graph
On the process and implementation of the algorithm, there are a lot of introduction on the Internet, basically is the discussion about how to modify the feasible benchmark, as to the principle does not give a deep discussion.
KM algorithm is used to find the best matching algorithm with weighted binary graph.
A binary chart is a graph in which all vertices can be divided into two sets: X and Y, where any two of x and Y are not connected in the same set of points, and vertices from the X-set are wired to vertices from the Y-set. When these lines are assigned to a certain weight, such a binary graph is a weighted two-part graph.
Binary graph matching is to find a set of edges, where the vertices are in two sets, and any two edges do not have the same vertex, this group of edges is called a binary graph matching, and the maximum number of edges can be obtained, called the maximum match of the binary graph.
We can also look at the maximum matching of the binary graph, that is, the default weight of each side of the two graphs is 1, and we find the maximum matching weights of the binary graphs. For the weighted binary graph, the edge has a weight greater than 0, find a set of matches, make it the most weight, that is, the best match with the weighted binary graph.
The Hungarian algorithm is generally used to find the maximum matching of a binary graph. The algorithm chooses the side of the binary graph according to certain rules to join the matching sub-graph, and its basic mode is:
Initialize matching sub-graph to null
While the augmented path can be found
Do add the augmented path to the matching sub-graph
The augmented path has the following characteristics:
1 an odd number of edges
2. Beginning at x-side of the dichotomy, end point on the Y side of the binary graph
3. The point on the path must be one on the X side, one on the Y side, and interlaced.
4. There are no duplicate points on the entire path
5. The starting and ending points are not currently paired, and the other points have been shown in the matching sub-graph
6. All the odd edges on the path are not currently on the edge of the current matching sub-graph, and all the even-odd bars have entered the current matching sub-graph. An odd edge is one edge more than an even edge
7. So when we add all of the odd-number edges to the matching sub-graph and delete the strip edges, the number of matches increases by 1.
For example, the blue is the current matching sub-graph, currently only the Edge x0y0, and then through X1 found the augmented path: x1y0->y0x0->x0y2
The first and the odd first edges x1y0 and x0y2 are not in the current matching sub-graph, and the number of the x0y0 is in the matching sub-graph, by adding x1y0 and x0y2 to the matching sub-graph and removing x0y0, the match count is increased from 1 to 2. Each time an augmented path is found, by adding a delete edge, we can always add 1 to the number of matches.
There are two ways to find the augmented path, one is deep search, the other is wide search. For example, from X2 to find the augmented path, if it is deep search, X2 find y0 match, but found that y0 has been X1 match, so go deep to X1, to find a new matching node x1, the results found no other matching nodes, so the match failed, X1 then find x2, found Y1 can match, The new augmented path was found. If it is wide search, X1 find y0 node, because can not immediately get a legitimate match, so it as a candidate into the queue, and then find Y1, because Y1 has been matched, so the match successfully returned. Relatively speaking, the deep search to be easy to understand, its stack can be maintained by the recursive process, while the wide search will need to maintain a queue, and on the way over the route to do their own marking, to achieve a more troublesome.
For a binary graph with weights, we can think of it as a binary graph of all the vertices of the X set to all the vertices of the Y-set (adding the previously absent edges into a binary graph with a weight of 0), meaning that it must have a complete match (that is, its match number is min (| x|,| y|) )。 In order to maximize the weight, we actually choose the edge by the greedy algorithm, forming a new binary graph (we call it a two-molecule map), and on the basis of the binary map to find the maximum match, when the maximum match is a complete match, we can determine that the match is the best match. (Here we define the maximum match: match and best match with the maximum number of matched edges: the weight of the matched edge and the maximum match.) )
The greedy algorithm always adds the optimal edge to the two-molecule graph, and the optimal edge will bring the greatest contribution to the current matching sub-graph, and the measure of contribution is achieved by benchmarking. Below we will explain this process in an example.
With the right two-part diagram:
The algorithm converts weights to benchmarks, and each vertex of the X-set and Y-sets has a benchmark value, and the weights are all placed on the X-set in the initial case. Since each vertex will have at least one matching point, the greedy algorithm must first select the most weighted edge on the vertex (ideally, these edges have no intersection, so we naturally get the best match). The initial two molecular figure is: (you can see that the X-benchmark is the maximum weight on the vertex at initialization, and the Y-benchmark is 0)
Find the augmented path from the X0, find the X0y4, from X1 can not find the augmented path, that is, you must add a new edge to the two molecular diagram, so that X1 to find its match, at the same time the sum of the weights added maximum. Since X1 to Y4 and Y4 has been X0 matched, there are two possible, one is to find a new match for X0 and Y4 to X1, or to find a new match for X1, now we will see the role of the benchmark. According to the traditional algorithm description, the condition of being able to enter the edge of the two molecular graph is L (x) +l (y) >=weight (XY). When an augmented path is not found, the X-vertex set on the searched path is set to S,y-T, and for all points in S and points not in T YJ, d=min{(L (xi) +l (YJ)-weight (XIYJ)} is computed, Subtract d from the X benchmark in the S-set and add it to the benchmark of y in the T-set, because the X-benchmark in the S-set is reduced, and the Y-benchmark in T is not the same, which is equivalent to the two sets of L (x) +l (y) smaller, that is, a new edge can be added to the two From the greedy side of the edge, we can choose a new edge for the X0 to discard the original two molecular map of the matching edge, you can also choose a new edge for X1 and discard the original two molecular map of the matching edge, because we can not select both X0y4 and X1y4, because this is an illegal match, this time, d=min{(L (xi) The meaning of +l (YJ)-weight (XIYJ)} is that we select a new edge, which will be added to the matching sub-graph so that the matching is valid, select this edge to form the matched sub-graph, The weight of the illegally matched sub-graph, which is the same as the original matching sub-graph plus this illegal edge, and (if it is legal, it will be the largest) is the smallest, that is, the maximum weight. A good wound. To express in a mathematical way, set the original illegal match (its weight is the largest, because we always find from the weight of the largest side) of the weight of W, the new legal match for W ', D for Min{w-w ' I}. In this example, S={x0, x1},y={y4}, find the minimum d=l (X1) +l (Y0)-weight (x1y0) = 2 to get a new two molecular diagram:
Re-X1 search for the augmented path, find the x1y0, you can see the new matching sub-graph weight of 9+6=15, than the original illegal matching weight 9+8=17 just less d=2.
Next, you cannot find the augmented path from X2, and the path it walks through is shown as a blue route. The resulting illegal matching sub-graph: x0y4,x1y0 and x2y0 weights and is 22. On this path, if a new match is found for any of the vertices in s={x0,x1,x2}, the problem can be resolved and then the D is started.
D=l (X0) +l (Y2)-weight (x0y2) =l (X2) +l (Y1)-weight (x2y1) =1.
The new two molecular chart is:
Re-searching for the augmented path for X2, if we are using deep search, we will get the path: X2y0->y0x1->x1y4->y4x0->x0y2, that is, the odd bar edge and delete the even number of edges, the new matching sub-graph from these vertices to get the new weight of 21 If you are using a wide search, you will get the path x2y1, and the original two matching edges with a weight of 21. Suppose we are using a wide search, and the resulting new matching sub-graph is:
Next, and so on, until a match point is found for X4.
The biggest feature of the KM algorithm is the use of benchmarks and weights to generate a two-molecule graph, finding the maximum match on the two molecular map, and when only a complete match is found, the best match can be obtained. The role of benchmarking and weighting is to limit the addition of new edges, so that new edges added will always be able to add matching numbers to the sub-graph, while at the same time make the weight and the maximum increase.
Two-figure multi-match: Set source point of direct network flow can, in this not to speak, will be in the network flow part of this article is reproduced, forgive me!
Algorithm explanation: Binary graph matching