Reprint please attach the original link: http://blog.csdn.net/u013351484/article/details/51598270
Binary graph with weighted matching (also called binary graph Optimal (best) matching,kuhn-munkres algorithm )
Preliminary knowledge: Binary graph maximum matching, two-part graph complete (perfect) match
Binary graph with right matching, you can think of it as a set of x each vertex to the set Y each vertex has an edge of the two-part graph (set weights are positive, then the original set X and set Y without edge of the vertex xi,yi can add a weight of 0 of the edge, that is w[xi,yi] = 0); Such a dichotomy must exist In a complete match.
Next, all the contents of the maximum weight matching process for the binary graph will be expanded in the following example:
Equipped with two-point figure G:
It's best to draw this watch on paper:)
First, there is the concept of a top-mark :
The maximum weights of two graphs are matched by defining a value for each vertex of the set X and set Y, which is called the top label, and can be set to the top of the vertex XI in the set X Lx[i], and set Y as the same. During algorithm execution ensure lx[i] + ly[j] >= w[xi, YJ] is established (only this formula is established to prove the correctness of the KM algorithm).
Figure G at the beginning of the implementation of the algorithm to meet Lx[i] + ly[j] >= w[xi, YJ] The requirements can be set as follows: Lx[i] equals the maximum value of all vertices in the set Y, i.e. lx[i] = Max{w[xi, Yj]},j = {0,. . , 4},ly[i] is all 0; so the first figure could be this:
Then there is the concept of equal sub-graphs :
The side of the Lx[i] + ly[j] = W[xi,yj] In the dichotomy is called the equilateral , and the sub-graph consisting of the equilateral is called the equal sub-graph. If it is an equal sub-graph, removing any edges (at least one) is still an equal sub-graph, but adding any edge is not an equal sub-graph, because adding any edge will make the newly added edge Lx[i] + ly[j] > W[xi, YJ].
Then there will be:
If there is a complete match for the equal sub-graph of the binary graph, then this complete match is the maximum weighted value matching of the binary graph . Because for any one of the binary graphs, if it is contained in an equal sub-graph, then its Benquan and equals the top label of all vertices, and if it has an edge that is not included in the equal sub-graph, then its Benquan and is smaller than the top of all vertices and. So the complete match of the equal sub-graph must be the maximal right match of the binary graph. It's okay to read the whole process and come back and see if you can understand.
Therefore, the binary graph maximum weight matching process is in fact by modifying the set X and set Y of the top mark so that the two lx[i] + ly[j] = W[xi, Yi] The edge of increasing, that is, the equality of the sub-graph continues to expand, until the equivalent sub-graph complete matching, and this complete match is the request.
KM the algorithm flow is basically based on:
And then there will be: how to change the top label? When do you want to modify the top label? Go down with this question ~
With the above concept (though it may be blurry), we try to find the maximum weight matching of the binary graph G.
For ease of understanding, the solution process is based on the DFS sequence.
So, here we go:
1)
For X0 find staggered Road, X0 and Y1; This step is no problem.
2)
Find staggered road for X1, X1-Y1-X0; the end point is not in set Y, indicating that the current equality sub-graph cannot find a match for x0,x1 (currently traversing to them), and then naturally think of the need to add a new equilateral to the current equality sub-diagram to make the match possible.
Which edges can enter an equal sub-graph? These two conditions must be met:
1. At any time you must ensure that all vertices in the binary plot meet Lx[i] + ly[j] >= w[xi, YJ].
2. The newly added edge must be equal, because only the equilateral side belongs to the edge set of the equality sub-graph.
In fact, without modification of the top mark, condition 1 is definitely satisfied; If you don't consider the other, add the side directly, like this:
we joined the side [X1, Y2], and did make x0,x1 find their match (X0-y1;x1-Y2) but this side lx[1] + ly[2] = 5 > W[x1,y2] = 0; So it's not included in the equality sub-graph, which causes Solution error (km algorithm principle is to find a complete match in the equal sub-graph, if the match contains non-equal sub-image of the edge, natural error). Therefore, we must modify the lx[] or ly[] to make the appropriate edges into the equal sub-graph, so that the matching is possible, but also to meet the above two conditions.
Come up with some new concepts:
When a point XI in the set X fails in the equal sub-graph, all staggered paths starting from Xi can form a staggered tree (the Hungarian tree); We define all the elements in the staggered tree that belong to the X collection as the set S; all elements that belong to the Y collection are defined as set T. Therefore, all elements in set X that do not belong to set S are defined as set nots, and all elements in set Y that do not belong to the set T are defined as set Nott.
Back to X1 match failure, there is staggered road X1--Y1->x0; so S = {X0, x1},t = {Y1}. The way we modify the top mark is by subtracting a value d from the top of all vertices in S, and D as a positive number (never mind how it comes), and the top of all the vertices in T plus d. How does this make the original equality sub-graph change?
1, for the edge of the staggered tree (that is, Xi belongs to S,yi), the respective top (Lx[i]-D) + (Ly[j] + D) = Lx[i] + ly[j] has not changed, also is that these edges originally in the equal sub-diagram, modified the top label is still in the equal sub-diagram. Edge X0-y1 and Edge x1-y1.
2, for Xi belongs to the nots,yi belongs to the Nott side, their respective top mark has not changed,Lx[i] + ly[j] did not change, that is, these edges are not in the equal sub-diagram, modified the top label is still not in the equal sub-diagram, that is, these edges will not be added equal to the sub-graph.
3, for Xi belongs to Nots,yi belong to the edge of T, the respective top label Lx[i] + (Ly[j] + D) becomes larger, which shows that if these edges were originally in the equal sub-diagram, if the top label is modified, these edges will exit the equal sub-graph, than the edge of the Y1-X2 (because that's the way I understand it.) If these edges are not in the same sub-graph, then it is more impossible to add the equal sub-graph when the top label is modified.
4, for XI belongs to S,yi belongs to the side of Nott, this is not the same, this will have (Lx[i]-D) + ly[j], and modify the top mark before the Lx[i] + ly[j] > W[xi,yj]; that is, the top of each of these sides Bidding clubs decrease! We make the new top label lx[i] = Lx[i]-d,ly[j] = ly[j]; This means that each new top of these edges may cause lx[i] + ly[j] = W[xi,yj] to be established, which means that the sides of the equation can be added to the equal sub-graph! Make it possible to match successfully.
Next, how does D come? KM algorithm is calculated in this way:
d = min{lx[i] + ly[j]-w[xi,yj]}, where Xi belongs to S,yj belongs to Nott
So why? Why do you do this? Skip first, Count it again:
D = lx[0] + ly[0]-w[x0,y0] = 4 d = lx[1] + ly[0]-w[x1,y0] = 1
D = lx[0] + ly[2]-w[x0,y2] = 3 d = lx[1] + ly[2]-w[x1,y2] = 5
D = lx[0] + ly[3]-w[x0,y3] = 1 d = lx[1] + ly[3]-w[x1,y3] = 5
D = lx[0] + ly[4]-w[x0,y4] = 7 d = lx[1] + ly[4]-w[x1,y4] = 2
Well, the smallest is d = 1; the equal sub-graph after modifying the top label is:
You can find two more edges (X0-y3 and x1-y0) and one Less edge (x2-y1).
Many of the two edges are exactly the edges of the two formulas in the above 8 formulas that satisfy d = 1; Because in the process of modifying the top mark, for the Edge (X0-y3), (Lx[0]-D) + ly[3] D = lx[0] + ly[3]-w[x0,y3] substituting (lx[0]-D) + ly[3] = W[x0,y3 ], that is, the new top label satisfies lx[0] + ly[3] = W[x0,y3], so it is equal, naturally also the edge of the same sub-graph; (x1-y0) similar.
And the less edge is because after modifying the top lx[2] + ly[1] = 9 > W[x2,y1] = 8, it is not equal, so it is not a side of the equality sub-graph, it is removed from the equal sub-graph.
d = min{lx[i] + ly[j]-w[xi,yj]} , of which Xi belonged to S,yj belonged to Nott.
Look at this, for the above 8 formulas, if D does not take 1, but makes D > 1, such as D = 2
This causes (X1-Y4) to enter the equal sub-graph, (x2-y1) exits the equal sub-graph. It may seem that X1-Y4 is established, but in fact this is not satisfied: at any time must ensure that all vertices in the binary map meet Lx[i] + ly[j] >= w[xi, YJ] This condition, such as lx[0] + L Y[3] = 5 < W[x0,y3] = 6, which causes the KM algorithm error.
And if D < 1, such as D = 0.5, there will be no new edges to join the equal sub-graph, because for the only side that may join the equal sub-graph, that is , Xi belongs to the s,yi belongs to the Nott Edge will have (Lx[i]-D) + ly[j] > W[xi,yj] (because D = 1 o'clock is the equality of the equation), so it is impossible to have an equilateral result.
So there is d = min{lx[i] + ly[j]-w[xi,yj]}, where Xi belongs to S,yj belongs to Nott.
3)
After successfully finding a match for X1 (X1-Y0), and then finding the staggered path for X2, it also failed, its staggered tree only X2 vertex itself; S = {x2},t = {}.
D = lx[2] + ly[0]-w[x2,y0] = 6
D = lx[2] + ly[1]-w[x2,y1] = 1
D = lx[2] + ly[2]-w[x2,y2] = 1
D = lx[2] + ly[3]-w[x2,y3] = 5
D = lx[2] + ly[4]-w[x2,y4] = 3
Visible d = 1
(left) You can see that there are (X2-y1 and x2-y2) two edges joined equal sub-graphs, no edges exit equal sub-graphs; the top label that needs to be changed is only lx[2].
Thus there are staggered road X2, Y1, X0, and Y3, the Path property can be reversed to get (right) matching.
4)
Then for X3 find staggered Road, fortunately, a success:)
5)
Finally, for X4 to find staggered road, staggered tree X4-Y2-x3;s = {X4, x3},t = {Y2}.
D = lx[4] + ly[0]-w[x4,y0] = 8 d = lx[3] + ly[0]-w[x3,y0] = 3
D = lx[4] + ly[1]-w[x4,y1] = 3 D = lx[3] + ly[1]-w[x3,y1] = 3
D = lx[4] + ly[3]-w[x4,y3] = 1 d = lx[3] + ly[3]-w[x3,y3] = 1
D = lx[4] + ly[4]-W[X4,Y4] = 9 d = lx[3] + ly[4]-W[X3,Y4] = 4
D = 1
as you can see, there are (X4-y3 and x3-y3) two edges added equal to the sub-graph, and the Edge (x2-y2) exits the equal sub-graph.
Thus there are staggered road X4, Y3, X0->y1, x2,gg~, the end does not belong to the set Y;:), there's another one. X4, Y2, X3 Y3-X0 Y1 T X2, no two roads. Only follow the steps to do it again; notice that the staggered tree with the beginning of the X4 has two branches, to calculate the two paths, i.e. S = {X0, X2, X3, x4},t = {Y1, Y2, Y3};
D = lx[0] + ly[0]-w[x0,y0] = 3 D = lx[3] + ly[0]-w[x3,y0] = 2
D = lx[0] + ly[4]-W[X0,Y4] = 6 d = lx[3] + ly[4]-w[x3,y4] = 3
D = lx[2] + ly[0]-w[x2,y0] = 5 D = lx[4] + ly[0]-w[x4,y0] = 7
D = lx[2] + ly[4]-W[X2,Y4] = 2 D = lx[4] + ly[4]-w[x4,y4] = 8
D = 2
as you can see, there are (X2-y4 and x3-y0) two edges added equal to the sub-graph, and the Edge (X1, Y1) exits the equal sub-graph.
in the above (left) figure again for X4 find staggered road X4, Y2, X3, Y0 X1, not found? there is a X4---------Y2, X3, X0--Y1-X2 The inverse of the path attribute becomes the upper (right) graph. At this point, all the vertices in set X already have corresponding matching, that is, complete matching! That is, the maximum weight of this binary graph match!
X0-Y1
X1-Y0
X2-Y4
X3-Y3
X4-Y2
Maximum power value is 30
What about the minimum weight matching requirement? Very simple, before solving the ownership value of the opposite number, the results obtained after the opposite number of the results can be.
Then talk about complexity, the more authoritative argument is this (if you understand the above, the complexity is good to understand):
General Practice Time Complexity O (n^4)-Need to find O (n) times the augmentation path, each augmentation needs to modify the O (n) times the top label, each time the top of the index to enumerate the edge to find the D value, the complexity of O (n^2). In fact, the complexity of the kuhn-munkers algorithm can be achieved by O (N^3). We give each Y vertex a "slack" function slack, which is initialized to infinity each time we start looking for an augmented path. While looking for an augmented path, when checking for edges (XI,YJ), if it is not in the equal sub-graph, let slack[j] be the smaller value of the original value and Lx[i] + ly[j]-w[xi,yj]. This way, when you modify the top label, take all the minimum values in the slack value of the Y vertices that are not in the interleaved tree as the D value. However, it is important to note that all slack values are subtracted from D when the top label is modified.
Finally, attach a C code to solve this problem, which is the complexity of O (n^3) km algorithm
Similarly, the code can be easily understood if you understand what you've said above.
#include <stdio.h> #include <stdlib.h> #include <string.h> #include <math.h> #define N 110const int inf = (1<<30); int m[n][n];int lx[n];int ly[n];int vx[n];int vy[n];int slack[n];int link[n];int m_count = 5;int h _count = 5;int row, col;void Cal_dis () {int I, j;m[0][0] = 3;m[0][1] = 7;m[0][2] = 4;m[0][3] = 6;m[0][4] = 0;m[1][0] = 4;m[ 1][1] = 5;m[1][2] = 0;m[1][3] = 0;m[1][4] = 3;m[2][0] = 2;m[2][1] = 8;m[2][2] = 7;m[2][3] = 3;m[2][4] = 5;m[3][0] = 3;m[3] [1] = 4;m[3][2] = 6;m[3][3] = 5;m[3][4] = 2;m[4][0] = 1;m[4][1] = 7;m[4][2] = 9;m[4][3] = 8;m[4][4] = 0;/* Initialize the top label */for (i = 0; i < N; i++) Lx[i] =-inf;for (i = 0; i < M_count; i++) {for (j = 0; J < H_count; J + +) {if (M[i][j] > Lx[i]) {Lx[i] = M[i][j];}} memset (ly, 0, sizeof (ly)); int dfs (int u) {int v;vx[u] = 1; for (v = 0; v < h_count; v++) {if (!vy[v]) {int t = Lx[u] + ly[v]-m[u][v];if (!t) {Vy[v] = 1;if (Link[v] = = 1 | | DFS (link [v])) {Link[v] = u; return 1; }}elseif (T < Slack[v]) Slack[v] = t;} } return 0; } int () {int I, j;int ans = 0;memset (link,-1, sizeof); for (i = 0; i < M_count; i++) {while (1) {int d = inf;for (j = 0; J < H_count; J + +) {Slack[j] = inf;} memset (VX, 0, sizeof (VX)), memset (vy, 0, sizeof (VY)), if (Dfs (i)) break;for (j = 0; J < H_count; J + +) {if (!vy[j] && s LACK[J] < D) d = Slack[j];} for (j = 0; J < H_count; J + +) {if (Vx[j]) lx[j]-= D;} for (j = 0; J < H_count; J + +) {if (Vy[j]) ly[j] + = D;}}} for (i = 0; i < h_count; i++) ans + = M[link[i]][i];return ans;} int main () {int i;int ans;cal_dis (); ans = km ();p rintf ("ans:%d\n", ans); for (i = 0; i < 5; i++) {if (link[i]! =-1) {printf ( "X%d-y%d\n", link[i], i);}} return 0;}
Because the program uses DFS solution, the results of the visible program are consistent with our projections.
Experience:
KM algorithm I also understood the last year, but was busy brushing OJ, then the urgency of the mentality so that they did not do a good job to summarize. After a year, recently to push the box Solver ida* algorithm design a more accurate valuation function, it is obvious that the minimum weight of the two-figure matching can be berthelot off the application up. However, the KM algorithm almost all forget ~; the teeth in the Internet over 2 days of information, successful application in the program, has achieved a relatively satisfactory effect; Although there are some areas of the KM algorithm that do not understand, and may have the wrong place, there will be time to complete. Quickly record your own understanding of the process. In fact, there is such a sense of consciousness, but many times did not do so: the knowledge that they have just mastered, if they think that these knowledge is very useful to themselves, but also not easy to understand, it is better to understand their own process of recording down, convenient to consult later when needed, otherwise, even if once understood, Later need to re-online a bad chew (seriously, online copy paste party or a lot of, search for their own needs of things not easy) ~, so instead of more time, why not a little bit of painstaking efforts to strike the heat?
Although N is the number of boxes, the complexity of O (n^3) makes the solution a lot of the time used in the binary graph match above ~, because the KM algorithm's H () function and the DFS () function call too often, add up all 4,000,000+ times, this is just a very small level ~; if n^3 that 3 It will be very good to be reduced. At the same time, it was found that the GPROF command, which provides performance evaluation for the gcc/g++ compiler, and similar commands under Linux, is really useful.
Two-dimensional graph with weighted matching (thinking of pushing box problem)