-
-
Notifications
You must be signed in to change notification settings - Fork 7.6k
feat: Added gale_shapley.cpp in greedy_algorithms #2743
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 5 commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
321ff76
Added gale_shapley.cpp in greedy_algorithms
BKarthik7 13f9825
Added gale_shapley.cpp in greedy_algorithms
BKarthik7 418980d
Genralized GaleShapley with reviewed change
BKarthik7 a689963
fix: added description
BKarthik7 f9fcb73
fix: fixed nameing of namespace
BKarthik7 bdce70d
fix: reviewed changes
BKarthik7 ca6586d
fix: reviewed changes
BKarthik7 272e366
TestCase Empty vector
BKarthik7 bb17cb8
function description
BKarthik7 3021d9d
Update greedy_algorithms/gale_shapley.cpp
BKarthik7 08d29f2
Update greedy_algorithms/gale_shapley.cpp
BKarthik7 ca2c57a
Change type
BKarthik7 831dfd8
Merge branch 'master' of https://github.com/BKarthik7/C-Plus-Plus
BKarthik7 e4ff4af
typechange with header documentation
BKarthik7 aa04bc1
Merge branch 'master' into master
realstealthninja File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
/** | ||
* @file | ||
* @brief [Gale Shapley Algorithm](https://en.wikipedia.org/wiki/Gale%E2%80%93Shapley_algorithm) | ||
* @details | ||
* This implementation utilizes the Gale-Shapley algorithm to find stable matches | ||
* | ||
* **Gale Shapley Algorithm** aims to find a stable matching between two equally sized | ||
* sets of elements given an ordinal preference for each element. The algorithm was | ||
* introduced by David Gale and Lloyd Shapley in 1962. | ||
* | ||
* Reference: | ||
* [Wikipedia](https://en.wikipedia.org/wiki/Gale%E2%80%93Shapley_algorithm) | ||
* [Wikipedia](https://en.wikipedia.org/wiki/Stable_matching_problem) | ||
* | ||
* @author [B Karthik](https://github.com/BKarthik7) | ||
*/ | ||
|
||
#include <iostream> | ||
#include <vector> | ||
#include <cstring> | ||
#include <algorithm> | ||
|
||
BKarthik7 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
/** | ||
* @namespace | ||
* @brief Greedy Algorithms | ||
*/ | ||
namespace greedy_algorithms { | ||
/** | ||
* @namespace | ||
* @brief Functions for the Gale Shapley Algorithm | ||
*/ | ||
namespace stable_matching { | ||
void gale_shapley(const std::vector<std::vector<int>>& set_2_prefs, const std::vector<std::vector<int>>& set_1_prefs) { | ||
int n = set_2_prefs.size(); | ||
std::vector<int> engagements(n, -1); | ||
std::vector<bool> free_set_1_s(n, true); | ||
std::vector<int> next_proposal(n, 0); // Tracks the next set_2 to propose for each set_1 | ||
|
||
while (true) { | ||
BKarthik7 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
int free_set_1; | ||
BKarthik7 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
for (free_set_1 = 0; free_set_1 < n; free_set_1++) { | ||
if (free_set_1_s[free_set_1]) break; | ||
} | ||
if (free_set_1 == n) break; | ||
|
||
int set_2_to_propose = set_1_prefs[free_set_1][next_proposal[free_set_1]]; | ||
next_proposal[free_set_1]++; | ||
int currentFiance = engagements[set_2_to_propose]; | ||
|
||
if (currentFiance == -1) { | ||
engagements[set_2_to_propose] = free_set_1; | ||
free_set_1_s[free_set_1] = false; | ||
} else { | ||
if (std::find(set_2_prefs[set_2_to_propose].begin(), set_2_prefs[set_2_to_propose].end(), free_set_1) < | ||
std::find(set_2_prefs[set_2_to_propose].begin(), set_2_prefs[set_2_to_propose].end(), currentFiance)) { | ||
engagements[set_2_to_propose] = free_set_1; | ||
free_set_1_s[free_set_1] = false; | ||
free_set_1_s[currentFiance] = true; | ||
} | ||
} | ||
} | ||
|
||
std::cout << "Stable Matches:\n"; | ||
for (int set_2 = 0; set_2 < n; set_2++) { | ||
std::cout << "set_2's " << set_2 << " is matched to set_1's " << engagements[set_2] << std::endl; | ||
} | ||
std::cout << std::endl; | ||
} | ||
} // namespace stable_matching | ||
} // namespace greedy_algorithms | ||
|
||
BKarthik7 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
/** | ||
* @brief Self-test implementations | ||
* @returns void | ||
*/ | ||
|
||
BKarthik7 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
static void tests() { | ||
BKarthik7 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
// Test Case 1 | ||
std::vector<std::vector<int>> set_1_prefs = {{0, 1, 2, 3}, {2, 1, 3, 0}, {1, 2, 0, 3}, {3, 0, 1, 2}}; | ||
std::vector<std::vector<int>> set_2_prefs = {{1, 0, 2, 3},{3, 0, 1, 2},{0, 2, 1, 3},{1, 2, 0, 3}}; | ||
greedy_algorithms::stable_matching::gale_shapley(set_2_prefs, set_1_prefs); | ||
BKarthik7 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
||
// Test Case 2 | ||
set_1_prefs = {{0, 2, 1, 3}, {2, 3, 0, 1}, {3, 1, 2, 0}, {2, 1, 0, 3}}; | ||
set_2_prefs = {{1, 0, 2, 3},{3, 0, 1, 2},{0, 2, 1, 3},{1, 2, 0, 3}}; | ||
greedy_algorithms::stable_matching::gale_shapley(set_2_prefs, set_1_prefs); | ||
|
||
// Test Case 3 | ||
set_1_prefs = {{0, 1, 2}, {2, 1, 0}, {1, 2, 0}}; | ||
set_2_prefs = {{1, 0, 2},{2, 0, 1},{0, 2, 1}}; | ||
greedy_algorithms::stable_matching::gale_shapley(set_2_prefs, set_1_prefs); | ||
} | ||
|
||
/** | ||
* @brief Main function | ||
* @returns 0 on exit | ||
*/ | ||
int main() { | ||
tests(); // run self-test implementations | ||
return 0; | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.