Thursday, 23rd October at 12:00 (noon)
For this project you will implement the stable marriage problem to pair hands. Every left hand needs a right hand pair. You will write a high up "Hand" class that contains the functionality of all Hands. You will also have 2 derived classes, "LeftHand" and "RightHand" that contain the functionality that each needs to extend from the base class (they will be different). In a main program, you will read in a test case from an input file that says the number of Hands to create, and specifies each Hand's (both Left and Right) preferences for the other kind of Hand. Then you will implement the stable marriage algorithm to pair your Hands so they live happily ever after.
The
stable marriage problem is a problem of finding a stable matching
between 2 matched sets. [Originally this was matches between men and
women, but to be politically correct, I use hands now. :-)] Given n
left hands and n
right hands, where each hand has ranked all members of the opposite kind of hand with a unique number between 1 and n in order of preference, match the left and right hands such that there are no 2 hands of opposite kinds who would both rather have each other than their current partners. If there are no such hands, the marriages are "stable".
Here is the algorithm for creating a stable pairing:
function stableMatching { Initialize all l in L and r in R to free while exists free left l who still has a right r to propose to { r = l's highest ranked such right if r is free (l, r) become engaged else some pair (l', r) already exists if r prefers l to l' (l, r) become engaged l' becomes free else (l', r) remain engaged } }
n
. The following 2n
lines will have the preferences for the n
left hands, then the n
right hands. The preferences for each left hand will be for the n
right hands, with the highest preferred first. Same for right hands.
So the following: n
left-right pairs), such as this. You can just output to the screen using cout. The output should be in ascending order of the left hands (id the lefties in the order in which the input is provided and output in the same order). Print nothing else but the ids involved in each pairing, and list each pairing on its own line, then separate cases with a blank line.
Hand.h
and Hand.cpp
) that is the base class for the LeftHand and RightHand classes. The Hand class contains the functionality common to both Left and Right Hand classes. You will also define the LeftHand and RightHand classes (each with a .h and .cpp file) that derive from the Hand class and are more specialized. There should be some difference between the Left and Right Hands! (don't make them empty classes). testSMPHand.cpp
that implements the stable marriage algorithm several times over based on the input file - for each case, pairing Left and Right hands based on their preference. Generate output as specified above. Hand.h
,
Hand.cpp
, LeftHand.h
,
LeftHand.cpp
, RightHand.h
,
RightHand.cpp
, and testSMPHand.cpp
. Make sure your name is on the top of
each file.
Hand.h
Hand.cpp
LeftHand.h
LeftHand.cpp
RightHand.h
RightHand.cpp
testSMPHand.cpp