CS 411 Fall 2025 > Outline for November 14, 2025
CS 411 Fall 2025
Outline
for November 14, 2025
Outline
The Stable Marriage Problem [L 10.4]
- The Problem
- Given two sets of equal size, find a matching between them that is optimal according to certain criteria. Usually stated in terms of “men” and “women”, but there are many applications.
- Idea
- Given:
- Two disjoint sets of equal size (\(n\)): a set of men and a set of women.
- For each element of each set, an ordering (best to worst) of the elements of the other set. We think of this as how the man/woman rates or prefers members of the other gender.
- A marriage matching is a collection of pairs, each containing one woman and one man, such that each woman or man lies in exactly one pair.
- A marriage matching is unstable if there are a man & a woman, not paired together, each of whom prefers the other to their current partner. Otherwise, the marriage matching is stable.
- Given:
- Two Questions
- Does a stable marriage matching always exist? (Answer turns out to be yes.)
- How to find one? This is the Stable Marriage Problem.
- Algorithm: Description
- Input is as above.
- We keep track of a changing set of pairs, each with one man and one woman. A man or a woman that is not currently in a pair is free. A man & woman that form a pair are said to be matched. No man or woman will ever be matched with more than one person at the same time.
- Procedure
- All men, women are initially free.
- While there are free men/women:
- Choose a free man \(m\).
- Find the woman \(w\) that is highest in \(m\)’s ratings, among all those who have not yet said “no” to \(m\). (Does such a woman necessarily exist? See the Note, below.*)
- \(m\) proposes to \(w\)—regardless of whether \(w\) is free or matched.
- If \(w\) is free, then \(w\) says “yes” to \(m\); \(w\) and \(m\) are now matched.
- If \(w\) is matched and prefers her current partner to \(m\), then \(w\) says “no” to \(m\); the pairs are unchanged.
- If \(w\) is matched and prefers \(m\) to her current partner, then \(w\) says “no” to her current partner and “yes” to \(m\); the existing pair including \(w\) is dissolved, and \(w\) and \(m\) are now matched.
- Return the set of pairs. END.
- *Note. If \(m\) is a free man, will there always be a woman who has not yet said “no” to \(m\)? Since there is a free man, there must be a free woman. Because of the way the algorithm works, a woman who is free has never been proposed to, so she has never never said “no” to \(m\). So, yes, there will always be a woman who has not yet said “no” to \(m\).
- Algorithm: Termination
- Each time we process a free man \(m\),
one of two things happens.
- The number of pairs increases (if \(w\) is free, she will say “yes” to \(m\), forming a new pair and not dissolving any existing pairs).
- The number of “no” responses increases (if \(w\) is matched, she will say “no” either to \(m\) or to her current partner).
- Each time we process a free man \(m\),
one of two things happens.
- Algorithm: Correctness
- Proof by contradiction: if the algorithm terminates without a stable marriage matching found, then we can argue that the algorithm did not run correctly.
- Since algorithm always ends with stable marriage matching found, one must always exist.
- Algorithm: Analysis
- Time Efficiency
- Store info needed by each man in an array, and similarly for the women. Refer to a man/woman by their array index.
- Each man has a queue of women, best to worst.
- Each woman has a look-up table: \(\text{man}\to\text{rating}\); this can be an array. She also needs to know who she is matched with, if anyone: an array index with possible no one value.
- Store the set of free men in a sequence with constant-time access and deletion at one end, for example, a resizable array, using the end, or a singly linked list, using the beginning.
- With data in this form, checking for termination and choosing a free man is constant-time.
- Processing a free man is also constant-time. Note that, if a man’s proposal is accepted by a woman who is currently matched, then we can just replace the current free man with the newly rejected man in the free-men data structure.
- Each man proposes to each woman at most once. So the inside of the loop runs at most \(\Theta(n^2)\) times.
- Preprocessing—setting up the data—requires \(\Theta(n^2)\).
- Therefore: \(\Theta(n^2)\) algorithm.
- Solution & Implications
- Solution found is (unique) man-optimal solution: each man is matched with highest-ranked woman possible in any stable marriage matching.
- So the order in which free men are considered does not affect the result.
- Implications for traditional courtship? Strictly speaking, no, but it is suggestive.
- Other implications: algorithm has been used to match medical-school graduates to residency positions at hospitals, with hospitals playing the role of men.
- Time Efficiency