The Gale-Shapley Algorithm 学习笔记
来源:互联网 发布:office图标修复软件 编辑:程序博客网 时间:2024/06/05 08:05
最近在认真专研匹配算法,同学给推荐了这个算法,感觉不错。不过,先前有很多大神已经写过这个算法的介绍了,我这当晚辈的,迟迟不敢动笔怕班门弄斧。但是呢?如果有前辈看我写得一塌糊涂看不下去了给我指点一点两点,那就是真的赚到啦!
首先看看德高望重的David Gale老教授。Gale老教授研究的领域主要有数理经济学、博弈论和凸分析。
他在数理经济学中的主要贡献有:1.the existence of competitive equilibrium(竞争均衡的存在性);2.the solution of the n-dimensional Ramsey problem(n维拉姆齐定价,垄断厂商的定价) 3.the theory of linear programming and linear inequalities(线性模型和编程领域)
1962年的时候Gale和Lloyd Shapley撰写的关于 Stable Marriage Problem的论文是“the first formal statement and proof of a problem that has far-reaching implications in many matching markets”.原文链接如下:College admissions and the stability of marriage (with L.S. Shapley). American Mathematical Monthly 69 (1962), pp. 9–15 这个来自1962年的思想值得细细品味。
那么这个匹配是如何做到稳定的呢?
- 前提准备是什么?
在n个男人和n个女人的一对一匹配中(默认一夫一妻&异性结合)。此时,将每个男人对n个女人的喜好程度从大到小排序(情人眼里出西施,你爱萝莉脸我爱魔鬼身材~这些我们都不管),同时也对每个女人对n个男人的喜爱程度从大到小排序。 - 算法如何开始呢?
随便选择一个女人A,将她与她最喜爱的男人a进行配对,我们暂时不管这个男人对这个女人感不感冒。此时我们已经配对成功一对A-a - 算法如何对匹配结果进行优化呢?
接下去考虑女人B,如果她也最喜欢男人a,同时因为女人B长得比较漂亮所以男人a更喜欢女人B。唉,这时候虽然对不起女人A,但是由于出现了更好的匹配B-a,所以A-a匹配就失效了。 - 优化后呢?
因为比较对不起A,所以这个时候我们继续为A努力,将女人A和女人A第二喜欢的男人b进行配对。 - 以此类推···最终实现最优的稳定的匹配。
根据wikipedia上的算法介绍,上述算法可以进行如下编程,由于我是统计专业,所以暂时用R语言进行编程:
X_M<-matrix(rep(0,100),10,10) #站在男性视角的喜爱程度,X_M[i,j]代表第i个男生对第j个女生的喜欢程度a<-1:10for(i in 1:10){X_M[i,]<-sample(a,10) #喜欢程度随机生成,以1-10的数字表示,数字越高,喜欢程度越大}X_M1<-X_MX_F<-matrix(rep(0,100),10,10) #站在女性视角之上,原理同上for(i in 1:10){X_F[i,]<-sample(a,10)}X_FX_F1<-X_Fmatch<-rep(0,10) #站在男性视角之上的匹配结果,初始为0向量,match[i]=j 代表第i个男生与第j个女生配对成功while(length(which(match==0))!=0){waitboy<-min(which(match==0))i<-waitboymax<-which.max(X_M[i,])#第i个男生最喜欢的女生编号if(length(which(max==match))>0){ #呀,冲突了preboy<-which(match==max)#姑娘的前男友if(X_F[max,i]>X_F[max,preboy]){#如果姑娘的前男友被比下去了match[i]<-maxmatch[preboy]<-0}else{#如果姑娘的前男友胜利啦X_M[i,max]<-0}}else{match[i]<-max}}#总体匹配效果为:fit1<-0fit2<-0for(i in 1:10){fit1<-fit1+X_M1[i,match[i]]fit2<-fit2+X_F1[match[i],i]}
结果展示:
<pre name="code" class="html"> X_M [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [1,] 8 10 1 5 6 3 4 7 9 2 [2,] 4 10 1 8 3 7 2 5 6 9 [3,] 8 3 1 9 5 2 10 6 4 7 [4,] 7 2 8 1 4 5 9 3 6 10 [5,] 3 9 2 10 6 4 5 7 1 8 [6,] 4 1 3 8 7 5 10 9 2 6 [7,] 4 3 10 2 5 1 7 8 6 9 [8,] 6 8 5 4 7 1 9 2 10 3 [9,] 2 6 9 8 7 5 4 1 3 10[10,] 10 2 5 8 6 1 3 4 7 9> X_F [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [1,] 10 7 5 2 6 1 3 4 8 9 [2,] 2 6 4 7 5 1 3 10 8 9 [3,] 5 6 4 1 7 9 10 3 8 2 [4,] 3 9 4 1 2 7 5 6 10 8 [5,] 10 7 9 1 5 6 4 8 3 2 [6,] 8 5 2 10 9 6 4 3 1 7 [7,] 7 3 10 2 6 4 5 8 9 1 [8,] 7 2 5 3 1 9 10 4 6 8 [9,] 9 10 4 1 2 6 3 5 7 8[10,] 6 5 8 7 10 3 1 9 4 2> match [1] 1 9 7 6 10 8 3 2 4 5> fit1[1] 94> fit2[1] 63
思考:
1.这个默认的喜好排序能通过引入机器学习来自动进行呢?
2.好吧,如果是机器学习就用不到这个算法了
3.先站在男性视角考虑的结果是,男性的满意度比女性高很多(fit1>fit2),估计我只学到算法皮毛
- The Gale-Shapley Algorithm 学习笔记
- Gale-Shapley algorithm 博弈算法
- Stable Matching: Gale and Shapley Algorithm
- [算法]Gale-Shapley Algorithm-稳定匹配算法的设计、实现与探讨(上)
- [算法]Gale-Shapley Algorithm-稳定匹配算法的设计、实现与探讨(下)
- poj 3487 zoj 3120 The Stable Marriage Problem 延迟认可算法(Gale-Shapley算法)
- POJ 3487 The Stable Marriage Problem(Gale-Shapley算法求稳定婚姻)
- poj3487(Gale-Shapley算法) The Stable Marriage Problem(稳定婚姻问题)
- 稳定婚姻问题和Gale-Shapley算法
- 稳定婚姻问题和Gale-Shapley算法
- 稳定婚姻问题和Gale-Shapley算法
- gale-shapley算法的C语言实现
- 稳定婚姻问题和Gale-Shapley算法
- 稳定的匹配算法–Gale-Shapley
- 稳定婚姻问题和Gale-Shapley算法
- 稳定婚姻问题和Gale-Shapley算法
- Gale-Shapley---婚姻匹配算法算法
- Algorithm学习笔记 --- The Pilots Brothers' refrigerator
- AsyncTask onPostExecute 未执行问题
- Android studio各种问题
- Mac下好用的HTTP/HTTPS抓包工具Charles
- Jsoup select 选择器
- C语言程序的内存如何布局
- The Gale-Shapley Algorithm 学习笔记
- NSString的八条实用技巧
- 【Hibernate七】session的缓存机制
- android如何更新
- 骑驴找马被察觉怎么办?
- c#中如何获取本机MAC地址、IP地址、硬盘ID、CPU序列号等系统信息
- dd命令
- C++中的delete浅析
- js获取int类型数字,长度过长出现错误