Skip to content

Commit ca2c57a

Browse files
committed
Change type
1 parent bb17cb8 commit ca2c57a

File tree

1 file changed

+13
-13
lines changed

1 file changed

+13
-13
lines changed

greedy_algorithms/gale_shapley.cpp

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -41,17 +41,17 @@ namespace greedy_algorithms {
4141
*/
4242

4343
namespace stable_matching {
44-
std::vector<int> gale_shapley(const std::vector<std::vector<int>>& secondary_preferences, const std::vector<std::vector<int>>& primary_preferences) {
45-
int num_elements = secondary_preferences.size();
46-
std::vector<int> matches(num_elements, -1);
44+
std::vector<unsigned int> gale_shapley(const std::vector<std::vector<unsigned int>>& secondary_preferences, const std::vector<std::vector<unsigned int>>& primary_preferences) {
45+
unsigned int num_elements = secondary_preferences.size();
46+
std::vector<unsigned int> matches(num_elements, -1);
4747
std::vector<bool> is_free_primary(num_elements, true);
48-
std::vector<int> proposal_index(num_elements, 0); // Tracks the next secondary to propose for each primary
48+
std::vector<unsigned int> proposal_index(num_elements, 0); // Tracks the next secondary to propose for each primary
4949

5050
while (true) {
5151
int free_primary_index = -1;
5252

5353
// Find the next free primary
54-
for (int i = 0; i < num_elements; i++) {
54+
for (unsigned int i = 0; i < num_elements; i++) {
5555
if (is_free_primary[i]) {
5656
free_primary_index = i;
5757
break;
@@ -62,11 +62,11 @@ std::vector<int> gale_shapley(const std::vector<std::vector<int>>& secondary_pre
6262
if (free_primary_index == -1) break;
6363

6464
// Get the next secondary to propose
65-
int secondary_to_propose = primary_preferences[free_primary_index][proposal_index[free_primary_index]];
65+
unsigned int secondary_to_propose = primary_preferences[free_primary_index][proposal_index[free_primary_index]];
6666
proposal_index[free_primary_index]++;
6767

6868
// Get the current match of the secondary
69-
int current_match = matches[secondary_to_propose];
69+
unsigned int current_match = matches[secondary_to_propose];
7070

7171
// If the secondary is free, match them
7272
if (current_match == -1) {
@@ -101,24 +101,24 @@ std::vector<int> gale_shapley(const std::vector<std::vector<int>>& secondary_pre
101101
*/
102102
static void tests() {
103103
// Test Case 1
104-
std::vector<std::vector<int>> primary_preferences = {{0, 1, 2, 3}, {2, 1, 3, 0}, {1, 2, 0, 3}, {3, 0, 1, 2}};
105-
std::vector<std::vector<int>> secondary_preferences = {{1, 0, 2, 3}, {3, 0, 1, 2}, {0, 2, 1, 3}, {1, 2, 0, 3}};
106-
assert(greedy_algorithms::stable_matching::gale_shapley(secondary_preferences, primary_preferences) == std::vector<int>({0, 2, 1, 3}));
104+
std::vector<std::vector<unsigned int>> primary_preferences = {{0, 1, 2, 3}, {2, 1, 3, 0}, {1, 2, 0, 3}, {3, 0, 1, 2}};
105+
std::vector<std::vector<unsigned int>> secondary_preferences = {{1, 0, 2, 3}, {3, 0, 1, 2}, {0, 2, 1, 3}, {1, 2, 0, 3}};
106+
assert(greedy_algorithms::stable_matching::gale_shapley(secondary_preferences, primary_preferences) == std::vector<unsigned int>({0, 2, 1, 3}));
107107

108108
// Test Case 2
109109
primary_preferences = {{0, 2, 1, 3}, {2, 3, 0, 1}, {3, 1, 2, 0}, {2, 1, 0, 3}};
110110
secondary_preferences = {{1, 0, 2, 3}, {3, 0, 1, 2}, {0, 2, 1, 3}, {1, 2, 0, 3}};
111-
assert(greedy_algorithms::stable_matching::gale_shapley(secondary_preferences, primary_preferences) == std::vector<int>({0, 3, 1, 2}));
111+
assert(greedy_algorithms::stable_matching::gale_shapley(secondary_preferences, primary_preferences) == std::vector<unsigned int>({0, 3, 1, 2}));
112112

113113
// Test Case 3
114114
primary_preferences = {{0, 1, 2}, {2, 1, 0}, {1, 2, 0}};
115115
secondary_preferences = {{1, 0, 2}, {2, 0, 1}, {0, 2, 1}};
116-
assert(greedy_algorithms::stable_matching::gale_shapley(secondary_preferences, primary_preferences) == std::vector<int>({0, 2, 1}));
116+
assert(greedy_algorithms::stable_matching::gale_shapley(secondary_preferences, primary_preferences) == std::vector<unsigned int>({0, 2, 1}));
117117

118118
// Test Case 4
119119
primary_preferences = {};
120120
secondary_preferences = {};
121-
assert(greedy_algorithms::stable_matching::gale_shapley(secondary_preferences, primary_preferences) == std::vector<int>({}));
121+
assert(greedy_algorithms::stable_matching::gale_shapley(secondary_preferences, primary_preferences) == std::vector<unsigned int>({}));
122122
}
123123

124124
/**

0 commit comments

Comments
 (0)