1515 * @author [B Karthik](https://github.com/BKarthik7)
1616 */
1717
18- #include < iostream>
19- #include < vector>
20- #include < algorithm>
21- #include < cassert>
18+ #include < iostream> // / for std::u32int_t
19+ #include < vector> // / for std::vector
20+ #include < algorithm> // / for std::find
21+ #include < cassert> // / for assert
2222
2323/* *
2424 * @namespace
@@ -39,17 +39,17 @@ namespace stable_matching {
3939 * @param secondary_preferences the preferences of the secondary set should be a 2D vector
4040 * @returns matches the stable matching between the two sets
4141 */
42- std::vector<unsigned int > gale_shapley (const std::vector<std::vector<unsigned int >>& secondary_preferences, const std::vector<std::vector<unsigned int >>& primary_preferences) {
43- unsigned int num_elements = secondary_preferences.size ();
44- std::vector<unsigned int > matches (num_elements, -1 );
42+ 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) {
43+ std:: uint32_t num_elements = secondary_preferences.size ();
44+ std::vector<std:: uint32_t > matches (num_elements, -1 );
4545 std::vector<bool > is_free_primary (num_elements, true );
46- std::vector<unsigned int > proposal_index (num_elements, 0 ); // Tracks the next secondary to propose for each primary
46+ std::vector<std:: uint32_t > proposal_index (num_elements, 0 ); // Tracks the next secondary to propose for each primary
4747
4848 while (true ) {
4949 int free_primary_index = -1 ;
5050
5151 // Find the next free primary
52- for (unsigned int i = 0 ; i < num_elements; i++) {
52+ for (std:: uint32_t i = 0 ; i < num_elements; i++) {
5353 if (is_free_primary[i]) {
5454 free_primary_index = i;
5555 break ;
@@ -60,11 +60,11 @@ std::vector<unsigned int> gale_shapley(const std::vector<std::vector<unsigned in
6060 if (free_primary_index == -1 ) break ;
6161
6262 // Get the next secondary to propose
63- unsigned int secondary_to_propose = primary_preferences[free_primary_index][proposal_index[free_primary_index]];
63+ std:: uint32_t secondary_to_propose = primary_preferences[free_primary_index][proposal_index[free_primary_index]];
6464 proposal_index[free_primary_index]++;
6565
6666 // Get the current match of the secondary
67- unsigned int current_match = matches[secondary_to_propose];
67+ std:: uint32_t current_match = matches[secondary_to_propose];
6868
6969 // If the secondary is free, match them
7070 if (current_match == -1 ) {
@@ -99,24 +99,24 @@ std::vector<unsigned int> gale_shapley(const std::vector<std::vector<unsigned in
9999 */
100100static void tests () {
101101 // Test Case 1
102- std::vector<std::vector<unsigned int >> primary_preferences = {{0 , 1 , 2 , 3 }, {2 , 1 , 3 , 0 }, {1 , 2 , 0 , 3 }, {3 , 0 , 1 , 2 }};
103- std::vector<std::vector<unsigned int >> secondary_preferences = {{1 , 0 , 2 , 3 }, {3 , 0 , 1 , 2 }, {0 , 2 , 1 , 3 }, {1 , 2 , 0 , 3 }};
104- assert (greedy_algorithms::stable_matching::gale_shapley (secondary_preferences, primary_preferences) == std::vector<unsigned int >({0 , 2 , 1 , 3 }));
102+ std::vector<std::vector<std:: uint32_t >> primary_preferences = {{0 , 1 , 2 , 3 }, {2 , 1 , 3 , 0 }, {1 , 2 , 0 , 3 }, {3 , 0 , 1 , 2 }};
103+ std::vector<std::vector<std:: uint32_t >> secondary_preferences = {{1 , 0 , 2 , 3 }, {3 , 0 , 1 , 2 }, {0 , 2 , 1 , 3 }, {1 , 2 , 0 , 3 }};
104+ assert (greedy_algorithms::stable_matching::gale_shapley (secondary_preferences, primary_preferences) == std::vector<std:: uint32_t >({0 , 2 , 1 , 3 }));
105105
106106 // Test Case 2
107107 primary_preferences = {{0 , 2 , 1 , 3 }, {2 , 3 , 0 , 1 }, {3 , 1 , 2 , 0 }, {2 , 1 , 0 , 3 }};
108108 secondary_preferences = {{1 , 0 , 2 , 3 }, {3 , 0 , 1 , 2 }, {0 , 2 , 1 , 3 }, {1 , 2 , 0 , 3 }};
109- assert (greedy_algorithms::stable_matching::gale_shapley (secondary_preferences, primary_preferences) == std::vector<unsigned int >({0 , 3 , 1 , 2 }));
109+ assert (greedy_algorithms::stable_matching::gale_shapley (secondary_preferences, primary_preferences) == std::vector<std:: uint32_t >({0 , 3 , 1 , 2 }));
110110
111111 // Test Case 3
112112 primary_preferences = {{0 , 1 , 2 }, {2 , 1 , 0 }, {1 , 2 , 0 }};
113113 secondary_preferences = {{1 , 0 , 2 }, {2 , 0 , 1 }, {0 , 2 , 1 }};
114- assert (greedy_algorithms::stable_matching::gale_shapley (secondary_preferences, primary_preferences) == std::vector<unsigned int >({0 , 2 , 1 }));
114+ assert (greedy_algorithms::stable_matching::gale_shapley (secondary_preferences, primary_preferences) == std::vector<std:: uint32_t >({0 , 2 , 1 }));
115115
116116 // Test Case 4
117117 primary_preferences = {};
118118 secondary_preferences = {};
119- assert (greedy_algorithms::stable_matching::gale_shapley (secondary_preferences, primary_preferences) == std::vector<unsigned int >({}));
119+ assert (greedy_algorithms::stable_matching::gale_shapley (secondary_preferences, primary_preferences) == std::vector<std:: uint32_t >({}));
120120}
121121
122122/* *
0 commit comments