2 - Stable Matching
ucla | CS 180 | 2023-10-03T07:51
Table of Contents
Supplemental
Lecture
Stable Matching Problem
- set A and B of n elements
- each element of each set has a full and unique ranking of all elements of the other set (priority list)
- some problems only have 1 set matching
- must be a complete matching
- from sset A of n obj and B of n obj
- we want exactly 1-to-1 matching
- must be a stable matching
- there are no unstable pairs
- unstable pair - x prefers y’ to its assigned y AND y’ prefers x to its assigned x’
Gale-Shapley (”Propose-and-reject”, 1962)
- algorithm - GUARANTEES stable matching
- pick an arbitrary element from set A, e.g., 4
- now pick 4’s FIRST preference from set B: m1
- TREE: check if m1 is already matched to some other element of A
- if it’s already matched, e.g., with 7
- check if 4 comes before or after 7 in m1’s preference list
- prefers 4 more than 7
- match 4 and m1
- else - move onto 4’s next mot preferred: m2 (and loop until matched)
- prefers 4 more than 7
- check if 4 comes before or after 7 in m1’s preference list
- else - match
- if it’s already matched, e.g., with 7
- now loop until there are no unmatched elements of set A
Worst case time complexity: O(n^2)
Termination
- terminates after at most
iterations - after
elements from gets matched, e.g., , it never gets unmatched only traded up - only the elements of set A can be unmatched - each iteraation an element of set A proposesmatch with a NEW element of B
- so, there are only at most n^2 possible proposals
Perfection
- all elements are matched exatly for 1 pairing
- Contradictorily: if one from A is not matched, then another from B is not matched
- so we can pair these up so theres no more unmatched
Stability
- no unstable pair
Run Time Analysis
Per iteration:
- identify a free student
- if we use a linked list for students, picking first is const time, and if we displace a student, placing them back is const time at front or back
- For each student
find the highest-ranked med school that the student has not approached before- if we use an array (pre-sorted by ranking) for each student, then checking the first element is const. time
- For med
check if it’s paired, and find the student paired with- we can store this as a single integer for each med school representing the assigned student, all initialized at 0 at the beginning and once assigned, only switch to another assignment if at all, thus const. time
- Find
’s relative ranking of and $s’- if we also give m a priority list of students using a hashmap or a list where the index is the student and the value is the ranking, we can get the ranks and compare in const. time