Ok(matches)
}
-
// ============================================================================
// Example Usage and Tests
// ============================================================================
// Create bipartite graph with Person nodes
let left_partition = HashMap::from([
- (1, Person {
- id: 1,
- name: "Alice".to_string(),
- gender: Gender::Female,
- }),
- (4, Person {
- id: 4,
- name: "Bob".to_string(),
- gender: Gender::Female,
- }),
+ (
+ 1,
+ Person {
+ id: 1,
+ name: "Alice".to_string(),
+ gender: Gender::Female,
+ },
+ ),
+ (
+ 4,
+ Person {
+ id: 4,
+ name: "Bob".to_string(),
+ gender: Gender::Female,
+ },
+ ),
]);
let right_partition = HashMap::from([
- (2, Person {
- id: 2,
- name: "Charlie".to_string(),
- gender: Gender::Male,
- }),
- (3, Person {
- id: 3,
- name: "David".to_string(),
- gender: Gender::Male,
- }),
+ (
+ 2,
+ Person {
+ id: 2,
+ name: "Charlie".to_string(),
+ gender: Gender::Male,
+ },
+ ),
+ (
+ 3,
+ Person {
+ id: 3,
+ name: "David".to_string(),
+ gender: Gender::Male,
+ },
+ ),
]);
let graph: BipartiteGraph<u32, Person, Person> =
BipartiteGraph::new(left_partition, right_partition);
// Run stable matching algorithm
- let result = stable_match::<u32, Person, Person>(
- &graph,
- &left_prefs,
- &right_prefs,
- );
+ let result = stable_match::<u32, Person, Person>(&graph, &left_prefs, &right_prefs);
assert!(result.is_ok());
let matches = result.unwrap();
fn test_stable_match_with_string_keys() {
// Demonstrate that the algorithm works with different key types
let left_prefs: HashMap<String, Preferences<String>> = HashMap::from([
- ("alice".to_string(), Preferences::new(vec!["bob".to_string(), "charlie".to_string()])),
- ("diana".to_string(), Preferences::new(vec!["charlie".to_string(), "bob".to_string()])),
+ (
+ "alice".to_string(),
+ Preferences::new(vec!["bob".to_string(), "charlie".to_string()]),
+ ),
+ (
+ "diana".to_string(),
+ Preferences::new(vec!["charlie".to_string(), "bob".to_string()]),
+ ),
]);
let right_prefs: HashMap<String, Preferences<String>> = HashMap::from([
- ("bob".to_string(), Preferences::new(vec!["alice".to_string(), "diana".to_string()])),
- ("charlie".to_string(), Preferences::new(vec!["diana".to_string(), "alice".to_string()])),
+ (
+ "bob".to_string(),
+ Preferences::new(vec!["alice".to_string(), "diana".to_string()]),
+ ),
+ (
+ "charlie".to_string(),
+ Preferences::new(vec!["diana".to_string(), "alice".to_string()]),
+ ),
]);
let left_partition = HashMap::from([
- ("alice".to_string(), Person {
- id: 1,
- name: "Alice".to_string(),
- gender: Gender::Female,
- }),
- ("diana".to_string(), Person {
- id: 2,
- name: "Diana".to_string(),
- gender: Gender::Female,
- }),
+ (
+ "alice".to_string(),
+ Person {
+ id: 1,
+ name: "Alice".to_string(),
+ gender: Gender::Female,
+ },
+ ),
+ (
+ "diana".to_string(),
+ Person {
+ id: 2,
+ name: "Diana".to_string(),
+ gender: Gender::Female,
+ },
+ ),
]);
let right_partition = HashMap::from([
- ("bob".to_string(), Person {
- id: 3,
- name: "Bob".to_string(),
- gender: Gender::Male,
- }),
- ("charlie".to_string(), Person {
- id: 4,
- name: "Charlie".to_string(),
- gender: Gender::Male,
- }),
+ (
+ "bob".to_string(),
+ Person {
+ id: 3,
+ name: "Bob".to_string(),
+ gender: Gender::Male,
+ },
+ ),
+ (
+ "charlie".to_string(),
+ Person {
+ id: 4,
+ name: "Charlie".to_string(),
+ gender: Gender::Male,
+ },
+ ),
]);
let graph: BipartiteGraph<String, Person, Person> =
BipartiteGraph::new(left_partition, right_partition);
- let result = stable_match::<String, Person, Person>(
- &graph,
- &left_prefs,
- &right_prefs,
- );
+ let result = stable_match::<String, Person, Person>(&graph, &left_prefs, &right_prefs);
assert!(result.is_ok());
let matches = result.unwrap();