-
-
Notifications
You must be signed in to change notification settings - Fork 46.8k
Gale Shapley Algorithm #2100
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Gale Shapley Algorithm #2100
Changes from 1 commit
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
c6095bf
Gale Shapley Algorithm
akashvshroff b05ee58
Update graphs/gale_shapley_bigraph.py
akashvshroff fe8a7a2
Fixed some flake8 issues.
akashvshroff e196934
Updated it to donors and recipients
akashvshroff c18c55e
description changes
akashvshroff 9443cc4
description changes
akashvshroff 5567fc5
description changes
akashvshroff 054dff2
Edited the line lengths
akashvshroff 312b87d
Update gale_shapley_bigraph.py
cclauss 6883cd5
Update gale_shapley_bigraph.py
cclauss File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
def stable_matching(n: int, men_preferences: list, women_preferences: list) -> list: | ||
''' | ||
Finds the stable match in any bipartite graph, i.e a pairing where no 2 objects prefer each other over their partner. | ||
Here marriage is used to make the variable names easier and so the algorithm can be intuitively understood. | ||
The function accepts the preferences of the men and women (where both are named from 0 to n-1) and returns a list where index | ||
position corresponds to the man and value at the index is the woman he is marrying. | ||
E.g: | ||
n = 4 | ||
men_preferences = [[0, 1, 3, 2], [0, 2, 3, 1], [1, 0, 2, 3], [0, 3, 1, 2]] | ||
women_preferences = [[3, 1, 2, 0], [3, 1, 0, 2], [0, 3, 1, 2], [1, 0, 3, 2]] | ||
>>>print(stable_matching(n,men_preferences,women_preferences)) | ||
[1,2,3,0] | ||
P.S: Marriages are heterosexual since it is a bipartite graph where there must be 2 distinct sets of objects to be matched - i.e | ||
patients and organ donors. | ||
To better understand the algorithm, see also: | ||
https://github.com/akashvshroff/Gale_Shapley_Stable_Matching (Detailed README). | ||
https://www.youtube.com/watch?v=Qcv1IqHWAzg&t=13s (Numberphile YouTube Video). | ||
''' | ||
unmarried_men = [i for i in range(n)] | ||
man_spouse = [None for i in range(n)] | ||
woman_spouse = [None for i in range(n)] | ||
num_proposals = [0 for i in range(n)] | ||
while unmarried_men: | ||
man = unmarried_men[0] | ||
his_preferences = men_preferences[man] | ||
woman = his_preferences[num_proposals[man]] | ||
num_proposals[man] += 1 | ||
her_preferences = women_preferences[woman] | ||
husb = woman_spouse[woman] | ||
if husb != None: | ||
if her_preferences.index(husb) > her_preferences.index(man): | ||
woman_spouse[woman], man_spouse[man] = man, woman | ||
unmarried_men.append(husb) | ||
unmarried_men.remove(man) | ||
else: | ||
woman_spouse[woman], man_spouse[man] = man, woman | ||
unmarried_men.remove(man) | ||
return man_spouse |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.