0

I've been working on the "Stable Marriage Problem" for a class I'm in and I cannot get it to implement the Gale-Shapely Algorithm correctly. The out put when prompted:

for (int q = 0; q < numMen; q++ ) 
System.out.println(couples[q].toString());

is "Brian Patricia George Nancy John Susan Robert Nancy Stephen Joyce" only they are all on different lines. How ever the proper combination is "Brian Patricia George Joyce John Susan Robert Nancy Stephen Anne. The issue is it won't remove "Nancy" as George's pair. Also for background men[][] is the matrix of the men's preferences in the format men[i][0] is the name of each man for 0 < i < 4 and men[i][k] with k as the preferences in the positions 1 < k < 5. Same thing for the women matrix. Also couples[] is the array of object "couple" which is basically and array that holds each matched pair of a man and a woman. Here's the place where the problem is:

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;
}
}

0 Answers0