Professional Documents
Culture Documents
1.00 Problem Set #9: Sample Solution A: "Greedy" Algorithm
1.00 Problem Set #9: Sample Solution A: "Greedy" Algorithm
Computation is as follows:
Possible modifications
This algorithm treats all characteristics equally, including those that the participants
designated as dont care values. It could instead assign lower scores to dont care
type matches, since the user didnt think that characteristic was important.
Also, it doesnt 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
wouldnt work well for any of the other characteristics in this particular survey, though.)
The task is to match all males with femalesor at least as many as possible. High
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
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
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
When one of the following conditions is true, a solution has been reached:
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.
In the initial condition, the paired couples list is empty, and the unpaired lists contain all
the subjects.
Lets say that the match with the highest compatibility score (of 7) occurred for male2
and female1. Lets 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 paired couples unpaired males unpaired females paired couples
male1 female2 (male2, female1) male2 female1 (male1, femalem)
male3 female3 male3 female2
... ... ... ...
malen femalem malen femalem-1
male2 removed female1 removed male1 removed femalem removed
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).