Hungary with maximum matching Algorithm Template ..
From: http://www.cnblogs.com/Mu-Tou/archive/2011/08/11/2135405.html
To learn about Hungary algorithms, you must first understand the concepts of digraphs .. The general concepts summarized by O (∩ _ Yu) O miyu are given below. These concepts are very important and must be understood ..
The basic concept of a bipartite graph: (meaning that all vertices are divided into two sets X, Y. vertices in each set have no edge between each other)
An undirected graph G = <V, E>. If two sets of X and Y exist, make X ∪ y = V, X ∩ y = Phi, and each edge e = {X, y}
G is a bipartite graph (bipartite graph). It is usually used to represent a bipartite graph. If any X or Y in X is
If e = {x, y} has one side of E, G is called a completely bipartite graph (complete bipartite graph ).
The nature of the Bipartite Graph: (the concept of staggered track and augmented path is very important)
Theorem: it is necessary that an undirected graph G contains at least two vertices and the length of all its loops is even.
Match: Set G = to a bipartite graph. If M has E, and m does not have any public endpoints on both sides. M = Phi indicates that M is a null match.
Cover point: if M is a matching of a bipartite graph, the vertex associated with multiple edges in M is called a cover point, and the rest are uncovered points.
Staggered rail: If the edges of a path that belong to M alternate with those that do not belong to m, the path is called an staggered rail.
Augmented path: If path P is an interleaved path with both the starting and ending points, then path P is called the augmented path of M.
Maximum match: the matching with the largest number of edges in all matches of G is called the maximum match.
Property 1: the length of an augmented path about m must be odd, and the first and last edges on the road do not belong to M.
Property 2: For an augmented path P of M, delete the edge that belongs to P in M and add the edge that does not belong to m in P to M,
If the obtained edge set is m p, M Θ P has more matching edges than M.
Property 3: M is a maximum matching of G if and only if there is no augmented path about M.
Hall theorem: For a bipartite graph G = (X, Y, e), there is a matching M, so that all vertices of X are saturated with M.
The necessary and sufficient condition is: For any subset A of X, the vertex set adjacent to a is P (A), and the constant is: | P [a] | >=| A |
Among them, the adequacy proofs of Nature 2 and nature 3 and Hall theorem are the basis of the Hungary algorithm .....
Minimum vertex overwrite === maximum matching
Minimum path overwrite count of a DAG graph = number of nodes-Maximum number of matching nodes
Maximum number of independent sets in the bipartite graph = number of nodes-Maximum number of matching
Find the least vertex in the bipartite graph, and associate each edge with at least one vertex. This is
The "minimum vertex overwrite" of a bipartite graph ".
Minimum path coverage:
In a pxp directed graph, path overwrite is to find some paths in the graph to overwrite them.
Cover all vertices in the graph, and any vertex has and only one path associated with it ;(
If you move each path in these paths from its start point to its end point, you can
After each vertex in the graph once and only once); if there is a loop in the graph
Each path is a subset of weak connections.
We can conclude from the above:
1. A separate vertex is a path;
2. If there is a path P1, P2 ,...... PK, where P1 is the start point and PK is the end point.
In the overwrite graph, vertex P1, P2 ,...... PK no longer has a directed edge with other vertices.
The minimum path overwrite is to find the minimum number of paths to make it a path overwrite of P.
The relationship between path coverage and bipartite graphs (must be a directed graph without loops ):
Minimum path overwrite = | p |-Maximum number of matches;
The method for finding the maximum number of matches is to divide each vertex PI in P into two vertex PI 'and Pi ",
If P contains an edge from Pi to PJ, then in the Bipartite Graph P, there is a connection between PI 'and
The undirected edge of PJ. Here pi is the outbound edge of PI in P, and PJ is an inbound edge of PJ in P;
For the formula: Minimum path overwrite = | p |-Maximum number of matches, this can be understood;
If the number of matches is zero, there is no directed edge in P, so obviously there are:
Minimum path overwrite = | p |-Maximum number of matches = | p |-0 = | p |; that is, P
| P |;
The number of path overwrites when the p 'does not match the edge | p |;
If you add a matched edge PI '--> PJ "to P, overwrite the path of P.
There is an edge connecting PJ by PI, that is, Pi and PJ are in a path, so
The number of path overwrites can be reduced by one;
In this way, the matching edge is added. Each time a matching edge is added, the number of path overwrites is reduced
When the matching edge cannot be increased, the number of path overwrites cannot be reduced.
But it only shows that each matching edge corresponds to a path in path overwrite.
The directed edge of a link between two vertices. The following describes each vertex in a path overwrite.
The directed edge connecting two vertices corresponds to a matching edge;
Similar to the above, for each link in the path overwrite, each directed link between two vertices
Edge pi-> PJ, we can create an edge connecting PI 'and PJ in the matching graph. Obviously
The following figure shows a matching graph (this is easily proved by the reverse verification method.
If it is not a matching graph, the two sides PI '-PJ "and PI' -- Pk" must exist in this graph ",
(J! = K), then there are two sides in the path overwrite graph: pi-> PJ, Pi-> PK, from there
There are more than one path starting with Pi, which is in conflict with the path overwrite diagram.
Is there PI '-PJ ", PK'-PJ", this situation is similar to verifiable );
At this point, it shows the one-to-one correspondence between the matching edge and the path overwrite graph that connects the two vertices,
The preceding formula is true!
The theory of the Hungarian algorithm is: matching from the current
(
If no match exists
With
0)
Start, check every uncovered point, and then start from it to find the augmented path,
If you find the accessible path, you can expand it along this path until the available path does not exist.
Guang Lu.
You can find the method of Augmented-wide-path search based on the number of points that have never been built. It can be divided:
1) DFS
Augmented
2) BFS
Augmented
3)
Duozengguang Road
(Hopcroft-Karp
Algorithm
)
Then there is the Hungary template:
The template is provided in combination with HDU 1240 asteroids:
The question is to connect the given point, that is, to obtain the minimum point coverage.
# Include <iostream>
# Include <math. h>
Using namespace STD;
# Deprecision Max 502
Int map [Max] [Max];
Int N, K;
Int MK [Max];
// Starting from the vertex u in the X set, use the depth-first policy to find the augmented path.
// (This augmented path can only increase the current number of matches by 1)
Int NX, NY; // Number of vertices in the X and Y Sets
Int CX [Max], CY [Max];
// CX [I] indicates the y vertex matched with XI in the final maximum match. The same applies to cy [I ].
Int path (int u)
{
For (INT v = 1; v <= NY; V ++) // consider all Yi vertices v
{
If (Map [u] [v] &! MK [v])
{
MK [v] = 1;
// If V does not match, or if V already matches,
// However, starting from Y [v], you can find an augmented path.
If (CY [v] =-1 | path (CY [v])
{
CX [u] = V; // match V to u
CY [v] = u; // match u to V
Return 1; // find the augmented path
}
}
}
Return 0; // if there is no augmented path from u
}
Int maxmatch () // calculate the Hungarian algorithm for maximum matching of Two Graphs
{
Int res = 0;
Memset (CX, 0xff, sizeof (CX); // extended from 0 matching
Memset (CY, 0xff, sizeof (CY ));
For (INT I = 1; I <= NX; I ++)
{
If (CX [I] =-1) // search for augmented path from each uncovered point
{
Memset (MK, 0, sizeof (MK ));
Res + = path (I); // each time an augmented path is found, the matching number plus 1 is allowed.
}
}
Return res;
}
Int main ()
{
Int I, J;
Int A, B;
While (CIN> N> K)
{
Nx = N; ny = N;
Memset (MAP, 0, sizeof (MAP ));
For (I = 0; I <K; I ++)
{
Cin> A> B;
Map [a] [B] = 1;
}
Int max = maxmatch ();
Cout <max <Endl;
}
Return 0;
}
The BFS template is given below:
Int
PRED [maxn], MK [maxn], open [maxn];
Int
Maxmatch ()
{
Int
I, U, V, T, D, E, cur, tail, res (0 );
Memset (MK, 0xff,
Sizeof
(MK ));
Memset (CX, 0xff,
Sizeof
(CX ));
Memset (CY, 0xff,
Sizeof
(CY ));
For
(I = 0; I <NX; I ++)
{
PRED [I] =-1;
For
(Open [cur = tail = 0] = I; cur <= tail & CX [I] =-1; cur ++)
{
For
(U = open [cur], V = 0; v <ny & CX [I] =-1; V ++)
{
If
(G [u] [v] & MK [v]! = I)
{
MK [v] = I;
Open [++ tail] = CY [v];
If
(Open [tail]> = 0)
{Pred [open [tail] = u;
Continue
;}
For
(D = u, E = V; D! =-1;
T = Cx [d], CX [d] = E, CY [e] = D, E = T, D =
PRED [d]);
}
}
}
If
(CX [I]! =-1) RES ++;
}
// End of
Return
Res;
}