Definition: in an no-map, define an edge-covered point for the two endpoints of the Edge. Find a side set S contains the most edges, so that each vertex in all vertices covered by this edge set is overwritten by only one edge. The size of S is called the maximum match of the Graph.
The maximal matching algorithm of the binary Graph: set the left set as a set, with the edge set as the B set. Two methods are commonly used for the maximum matching of binary graphs.
(1) The first method is called the Hungarian Algorithm. This method enumerates each point in sequence a, attempting to find a match in the B Collection. For a point x in the set, suppose that there is a point y in the B collection, and if Y does not yet have a matching point, then x can match y and find; otherwise, set Y to match the point Z (obviously z is a point in a set), then we will try to find a match for z except y, if found, Then x can match y, otherwise x cannot match Y.
We consider an example to illustrate the Hungarian matching Algorithm.
Step1: starting from 1, find the right side of point 4, found that point 4 is not matched, so found 1 of the match point is 4. Get such as:
Step2: next, starting with 2, try to find a match point on the right. We enumerated 5 and found that 5 had not been matched, so we found 2 of the matching points, 5. get as Follows:
Step3: next, we're Looking for a 3 match Point. We enumerated 5 and found that 5 already has a matching point of 2. At this point, we tried to find 2 except 5 for another match, we found that we can find 7, so 2 can match 7, so 5 can match to 3, get as Follows:
At this point, the end, we get the maximum match for 3.
(2) the second method is called the Hopcroft-karp algorithm. The algorithm is roughly the same as the first method. The difference is that this method finds a set of disjoint augmented paths each Time. We use the following example to Illustrate.
Step1: we find the augmented path from all points where we have not found the augmented path, that is, 1,2,3,4, we found the 1->2,3->3 two (the red line on the left represents the augmented path), and then we seek the matching point along these augmented paths, and we get the graph on the right, that is, 1 matches 2,3 match 3.
Maximum matching algorithm for general graphs: we start with a non-matching node s and use BFS to generate a search Tree. Whenever you find a node u, if you have not yet been matched, then you can do a successful augmentation, that is, s match u; otherwise, we will connect the node U with its spouse V to the tree, then throw v into the queue to continue the Search. We give each point on the search tree a type: s or T. When u throws its spouse V into the queue, we mark u as T-type and V as S. so, The search tree looks like this:
otherwise, we find a ring with an odd length, as shown in
A "pinch" operation is necessary! The so-called pinch operation is to shrink the ring to a point. This graph is then turned into 5 points (a large point, or a flower, add the original 4 points): after the completion of the indentation, but also the original ring inside the T-type point all into the s-type points, such as
It is possible to shrink to a point because a ring with an odd length (for example, s-b-d-j-f-c-a-s), If we can find a degree in any of its points, then the other points in the ring can be paired exactly, which means that the degree of each point is Equivalent. This is the thought source of the shrinking Point.
Non-graph Maximum Matching implementation:
1 #defineMAXN 2502 3 classGraphmaxmatch4 {5 Private:6 intque[maxn],quehead,quetail;7 BOOLg[maxn][maxn];8 BOOLinque[maxn],inblossom[maxn];9 intmatch[maxn],pre[maxn],s[maxn];Ten intn; one a voidAddqueele (intU) - { - if(inque[u])return; theinque[u]=1; -que[quetail++]=u; - if(quetail==maxn) quetail=0; - } + intPopqueele () - { + intu=que[quehead++]; a if(quehead==maxn) quehead=0; at returnu; - } - - intFindAncestorintUintV) - { - intvisit[maxn]; inMemset (visit,0,sizeof(visit)); - while(1) to { +u=s[u]; -visit[u]=1; the if(match[u]==-1) break; *u=pre[match[u]]; $ }Panax Notoginseng while(1) - { thev=s[v]; + if(visit[v]) break; av=pre[match[v]]; the } + returnv; - } $ voidResetintUintRoot) $ { - intv; - while(u!=Root) the { -v=match[u];Wuyiinblossom[s[u]]=1; theinblossom[s[v]]=1; -v=pre[v]; wu if(s[v]!=root) pre[v]=match[u]; -u=v; about } $ } - - voidContract (intUintV) - { a introot=FindAncestor (u,v); +Memset (inblossom,0,sizeof(inblossom)); the Reset (u,root); Reset (v,root); - if(s[u]!=root) pre[u]=v; $ if(s[v]!=root) pre[v]=u; the for(intI=1; I<=n;i++)if(inblossom[s[i]]) the { thes[i]=root; the Addqueele (i); - } in } the the BOOLBFS (intstart) about { the for(intI=1; I<=n;i++) pre[i]=-1, inque[i]=0, s[i]=i; theQuehead=quetail=0; the Addqueele (start); + while(quehead!=Quetail) - { the intu=Popqueele ();Bayi the for(intv=1; V<=n;v++)if(g[u][v]&&s[v]!=s[u]&&match[u]!=V) the { - if(v==start| | match[v]!=-1&&pre[match[v]]!=-1) - { the Contract (u,v); the } the Else if(pre[v]==-1) the { -pre[v]=u; the if(match[v]!=-1) Addqueele (match[v]); the Else the {94u=v; the while(u!=-1) the { thev=pre[u];98 inttmp=match[v]; aboutmatch[u]=v; -match[v]=u;101u=tmp;102 }103 return true;104 } the }106 }107 }108 return false;109 } the public:111 /** the Vertexnum:vertex number113 G[1~vertexnum][1~vertexnum]: Edge Relation the */ thevector<pair<int,int> >Calmaxmatch ( the intvertexnum,Const BOOLG[maxn][maxn])117 {118n=vertexnum;119 for(intI=1; i<=n;i++) - {121 for(intj=1; J<=n;j++) g[i][j]=g[i][j];122 }123Memset (match,-1,sizeof(match));124 for(intI=1; I<=n;i++)if(match[i]==-1) BFS (i); the 126vector<pair<int,int> >ans;127 for(intI=1; i<=n;i++) - {129 if(match[i]!=-1&&match[i]>I) the {131 ans.push_back (make_pair (i,match[i)); the }133 }134 returnans;135 }136};
Maximum matching algorithm for graphs