Skip to content

Commit 7474093

Browse files
committed
upload code for the algorithm
1 parent 2881cc0 commit 7474093

File tree

2 files changed

+105
-0
lines changed

2 files changed

+105
-0
lines changed
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,29 @@
11
package csaba79coder;
22

3+
import csaba79coder.model.GaleShapley;
4+
35
public class GaleShapleyAlgorithmApp {
46

57
public static void main(String[] args) {
68

9+
System.out.println("Gale Shapley Marriage Algorithm\n");
10+
11+
String[] m = {"M1", "M2", "M3", "M4", "M5"};
12+
13+
String[] w = {"W1", "W2", "W3", "W4", "W5"};
14+
15+
String[][] mp = {{"W5", "W2", "W3", "W4", "W1"},
16+
{"W2", "W5", "W1", "W3", "W4"},
17+
{"W4", "W3", "W2", "W1", "W5"},
18+
{"W1", "W2", "W3", "W4", "W5"},
19+
{"W5", "W2", "W3", "W4", "W1"}};
20+
21+
String[][] wp = {{"M5", "M3", "M4", "M1", "M2"},
22+
{"M1", "M2", "M3", "M5", "M4"},
23+
{"M4", "M5", "M3", "M2", "M1"},
24+
{"M5", "M2", "M1", "M4", "M3"},
25+
{"M2", "M1", "M4", "M3", "M5"}};
26+
27+
GaleShapley gs = new GaleShapley(m, w, mp, wp);
728
}
829
}
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
package csaba79coder.model;
2+
3+
public class GaleShapley {
4+
5+
private final int N;
6+
private int engagedCount;
7+
private final String[][] menPref;
8+
private final String[][] womenPref;
9+
private final String[] men;
10+
private final String[] women;
11+
private final String[] womenPartner;
12+
private final boolean[] menEngaged;
13+
14+
public GaleShapley(String[] m, String[] w, String[][] mp, String[][] wp) {
15+
N = mp.length;
16+
engagedCount = 0;
17+
men = m;
18+
women = w;
19+
menPref = mp;
20+
womenPref = wp;
21+
menEngaged = new boolean[N];
22+
womenPartner = new String[N];
23+
calcMatches();
24+
}
25+
26+
private void calcMatches() {
27+
while (engagedCount < N) {
28+
int free;
29+
for (free = 0; free < N; free++)
30+
if (!menEngaged[free])
31+
break;
32+
33+
for (int i = 0; i < N && !menEngaged[free]; i++) {
34+
int index = womenIndexOf(menPref[free][i]);
35+
if (womenPartner[index] == null) {
36+
womenPartner[index] = men[free];
37+
menEngaged[free] = true;
38+
engagedCount++;
39+
}
40+
else {
41+
String currentPartner = womenPartner[index];
42+
if (morePreference(currentPartner, men[free], index)) {
43+
womenPartner[index] = men[free];
44+
menEngaged[free] = true;
45+
menEngaged[menIndexOf(currentPartner)] = false;
46+
}
47+
}
48+
}
49+
}
50+
printCouples();
51+
}
52+
53+
private boolean morePreference(String curPartner, String newPartner, int index) {
54+
for (int i = 0; i < N; i++)
55+
{
56+
if (womenPref[index][i].equals(newPartner))
57+
return true;
58+
if (womenPref[index][i].equals(curPartner))
59+
return false;
60+
}
61+
return false;
62+
}
63+
64+
private int menIndexOf(String str) {
65+
for (int i = 0; i < N; i++)
66+
if (men[i].equals(str))
67+
return i;
68+
return -1;
69+
}
70+
71+
private int womenIndexOf(String str) {
72+
for (int i = 0; i < N; i++)
73+
if (women[i].equals(str))
74+
return i;
75+
return -1;
76+
}
77+
78+
public void printCouples() {
79+
System.out.println("Couples are : ");
80+
for (int i = 0; i < N; i++) {
81+
System.out.println(womenPartner[i] +" "+ women[i]);
82+
}
83+
}
84+
}

0 commit comments

Comments
 (0)