-
-
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
Merged
Changes from 4 commits
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,199 @@ | ||
| use std::collections::{HashMap, VecDeque}; | ||
|
|
||
| pub fn stable_matching( | ||
| men_preferences: &HashMap<String, Vec<String>>, | ||
| women_preferences: &HashMap<String, Vec<String>>, | ||
| ) -> HashMap<String, String> { | ||
| let mut free_men: VecDeque<String> = VecDeque::new(); | ||
| let mut current_partner: HashMap<String, Option<String>> = HashMap::new(); | ||
| let mut man_engaged: HashMap<String, Option<String>> = HashMap::new(); | ||
| let mut next_proposal: HashMap<String, usize> = HashMap::new(); | ||
|
|
||
| for man in men_preferences.keys() { | ||
| free_men.push_back(man.clone()); | ||
| next_proposal.insert(man.clone(), 0); | ||
| } | ||
|
|
||
| for woman in women_preferences.keys() { | ||
| current_partner.insert(woman.clone(), None); | ||
| } | ||
|
|
||
| fn woman_prefers_new_man( | ||
| woman: &str, | ||
| man1: &str, | ||
| man2: &str, | ||
| preferences: &HashMap<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 let Some(man) = free_men.pop_front() { | ||
| let man_pref_list = &men_preferences[&man]; | ||
| let next_woman_idx = *next_proposal.get(&man).unwrap(); | ||
| let woman = &man_pref_list[next_woman_idx]; | ||
|
|
||
| next_proposal.insert(man.clone(), next_woman_idx + 1); | ||
|
|
||
| if let Some(current_man) = current_partner[woman].clone() { | ||
| if woman_prefers_new_man(woman, &man, ¤t_man, women_preferences) { | ||
| man_engaged.insert(man.clone(), Some(woman.clone())); | ||
| current_partner.insert(woman.clone(), Some(man.clone())); | ||
| free_men.push_back(current_man); | ||
| } else { | ||
| free_men.push_back(man); | ||
| } | ||
| } else { | ||
| man_engaged.insert(man.clone(), Some(woman.clone())); | ||
| current_partner.insert(woman.clone(), Some(man.clone())); | ||
| } | ||
| } | ||
|
|
||
| let mut stable_matches: HashMap<String, String> = HashMap::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::*; | ||
| use std::collections::HashMap; | ||
|
|
||
| #[test] | ||
| fn test_stable_matching_scenario_1() { | ||
| let men_preferences = HashMap::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 = HashMap::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); | ||
|
|
||
| let expected_matches1 = HashMap::from([ | ||
| ("A".to_string(), "Y".to_string()), | ||
| ("B".to_string(), "X".to_string()), | ||
| ("C".to_string(), "Z".to_string()), | ||
| ]); | ||
|
|
||
| let expected_matches2 = HashMap::from([ | ||
| ("A".to_string(), "X".to_string()), | ||
| ("B".to_string(), "Y".to_string()), | ||
| ("C".to_string(), "Z".to_string()), | ||
| ]); | ||
|
|
||
| assert!(matches == expected_matches1 || matches == expected_matches2); | ||
| } | ||
|
|
||
| #[test] | ||
| fn test_stable_matching_empty() { | ||
| let men_preferences = HashMap::new(); | ||
| let women_preferences = HashMap::new(); | ||
|
|
||
| let matches = stable_matching(&men_preferences, &women_preferences); | ||
| assert!(matches.is_empty()); | ||
| } | ||
|
|
||
| #[test] | ||
| fn test_stable_matching_duplicate_preferences() { | ||
| let men_preferences = HashMap::from([ | ||
| ("A".to_string(), vec!["X".to_string(), "X".to_string()]), // Man with duplicate preferences | ||
| ("B".to_string(), vec!["Y".to_string()]), | ||
| ]); | ||
|
|
||
| let women_preferences = HashMap::from([ | ||
| ("X".to_string(), vec!["A".to_string(), "B".to_string()]), | ||
| ("Y".to_string(), vec!["B".to_string()]), | ||
| ]); | ||
|
|
||
| let matches = stable_matching(&men_preferences, &women_preferences); | ||
| let expected_matches = HashMap::from([ | ||
| ("A".to_string(), "X".to_string()), | ||
| ("B".to_string(), "Y".to_string()), | ||
| ]); | ||
|
|
||
| assert_eq!(matches, expected_matches); | ||
| } | ||
|
|
||
| #[test] | ||
| fn test_stable_matching_single_pair() { | ||
| let men_preferences = HashMap::from([("A".to_string(), vec!["X".to_string()])]); | ||
| let women_preferences = HashMap::from([("X".to_string(), vec!["A".to_string()])]); | ||
|
|
||
| let matches = stable_matching(&men_preferences, &women_preferences); | ||
| let expected_matches = HashMap::from([("A".to_string(), "X".to_string())]); | ||
|
|
||
| assert_eq!(matches, expected_matches); | ||
| } | ||
| #[test] | ||
| fn test_woman_prefers_new_man() { | ||
| let men_preferences = HashMap::from([ | ||
| ( | ||
| "A".to_string(), | ||
| vec!["X".to_string(), "Y".to_string(), "Z".to_string()], | ||
| ), | ||
| ( | ||
| "B".to_string(), | ||
| vec!["X".to_string(), "Y".to_string(), "Z".to_string()], | ||
| ), | ||
| ( | ||
| "C".to_string(), | ||
| vec!["X".to_string(), "Y".to_string(), "Z".to_string()], | ||
| ), | ||
| ]); | ||
|
|
||
| let women_preferences = HashMap::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); | ||
|
|
||
| let expected_matches = HashMap::from([ | ||
| ("A".to_string(), "Y".to_string()), | ||
| ("B".to_string(), "X".to_string()), | ||
| ("C".to_string(), "Z".to_string()), | ||
| ]); | ||
|
|
||
| assert_eq!(matches, expected_matches); | ||
| } | ||
| } | ||
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.