Using Hungarian algorithm to solve the maximum matching problem of binary graphs

Source: Internet
Author: User

In fact, when writing this code, I was wondering, x sets and y sets of points, can be used at the same time 1, or both?

And then I tried to convince myself: it's already a two-part picture.

It just saved A →

Okay, I was convinced by myself.

The binary graph match says that each person has a number of options, but each choice can only hold one person, ask you how many pairs of

Or you can't go through the same point when you pick a side.

Maximum match is the question of how many edges to select

The Hungarian algorithm is that, there is opportunity on, no opportunity to create opportunities to go, as far as possible to the current free space, the process of freeing is a recursive process

In fact, this algorithm is very sentimental.

BOOLFindintu) {     for(inttmp=g[u];tmp;tmp=e[tmp].next)if(!y[e[tmp].t]) {y[e[tmp].t]=1; if(lk[e[tmp].t]==0||find (lk[e[tmp].t])) {lk[e[tmp].t]=u; return 1; }        }        return 0;}

After the map is built, for each X point, clear the Y array and find is good.

And then forget to say the definition, fill it up.

int N,m,cnt,ans; int Y[MAXN],LK[MAXN],G[MAXN];

Y records whether the subscript node in Y has been accessed

LK records the nodes in X that are connected to the current subscript node (y)

Then give the complete implementation:

1#include <cstdio>2#include <cstring>3 Const intmaxn=205;4 Const intmaxm=205;5 intN,m,cnt,ans;6 intY[MAXN],LK[MAXN],G[MAXN];7 structedge{intT,next;} e[maxn*MAXM];8 voidAddedge (intUintv)9 {Tene[++cnt].t=v;e[cnt].next=G[u]; Oneg[u]=CNT; A } - BOOLFindintu) - { the      for(inttmp=g[u];tmp;tmp=e[tmp].next) -         if(!y[e[tmp].t]) -         { -y[e[tmp].t]=1; +             if(lk[e[tmp].t]==0||find (lk[e[tmp].t])) -             { +lk[e[tmp].t]=u; A                 return 1; at             } -         } -         return 0; - } - intMain () - { inscanf"%d%d",&n,&m); -     intTmp,tmp1; to      for(intI=1; i<=n;i++) +     { -scanf"%d",&tmp); the          for(intj=1; j<=tmp;j++) *         { $scanf"%d",&tmp1);Panax Notoginseng Addedge (I,TMP1); -         } the     } +      for(intI=1; i<=n;i++) A     { thememset (Y,0,sizeof(y)); +         if(Find (i)) ans++; -     } $printf"%d", ans); $     return 0; -}

To be honest, this algorithm is really magical.

Using Hungarian algorithm to solve the maximum matching problem of 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.