單身狗脫單算法 脫單了算你贏

問題背景

需要考慮的問題

算法描述

比較懶,怎麼記錄方便我就怎麼來了,於是隨手粘貼了圖片

數據結構

1.將男士和女士對異性好感的排序表抽象成一個二維數組MP和WP
2.創建兩個數組用來記錄男士和女士是否單身,其中索引表示第幾位男士,存儲的值表示是否脫單。例如:

isManFree[0] = True # 表示第0位男士還是單身

3.設計男士對女士的追求表,記錄男士有沒有追過這位女士,二維表中女士表示橫軸,男士表示縱軸

isManProposed[0][1] = True # 0號男士已經追求過1號女士

4.匹配對的存儲match,match列表中存儲的元素是一個個元組(m,w),match的索引表示男士的id

match[0] = (0,1) #0號男士和1號女士匹配 元組中的第一位爲男士,第二位是女士,且男士的id==match的索引

Python實現

def stable_match(MP,WP):
    n = len(MP)
    isManFree = [True for i in range(n)] # 設置男士都爲單身
    isWomanFree = [True for i in range(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]: # 如果該男士還存在沒有求過婚的女士
            for i in range(n):
                w = MP[indexM][i]
                if not isManProposed[indexM][w]:
                    indexW = w 
                    break
            isManProposed[indexM][indexW] = True # 設置這位男士追求過這位女士
            if isWomanFree[indexW]: # 女士還是單身
                isWomanFree[indexW] = False #開始約會了!脫單
                isManFree[indexM] = False #脫單
                match[indexM] = (indexM,indexW) #生成一組匹配
            else:# 被追求的女士早就名花有主,看男士的魅力大不大了
                indexM_ = -1 # 該女士的現任,先賦值爲-1
                for j in range(n):
                    if match[j][1]==indexW: # 這位女士已經名花有主,找到她的主
                        indexM_ = j
                        break
                if WP[indexW].index(indexM) < WP[indexW].index(indexM_):# 在這位女士的優先列表中判斷,看後來的男士是否更加具有吸引力
                    isManFree[indexM_] = True
                    isManFree[indexM] = False
                    match[indexM] = (indexM,indexW)

    print(match)
    return match

def test():
    MP = [
        [0,1],
        [1,0],
    ]
    WP = [
        [1,0],
        [0,1],
    ]
    stable_match(MP,WP)

def test2():
    MP = [
        [1,0,2],
        [0,2,1],
        [1,2,0],
    ]
    WP = [
        [0,2,1],
        [1,0,2],
        [0,1,2],
    ]
    stable_match(MP,WP)



if __name__ == "__main__":
    test()
    test2()
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章