std::vector<std::vector<std::uint32_t>>& secondary_preferences,
48 conststd::vector<std::vector<std::uint32_t>>& primary_preferences) {
49std::uint32_t num_elements = secondary_preferences.size();
50std::vector<std::uint32_t> matches(num_elements, -1);
51std::vector<bool> is_free_primary(num_elements,
true);
52std::vector<std::uint32_t> proposal_index(
57 intfree_primary_index = -1;
60 for(std::uint32_t i = 0; i < num_elements; i++) {
61 if(is_free_primary[i]) {
62free_primary_index = i;
68 if(free_primary_index == -1)
72std::uint32_t secondary_to_propose =
73primary_preferences[free_primary_index]
74[proposal_index[free_primary_index]];
75proposal_index[free_primary_index]++;
78std::uint32_t current_match = matches[secondary_to_propose];
81 if(current_match == -1) {
82matches[secondary_to_propose] = free_primary_index;
83is_free_primary[free_primary_index] =
false;
86 autonew_proposer_rank =
87std::find(secondary_preferences[secondary_to_propose].begin(),
88secondary_preferences[secondary_to_propose].end(),
90 autocurrent_match_rank =
91std::find(secondary_preferences[secondary_to_propose].begin(),
92secondary_preferences[secondary_to_propose].end(),
96 if(new_proposer_rank < current_match_rank) {
97matches[secondary_to_propose] = free_primary_index;
98is_free_primary[free_primary_index] =
false;
99is_free_primary[current_match] =
116std::vector<std::vector<std::uint32_t>> primary_preferences = {
117{0, 1, 2, 3}, {2, 1, 3, 0}, {1, 2, 0, 3}, {3, 0, 1, 2}};
118std::vector<std::vector<std::uint32_t>> secondary_preferences = {
119{1, 0, 2, 3}, {3, 0, 1, 2}, {0, 2, 1, 3}, {1, 2, 0, 3}};
121secondary_preferences, primary_preferences) ==
122std::vector<std::uint32_t>({0, 2, 1, 3}));
125primary_preferences = {
126{0, 2, 1, 3}, {2, 3, 0, 1}, {3, 1, 2, 0}, {2, 1, 0, 3}};
127secondary_preferences = {
128{1, 0, 2, 3}, {3, 0, 1, 2}, {0, 2, 1, 3}, {1, 2, 0, 3}};
130secondary_preferences, primary_preferences) ==
131std::vector<std::uint32_t>({0, 3, 1, 2}));
134primary_preferences = {{0, 1, 2}, {2, 1, 0}, {1, 2, 0}};
135secondary_preferences = {{1, 0, 2}, {2, 0, 1}, {0, 2, 1}};
137secondary_preferences, primary_preferences) ==
138std::vector<std::uint32_t>({0, 2, 1}));
141primary_preferences = {};
142secondary_preferences = {};
144secondary_preferences, primary_preferences) ==
145std::vector<std::uint32_t>({}));
static void tests()
Self-test implementations.
int main()
Main function.
Functions for the Gale-Shapley Algorithm.
std::vector< std::uint32_t > gale_shapley(const std::vector< std::vector< std::uint32_t > > &secondary_preferences, const std::vector< std::vector< std::uint32_t > > &primary_preferences)
The main function that finds the stable matching between two sets of elements using the Gale-Shapley ...
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4