A simple analysis of the maximal matching algorithm for binary graphs

Source: Internet
Author: User

There are so two strange factories: Factory x only produces cups, factory y only produces washing tools. More recently, two plants have decided to implement a packaging strategy: a cup with a washing tool. However, since the cups and washes have different shapes and functions, for a certain category of cups, they can only be paired with certain types of washing. Now, the factory director of two plants wants to know how many pairs of cups and washes can be successfully paired.

Similar to the collocation problem mentioned in the above example, there is a canonical name in graph theory: match. Note that the above example involves only two types of items (cups and washes), and the problem relates only to the matching of the cup with the wash, and we refer to a matching problem involving only one relationship as a binary matching problem.

Now, let's clarify some concepts.

Two-part diagram: If the points in Figure G can be divided into X and y parts, and each part of the interior is not connected to any side, (you can imagine, under normal circumstances will not appear in the base.) ) The figure G is a two-point figure.

Match: A collection of edges without a common point (you can imagine the word marriage).

Number of matches: the number of edges in the edge set

Maximum match: Match the maximum number of matches.

1-1, Show is a two-part diagram: The bold line represents a matching method of the dichotomy, it is not difficult to find that the match is now the maximum match.

How can I get a maximum match for a binary graph? Use a simple enumeration: Find out all matches, and then keep the most matching numbers. However, the time complexity of this algorithm is the number of points of the number of edges, which is usually not tolerable in time. Therefore, it is necessary to seek a more efficient algorithm. This leads to the Hungarian algorithm (Hungary), the name of which is interesting, it was proposed by the Hungarian mathematician Edmonds in 1965.

Before formally speaking this algorithm, think about it, what other methods can be used to calculate the maximum matching of two-cent graph more quickly? Yes, the network's maximum flow algorithm can be done: we need to add an additional Yuanhui point s,t, then for figure 1-1 we are easy to get 1-2 of the network model, all the edge capacity of the figure is 1, the bold arrows indicate that the flow from the edge:

Thus, the problem is equivalent to the conversion: the maximum number of matches = maximum flow. If the SAP algorithm is used to calculate the maximum flow, the time complexity is O (v2e), which has high efficiency. But kill chicken with slaughter sledgehammer, in fact, we do not need to complicate the problem, in view of the particularity of the dichotomy graph, we can use more efficient, less code Hungary algorithm solution.

Thus, the problem is equivalent to the conversion: the maximum number of matches = maximum flow. If the SAP algorithm is used to calculate the maximum flow, the time complexity is O (v2e), which has high efficiency. But kill chicken with slaughter sledgehammer, in fact, we do not need to complicate the problem, in view of the particularity of the dichotomy graph, we can use more efficient, less code Hungary algorithm solution.

    1. Initializes the match number CNT to 1.
    2. Looking for an augmented path in the diagram, if no augmented path can be found, execute 4, otherwise execute 3.
    3. The two points of the augmentation road are set as non-open points, and the edges of the augmented path are reversed,cnt+1, and the execution 2.
    4. At the end of the algorithm, the current CNT is the maximum number of matches.

For the above mentioned method, use the concrete calculation of figure 1-3 to demonstrate its implementation process:

(a red bold edge that represents a matching edge; a black thin edge that represents an unmatched edge.) The sky-blue dots indicate an open point, and the indigo dots indicate the non-covered points. and set the node number ≥0)

At the beginning,match[]are set to-1. Because you can start matching from any point, you might want to start with the numbered order of the points. ForX1, you can findY2match it, and makematch[y2]=x1. Similarly, forX2, you can findY3match it, and makematch[y3]=x2. When validatingX3, you will find that the only point that matchesY3has been matched, try modifying the previous matching scheme: you can findX2can also be associated withY2match, but the samematch[y2]=x1≠-1, and then to findX1whether a new match can be found;X1can also be associated withY1match, andMatch[y1]=-1, it makesmatch[y1]=x1,match[y2]=x2,match[y3]=x3. The final maximum number of matches is obtained.=3.

The above paragraph describes the algorithm specific operating procedures, it is now advisable to consider from the perspective of the augmented path: Initially, all points are not covered points, the number of matchescnt=0; We can easily find an augmented path.X1-y2, after the reverse operation, the Edge(x1,y2)from a non-matching edge into a matching edge,cnt+1=1, andX1,Y2become a non-covered point; Keep looking, we can easily find the augmented roadX2-y2, after the reverse operation, the Edge(X2,Y3)from a non-matching edge into a matching edge,cnt+1=2, andX2,Y3becomes a non-covered point. Finally, you can find the augmented pathx3-y3-x2-y2-x1-y1, the same counter operation, the cumulative number of matches:cnt+1=3, whileX1,Y1Has become a non-covered point. Notice that there is no augmented path in this diagram, that is, the maximum number of matches for the graph is3.

1 #defineMAXN 500//maximum number of vertices in X part2 #defineMAXM 500//maximum number of vertices in the Y section3 #define_CLR (x, y) memset (x,y,sizeof (×))4 5 intn,m;6 7 intMATCH[MAXM];//Tag Array8 intG[MAXN][MAXM];//adjacency Matrix9 Ten BOOLUSED[MAXM];//sentence weight One  A BOOLFindintK//Dfs looking for augmented roads - { -      for(intI=1; i<m;i++) the     { -         if(G[k][i] &&!Used[i]) -         { -used[i]=true; +             if(match[i]==-1||find (Match[i])) -             { +match[i]=K; A                 return true; at             } -         } -     } -     return false; - }  -  in intHungary () - { to     intCnt=0; +_CLR (match,-1); -      for(intI=1; i<n;i++) the     { *_CLR (Used,0); $         if(Find (i))Panax Notoginseng         { -cnt++; the         } +     } A     returnCNT; the}

Take HDU2063 as an example: http://acm.hdu.edu.cn/showproblem.php?pid=2063

1#include <cstdio>2#include <cstring>3 using namespacestd;4 #defineN 5055 intK, M, N;6 intMap[n][n], match[n];7 BOOLUsed[n];8 9 BOOLFind (intx)Ten { One      for(intI=1; i<=n; i++) A     { -         if(Map[x][i] &&!Used[i]) -         { theUsed[i] =true; -             if(match[i]==-1||find (Match[i])) -             { -Match[i] =x; +                 return true; -             } +         } A     } at     return false; - } -  - voidHungary () - { -     intCnt=0; inmemset (Match,-1,sizeofmatch); -      for(intI=1; i<=m; i++) to     { +memset (Used,0,sizeofused); -         if(Find (i)) cnt++; the     } *printf ("%d\n", CNT); $ }Panax Notoginseng intMain () - { the     intA, B; +      while(~SCANF ("%d", &k) &&k) A     { thememset (Map,0,sizeofmap); +scanf ("%d%d", &m, &n); -          while(k--) $         { $scanf ("%d%d", &a, &b); -MAP[A][B] =1; -         } the Hungary (); -     }Wuyi     return 0; the}
View Code

Source: A Senior

A simple analysis of the maximal matching algorithm for 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.