Bipartite graph matching algorithms HK Hungary -

The bipartite graph and only if the ring is not present when the length of the odd figures

The above theorem bipartite graph coloring determination, complexity of O (N + M)

 1 void dfs(int x, int color) {
 2     v[x]=color;
 3     for(int i=head[x];i;i=nex[i]){
 4         int y=ver[i];
 5         if(!v[y]) {if(!dfs(y,3-color)) return false;}
 6         else if(v[y]==color) return false;
 7     }
 8     return true;
 9 }
10 
11 int main(){
12     bool flag=true;
13     for(int i=1;i<=n;i++) if(!v[i]) if(!dfs(i,1)) flag=false;
14 }

About bipartite graph matching algorithm, the most basic is the Hungarian algorithm, to find a point each time, to see whether he has augmented the road, matching the increase in complexity of O (N * M)

 1 bool dfs(int x){
 2     for(int i=head[x];i;i=nex[i]){
 3         int y=ver[i];
 4         if(!v[y]){
 5             v[y]=1;
 6             if(!m[y] || dfs(m[y])){
 7                 m[y]=x;
 8                 return true;
 9             }
10         }
11     }
12 }
13 for(int i=1;i<=n;i++){
14     memset(v,0,sizeof(v));
15     if(dfs(i)) ans++;
16 }

HK algorithm complexity is matched with each of the M match points, so that is no longer used N times, but only √N times to (N dots due match the worst case, each match a √N matching √N times, as each match is a match on the basis of the match, it can not exceed the last number of matches, only √N each with M * will √N complexity, if 1 * N M need only once , or N * 1 is a M, so the worst case M is the number of iterations is √N worst)

. 1  BOOL Find ( int X) {
 2      / * Find all the left to the right depth is +1 point, this is equivalent to looking for augmenting path, to see whether the augmented augmenting path * / 
. 3      for ( int I = 0 ; I <G [X] .size (); I ++ ) {
 . 4          int Y = G [X] [I];
 . 5          IF (VIS [Y] && Dy [Y] == DX [X] +! . 1 ) {
 . 6              VIS [Y] = to true ;
 . 7              IF {MX [X] = Y; My [Y] = X; (My [Y] || Find (My [Y])!) return  to true ;}
 . 8          }
 . 9      }
 10      return  false ;
 11 }
12 
13 int match(){
14     /* 初始化 */
15     memset(mx,0,sizeof(mx));
16     memset(my,0,sizeof(my));
17     int ans=0;
18     while(true){
19         /* 初始化 */
20         bool flag=false;
21         while(q.size()) q.pop();                      
22         memset(dx,0,sizeof(DX));
 23 is          Memset (Dy, 0 , the sizeof (Dy));
 24          / * all left unmatched points to queue * / 
25          Fore (I, . 1 , NX) IF ! ( MX [P [I] ]) q.push (P [I]);
 26 is          / * all non-matching point BFS, all tap layer number * / 
27          the while (q.size ()) {
 28              int X = q.front () ; q.pop ();
 29              for ( int I = 0 ; I <G [X] .size (); I ++ ) {
 30                  int Y = G [X] [I];
 31 is                  IF (! Dy [Y]) {
 32                     Dy [Y] = DX [X] + . 1 ;
 33 is                      / * all the matched points continue layer number to be matched * / 
34 is                      IF (My [Y]) DX [My [Y]] = Dy [Y] + . 1 , q.push (My [Y]);
 35                      the else In Flag = to true ;
 36                  }
 37 [              }
 38 is          }
 39          IF (In Flag!) BREAK ; // make sure there is a null point of a matching point 
40          Memset (VIS, 0 , the sizeof (VIS));
 41 is          Fore (I, . 1 , NX) IF ! (MX [P [I]] Find && (P [I])) ANS ++;
42      }
 43      return year;
44 }
1 vector<int>G[N];
2 int nx,ny;
3 int mx[N],my[N];
4 queue<int>q;
5 int dx[N],dy[N];
6 bool vis[N];

 

Guess you like

Origin www.cnblogs.com/rign/p/11116871.html