15
15
* @author [B Karthik](https://github.com/BKarthik7)
16
16
*/
17
17
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
22
22
23
23
/* *
24
24
* @namespace
@@ -39,17 +39,17 @@ namespace stable_matching {
39
39
* @param secondary_preferences the preferences of the secondary set should be a 2D vector
40
40
* @returns matches the stable matching between the two sets
41
41
*/
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 );
45
45
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
47
47
48
48
while (true ) {
49
49
int free_primary_index = -1 ;
50
50
51
51
// 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++) {
53
53
if (is_free_primary[i]) {
54
54
free_primary_index = i;
55
55
break ;
@@ -60,11 +60,11 @@ std::vector<unsigned int> gale_shapley(const std::vector<std::vector<unsigned in
60
60
if (free_primary_index == -1 ) break ;
61
61
62
62
// 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]];
64
64
proposal_index[free_primary_index]++;
65
65
66
66
// 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];
68
68
69
69
// If the secondary is free, match them
70
70
if (current_match == -1 ) {
@@ -99,24 +99,24 @@ std::vector<unsigned int> gale_shapley(const std::vector<std::vector<unsigned in
99
99
*/
100
100
static void tests () {
101
101
// 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 }));
105
105
106
106
// Test Case 2
107
107
primary_preferences = {{0 , 2 , 1 , 3 }, {2 , 3 , 0 , 1 }, {3 , 1 , 2 , 0 }, {2 , 1 , 0 , 3 }};
108
108
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 }));
110
110
111
111
// Test Case 3
112
112
primary_preferences = {{0 , 1 , 2 }, {2 , 1 , 0 }, {1 , 2 , 0 }};
113
113
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 }));
115
115
116
116
// Test Case 4
117
117
primary_preferences = {};
118
118
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 >({}));
120
120
}
121
121
122
122
/* *
0 commit comments