Definition of bipartite graph:
A graph in which nodes are composed of two sets and there are no edges inside the two sets.
When applied,
First, we investigate whether there is a scheme to divide the nodes into two sets that meet the above properties; Then consider the properties of the bipartite graph and how to obtain the properties.
Simple nature:
Two points connected by any edge belong to two different sets. (available by definition)
There are no odd rings in bipartite graphs. (sufficient and necessary conditions)
Judgment conditions:
dfs traversal to determine whether there is an odd ring;
It is proved that odd rings do not exist<=>The graph is a bipartite graph <=Suppose that odd rings exist Let a point in an odd ring belong to a set a Not a collection b From the simple property 1, it can be seen that after making the point circle for one week, the point belongs to b Not belong to a Contradiction, assumption error Evidence of necessity =>It can be easily proved that when there are multiple connected blocks in a graph, we only need to consider the case of each connected block. For any two points in a connection block, there are two paths with different parity. It can be proved that all the different edges in the two paths can form one or more paths n Two rings. (otherwise, the ends and starting points of the two roads must be different) Because the odd ring does not exist, the sum of the edges of the ring is even. Then the parity of the two paths is the same and contradictory. Then we can get that the parity of any path between two points is the same. Take a point in the connected block as the starting point, traverse the whole graph, mark the distance from each point to the starting point, and take the parity of the distance as the division basis of the set of bipartite graphs. It can be concluded from the above proof that two points on any edge belong to different sets. The construction of bipartite graph is completed. Evidence of adequacy
It is not difficult to see from the proof process that the method of judging bipartite graph can also be used to construct a classification scheme of point set
Application:
Maximum matching of bipartite graph:
Match:
In graph theory, a "matching" is a set of edges in which any two edges have no common vertices.
Maximum match:
In all matches of a graph, the match with the largest number of matching edges is called the maximum match of the graph. Usually, for a graph, its maximum matching is not unique.
Alternate path:
The path formed by starting from an unmatched point and passing through unmatched edge, matched edge and unmatched edge in turn is called alternating path.
Zengguang Road:
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.
In particular, an edge connecting two unmatched points can also be called an augmented path.
Hungarian algorithm:
When investigating Zengguang Road, it is found that it has an important property. The beginning and end are unmatched points, and the number of unmatched edges is more than that of matched edges.
At this time, if we change all matching edges into unmatched edges and unmatched edges into matching edges in Zengguang Road, then the number of matching edges + 1 and the number of matching points + 2.
Augmented path theorem:
The necessary and sufficient condition that a match is a maximum match is that there is no augmented path, which is applicable to any graph.
The following is the necessary and sufficient proof of the theorem:
necessity: If a match is a maximum match and there is an augmented path, Then the augmented path property can be used to increase the matching number, so it does not exist. Proof of proposition adequacy: It is known that there is no augmented path between any two unmatched points,
Reuse Network flow proof , (network flow Pit 1 / 1)
So we only need to find the augmented path for each point of group a or group b, and we will complete the construction of maximum matching.
This is the core of the Hungarian algorithm. The rest is the optimization of some code implementation. bfs and dfs are used to realize search in the mainstream.
[dfs]
cnt represents the maximum number of matches, and key is the number of common points that record all the maximum matches
#include<cmath> #include<cstdio> #include<cstring> #include<iostream> #include<algorithm> using namespace std; const int maxn=1000; struct node{int x,y;}; int n,m,k; bool g[maxn][maxn]; int a[maxn],b[maxn]; bool find(int x){ for(int i=1;i<=n;i++){ if(g[x][i]&&!b[i]){ b[i]=1; if(!a[i]||find(a[i])){ a[i]=x; return 1; } } } return 0; } int main(){ // ios::sync_with_stdio(false); int T=1; while(cin>>n>>m>>k){ memset(g,0,sizeof(g)); memset(a,0,sizeof(a)); for(int i=1;i<=k;i++){ int x,y;cin>>x>>y; g[x][y]=1; } int cnt=0,key=0; for(int i=1;i<=n;i++){ memset(b,0,sizeof(b)); if(find(i))cnt++; } for(int i=1;i<=n;i++) for(int j=1;j<=m;j++) if(g[i][j]){ g[i][j]=0; int sum=0; memset(a,0,sizeof(a)); for(int l=1;l<=n;l++){ memset(b,0,sizeof(b)); if(find(l))sum++; } if(sum<cnt)key++; g[i][j]=1; } printf("Board %d have %d important blanks for %d chessmen.\n",T++,key,cnt); } return 0; }
[bfs]
The code is more complex than dfs. It is about twice as fast as dfs on sparse graph. I'll fill in the code of bfs when I have time...
Extension theorem:
Minimum point coverage: select the least points so that at least one endpoint of any edge is selected
maximum matching number = minimum point coverage number (this is Konig theorem)
Maximum number of independent points: select the most points so that any selected two points are not connected
maximum matching number = maximum independent number
Minimum path coverage: for a DAG (directed acyclic graph), select the least paths so that each vertex belongs to and only belongs to one path. The path length can be 0 (i.e. a single point).
minimum path coverage = number of vertices - maximum number of matches
Network flow (another algorithm)
Fill the pit again later 23333
Maximum weight matching of weighted bipartite graphs
Maximum matching of general graphs
Maximum weight matching of general graphs
. -. -..