cs3000: algorithms & data paul handkhoury.northeastern.edu/home/hand/teaching/cs3000... ·...

32
CS3000: Algorithms & Data Paul Hand Lecture 2: Finish Induction Stable Matching: the Gale-Shapley Algorithm Jan 9, 2019

Upload: others

Post on 11-Jul-2020

6 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: CS3000: Algorithms & Data Paul Handkhoury.northeastern.edu/home/hand/teaching/cs3000... · Gale-Shapley Algorithm • Let M be empty • While (some job j is unmatched): • If (j

CS3000:Algorithms&Data PaulHand

Lecture2:• FinishInduction• StableMatching:theGale-ShapleyAlgorithm

Jan9,2019

Page 2: CS3000: Algorithms & Data Paul Handkhoury.northeastern.edu/home/hand/teaching/cs3000... · Gale-Shapley Algorithm • Let M be empty • While (some job j is unmatched): • If (j

CourseWebsite

http://www.ccs.neu.edu/home/hand/teaching/cs3000-spring-2018/

Page 3: CS3000: Algorithms & Data Paul Handkhoury.northeastern.edu/home/hand/teaching/cs3000... · Gale-Shapley Algorithm • Let M be empty • While (some job j is unmatched): • If (j

• HomeworkwillbesubmittedonGradescope!• MoredetailsonWednesday• EntryCode:MKKEW2• https://www.gradescope.com/courses/36055

HomeworkPolicies

Page 4: CS3000: Algorithms & Data Paul Handkhoury.northeastern.edu/home/hand/teaching/cs3000... · Gale-Shapley Algorithm • Let M be empty • While (some job j is unmatched): • If (j

CourseStructureEnd4/17

Start1/7

DivideandConquer

DynamicProgramming

Graphs NetworkFlow

Textbook:AlgorithmDesignbyKleinbergandTardos

Moreresourcesonthecoursewebsite

FinalTBD

MidtermI2/20

MidtermII3/27

Page 5: CS3000: Algorithms & Data Paul Handkhoury.northeastern.edu/home/hand/teaching/cs3000... · Gale-Shapley Algorithm • Let M be empty • While (some job j is unmatched): • If (j

ProofsbyInduction

Page 6: CS3000: Algorithms & Data Paul Handkhoury.northeastern.edu/home/hand/teaching/cs3000... · Gale-Shapley Algorithm • Let M be empty • While (some job j is unmatched): • If (j

• Claim:Forevery,

• ProofbyInduction:

Exercise

n 2 N,n�1X

i=0

2i = 2n � 1

Page 7: CS3000: Algorithms & Data Paul Handkhoury.northeastern.edu/home/hand/teaching/cs3000... · Gale-Shapley Algorithm • Let M be empty • While (some job j is unmatched): • If (j

StableMatchingProblemandtheGale-ShapleyAlgorithm

Page 8: CS3000: Algorithms & Data Paul Handkhoury.northeastern.edu/home/hand/teaching/cs3000... · Gale-Shapley Algorithm • Let M be empty • While (some job j is unmatched): • If (j

Processforsolvingcomputationalproblemswithalgorithms

• Formulateproblemandquestions• Playaround• Devisealgorithm• Determinehowlongittakestorun• Determineifalgorithmiscorrect• Determineappropriatedatastructures

Page 9: CS3000: Algorithms & Data Paul Handkhoury.northeastern.edu/home/hand/teaching/cs3000... · Gale-Shapley Algorithm • Let M be empty • While (some job j is unmatched): • If (j

StableMatchingProblem

• Manyjobcandidates(eg.doctors).Manyjobs(eg.residencyprograms).Youaretoassigncandidatestojobs.Howshouldyoudoit?

Page 10: CS3000: Algorithms & Data Paul Handkhoury.northeastern.edu/home/hand/teaching/cs3000... · Gale-Shapley Algorithm • Let M be empty • While (some job j is unmatched): • If (j
Page 11: CS3000: Algorithms & Data Paul Handkhoury.northeastern.edu/home/hand/teaching/cs3000... · Gale-Shapley Algorithm • Let M be empty • While (some job j is unmatched): • If (j
Page 12: CS3000: Algorithms & Data Paul Handkhoury.northeastern.edu/home/hand/teaching/cs3000... · Gale-Shapley Algorithm • Let M be empty • While (some job j is unmatched): • If (j
Page 13: CS3000: Algorithms & Data Paul Handkhoury.northeastern.edu/home/hand/teaching/cs3000... · Gale-Shapley Algorithm • Let M be empty • While (some job j is unmatched): • If (j
Page 14: CS3000: Algorithms & Data Paul Handkhoury.northeastern.edu/home/hand/teaching/cs3000... · Gale-Shapley Algorithm • Let M be empty • While (some job j is unmatched): • If (j
Page 15: CS3000: Algorithms & Data Paul Handkhoury.northeastern.edu/home/hand/teaching/cs3000... · Gale-Shapley Algorithm • Let M be empty • While (some job j is unmatched): • If (j
Page 16: CS3000: Algorithms & Data Paul Handkhoury.northeastern.edu/home/hand/teaching/cs3000... · Gale-Shapley Algorithm • Let M be empty • While (some job j is unmatched): • If (j
Page 17: CS3000: Algorithms & Data Paul Handkhoury.northeastern.edu/home/hand/teaching/cs3000... · Gale-Shapley Algorithm • Let M be empty • While (some job j is unmatched): • If (j
Page 18: CS3000: Algorithms & Data Paul Handkhoury.northeastern.edu/home/hand/teaching/cs3000... · Gale-Shapley Algorithm • Let M be empty • While (some job j is unmatched): • If (j
Page 19: CS3000: Algorithms & Data Paul Handkhoury.northeastern.edu/home/hand/teaching/cs3000... · Gale-Shapley Algorithm • Let M be empty • While (some job j is unmatched): • If (j

Gale-ShapleyAlgorithm

• Let M be empty • While (some job j is unmatched):

• If (j has offered a job to everyone): break • Else: let c be the highest-ranked candidate to which j has not yet offered a job

• j makes an offer to c: • If (c is unmatched):

• c accepts, add (c,j) to M • ElseIf (c is matched to j’ & c: j’ > j):

• c rejects, do nothing • ElseIf (c is matched to j’ & c: j > j’):

• c accepts, remove (c,j’) from M and add (c,j) to M

• Output M

Page 20: CS3000: Algorithms & Data Paul Handkhoury.northeastern.edu/home/hand/teaching/cs3000... · Gale-Shapley Algorithm • Let M be empty • While (some job j is unmatched): • If (j

Gale-ShapleyDemo

1st 2nd 3rd 4th 5th

MGH Bob Alice Dorit Ernie Clara

BW Dorit Bob Alice Clara Ernie

BID Bob Ernie Clara Dorit Alice

MTA Alice Dorit Clara Bob Ernie

CH Bob Dorit Alice Ernie Clara

1st 2nd 3rd 4th 5th

Alice CH MGH BW MTA BID

Bob BID BW MTA MGH CH

Clara BW BID MTA CH MGH

Dorit MGH CH MTA BID BW

Ernie MTA BW CH BID MGH

Page 21: CS3000: Algorithms & Data Paul Handkhoury.northeastern.edu/home/hand/teaching/cs3000... · Gale-Shapley Algorithm • Let M be empty • While (some job j is unmatched): • If (j

Activity:Whatarethefirst4stepsofG-Salgorithm?

• Jobs:1,2,3,4• Candidates:A,B,C,D

Jobs’Preferences Candidates’Preferences

(Assumeitstepsthroughjobsinorder1-4,afterwardsstartingoverwith1ifnecessary)

Page 22: CS3000: Algorithms & Data Paul Handkhoury.northeastern.edu/home/hand/teaching/cs3000... · Gale-Shapley Algorithm • Let M be empty • While (some job j is unmatched): • If (j

Observations• Atallsteps,thestateofthealgorithmisamatching

• Jobsmakeoffersindescendingorder

• Candidatesthatgetajobneverbecomeunemployed

• Candidatesacceptoffersinascendingorder

Page 23: CS3000: Algorithms & Data Paul Handkhoury.northeastern.edu/home/hand/teaching/cs3000... · Gale-Shapley Algorithm • Let M be empty • While (some job j is unmatched): • If (j

Gale-ShapleyAlgorithm

• QuestionsabouttheGale-ShapleyAlgorithm:• Willthisalgorithmterminate?Afterhowlong?• Doesitoutputaperfectmatching?• Doesitoutputastablematching?• Howdoweimplementthisalgorithmefficiently?

Page 24: CS3000: Algorithms & Data Paul Handkhoury.northeastern.edu/home/hand/teaching/cs3000... · Gale-Shapley Algorithm • Let M be empty • While (some job j is unmatched): • If (j

GSAlgorithm:Termination

• Claim:TheGSalgorithmterminatesaftern2iterationsofthemainloop,wherenisnumberofcandidates/jobs.

Page 25: CS3000: Algorithms & Data Paul Handkhoury.northeastern.edu/home/hand/teaching/cs3000... · Gale-Shapley Algorithm • Let M be empty • While (some job j is unmatched): • If (j

GSAlgorithm:PerfectMatching

• Claim:TheGSalgorithmreturnsaperfectmatching(alljobs/candidatesarematched)

Page 26: CS3000: Algorithms & Data Paul Handkhoury.northeastern.edu/home/hand/teaching/cs3000... · Gale-Shapley Algorithm • Let M be empty • While (some job j is unmatched): • If (j

GSAlgorithm:StableMatching

• Stability:GSalgorithmoutputsastablematching• Proofbycontradiction:

• Supposethereisaninstability

Page 27: CS3000: Algorithms & Data Paul Handkhoury.northeastern.edu/home/hand/teaching/cs3000... · Gale-Shapley Algorithm • Let M be empty • While (some job j is unmatched): • If (j

GSAlgorithm:RunningTime

• RunningTime:• Astraightforwardimplementationrequiresatoperations,space(memory).

⇡ n3

⇡ n2

Page 28: CS3000: Algorithms & Data Paul Handkhoury.northeastern.edu/home/hand/teaching/cs3000... · Gale-Shapley Algorithm • Let M be empty • While (some job j is unmatched): • If (j

GSAlgorithm:RunningTime

• Let M be empty • While (some job j is unmatched):

• If (j has offered a job to everyone): break • Else: let c be the highest-ranked candidate to which j has not yet offered a job

• j makes an offer to c: • If (c is unmatched):

• c accepts, add (c,j) to M • ElseIf (c is matched to j’ & c: j’ > j):

• c rejects, do nothing • ElseIf (c is matched to j’ & c: j > j’):

• c accepts, remove (c,j’) from M and add (c,j) to M

• Output M

Page 29: CS3000: Algorithms & Data Paul Handkhoury.northeastern.edu/home/hand/teaching/cs3000... · Gale-Shapley Algorithm • Let M be empty • While (some job j is unmatched): • If (j

GSAlgorithm:RunningTime

• RunningTime:• Acarefulimplementationrequiresjusttimeandspace

⇡ n2

⇡ n2

Page 30: CS3000: Algorithms & Data Paul Handkhoury.northeastern.edu/home/hand/teaching/cs3000... · Gale-Shapley Algorithm • Let M be empty • While (some job j is unmatched): • If (j

GSAlgorithm:RunningTime

• RunningTime:• Acarefulimplementationrequiresjusttimeandspace

1st 2nd 3rd 4th 5th

Alice CH MGH BW MTA BID

Bob BID BW MTA MGH CH

Clara BW BID MTA CH MGH

Dorit MGH CH MTA BID BW

Ernie MTA BW CH BID MGH

MGH BW BID MTA CH

Alice 2nd 3rd 5th 4th 1st

Bob 4th 2nd 1st 3rd 5th

Clara 5th 1st 2nd 3rd 4th

Dorit 1st 5th 4th 3rd 2nd

Ernie 5th 2nd 4th 1st 3rd

Page 31: CS3000: Algorithms & Data Paul Handkhoury.northeastern.edu/home/hand/teaching/cs3000... · Gale-Shapley Algorithm • Let M be empty • While (some job j is unmatched): • If (j

GSAlgorithm:RunningTime

• RunningTime:• Acarefulimplementationrequiresjusttimeandspace

⇡ n2

⇡ n2

Page 32: CS3000: Algorithms & Data Paul Handkhoury.northeastern.edu/home/hand/teaching/cs3000... · Gale-Shapley Algorithm • Let M be empty • While (some job j is unmatched): • If (j

NotesforinstructorStudentsmayignorebecausetheyarerepeatedelsewhere