Maximum matching of binary graphs: Hungarian algorithm

Source: Internet
Author: User

1. Two The matching problem 1.1 two-part diagram

Simply put, if the midpoint of the diagram can be divided into two groups, and all edges cross the boundary of the group, then this is a binary graph.
To be exact: divide the vertex of a graph into two disjoint sets U and V, so that each edge is connected to the vertices in U and V, respectively. If such a division exists, the graph is a two-part graph.
An equivalent definition of a binary graph is: A graph that does not contain "rings with odd numbers of edges". Figure 1 is a two-part diagram. In order to be clear, we will later draw it into the form of Figure 2.

1.2 Matches

In graph theory, a "match" (matching) is a set of edges in which any two edges have no public vertices. For example, the red edge in Figure 3, Figure 4, is the match of Figure 2.

We define matching points, matching edges, unmatched points, mismatched edges, and they are very obvious. Example 3, 1, 4, 5, 7 is the matching point, the other vertices are unmatched points, 1-5, 4-7 is the matching edge, the other edges are non-matching edges.

1.3 Maximum Match

A match of all matches in a graph with the largest number of matched edges, called the maximum match for this graph. Figure 4 is a maximum match that contains 4 matching edges.

1.4 Perfect Match

If a match in a graph, all vertices are match points, then it is a perfect match. Figure 4 is a perfect match.
Obviously, the perfect match must be the maximum match (any point of the perfect match has already been matched, adding a new matching edge will certainly conflict with the existing matching edge). But not every diagram has a perfect match.

For example: as shown, if there is a connecting edge between a pair of boys and girls, it means they like each other. Is it possible for all boys and girls to be paired 22 so that each pair likes each other? In graph theory, this is the perfect match problem. If it's a different story: how many boys/girls do you like to pair with each other? This is the maximum matching problem.

2. Hungarian algorithm

Alternate path: From an unmatched point, followed by a non-matching edge, matching edge, non-matching edge ... The path formed is called an alternate path.
Augmented path: From an unmatched point, take the alternate path, if the path to another unmatched point (the point of departure is not counted), then this alternate road is called the augmented path (agumenting path).
For example, one of the augmented path 6 in Figure 5 is shown (the matching points in the figure are marked in red):

2.1 Nature of the augmented path
    • There are odd strips of edges.
    • The starting point is in the left half of the dichotomy, ending at the right half.
    • The point on the path must be one on the left half and one in the right half, alternating.
    • There are no duplicate points on the entire path.
    • The starting and ending points are not currently paired, and all the other points are well-matched.
    • All the odd-number edges on the path are not in the original match, and all of the even-odd bars appear in the original match.
    • Finally, and most importantly, adding all the odd bars on the augmented path to the original match, and removing all the odd bars in the augmented path from the original match (this operation is called the inverse of the augmented path), the new match number is 1 higher than the original match number.
2.2 Algorithmic thinking

The core idea is to find the augmented path and improve the match. Simply swap the identities of the matching and non-matching edges in the augmented path.

We can add matching edges and matching points in the match by constantly looking for the augmented path. When the augmented path is not found, the maximum match is reached (this is the augmented path theorem).

2.2 Applications

Many problems can be transformed into a two-point graph matching model. There are several common variants of the binary diagram:
(1) Minimum vertex coverage of a binary graph
The minimum vertex overlay requires a minimum number of points (both in x or Y), so that each edge is associated with at least one of the points.
Knoig theorem: The minimum vertex cover number of a binary graph is equal to the maximum number of two-point graphs.

(2) Minimum path overlay for DAG graphs
Overwrite all vertices of the directed acyclic graph (DAG) G with as few disjoint simple paths as possible, which is the minimum path overlay problem for the DAG graph.
Conclusion: Minimum path coverage of dag graphs = number of nodes (n)-Maximum number of matches (m)

(3) The maximum independent set of the dichotomy graph
Maximum independent set problem: A M point is selected in Figure g of N points, so that there is no edge between the M points 22. Ask for M maximum value
Conclusion: Maximum number of independent sets of binary graphs = number of nodes (n)-Maximum number of matches (m)

The implementation code for the Hungarian algorithm is:

#include <iostream>#include <string.h>#include <stdio.h>using namespace STD;Const intN =2005;BOOLVis[n];intLink[n],head[n];intCnt,n;structEdge {intto;intNext  }; Edge Edge[n*n];voidInit () {cnt =0;memset(head,-1,sizeof(head)); }voidAddintUintV) {edge[cnt].to = v;      Edge[cnt].next = Head[u];  Head[u] = cnt++; }BOOLDfsintu) { for(intI=head[u];~i;i=edge[i].next) {intv = edge[i].to;if(!vis[v]) {Vis[v] =1;if(Link[v] = =-1|| DFS (Link[v])) {Link[v] = u;return true; }          }      }return false; }intMatch () {intAns =0;memset(link,-1,sizeof(link)); for(intI=0; i<n;i++) {memset(Vis,0,sizeof(VIS));if(Dfs (i)) ans++; }returnAns }intMain () { while(~scanf("%d", &n)) {Init (); for(intI=0; i<n;i++) {intU,v,k;scanf("%d: (%d)", &u,&k); while(k--) {scanf("%d", &v);                  Add (U,V);              Add (V,u); }          }printf("%d\n", Match () >>1); }return 0; }

Maximum matching of binary graphs: Hungarian algorithm

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.