稳定匹配 - Stable Matching
这篇文章将会对稳定匹配算法进行介绍及Python代码的实现,第一部分会针对稳定匹配的Gale-Shapley算法进行解析,第二部分就是用Python对该算法进行实现。
一、稳定匹配算法原理
1.1 介绍
稳定匹配(Stable Matching)问题就是假设现在有N个男生和N个女生跳舞选择伴侣,然后最开始的时候男、女生按照下面情况对彼此进行排序选择舞伴(见图1):
- 每个男生都对女生按照最喜欢到最不喜欢进行排序;
- 同样的,女生也是按照最喜欢的到最不喜欢对男生进行排序。

算法目标:每个男都找到唯一一个女舞伴,反之亦如此,从而达到了所谓的稳定匹配。
演示步骤:






1.2 伪代码(Gale-Shapley Algorithm)
1 # 首先初始化所有男生的状态为自由
2 initialize each person to be free
3
4 # 当男生没有未曾被匹配过并且也没有向所有其他女生寻求舞伴过时不断循环
5 while some man m is not yet matched:
6 # 每个男生按照对女生的喜欢程度选择舞伴
7 w := m's most favroite woman to whom he has not yet proposed
8 # 如果女生未被匹配到,则与男生进行配对
9 if w is also not yet matched:
10 w and m are paired
11 # 如果女生与已匹配的男生相比更喜欢当前的这个男生,则拆散重新匹配
12 elif w favors m to her current matched m':
13 w and m are paired and m' is dis-matched
14 # 否则该女生拒绝成为男生的舞伴
15 else:
16 w rejects m
17 # 返回所有匹配成功的舞伴对
18 return matched pairs
二、Python代码实现
# -*- encoding: UTF-8 -*-
import copy # 男的所期望的对象
manPrefers = dict((m, prefs.split(', ')) for [m, prefs] in (line.rstrip().split(': ')
for line in open('men.txt')))
# 女的所期望的对象
womenPrefers = dict((m, prefs.split(', ')) for [m, prefs] in (line.rstrip().split(': ')
for line in open('women.txt'))) men = sorted(manPrefers.keys())
women = sorted(womenPrefers.keys()) # 定义检测函数检测匹配的伴侣是否稳定
def check(engaged):
inverseengaged = dict((v,k) for k,v in engaged.items())
for w, m in engaged.items():
shelikes = womenPrefers[w]
shelikesbetter = shelikes[:shelikes.index(m)]
helikes = manPrefers[m]
helikesbetter = helikes[:helikes.index(w)]
for man in shelikesbetter:
womenOftheMan = inverseengaged[man]
manLoves = manPrefers[man]
if manLoves.index(womenOftheMan) > manLoves.index(w):
print("%s 和 %s 更喜欢彼此相比起他们当前的伴侣: %s 和 %s" % (w, man, m, womenOftheMan))
return False
for woman in helikesbetter:
manOfTheWomen = engaged[woman]
womanLoves = womenPrefers[woman]
if womanLoves.index(manOfTheWomen) > womanLoves.index(m):
print("%s 和 %s 更喜欢彼此相比起他们当前的伙伴:%s 和 %s" % (m, woman, w, manOfTheWomen))
return False
return True def stableMatching():
free_men = men[:]
engaged = {}
manPref_temp = copy.deepcopy(manPrefers)
womenPref_temp = copy.deepcopy(womenPrefers)
while free_men:
man = free_men.pop(0)
manList = manPref_temp[man]
woman = manList.pop(0)
fiance = engaged.get(woman)
if not fiance:
engaged[woman] = man
print(" %s 和 %s 成为伴侣" % (man, woman))
else:
womenList = womenPref_temp[woman]
if womenList.index(fiance) > womenList.index(man):
engaged[woman] = man
print(" %s 舍弃 %s 而和 %s 成为伴侣" % (woman, fiance, man))
if manPref_temp[fiance]:
free_men.append(fiance)
else:
if manList:
free_men.append(man)
return engaged if __name__ == '__main__':
print('\n伴侣匹配:')
engaged = stableMatching() print('\n伴侣匹配:')
print(' ' + ',\n '.join('%s 和 %s 成为伴侣' % couple for couple in sorted(engaged.items())))
print()
print('伴侣稳定性检测通过' if check(engaged) else '伴侣稳定性检测不通过') print('\n\n因交换而产生伴侣搭配错误')
engaged[women[0]], engaged[women[1]] = engaged[women[1]], engaged[women[0]]
for woman in women[:2]:
print(' %s 现在和 %s 成为伴侣' % (woman, engaged[woman]))
print()
print('伴侣稳定性检测通过' if check(engaged) else '伴侣稳定性检测不通过')
稳定匹配 - Stable Matching的更多相关文章
- Stable Matching 稳定匹配 婚姻算法 shapley 算法
作者:jostree 转载请注明出处 http://www.cnblogs.com/jostree/p/4051286.html 稳定匹配问题:有N男N女,每个人对于异性都一个排名,先需要得到一种稳 ...
- 完美匹配(matching)
完美匹配(matching) 题目描述 给定nn个点,mm条边的无向图G=(V,E)G=(V,E),求出它的完美匹配数量对106+3106+3取模的值. 一个完美匹配可以用一个排列ϕ:V→Vϕ:V→V ...
- Stable Matching (Gale Sharpley Algorithm)
稳定婚配问题:n个男生n个女生.当中每一个人都有自己心仪的列表. 问怎样达成稳定的匹配(比方, b想B求婚,可是B已有的对象的优先级高于b,此时b的魅力不足以拆散B所处的那一对,即达到稳定状态.) ( ...
- UVALive 3989 Ladies' Choice(稳定婚姻问题:稳定匹配、合作博弈)
题意:男女各n人,进行婚配,对于每个人来说,所有异性都存在优先次序,即最喜欢某人,其次喜欢某人...输出一个稳定婚配方案.所谓稳定,就是指未结婚的一对异性,彼此喜欢对方的程度都胜过自己的另一半,那么这 ...
- [Swift]LeetCode44. 通配符匹配 | Wildcard Matching
Given an input string (s) and a pattern (p), implement wildcard pattern matching with support for '? ...
- 阅读笔记——长文本匹配《Matching Article Pairs with Graphical Decomposition and Convolutions》
论文题目:Matching Article Pairs with Graphical Decomposition and Convolutions 发表情况:ACL2019 腾讯PCG小组 模型简介 ...
- Unity-Animator深入系列---目标匹配Target Matching
回到 Animator深入系列总目录 一开始会理所当然的觉得,匹配是这样的: 但结果却是这样的(右边的Cube是匹配目标): 感觉这个接口应该是专门为攀爬之类的动画准备的,属于被动匹配位置,移动整个对 ...
- [Leetcode 44]通配符匹配Wildcard Matching
[题目] 匹配通配符*,?,DP动态规划,重点是*的两种情况 想象成两个S.P长度的字符串,P匹配S. S中不会出现通配符. [条件] (1)P=null,S=null,TRUE (2)P=null, ...
- [Swift]LeetCode1023. 驼峰式匹配 | Camelcase Matching
A query word matches a given pattern if we can insert lowercase letters to the pattern word so that ...
随机推荐
- Python—面向对象04 绑定方法
坚持把梳理的知识都给记下来....... 嗯哼哼 1.绑定方法与非绑定方法 在类内部定义的函数,分为两大类: 绑定到类的方法:用classmethod装饰器装饰的方法. 为类量身定制 类.boud_m ...
- SqlSugar之DbContext
创建一个DbContext和DbSet进行使用,我们可以在DbSet中进行扩展我们的方法 //可以直接用SimpleClient也可以扩展一个自个的类 //推荐直接用 SimpleClient //为 ...
- Emmet插件使用
目录 Emmet插件使用 1.生成html5文档 2.header部分 3.body部分 Emmet插件使用 标签(空格分隔): php 前端 1.生成html5文档 html5:5 ! 2.head ...
- js实现图片点击弹出放大效果
点击图片,显示蒙板,放大图片的简单案例 HTML代码: <div> <img height=" src="https://img-blog.csdn.net/20 ...
- 剑指offer—从头到尾打印链表
输入一个链表,按链表值从尾到头的顺序返回一个ArrayList. 递归添加...不为空就加 import java.util.ArrayList; public class Solution { pu ...
- 微信小程序 嵌套循环
前言 入门教程之列表渲染多层嵌套循环,目前官方的文档里,主要是一维数组列表渲染的案例,还是比较简单单一,给刚入门的童鞋还是无从入手的感觉. <view wx:for="{{items} ...
- 21.1 XMLHttpRequest 对象【JavaScript高级程序设计第三版】
IE5 是第一款引入XHR 对象的浏览器.在IE5 中,XHR 对象是通过MSXML 库中的一个ActiveX对象实现的.因此,在IE 中可能会遇到三种不同版本的XHR 对象,即MSXML2.XMLH ...
- 10.1.5 Comment类型【JavaScript高级程序设计第三版】
注释在DOM中是通过Comment 类型来表示的.Comment 节点具有下列特征: nodeType 的值为8: nodeName 的值为"#comment": nodeValu ...
- LINUX SSH 建立密钥对
配置私钥和公钥 先检查一下服务器的ssh配置文件 /etc/ssh/sshd_config RSAAuthentication yes # 启用 RSA 认证 默认为 yes PubkeyAuthen ...
- Hadoop(11)-MapReduce概述和简单实操
1.MapReduce的定义 2.MapReduce的优缺点 优点 缺点 3.MapReduce的核心思想 4.MapReduce进程 5.常用数据序列化类型 6.MapReduce的编程规范 用户编 ...