KM algorithm (best fit for binary graphs)

Source: Internet
Author: User

Online a bunch of people to write km algorithm, have not found out which speak better.

KM algorithm approximate process:

(1) Initializes an LX array to the one of the boy's most weighted edges. Initialize the LY array to 0.

(2) For each boy, find a Girl object with DFS, record the S and T sets on the way, and update the slack value for each girl. If the object cannot be found, go to 3.

(3) Finding the minimum slack value of girl for non-T sets is D, updating the girl of the boy and T concentration in the s set, and updating the slack value in the non-t set. For the boy who failed, the 2nd step continues.

The nutshell is to try to find the right side for each boy as much as possible, while maintaining the current right and highest condition. The process of looking for is the DFS process, marking S and T set is to ensure that the right and maximum, because as long as the boy in the other to find a female object, for the boy's off-the-list of the road ended.

DFS's tasks to complete:

(1) Mark S and T sets.

(2) Update the slack value of each girl to minimum.

The template is still required, with a full comment, and changed from the Kuangbin template.

  

1 /*km algorithm2 * Complexity O (nx*nx*ny)3 * Seek maximum weight matching (seemingly only perfect match?) )4 * If the minimum weight match is obtained, the inverse number of the weights can be obtained, and the opposite number will be taken.5 * Number of points starting from 16 * Templates are more intuitive for male and female models. 7 */8 intNX, NY;//points on both sides, X for male, y for female. 9 intG[n][n];//two-part diagram description, G[x][y]. Ten intGirl[n], Lx[n], ly[n];//Girl[i] record I match success object, male and female's top label One intSlack[n];//a relaxation value that is connected to the corresponding girl for optimization purposes.  A BOOLS[n], t[n];//the collection of nodes of the Hungarian tree, S is male and T is female.  -  - BOOLDFS (intX//x must be a man. the { -s[x]=true; -      for(intI=1; i<=ny; i++)//for each woman's -     { +         if(T[i])Continue; -         inttmp=lx[x]+ly[i]-G[x][i]; +         if(tmp==0 ) A         { att[i]=true; -             if(girl[i]==-1|| DFS (Girl[i]))//find another female object for the first girl -             { -Girl[i]=x;//record a matching boy -                 return true; -             } in         } -         Else if(slack[i]>tmp)//by the way update under Slack toslack[i]=tmp; +     } -     return false; the } *  $ intKM ()Panax Notoginseng { -Memset (Girl,-1,sizeof(Girl)); thememset (Ly,0,sizeof(Ly)); +  A      for(intI=1; i<=nx; i++)//initializes two l arrays of-inf and 0, respectively the     { +Lx[i] =-INF; -          for(intj=1; j<=ny; J + +) $             if(g[i][j]>Lx[i]) $lx[i]=G[i][j]; -     } -  the      for(intj=1; j<=nx; J + +)//for every man -     {Wuyi          for(intI=1; i<=ny; i++)//The initial slack is infinite. Slack only need to record women's.  theslack[i]=INF; -  Wu          while(true)//Infinite Loop until you find the object for it -         { Aboutmemset (S),0,sizeof(S)); $memset (T,0,sizeof(T)); -  -             if(DFS (j)) Break;//we'll find the object directly and get it done.  -  A             intD=INF; +              for(intI=1; i<=ny; i++)//based on the slack of women who are not on the Hungarian tree, find the minimum D the                 if(! T[i] && d>Slack[i]) -D=Slack[i]; $  the              for(intI=1; i<=nx; i++)//all the men on the Hungarian tree update the LX value the                 if(S[i]) thelx[i]-=D; the  -              for(intI=1; i<=ny; i++)//The woman on the tree plus D, the woman not on the tree slack minus D.  in             { the                 if(T[i]) Ly[i]+=d;//It's for the equation to still be true . the                 ElseSlack[i]-=d;//Why do you want to do this?  About             } the         } the     } the     intans=0; +      for(intI=1; i<=ny; i++)//The right and the cumulative matching edge -         if(girl[i]>0) theans+=G[girl[i]][i];Bayi     returnans; the}
km algorithm templates

KM algorithm (best fit 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.