Guest
Sign in to keep your graphs and progress.

Practice book
Graph Theory Through Problems300 Exercises with Hints, Worked Algorithms and Complete Solutions
By Mohammed Islam Hadjoudj
Learn graph theory by solving problems: 300 exercises, from routine checks to challenging proofs, each with a complete solution.
Also by the authorGraph Algorithms for Coding Interviews100 solved interview problems with visual explanations and Python solutions.View on AmazonSelect an algorithm and generate steps to begin visualization
Nobody would rather swap
Matches the two sides of a bipartite graph by their rankings: free proposers ask down their lists and receivers keep the best offer, until no pair would rather swap
Click a name to rank it one place higher. Choose a start vertex on the other side to let that side propose.
Select an algorithm and generate steps to begin visualization
The stable matching problem pairs the members of two groups, such as students and schools or doctors and hospitals, when everyone ranks the other side. A matching is stable if no student and school would both rather have each other than the partners they got: such a pair would simply break the rules and leave together. David Gale and Lloyd Shapley showed in 1962 that a stable matching always exists and gave an algorithm that finds one, now called deferred acceptance. The problem is also known as the stable marriage problem. Alvin Roth and Lloyd Shapley received the 2012 Nobel Memorial Prize in Economics for the theory and its use in real markets.
One side proposes and the other receives. Every free proposer asks the receiver it ranks highest among those it has not asked yet. A free receiver accepts, and an engaged receiver compares the new offer with its partner, keeps the one it ranks higher and turns the other away, who becomes free again. Receivers only trade up and proposers only move down their lists, so nobody proposes twice to the same receiver, and the process ends after at most n² proposals. The result is stable, and it is the best stable matching for every proposer and the worst for every receiver, so which side proposes matters. In the visualizer the start vertex's side proposes, and each vertex's ranking is shown next to it.
Deferred acceptance runs the National Resident Matching Program, which places medical graduates in US hospitals, and assigns students to public schools in New York, Boston and many other cities. Variants allocate university places and assign cadets to army branches. In computer science it is a standard example of an algorithm with a clean invariant and a proof of termination, and it is taught next to bipartite matching, which ignores preferences and only maximises the number of pairs.
Gale-Shapley keeps a queue of free proposers and two pieces of state: for each proposer the position it has reached in its own list, and for each receiver its current partner. A proposer never asks the same receiver twice.
GaleShapley(proposers P, receivers R, rankings):
every p and every r is free
next[p] = 1 for every p // next position to try
while some free p has next[p] <= length of its list:
r = list(p)[next[p]]
next[p] = next[p] + 1
if r is free:
engage(p, r)
else if r ranks p above its partner q:
q becomes free
engage(p, r)
else:
r rejects p // p stays free
return the engaged pairsTwo monotone quantities drive the proof. A receiver's partner only gets better, and a proposer only moves down its list, so there are at most n² proposals. The result is stable: if p prefers r to its final partner, p proposed to r earlier and was turned down or left for someone r ranks higher, and r's partner has only improved since, so r does not prefer p.
Students A, B, C and D apply to schools 1, 2, 3 and 4, each with one seat, on the example this page loads. The students propose.
Example graph: Students' rankings: A 3, 1, 2, 4. B 1, 2, 4, 3. C 3, 1, 2, 4. D 3, 4, 2, 1. Schools' rankings: 1 D, A, B, C. 2 A, C, D, B. 3 B, D, A, C. 4 C, D, B, A.
After 9 proposals the matching is A-1, B-4, C-2, D-3, and no student and school prefer each other to their partners. If the schools proposed instead, the result would be A-2, B-3, C-4, D-1: every student would get a worse school and every school a better student, which shows how much the choice of the proposing side matters.
Time: O(n²) · Space: O(n²)
Each proposer asks each receiver at most once, so there are at most n² proposals for n people on each side, and the worst case comes close, with n(n - 1) + 1 proposals. Each proposal costs O(1) if every receiver's ranking is first turned into an array giving the position of each proposer, which takes O(n²) time and space for all receivers. Reading the rankings already takes O(n²), so the algorithm is linear in the size of its input.
Stable matching is one of several ways to pair two groups. Which one fits depends on whether the pairs have preferences, costs or only a yes or no.
| Alternative | Prefer it when | Cost |
|---|---|---|
| Hopcroft-Karp | Pairs are simply allowed or not and the goal is as many pairs as possible. Preferences play no role. | O(E√V) |
| Hungarian method | Every pair has a cost or a score and the total must be optimal, as when assigning workers to jobs. | O(n³) |
| Receiver-proposing Gale-Shapley | The other side's interests come first: the same algorithm with the roles swapped gives the stable matching that is best for the receivers. | O(n²) |
| Irving's stable roommates algorithm | There is a single group and anyone can be paired with anyone. A stable matching may then not exist, and the algorithm finds one or proves there is none. | O(n²) |
| Top trading cycles | One side owns objects instead of having preferences, as in house allocation or kidney exchange. It is strategy-proof and Pareto efficient. | O(n²) |
Related algorithms: Hopcroft-Karp Matching, Bipartite Check, Maximum Flow, Min-Cost Max Flow