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 with . After line L1, the lists are symmetric: is on the list of if and only if is on the list of . The table is the current set of lists; it only loses entries, and every deletion of a pair removes both entries. A matching is contained in if each pair of is in . We write when ranks above on its original list, and , , 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 after on the list of " at line L6 or L14, which deletes each pair with still present. We say that made the deletion, keeping . Truncation is by rank, so it is well defined even if is no longer on the list of .
Lemma 1 (phase 1 holders). During phase 1:
(a) if , then and ; (b) each agent with a nonempty list is in or equals ; (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 . Consider one iteration in which is popped with a nonempty list, and let . By symmetry, is on the list of , so , and line L6 deletes exactly the pairs with . After it, .
Take any other holder relation from before the iteration, so and by (a). If , then is the old holder of ; if line L6 deletes , DELETE sets to nil and puts back into , and in any case line L7 replaces the holder of . If , the pair is not deleted: it would have to be with and , so , which is impossible. Entries are never added, so still holds. The list of loses at most the entry , so still holds. Line L7 gives (a) for the new pair and (b) for . An agent that loses its holder relation is put into , so (b) holds for every agent. For (c), only line L6 for the same can remove the holder of , and line L7 gives a holder at once.
Lemma 2 (phase 1 terminates; the stable table). Phase 1 stops after at most pops. Let be the set of agents with nonempty lists at its end, and for . Then is a bijection of onto , and for each . Every agent outside has an empty list.
Proof. An agent is pushed into only at line L2 or when a pair is deleted (one push per deletion), and at most pairs exist. So there are at most pops. At the end is empty, so by Lemma 1(b) each is the holder of . The agent has on its list, so . Distinct agents hold distinct agents, so is injective, hence a bijection of the finite set . Lemma 1(a) gives .
We call a table with properties of Lemma 2 (symmetric, a bijection of , on , lists outside empty) a stable table on . In a stable table, , so if and only if .
Lemma 3 (phase 1 deletes no stable pair). No pair deleted in phase 1 belongs to a stable matching.
Proof. Suppose some stable matching contains a pair deleted in phase 1, and take the first such deletion: at line L6, proposes to and is deleted with and . The agents that ranks above 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 in . The partner of is not , as is matched to . So is single in or prefers to its partner, and prefers to . The pair is acceptable and blocks , a contradiction.
Lemma 4 (cover). Let be a table that is contained in a stable table on , and let be a stable matching contained in . Then matches every agent of , and no agent outside .
Proof. Agents outside have empty lists in , so they are single. Use and of . Each matched in has its partner on its list in , so , and for each matched .
Suppose is single in . Define and . We claim: for each , is single or prefers to , and then is matched and is defined with the same property. Indeed, is acceptable and would like , so, as is stable, is matched to some with ; in particular , so . As is on the list of in and is not its first entry, prefers to . This proves the claim. So with , an injective map on the matched agents of . The agent is not in the image of , because is injective and is nobody's partner. The sequence lies in a finite set, so it repeats: let with and least. If , injectivity gives , against the choice of . So , a contradiction.
By Lemma 3, every stable matching is contained in the table after phase 1. By Lemma 4, every stable matching matches exactly the agents of .
Invariant of phase 2. At each test of line L10, the table is a stable table on , contained in , and
(P) if some stable matching exists, then 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 , if and , then and .
Proof. Let . Then , while , so . The list of contains (by symmetry) and , two different agents. If the list of were alone, then since is onto, for some on the list of , so and ; the first and last entries of would be equal, so would have one entry, a contradiction.
After line L10, each agent of has at least 2 entries and each next agent of is 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 is defined at each step. The stack holds distinct agents and is finite, so line L12 stops. The agents taken at line L13 are distinct, satisfy with indices modulo , and by Lemma 5. Put . Since , we have . The are distinct because is injective. On the list of , the agent comes strictly before its last entry . Line L14 deletes the set of pairs with ; since truncation is by rank, the result is whatever the order. contains every pair .
Lemma 6 (deleted pairs never block). At any time, each pair deleted so far was made by an agent keeping some with (the other agent), and every entry still on the list of is .
Moreover, , and while no list of is empty the list of is not empty.
Proof. At the deletion, all entries of after were removed, and lists only lose entries. In phase 1, 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 . In phase 2, is some .
Lemma 7 (elimination keeps a stable matching). If contains a stable matching , then contains a stable matching, and no list of is empty in .
Proof. By Lemma 4, matches every agent of , and in , .
If contains one pair , it contains all of them. Suppose . The agent is on the list of before , so prefers to its partner. As is stable, prefers its partner to ; its partner is on its list in , so it is . Going back around the cycle gives all pairs.
Case 1: no pair is in . Suppose a pair is in , with . The partner of is on its list and is not , so it is or worse for ; it is not , whose partner is . So prefers to its partner and prefers to , and blocks . So and is contained in .
Case 2: all pairs are in . Let . The and the are distinct, so is a matching contained in . In , the partner of is , which is not after , and no other pair of contains a , so and is contained in . is stable: compared with , each has a better partner ( instead of its last entry ), each has a worse one, and the others are unchanged. So a pair that blocks but not contains some and an agent with . If is on the list of in , then , whose partner in is ; no block. Otherwise was deleted; by Lemma 6 the agent that made it is in , so it is matched in , and it prefers every entry of its list in , in particular its partner in , to the other agent. So does not block.
In both cases contains a stable matching, which matches all of (Lemma 4 applies, since ), so no list of is empty in .
Lemma 8 (elimination keeps a stable table). If no list of is empty in , then is a stable table on , with and for not in the rotation.
Proof. Symmetry is kept, as deletions are symmetric. Which agents lose their first entry? A pair has the form with . It cannot be , , since the first entry of is not after , which is on its list. So and , that is . Hence only the lose their first entry, and for the others.
The second entry of stays, unless . The truncation of keeps . The only other way is for some with , so is the first entry of : . Then and , so the list of is exactly . The pair is in , and truncates after , which is on its list and differs from , so it equals , and . Then the list of is empty in , which is excluded. So , and is a bijection of : it equals with the images on the rotation shifted cyclically.
Last entries: for not in the rotation, is not a , so its list is not truncated; the pair is not in (shown above), and lists only lose entries, so . For , the list of keeps , which was shown to stay, and loses all entries after it, so . So on , and lists outside stay empty.
The loop. If line L15 finds an empty list of , then by Lemma 7, contains no stable matching, so by (P) none exists, and "no stable matching" is correct. Otherwise, by Lemma 8 and Lemma 7, is a stable table on satisfying (P): if a stable matching exists, contains one, and then so does .
Termination. Each elimination deletes the pairs , and there are at most pairs, so there are at most 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 is empty (it would have been caught at line L15, and after phase 1 lists of are nonempty), so each has the one entry . By symmetry the list of is , so the pairs form a matching of , using acceptable pairs only. Let be an acceptable pair not in . It is not in the final table, so it was deleted, by an agent, say , keeping some . By Lemma 6, and the one entry of its final list satisfies . So , and does not prefer . No pair blocks , and line L16 returns a stable matching.