The Hungarian algorithm of binary graph

Source: Internet
Author: User

(⊙o⊙) ... —————————————————————————————————————————————————————————————————— the previous concepts are mostly used on-line sticky (ORZ)

One, Basic concepts

1. Two-minute chart

The dichotomy graph is a special model in graph theory. If the vertex V of the g= (v,e) can be divided into two sets of vertices with an empty intersection, and the two endpoints of any edge are divided into two sets, then the figure G is a two-minute graph.

2. Match

A match is a collection that contains several edges, and any of the two edges have no public endpoints. For example, the red edge of Figure 3 is a match for Figure 2.

About matching

① Maximum Match

In a sub-figure m of G, any two edges in M's edge set are not attached to the same vertex, then M is a match. Selecting the largest subset of such edges is called the maximum matching problem, and the maximum number of matches is called the maximum number of matches. If each vertex in a match is associated with an edge in the graph, it is called an exact match, also known as a complete match. If the Yuanhui point is added to the left and right sides, the graph G is equivalent to a network stream, and the maximum matching problem can be converted to the maximum flow problem. The essence of the Hungarian algorithm to solve this question is to find the augmented path of the maximum flow. The maximum matches in the red Edge are as follows:

② Optimal Matching

The optimal match, also known as the weighted maximum match, refers to a binary graph with a right-value edge, which makes a match to the weighted value on the matching edge and the maximum. The general x and Y sets have the same number of vertices, and the optimal match is a complete match, that is, each vertex is matched. If the number is not equal, the conversion can be achieved by adding 0 sides to the complement point. This problem is usually solved using the KM algorithm.

③ Minimum Coverage

The minimum coverage of a binary graph is divided into minimum vertex coverage and minimum path coverage:

① minimum vertex overlay refers to the minimum number of vertices so that each edge in the binary graph G is associated with at least one of the points, and the minimum vertex cover number of the binary graph is the maximum match number of the two fractal graph;

The ② minimum path overlay, also known as the minimum edge overlay, refers to overwriting all vertices in a binary diagram with as few disjoint simple paths as possible. The minimum path covering number =| of a binary graph The maximum matching number of v|-binary graphs;

④ Maximum Independent set

The maximum independent set is the search for a point set so that any two points in the graph have no corresponding edges. For general graphs, the maximum independent set is a NP-complete problem, and the maximum independent set =| for a binary graph The maximum number of matches for a v|-binary graph. If the black point is a maximum independent set:

Basic Concepts - Hungary algorithm

Alternate Path: from an unmatched point, followed by a non-matching edge, matching edge, non-matching edge ... The formed path is called alternating road. *

Augmented Road : From an unmatched point, take the alternate road, if the way another unmatched point (the point of departure does not count), then this alternate road is called the augmented path (agumenting path).

3. Maximum match and minimum point coverage

Minimum Point overlay : If you choose a point that is equivalent to covering all the edges with it as the endpoint, you need to select the fewest points to cover all the edges

The minimum cut theorem is an important theorem in a binary graph: the maximum number of matches in a binary graph is equal to the minimum number of points covered in the graph.

The minimum point set overrides = = Maximum Match . to explain the reason here, first of all, the minimum point set covers a certain >= maximum match, because assuming the maximum match is n, then we get n each other side, the light covering these edges will need to use n points. Now let's think about why the minimum click Overrides a certain <= maximum match. Any kind of n-point minimum click Overlay must be converted to a maximum of n matches. Because each point in the minimum point set overlay can find at least one edge in the point set (if it is not found, the other endpoint of all edges of that point is overwritten, the point is not necessarily overwritten, and it contradicts the minimum point set overlay), so long as each endpoint chooses one such edge, Must be converted into a matching scheme where the number of matches is equal to the point set coverage. So the maximum match is at least the minimum point set cover number, that is, the minimum click Overrides a certain <= maximum match. On the other, the two are equal.

4. Boring explanation (this is from the seniors blog)

By the nature of the augmented path, the matching edge in the augmented path is always one more than the unmatched edge, so if we discard a matching edge in an augmented path and select an unmatched edge as the matching edge, the number of matches will increase. The Hungarian algorithm is constantly looking for augmented paths, and if no augmented path is found, the maximum match is reached.

Give an example first
1. No match at start

2, check the first x point to find the first and connect


3, select the second point to find the second and the connection


4, found that the first side of the X3 x3y1 has been accounted for, to find out the x3 of the staggered path X3-y1-x1-y4, the staggered road has been matched on the side of the x1y1 from the match removed, the remaining side x3y1 X1y4 added to the match


5. Join x4,x5 in the same vein.

Hungarian algorithm can be limited depth or breadth first, just the example is depth first, that is, X3 find y1,y1 already have a match, then find staggered road. If breadth first, should be: X3 find y1,y1 have match, x3 find Y2.

Fun explanation

Did you see the above thing?? Well, let's see a lively and interesting explanation.

First of all we have n princes and n girls. Now you are the emperor, you want to give them the marriage, you certainly need to try to match these girls with the prince. We know that these princes have their own favorite girls, girls have their own dream of Prince Charming, (of course, as a myungeun, you will not abruptly these lovers apart. )

In the spirit of saving a life, the principle of building a seven-storey pagoda, you want to match as many couples as possible, the Hungarian algorithm working mode will teach you to do this:

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

A: first try to find a sister to 1th Prince, found that the first and he connected to the number 1th girls are still flowers, got it, even on a red line

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

two : then to the 2nd boys to find a sister, found the first and he connected to the 3rd girl is no master, got it

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

Three : Next is the number 3rd boys, it is very regrettable that the number 1th girls already have the Lord, how to do?

We tried to assign another sister to the boy who was a match for girl number 1th (aka Number 1th).

(yellow means the side is temporarily torn down)

The second girl connected to number 1th is girl number 2nd, but no. 2nd girl has no master, to lead the red line.

come on, we'll match Prince 3rd again (this is the time he's on the red Rope with girl number 1th)

come to Prince 4th again. So, the handsome prince's favorite girl was also led away ... What do we do? We're going to try to get a girl for number 2nd again. (Note this step is the same as above, this is a recursive process)

At this time found 2nd boys can still find the number 4th girl, then the problem solved, go back

Come and bring the red cord to Prince 4th.

Prince 2nd can find the number 4th Girl ~ ~ 1th Boys can find 2nd sister, ~ ~ ~ 3rd Boys can find 1th sister 4th, the Prince can find 3rd sister.

So the final result of the third step is:

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

Four : The next is Prince 5th, unfortunately, according to the rhythm of the third step we can not give the Prince 5th to free a princess, we are helpless ... (Then the boar, live together ....) )

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

This is the Hungarian algorithm of the process, in which looking for a sister is a recursive process, the most critical word is "teng" word

The principle is: there is a chance, no chance to create a chance to go on

Three, code rendering

#include <cstdio>#include<cstdlib>#include<cstring>#include<iostream>#include<algorithm>#defineN 1010using namespacestd;BOOLVis[n];intN1,n2,m,x,y,ans,girl[n],map[n][n];intRead () {intx=0, f=1;CharCh=GetChar ();  while(ch<'0'|| Ch>'9') {if(ch=='-') f=-1; Ch=GetChar ();}  while(ch<='9'&&ch>='0') {x=x*Ten+ch-'0'; ch=GetChar ();} returnx*F;}intFindintx) {     for(intI=1; i<=n2;i++)     if(map[x][i]&&!Vis[i]) {Vis[i]=true; if(!girl[i]| |find (Girl[i])) {Girl[i]=x; return 1; }     }     return 0;}intMain () {N1=read (), N2=read (), m=read ();  for(intI=1; i<=m;i++) x=read (), Y=read (), map[x][y]=1;  for(intI=1; i<=n1;i++) {memset (Vis,0,sizeof(VIS)); if(Find (i)) ans++; } printf ("%d", ans); return 0;}

The Hungarian algorithm of binary graph

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.