An efficient implementation of the Gale and Shapley “propose-and-reject” algorithm

We consider a version of the Hospitals/Residents problem which was first defined in 1962 by Gale and Shapley [9] under the name “College Admissions Problem”. In particular, we consider the Firms/Candidates problem, where each Firm wishes to hire at least one Candidate and each Candidate can be finally assigned to a single Firm. We present an efficient implementation of the Gale and Shapley “propose-and-reject” algorithm when applied to the case of the Firms/Candidates problem.


Introduction
In terms of graph theory, marriage is expressed as a matching problem. In the simplest form of a matching problem, we are given a graph where the edges indicate compatibility, i.e., vertices connected by an edge can be paired or "married". Given a graph G = (V, E), a matching is a subgraph of G where every node has degree 1, implying that everybody can be married just to one person. When every node is involved in a matching, then this matching is called perfect matching. The need to efficiently compute matchings arises often in a wide range of popular algorithmic problems (see, e.g., [4,16,19]) which include scheduling, bipartite matching, independent set, competitive In short, the SMP can be informally stated as follows. Given n men and n women each with their own preference list, provide an algorithm that computes a stable set of matchings, i.e., matchings not containing blocking pairs. A man and a woman form a blocking pair if they are not matched but prefer each other to their current matches, implying that these two persons are likely to cheat on their partners.
Gale and Shapley proposed a polynomial-time deferred acceptance algorithm for the SMP, the so-called Gale and Shapley algorithm, which always finds a stable matching, thus providing a constructive proof of the fact that any instance of the SMP admits at least one stable matching. In particular, their algorithm is a "propose-and-reject" procedure and works as follows. While there are men that are not matched, have them propose to the woman that is next on their preference list, and have each woman select their favorite proposal based on her preference list. The algorithm terminates once every woman has received a proposal.
The Gale and Shapley algorithm always returns a stable, male-optimal and female-pessimal matching. Male-optimal implies that men, going down their preference list, are eventually engaged to their most preferred available women. Female-pessimal implies that women, going up their preference list, are eventually engaged to their most preferred available men. In particular, the (men-oriented version of the) Gale and Shapley algorithm incorporates a sequence of proposals from men to women and computes a stable matching which is uniquely favorable to men, i.e., male-optimal: every man is matched to his best stable female partner. However, in the computed matching each woman is assigned to her least-worst male partner; that is, the algorithm returns a female-pessimal result. Analogously, if women propose to men, then the algorithm computes a stable matching which is uniquely favorable to women, i.e., female-optimal, male-pessimal.
Gale and Shapley also studied the "College Admissions" problem, a many-to-one stable matching problem stated in terms of colleges and students (instead of men and women) as follows [31]. Each college c ∈ C wishes to be matched to q(c) ≥ 1 students. Each student is interested in being matched to only 1 college. That is, each student has a preference over colleges, each college has a preference over individual students, and a matching is a function that assigns each student to at most one college, and each college c to at most q(c) students. They observed that essentially the same deferred acceptance algorithm -with college c proposing at each point to its q(c) most preferred students who hadn't yet rejected it in the college-proposing version, or rejecting all but the q(c) most preferred applications it had received at any point of the student-proposing versionwould produce a stable matching defined as before. That is, the outcome produced by the algorithm would not admit any student-college blocking pairs defined precisely as for the SMP. Their proof of this result was constructive, i.e., they suggested an algorithm for computing a stable matching.
In [9], the Hospitals/Residents problem (HRP) was also stated. The reference to Hospitals and Residents was due to the application of the "College Admissions problem" to large-scale matching schemes administering the annual match of graduating medical students to hospital appointments in the US (National Resident Matching Program [29,28]), Canada (Canadian Resident Matching Service [3]) and Scotland (Scottish Pre-registration house officer Allocations-SPA [11]).
An instance of the Hospitals/Residents problem involves a set R of residents and a set H of hospitals. Each resident r ∈ R is seeking a post at one hospital, and each hospital h ∈ H offers q(h) ≥ 1 posts. Each resident in R ranks a subset of H in strict order, and each hospital h ∈ H ranks its applicants in strict order. An agent p ∈ R ∪ H finds an agent q ∈ R ∪ H acceptable if q appears on p' s preference list; p finds q unacceptable otherwise. A matching M is a subset of R × H, where (r, h) ∈ M implies that (i) r, h find each other acceptable, (ii) r is assigned to at most one hospital in M , and (iii) at most q(h) residents are assigned to h ∈ M .
The classical Stable Marriage problem [7,32,17] is a restriction of the Hospitals/Residents problem in which each hospital has exactly one post, the number of hospitals equals the number of residents, and all preference lists are complete. For a given instance I of the Hospitals/Residents problem, the Gale and Shapley algorithm for the Stable Marriage problem [9] may be extended in order to find a stable matching for I (such a matching in I always exists) in O(mn) time, where n = |R| and m = |H| [7]. Again, if the residents propose to the hospitals (the resident-oriented algorithm), then a stable matching M is computed which is uniquely favorable to the residents: every resident r ∈ M is assigned to her best stable partner, and every resident unassigned in M is unassigned in any stable matching. Similarly, if the hospitals propose to the residents (the hospital-oriented algorithm), then a stable matching M is computed which is uniquely favorable to the hospitals: every hospital h ∈ H is assigned either its q(h) best stable partners, or a set of fewer than q(h) residents; in the latter case, no other resident is assigned to h in any stable matching.
Although an instance of the Hospitals/Residents problem may admit more than one stable matchings, every stable matching has particular properties summarized in the "Rural Hospitals Theorem" [29,10,30]. The "Rural Hospitals Theorem" states that for a given instance of the Hospitals/Residents problem, (i) each hospital is assigned the same number of residents in all stable matchings, (ii) exactly the same residents are unassigned in all stable matchings, and (iii) any hospital that is under-subscribed in one stable matching is matched with precisely the same set of residents in all stable matchings. There exists no algorithm that can produce a stable matching changing the allocation of residents to unpopular hospitals. In the experience of the NRMP, these tend to be rural hospitals.
Several other variations of the original problem have been studied [22,25]. These variations mainly arise in practical contexts. For example, in the original version of the Stable Marriage problem preference lists must be complete and strictly ordered. However, these assumptions do not really hold in real-world applications, especially in a large-scale matching system. Having said that, two natural relaxations of the original problem result in two variants called "Incomplete Preference Lists" and "Incomplete Preference Lists with Ties" [14,15]; the former allows incompleteness while the latter allows both incompleteness and ties in preference lists and under particular assumptions can be computationally hard [21,24]. Other interesting variants [15] include (i) the Stable Roommates Problem [7] which is a a non-bipartite extension of the original Stable Marriage problem with applications to the formation of player pairs in chess tournaments [18] and pairwise kidney exchange between incompatible patient-donor pairs [13,33], (ii) the Man-Exchange Stable Marriage where stability requires also that no two men prefer to exchange their partners making the problem computationally hard [12], (iii) Many-to-Many Stable Marriage, a many-to-many extension of the original Stable Marriage problem, (iv) the Student Project Allocation Problem where students are assigned to projects based on his/her preferences over projects (see for example [1]), (v) the 3-Dimensional Stable Matching proposed by Knuth [17] which involves 3 set of agents, (vi) the One-Sided Preference Lists where only one party has preference lists over the other.
In this work, we consider the Firms/Candidates problem (FCP, in short), a variation of the Hospitals/Residents problem. In particular, given two non-empty sets of Firms and Candidates together with their complete preference lists with no ties, we use the Gale and Shapley "propose-and-reject" algorithm for producing stable matchings. Our contribution is a "search-without-searching" implementation of the Gale and Shapley algorithm for the Firms/Candidates problem.
The rest of the paper is structured as follows: in Section 3 we overview the Gale and Shapley algorithm and its application to the Firms/Candidates problem. In Section 4, we present in detail our implementation approaches and obtained experimental results. We conclude in Section 5.

. SMP statement
An instance I of the classical Stable Marriage problem (SMP) involves n men and n women, each of whom ranks all the members of the opposite sex in strict order of preference. A matching M in I is a one-to-one correspondence between the men and women. We say that a (man, woman) pair (m, w) blocks M , or is a blocking pair with respect to M , if m prefers w to p M (m), and w prefers m to p M (w), where p M (q) denotes the partner of q in M . A matching that admits no blocking pair is said to be stable. It is known that every instance of SMP admits at least one stable matching, and that such a matching can be found in O(n 2 ) time using the Gale and Shapley algorithm [9].
This version of the Stable Marriage problem (SMP) can be modelled by means of a bipartite graph G = (U, V, E), where U denotes the set of men and V denotes the set of women. Both sets are equally-sized, i.e., |U | = |V | = n. Furthermore, each man m ∈ U (resp., each woman w ∈ V ) maintains a strictly ordered preference list, R m (resp., R w ), containing all women w ∈ V (resp., all men in m ∈ U ). Each such list is complete (i.e., it contains n elements) and shows men's (resp., women's) preferences for all women (resp., men) in decreasing order. For example, let n = 4, U = {A, B, C, D}, V = {a, b, c, d} and R A = [d, a, b, c]. List R A indicates that A's first preference is woman d, women a and b follow and woman c is A's lowest preference. Then, a solution to the Stable Marriage problem consists in finding a perfect matching M in G, i.e., a subset of n edges of E where each vertex of U ∪ V appears exactly once.

The Gale and Shapley algorithm for SMP
Gale and Shapley actually devised a "propose-and-reject" algorithm which works as follows. The "propose-and-reject" procedure takes place over several runs or days. Each day is broken up into three parts: the morning, the afternoon, and the evening. In the morning, each man proposes to his favorite woman who is still on his list. Initially, every woman is on his list. In the afternoon, each women who received at least one proposal picks her favorite man and gets engaged to him. She rejects all other men who proposed to her but have lower priority in her list. That night, any man who was rejected by a woman crosses that woman off his list. This procedure is repeated in every run. When every woman is engaged to at most one man, the algorithm stops and returns the result.
The Gale and Shapley algorithm for SMP can be formally described as follows. Input.
• A set of n men: M = {m 0 , m 1 , . . . , m n } • A set of n women: W = {w 0 , w 1 , . . . , w n } • A preference list for each man and woman: for example R m i = (w 10 , w 3 , . . . , w 7 ) • define R w i (m j ) to be the index of m j in w i 's preference list Note that a smaller index indicates a stronger preference. Specifically, R w (m) < R w (m 0 ) means that w prefers m over m 0 . Output.
• A stable matching of men and women: S ⊆ M × W • S is a stable matching if all of the following are true: 1. Initially all men and women are free 2. While there exists a free man m, he proposes to her most preferred woman w whom he has not proposed to yet as follows: (a) m proposes to w (b) If w is free, m and w get engaged and (m, w) is added to S (c) If w is currently engaged to another man, m 0 , and she prefers m 0 over m, R w (m) > R w (m 0 ): m remains free (d) If w is currently engaged to another man, m 0 , and she prefers m over m 0 , R w (m) < R w (m 0 ): w breaks off the engagement with m 0 and gets engaged to m. (m 0 , w) is removed from S and (w, m) is added to S. m 0 becomes free. 3. return S as a stable matching.
Below, we summarize basic statements from regarding correctness and running time analysis of the Gale and Shapley algorithm for the men-oriented version of the SMP. Proofs are omitted but and be found in [9,7,11,16,23].
Observation 3.1. Once a woman gets engaged, she never becomes free again and gets engaged to more preferred men.
Observation 3.2. Men stay engaged to the same woman or continue getting engaged to worse women.

FCP
In their seminal work [9], David Gale and Lloyd Shapley considered a model with two sets of agents, for example workers and firms, that must be paired with each other. If a particular worker is hired by employer A, but this worker would have preferred employer B, who would also have liked to hire this worker (but did not), then there are unexploited gains from trade. If employer B had hired this worker, both of them would have been better off. Gale and Shapley defined a pairing to be stable if no such unexploited gains from trade exist. In an ideal market, where workers and firms have unrestricted time and ability to make deals, the outcome would always be stable. Of course, real-world markets may differ from this ideal in important ways. But Gale and Shapley suggested a "deferred-acceptance" procedure which is easy to understand and always leads to a stable outcome. The procedure specifies how agents on one side of the market (e.g., the employers) make offers to those on the other side, who accept or reject these offers according to certain rules [27].
More precisely, "agents on one side of the market, say the firms, make offers to agents on the other side, the workers. Each worker reviews the proposals she receives, holds on to the one she prefers (assuming it is acceptable), and rejects the rest. A crucial aspect of this algorithm is that desirable offers are not immediately accepted, but simply held on to: deferred acceptance. Any firm whose offer is rejected can make a new offer to a different worker. The procedure continues until no firm wishes to make another offer, at which time the workers finally accept the proposals they hold. In this process, each firm starts by making its first offer to its top-ranked worker, i.e., the worker it would most like to have as an employee. If the offer is rejected, the firm makes an offer to the worker it ranks as number two, etc. Thus, during the operation of the algorithm, the expectations of a firm are lowered as it makes offers to workers further and further down its preference ordering. (Of course, no offers are made to unacceptable firms.) Conversely, since workers always hold on to the most desirable offer they have received, and as offers cannot be withdrawn, each worker's satisfaction is monotonically increasing during the operation of the algorithm. When the decreased expectations of firms have become consistent with the workers' increased aspirations, the algorithm stops" [27].

FCP statement
Based on the Hospitals/Residents problem, we state the Firms/Candidates problem as follows. An instance of the Firms/Candidates problem involves a set C of candidates and a set F of firms. Each candidate c ∈ C is seeking a post at one firm, and each firm f ∈ F offers q(f ) ≥ 1 posts. Preference lists of candidates and firms are complete. That is, each candidate c ∈ C ranks F in strict order, and each firm f ∈ F ranks all applicants in strict order. A matching M is a subset of C × F , where (c, f ) ∈ M implies that (i) r is assigned to at most one firm in M , and (ii) at most q(h) candidates are assigned to h ∈ M . A matching M for an instance of the Firms/Candidates problem is stable if M admits no blocking pair. A blocking pair (c, f ) for M is a candidate c and firm f such that (i) c either is unassigned or prefers f to her assigned firm in M , and (ii) f either is undersubscribed or prefers c to the worst candidate assigned to it in M . If (c, f ) form a blocking pair with respect to a matching M , then (c, f ) is said to block M . Also, if (c, f ) ∈ M for some stable matching M , then we say that (c, f ) is a stable pair, and c is a stable partner of f (and vice versa).

The Gale and Shapley algorithm for FCP
We consider the firm-oriented version of the FCP. We use the Gale and Shapley algorithm to obtain a stable matching M which is uniquely favorable to firms. Every firm f ∈ F is assigned either its q(f ) best stable candidates, or a set of fewer than q(f ) candidates; in the latter case, no other candidate is assigned to h in any stable matching [7].
In particular, the algorithms proceeds in rounds as follows. In each round, (i) each firm f which still has k uncovered posts, proposes to the k candidates who are currently ranked higher on the preference list of the firm and have not rejected it yet, (ii) each candidate accepts the proposal (if any) of the firm which is ranked higher in her preference list and rejects all other proposals (if any). The procedure continues until either all posts are assigned or there are no unassigned candidates.
The Gale and Shapley algorithm for FCP can be formally described as follows (see also [16]).

INITIALIZE M to empty matching
WHILE some firm f is unmatched and has not proposed to every candidate c ← first candidate on the list of firm f to whom f has not yet proposed Proofs for correctness, stability and complexity follow from the corresponding proofs of the SMP version of the Gale and Shapley algorithm and can be found in [9,7,11,16,23].
An indicative execution of this algorithm for a given input instance of the FCP is presented in Fig. 1.
The outcome produced by the algorithm does not admit any firm-candidate blocking pairs defined precisely as for the SMP. In the particular example of Fig. 1, all candidates have been assigned to their most preferred firms, so there is no way for a blocking pair to exist.

Our "search-without-searching" implementation
Given two non-empty sets of Firms and Candidates together with their complete preference lists with no ties, we use the Gale and Shapley "propose-and-reject" algorithm for computing stable matchings. Our contribution is a "search-without-searching" implementation of the Gale and Shapley algorithm for the Firms/Candidates problem.
Let F be the set of firms and C the set of candidates. F and C are not necessarily equally-sized. Each candidate c ∈ C can be assigned to at most one firm/post, i.e., a candidate cannot be assigned to multiple firms/posts. Related to F , a list J is maintained containing the q(f ) ≥ 1 available posts that each firm f ∈ F wishes to assign. J is constructed according to the alphabetical order of firms in F . For example, let F = {A, B, C, D} be the set of firms. A list J = [4, 1, 1, 6] would indicate that firm A has 4 available posts, firms B and C have 1 available post each, and firm D has 6 available posts (Fig. 2). Note that the total number of available posts is not necessarily equal to the total number of candidates.
Each firm f maintains a preference list R f ordered in descending order, i.e., from the most preferred to the least preferred candidate, which is complete, i.e., contains all candidates. See for example Fig. 3. Also, each candidate maintains a preference list ordered in descending order, i.e., from the most preferred to the least preferred firm, which is complete, i.e., contains all firms. See for example Fig. 4.
We consider the firm-proposing version of the problem, i.e., firms propose and candidates accept or reject. In particular, at each round, firm f ∈ F proposes to its q(f ) most preferred candidates who have not yet rejected it. Respectively, in the candidate-proposing version of the problem, at each round, firm f would reject all but the q(f ) most preferred applications it would have received.
A valid assignment of candidates to firms must have the following properties.
• Each candidate is assigned to at most 1 firm/post.
• Each firm can be assigned a number of candidates which is at most equal to the number of its available posts.
• Each firm wishes to obtain candidates ranked as high as possible in its preference list. The rank of a candidate on the preference list of a firm is a non-zero, natural number showing the candidate's position in the preference list of a firm which is sorted in decreasing order. If a highly-ranked candidate is not assigned to a proposing firm, then this candidate prefers her current firm more than the proposing one.
• Each candidate c ∈ C is assigned to the firm with the least-worst rank in her list. If a candidate is not assigned to a highly-ranked firm, then this firm prefers its currently assigned candidates more than c.
For the firm-oriented version of the algorithm, our implementation performs searching in time linear in the number of firms, resulting in O(n 2 ) total execution time. In particular, we present three slightly different approaches for the implementation of the Gale and Shapley algorithm for the Firms/Candidates problem. The difference among these three approaches consists in the way we implement how each candidate decides which proposal (if any) to accept in each round.

Implementation 1: "search exhaustively"
A firm f ∈ F with q(f ) = k available posts proposes to the first k candidates ranked higher in its preference list. Then, each of these candidates must decide whether to accept or reject the proposal. More precisely, given the decreasing order of her preference list, a candidate accepts the proposal of a firm if this firm is before her current assignment in her preference list, otherwise the candidate rejects the proposal.
For example, consider a candidate a whose preference list is r a = [B, C, D, A] and firms B and C whose preference lists sorted in decreasing order are R A = [a, d, b, c] and R C = [a, d, c, b], respectively. Initially, candidate a is unassigned. Firm B proposes to candidate a, who accepts the proposal. Then, firm C proposes to a, who reads her preference list and accepts the proposal of the firm found first in her list. Since C appears first in a's preference list, a accepts the proposal of C (rejecting B). It took a 2 steps to decide that she accepts the proposal of firm C.
A "bad" instance would require reading n − (j − 1) positions of the candidate's preference list (of total length n) in each round j > 1 for deciding whether the new proposal should be accepted or rejected. For example, consider the following scenario. Candidate's a preference list is depicted in Fig. 5. Initially, firm A proposes to candidate a. In subsequent rounds, candidate a, receives the following proposals: (round 2) firm B, (round 3) firm C and (round 4) firm D. As sketched in Fig. 6, candidate a needs 3 steps to accept the proposal of firm B in round 2 (rejecting firm A), 2 steps to accept the proposal of C in round 3 (rejecting firm B), and 1 step to accept the proposal of D in round 4 (rejecting firm C).
Thus, following this "search exhaustively" approach, it would take each candidate a total of at most Σ n−1 i=1 i, i.e., O(n 2 ), comparison steps to make her final decision, where n = |F |, given that candidate preference lists are complete. So, overall, this approach requires O(n 3 ) steps for computing stable matchings.

Implementation 2: "search partially"
We, now, present a more efficient implementation of the procedure followed by candidates in order to decide whether to accept or reject a proposal. More precisely, reading the candidate's preference list (i.e., performing comparisons) is still required. However, now, the number of required "reads" is decreased via the exploitation of two facts: (a) the decreasing order of the candidate's preference list and (b) the "rank" of the candidate's current firm, i.e., the position of the currently assigned firm (if any) on the candidate's preference list. Again, accepting or rejecting a proposal depends on whether the proposing firm is (respectively) before or after the candidate's current firm in her preference list. The following example sketches the main idea behind this optimization.
Let r a = [E, D, C, B, A] be the preference list of candidate a, sorted in decreasing order. Assume that candidate a, who is initially not engaged to any firm, receives a proposal by firm A. Subsequently, she receives proposals by firms B and C. Candidates not engaged to some firm unconditionally accept the first proposal they receive by a firm. So, candidate a accepts the proposal of firm A (round 1). When she receives a proposal by firm B (round 2), candidate a has to "read" the first 4 positions of r a (4 steps) for deciding whether to accept the proposal of firm B, with rank B = 4 (rejecting firm A). When she receives a proposal by firm C (round 3), in order to decide whether to accept or reject the proposal candidate a must determine whether firm C is before or after her current firm B in her preference list. Given the decreasing order of a's preference list, the relative position of C and B can be determined by the a's answer to the following "Yes/No" question: "is C on the left of B in your preference list?" If Yes, accept the proposal of C (rejecting B), otherwise reject the proposal of C.
Candidate a knows that (i) her preference list is sorted in decreasing order, (ii) the size of her preference list is |r a | = 5, and (iii) that for her current firm B, rank B = 4. Since rank B ≥ |ra| 2 , candidate a concludes that her current firm lies in the 2nd half of her ordered preference list. So, it suffices that a checks the single position on the right of B (instead of the 3 positions on the left of B) in her preference list about whether it contains firm C: if Yes, a rejects the proposal of C (since it is a low-preference firm compared to C), otherwise a accepts the proposal of C (rejecting B). In this particular example (Fig. 7), firm C does not lay on the right of B, therefore candidate a accepts the proposal of C without calculating rank C . That is, knowing that rank B = 4, a was required to perform 1 comparison involving 1 list element (on the right of B) in position 5 of her preference list instead of 3 comparisons involving 3 list elements in positions 1, 2 and 3 -from the beginning of the list to the position of C -in order to decide whether to accept or reject the new proposal by firm C. Overall, it can be easily observed that this implementation approach requires at most a single full "read" of the n elements of the candidate's preference list every second round. This suggests an improvement compared to the previous approach which required a full "read" of the n elements of the candidate's preference list in each round.
More precisely, in even rounds (rounds 2, 4, 6, etc), acceptance or rejection of a new proposal requires reading a number, say t, of positions of the candidate's preference list, from its beginning up to the position of either her current or the proposing firm. However, during the following odd rounds (rounds 3, 5, 7, etc, respectively), the candidate already knows that the rank of her current firm is t. Thus, for accepting or rejecting the new proposal, it suffices to "read" only the smallest part of her preference list either on the left (of length t − 1) or on the right (of length n − t) of the position of her current firm. Despite the fact that this improvement does not have an important impact on the order of magnitude of the necessary comparisons, it requires half the number of "reads" comparing to the number of "reads" of the candidates' preference lists needed by Implementation 1.
The algorithm for implementing this approach proceeds as follows: 1. For each firm F with a least 1 available post which has not reached the end of its preference list 2. If F proposes to a candidate c not yet engaged then c accepts the proposal (having no knowledge about her preference for this particular firm), otherwise if F proposes to an already engaged candidate c (a) If c has no knowledge of the rank of her current firm rank current in her preference list, c searches her preference list from its beginning until one of the following is true: i. If c finds her current firm first, c "remembers" rank current and rejects the proposal of F ii. If c finds firm F first, c "remembers" rank F and accepts the proposal of F (b) If c has knowledge of the rank of her current firm rank current in her preference list, i. if rank current ≥ n 2 (where n equals the number of firms or, equivalently, the size of c's preference list) that is, if her current firm is a low-preference choice for c, c searches positions [rank current , n] starting from the position rank current to the end of the preference list A. If c finds F then she rejects the proposal of F (since she prefers F less than her current firm) B. If c does not find F then she accepts the proposal of F (since she prefers F to her current firm) without knowing rank F ii. if rank current < n 2 (where n equals the number of firms or, equivalently, the size of c's preference list) that is, if her current firm is a high-preference choice for c, c searches positions [1, rank current ] starting from the beginning of the preference list to the position rank current A. If c finds F then she accepts the proposal of F (since she prefers F to her current firm) and updates rank current (i.e., rank F → rank current ) B. If c does not find F then she rejects the proposal of F (since she prefers F less than her current firm) This procedure continues until either all firms cover all posts or are rejected by all candidates on their preference lists.
A "bad" instance would require reading all n positions of a candidate's preference list during each pair of successive rounds in order for a candidate to decide whether to accept or reject new proposals.
For example (Fig. 8), let r a = [E, D, F, C, H, B, G, A] be the candidate's a preference list. Initially, a (not engaged) receives a proposal by firm A. Then, she receives proposals by firms G, B, H and C. Candidates always accept the first proposal they receive. So, a accepts the proposal of firm A (round 1). When she receives a proposal by firm G (round 2), a must read the first 7 positions of her preference list (7 steps) for deciding whether to accept the proposal of G with rank G = 7 (rejecting A). When she receives a proposal by firm B (round 3), in order to decide whether to accept or reject the proposal a must determine whether B proceeds (i.e., acceptance of its proposal) or follows (i.e., rejection of its proposal) her current firm G in her ordered preference list. Therefore, in order to decide whether to accept or reject the proposal of B, a needs to determine the relative position of B with regard to the position of G in her preference list. Since her preference list is strictly ordered, the exact order of B and G results from the answer to the "Yes/No" question: "is B on the left of G in a's preference list?" If Yes, a must accept the proposal of B, otherwise a must reject the new proposal.
a knows that (i) her preference list is sorted in decreasing order, (ii) the size of her preference list is |r a | = 8, and (iii) that rank G = 7. Since rank G = 7 ≥ 4 = |ra| 2 , a concludes that her current firm lies in the 2nd half of her ordered preference list. Therefore, she only needs to check the 1 position on the right of G (instead of the 6 positions on the left of G) in her preference list about whether it contains firm B: if Yes, a rejects the proposal of B (since she prefers it less than G), otherwise a accepts the proposal of B (rejecting G). In this particular example, firm B lies on the left of G, therefore a accepts the proposal of B without calculating rank B . To sum up, knowing that rank G = 7, a performed a single comparison with the single element (on the left of G) in position 8 of her preference list instead of 6 comparisons involving the 6 elements in positions 1, 2, 3, 4, 5 and 6 -from the beginning of her preference list to G -in order to decide whether to accept or reject the proposal of firm B. Similarly, when a receives a proposal by firm H (round 4), she knows that her current firm is B, though she does not know rank B since it was not calculated during the previous round (round 3). Now, for deciding whether to accept or reject the proposal of H, a cannot avoid searching from the beginning of r a until either B (current firm) or H occurs. After checking the first 5 positions of r a , a finds firm H, accepts its proposal and "remembers" its rank (rank a (H) = 5). When a receives a proposal by C (round 5), given that she knows that rank C = 5 ≥ 4 = |ra| 2 , it suffices to examine the last 3 positions of her preference list (instead of the first 4 ones) about whether they contain firm C. The last 3 positions of r a do not contain C. Therefore, a accepts the proposal of C (rejecting H). The procedure continues until a knows rank current and rank current < |ra| 2 . Regarding the maximum required number of comparisons, observe that during each pair of successive rounds (even-odd round) |F | comparisons are performed. For instance, in the example of Fig. 8, processing the pair of successive proposals by G and B required 8 comparisons (7 for  G and 1 for B), processing the pair of successive proposals by H and C required 8 comparisons (5 for H and 3 for C), and so on. Therefore, since the procedure continues until rank current = |F | 2 and the number of comparisons required by each pair of successive rounds is |F |, at most |F | 4 · |F | comparisons are needed for reaching the middle of the preference list. If a candidate receives proposals by all firms in her preference list, Σ |F | 2 i=1 i additional comparisons will be needed. Overall, at most |F | 4 · |F | + Σ |F | 2 i=1 i comparisons may be performed by a single candidate. Since |F | = n, the number of required comparison per candidate is n 4 n + Σ n 2 i=1 i. In order to determine for which values of n this second implementation approach is more efficient, we have to evaluate the following inequality: Thus, for n > 6, partial search (i.e., Implementation 2) requires fewer comparisons than exhaustive search (i.e., Implementation 1) for a candidate receiving proposals by all firms in her preference list.
To be exact, when a candidate receives proposals by all firms in her preference list, the total number of required comparisons is at most n 4 n + Σ i=1 i, if n mod 4 = 1. This is because there are 4 different cases depending on whether (i) n is even or odd and (ii) a pair of sum n is produced when the middle of the preference list is reached.

Implementation 3: "search without searching"
In the context of our third implementation approach, we focused on minimizing the number of comparisons required for solving the Firms/Candidates problem using the algorithm of Gale and Shapley. For the previous two implementation approaches, we needed to maintain for each candidate a sorted preference list containing all firms. However, candidates do not know the rank of each firm on their preference lists. This implies that whenever a candidate a receives a new proposal by some firm F , a needs to search for F in her preference list r a and determine whether it comes before or after her current firm in order to decide whether to accept or reject the proposal of F . However, if a knew rank F and rank current , she could just compare these two values and make a straightforward decision on whether to accept (if rank F < rank current ) or reject (if rank F > rank current ) the proposal of F . This can be possible if we abandon the idea of the sorted candidates' preference list. In particular, in the context of this third implementation approach, each candidate's a preference list r a is replaced with a new list r a with the following characteristics: (i) r a has a position for each firm, (ii) firms are sorted in alphabetical order and are placed in r a accordingly (e.g., firm A is placed in the 1st position of r a , firm B is placed in the 2nd position of r a , and so on) and (iii) each position i of r a contains a discrete integer value in [1, |F |] reflecting the rank of firm i in candidate's a preference list. For example (Fig. 9), let a be a candidate with preference list r a = [B, C, D, A] sorted in decreasing order. Then r a = [4, 1, 2, 3] since firm A is ranked 4th, firm B is ranked 1st, firm C is ranked 2nd and firm D is ranked 3rd in r a . Figure 9. r a replaces r a for each candidate a.
In this way, candidate a decides whether to accept or reject the proposal of a firm F by simply retrieving and comparing the value of her current firm r a (current) and the value r a (F ) of firm F in r a . Candidate a accepts the proposal of F if r a (F ) < r a (current), and rejects it otherwise. Following this approach, the example discussed in Section 4.2 would evolve as follows. Candidate a accepts proposals by firms A, G, B and H, successively. The modified list r a maintained by a is depicted in Fig. 10. Candidates always accept the first proposal they receive. Therefore, a accepts the proposal by firm A (round 1). Upon receiving a proposal by firm G (round 2), a retrieves and compares r a (A) and r a (G), whose values are 8 and 7, respectively. Since 7 is the smaller of the two values, a (having performed a single comparison) decides to accept the proposal of G (rejecting A). Similarly, upon receiving a proposal by firm B (round 3), a retrieves and compares r a (G) and r a (B), whose values are 7 and 6, respectively. Since 6 is the smaller of the two values, a (again, having performed a single comparison) decides to accept the proposal of B (rejecting G). Finally, when a receives a proposal by H (round 4), she retrieves and compares r a (B) and r a (H), with values 6 and 5, respectively. Since 5 is the smaller of these two values, a decides (having performed a single comparison) to accept the proposal of H (rejecting B). This indicative run is illustrated in Fig. 11. A "bad" instance would result if each candidate received the maximum number of proposals, i.e., n proposals (where n equals the number of firms). Then, exactly n − 1 comparisons would be required for each candidate to decide whether to accept or reject the proposal received in each round. This implementation approach drastically decreases the overall number of comparisons required for the execution of the Gale and Shapley algorithm for the Firms/Candidates problem. In particular, the O(n 2 ) comparisons required by each candidate in the context of the first two Figure 11. The "search without searching" implementation approach. implementation approaches have now been decreased to O(n), thus yielding a significant overall reduction in the number of required comparisons (from O(n 3 ) to O(n 2 )). Despite the fact that the theoretical upper bound remains polynomial in the number of firms, this reduction can be of great practical importance.
Termination and Stability. All three suggested approaches actually implement the original Gale and Shapley algorithm. The difference among them consists in the number of comparisons required for candidates to decide whether to accept or reject newly received proposals. Therefore, proofs regarding termination and stability of the result are the same as they appear in the original work of Gale and Shapley [9].
However, we also experimentally validated the stability of the results returned by our implementations. In particular, we developed "Check", a routine which verifies that the assignments computed by our implementations do not contain blocking pairs. "Check" receives as input the candidates' preference lists, the preference lists of firms and the computed assignments of candidates to firms and outputs an error message if a blocking pair is detected.
Initially, we define "stability", a Boolean variable which becomes false if a blocking pair is found. For each candidate c, firms in her preference list are exhaustively checked until either her current firm is found or the end of the preference list is reached (i.e., c is not assigned to any firm). For each firm f ∈ F in c's preference list, we check whether c precedes the last candidate assigned to f on the preference list of f . If this is true, then (i) f prefers c more than at least one other candidate already assigned to it and (ii) c prefers f more than her current firm. Thus, (f, c) form a blocking pair and "stability" becomes false. This procedure continues until either "stability" becomes false or all candidates are checked. If the procedure terminates and "stability" is true, then the examined assignment does not contain blocking pairs. This is because all firms that candidate c prefers more than her current firm do not prefer c more than their currently assigned candidates. That is, no firm occurring in c's preference list earlier than her current firm would prefer replacing a currently assigned candidate with c. Therefore, c cannot participate in any blocking pair.
"Check" also detects incomplete assignments by comparing the number of uncovered posts with the number of unassigned candidates. Since all preference lists are complete, a non-zero value of uncovered posts or unassigned candidates indicates an incomplete assignment. When the number of available posts equals the number of candidates, uncovered posts and unassigned candidates are equal to 0.

Experimental framework and results
In this section, we present our experimental results regarding the comparison of the three suggested implementation approaches for the Gale and Shapley algorithm for the Firms/Candidate problem, in terms of required number of comparisons and execution time. Obtained results indicate that the "search without searching" approach (i.e., Implementation 3) outperforms the previous two.
Code was developed using C++ and Visual Studio 2017 version 15.7.5.
For the development and the experimentation a computer with the following characteristics was used.

• Windows 10 Home Operating System
In order to compare the three suggested implementation approaches for the Gale and Shapley algorithm for the Firms/Candidates problem in terms of required number of comparisons and execution time, we implemented a program which generates arbitrary preference lists for Firms and Candidates. More precisely, this program receives as input the number of Firms, the number of vacancies per Firm as well as the number of Candidates. It outputs the list of vacancies for each Firm as well as the preference lists for Firms and Candidates.
In each trial, all three approaches were executed for the same set of input instances. We started by considering small sets of decades of Firms/Candidates and continued by gradually increasing set size to 100, 500 and 1000 Firms/Candidates. For each of these set sizes, we evaluated the performance of the three approaches for worst-case instances, i.e., input sets maximizing the number of proposals and rejections.
Furthermore, we developed an auxiliary routine which transforms candidates' preference lists (r c ) to lists indicating the rank of each firm on the preference list of each candidate (r c ) used by the "search without searching" implementation approach.
Comparisons. Figures 12 and 13 show the number of comparisons performed by each of the three suggested implementation approaches for the Gale and Shapley algorithm for the Firms/Candidates problem. In particular, Fig. 12 shows the number of comparisons required when arbitrary preference lists are provided as input to the three suggested implementation approaches. Fig. 13 shows the number of comparisons required when input instances involve maximum number for proposals/rejections. It can be observed that in both cases, especially as the size of the input increases, the "search without searching" approach (i.e., Implementation 3) requires a significantly smaller number of comparisons compared to the other two approaches.
Execution time. As expected, the number of required comparisons significantly affects execution time. Regardless of the kind of candidates' preference lists provided as part of the input (i.e., preference lists that maximize the number of proposals/rejections or arbitrary preference lists), it is observed that the "search without searching" implementation approach outperforms the other two. In particular, Fig. 14 shows the execution time for each of the three suggested implementation approaches receiving as input preference lists of size 10, 100, 500 and 1000 which maximize the number of proposals and rejections. Fig. 15 shows the execution time for each of the three suggested implementation approaches when arbitrary preference lists of size 10, 100, 500 and 1000 are used. Note that execution time of the "search without searching" implementation approach does not include the time needed for the transformation of candidates' preference lists (r c ) to lists indicating the rank of each firm on the preference list of each candidate (r c ). Fig. 16 shows the execution time for each of the three suggested implementation approaches when arbitrary preference lists of size 10, 100, 500 and 1000 are used, but, now, time required for the transformation of candidates' preference lists has been taken into account for the "search without searching" implementation approach. However, it is realistic to assume that in many practical contexts, the required  list transformation takes place independently, before the execution of the the Gale and Shapley algorithm. Then, it can be observed that the "search without searching" approach (i.e., Implementation 3) significantly outperforms the other two approaches, especially as the size of the input (i.e., number of firms and candidates) increases. Figure 14. Input instances with preference lists maximizing proposals/rejections: execution time required by the suggested implementation approaches.

Concluding Remarks
In this work, we focused on a version of the Hospitals/Residents problem first defined in 1962 by Gale and Shapley [9] under the name "College Admissions Problem". In particular, we considered the Firms/Candidates problem, where each Firm wishes to hire at least one Candidate and each Candidate can be eventually assigned to a single Firm. Preference lists of candidates and firms are complete, i.e., firms rank all candidates and candidates rank all firms. The Gale and Shapley "propose-and-reject" algorithm computes a stable matching for the Firms/Candidates problem. We suggested an efficient implementation of the Gale and Shapley algorithm. The efficiency of our implementation results from replacing candidates' preference lists with lists indicating the rank of  each firm on the preference list of each candidate. In this way, searching is avoided and the number of comparisons required by candidates in order to decide whether to accept or reject a proposal by a firm is significantly decreased. This reduction, even not affecting the polynomial execution time of the Gale and Shapley algorithm, can still be of great practical importance.