1. A Dating Algorithm
Implementing the Simplex Algorithm
Implementing Euclid’s GCD
Presented in partial fulfillment of the requirements for Project 2
By: David Poeschl & Tim Reynolds
2. A Quick Preview
• The Problem: Weighted Bipartite Matching
– Given a group of n (heterosexual) boys and n (heterosexual)
girls, each of whom has previously assigned values to each
member of the opposite gender, find a matching of boys with
girls which maximizes happiness (which can be defined many
ways). To do this, we designed the…
• Reynolds-Poeschl Matching Algorithm (RPMA)
– Works by converting the data into a linear programming problem
which can then be solved by…
• The Simplex Algorithm
– A well known algorithm for solving linear programming problems.
Since Simplex uses a lot of operations on rational numbers,
we’ve created our own Rational Number Class which
implements
• Euclid’s GCD
3. Obligatory Diagram
Input: Output:
Preferences An optimal matching
LP Problem (only once…) Solution to the LP Problem
Integers x & y (repeatedly…) The GCD of x and y
4. Algorithm – Simplex
• The Simplex Algorithm portion of the project is a stand-
alone application which solves any linear programming
problem with a feasible solution.
– Input: A LP Problem P = (c[ ], b[ ], A[ ][ ]) in n variables
{x1 ,… xn} and m constraints:
Maximize c1x1 + … + cnxn (the objective function) subject to:
A1,1x1 + … + A1,nxn ≤ b1
…
Am,1x1 + … + Am,nxn ≤ bm
– Output: An array representing a setting of the values of
{x1 ,… xn} which maximizes the objective function (if possible).
5. Algorithm – Simplex
• Simplex Algorithm
– If you honestly want us to drag you through the details of the Simplex
Algorithm and you ∈ Class–{Mike, Jarod, Will}, raise your hand…
– No takers?
– Last chance…
– Jarod, put your hand down.
– Seriously, put your hand down.
• Everybody* knows how to implement the Simplex Algorithm by now,
so we’ll move on to the mildly interesting** portion.
* Unless you’re a dirty*** cheater
** Interesting is a relative term
*** Dirty means “of low academic integrity”
6. Algorithm – RPMA
• Input: Two n by n matrices PBG and PGB
– PBGi, j = The value that boy i assigned girl j
– PGBi, j = The value that girl i assigned boy j
• Output: A set of n boy-girl pairs which produce
the optimal happiness.
– Each boy and girl appears exactly once in the set of
pairings.
• The values assigned are positive integers. The
lower a value is, the more (for example) the boy
likes that particular girl.
7. Algorithm – RPMA
• What is happiness?
– The happiness of a complete matching is defined as the PBG Lisa Jess
sum of the happiness of each individual pair. The
happiness of a pair can be defined in several ways. Joe 1 4
Consider the following matrices:
Bob 3 7
• Maximizing Happiness (Higher value = happier)
– Define max_happy as the highest value appearing in either matrix, P GB
Joe Bob
and min_happy similarly. Then…
– Additive Happiness Lisa 2 6
• Happiness(Boy b, Girl g) =
[max_happy + min_happy – PBGb, g ] + Jess 1 5
[max_happy + min_happy – PGBg, b ]
– Multiplicative Happiness
• Happiness(Boy b, Girl g) = Note: Lower values = more attraction
[max_happy + min_happy – P b, g ] *
BG
[max_happy + min_happy – PGBg, b ]
Ex. Multiplicative Happiness (Bob, Lisa)
• Minimizing Unhappiness (Higher value = less happy)
– Additive Unhappiness = [7+1-3] * [7+1-6] = 5 * 2 = 10
• Happiness(Boy b, Girl g) = -1 * (PBGb, g + PGBg, b)
– Multiplicative Unhappiness
• Happiness(Boy b, Girl g) = -1 * (PBGb, g * PGBg, b)
8. Algorithm – RPMA
• We now proceed to creating an LP:
– We have n * n variables {x1 ,… xn*n}
• Variable xi represents whether the boy-girl pair of (Boy (i-1) / n + 1,
Girl [(i -1) mod n]+1) is part of our matching
– We have 2n constraints…
• Constraints 1 through n ensure that each boy is paired with no more than
one girl.
• Constraints n+1 through 2n ensure that each girl is paired with no more than
one boy.
• If we are minimizing unhappiness, then we need one additional constraint
-1*x1 + -1*x2 + … + -1*xn*n ≤ -1*n
– This causes any matching with less than n pairs to be infeasible.
– The objective function
• Happiness(Boy (1-1) / n + 1, Girl [(1-1) mod n]+1)*x1 +
Happiness(Boy (2-1) / n + 1, Girl [(2-1) mod n]+1)*x2 +
…+
Happiness(Boy (n*n-1) / n + 1, Girl [(n*n-1) mod n]+1)*xn*n
9. Algorithm – RPMA
• A (potentially) big problem
– What if somebody assigns their favorite person a value of 1,
but assigns everybody else very large values like 999,937 or
827,399?
• This person is selfishly trying to hack our algorithm to guarantee
they get their number one choice.
– Solution: Convert values to rankings, then calculate
happiness values.
PBG Lisa Jess PBG Lisa Jess
Joe 1 4 Joe 1 2
Bob 3 7 Bob 1 2
11. Analysis, Testing & Results
• Data Structures
– We used Arrays instead of ArrayLists…
• Because we did not use iterative structures, this
improved our performance.
– The implementation of our Rational Number
Class greatly decreased our performance.
Every time two rational numbers are added,
we actually perform 3 integer multiplications,
then run GCD to reduce the new rational.
12. Analysis, Testing & Results
• Testing the Simplex Algorithm
– A test case for which there is no feasible solution
– A test case for which the solution is unbounded
– A test case for which the initial basic solution is not feasible, but there
exists a feasible solution somewhere
– A large test case with 3600 variables and 121 constraints.
• Testing the RPMA (Matching)
– Wrote a program that, given an integer n, generates random data sets
of for 2*n people
• Two n x n arrays of positive integers in [1, 100]
– We then have n2 variables and 2*n constraints
13. Analysis, Testing & Results
• Results
– Ran timings on random data sets for n boys & n girls
• n = 1, 2, 5, 10, 15, 20, 25, 30, 35, 40, 50, 60.
Time versus n (Logarithmic Scale)
100000
10000
1000
Time (s)
100 RPMA
10
1
0.1
5 10 15 20 25 30 35 40 45 50 55 60
n
14. Extension: The Poeschl-Reynolds Gender
Preference Neutral Matching Algorithm
(or NAMbLA)
• Thus far, we have only considered cases with n boys and n girls, each of
whom is heterosexual…
– But what if some boys like other boys? What if some girls like boys and
girls?
• Consider a new, non-heterocentric problem which considers n
people, each of whom rates everybody else.
– Creates an n x n matrix P
– Lower rating values mean more attraction
– A value of 0 at Pi, j to indicates that person i is not attracted to person j
at all and should not be matched with them.
• We can then support heterosexual, bisexual, or homosexual users.
• Alternatively, if you think homosexuality is immoral, we could be
trying to find an optimal seating of n friends/enemies on a 2 seat
wide rollercoaster train.
15. Extension: The Poeschl-Reynolds Gender
Preference Neutral Matching Algorithm
• This new version will have (n C 2) variables and n+1 constraints. Xi p1 p2 p3 P4
– Variables p1 0 1 2
• Variable X0 represents whether person 1 and 2 are paired together
p2 3 4
• Variable X1 represents whether person 1 and 3 are paired together …
• Variable Xn-2 represents whether person 1 and n are paired together p3 5
• … p4
• Variable Xn-1 represents whether person 2 and 3 are paired together
• Variable Xn represents whether person 2 and 4 are paired together ……
– Constraints
• Constraint i is made up of all 0’s except where person i is part of that pairing
– Constraint 1: 1 1 1 0 0 0 [ p1 is part of pairings 0, 1, and 2]
– Constraint 2: 1 0 0 1 1 0
– Constraint 3: 0 1 0 1 0 1
– Constraint 4: 0 0 1 0 1 1
– Objective Function (Minimizing Additive Unhappiness Only)
• The coefficient on Xi is
– If both people involved in Xi have non-0 entries for each other, -1 * (summed
happiness)
– Else, -1 * (an arbitrarily large value)
17. The Poeschl-Reynolds Gender Preference
Neutral Matching Algorithm
• Why is this interesting?
– You mean besides the political statement?
– The constraints in this generalization are much more complicated than in the
heterocentric version
– We also have to consider what happens if a person absolutely cannot be paired with
somebody else. We might end up with several singletons
– It really is an extension of the original problem.
P {Boys} {Girls}
• Given PBG and PGB , create our new matrix P =
• This gives the same solution as our original
algorithm. {Boys} 0…0 PBG
. .
. .
0…0
{Girls} PGB 0…0
. .
. .
0…0
19. References & Thanks
- Introduction to Algorithms, Second Edition Cormen, Leiserson, Rivest, Stein
- Thanks to Mike & Jarod for fun discussions about rounding errors
- We told you so.
- Wikipedia
- It’s always correct.