我一直在研究我所在班级的“稳定婚姻问题”,我无法正确地实现Gale-Shapely算法。提示时输出:
for (int q = 0; q < numMen; q++ )
System.out.println(couples[q].toString());
是
“Brian Patricia
乔治南希
约翰苏珊
罗伯特南希
斯蒂芬乔伊斯“
只有他们都在不同的路线上。然而,正确的组合是“Brian Patricia George Joyce John Susan Robert Nancy Stephen Anne 。问题是它不会删除”Nancy“作为George的配对。也是为了背景men [] []是男性偏好的矩阵,男性[i] [0]是每个男性的名字,0
public static void SolveMenMarriages() {
int i, k;
String m, w, fiance;
i = 0;
fiance = null;
while (i < numMen) {
k = 1;
m = men[i][0];
w = men[i][k];
if (isProposedTo(w))
fiance = findFiance(w);
if (!isProposedTo(w)) {
couples[i] = new couple(m, w);
i++;
} else if (checkPreferenceWoman(fiance, m, w).equals(m)){
couples[i] = new couple(m, w);
removeFiance(fiance);
i = fianceIndex(fiance);
} else {
k++;
}
if (i == (numMen - 1)) {
for(int v = 0; v < numMen; v++) {
if (couples[v].woman == null)
i = 0;
else
break;
}
}
}
}
public static String checkPreferenceWoman(String M1, String M2, String w) {
int c;
c = 0;
while (c < numWomen) {
if (women[c][0].equals(w)) {
for (int d = 1; d < (numMen + 1); d++) {
if (women[c][d].equals(M2)) {
return M2;
}
}
}
c++;
}
return M1;
}
}