In 1962,
David Gale and
Lloyd Shapley proved that, for any equal number of participants of each type, it is always possible to find a matching in which all pairs are stable. They presented an algorithm to do so. In 1984,
Alvin E. Roth observed that essentially the same algorithm had already been in practical use since the early 1950s, as the "Boston Pool algorithm" used by the
National Resident Matching Program. The Gale–Shapley algorithm involves a number of "rounds" (or "
iterations"). In terms of job applicants and employers, it can be expressed as follows: • In each round, one or more employers with open job positions each make a job offer to the applicant they prefer, among the ones they have not yet already made an offer to. • Each applicant who has received an offer evaluates it against their current position (if they have one). If the applicant is not yet employed, or if they receive an offer from an employer they like better than their current employer, they accept the best new offer and become matched to the new employer (possibly leaving a previous employer with an open position). Otherwise, they reject the new offer. • This process is repeated until all employers have either filled their positions or exhausted their lists of applicants.
Implementation details and time analysis To implement the algorithm efficiently, each employer needs to be able to find its next applicant quickly, and each applicant needs to be able to compare employers quickly. One way to do this is to number each applicant and each employer from 1 to n, where n is the number of employers and applicants, and to store the following
data structures: • A
set of employers with unfilled positions • A one-dimensional
array indexed by employers, specifying the preference index of the next applicant to whom the employer would send an offer, initially 1 for each employer • A one-dimensional array indexed by applicants, specifying their current employer, initially a
sentinel value such as 0 indicating they are unemployed • A two-dimensional array indexed by an applicant and an employer, specifying the position of that employer in the applicant's preference list • A two-dimensional array indexed by an employer and a number i from 1 to n, naming the applicant who is each employer's preference Setting up these data structures takes O(n^2) time. With these structures it is possible to find an employer with an unfilled position, make an offer from that employer to their next applicant, determine whether the offer is accepted, and update all of the data structures to reflect the results of these steps, in constant time per offer. Once the algorithm terminates, the resulting matching can be read off from the array of employers for each applicant. There can be O(n^2) offers before each employer runs out of offers to make, so the total time is O(n^2). Although this time bound is
quadratic in the number of participants, it may be considered as
linear time when measured in terms of the size of the input, two matrices of preferences of size O(n^2).
Correctness guarantees This algorithm guarantees that: ; Everyone gets matched: At the end, there cannot be an applicant and employer both unmatched. An employer left unmatched at the end of the process must have made an offer to all applicants. But an applicant who receives an offer remains employed for the rest of the process, so there can be no unemployed applicants. Since the numbers of applicants and job openings are equal, there can also be no open positions remaining. ; The matches are stable: No applicant
X and employer
Y can prefer each other over their final match. If
Y makes an offer to
X, then
X would only reject
Y after receiving an even better offer, so
X cannot prefer
Y to their final match. And if
Y stops making offers before reaching
X in their preference list,
Y cannot prefer
X to their final match. In either case,
X and
Y do not form an unstable pair. ==Optimality of the solution==