Stable matching problem stable matching-stable marriage Algorithm

Source: Internet
Author: User

Original address "http://blog.csdn.net/cscmaker/article/details/8291131

(1) Question:

There are n male and n female, and each person ranks according to his liking for the opposite sex. Now we need to write an algorithm to arrange for the marriage of N male and n female, which requires that the marriage of two men be stable.

What is stability?

There are two couples: M1 F2 and M2 F1. M1 prefers F1, but he and F2 are married. m2 prefers F2, but fate makes him marry F1. Obviously, such a marriage is unstable, at any time, M1 and F1 may run away or M2 and F2 run away. So when we make a matching choice (that is, when we get married), we need to make a stable choice to prevent this situation from happening.

(2) algorithm introduction:

Reference: http://www.matrix67.com/blog/archives/2976

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.

The algorithm adopts the form of boys actively pursuing girls.

Algorithm step description:

In the first round, every man chooses the first woman on his/her list and tells her. In this case, there may be two situations: (1) the lady has not been pursued by boys, then the lady accepts the boy's request. (2) If the girl has accepted the pursuit of other boys, the girl will compare the man with her current boyfriend. If she prefers her boyfriend, reject the pursuit of this person. Otherwise, abandon his boyfriend )......

After the first round, some men already have girlfriends, and some men are still single.

In the second round of female Chase, every single man selects his favorite one from all the girls who have not rejected him, and tells her whether she is single or not. In this case, the two situations mentioned above are still the same solutions. Until everyone is not single.

How can we prove that this algorithm will surely get a stable marriage:

(1) as the number of turns increases, everyone can always match the correct number. Because boys confess to ladies in sequence based on their own rankings, if one person is not matched, then this person must confess to all girls. However, if a girl is confessed once, she cannot be single. That is to say, all girls are not single at this time, which is contrary to the absence of matching. Therefore, this assumption is not true. This algorithm will make it possible for everyone to pair successfully.

(2) As the number of turns increases, men's targets are getting worse and worse, while women's boyfriends may get better and better. Assume that male A and female 1 have their own objects, but male a prefers female 1 compared to the current objects. Therefore, male a must have already expressed his/her feelings with female 1 before that, in addition, female 1 rejects male A, that is, female 1 has a boyfriend who is better than male A, so she will not run away .......

(3) Algorithm Implementation

# Include <iostream> # include <stack> using namespace STD; # define num 4 # define nil-1int getpositionfromladay (INT ladayarray [] [num], int Laday, int man) {for (INT I = 0; I <num; I ++) if (ladayarray [Laday] [I] = man) return I; return nil ;} void choosepartener (stack <int> & manstack, int manpos, int manarray [] [num], int ladayarray [] [num], int manperfer [], int manstartpos [], int ladaynow []) {// select the first woman in the list. Int perfer Laday = manarray [manpos] [manstartpos [manpos]; // if the girl has not accepted the confession, accept the confession of the boy if (ladaynow [perferladay] = nil) {ladaynow [perferladay] = manpos; manperfer [manpos] = perferladay;} // if someone already confessed to her, determine whether else {int oldpos = round (ladayarray, perferladay, ladaynow [perferladay]) is well pursued. Int newpos = getpositionfromladay (ladayarray, perferladay, manpos ); if (oldpos <newpos) {manstartpos [manpos] ++; // specifies Girls prefer what they have now. Select the second place in their minds. // join the single rank manstack. push (manpos);} else // change boyfriend {// lost boyfriend restores his free identity manstartpos [ladaynow [perferladay] ++; // join the single row manstack. push (ladaynow [perferladay]); // change the target man to the current boyfriend ladaynow [perferladay] = manpos; manperfer [manpos] = perferladay ;}} int main () {int manarray [num] [num] = }}; int ladayarray [num] [num] = {0, 3,}, {,}; int manperfer [num] = {0}; // int manstartpos [num] = {0} selected by each boy }; // record the number of girls selected in the minds of each boy int ladaynow [num] = {nil, nil }; // The boys' stack corresponding to the female <int> manstack; // the men who are still in the single State // perform the first iteration. Each male chooses the girl who ranks first in the list. For (INT Pos = 0; POS <num; POS ++) {choosepartener (manstack, POs, manarray, ladayarray, manperfer, manstartpos, ladaynow);} while (manstack. size ()! = 0) {int manpos = manstack. top (); manstack. pop (); choosepartener (manstack, manpos, manarray, ladayarray, manperfer, manstartpos, ladaynow) ;}for (INT I = 0; I <num; ++ I) cout <"man no.: "<I <" Laday No.: "<manperfer [I] <Endl ;}

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.