这篇文章将会对稳定匹配算法进行介绍及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的更多相关文章

  1. Stable Matching 稳定匹配 婚姻算法 shapley 算法

    作者:jostree  转载请注明出处 http://www.cnblogs.com/jostree/p/4051286.html 稳定匹配问题:有N男N女,每个人对于异性都一个排名,先需要得到一种稳 ...

  2. 完美匹配(matching)

    完美匹配(matching) 题目描述 给定nn个点,mm条边的无向图G=(V,E)G=(V,E),求出它的完美匹配数量对106+3106+3取模的值. 一个完美匹配可以用一个排列ϕ:V→Vϕ:V→V ...

  3. Stable Matching (Gale Sharpley Algorithm)

    稳定婚配问题:n个男生n个女生.当中每一个人都有自己心仪的列表. 问怎样达成稳定的匹配(比方, b想B求婚,可是B已有的对象的优先级高于b,此时b的魅力不足以拆散B所处的那一对,即达到稳定状态.) ( ...

  4. UVALive 3989 Ladies' Choice(稳定婚姻问题:稳定匹配、合作博弈)

    题意:男女各n人,进行婚配,对于每个人来说,所有异性都存在优先次序,即最喜欢某人,其次喜欢某人...输出一个稳定婚配方案.所谓稳定,就是指未结婚的一对异性,彼此喜欢对方的程度都胜过自己的另一半,那么这 ...

  5. [Swift]LeetCode44. 通配符匹配 | Wildcard Matching

    Given an input string (s) and a pattern (p), implement wildcard pattern matching with support for '? ...

  6. 阅读笔记——长文本匹配《Matching Article Pairs with Graphical Decomposition and Convolutions》

    论文题目:Matching Article Pairs with Graphical Decomposition and Convolutions 发表情况:ACL2019 腾讯PCG小组 模型简介 ...

  7. Unity-Animator深入系列---目标匹配Target Matching

    回到 Animator深入系列总目录 一开始会理所当然的觉得,匹配是这样的: 但结果却是这样的(右边的Cube是匹配目标): 感觉这个接口应该是专门为攀爬之类的动画准备的,属于被动匹配位置,移动整个对 ...

  8. [Leetcode 44]通配符匹配Wildcard Matching

    [题目] 匹配通配符*,?,DP动态规划,重点是*的两种情况 想象成两个S.P长度的字符串,P匹配S. S中不会出现通配符. [条件] (1)P=null,S=null,TRUE (2)P=null, ...

  9. [Swift]LeetCode1023. 驼峰式匹配 | Camelcase Matching

    A query word matches a given pattern if we can insert lowercase letters to the pattern word so that ...

随机推荐

  1. 188. Best Time to Buy and Sell Stock IV——LeetCode

    Say you have an array for which the ith element is the price of a given stock on day i. Design an al ...

  2. C#4.0 System.Dynamic

    System.Dynamic 命名空间提供支持动态语言运行时的类和接口. 代码下载地址:GitHub 本篇主要讲两个类:①ExpandoObject   ②DynamicObject  (重点) ①E ...

  3. Java基础——线程复习总结

                                                                                                    线程 T ...

  4. rm -f + 文件名+* 与 rm -f + 文件名* 的不同效果,大坑呀。

    rm -f catalina.2018-10-22.*    与*号间无空格 rm -f catalina.2018-10-22. *    :多了空格:

  5. eclipse 突然debug模式不能正常运行了

    eclipse 突然debug模式不能正常运行了,但非debug模式却能正常运行.debug模式不能正常启动的现象描述如下:    点击eclipse debug按钮,console窗口显示tomca ...

  6. oracle12c管理作业资源的一种方式

    数据库:12.1.0.2,rac,cdb模式 笔者负责移动两个12.1.0.2的cdb集群,一个在aix上,一个在linux上,不幸的是,它们都是混合型,数据有100多T. 由于其它部门交付的时候,已 ...

  7. php常见面试(Smarty及相关知识)

    1.Smarty简介: 是一种php模板引擎,它分开了后台程序和前台页面内容,使程序员和美工分工合作.还有其它模版引擎实现同样的目的. 2.Smarty使用: 建立php文件: 包含smarty类文件 ...

  8. 12.2.1 访问元素的样式【JavaScript高级程序设计第三版】

    任何支持style 特性的HTML 元素在JavaScript 中都有一个对应的style 属性.这个style 对象是CSSStyleDeclaration 的实例,包含着通过HTML 的style ...

  9. 双击 ajax修改单元格里的值

    最终效果 列表页面表格里双击排序修改其值 按钮样式要引入bootstrap才可以用 本文件用的是laravel框架环境 larave路由里 Route::get('category/changesta ...

  10. Delphi初始化与结束化

    unit Unit1; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms ...