]> localhost Git - algorithms.git/commitdiff
formatted
authorJeff Hemminger <jeff@hemminger.haus>
Wed, 24 Dec 2025 01:20:34 +0000 (10:20 +0900)
committerJeff Hemminger <jeff@hemminger.haus>
Wed, 24 Dec 2025 01:20:34 +0000 (10:20 +0900)
src/algorithms/stable_matching/bipartite.rs

index cd65d53b14339b2a1802a4599dae3d5bae19c981..0fe9e62398b4de133257b770cd37bba07f48fd5e 100644 (file)
@@ -155,7 +155,6 @@ where
     Ok(matches)
 }
 
-
 // ============================================================================
 // Example Usage and Tests
 // ============================================================================
@@ -200,40 +199,48 @@ mod 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();
@@ -246,49 +253,69 @@ mod tests {
     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();