[Hungarian Algorithm] Bipartite Graph Hungarian Algorithm (Bipartite Graph)

Through the efforts of several generations, you have finally caught up with the tide of leftover men and leftover women. Suppose you are a glorious new century matchmaker. You have N leftover men and M leftover women in your hands. Everyone may be right. The opposite sex has a favorable impression ( surprise-_-|| does not consider special sexual orientation for the time being), if a man and a woman have a favorable impression of each other, then you can match the pair together, now let’s ignore all unrequited love (OK Sad feeling Almost crying), you probably have a relationship diagram like the one below, where each connection indicates mutual affection.

 

Based on the principle of saving a life, it is better to build a seven-level pagoda. You want to match as many couples as possible. The working mode of the Hungarian algorithm will teach you to do this:

===============================================================================

1: First try to find a girl for the No. 1 boy, and find that the first No. 1 girl connected to him is still named Hua Wuzhu, got it, and connected a blue line

 

===============================================================================

2: Then find a girl for the No. 2 boy, and find that the first No. 2 girl connected to him has no name, got it

 

===============================================================================

Three: Next is the boy No. 3. It is a pity that the girl No. 1 already has the master. What should I do?

We try to assign another girl to the boy (that is, the boy number 1) who matched the previous girl number 1.

 

(Yellow means this side is temporarily removed)

 

The second girl connected with the boy number 1 is the girl number 2, but the girl number 2 also has the master, what should I do? Let's try to find a new girl for the original partner ( get angryget angry) of the No. 2 girl (note that this step is the same as the above, this is a recursive process)

 

 

At this point, it is found that the No. 2 boy can still find the No. 3 girl, then the previous problem is solved, and backtracking

 

No. 2 boy can find No. 3 girl~~~ No. 1 boy can find No. 2 girl~~~ No. 3 boy can find No. 1 girl

So the final result of the third step is:

 

===============================================================================

Four: Next is Boy No. 4. Unfortunately, according to the rhythm of the third step, we can't make room for a girl for Boy No. 4. We really can't do anything... Xiangji Shi, go well.

 

===============================================================================

This is the process of the Hungarian algorithm, in which finding a girl is a recursive process, and the most critical word is the word "Teng"

The principle is probably: there is a chance, and there is no chance to create an opportunity.

 

Basic Concepts - Bipartite Graph

Bipartite graph : is a special model in graph theory. If the vertex V of the undirected graph G=(V, E) can be divided into two vertex sets whose intersection is empty, and the two endpoints of any edge belong to the two sets, then the graph G is called a bipartite graph .

#define maxn 10 // represents the maximum number of vertices in the x set and the y set! 
 int nx,ny; // the number of vertices in x set and y set 
 int edge[maxn][maxn]; // edge[i][j] is 1 means ij can match 
 int cx[maxn],cy[maxn ]; // Used to record which y element matches in the x set! 
 int visited[maxn]; // Used to record whether the vertex has been visited! 
 int path( int u)
 {
     int v;
     for(v=0;v<ny;v++)
     {
         if(edge[u][v]&&!visited[v])
         {
             visited[v] = 1 ;
             if (cy[v]==- 1 ||path(cy[v])) // If the v element in the y set does not match or v has been matched, but from cy[v] An augmentation path can be found in 
             {
                 cx [u] = v;
                 cy[v]=u;
                 return 1;
             }
         }
     }
     return 0;
 }
 int maxmatch()
 {
     int res=0;
     memset(cx, 0xff , sizeof (cx)); // The initial value of -1 means that there are no matching elements in both sets! 
     memset(cy, 0xff , sizeof (cy));
      for ( int i= 0 ;i<=nx;i++ )
     {
         if(cx[i]==-1)
         {
             memset(visited,0,sizeof(visitited));
             res+=path(i);
         }
     }
     return res;
 }

Reference blog post:

Hungarian Algorithm (Bipartite Graph) 

Fun Write Algorithm Series--Hungarian Algorithm

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325897476&siteId=291194637