@@ -41,17 +41,17 @@ namespace greedy_algorithms {
41
41
*/
42
42
43
43
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 );
47
47
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
49
49
50
50
while (true ) {
51
51
int free_primary_index = -1 ;
52
52
53
53
// Find the next free primary
54
- for (int i = 0 ; i < num_elements; i++) {
54
+ for (unsigned int i = 0 ; i < num_elements; i++) {
55
55
if (is_free_primary[i]) {
56
56
free_primary_index = i;
57
57
break ;
@@ -62,11 +62,11 @@ std::vector<int> gale_shapley(const std::vector<std::vector<int>>& secondary_pre
62
62
if (free_primary_index == -1 ) break ;
63
63
64
64
// 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]];
66
66
proposal_index[free_primary_index]++;
67
67
68
68
// Get the current match of the secondary
69
- int current_match = matches[secondary_to_propose];
69
+ unsigned int current_match = matches[secondary_to_propose];
70
70
71
71
// If the secondary is free, match them
72
72
if (current_match == -1 ) {
@@ -101,24 +101,24 @@ std::vector<int> gale_shapley(const std::vector<std::vector<int>>& secondary_pre
101
101
*/
102
102
static void tests () {
103
103
// 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 }));
107
107
108
108
// Test Case 2
109
109
primary_preferences = {{0 , 2 , 1 , 3 }, {2 , 3 , 0 , 1 }, {3 , 1 , 2 , 0 }, {2 , 1 , 0 , 3 }};
110
110
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 }));
112
112
113
113
// Test Case 3
114
114
primary_preferences = {{0 , 1 , 2 }, {2 , 1 , 0 }, {1 , 2 , 0 }};
115
115
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 }));
117
117
118
118
// Test Case 4
119
119
primary_preferences = {};
120
120
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 >({}));
122
122
}
123
123
124
124
/* *
0 commit comments