Last active
October 6, 2015 23:37
-
-
Save osjayaprakash/3070993 to your computer and use it in GitHub Desktop.
Stable Marriage Problem
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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