Algorithm Search⌘K

Proofs: Irving's stable roommates algorithm

These are the human proofs of Irving's stable roommates algorithm, at full length.

Irving's algorithm returns a stable matching exactly when one exists full

This proof shows: correctness, termination.

Setting. The agents form a set VV with ∣V∣=n|V| = n. After line L1, the lists are symmetric: bb is on the list of aa if and only if aa is on the list of bb. The table TT is the current set of lists; it only loses entries, and every deletion of a pair {a,b}\{a, b\} removes both entries. A matching MM is contained in TT if each pair of MM is in TT. We write b≻acb \succ_a c when aa ranks bb above cc on its original list, and first\text{first}, second\text{second}, last\text{last} refer to the current table. Pairs removed at line L1 are not acceptable, so no matching uses them, and they never block.

Every deletion is made by a truncation: "delete all zz after cc on the list of aa" at line L6 or L14, which deletes each pair {a,z}\{a, z\} with c≻azc \succ_a z still present. We say that aa made the deletion, keeping cc. Truncation is by rank, so it is well defined even if cc is no longer on the list of aa.

Lemma 1 (phase 1 holders). During phase 1:

(a) if holder[y]=x\text{holder}[y] = x, then first(x)=y\text{first}(x) = y and last(y)=x\text{last}(y) = x; (b) each agent xx with a nonempty list is in FF or equals holder[first(x)]\text{holder}[\text{first}(x)]; (c) once an agent has received a proposal, it has a holder at the end of every later iteration of line L3.

Proof. At line L2, (a) and (c) say nothing and (b) holds since F=VF = V. Consider one iteration in which xx is popped with a nonempty list, and let y=first(x)y = \text{first}(x). By symmetry, xx is on the list of yy, so first(y)⪰yx\text{first}(y) \succeq_y x, and line L6 deletes exactly the pairs {y,z}\{y, z\} with x≻yzx \succ_y z. After it, last(y)=x\text{last}(y) = x.

Take any other holder relation holder[v]=w\text{holder}[v] = w from before the iteration, so first(w)=v\text{first}(w) = v and last(v)=w\text{last}(v) = w by (a). If v=yv = y, then ww is the old holder of yy; if line L6 deletes {y,w}\{y, w\}, DELETE sets holder[y]\text{holder}[y] to nil and puts ww back into FF, and in any case line L7 replaces the holder of yy. If v≠yv \ne y, the pair {w,v}\{w, v\} is not deleted: it would have to be {y,z}\{y, z\} with w=yw = y and v=zv = z, so z=first(y)⪰yx≻yzz = \text{first}(y) \succeq_y x \succ_y z, which is impossible. Entries are never added, so first(w)=v\text{first}(w) = v still holds. The list of vv loses at most the entry y≠wy \ne w, so last(v)=w\text{last}(v) = w still holds. Line L7 gives (a) for the new pair (x,y)(x, y) and (b) for xx. An agent that loses its holder relation is put into FF, so (b) holds for every agent. For (c), only line L6 for the same yy can remove the holder of yy, and line L7 gives yy a holder at once. □\square

Lemma 2 (phase 1 terminates; the stable table). Phase 1 stops after at most n+mn + m pops. Let AA be the set of agents with nonempty lists at its end, and f(x)=first(x)f(x) = \text{first}(x) for x∈Ax \in A. Then ff is a bijection of AA onto AA, and last(f(x))=x\text{last}(f(x)) = x for each x∈Ax \in A. Every agent outside AA has an empty list.

Proof. An agent is pushed into FF only at line L2 or when a pair is deleted (one push per deletion), and at most mm pairs exist. So there are at most n+mn + m pops. At the end FF is empty, so by Lemma 1(b) each x∈Ax \in A is the holder of f(x)f(x). The agent f(x)f(x) has xx on its list, so f(x)∈Af(x) \in A. Distinct agents hold distinct agents, so ff is injective, hence a bijection of the finite set AA. Lemma 1(a) gives last(f(x))=x\text{last}(f(x)) = x. □\square

We call a table with properties of Lemma 2 (symmetric, ff a bijection of AA, last∘f=id\text{last} \circ f = \text{id} on AA, lists outside AA empty) a stable table on AA. In a stable table, last=f−1\text{last} = f^{-1}, so last(y)=x\text{last}(y) = x if and only if first(x)=y\text{first}(x) = y.

Lemma 3 (phase 1 deletes no stable pair). No pair deleted in phase 1 belongs to a stable matching.

Proof. Suppose some stable matching MM contains a pair deleted in phase 1, and take the first such deletion: at line L6, xx proposes to y=first(x)y = \text{first}(x) and {y,z}\{y, z\} is deleted with x≻yzx \succ_y z and {y,z}∈M\{y, z\} \in M. The agents that xx ranks above yy had all been deleted from its list before, by earlier deletions, so by the choice of the first deletion none of them is the partner of xx in MM. The partner of xx is not yy, as yy is matched to z≠xz \ne x. So xx is single in MM or prefers yy to its partner, and yy prefers xx to zz. The pair {x,y}\{x, y\} is acceptable and blocks MM, a contradiction. □\square

Lemma 4 (cover). Let TT be a table that is contained in a stable table T1T_1 on AA, and let MM be a stable matching contained in TT. Then MM matches every agent of AA, and no agent outside AA.

Proof. Agents outside AA have empty lists in T1T_1, so they are single. Use ff and last\text{last} of T1T_1. Each w∈Aw \in A matched in MM has its partner on its list in T1T_1, so f(w)⪰wM(w)f(w) \succeq_w M(w), and M(y)⪰ylast(y)M(y) \succeq_y \text{last}(y) for each matched yy.

Suppose x∈Ax \in A is single in MM. Define u1=xu_1 = x and y1=f(u1)y_1 = f(u_1). We claim: for each k≥1k \ge 1, uku_k is single or prefers yk=f(uk)y_k = f(u_k) to M(uk)M(u_k), and then yky_k is matched and uk+1=M(yk)u_{k+1} = M(y_k) is defined with the same property. Indeed, {uk,yk}\{u_k, y_k\} is acceptable and uku_k would like yky_k, so, as MM is stable, yky_k is matched to some uk+1u_{k+1} with uk+1≻ykuku_{k+1} \succ_{y_k} u_k; in particular uk+1≠uk=last(yk)u_{k+1} \ne u_k = \text{last}(y_k), so f(uk+1)≠ykf(u_{k+1}) \ne y_k. As yk=M(uk+1)y_k = M(u_{k+1}) is on the list of uk+1u_{k+1} in T1T_1 and is not its first entry, uk+1u_{k+1} prefers yk+1=f(uk+1)y_{k+1} = f(u_{k+1}) to M(uk+1)M(u_{k+1}). This proves the claim. So yk+1=h(yk)y_{k+1} = h(y_k) with h=f∘Mh = f \circ M, an injective map on the matched agents of AA. The agent y1=f(x)y_1 = f(x) is not in the image of hh, because ff is injective and xx is nobody's partner. The sequence y1,y2,…y_1, y_2, \ldots lies in a finite set, so it repeats: let yi=yjy_i = y_j with i<ji < j and jj least. If i>1i > 1, injectivity gives yi−1=yj−1y_{i-1} = y_{j-1}, against the choice of jj. So y1=yj=h(yj−1)y_1 = y_j = h(y_{j-1}), a contradiction. □\square

By Lemma 3, every stable matching is contained in the table T1T_1 after phase 1. By Lemma 4, every stable matching matches exactly the agents of AA.

Invariant of phase 2. At each test of line L10, the table TT is a stable table on AA, contained in T1T_1, and

(P) if some stable matching exists, then TT contains one.

At the first test this holds by Lemmas 2 and 3. We show that each pass of the loop keeps it or returns correctly.

Lemma 5 (the walk). In a stable table on AA, if ∣list(x)∣≥2|\text{list}(x)| \ge 2 and p=last(second(x))p = \text{last}(\text{second}(x)), then p≠xp \ne x and ∣list(p)∣≥2|\text{list}(p)| \ge 2.

Proof. Let y=second(x)y = \text{second}(x). Then f(p)=yf(p) = y, while f(x)≠yf(x) \ne y, so p≠xp \ne x. The list of yy contains xx (by symmetry) and pp, two different agents. If the list of pp were {y}\{y\} alone, then since ff is onto, p=f(w)p = f(w) for some ww on the list of pp, so w=yw = y and f(y)=p=last(y)f(y) = p = \text{last}(y); the first and last entries of yy would be equal, so yy would have one entry, a contradiction. □\square

After line L10, each agent of SS has at least 2 entries and each next agent of SS is last(second(⋅))\text{last}(\text{second}(\cdot)) of the one before it. Line L11 pushes an agent with at least 2 entries. By Lemma 5, each push at line L12 is an agent with at least 2 entries, so second\text{second} is defined at each step. The stack holds distinct agents and AA is finite, so line L12 stops. The agents x0,…,xr−1x_0, \ldots, x_{r-1} taken at line L13 are distinct, satisfy xi+1=last(second(xi))x_{i+1} = \text{last}(\text{second}(x_i)) with indices modulo rr, and r≥2r \ge 2 by Lemma 5. Put yi=f(xi)y_i = f(x_i). Since f(xi+1)=second(xi)f(x_{i+1}) = \text{second}(x_i), we have second(xi)=yi+1\text{second}(x_i) = y_{i+1}. The yiy_i are distinct because ff is injective. On the list of yi+1y_{i+1}, the agent xix_i comes strictly before its last entry xi+1x_{i+1}. Line L14 deletes the set DD of pairs {yi+1,z}\{y_{i+1}, z\} with xi≻yi+1zx_i \succ_{y_{i+1}} z; since truncation is by rank, the result is T′=T∖DT' = T \setminus D whatever the order. DD contains every pair {xi,yi}\{x_i, y_i\}.

Lemma 6 (deleted pairs never block). At any time, each pair deleted so far was made by an agent aa keeping some cc with c≻ac \succ_a (the other agent), and every entry still on the list of aa is ⪰ac\succeq_a c.

Moreover, a∈Aa \in A, and while no list of AA is empty the list of aa is not empty.

Proof. At the deletion, all entries of aa after cc were removed, and lists only lose entries. In phase 1, aa is an agent that received a proposal, so it has a holder at the end of phase 1 by Lemma 1(c), and its list is not empty; so a∈Aa \in A. In phase 2, aa is some yi+1∈Ay_{i+1} \in A. □\square

Lemma 7 (elimination keeps a stable matching). If TT contains a stable matching MM, then T′T' contains a stable matching, and no list of AA is empty in T′T'.

Proof. By Lemma 4, MM matches every agent of AA, and M(w)⪯wf(w)M(w) \preceq_w f(w) in TT, M(y)⪰ylast(y)M(y) \succeq_y \text{last}(y).

If MM contains one pair {xi,yi}\{x_i, y_i\}, it contains all of them. Suppose {xi,yi}∈M\{x_i, y_i\} \in M. The agent xi−1x_{i-1} is on the list of yiy_i before xi=last(yi)=M(yi)x_i = \text{last}(y_i) = M(y_i), so yiy_i prefers xi−1x_{i-1} to its partner. As MM is stable, xi−1x_{i-1} prefers its partner to yi=second(xi−1)y_i = \text{second}(x_{i-1}); its partner is on its list in TT, so it is first(xi−1)=yi−1\text{first}(x_{i-1}) = y_{i-1}. Going back around the cycle gives all pairs.

Case 1: no pair {xi,yi}\{x_i, y_i\} is in MM. Suppose a pair {yi+1,z}∈D\{y_{i+1}, z\} \in D is in MM, with xi≻yi+1zx_i \succ_{y_{i+1}} z. The partner of xix_i is on its list and is not yiy_i, so it is yi+1y_{i+1} or worse for xix_i; it is not yi+1y_{i+1}, whose partner is z≠xiz \ne x_i. So xix_i prefers yi+1y_{i+1} to its partner and yi+1y_{i+1} prefers xix_i to zz, and {xi,yi+1}\{x_i, y_{i+1}\} blocks MM. So M∩D=∅M \cap D = \emptyset and MM is contained in T′T'.

Case 2: all pairs {xi,yi}\{x_i, y_i\} are in MM. Let M′=(M∖{xiyi})∪{xiyi+1}M' = (M \setminus \{x_i y_i\}) \cup \{x_i y_{i+1}\}. The xix_i and the yiy_i are distinct, so M′M' is a matching contained in TT. In M′M', the partner of yi+1y_{i+1} is xix_i, which is not after xix_i, and no other pair of M′M' contains a yjy_j, so M′∩D=∅M' \cap D = \emptyset and M′M' is contained in T′T'. M′M' is stable: compared with MM, each yi+1y_{i+1} has a better partner (xix_i instead of its last entry xi+1x_{i+1}), each xix_i has a worse one, and the others are unchanged. So a pair that blocks M′M' but not MM contains some xix_i and an agent bb with b≻xiyi+1b \succ_{x_i} y_{i+1}. If bb is on the list of xix_i in TT, then b=yib = y_i, whose partner in M′M' is xi−1≻yixix_{i-1} \succ_{y_i} x_i; no block. Otherwise {xi,b}\{x_i, b\} was deleted; by Lemma 6 the agent aa that made it is in AA, so it is matched in M′M', and it prefers every entry of its list in TT, in particular its partner in M′M', to the other agent. So {xi,b}\{x_i, b\} does not block.

In both cases T′T' contains a stable matching, which matches all of AA (Lemma 4 applies, since T′⊆T1T' \subseteq T_1), so no list of AA is empty in T′T'. □\square

Lemma 8 (elimination keeps a stable table). If no list of AA is empty in T′T', then T′T' is a stable table on AA, with f′(xi)=yi+1f'(x_i) = y_{i+1} and f′(w)=f(w)f'(w) = f(w) for ww not in the rotation.

Proof. Symmetry is kept, as deletions are symmetric. Which agents lose their first entry? A pair {w,f(w)}∈D\{w, f(w)\} \in D has the form {yk+1,z}\{y_{k+1}, z\} with xk≻yk+1zx_k \succ_{y_{k+1}} z. It cannot be w=yk+1w = y_{k+1}, z=f(w)z = f(w), since the first entry of yk+1y_{k+1} is not after xkx_k, which is on its list. So w=zw = z and f(w)=yk+1f(w) = y_{k+1}, that is w=xk+1w = x_{k+1}. Hence only the xix_i lose their first entry, and f′(w)=f(w)f'(w) = f(w) for the others.

The second entry yi+1y_{i+1} of xix_i stays, unless {xi,yi+1}∈D\{x_i, y_{i+1}\} \in D. The truncation of yi+1y_{i+1} keeps xix_i. The only other way is xi=yk+1x_i = y_{k+1} for some kk with xk≻xiyi+1x_k \succ_{x_i} y_{i+1}, so xkx_k is the first entry of xix_i: xk=f(xi)=yix_k = f(x_i) = y_i. Then last(xk)=last(f(xi))=xi\text{last}(x_k) = \text{last}(f(x_i)) = x_i and second(xk)=yk+1=xi\text{second}(x_k) = y_{k+1} = x_i, so the list of xkx_k is exactly yk,xiy_k, x_i. The pair {xk,yk}\{x_k, y_k\} is in DD, and xk=yix_k = y_i truncates after xi−1x_{i-1}, which is on its list and differs from xix_i, so it equals yky_k, and {xk,xi}∈D\{x_k, x_i\} \in D. Then the list of xkx_k is empty in T′T', which is excluded. So f′(xi)=yi+1=f(xi+1)f'(x_i) = y_{i+1} = f(x_{i+1}), and f′f' is a bijection of AA: it equals ff with the images on the rotation shifted cyclically.

Last entries: for ww not in the rotation, b=f(w)b = f(w) is not a yjy_j, so its list is not truncated; the pair {w,b}\{w, b\} is not in DD (shown above), and lists only lose entries, so last′(b)=w\text{last}'(b) = w. For xix_i, the list of yi+1y_{i+1} keeps xix_i, which was shown to stay, and loses all entries after it, so last′(yi+1)=xi\text{last}'(y_{i+1}) = x_i. So last′∘f′=id\text{last}' \circ f' = \text{id} on AA, and lists outside AA stay empty. □\square

The loop. If line L15 finds an empty list of AA, then by Lemma 7, TT contains no stable matching, so by (P) none exists, and "no stable matching" is correct. Otherwise, by Lemma 8 and Lemma 7, T′T' is a stable table on AA satisfying (P): if a stable matching exists, TT contains one, and then so does T′T'.

Termination. Each elimination deletes the r≥2r \ge 2 pairs {xi,yi}\{x_i, y_i\}, and there are at most mm pairs, so there are at most m/2m/2 passes of the loop. Each pass stops, as shown after Lemma 5.

The output is stable. The loop exits at line L11 when no list has 2 or more entries. No list of AA is empty (it would have been caught at line L15, and after phase 1 lists of AA are nonempty), so each x∈Ax \in A has the one entry f(x)f(x). By symmetry the list of f(x)f(x) is {x}\{x\}, so the pairs {x,f(x)}\{x, f(x)\} form a matching MM of AA, using acceptable pairs only. Let {a,b}\{a, b\} be an acceptable pair not in MM. It is not in the final table, so it was deleted, by an agent, say aa, keeping some c≻abc \succ_a b. By Lemma 6, a∈Aa \in A and the one entry M(a)M(a) of its final list satisfies M(a)⪰acM(a) \succeq_a c. So M(a)⪰ac≻abM(a) \succeq_a c \succ_a b, and aa does not prefer bb. No pair blocks MM, and line L16 returns a stable matching. ■\blacksquare

Back to Irving's stable roommates algorithm