Skip to content

Commit ff5fb15

Browse files
committed
Update GaleShapley.java
1 parent 7920387 commit ff5fb15

File tree

1 file changed

+15
-16
lines changed

1 file changed

+15
-16
lines changed

src/main/java/com/thealgorithms/greedyalgorithms/GaleShapley.java

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -20,47 +20,46 @@ private GaleShapley() {
2020
* @param menPrefs A map containing men's preferences where each key is a man and the value is an array of women in order of preference.
2121
* @return A map containing stable matches where the key is a woman and the value is her matched man.
2222
*/
23-
public static Map<String, String> stableMatch(Map<String, LinkedList<String>> womenPrefs,
24-
Map<String, LinkedList<String>> menPrefs) {
23+
public static Map<String, String> stableMatch(Map<String, LinkedList<String>> womenPrefs, Map<String, LinkedList<String>> menPrefs) {
24+
// Initialize all men as free
2525
Map<String, String> engagements = new HashMap<>();
26-
Queue<String> freeMen = new LinkedList<>(menPrefs.keySet());
26+
LinkedList<String> freeMen = new LinkedList<>(menPrefs.keySet());
2727

28+
// While there are free men
2829
while (!freeMen.isEmpty()) {
29-
String man = freeMen.poll();
30-
LinkedList<String> manPref = menPrefs.get(man);
30+
String man = freeMen.poll(); // Get the first free man
31+
LinkedList<String> manPref = menPrefs.get(man); // Get the preferences of the man
3132

32-
// Ensure manPref is not null before proceeding
33-
if (manPref == null) {
34-
continue;
33+
// Check if manPref is null or empty
34+
if (manPref == null || manPref.isEmpty()) {
35+
continue; // Skip if no preferences
3536
}
3637

3738
// Propose to the first woman in the man's preference list
3839
String woman = manPref.poll();
3940
String fiance = engagements.get(woman);
4041

42+
// If the woman is not engaged, engage her with the current man
4143
if (fiance == null) {
42-
// Woman is free, engage the man and woman
4344
engagements.put(woman, man);
4445
} else {
45-
// Woman is already engaged, check if she prefers this man over her current fiance
46+
// If the woman prefers the current man over her current fiance
4647
LinkedList<String> womanPrefList = womenPrefs.get(woman);
4748

48-
// Ensure womanPrefList is not null before comparing
49+
// Check if womanPrefList is null
4950
if (womanPrefList == null) {
50-
continue;
51+
continue; // Skip if no preferences for the woman
5152
}
5253

53-
// If the woman prefers the current man over her current fiance
5454
if (womanPrefList.indexOf(man) < womanPrefList.indexOf(fiance)) {
5555
engagements.put(woman, man);
5656
freeMen.add(fiance); // Previous fiance becomes free
5757
} else {
58-
// Woman stays with her current fiance, man remains free
58+
// Woman rejects the new proposal, the man remains free
5959
freeMen.add(man);
6060
}
6161
}
6262
}
63-
64-
return engagements;
63+
return engagements; // Return the stable matches
6564
}
6665
}

0 commit comments

Comments
 (0)