|
1 | 1 | package com.thealgorithms.greedyalgorithms;
|
2 | 2 |
|
3 |
| -import static org.junit.jupiter.api.Assertions.assertEquals; |
4 |
| - |
5 | 3 | import java.util.HashMap;
|
6 | 4 | import java.util.LinkedList;
|
7 |
| -import java.util.List; |
8 | 5 | import java.util.Map;
|
9 |
| -import org.junit.jupiter.api.Test; |
10 |
| - |
11 |
| -public class GaleShapleyTest { |
12 |
| - |
13 |
| - @Test |
14 |
| - public void testStableMatch() { |
15 |
| - Map<String, LinkedList<String>> womenPrefs = new HashMap<>(); |
16 |
| - womenPrefs.put("A", new LinkedList<>(List.of("X", "Y", "Z"))); |
17 |
| - womenPrefs.put("B", new LinkedList<>(List.of("Y", "X", "Z"))); |
18 |
| - womenPrefs.put("C", new LinkedList<>(List.of("X", "Y", "Z"))); |
19 |
| - |
20 |
| - Map<String, LinkedList<String>> menPrefs = new HashMap<>(); |
21 |
| - menPrefs.put("X", new LinkedList<>(List.of("A", "B", "C"))); |
22 |
| - menPrefs.put("Y", new LinkedList<>(List.of("B", "A", "C"))); |
23 |
| - menPrefs.put("Z", new LinkedList<>(List.of("A", "B", "C"))); |
24 |
| - |
25 |
| - Map<String, String> result = GaleShapley.stableMatch(womenPrefs, menPrefs); |
26 |
| - |
27 |
| - Map<String, String> expected = new HashMap<>(); |
28 |
| - expected.put("A", "X"); |
29 |
| - expected.put("B", "Y"); |
30 |
| - expected.put("C", "Z"); |
31 |
| - |
32 |
| - assertEquals(expected, result); |
33 |
| - } |
34 |
| - |
35 |
| - @Test |
36 |
| - public void testSinglePair() { |
37 |
| - Map<String, LinkedList<String>> womenPrefs = new HashMap<>(); |
38 |
| - womenPrefs.put("A", new LinkedList<>(List.of("X"))); |
39 |
| - |
40 |
| - Map<String, LinkedList<String>> menPrefs = new HashMap<>(); |
41 |
| - menPrefs.put("X", new LinkedList<>(List.of("A"))); |
42 |
| - |
43 |
| - Map<String, String> result = GaleShapley.stableMatch(womenPrefs, menPrefs); |
44 |
| - |
45 |
| - Map<String, String> expected = new HashMap<>(); |
46 |
| - expected.put("A", "X"); |
47 |
| - |
48 |
| - assertEquals(expected, result); |
49 |
| - } |
50 |
| - |
51 |
| - @Test |
52 |
| - public void testEqualPreferences() { |
53 |
| - Map<String, LinkedList<String>> womenPrefs = new HashMap<>(); |
54 |
| - womenPrefs.put("A", new LinkedList<>(List.of("X", "Y", "Z"))); |
55 |
| - womenPrefs.put("B", new LinkedList<>(List.of("X", "Y", "Z"))); |
56 |
| - womenPrefs.put("C", new LinkedList<>(List.of("X", "Y", "Z"))); |
57 |
| - |
58 |
| - Map<String, LinkedList<String>> menPrefs = new HashMap<>(); |
59 |
| - menPrefs.put("X", new LinkedList<>(List.of("A", "B", "C"))); |
60 |
| - menPrefs.put("Y", new LinkedList<>(List.of("A", "B", "C"))); |
61 |
| - menPrefs.put("Z", new LinkedList<>(List.of("A", "B", "C"))); |
62 |
| - |
63 |
| - Map<String, String> result = GaleShapley.stableMatch(womenPrefs, menPrefs); |
64 |
| - |
65 |
| - Map<String, String> expected = new HashMap<>(); |
66 |
| - expected.put("A", "X"); |
67 |
| - expected.put("B", "Y"); |
68 |
| - expected.put("C", "Z"); |
69 |
| - |
70 |
| - assertEquals(expected, result); |
| 6 | +import java.util.Queue; |
| 7 | + |
| 8 | +public class GaleShapley { |
| 9 | + |
| 10 | + public static Map<String, String> stableMatch(Map<String, LinkedList<String>> womenPrefs, |
| 11 | + Map<String, LinkedList<String>> menPrefs) { |
| 12 | + Map<String, String> engagements = new HashMap<>(); |
| 13 | + Queue<String> freeMen = new LinkedList<>(menPrefs.keySet()); |
| 14 | + |
| 15 | + while (!freeMen.isEmpty()) { |
| 16 | + String man = freeMen.poll(); |
| 17 | + LinkedList<String> manPref = menPrefs.get(man); |
| 18 | + |
| 19 | + // Ensure manPref is not null before proceeding |
| 20 | + if (manPref == null) { |
| 21 | + continue; |
| 22 | + } |
| 23 | + |
| 24 | + // Propose to the first woman in the man's preference list |
| 25 | + String woman = manPref.poll(); |
| 26 | + String fiance = engagements.get(woman); |
| 27 | + |
| 28 | + if (fiance == null) { |
| 29 | + // Woman is free, engage the man and woman |
| 30 | + engagements.put(woman, man); |
| 31 | + } else { |
| 32 | + // Woman is already engaged, check if she prefers this man over her current fiance |
| 33 | + LinkedList<String> womanPrefList = womenPrefs.get(woman); |
| 34 | + |
| 35 | + // Ensure womanPrefList is not null before comparing |
| 36 | + if (womanPrefList == null) { |
| 37 | + continue; |
| 38 | + } |
| 39 | + |
| 40 | + // If the woman prefers the current man over her current fiance |
| 41 | + if (womanPrefList.indexOf(man) < womanPrefList.indexOf(fiance)) { |
| 42 | + engagements.put(woman, man); |
| 43 | + freeMen.add(fiance); // Previous fiance becomes free |
| 44 | + } else { |
| 45 | + // Woman stays with her current fiance, man remains free |
| 46 | + freeMen.add(man); |
| 47 | + } |
| 48 | + } |
| 49 | + } |
| 50 | + |
| 51 | + return engagements; |
71 | 52 | }
|
72 | 53 | }
|
0 commit comments