Hungary algorithm
I came into contact with this algorithm because I saw a problem called prime significant other. That is to say, you are given a string of numbers, and you choose two numbers to add. If their sum is a prime number, then this pair is called a prime significant other. Then we need to find how many pairs of prime numbers significant other can be found at most in this string of numbers.
The solution to this problem is first to divide the number into two parts, one part is even and the other part is odd, because the addition of two even numbers or the addition of two odd numbers must still be even and cannot be prime.
So the problem becomes to choose one of the even numbers, then choose one of the odd numbers, and see how many pairs are added to the prime number at most. This problem uses the Hungary algorithm.
Hungary algorithm is mainly used to solve some problems related to bipartite graph matching, so let’s first understand bipartite graph.
Bipartite graph
Bipartite graph: Also known as bipartite graph, it is a special model in graph theory. Let G = (V, E) be an undirected graph. If vertices V can be divided into two disjoint subsets (A, B), and the two vertices i and j associated with each edge in the graph belong to these two different sets of vertices (i δ A, j δ B), then the graph G is said to be a bipartite graph.
In simple terms, if all vertices in the graph can be divided into two sets, and the heads and tails of all edges in the graph do not belong to the same set of vertices, but span both sets, then the graph is a bipartite graph.
Let’s take another look at this picture:
This graph doesn’t look like a bipartite graph at first glance, but if we translate its appearance, we will find that it is actually a bipartite graph
Hungary algorithm
Pre-concept
In graph theory, a matching is a set of edges where no two edges have common vertices.
** Maximum match **: The match with the largest number of matched edges among all matches of a graph is called the maximum match of this graph.
** Perfect match **: A graph is a perfect match if all vertices in a match are matching points. A perfect match must be the maximum match (any point of a perfect match has already matched, and adding a new matching edge will definitely conflict with an existing matching edge), but not every graph has a perfect match.
** Alternate Path **: Starting from an unmatched point, passing through non-matching edges, matching edges, and non-matching edges in turn… The path formed is called an alternate path.
** Augmenting Path **: Starting from an unmatched point, take an alternate path. If you pass through another unmatched point (the starting point does not count), this alternate path is called an augmenting path (agumenting path).
** Augmented path properties **:
- The path length of P must be odd, and neither the first nor the last edge belongs to M, because the two endpoints belong to two sets and do not match.
- P can obtain a larger match M 'after inversion operation.
- M is the maximum match of G if and only if there is no augmented path with respect to M.
Basic principle of algorithm
Hungary algorithm: The maximum matching algorithm that uses augmented paths to find bipartite graphs is called the Hungary algorithm. (Hungary mathematician Edmonds proposed it in 1965).
The basic idea: by finding an augmented path, the matching and non-matching edges in the augmented path are exchanged with each other, so that an additional matching edge will be created until the augmented path cannot be found.
One thing to be sure of here is that this process is to find matching and non-matching edges from existing edges, and cannot create new edges
Let’s take the last figure above as an example to explain the process of the entire algorithm:
- Starting from the vertex a, follow the alternate path. The first non-matching edge is, reaching the vertex e, and e is the non-matching point, forming an augmented path. Let it be a matching edge, and the vertices a and e are matching vertices.
- Starting from vertex b, the first non-matching edge is, reach vertex e, select the matching edge, reach a, select the non-matching edge, g is the non-matching point, find an augmented path, exchange the matching edge in the augmented path with The non-matching edge, that is, b-e, a-g becomes the matching edge, and a-e becomes the non-matching edge.
- Starting from the vertex c, the first non-matching edge is, reaches the vertex e, and then proceeds according to the alternate path, reaches the vertex b, cannot continue to move forward, and b is already a matching point, so no new grace path is found
- Starting from vertex c, select the second non-matching edge
- Starting from vertex d, select non-matching edge, reach vertex g, select matching edge, reach vertex a, select non-matching edge to reach vertex e, select matching edge, reach top b, there is no edge to choose, and no augmentation path is found
- Continue from vertex d, select the non-matching edge, find the augmented path, change the edge to the matching edge, and the algorithm ends.
Finally, the result we get is the following figure, and the red line in the following figure is the maximum match obtained by the algorithm:
Algorithm implementation
1 | //Numbering of vertices and edges starts from 0 |
1 | queue<int> Q; |
Reference article: https://www.cxyxiaowu.com/874.html