Topic:
Dating agency registered n Boys and N girls, each boy to n a girl's liking degree to do a sort of, each girl to n boy's liking degree to do a sort of, you as matchmaker, can give a steady hand plan?
Stable definition: If boy I and girl a hand, but boy I to girl B more like, while Girl B's boyfriend J spell but boy I, then there is no power to hinder boys I and Girl B's Elope, which is unstable.
Ideas:
In 1962, American mathematician David Gale and Lloyd Shapley invented a strategy for finding a stable marriage. Regardless of the number of men and women, regardless of their preferences, applying this strategy can always get a stable marriage match. In other words, they proved that a stable marriage match always exists. Interestingly, this strategy reflects a lot of real life situations.
The algorithm uses the male to pursue the girl's form actively.
Algorithm Step Description:
In the first round, every man chooses the first woman on his list and confessed to her. There are two situations at this time:
(1) The woman was not pursued by the boy, and the woman accepted the request of the boy.
(2) If the girl has been the pursuit of other boys, then the girl will compare the man with her current boyfriend, if more like her boyfriend, then reject the pursuit of this person, otherwise, abandon his boyfriend
At the end of the first round, some men already have girlfriends and some men are still single.
In the second round, every single man chooses his favorite from all the girls who have not rejected him, and she confessed to her whether she is single now or not. At this point, you will still encounter the two situations mentioned above, or the same solution. Until everyone is no longer single.
How do you prove that this algorithm is sure to get a stable marriage?
(1) As the number of rounds increases, there is always a time for everyone to match up. Because the boys according to their own rank in order to the woman to vindicate, if there is a person did not match the right, then this person must be to all the girls to vindicate. But once the girl has been confessed once, it is impossible to be single, that is, all the girls are not single, this and there is a person does not match the pair is contrary. So the hypothesis is not tenable. The algorithm is sure to make the pairing successful for everyone.
(2) as the number of wheels increases, men are getting worse, while women's boyfriends may become more and more good. Suppose male A and female 1 each have their own object, but compared to the current object, male a more like female 1, so, before this male a must have with the female 1 confessed, and the female 1 refused male A, that is, female 1 has a better boyfriend than male, will not appear to elope situation ....
Code:
#include <iostream>using namespacestd;Const intn=4;voidGaleshapley (Const int(&man) N [N],Const int(&woman) N [N],int(&match) [N]) { intWm[n][n];//Wm[i][j]: rank from girl I to boy J intChoose[n];//Choose[i]: current boyfriend of Girl I intManindex[n];//Manindex[i]: How many girls that has rejected boy I inti,j; intw,m; for(i=0; i<n;i++) {Match[i]=-1; Choose[i]=-1; Manindex[i]=0; for(j=0; j<n;j++) Wm[i][woman[i][j]]=J; } BOOLbsingle=false; while(!bsingle) {Bsingle=true; for(i=0; i<n;i++){ if(match[i]!=-1)//Boy I already had a girlfriend Continue; Bsingle=false; J=manindex[i]++;//The jth girl .w=Man[i][j]; M=CHOOSE[W];//Current girl W ' s boyfriend if(m==-1|| Wm[w][i]<wm[w][m]) {//if girl w prefer boy Imatch[i]=W; CHOOSE[W]=i; if(m!=-1) Match[m]=-1; } } }}voidPrint (Const int(&match) [N],intN) { for(intI=0; i<n;i++) cout<<" Boy"<<i<<"matches"<<"Girl"<<match[i]<<Endl; cout<<Endl;}intMain () {intman[n][n]={ {2,3,1,0}, {2,1,3,0}, {0,2,3,1}, {1,3,2,0}, }; intwoman[n][n]={ {0,3,2,1}, {0,1,2,3}, {0,2,3,1}, {1,0,3,2}, }; intMatch[n]; Galeshapley (Man,woman,match); Print (Match,n); return 0;}
(algorithm) stable marriage match