求婚拒绝算法(GS算法)的Python实现

来源:互联网 发布:快速排序算法实例讲解 编辑:程序博客网 时间:2024/05/06 02:31

问题

给定一组男人和一组女人,每个人在心目中都对所有的异性有一个倾慕度排序,从最喜欢到最不喜欢依次排序1、2、3。现在给出问题,如何对这些男女进行配对使得在分配好后不出现偷情的现象。

算法

可以有男人优先和女人优先两种算法。以男人优先为例,为代码如下:

while  存在男人m是自由的且还没对每个女人都求过婚      选择这个男人m                令w是m的优先表中还没求过婚的最高排名的女人        if  w是自由的              (m,w)变成约会状态        else  w当前与m1约会              if  w更偏爱m1而不爱m                    m保持自由              else    w更偏爱m而不爱m1                   (m,w)变成约会状态                    m1变成自由              endif                  endifendwhile

需要特别注意的是!虽然从第9、10行来看,女人能够选择更好的伴侣。但从第3行来看,男人能够优先选择排名最高的女人。实际上,第9、10行只是为了保证婚姻的稳定性。

Python实现


# Homework 1, Problem 4: Implementing the Gale-Shapley algorithm# You are to code the function "find_stable_matching". DO NOT rename# the function. Details of the format of the input/output are given in# the function's comments. Edit this file and use blackboard to submit# the file along with your homework solutions.# Some basic testing for your code is provided below. DO NOT modify# these tests. Your code MUST pass these tests. Note, however, that# passing these tests does not guarantee that your algorithm is# correct. You should carry out more testing!# Use Python 2.x for your implementationdef find_stable_matching(MP,WP):    """ The input of this function is defined by two lists MP and WP    Men and women are numbered 0 through n-1    MP[i] encodes the ith man's preferences. It is simply a list    containing the numbers 0, 1, ... , n-1 in some order    WP[i] encodes the ith woman's preferences. It is simply a list    containing the numbers 0, 1,... , n-1 in some order    The output is defined by a list of pairs of the form (i,j)    indicating that man i is married to woman j    Your output should be the man-optimal stable matching found by the    GS-algorithm. """    # your code goes here!    n=len(MP)    isManFree=[True]*n    isWomanFree=[True]*n    #isManProposed=[[False]*n]*n    isManProposed=[[False for i in range(n)] for j in range(n)]    match=[(-1,-1)]*n    while(True in isManFree):        indexM=isManFree.index(True)        if(False in isManProposed[indexM]):            indexW=-1            for i in range(n):                w=MP[indexM][i]                if(not isManProposed[indexM][w]):                    indexW=w                    break            isManProposed[indexM][indexW]=True            if(isWomanFree[indexW]):                #isManProposed[indexM][indexW]=True                isWomanFree[indexW]=False                isManFree[indexM]=False                match[indexM]=(indexM,indexW)            else:                indexM1=-1                for j in range(n):                    if(match[j][1]==indexW):                        indexM1=j                        break                if(WP[indexW].index(indexM)<WP[indexW].index(indexM1)):                    isManFree[indexM1]=True                    isManFree[indexM]=False                    match[indexM]=(indexM,indexW)    print match    return matchdef test1():    """ basic test for your code """    MP = [ [0,1], [1,0] ]    WP = [ [0,1], [0,1] ]    SM = find_stable_matching(MP,WP)    assert ( (0,0) in SM and (1,1) in SM )def test2():    """ basic test for your code """    MP = [ [0,1], [0,1] ]    WP = [ [0,1], [0,1] ]    SM = find_stable_matching(MP,WP)    assert ( (0,0) in SM and (1,1) in SM )def test3():    """ basic test for your code """    MP = [ [0,1], [0,1] ]    WP = [ [1,0], [1,0] ]    SM = find_stable_matching(MP,WP)    assert ( (0,1) in SM and (1,0) in SM )if __name__=="__main__":    print "**********test1************"    test1()    print "**********test2************"    test2()    print "**********test3************"    test3()

面向对象的Python实现请参见这里