CS3000: Algorithms & Data Paul Hand - Khoury...
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 •](https://reader036.fdocuments.us/reader036/viewer/2022062303/611308abdf1a5770c3268a53/html5/thumbnails/1.jpg)
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 •](https://reader036.fdocuments.us/reader036/viewer/2022062303/611308abdf1a5770c3268a53/html5/thumbnails/2.jpg)
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 •](https://reader036.fdocuments.us/reader036/viewer/2022062303/611308abdf1a5770c3268a53/html5/thumbnails/3.jpg)
• 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 •](https://reader036.fdocuments.us/reader036/viewer/2022062303/611308abdf1a5770c3268a53/html5/thumbnails/4.jpg)
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 •](https://reader036.fdocuments.us/reader036/viewer/2022062303/611308abdf1a5770c3268a53/html5/thumbnails/5.jpg)
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 •](https://reader036.fdocuments.us/reader036/viewer/2022062303/611308abdf1a5770c3268a53/html5/thumbnails/6.jpg)
• 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 •](https://reader036.fdocuments.us/reader036/viewer/2022062303/611308abdf1a5770c3268a53/html5/thumbnails/7.jpg)
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 •](https://reader036.fdocuments.us/reader036/viewer/2022062303/611308abdf1a5770c3268a53/html5/thumbnails/8.jpg)
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 •](https://reader036.fdocuments.us/reader036/viewer/2022062303/611308abdf1a5770c3268a53/html5/thumbnails/9.jpg)
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 •](https://reader036.fdocuments.us/reader036/viewer/2022062303/611308abdf1a5770c3268a53/html5/thumbnails/10.jpg)
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 •](https://reader036.fdocuments.us/reader036/viewer/2022062303/611308abdf1a5770c3268a53/html5/thumbnails/11.jpg)
![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 •](https://reader036.fdocuments.us/reader036/viewer/2022062303/611308abdf1a5770c3268a53/html5/thumbnails/12.jpg)
![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 •](https://reader036.fdocuments.us/reader036/viewer/2022062303/611308abdf1a5770c3268a53/html5/thumbnails/13.jpg)
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 •](https://reader036.fdocuments.us/reader036/viewer/2022062303/611308abdf1a5770c3268a53/html5/thumbnails/14.jpg)
![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 •](https://reader036.fdocuments.us/reader036/viewer/2022062303/611308abdf1a5770c3268a53/html5/thumbnails/15.jpg)
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 •](https://reader036.fdocuments.us/reader036/viewer/2022062303/611308abdf1a5770c3268a53/html5/thumbnails/16.jpg)
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 •](https://reader036.fdocuments.us/reader036/viewer/2022062303/611308abdf1a5770c3268a53/html5/thumbnails/17.jpg)
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 •](https://reader036.fdocuments.us/reader036/viewer/2022062303/611308abdf1a5770c3268a53/html5/thumbnails/18.jpg)
![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 •](https://reader036.fdocuments.us/reader036/viewer/2022062303/611308abdf1a5770c3268a53/html5/thumbnails/19.jpg)
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 •](https://reader036.fdocuments.us/reader036/viewer/2022062303/611308abdf1a5770c3268a53/html5/thumbnails/20.jpg)
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 •](https://reader036.fdocuments.us/reader036/viewer/2022062303/611308abdf1a5770c3268a53/html5/thumbnails/21.jpg)
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 •](https://reader036.fdocuments.us/reader036/viewer/2022062303/611308abdf1a5770c3268a53/html5/thumbnails/22.jpg)
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 •](https://reader036.fdocuments.us/reader036/viewer/2022062303/611308abdf1a5770c3268a53/html5/thumbnails/23.jpg)
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 •](https://reader036.fdocuments.us/reader036/viewer/2022062303/611308abdf1a5770c3268a53/html5/thumbnails/24.jpg)
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 •](https://reader036.fdocuments.us/reader036/viewer/2022062303/611308abdf1a5770c3268a53/html5/thumbnails/25.jpg)
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 •](https://reader036.fdocuments.us/reader036/viewer/2022062303/611308abdf1a5770c3268a53/html5/thumbnails/26.jpg)
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 •](https://reader036.fdocuments.us/reader036/viewer/2022062303/611308abdf1a5770c3268a53/html5/thumbnails/27.jpg)
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 •](https://reader036.fdocuments.us/reader036/viewer/2022062303/611308abdf1a5770c3268a53/html5/thumbnails/28.jpg)
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 •](https://reader036.fdocuments.us/reader036/viewer/2022062303/611308abdf1a5770c3268a53/html5/thumbnails/29.jpg)
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 •](https://reader036.fdocuments.us/reader036/viewer/2022062303/611308abdf1a5770c3268a53/html5/thumbnails/30.jpg)
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 •](https://reader036.fdocuments.us/reader036/viewer/2022062303/611308abdf1a5770c3268a53/html5/thumbnails/31.jpg)
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 •](https://reader036.fdocuments.us/reader036/viewer/2022062303/611308abdf1a5770c3268a53/html5/thumbnails/32.jpg)
NotesforinstructorStudentsmayignorebecausetheyarerepeatedelsewhere