Skip to content

Instantly share code, notes, and snippets.

@osjayaprakash
Last active October 6, 2015 23:37
Show Gist options
  • Save osjayaprakash/3070993 to your computer and use it in GitHub Desktop.
Save osjayaprakash/3070993 to your computer and use it in GitHub Desktop.
Stable Marriage Problem
while (unenm != 0) {
while (men[j] != -1)
j++; //man no. j is unengaged. he proposes to men2[j].
int wc = m[j][men2[j]];
if (women[wc] == -1){ //wc and j get engaged
women[wc] = j;
men[j] = wc;
unenm --;
j = 0;
} else if (w[wc][women[wc]] > w[wc][j]) {
//wc and j get engaged, but total number of unemployed men remains same
men[women[wc]] = -1;
women[wc] = j;
men[j] = wc;
j = 0;
} else {
//else keep j the same, so that this man can explore further options, until he gets engaged
men2[j] ++;
}
}
for (j=1; j<=n; j++)
printf("%d %d\n",j,men[j]);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment