CS3000: Algorithms & Data Paul Hand - Khoury...

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

Transcript of CS3000: Algorithms & Data Paul Hand - Khoury...

Page 1: CS3000: Algorithms & Data Paul Hand - Khoury Collegekhoury.northeastern.edu/home/hand/teaching/cs3000-spring...CS3000: Algorithms & Data Paul Hand Lecture 2: • Finish Induction •

CS3000:Algorithms&Data PaulHand

Lecture2:• FinishInduction• StableMatching:theGale-ShapleyAlgorithm

Jan9,2019

Page 2: CS3000: Algorithms & Data Paul Hand - Khoury Collegekhoury.northeastern.edu/home/hand/teaching/cs3000-spring...CS3000: Algorithms & Data Paul Hand Lecture 2: • Finish Induction •

CourseWebsite

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

Page 3: CS3000: Algorithms & Data Paul Hand - Khoury Collegekhoury.northeastern.edu/home/hand/teaching/cs3000-spring...CS3000: Algorithms & Data Paul Hand Lecture 2: • Finish Induction •

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

HomeworkPolicies

Page 4: CS3000: Algorithms & Data Paul Hand - Khoury Collegekhoury.northeastern.edu/home/hand/teaching/cs3000-spring...CS3000: Algorithms & Data Paul Hand Lecture 2: • Finish Induction •

CourseStructureEnd4/17

Start1/7

DivideandConquer

DynamicProgramming

Graphs NetworkFlow

Textbook:AlgorithmDesignbyKleinbergandTardos

Moreresourcesonthecoursewebsite

FinalTBD

MidtermI2/20

MidtermII3/27

Page 5: CS3000: Algorithms & Data Paul Hand - Khoury Collegekhoury.northeastern.edu/home/hand/teaching/cs3000-spring...CS3000: Algorithms & Data Paul Hand Lecture 2: • Finish Induction •

ProofsbyInduction

Page 6: CS3000: Algorithms & Data Paul Hand - Khoury Collegekhoury.northeastern.edu/home/hand/teaching/cs3000-spring...CS3000: Algorithms & Data Paul Hand Lecture 2: • Finish Induction •

• Claim:Forevery,

• ProofbyInduction:

Exercise

n 2 N,n�1X

i=0

2i = 2n � 1

100000,0

Base cases n L 42 2 1 1

General case n I

Assume 02 I I

o2i f j2 it 27 271 2

2Mt I

Page 7: CS3000: Algorithms & Data Paul Hand - Khoury Collegekhoury.northeastern.edu/home/hand/teaching/cs3000-spring...CS3000: Algorithms & Data Paul Hand Lecture 2: • Finish Induction •

StableMatchingProblemandtheGale-ShapleyAlgorithm

Page 8: CS3000: Algorithms & Data Paul Hand - Khoury Collegekhoury.northeastern.edu/home/hand/teaching/cs3000-spring...CS3000: Algorithms & Data Paul Hand Lecture 2: • Finish Induction •

Processforsolvingcomputationalproblemswithalgorithms

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

Page 9: CS3000: Algorithms & Data Paul Hand - Khoury Collegekhoury.northeastern.edu/home/hand/teaching/cs3000-spring...CS3000: Algorithms & Data Paul Hand Lecture 2: • Finish Induction •

StableMatchingProblem

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

Page 10: CS3000: Algorithms & Data Paul Hand - Khoury Collegekhoury.northeastern.edu/home/hand/teaching/cs3000-spring...CS3000: Algorithms & Data Paul Hand Lecture 2: • Finish Induction •

inputs to alg

Page 11: CS3000: Algorithms & Data Paul Hand - Khoury Collegekhoury.northeastern.edu/home/hand/teaching/cs3000-spring...CS3000: Algorithms & Data Paul Hand Lecture 2: • Finish Induction •
Page 12: CS3000: Algorithms & Data Paul Hand - Khoury Collegekhoury.northeastern.edu/home/hand/teaching/cs3000-spring...CS3000: Algorithms & Data Paul Hand Lecture 2: • Finish Induction •
Page 13: CS3000: Algorithms & Data Paul Hand - Khoury Collegekhoury.northeastern.edu/home/hand/teaching/cs3000-spring...CS3000: Algorithms & Data Paul Hand Lecture 2: • Finish Induction •

Candidates G G Cnjobs I j2 Jn3

pair Ci Jsfirst isthjobperson

Page 14: CS3000: Algorithms & Data Paul Hand - Khoury Collegekhoury.northeastern.edu/home/hand/teaching/cs3000-spring...CS3000: Algorithms & Data Paul Hand Lecture 2: • Finish Induction •
Page 15: CS3000: Algorithms & Data Paul Hand - Khoury Collegekhoury.northeastern.edu/home/hand/teaching/cs3000-spring...CS3000: Algorithms & Data Paul Hand Lecture 2: • Finish Induction •

somorppl havea job

not EVERYone

itbelongsto

Page 16: CS3000: Algorithms & Data Paul Hand - Khoury Collegekhoury.northeastern.edu/home/hand/teaching/cs3000-spring...CS3000: Algorithms & Data Paul Hand Lecture 2: • Finish Induction •

any of the followingprefers

note proofs of jd dont matter

Page 17: CS3000: Algorithms & Data Paul Hand - Khoury Collegekhoury.northeastern.edu/home/hand/teaching/cs3000-spring...CS3000: Algorithms & Data Paul Hand Lecture 2: • Finish Induction •

Find a

c painthat both prefersEach other

0 UC 2732 c B

M l B Ca A 13,4Mz HA B 12,93

Page 18: CS3000: Algorithms & Data Paul Hand - Khoury Collegekhoury.northeastern.edu/home/hand/teaching/cs3000-spring...CS3000: Algorithms & Data Paul Hand Lecture 2: • Finish Induction •
Page 19: CS3000: Algorithms & Data Paul Hand - Khoury Collegekhoury.northeastern.edu/home/hand/teaching/cs3000-spring...CS3000: Algorithms & Data Paul Hand Lecture 2: • Finish Induction •

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

Icoording to j

Page 20: CS3000: Algorithms & Data Paul Hand - Khoury Collegekhoury.northeastern.edu/home/hand/teaching/cs3000-spring...CS3000: Algorithms & Data Paul Hand Lecture 2: • Finish Induction •

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

D

XO

Page 21: CS3000: Algorithms & Data Paul Hand - Khoury Collegekhoury.northeastern.edu/home/hand/teaching/cs3000-spring...CS3000: Algorithms & Data Paul Hand Lecture 2: • Finish Induction •

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 Hand - Khoury Collegekhoury.northeastern.edu/home/hand/teaching/cs3000-spring...CS3000: Algorithms & Data Paul Hand Lecture 2: • Finish Induction •

Observations• Atallsteps,thestateofthealgorithmisamatching

• Jobsmakeoffersindescendingorder

• Candidatesthatgetajobneverbecomeunemployed

• Candidatesacceptoffersinascendingorder

Page 23: CS3000: Algorithms & Data Paul Hand - Khoury Collegekhoury.northeastern.edu/home/hand/teaching/cs3000-spring...CS3000: Algorithms & Data Paul Hand Lecture 2: • Finish Induction •

Gale-ShapleyAlgorithm

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

Page 24: CS3000: Algorithms & Data Paul Hand - Khoury Collegekhoury.northeastern.edu/home/hand/teaching/cs3000-spring...CS3000: Algorithms & Data Paul Hand Lecture 2: • Finish Induction •

GSAlgorithm:Termination

• Claim:TheGSalgorithmterminatesaftern2iterationsofthemainloop,wherenisnumberofcandidates/jobs.

af most

At most n2 possible offasAt Each iter an offer ismade None repeated

So f n iteratus

Page 25: CS3000: Algorithms & Data Paul Hand - Khoury Collegekhoury.northeastern.edu/home/hand/teaching/cs3000-spring...CS3000: Algorithms & Data Paul Hand Lecture 2: • Finish Induction •

GSAlgorithm:PerfectMatching

• Claim:TheGSalgorithmreturnsaperfectmatching(alljobs/candidatesarematched)

Page 26: CS3000: Algorithms & Data Paul Hand - Khoury Collegekhoury.northeastern.edu/home/hand/teaching/cs3000-spring...CS3000: Algorithms & Data Paul Hand Lecture 2: • Finish Induction •

GSAlgorithm:StableMatching

• Stability:GSalgorithmoutputsastablematching• Proofbycontradiction:

• Supposethereisaninstability

Page 27: CS3000: Algorithms & Data Paul Hand - Khoury Collegekhoury.northeastern.edu/home/hand/teaching/cs3000-spring...CS3000: Algorithms & Data Paul Hand Lecture 2: • Finish Induction •

GSAlgorithm:RunningTime

• RunningTime:• Astraightforwardimplementationrequiresatoperations,space(memory).

⇡ n3

⇡ n2

Page 28: CS3000: Algorithms & Data Paul Hand - Khoury Collegekhoury.northeastern.edu/home/hand/teaching/cs3000-spring...CS3000: Algorithms & Data Paul Hand Lecture 2: • Finish Induction •

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 Hand - Khoury Collegekhoury.northeastern.edu/home/hand/teaching/cs3000-spring...CS3000: Algorithms & Data Paul Hand Lecture 2: • Finish Induction •

GSAlgorithm:RunningTime

• RunningTime:• Acarefulimplementationrequiresjusttimeandspace

⇡ n2

⇡ n2

Page 30: CS3000: Algorithms & Data Paul Hand - Khoury Collegekhoury.northeastern.edu/home/hand/teaching/cs3000-spring...CS3000: Algorithms & Data Paul Hand Lecture 2: • Finish Induction •

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 Hand - Khoury Collegekhoury.northeastern.edu/home/hand/teaching/cs3000-spring...CS3000: Algorithms & Data Paul Hand Lecture 2: • Finish Induction •

GSAlgorithm:RunningTime

• RunningTime:• Acarefulimplementationrequiresjusttimeandspace

⇡ n2

⇡ n2

Page 32: CS3000: Algorithms & Data Paul Hand - Khoury Collegekhoury.northeastern.edu/home/hand/teaching/cs3000-spring...CS3000: Algorithms & Data Paul Hand Lecture 2: • Finish Induction •

NotesforinstructorStudentsmayignorebecausetheyarerepeatedelsewhere