SLIDE 7 Queues in Python: deque
> > > from c o l l e c t i o n s import deque > > > q = deque ( [ 2 , 5 , 1 , 7 ] ) > > > q . append (6) > > > q deque ( [ 2 , 5 , 1 , 7 , 6 ] ) > > > q . p o p l e f t ( ) 2 > > > Enqueue: q.append() Dequeue: q.popleft()
25 / 33
An intuitive method that guarantees to find a stable matching.
9
Gale-Shapley deferred acceptance algorithm
GALE–SHAPLEY (preference lists for men and women)
________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________
INITIALIZE S to empty matching.
WHILE (some man m is unmatched and hasn't proposed to every woman)
w ← first woman on m's list to whom m has not yet proposed. IF (w is unmatched) Add pair m–w to matching S.
ELSE IF (w prefers m to her current partner m')
Remove pair m'–w from matching S. Add pair m–w to matching S.
ELSE
w rejects m.
RETURN stable matching S.
________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________
Python data structures to represent preferences
1 men = [ ’ x a v i e r ’ , ’ yancey ’ , ’ zeus ’ ] 2 women = [ ’amy ’ , ’ bertha ’ , ’ c l a r e ’ ] 3 4 p r e f = { ’ x a v i e r ’ :
[ ’amy ’ , ’ bertha ’ , ’ c l a r e ’ ] ,
5
’ yancey ’ : [ ’ bertha ’ , ’amy ’ , ’ c l a r e ’ ] ,
6
’ zeus ’ : [ ’amy ’ , ’ bertha ’ , ’ c l a r e ’ ] ,
7
’amy ’ : [ ’ yancey ’ , ’ x a v i e r ’ , ’ zeus ’ ] ,
8
’ bertha ’ : [ ’ x a v i e r ’ , ’ yancey ’ , ’ zeus ’ ] ,
9
’ c l a r e ’ : [ ’ x a v i e r ’ , ’ yancey ’ , ’ zeus ’ ]
10
}
11 #turn
the p r e f e r e n c e s i n t o a usable stack by r e v e r s i n g
12 for n in
p r e f :
13
p r e f [ n ] . r e v e r s e ()
26 / 33
Python data structures to represent preferences
1 rank = { ’amy ’ :{ ’ x a v i e r ’ :2 , ’ yancey ’ :1 , ’ zeus ’ :3} , 2
’ bertha ’ :{ ’ x a v i e r ’ :1 , ’ yancey ’ :2 , ’ zeus ’ :3} ,
3
’ c l a r e ’ :{ ’ x a v i e r ’ :1 , ’ yancey ’ :2 , ’ zeus ’ :3}
4
}
5 6 freemen = l i s t (men)
#i n i t i a l l y a l l men and women are f r e e
7 numpartners = len (men) 8 #c r e a t e
l i s t
matches
9 S = {} 27 / 33