Introduction: What is an algorithm?
How to find a stable marriage match
It is said that a book starts with a straightforward talk about gender topics, and this book will soon sell well. Fortunately, among the many topics that can be used to introduce "algorithms", my favorite one has such a relationship with the gender. If you are a matchmaker, there are several single men asking for help, there are also a number of single women also come to marry. If you already know the ranking of these girls in the minds of every man and the ranking of boys in the minds of every girl (1), how should you tie them together?
The best matching solution is, of course, that the other half of each person is just their "first choice ". This is perfect, but it is impossible in most cases. For example, male 1's favorite is female 1, while female 1's favorite is not male 1, the best choice of both of them cannot be satisfied at the same time. If several men love the same girl most, their first choice will not be satisfied at the same time. When the most ideal pairing scheme cannot be implemented, what matching scheme can be satisfactory?
In fact, finding perfect objects is not necessarily a good thing. Harmony is the key to marriage. If male 1 and female 1 have their own objects, but male 1 thinks that female 1 is better than their current objects. FEMALE 1 also finds that, in my mind, the ranking of male 1 is higher than that of his current boyfriend. In this way, the two may have an affair, and finally drop their current object and run away together-because this result is better for both of them. In a pairing scheme between men and women, if this happens, we say that marriage is unstable. As a matchmaker, you are deeply aware that it is irrelevant to the object, so you are afraid that the marital relationship is unstable. Although it is impossible for everyone to get the most suitable match for the customer, the marriage match must be stable. In other words, for each person, in his mind is better than his current partner, will not think that he is also a better choice. Now, our question is: is there always a stable marriage combination? How can we find a stable marriage combination?
For ease of analysis, we will make some conventions below. We numbered male with letters A, B, and C, and female with numbers 1, 2, and 3. We put all men on the left from top to bottom, and the numbers in parentheses indicate the ranking of all women in each person's mind. Then, we put all women on the right, the letters in brackets indicate their preferences for men. Figure 0-1 shows a situation where two men and two women prefer female 1, but female 1 prefers male B, FEMALE 2 prefers male. If match by A-1, B-2, then male B and female 1 are more like each other, such a marriage is not stable. However, if you change the matching scheme (0-2), the matching will be stable.
Figure 0-1 an unstable marriage with both male B and female 1 is not satisfied with the current partner
Figure 0-2 a stable marriage match
Many may immediately think of a strategy to find a stable marriage match: Constantly patching the current match plan. If two people both think that the other person is better than their current partner, let the two become a pair, and the two who are left to be dumped form a pair. If there are still male couples who want to run away, they will continue to change their desires until all unstable combinations are finally eliminated. It is easy to see that the final result obtained by applying this "repair strategy" must satisfy the stability of marriage, but the problem with this strategy is that it does not necessarily have a "final result ". As a matter of fact, adjusting the matching scheme repeatedly based on the above method may eventually fall into an endless loop. Therefore, this strategy cannot even guarantee a definite scheme.
Figure 0-3 applying a "repair policy" may produce an endless loop
In 1962, American mathematicians David Gale and Lloyd Shapley invented a strategy to find a stable marriage. Regardless of the number of men and women, or their preferences, a stable marriage match can always be achieved after such a strategy is applied. In other words, they prove that a stable marriage always exists. Interestingly, this strategy reflects many real situations in real life.
In this strategy, a man will pursue his desired girl round by round. A woman can choose to accept or reject her pursuer. In the first round, every man chooses the first woman on his/her list and tells her. At this time, a girl may face three situations: No one tells her, only one tells her, and more than one tells her. In the first case, the girl does not have to do anything. She just needs to wait. In the second case, she accepts the confession of the girl and promises to be a friend of both men and women for the moment; in the third case, select the one that you like most from all the pursuers, promise to be friends with him for the moment, and reject all others.
After the first round, some men already have girlfriends, and some men are still single. In the second round of female Chase, every single manFrom all the girls who have not rejected himSelect the one you like most and tell her whether she is single or not. Like in the first round, female needs to select the most desirable one from the confession and reject other pursuers. Note: If this girl already has a boyfriend, when she meets a better pursuer, she must drop her current boyfriend and embrace the new pursuer. In this way, some single men will get their girlfriends, and those who already have their girlfriends may be dumped and re-become bachelors. In each round in the future, single men will continue to pursue the next girl in the list. Girls will choose the best one from all the pursuers, including their current boyfriends, and say no to others. This round continues until everyone is no longer single at a certain time. There will be no new confession in the next round, and the whole process will automatically end. At this time, the marriage match must be stable.
Figure 0-4 apply the above strategy and a stable marriage match will be obtained after three rounds
Will this policy be like the previous repair method, and it will never be terminated? No. Next we will explain that with the increase in the number of rounds, everyone can always match the correct number. Since at least one man confessed to a woman in each round, the total number of confessions will increase with the increase in the number of rounds. If the entire process has never ended because everyone is right, then a man will inevitably catch up with all the girls. As long as a girl is chased once, she cannot be single again in the future. Since all girls have been chased by this man, it means that all girls are not single now, that is to say, everyone is right now.
Next, we need to prove that the matching scheme is indeed stable. First of all, I noticed that as the number of turns increases, a man's pursuit of objects always gets worse and worse, and a girl's boyfriend may only get better and better. Assume that male A and female 1 have their own objects, but male a prefers female 1 compared with the current objects. Therefore, before that, male a must have confessed to female 1. Since female 1 was not with male a at last, it means female 1 rejects male A, that is, she has a better man than male. This proves that although two people are not one, they both feel that the other party is better than their current partner, and such a situation will never happen.
We call a strategy, or a solution, or a processing process, or a series of operation rules, or a more appropriate set of computing methods, called algorithms ". The above strategy for finding a stable marriage is called the gale-Shapley algorithm, which is also called the latency recognition algorithm ".
Every algorithm has its practical significance, which can inspire us a lot. The biggest significance of the gale-Shapley algorithm is that, as a matchmaker for these men and women, you do not need to personally calculate stable marriage matching, or even understand everyone's preferences, you only need to organize a pairing activity between men and women based on this algorithm. All you need to do is to use the algorithm flow as a game rule to tell everyone that after the game ends, it will automatically get a marriage match that everyone is satisfied. For men, starting with their favorite girl is a matter of course. For women, it is in her interest to constantly choose the best man. Therefore, everyone will automatically abide by the rules of the game, so there is no need to worry about people falsely reporting their preferences.
In history, such a "pairing game" has actually been applied. What's more interesting is that the application of this algorithm is 10 years earlier than the algorithm. As early as 1952, the United States began to use this method to schedule work for medical school students, known as the "national resident physician pairing program ". The basic process of pairing is that hospitals select the best candidates from medical school students who have not yet rejected this position and send Employment notices. When students receive employment notices from hospitals, the system will automatically assign the desired position based on the willingness table he filled in, and reject other positions. This is repeated until every student is assigned a job. Of course, at that time, people didn't know that such a process could ensure the stability of work distribution, but they thought it was reasonable by intuition. It was not until 10 years later that Gale and Shapley systematically studied this process, raised the issue of stable marriage, and proved the correctness of this algorithm.
Stability can be used to evaluate the performance of pairing schemes. However, sometimes we may encounter other requirements, which correspond to many other problems in the algorithm world. For example, if we already know the matching degree between each pair of men and women, how can we find a matching solution to maximize the total matching degree? In the algorithm field, this is called the maximum weight matching problem of a bipartite graph. For another example, if we do not consider the differences in gender (such as the matching of the same table and partner), the problem becomes more complicated, which is usually classified into the category of general graph matching. These problems have now found effective algorithms, but they are too complicated and beyond the scope of this book. Algorithm applications in life can be seen everywhere. What this book is about is to guide everyone from the familiar things around them to see the endless charm of algorithms.
-------------------------------------------------------------------
(1) The source of this ranking is not important. It may be provided directly by the customer based on the personal information of the opposite sex, or it may be calculated based on the personal information submitted by the customer.