"The introduction to the algorithm", "graph": the maximum matching of the binary graph without weighted (Hungarian algorithm)

Source: Internet
Author: User

The "Best match, Perfect match, and Hungarian algorithm" of the "binary graph" is a special image that is particularly easy to understand for several concepts related to the dichotomy. This article is part of a major excerpt from this blog post.

There are other posts to consider:

A series of interesting writing algorithms--Hungarian algorithm

Hungarian algorithm for binary graph matching

1. Preface

two-part diagram: In simple terms, if the midpoint of the diagram can be divided into two groups, and all edges cross the boundary of the group, then this is a two-part graph. To be exact: dividing the vertices of a graph into two disjoint setsUAnd v , so that each edge is connected to the vertices in U andV , respectively. If such a division exists, the graph is a two-part graph. An equivalent definition of a binary graph is: A graph that does not contain "rings with odd numbers of edges". Figure 1 is a two-part diagram. In order to be clear, we will later draw it into the form of Figure 2.

match : In graph theory, a "match" (matching) is a set of edges in which any two edges have no public vertices. For example, the red edge in Figure 3, Figure 4, is the match of Figure 2.

We define matching points , matching edges , unmatched points , mismatched edges , and they are very obvious. Example 3, 1, 4, 5, 7 is the matching point, the other vertices are unmatched points, 1-5, 4-7 is the matching edge, the other edges are non-matching edges.

Maximum match : A match with the largest number of matched edges in all matches of a graph, called the maximum match for this graph. Figure 4 is a maximum match that contains 4 matching edges.

Perfect Match : if one of the graphs has a match, all vertices are matching points, then it is a perfect match. Figure 4 is a perfect match. Obviously, the perfect match must be the maximum match (any point of the perfect match has already been matched, adding a new matching edge will certainly conflict with the existing matching edge). But not every diagram has a perfect match.

For example: as shown, if there is a connecting edge between a pair of boys and girls, it means they like each other. Is it possible for all boys and girls to be paired 22 so that each pair likes each other? In graph theory, this is the perfect match problem. If it's a different story: how many boys/girls do you like to pair with each other? This is the maximum matching problem.

  

The basic concept is finished. One of the algorithms for solving the maximum matching problem is the Hungarian algorithm , and the concepts below are for this algorithm service.

  

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). For example, Figure 5 shows an augmented path of 6 (the matching points in the figure are marked in red):

  

There is an important feature of the augmented path: a non-matching edge is more than a matching edge. Therefore, the significance of studying the augmented path is to improve the matching . Simply swap the identities of the matching and non-matching edges in the augmented path. This does not break the matching character because there are no other matching edges in the middle of the matching node. After the swap, the number of matching edges in the figure is 1 more than the original one.

We can add matching edges and matching points in the match by constantly looking for the augmented path. When the augmented path is not found, the maximum match is reached (this is the augmented path theorem). This is exactly what the Hungarian algorithm does. Before you give the Hungarian algorithm DFS and BFS versions of the code, first talk about the Hungarian tree.

The Hungarian tree is generally constructed from BFS (similar to the BFS tree). Run BFS from an unmatched point (the only limitation is that you must take the alternate path) until you can no longer expand. For example, from Figure 7, you can get 8 of a BFS tree:

This tree has a leaf node as a non-matching point (number 7th), but the Hungarian tree requires all leaf nodes to be the matching points, so this is not a Hungarian tree. If the original image does not contain the number 7th node, then a Hungarian tree will be obtained from node 2nd. This scenario is shown in 9 (by the way, in Figure 8, the root node 2 to the non-matching leaf node 7 is obviously an augmented path that will be a perfect match along the augmented path).

2. Hungarian algorithm Implementation

The Hungarian algorithm can be implemented based on DFS or BFS. This article implements DFS -based.

The specific generation of the dock file is defined as:

1 #ifndef Hungarian_h2 #defineHungarian_h3 4#include"Bfs_n_dfs.h"//Detailed code please go to GitHub.5 6 classGraph_hun: PublicGRAPH_BD7 {8  Public:9 Graph_hun ();Ten     Virtual~Graph_hun (); One     Virtual BOOLDFS (intVexid); A     Virtual BOOLBFS (intVexid); -     intHungarian (); -  the     voidSetnumofleft (intnum); -     voidSetnumofright (intnum); -  - Private: +     intMatching[maxnum]; -     BOOL checked[Maxnum]; +     intNumofleft; A     intNumofright; at }; -  - #endif

The implementation file is:

1#include"Hungarian.h"2#include <cassert>3 4 Graph_hun::graph_hun ()5 {6memset (Matching,-1,sizeof(matching));7Memsetchecked,false,sizeof(checked));8 }9 Tengraph_hun::~Graph_hun () One { A  - } -  the BOOLGraph_hun::D FS (intVexid) - { -ASSERT (Vexid >=0&& Vexid <maxnum); -  +vector<int> adjvexes =adj (vexid); -     intSZ =adjvexes.size (); +      for(inti =0; I < sz; i++) A     { at         intAdjvex =Adjvexes[i]; -         if(!checked[Adjvex]) -         { -             checked[Adjvex] =true; -             if(Matching[adjvex] = =-1||DFS (Matching[adjvex])) -             { inMatching[adjvex] =Vexid; -MATCHING[VEXID] =Adjvex; to                 return true; +             } -         } the     } *     return false; $ }Panax Notoginseng  - BOOLGRAPH_HUN::BFS (intVexid) the { +     return true; A } the  + intGraph_hun::hungarian () - { $     intMaxmatching =0; $      for(inti =0; i < Numofleft; i++) -     { -         if(Matching[i] = =-1) the         { -Memsetchecked,false,sizeof(checked));Wuyi             if(DFS (i)) themaxmatching++; -         } Wu     } -     returnmaxmatching; About } $  - voidGraph_hun::setnumofleft (intnum) - { -Numofleft =num; A } +  the voidGraph_hun::setnumofright (intnum) - { $Numofright =num; the}
Hungarian.cpp

Below will use an example to illustrate the process of using the Hungarian algorithm to find the maximum matching of binary graphs. As an example, the two-part diagram is as follows:

  

Figure 2-1 A binary graph (where 0~1 represents the left side of the dichotomy, 6~12 represents the right side of the binary graph)

The detailed binary graph maximum matching process is shown in the following:

  

Figure 2-2 Detailed binary graph maximum matching process

In sub-figure 1 , 0 is directly connected to 6; In the figure 2, 1 is connected directly to 7, in the Figure 3, 2 tries to connect to 6 (because it is DFS search), but 6 is connected to 0, it is time to find the augmented path, find the path such as:

  

Figure 2-3 Augmented path "2->6->0->7->1->10"

The matched and unmatched paths are then reversed to get the sub Figure 3 in Figure 2-2. In figure 2-2 of the sub-figure 4, 3, 4 are directly connected to 8, 9, there is no way to find the augmented path process. For 5, the augmented path could not be found, so 5 could not match. The final match results are as follows:

  

Fig. 2-4 final matching result of binary graph

  

Please refer to the detailed code from GitHub.

  

"The introduction to the algorithm", "graph": the maximum matching of the binary graph without weighted (Hungarian algorithm)

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.