Finding the maximal matching--hopcroft-krap algorithm of binary graph

Source: Internet
Author: User

This article is a detailed comment on the implementation of the HOPCROFT-KRAP algorithm code in this article on the second-Division chart.

Fundamentals of the HK algorithm

The HOPCROFT-KARP algorithm uses BFS to find multiple augmented paths, and then uses the DFS traversal augmentation (cumulative number of matches to modify the set of matching points) to loop until there is no augmentation path.
The BFS traversal of the HOPCROFT-KARP algorithm only hierarchies the points (not marking matches and unmatched points) and then uses DFS traversal to see which of the above hierarchies is the augmented path (the last point is not matched).
The BFS process can be seen as an image tree structure traversing down the hierarchy, as well as to prevent intersecting augmented paths from occurring.

Two-part chart the popular explanation of the steps of HK algorithm is given in the lecture hall.

Set U and V to be a binary graph of Figure G, M is a match from u to V
(1) Using BFS traversal to layer the points of the graph, from X to find an unmatched point V, (all V) composed of the first layer, the next layer is formed--is to find the matching point (the augmented path nature), until the match found in V to terminate the search, Find augmented paths for other unmatched points of X (BFS only hierarchies do not mark if match points)
(2) Using DFS Traversal lookup (1) to form the augmented path, find the match number on the cumulative 1
(3) Repeat (1) (2) operation until the augmented path is not found

The HOPCROFT-KARP algorithm of the maximal matching of the binary graph gives the more academic steps, which is interesting to look at

Code annotations are as follows

#include <bits/stdc++.h>using namespace Std;const int maxn = 1e5 + 10;const int INF = 0x3f3f3f3f;bool Flag;int p, N;  Two sets: X and y//mx,my the matching vertices of the record node int MX[MAXN], MY[MAXN], Nx, ny;//dx,dy are nodes of the BFS traversal hierarchy int DX[MAXN], DY[MAXN], Dis;bool VST[MAXN],    G[110][310];bool SEARCHP () {queue<int> Q;    dis = INF;    memset (DX, 0x3f, sizeof DX);    memset (dy, 0x3f, sizeof dy); Use BFS traversal to layer the points of the graph, find an unmatched point from X V//(all V) to form the first layer, and the next layer is formed--each find//Match point (augmented path nature) until an unmatched point is found in Y to stop the lookup,//to find the same x other unmatched points        Augmented path (BFS only layering not mark//whether match point)//Find out all unmatched points in X make up the first layer of the BFS for (int i = 1; I <= Nx; ++i) {if (mx[i] = = 1)            {Q.push (i);        Dx[i] = 0; }} while (!        Q.empty ()) {int u = q.front ();        Q.pop (); The path length is greater than dis, waiting for the next BFS expansion/DIS to be the length of the y set, so the dis must be an odd number//and each call to SEARCHP will only traverse one layer x set node if (Dx[u] > dis) b        Reak;          for (int v = 1; v <= Ny; ++v) {//(u,v) with edges and V not layered//assigned v levels  if (G[u][v] && dy[v] = =-1) {Dy[v] = Dx[u] + 1;            }//V is an unmatched point, stop extending (Find)//Get the maximum traversal level of this BFS if (my[v] = =-1) dis = dy[v];                V is the match point, continue to extend else {dx[my[v]] = Dy[v] + 1;            Q.push (My[v]); }}}//If DIS is inf description y does not have an unmatched point, that is, there is no augmented path for return dis! = INF;} Find the augmented path formed by the BFS with DFS traversal, stop traversing if the augmented path can be found and return truebool DFS (int u) {for (int v = 1; v <= Ny; ++v) {if (!vst[v] &am            p;& G[u][v] && dy[v] = = Dx[u] + 1) {vst[v] = 1; The level (that is, the length of the augmented path) is greater than the dis//of this lookup is the case of a break in the SEARCHP, that is, not sure whether it is an augmented path//Only wait to call SEARCHP again to determine if (            MY[V]! =-1 && dy[v] = = dis) continue; is an augmented path, update match set if (my[v] = = 1 | |                DFS (My[v])) {My[v] = u;                My[u] = v;            return true; }}} return false;} Find a binary chartMaximum match int maxmatch () {int res = 0;    The matching vertices of each node are placed in empty memset (MX, 0x3f, sizeof MX);    Memset (My, 0x3f, sizeof my);        If BFS can find the augmented path while (SEARCHP ()) {memset (VST, 0, sizeof VST);            for (int i = 1; I <= Nx; ++i) {//Use DFS to find the augmented path, the augmented path must never match the start//If you find an augmented path, match number plus one        if (mx[i] = =-1 && DFS (i)) ++res; }} return res;}

Finding the maximum matching--hopcroft-krap algorithm for binary graphs

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.