stable marriage problem

来源:互联网 发布:tensorflow word2vec 编辑:程序博客网 时间:2024/05/16 15:33

Initialize all men and women to free

while there exist a free man m who still has a woman w to propose to {    w = m's highest ranked such woman to whom he has not yet proposed    if w is free       (m, w) become engaged    else some pair (m', w) already exists       if w prefers m to m'          (m, w) become engaged           m' becomes free       else          (m', w) remain engaged    }

bool woman_prefer(int prefer[2*N][N],int woman,int man1,int man2){for(int i = 0; i < N; i++){//if we encounter man1 first, which indicates man1 ranks higher than man2if(prefer[woman][i] == man1)return true;if(prefer[woman][i] == man2)return false;}}void stable_marriage(int prefer[2*N][N]){//stores partner of woman, this is our output array that stores paing infor//the value of w_partner[i] indicates the partner assigned to woman N+i.Note that//the woman number between N and 2N-1,the value -1 indicates that (N+i)th woman is freeint w_partner[N];memset(w_partner,-1,sizeof(w_partner));queue<int> free_man;//it stores the no. of free manfor(int i = 0; i < N; i++)free_man.push(i);//the proposed indice of man, proposed_indice[i] means the highest indice of //woman which he has proposed to beforeint proposed_indice[N];memset(proposed_indice,-1,sizeof(proposed_indice));while(!free_man.empty()){int man = free_man.front();free_man.pop();//弹出一个free man//this indice of highest ranked woman who has not been proposedint index_propose = ++proposed_indice[man];//the woman who has not been proposed by this manint woman = prefer[man][index_propose];//if this woman is free, then they are engagedif(w_partner[woman-N] == -1){w_partner[woman-N] = man;}// if this woman has a partnerelse{int man_other = w_partner[woman-N];//if this woman prefer man to man_other,then the woman engaged //man and dumped man_otherif(woman_prefer(prefer,woman,man,man_other)){w_partner[woman-N] = man;free_man.push(man_other);}elsefree_man.push(man);}}cout << " woman man " << endl;for(int i = 0; i < N; i++)cout << " " << i+N <<"\t"<< w_partner[i] << endl;}
int main(){/*int prefer[2*N][N] ={{7, 5, 6, 4},        {5, 4, 6, 7},        {4, 5, 6, 7},        {4, 5, 6, 7},        {0, 1, 2, 3},        {0, 1, 2, 3},        {0, 1, 2, 3},        {0, 1, 2, 3}};*/int prefer[2*N][N] ={{7, 5, 6, 9,8},        {5, 8,9, 6, 7},        {9,5, 8, 6, 7},        {8, 5, 6, 7,9},{9,8,7,6,5},        {0, 1, 2, 3,4},        {0, 1,4, 2, 3},        {4,0, 1, 2, 3},        {0, 1, 4, 3,2},{4,3,2,1,0}};stable_marriage(prefer);return 0;}
分析:每次man从他的prefer list中寻找一个然后求婚,这样求婚对数最大为n^2,每次迭代消除一对所以循环的最大次数为O(n^2)。故算法肯定收敛。而一旦某个woman配对成功后就永远保持配对(但可以换配偶),所以最终肯定是n对。而且肯定是stable的,用反证法即可证明.

0 0
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 住酒店遇到被色情诈骗怎么办 怎么办我陷入感情骗局了 移动手机卡被强制停机怎么办 诚e赊套现被骗了怎么办 钻戒小了怎么办小窍门 爱用商城逾期了怎么办 工程骗局洗脑了怎么办 还网贷客服电话打不通怎么办 中信客服电话打不通怎么办 亚马逊客服电话打不通怎么办 安卓软件闪退怎么办? 吃鸡麦克风炸麦怎么办 美版iphone屏摔了怎么办 天地荣域四十后怎么办 淘宝拍下商品不付款怎么办 淘宝买东西取消订单淘金币怎么办 淘宝淘金币快过期了怎么办 公众号密码忘了怎么办 酷狗直播个入驻粉丝少怎么办 健康证预约显示未接收怎么办 在互惠车贷逾期怎么办 天天中彩票怎么登陆不了怎么办 正宇新商城不能提现怎么办 融e购买完不发货怎么办 婴儿肚子胀气肚脐凸出来怎么办 微信商城买东西被骗了怎么办 拼多多商家不发货怎么办 鞋上的饰品掉了怎么办 饰品上的钻掉了怎么办 dota2饰品被好友礼物怎么办 dota2接收的礼物打不开怎么办 英雄联盟线上被压制怎么办 云联商城的钱怎么办 高顿财经不退款怎么办 拼多多连不上网怎么办 拼多多评分太低怎么办 拼多多限制提现怎么办 手机收不到验证码怎么办 淘宝未发货怎么取消退款怎么办 羊皮的鞋子刮坏怎么办 退货寄错了东西怎么办