Two or three things about the Hungarian algorithm (novice to)

Source: Internet
Author: User

This is a simple understanding of the Hungarian algorithm, first of all the Hungarian algorithm name sounds like the Hungarian beef rice to create an appetite (?). 233.

Okay, so let's take a look at what the Hungarian algorithm is all about now.

So, to understand the fundamentals of the algorithm, let's look at a picture

In this picture, we can clearly see that the points on the graph are divided into two, one is a number, the other is a letter, and the numbers and numbers, letters and letters are not connected to each other side, this point is divided into two kinds, and each kind of no link between the graph is called a binary map, And our Hungarian algorithm is used to deal with binary graph matching.

What is called two-figure matching pinch, very simple, we choose some sides from this picture, for example, we elected 1--c,2--a,4--d these three edges, so that each side corresponds to the left and right sides of each point, and for each point, the edge is unique.

And for the Hungarian algorithm, it is to find the maximum matching of the binary graph, what is called the maximum matching pinch, that is, to select as many sides in the binary graph, so that they follow the rules of the binary map matching, if we choose the number of sides of the full graph divided by 2, it is called an exact match.

So how exactly does the Hungarian algorithm implement pinch? Specifically, as a token

First we find the left 1, starting from 1 Select 1 can be connected to the first side of the 1--c (red edge)

Specifically, we start the search from the first point on the left that is not connected to the edge (as shown in the original figure, 1), find the first point C that can be connected to, and then select Connect two points, recording the connection point C is 1.

Next find the second point 2, the same connection 2 and the first not connected to the 2 point (as shown in a), record the connection point A is 2.

Focus!!!!!

  Next is the essence of the Hungarian algorithm, because we began to connect from 3 when we started to panic, the only and 3 connected point A has been connected, then the next is to show the power of the Hungarian algorithm of the moment hahaha haha (honey juice laughter)

So what we're going to do is to use a depth-first search to find out which points we can connect to at the current mismatch point 3 (the Cyan side is the process path we're searching for), so the current one is a, because a is already connected, so we can directly find the connection A is 2, and then continue to deep search.

Then continue to search from 2, find a can connect with 2, and is not the current path (2--A) Another path (according to the Hungarian algorithm, if it is not found here to return to the previous layer, know that the search depth first search start node 3 of each edge, if you can not find the algorithm to determine the implementation of no better results), So obviously, we found the C point.

Similarly, we previously recorded the connection C node (that is, 1), then continue to start with 1 to search and 1 connection, and not 1--c this path of another path,

Obviously, find the path to 1--E and then judge the point E.

At this point we find that E is no other path to connect with other points, then we start to return, then look at this time, all the blue side of the graph is our search path, then we have to do in return: all {only Green and not red edge} into a new connection edge, And all {red and green side} cancel the connection.

Then the picture becomes

So for the current situation, we are equivalent to complete the local optimal matching, and then continue to follow this method to complete the full-image optimal match.

Then the complete picture is

So we're going to find out the maximum match for two-point graph

(PS write at the end, we are to record : {After each connecting edge, successfully connected to the right of a point on the left side of the position}, and if we do find the ring can only go back to the previous layer, so to record { which points in the current search used to}, So the next time the search will not die Loop)

Attach a water question, codevs1022 http://codevs.cn/problem/1022/

So in this problem, we dye all the squares in the chess chessboard, so that all the dots that are adjacent to the black are white, the dots that are adjacent to the white are black, and then all the squares that are not flooded are divided into black and white to make a binary chart, Then the path of the beginning of each lattice is the path that is not out of bounds and is not flooded with lattice connections, and then simply do the Hungarian algorithm is good.

Then post the code (PS Honey juice variable name harmonious modification)

1#include <stdio.h>2 intxx[4]={0,1,0,-1},yy[4]={1,0,-1,0};3 BOOLusd[ the][ the],map[ the][ the],wat[ the][ the];4 intN,m,k, from[ the][ the][3],ans;5 BOOLFindintXinty)6 {7     intK;8      for(k=0;k<4; k++)9     {Ten         intxi=x+xx[k],yi=y+Yy[k]; One         if(xi<1|| xi>n| | yi<1|| YI&GT;M)Continue; A         if(!map[xi][yi]&&!usd[xi][yi]&&!Wat[xi][yi]) -         { -usd[xi][yi]=true; the             if(! from[XI] [Yi] [0]|| Find from[XI] [Yi] [0], from[XI] [Yi] [1])) -             { -                  from[XI] [Yi] [0]=x; -                  from[XI] [Yi] [1]=y; +                 return true; -             } +         } A     } at     return false; - } - intMain () - { -     intb; -scanf"%d%d%d",&n,&m,&k); in      for(intI=1; i<=k;i++) -     { toscanf"%d%d",&a,&b); +wat[a][b]=true; -     } the      for(intI=1; i<=n;i++) *          for(intj=1; j<=m;j++) $         {Panax Notoginseng             if(i%2==0&&j%2==0) map[i][j]=true; -             if(i%2&&j%2) map[i][j]=true; the         } +      for(intI=1; i<=n;i++) A     { the          for(intj=1; j<=m;j++) +         { -             if(!wat[i][j]&&Map[i][j]) $             { $                  for(intIi=1; ii<=n;ii++) -                      for(intjj=1; jj<=m;jj++) -usd[ii][jj]=false; the                 if(Find (i,j)) -                 {Wuyians++; the                 } -             } Wu         } -     } Aboutprintf"%d", ans); $     return 0; -}

Two or three things about the Hungarian algorithm (novice to)

Related Article

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.