Related concepts:
Binary graphs: the set of points in Figure G can be divided into two disjoint subsets, and the two points of each edge in G belong to these two subsets respectively .
Binary graph matching: The sub-figure m of the binary graph G has only one edge on each node, then M is a match .
Great match: Cannot add edges to a binary graph and match matching criteria
Maximum match: One of the most significant
Augmented path: If p is a path connecting two unmatched nodes on figure g, and the matched and mismatched edges appear alternately on p, then P is a augmented path relative to M
The Hungarian algorithm is used to find the maximal match of the binary graph.
Ideas:
The augmented path p is found in Figure G , and each edge on p is reversed (that is, the matched edge is changed to an unmatched edge, the unmatched edge is changed to a matched edge), and repeated until the augmentation path is not found .
Sample derivation:
Give a picture of a binary image
Starting from X1, search x1-y1, into the augmented road, connected X1-y1
[ATTENTION] from X2, search to X2-y1-x1-y3, into the augmented road
[ATTENTION] Cancel connection x1-y1, connection x1-y3,x2-y1
Starting from the X3, the search x3-y1-x2, does not pass, does not make the change
Search to X3-y2, into augmented road, connect X3-y2
Starting from the X4, the search x4-y3-x1-y1-x2, does not pass, does not make the change
Search to X4-y4, into augmented road, connect X4-y4
To find the great augmented road
adjacency table
1#include <cstdio>2#include <cstring>3 #defineMaxn4 intN,m,cnt,match[maxn],head[maxn],ans;5 BOOLCHECK[MAXN];6 structnode7 {8 intV,next;9}edge[maxn*5];Ten voidAddintXinty) One { Aedge[++cnt].next=Head[x]; -head[x]=CNT; -edge[cnt].v=y; the } - BOOLHungary (intu) - { - for(intI=head[u];i;i=edge[i].next) + { - intv=edge[i].v; + if(!check[v])//determine if V is in the augmented path A { atcheck[v]=true; - if(!match[v]| |Hungary (Match[v])) - { -Match[v]=u;//v corresponds to u - return true; - } in } - } to return false; + } - intMain () the { * //Omit input $ for(intI=1; i<=n;i++)Panax Notoginseng { - if(!Match[i]) the { +memset (check,false,sizeof(check)); A if(Hungary (i)) ans++; the } + } -printf"%d\n", ans); $ return 0; $}
Adjacency Matrix
1#include <cstdio>2#include <cstring>3 #defineMaxn4 intN,m,cnt,match[maxn],map[maxn][maxn],ans;5 BOOLCHECK[MAXN];6 BOOLHungary (intu)7 {8 for(intI=1; i<=n;i++)9 {Ten if(!check[i]&&Map[u][i]) One { Acheck[i]=true; - if(!match[i]| |Hungary (Match[i])) - { thematch[i]=u; - return true; - } - } + } - return false; + } A intMain () at { - //Omit input - for(intI=1; i<=n;i++) - { - if(!Match[i]) - { inmemset (check,false,sizeof(check)); - if(Hungary (i)) ans++; to } + } -printf"%d\n", ans); the return 0; *}
* Reference: https://www.byvoid.com/blog/hungary/
Http://baike.baidu.com/view/501081.htm
Http://baike.baidu.com/link?url=QbNL6DpNwvt1u3Yka7I-xy9Ymig1HNZid4j2MGsDxlyzCOXcdhjkG8sSJvBtK2zkZzOU007ooiSiujjwOpYCMq
Http://baike.baidu.com/link?url=YStVW2tIYiH6flGjr0VqIu44uM9Vhckq8pyKLpt651sGYpOFHsGI3TL9d2T5Tmy84QXmt__mVc0zerNUBfeecK
Graph theory-Two-point graph matching-Hungarian algorithm