Skip to content

Commit e6f597a

Browse files
authored
Add Gale-Shapley Algorithm and Tests (#5494)
1 parent dab8ff3 commit e6f597a

File tree

3 files changed

+139
-0
lines changed

3 files changed

+139
-0
lines changed

DIRECTORY.md

+2
Original file line numberDiff line numberDiff line change
@@ -265,6 +265,7 @@
265265
* [ActivitySelection](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/greedyalgorithms/ActivitySelection.java)
266266
* [CoinChange](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/greedyalgorithms/CoinChange.java)
267267
* [FractionalKnapsack](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/greedyalgorithms/FractionalKnapsack.java)
268+
* [GaleShapley](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/greedyalgorithms/GaleShapley.java)
268269
* [JobSequencing](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/greedyalgorithms/JobSequencing.java)
269270
* [MinimizingLateness](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/greedyalgorithms/MinimizingLateness.java)
270271
* io
@@ -749,6 +750,7 @@
749750
* [ActivitySelectionTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/greedyalgorithms/ActivitySelectionTest.java)
750751
* [CoinChangeTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/greedyalgorithms/CoinChangeTest.java)
751752
* [FractionalKnapsackTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/greedyalgorithms/FractionalKnapsackTest.java)
753+
* [GaleShapleyTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/greedyalgorithms/GaleShapleyTest.java)
752754
* [JobSequencingTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/greedyalgorithms/JobSequencingTest.java)
753755
* [MinimizingLatenessTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/greedyalgorithms/MinimizingLatenessTest.java)
754756
* io
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
package com.thealgorithms.greedyalgorithms;
2+
3+
import java.util.HashMap;
4+
import java.util.LinkedList;
5+
import java.util.Map;
6+
7+
/**
8+
* Implementation of the Gale-Shapley Algorithm for Stable Matching.
9+
* Problem link: https://en.wikipedia.org/wiki/Stable_marriage_problem
10+
*/
11+
public final class GaleShapley {
12+
13+
private GaleShapley() {
14+
}
15+
16+
/**
17+
* Function to find stable matches between men and women.
18+
*
19+
* @param womenPrefs A map containing women's preferences where each key is a woman and the value is an array of men in order of preference.
20+
* @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.
21+
* @return A map containing stable matches where the key is a woman and the value is her matched man.
22+
*/
23+
public static Map<String, String> stableMatch(Map<String, LinkedList<String>> womenPrefs, Map<String, LinkedList<String>> menPrefs) {
24+
// Initialize all men as free
25+
Map<String, String> engagements = new HashMap<>();
26+
LinkedList<String> freeMen = new LinkedList<>(menPrefs.keySet());
27+
28+
// While there are free men
29+
while (!freeMen.isEmpty()) {
30+
String man = freeMen.poll(); // Get the first free man
31+
LinkedList<String> manPref = menPrefs.get(man); // Get the preferences of the man
32+
33+
// Check if manPref is null or empty
34+
if (manPref == null || manPref.isEmpty()) {
35+
continue; // Skip if no preferences
36+
}
37+
38+
// Propose to the first woman in the man's preference list
39+
String woman = manPref.poll();
40+
String fiance = engagements.get(woman);
41+
42+
// If the woman is not engaged, engage her with the current man
43+
if (fiance == null) {
44+
engagements.put(woman, man);
45+
} else {
46+
// If the woman prefers the current man over her current fiance
47+
LinkedList<String> womanPrefList = womenPrefs.get(woman);
48+
49+
// Check if womanPrefList is null
50+
if (womanPrefList == null) {
51+
continue; // Skip if no preferences for the woman
52+
}
53+
54+
if (womanPrefList.indexOf(man) < womanPrefList.indexOf(fiance)) {
55+
engagements.put(woman, man);
56+
freeMen.add(fiance); // Previous fiance becomes free
57+
} else {
58+
// Woman rejects the new proposal, the man remains free
59+
freeMen.add(man);
60+
}
61+
}
62+
}
63+
return engagements; // Return the stable matches
64+
}
65+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
package com.thealgorithms.greedyalgorithms;
2+
3+
import static org.junit.jupiter.api.Assertions.assertEquals;
4+
5+
import java.util.HashMap;
6+
import java.util.LinkedList;
7+
import java.util.List;
8+
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);
71+
}
72+
}

0 commit comments

Comments
 (0)