Duplicated suitors in SuitorGaleShapley algorithm
zitmen opened this issue · comments
After few iterations the queue of freeSuitors
contains duplicit copies of single suiter because they are never excluded from the queue, not even after they get engaged.
This consequently causes NullPointerException
(due to the variable w
), because even if the prefs
are empty the suitor is still in the queue.
Calling freeSuitors.poll();
after each engagement (lines 97 and 104) seems to solve the problem.
Snippet:
// As per wikipedia algorithm
while (!freeSuitors.isEmpty()) {
// The next free suitor who has a reviewer to propose to
MTuple m = freeSuitors.peek();
// m's highest ranked such woman who he has not proposed to yet
W w = m.prefs.poll();
if(w == null) {
freeSuitors.poll();
continue;
}
// Tf w is free
if (!engagements.containsKey(w)) {
// (m, w) become engaged
engagements.put(w, m);
freeSuitors.poll();
} else {
// Some pair (m', w) already exists
MTuple mPrime = engagements.get(w);
if (reviewerPreference(w).compare(mPrime.suitor, m.suitor) < 0) {
// (m, w) become engaged
engagements.put(w, m);
freeSuitors.poll();
// m' becomes free
freeSuitors.add(mPrime);
}
}
}