gale-shapley算法的C语言实现
来源:互联网 发布:淘宝有哪些推广方式 编辑:程序博客网 时间:2024/05/21 06:48
gale-shapley算法的详细描述就不说了 ,算法过程如下:
初始所有的m∈M和w∈W都是自由的
While 存在男人m是自由的且还没对n个中每个女人都求过婚
选择一个这样的男人m
令w是m的优先表中m还没求过婚的最高排名的女人
If (w是自由的)
(m,w)变成约会状态
Elseif(w当前与m’)约会
If w是更偏爱m’then
m保持自由
Else w更偏爱m then
(m,w)变成约会状态
m’变成自由
endif
endif
Endwhile
输出已约会的配对的集合,即为稳定匹配。
代码实现思路的:用两个结构体来表示男人和女人,随机产生优先级数组,具体代码如下:
</pre><pre name="code" class="cpp">#include<stdio.h>#include<stdlib.h> #include<time.h>struct man{ int flag; int m_id; int ms[10];//男人对女人的优先级数组};struct woman{ int flag; int wm_id; int wms[10];<span style="font-family: Arial, Helvetica, sans-serif;">//女人对男人的优先级数组</span>};struct man men[10];struct woman women[10];void sui_ji_shu(int tmp[10],int j) //产生0-9之间的随机数,且没有重复的。 { int a[10]; int i,index,x=0; for(i=0;i<10;i++) { a[i]=i; tmp[i]=0;} //srand((unsigned)time(&j)); srand((unsigned)j); for(i=0;i<10;) { index=rand()%10; if(a[index]!=-1) { tmp[i]=a[index]; // printf("%d\n",tmp[i]); a[index]=-1; ++i ; } } // return b;}int exit_freeman(struct man men[10])//判断是否存在自由的m,存在(1),不存在(0) { //printf("5555\n"); int i; for(i=0;i<10;++i) { if(men[i].flag==0) return i+1;//return i+1是为了while循环好判断一点。 } return 0; }int has_woman(int i)//判断men[i]是否还有未求过婚的w,有(返回位置加1),没有(0) {int j;for(j=0;j<10;++j){if(men[i].ms[j]!=-1)return 1;}return 0;}int first_woman_id(int i){int j;for(j=0;j<10;++j){if(men[i].ms[j]!=-1)return j;}} int woman_love_more(int location,int mf,int mfp){int i,j,k;for(i=0;i<10;++i){if(women[location].wms[i]==mf){j=i;}if(women[location].wms[i]==mfp){k=i;}}if(k<j)return 1;elsereturn 0;}int main(){ int i,j; int index; int mf,wmf; int wm_id; int mfp; int location; int result[10][10];//二维数组用于存放最后结果 for(i=0;i<10;++i) for(j=0;j<10;++j) result[i][j]=0; for(i=0;i<10;++i) { men[i].flag=0; men[i].m_id=i; women[i].flag=0; women[i].wm_id=i; } for(j=0;j<10;++j){index=rand()%10;sui_ji_shu(men[j].ms,index);}for(j=0;j<10;++j){index=rand()%10;sui_ji_shu(women[j].wms,index);}mf=exit_freeman(men);wmf=has_woman(mf-1);while(mf&&wmf){wm_id=first_woman_id(mf-1);location=men[mf-1].ms[wm_id];men[mf-1].ms[wm_id]=-1;if(women[location].flag==0){women[location].flag=1;men[mf-1].flag=1;result[mf-1][location]=1;}else{for(i=0;i<10;++i)if(result[i][location]==1){mfp=i;break;}if(woman_love_more(location,mf-1,mfp)){men[mf-1].flag=0;}else{men[mf-1].flag=1;result[mf-1][location]=1;result[mfp][location]=0;men[mfp].flag=0;}}mf=exit_freeman(men);wmf=has_woman(mf-1);}for(i=0;i<10;++i){for(j=0;j<10;++j) printf("%d ",result[i][j]); printf("\n");} }
0 0
- gale-shapley算法的C语言实现
- 稳定的匹配算法–Gale-Shapley
- [算法]Gale-Shapley Algorithm-稳定匹配算法的设计、实现与探讨(上)
- [算法]Gale-Shapley Algorithm-稳定匹配算法的设计、实现与探讨(下)
- Gale-Shapley algorithm 博弈算法
- 稳定婚姻问题和Gale-Shapley算法实现
- Gale-Shapley算法中 男性优势地位 的证明
- Gale-Shapley---婚姻匹配算法算法
- 稳定婚姻问题和Gale-Shapley算法
- 稳定婚姻问题和Gale-Shapley算法
- 稳定婚姻问题和Gale-Shapley算法
- 稳定婚姻问题和Gale-Shapley算法
- 稳定婚姻问题和Gale-Shapley算法
- 稳定婚姻问题和Gale-Shapley算法
- Gale Shapely算法的实现
- 顾森:稳定婚姻问题和Gale-Shapley算法
- SGU 264 Travel 稳定婚姻匹配 Gale-Shapley算法
- Stable Matching Problem —— Gale Shapley算法
- Codeforces 464B Restore Cube(暴力)
- KMP模式匹配算法
- 链表有环问题
- static{}语句块详解
- Java 连接MySql数据库
- gale-shapley算法的C语言实现
- WordPress主题 Q9 全站ajax加载自适应CMS高级模板[更新至v1.9]
- 【刷机】LG G2 能进入recovery但是不能进入新系统,can't open file ,G2刷TOT
- accelerated c++习题0-10
- Java(二)--OOP设计思想
- [ZOJ 3812 We Need Medicine] DP
- Leetcode dfs Letter Combinations of a Phone Number
- 机房收费系统之添加和删除用户
- Python Selenium 问题记录