gale_shapley_algorithm
¶
gale-shapley-algorithm: A Python implementation of the Gale-Shapley algorithm.
Modules:
-
algorithm–Algorithm module.
-
matching–Convenience function for creating matchings.
-
person–Person module.
-
result–Result types for the Gale-Shapley algorithm.
-
stability–Stability analysis for matchings.
Classes:
-
Algorithm–Gale-Shapley Algorithm class.
-
MatchingResult–Result of running the Gale-Shapley algorithm.
-
Person–Person class, base class for Proposer and Responder.
-
Proposer–Proposer class, subclass of Person.
-
Responder–Responder class, subclass of Person.
-
StabilityResult–Result of a stability check on a matching.
Functions:
-
check_stability–Check the stability of an algorithm's matching.
-
create_matching–Create a matching from preference dictionaries.
-
find_blocking_pairs–Find all blocking pairs in a matching.
-
is_individually_rational–Check if the matching is individually rational.
Algorithm
dataclass
¶
Gale-Shapley Algorithm class.
Uses slots for memory efficiency.
Methods:
-
execute–Run the algorithm and return structured results.
-
format_all_preferences–Format preferences of all proposers and responders as a string.
-
format_matches–Format all matches as a string.
-
proposers_propose–Makes all unmatched proposers propose to their next choice.
-
responders_respond–Makes all responders that are awaiting to respond respond.
-
terminate–Returns True if all proposers are matched, False otherwise.
Attributes:
-
awaiting_to_respond_responders(list[Responder]) –Returns responders that are awaiting to respond.
-
persons(list[Proposer | Responder]) –Returns all proposers and responders.
-
unmatched_proposers(list[Proposer]) –Returns unmatched proposers, excludes self matches.
awaiting_to_respond_responders
property
¶
Returns responders that are awaiting to respond.
unmatched_proposers
property
¶
Returns unmatched proposers, excludes self matches.
execute
¶
execute() -> MatchingResult
Run the algorithm and return structured results.
Returns:
-
MatchingResult–MatchingResult with rounds, matches, unmatched, self_matches, all_matched.
Source code in src/gale_shapley_algorithm/algorithm.py
112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 | |
format_all_preferences
¶
Format preferences of all proposers and responders as a string.
Parameters:
Source code in src/gale_shapley_algorithm/algorithm.py
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 | |
format_matches
¶
format_matches() -> str
Format all matches as a string.
Source code in src/gale_shapley_algorithm/algorithm.py
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 | |
proposers_propose
¶
proposers_propose() -> None
Makes all unmatched proposers propose to their next choice.
Source code in src/gale_shapley_algorithm/algorithm.py
36 37 38 39 | |
responders_respond
¶
responders_respond() -> None
Makes all responders that are awaiting to respond respond.
Source code in src/gale_shapley_algorithm/algorithm.py
41 42 43 44 | |
terminate
¶
terminate() -> bool
Returns True if all proposers are matched, False otherwise.
Source code in src/gale_shapley_algorithm/algorithm.py
46 47 48 | |
MatchingResult
dataclass
¶
MatchingResult(rounds: int, matches: dict[str, str], unmatched: list[str], self_matches: list[str], all_matched: bool)
Result of running the Gale-Shapley algorithm.
Person
¶
Person class, base class for Proposer and Responder.
Methods:
-
format_preferences–Format the preferences of the person as a string, * indicates acceptable.
-
is_acceptable–Check if person is acceptable (ranked at or above self in preferences).
Attributes:
-
is_matched(bool) –Returns True if the person is matched to someone or self, False if match is None.
Source code in src/gale_shapley_algorithm/person.py
13 14 15 16 17 | |
is_matched
property
writable
¶
is_matched: bool
Returns True if the person is matched to someone or self, False if match is None.
format_preferences
¶
format_preferences() -> str
Format the preferences of the person as a string, * indicates acceptable.
Source code in src/gale_shapley_algorithm/person.py
42 43 44 45 46 47 48 49 50 | |
is_acceptable
¶
Check if person is acceptable (ranked at or above self in preferences).
Parameters:
Raises:
-
ValueError–If person is not in preferences.
Returns:
-
bool–True if person is acceptable, False otherwise.
Source code in src/gale_shapley_algorithm/person.py
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 | |
Proposer
¶
Bases: Person
Proposer class, subclass of Person.
Methods:
-
format_preferences–Format the preferences of the person as a string, * indicates acceptable.
-
is_acceptable–Check if person is acceptable (ranked at or above self in preferences).
-
propose–Propose to the next acceptable responder. If self is next, set match to self.
Attributes:
-
acceptable_to_propose(tuple[Responder | Proposer, ...]) –Returns a tuple of acceptable responders to propose to.
-
is_matched(bool) –Returns True if the person is matched to someone or self, False if match is None.
-
next_proposal(Responder | Proposer) –Returns the next acceptable responder to propose to, or self if exhausted.
Source code in src/gale_shapley_algorithm/person.py
69 70 71 | |
acceptable_to_propose
property
¶
Returns a tuple of acceptable responders to propose to.
is_matched
property
writable
¶
is_matched: bool
Returns True if the person is matched to someone or self, False if match is None.
next_proposal
property
¶
Returns the next acceptable responder to propose to, or self if exhausted.
format_preferences
¶
format_preferences() -> str
Format the preferences of the person as a string, * indicates acceptable.
Source code in src/gale_shapley_algorithm/person.py
42 43 44 45 46 47 48 49 50 | |
is_acceptable
¶
Check if person is acceptable (ranked at or above self in preferences).
Parameters:
Raises:
-
ValueError–If person is not in preferences.
Returns:
-
bool–True if person is acceptable, False otherwise.
Source code in src/gale_shapley_algorithm/person.py
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 | |
propose
¶
propose() -> None
Propose to the next acceptable responder. If self is next, set match to self.
Source code in src/gale_shapley_algorithm/person.py
90 91 92 93 94 95 96 97 | |
Responder
¶
Bases: Person
Responder class, subclass of Person.
Methods:
-
format_preferences–Format the preferences of the person as a string, * indicates acceptable.
-
is_acceptable–Check if person is acceptable (ranked at or above self in preferences).
-
respond–Respond to proposals and clear the current_proposals.
Attributes:
-
acceptable_proposals(list[Proposer]) –Returns a list of acceptable proposals among the current proposals.
-
awaiting_to_respond(bool) –Returns True if current_proposals is not empty.
-
is_matched(bool) –Returns True if the person is matched to someone or self, False if match is None.
Source code in src/gale_shapley_algorithm/person.py
103 104 105 | |
acceptable_proposals
property
¶
Returns a list of acceptable proposals among the current proposals.
awaiting_to_respond
property
¶
awaiting_to_respond: bool
Returns True if current_proposals is not empty.
is_matched
property
writable
¶
is_matched: bool
Returns True if the person is matched to someone or self, False if match is None.
format_preferences
¶
format_preferences() -> str
Format the preferences of the person as a string, * indicates acceptable.
Source code in src/gale_shapley_algorithm/person.py
42 43 44 45 46 47 48 49 50 | |
is_acceptable
¶
Check if person is acceptable (ranked at or above self in preferences).
Parameters:
Raises:
-
ValueError–If person is not in preferences.
Returns:
-
bool–True if person is acceptable, False otherwise.
Source code in src/gale_shapley_algorithm/person.py
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 | |
respond
¶
respond() -> None
Respond to proposals and clear the current_proposals.
Source code in src/gale_shapley_algorithm/person.py
127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 | |
StabilityResult
dataclass
¶
StabilityResult(is_stable: bool, is_individually_rational: bool, blocking_pairs: list[tuple[str, str]])
Result of a stability check on a matching.
check_stability
¶
check_stability(algorithm: Algorithm) -> StabilityResult
Check the stability of an algorithm's matching.
Parameters:
Returns:
-
StabilityResult–StabilityResult with is_stable, is_individually_rational, and blocking_pairs.
Source code in src/gale_shapley_algorithm/stability.py
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 | |
create_matching
¶
create_matching(proposer_preferences: dict[str, list[str]], responder_preferences: dict[str, list[str]]) -> MatchingResult
Create a matching from preference dictionaries.
This is the simplest way to use the library. Provide preference rankings for each side and get back a MatchingResult.
Persons not listed in a preference list are considered unacceptable. Each person is automatically added to their own preference list at the end (making everyone they listed acceptable).
Parameters:
-
(proposer_preferences¶dict[str, list[str]]) –Mapping of proposer names to ordered list of responder names.
-
(responder_preferences¶dict[str, list[str]]) –Mapping of responder names to ordered list of proposer names.
Returns:
-
MatchingResult–MatchingResult with the matching outcome.
Example
result = create_matching( ... proposer_preferences={ ... "alice": ["bob", "charlie"], ... "dave": ["charlie", "bob"], ... }, ... responder_preferences={ ... "bob": ["alice", "dave"], ... "charlie": ["dave", "alice"], ... }, ... ) result.matches
Source code in src/gale_shapley_algorithm/matching.py
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 | |
find_blocking_pairs
¶
find_blocking_pairs(proposers: list[Proposer], responders: list[Responder]) -> list[tuple[str, str]]
Find all blocking pairs in a matching.
A blocking pair (p, r) exists when proposer p and responder r both prefer each other over their current matches.
Parameters:
-
(proposers¶list[Proposer]) –List of proposers in the matching.
-
(responders¶list[Responder]) –List of responders in the matching.
Returns:
Source code in src/gale_shapley_algorithm/stability.py
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 | |
is_individually_rational
¶
Check if the matching is individually rational.
A matching is individually rational if every matched person finds their match acceptable (ranked at or above themselves).
Parameters:
-
(proposers¶list[Proposer]) –List of proposers in the matching.
-
(responders¶list[Responder]) –List of responders in the matching.
Returns:
-
bool–True if individually rational, False otherwise.
Source code in src/gale_shapley_algorithm/stability.py
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | |