Proofs: Gale-Shapley algorithm
These are the human proofs of Gale-Shapley algorithm, at full length.
Gale-Shapley runs in O(n + m) time full
This proof shows: time.worst.
Let and let be the total length of all preference lists. Agents are indices, and each array access, comparison and stack operation costs on the word RAM.
Preprocessing, line L1. For each entry at position on the list of , append the pair to a bucket of : . Then for each receiver , write the position of on the list of for each on that list, read for each pair in the bucket of and store it at entry of the list of (or if is not on the list of ), and reset the marks. Each list entry is touched a constant number of times, so this costs and gives at the moment of the proposal in .
The main loop. By the termination argument of the correctness proof, there are at most passes, and each pass costs : a pop, one test on , one list access, one rank comparison and at most one push. Line L12 costs . The total time is .
Complete lists. Let with complete lists, so and the bound is . The number of proposals is at most . Suppose proposes to the last receiver on its list. Then has been rejected by the other receivers, and each of them holds someone from then on, by Claim 2 of the correctness proof. If had received a proposal before, all receivers would hold proposers, so all proposers would be held, while is free; so this is the first proposal to . After it all receivers hold a proposer, so becomes empty and the run ends. Hence at most one proposer makes proposals, and the others make at most each: in total at most .
Space. The lists, the rank entries, the buckets, the arrays , and , and a stack of at most entries take space.
Gale-Shapley returns the proposer-optimal stable matching full
This proof shows: correctness, termination.
Fix an input: proposers , receivers , and strict lists without repeats. A pair is acceptable if is on the list of and is on the list of . Write if ranks above , and similarly. Say that proposes to when line L7 runs with these values, and that rejects when line L8 or L11 pushes after its proposal to , or when line L10 pushes as the old holder of .
Claim 1 (states). At the start of each pass of line L4, every proposer is in exactly one of three states: on the stack once, held by exactly one receiver, or done, popped in line L6 with its list exhausted. Every holder relation is an acceptable pair. Proof. Initially all proposers are on and nothing is held. A pass pops from ; line L6 makes it done; line L8 or L11 puts it back on ; line L9 makes it held by , which held nobody; line L10 makes it held by and moves the old holder of from held to . Lines L9 and L10 run only after line L8 has checked that is on the list of , and is on the list of by line L7.
Claim 2 (receivers trade up). Once , it stays so, and each new value is ranked higher by than the previous one. Proof. Only lines L9 and L10 change ; line L9 needs it to be NIL, and line L10 needs .
Claim 3 (proposals are remembered). If has proposed to and is acceptable, then from that moment on holds or a proposer with . Proof. Right after the proposal, holds (lines L9, L10) or a proposer it prefers to (line L11). By Claim 2, the holder of only improves later.
Claim 4 (proposals in order). Each proposer proposes to the receivers on its list in the order of the list, each at most once. Proof. Line L7 takes the entry at position and increments it, and nothing else changes .
Termination. Each pass of the loop pops one entry of . Entries are pushed once initially for each proposer, and afterwards only in lines L8, L10 and L11, at most once per proposal. By Claim 4 there are at most proposals, where is the total length of the lists of . So there are at most passes.
The output is a stable matching. At the end is empty, so by Claim 1 every proposer is held by exactly one receiver or is done, and each receiver holds at most one proposer. So the pairs returned in line L12 form a matching of acceptable pairs. Let be an acceptable pair not in , and suppose is single in or . If is single, it is done and has proposed to every receiver on its list, including . If is matched, is the receiver of its last proposal, which comes after on its list, so by Claim 4 proposed to before. In both cases Claim 3 says that holds at the end a proposer with . So is not single and prefers its partner, and does not block .
Claim 5 (no stable partner rejects). Call a stable partner of if belongs to some stable matching. In every run, no proposer is rejected by one of its stable partners. Proof. Suppose not, and consider the first rejection in the run of some by a stable partner ; let be a stable matching with . The pair is acceptable, so the rejection happens in line L10 or L11, and at that moment holds or takes a proposer with , and has proposed to . By Claim 4, has proposed to, and been rejected by, every receiver it ranks above , and all these rejections happened earlier. By the choice of the first rejection, none of these receivers is a stable partner of , so is not one of them. Also because . So in the proposer is single or has a partner it ranks below ; and has partner , which it ranks below . The pair is acceptable, because proposed to and preferred to an acceptable proposer, so is on its list. Hence blocks , a contradiction.
Proposer-optimality. For each , let be the stable partner of that ranks highest, if one exists. By Claim 5 and Claim 4, is never rejected by , so it never proposes beyond , and its final partner, if any, is or ranked higher. The output is stable, so , if it exists, is a stable partner of , hence ranked at most as high as . So . If has no stable partner, then cannot exist, and is single. So is determined by the input alone, whatever the order of the stack.
Receiver-pessimality. Let be stable and a receiver with . If is single in or , then since and , the proposer is single in or ranks above , so the acceptable pair blocks , a contradiction. Hence every receiver matched in has in a partner it ranks at least as high as .