-
-
Notifications
You must be signed in to change notification settings - Fork 2.5k
feat: Added Stable Matching Algorithm #806
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
+282
−0
Merged
Changes from 1 commit
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
589f6a0
feat: add Stable Matching Algorithm
BKarthik7 edb3635
Changes
BKarthik7 1701f6d
Changes
BKarthik7 c40fb3e
add cases
BKarthik7 5ec6fb0
consize
BKarthik7 f6e63ab
Suggested changes
BKarthik7 4f2168b
fmt
BKarthik7 d9b140c
Merge branch 'master' into master
vil02 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
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,3 @@ | ||
| mod stable_matching; | ||
|
|
||
| pub use self::stable_matching::stable_matching; |
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,155 @@ | ||
| use std::collections::{BTreeMap, VecDeque}; | ||
|
|
||
| // This function performs the Gale-Shapley stable matching algorithm | ||
| pub fn stable_matching( | ||
| men_preferences: &BTreeMap<String, Vec<String>>, | ||
| women_preferences: &BTreeMap<String, Vec<String>>, | ||
BKarthik7 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| ) -> BTreeMap<String, String> { | ||
| // Free men: those who haven't been paired yet | ||
| let mut free_men: VecDeque<String> = VecDeque::new(); | ||
|
|
||
| // Holds the current partner of each woman (if any) | ||
| let mut current_partner: BTreeMap<String, Option<String>> = BTreeMap::new(); | ||
|
|
||
| // Holds the current engagement of each man (if any) | ||
| let mut man_engaged: BTreeMap<String, Option<String>> = BTreeMap::new(); | ||
|
|
||
| // Holds the position in each man's preference list they are currently proposing to | ||
| let mut next_proposal: BTreeMap<String, usize> = BTreeMap::new(); | ||
|
|
||
| // Initialize all men as free | ||
| for man in men_preferences.keys() { | ||
| free_men.push_back(man.clone()); | ||
| next_proposal.insert(man.clone(), 0); | ||
| } | ||
|
|
||
| // Initialize all women as having no partner | ||
| for woman in women_preferences.keys() { | ||
| current_partner.insert(woman.clone(), None); | ||
| } | ||
|
|
||
| // Helper function to check if a woman prefers a new man over her current partner | ||
| fn woman_prefers_new_man( | ||
| woman: &str, | ||
| man1: &str, | ||
| man2: &str, | ||
| preferences: &BTreeMap<String, Vec<String>>, | ||
| ) -> bool { | ||
| let woman_preferences = &preferences[woman]; | ||
| woman_preferences.iter().position(|m| m == man1).unwrap() | ||
| < woman_preferences.iter().position(|m| m == man2).unwrap() | ||
| } | ||
|
|
||
| // While there are free men | ||
| while let Some(man) = free_men.pop_front() { | ||
| // Get the man's preference list and find the next woman to propose to | ||
| let man_pref_list = &men_preferences[&man]; | ||
| let next_woman_idx = *next_proposal.get(&man).unwrap(); | ||
| let woman = &man_pref_list[next_woman_idx]; | ||
|
|
||
| // Move to the next proposal for this man | ||
| next_proposal.insert(man.clone(), next_woman_idx + 1); | ||
|
|
||
| if let Some(current_man) = current_partner[woman].clone() { | ||
| // The woman is already engaged, compare her preference | ||
| if woman_prefers_new_man(woman, &man, ¤t_man, women_preferences) { | ||
| // Woman prefers the new man, engage them | ||
| man_engaged.insert(man.clone(), Some(woman.clone())); | ||
| current_partner.insert(woman.clone(), Some(man.clone())); | ||
| free_men.push_back(current_man); // The old partner becomes free | ||
| } else { | ||
| // Woman prefers her current partner, so the man remains free | ||
| free_men.push_back(man); | ||
| } | ||
| } else { | ||
| // The woman is not engaged, engage her with the man | ||
| man_engaged.insert(man.clone(), Some(woman.clone())); | ||
| current_partner.insert(woman.clone(), Some(man.clone())); | ||
| } | ||
| } | ||
|
|
||
| // Return the final stable matches as a BTreeMap | ||
| let mut stable_matches: BTreeMap<String, String> = BTreeMap::new(); | ||
| for (man, woman_option) in man_engaged { | ||
| if let Some(woman) = woman_option { | ||
| stable_matches.insert(man, woman); | ||
| } | ||
| } | ||
|
|
||
| stable_matches | ||
| } | ||
BKarthik7 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| #[cfg(test)] | ||
| mod tests { | ||
| use super::*; | ||
|
|
||
| #[test] | ||
| fn test_stable_matching() { | ||
| // Test 1 | ||
|
|
||
| let men_preferences = BTreeMap::from([ | ||
| ( | ||
| "A".to_string(), | ||
| vec!["X".to_string(), "Y".to_string(), "Z".to_string()], | ||
| ), | ||
| ( | ||
| "B".to_string(), | ||
| vec!["Y".to_string(), "X".to_string(), "Z".to_string()], | ||
| ), | ||
| ( | ||
| "C".to_string(), | ||
| vec!["X".to_string(), "Y".to_string(), "Z".to_string()], | ||
| ), | ||
| ]); | ||
|
|
||
| let women_preferences = BTreeMap::from([ | ||
| ( | ||
| "X".to_string(), | ||
| vec!["B".to_string(), "A".to_string(), "C".to_string()], | ||
| ), | ||
| ( | ||
| "Y".to_string(), | ||
| vec!["A".to_string(), "B".to_string(), "C".to_string()], | ||
| ), | ||
| ( | ||
| "Z".to_string(), | ||
| vec!["A".to_string(), "B".to_string(), "C".to_string()], | ||
| ), | ||
| ]); | ||
|
|
||
| let matches = stable_matching(&men_preferences, &women_preferences); | ||
|
|
||
| // Expected stable matching can be one of two possible outcomes | ||
| let expected_matches1 = BTreeMap::from([ | ||
| ("A".to_string(), "Y".to_string()), | ||
| ("B".to_string(), "X".to_string()), | ||
| ("C".to_string(), "Z".to_string()), | ||
| ]); | ||
|
|
||
| let expected_matches2 = BTreeMap::from([ | ||
| ("A".to_string(), "X".to_string()), | ||
| ("B".to_string(), "Y".to_string()), | ||
| ("C".to_string(), "Z".to_string()), | ||
| ]); | ||
|
|
||
| // Assert that the result matches one of the expected outcomes | ||
| assert!( | ||
| matches == expected_matches1 || matches == expected_matches2, | ||
| "Matching result is not as expected" | ||
| ); | ||
|
|
||
| //Test 2: Empty Inputs | ||
|
|
||
| let men_preferences = BTreeMap::from([]); | ||
| let women_preferences = BTreeMap::from([]); | ||
| let matches = stable_matching(&men_preferences, &women_preferences); | ||
| let expected_matches1 = BTreeMap::from([]); | ||
| let expected_matches2 = BTreeMap::from([]); | ||
|
|
||
| // Assert that the result matches one of the expected outcomes | ||
| assert!( | ||
| matches == expected_matches1 || matches == expected_matches2, | ||
| "Matching result is not as expected" | ||
| ); | ||
| } | ||
BKarthik7 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
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
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.