1.00 Problem Set #9: Sample Solution A “Greedy” Algorithm Problem 2: Compatibility Algorithm Refer to the more detailed description of classes in Problem 1. A Pair is a potential (male, female) pair of Lover objects. We assign an integer compatibility score for each pair. A compatibility value of 0 means that the two are not at all compatible. Each number after that is the number of characteristics (from the survey) that match both male→female and female→male directions. Male and female each have a “myProfile” and “desiredProfile”. Computation is as follows: For each characteristic in the list if male’s desiredProfile value is 0 (don’t care) then if the female’s desiredProfile value is 0 (don’t care) OR the same as the male’s myProfile, increment the score else if female’s desiredProfile value is 0 (don’t care) then if the male’s desiredProfile value is the same as the female’s myProfile value, increment the score else if the male’s value is same as female’s value then increment the score Possible modifications This algorithm treats all characteristics equally, including those that the participants designated as “don’t care” values. It could instead assign lower scores to “don’t care” type matches, since the user didn’t think that characteristic was important. Also, it doesn’t try to assign any “fuzzy” matches. For example, instead of requiring an exact match for age brackets, two potential mates who only differ by one age brackets could be assigned a value of “0.5” instead of “0” for that characteristic. (Fuzzy matches wouldn’t work well for any of the other characteristics in this particular survey, though.) 1.00 PS#9 Sample Solutions 1/3 Problem 3: Matching Algorithm The task is to match all males with females—or at least as many as possible. High compatibility is most important, so we can choose either to try to maximize total compatibility (the sum of compatibility scores for the final resulting matches), or average compatibility. Either is okay, but we have chosen to maximize total compatibility in this solution. The algorithm described here may not produce the optimal results, but it will at least find the very best match, second best match, etc, and it will do so relatively efficiently. Step 1. Put all male contestants in an unpaired males list. Put female contestants in an unpaired female list. Prepare an empty list for paired couples. Go through all possible matches of males to females (or vice versa) and find the couple with the HIGHEST COMPATIBILITY. Add this couple to a “paired list”, and remove them from their respective unpaired lists. Optional: instead of just taking one couple with the highest score, take a list of all couples with the same (highest) score. We will refer to this option as the “multiple solution” option. It will result in a better solution in most cases, but it is more complicated. Step 2. Recurse through the remaining list in the same was as step 1. (Find the couple with the highest compatibility from the remaining unpaired lists, add them to the paired list, and remove from the unpaired list.) When one of the following conditions is true, a solution has been reached: a) unpaired males list is empty b) unpaired females list is empty c) the best compatibility score among the remaining unpaired people is 0 When one of these occurs, the algorithm should terminate. The list of pairs should be output, and the list of unpaired males and females should also be output. Optional multiple solution: When following all “highest compatibility” valued solutions, it is necessary to copy the three lists: unpaired males, unpaired females, and paired couples each time a solution is tried. This is because each “branch” must be evaluated independently. Furthermore, we need to keep track of the compatibility sum, which is the sum of all the compatibility scores of pairs in the paired list. 1.00 PS#9 Sample Solutions 2/3 As an example, consider the following case in which we have been given n males and m females. Initial condition: unpaired males unpaired females paired couples male1 male2 ... malen female1 female2 ... femalem (empty list) In the initial condition, the paired couples list is empty, and the unpaired lists contain all the subjects. Let’s say that the match with the highest compatibility score (of 7) occurred for male2 and female1. Let’s also assume that the match between male1 and femalem resulted in a compatibility score of 7 as well. We copy all three lists for each scenario, and try them out separately: unpaired males unpaired females male1 male3 female2 female3 paired couples unpaired males unpaired females ... ... malen femalem male2 male3 ... malen male2 removed female1 removed male1 removed (male2, female1) female1 female2 paired couples (male1, femalem) ... femalem-1 femalem removed Example of recursion using multiple possible solutions The diagram shows the three lists as the first recursion happens. On the left, male2 has been paired with female1, and the two have been removed from their respective unpaired lists. On the right, male1 has been paired with femalem, and the two have been removed from their respective unpaired lists. In effect, this will create a tree, with as many children nodes as there are “highest value compatibility scores” from the remaining unpaired lists. The match() method described in problem 1 must call itself recursively several times in a loop in order to implement this algorithm. All lists used here can be Vector objects. (We could also use arrays, but would have to resize them…) The algorithm described here is O(n3). 1.00 PS#9 Sample Solutions 3/3