Ask Your Question
1

What is the procedure for carrying out the Gale-Shapely algorithm using Python?

asked 2022-01-31 11:00:00 +0000

nofretete gravatar image

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
0

answered 2022-12-27 20:00:00 +0000

bukephalos gravatar image

The Gale-Shapely algorithm, also known as the stable marriage algorithm, is used to solve matching problems. Here is an example of how to carry out the Gale-Shapely algorithm using Python:

Step 1: Initialize preferences Create two lists representing the preferences of two groups of individuals (for example, a list of male preferences and a list of female preferences). Each list should be a list of lists, where the order of the individuals in each sub-list represents their preferences. For example:

male_preferences = [['alice', 'betty', 'claire'], ['betty', 'claire', 'alice'], ['claire', 'alice', 'betty']]
female_preferences = [['charles', 'david', 'edward'], ['david', 'edward', 'charles'], ['edward', 'charles', 'david']]

Step 2: Initialize matchings Create two dictionaries representing the current matchings of the two groups. In each dictionary, the keys should be the individuals from one group, and the values should be the individuals from the other group they are currently matched with (or None if they are currently unmatched). For example:

male_matchings = {'alice': None, 'betty': None, 'claire': None}
female_matchings = {'charles': None, 'david': None, 'edward': None}

Step 3: Run the algorithm While there are still unmatched individuals in either group, repeat the following steps: - For each unmatched male individual, propose to the next female on his list that he has not already proposed to. - For each female individual that receives a proposal, consider the proposal and choose between her current matching (if she has one) and the new proposal based on her preference list. - If a female chooses a new matching, update the male and female matchings dictionaries accordingly. - If a female rejects a proposal, the male will move on to propose to his next choice.

Here is the Python code to implement the algorithm:

while None in male_matchings.values():
    for man, preference_list in zip(male_matchings.keys(), male_preferences):
        if male_matchings[man] is None:
            woman = preference_list.pop(0)
            female_match = female_matchings[woman]
            if female_match is None:
                male_matchings[man] = woman
                female_matchings[woman] = man
            else:
                if female_preferences[woman.index(man)].index(man) < female_preferences[woman.index(female_match)].index(female_match):
                    male_matchings[man] = woman
                    male_matchings[female_match] = None
                    female_matchings[woman] = man

print(male_matchings)
print(female_matchings)

The output will be the final matchings of the two groups.

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account. This space is reserved only for answers. If you would like to engage in a discussion, please instead post a comment under the question or an answer that you would like to discuss

Add Answer


Question Tools

Stats

Asked: 2022-01-31 11:00:00 +0000

Seen: 7 times

Last updated: Dec 27 '22