DON'T use objects or fancier data structures than the lists (= arrays) and variables above. You are REQUIRED to use the simple lists and stack as described above since the detailed time analysis of the algorithm that we'll perform in class depend on them.
Note that the wife list (and also the husband list) keeps track of the matching that the algorithm constructs. Hence, there is no need for an additional data structure to store the stable matching.
Note that in contrast with the notation used in these slides, Python indexes lists and arrays starting at 0 (not at 1). So in this homework we'll index men and women starting at 0. Hence, a man's favorite woman would be his 0th preference (not his 1st preference!).
For illustration purposes, the Python structures that will represent the preferences for men and for women for the example provided in Problem 2 below are:
menPreferences = [[0, 2, 4, 3, 1], [3, 1, 4, 2, 0], [2, 4, 0, 1, 3], [3, 1, 4, 2, 0], [0, 1, 2, 3, 4]] womenPreferences = [[1, 2, 3, 4, 0], [2, 3, 4, 0, 1], [3, 4, 0, 1, 2], [4, 0, 1, 2, 3], [0, 1, 2, 3, 4]]With this representation, the ith preferred woman of man j can be found at
menPreferences[j][i]For instance, man 1's favorite woman is woman 3 since:
menPreferences[1][0] is equal to 3.The stack of free men would be initially be:
FreeMen = [n-1, n-2, ..., 2, 1, 0]See http://docs.python.org/2/tutorial/datastructures.html for a description of how a list can be used as a stack in Python.
Finally, the list of lists ordinal for this example will be:
ordinal = [[4, 0, 1, 2, 3], [3, 4, 0, 1, 2], [2, 3, 4, 0, 1], [1, 2, 3, 4, 0], [0, 1, 2, 3, 4]]So woman 2 prefers man 3 over man 1 since ordinal[2][3] < ordinal[2][1]. Note that ordinal[2][3] == 0 0th preference) and ordinal[2][1] == 3 (3rd preference).
The total execution time should be printed by your program both in verbose and non-verbose modes.
|   |
|
Man (husband): M0 M1 M2 M3 M4 Woman (wife): W4 W2 W1 W0 W3
n= 10, 100, 1000, 2000, 3000, 4000, 5000.
W0, W1, W2, ....., W(n-2), W(n-1)
M(n-1), M(n-2), ...., M2, M1, M0.