cs3000: algorithms & data paul handkhoury.northeastern.edu/home/hand/teaching/cs3000... ·...
TRANSCRIPT
![Page 1: CS3000: Algorithms & Data Paul Handkhoury.northeastern.edu/home/hand/teaching/cs3000... · Gale-Shapley Algorithm 1st 2nd j1 c1 c2 j2 c2 c1 1st 2nd c1 j2 j1 c2 j1 j2 What matching](https://reader033.vdocuments.site/reader033/viewer/2022042513/5f3b5496217f31163e0f851d/html5/thumbnails/1.jpg)
CS3000: Algorithms & DataPaul Hand
Lecture 3: • Stable Matching: Gale-Shapley Algorithm• Asymptotic Analysis
Jan 14, 2019
![Page 2: CS3000: Algorithms & Data Paul Handkhoury.northeastern.edu/home/hand/teaching/cs3000... · Gale-Shapley Algorithm 1st 2nd j1 c1 c2 j2 c2 c1 1st 2nd c1 j2 j1 c2 j1 j2 What matching](https://reader033.vdocuments.site/reader033/viewer/2022042513/5f3b5496217f31163e0f851d/html5/thumbnails/2.jpg)
StableMatchingProblem
• Manyjobcandidates(eg.doctors).Manyjobs(eg.residencyprograms).Youaretoassigncandidatestojobs.Howshouldyoudoit?
![Page 3: CS3000: Algorithms & Data Paul Handkhoury.northeastern.edu/home/hand/teaching/cs3000... · Gale-Shapley Algorithm 1st 2nd j1 c1 c2 j2 c2 c1 1st 2nd c1 j2 j1 c2 j1 j2 What matching](https://reader033.vdocuments.site/reader033/viewer/2022042513/5f3b5496217f31163e0f851d/html5/thumbnails/3.jpg)
StableMatchingProblem
• Manyjobcandidates(eg.doctors).Manyjobs(eg.residencyprograms).Youaretoassigncandidatestojobs.Howshouldyoudoit?
![Page 4: CS3000: Algorithms & Data Paul Handkhoury.northeastern.edu/home/hand/teaching/cs3000... · Gale-Shapley Algorithm 1st 2nd j1 c1 c2 j2 c2 c1 1st 2nd c1 j2 j1 c2 j1 j2 What matching](https://reader033.vdocuments.site/reader033/viewer/2022042513/5f3b5496217f31163e0f851d/html5/thumbnails/4.jpg)
![Page 5: CS3000: Algorithms & Data Paul Handkhoury.northeastern.edu/home/hand/teaching/cs3000... · Gale-Shapley Algorithm 1st 2nd j1 c1 c2 j2 c2 c1 1st 2nd c1 j2 j1 c2 j1 j2 What matching](https://reader033.vdocuments.site/reader033/viewer/2022042513/5f3b5496217f31163e0f851d/html5/thumbnails/5.jpg)
Gale-Shapley AlgorithmGale-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 6: CS3000: Algorithms & Data Paul Handkhoury.northeastern.edu/home/hand/teaching/cs3000... · Gale-Shapley Algorithm 1st 2nd j1 c1 c2 j2 c2 c1 1st 2nd c1 j2 j1 c2 j1 j2 What matching](https://reader033.vdocuments.site/reader033/viewer/2022042513/5f3b5496217f31163e0f851d/html5/thumbnails/6.jpg)
Gale-Shapley Algorithm
1st 2ndj1 c1 c2j2 c2 c1
1st 2ndc1 j2 j1c2 j1 j2
What matching does the algorithm give this data for jobs (j1 and j2) and candidates(c1 and c2)?
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 7: CS3000: Algorithms & Data Paul Handkhoury.northeastern.edu/home/hand/teaching/cs3000... · Gale-Shapley Algorithm 1st 2nd j1 c1 c2 j2 c2 c1 1st 2nd c1 j2 j1 c2 j1 j2 What matching](https://reader033.vdocuments.site/reader033/viewer/2022042513/5f3b5496217f31163e0f851d/html5/thumbnails/7.jpg)
Gale-ShapleyAlgorithm
• QuestionsabouttheGale-ShapleyAlgorithm:• Willthisalgorithmterminate?Afterhowlong?• Doesitoutputaperfectmatching?• Doesitoutputastablematching?• Howdoweimplementthisalgorithmefficiently?
![Page 8: CS3000: Algorithms & Data Paul Handkhoury.northeastern.edu/home/hand/teaching/cs3000... · Gale-Shapley Algorithm 1st 2nd j1 c1 c2 j2 c2 c1 1st 2nd c1 j2 j1 c2 j1 j2 What matching](https://reader033.vdocuments.site/reader033/viewer/2022042513/5f3b5496217f31163e0f851d/html5/thumbnails/8.jpg)
Observations about GSGale-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
• At all steps, the state of the algorithm is matching.
• Jobs make offers in descending order
• Candidates that get a job never become unemployed
• Candidates accept offers in ascending order
![Page 9: CS3000: Algorithms & Data Paul Handkhoury.northeastern.edu/home/hand/teaching/cs3000... · Gale-Shapley Algorithm 1st 2nd j1 c1 c2 j2 c2 c1 1st 2nd c1 j2 j1 c2 j1 j2 What matching](https://reader033.vdocuments.site/reader033/viewer/2022042513/5f3b5496217f31163e0f851d/html5/thumbnails/9.jpg)
Does the GS algorithm terminate?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
• Claim: The GS algorithm terminates after !"iterations of the main loop
![Page 10: CS3000: Algorithms & Data Paul Handkhoury.northeastern.edu/home/hand/teaching/cs3000... · Gale-Shapley Algorithm 1st 2nd j1 c1 c2 j2 c2 c1 1st 2nd c1 j2 j1 c2 j1 j2 What matching](https://reader033.vdocuments.site/reader033/viewer/2022042513/5f3b5496217f31163e0f851d/html5/thumbnails/10.jpg)
Is the output a perfect matching?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
• Claim: The GS algorithm outputs a perfect matching (all jobs are matched and all candidates are matched).
![Page 11: CS3000: Algorithms & Data Paul Handkhoury.northeastern.edu/home/hand/teaching/cs3000... · Gale-Shapley Algorithm 1st 2nd j1 c1 c2 j2 c2 c1 1st 2nd c1 j2 j1 c2 j1 j2 What matching](https://reader033.vdocuments.site/reader033/viewer/2022042513/5f3b5496217f31163e0f851d/html5/thumbnails/11.jpg)
Is the output a stable matching?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
• Claim: The GS algorithm outputs a stable matching.• Proof by contradiction:
Suppose there is an instability
![Page 12: CS3000: Algorithms & Data Paul Handkhoury.northeastern.edu/home/hand/teaching/cs3000... · Gale-Shapley Algorithm 1st 2nd j1 c1 c2 j2 c2 c1 1st 2nd c1 j2 j1 c2 j1 j2 What matching](https://reader033.vdocuments.site/reader033/viewer/2022042513/5f3b5496217f31163e0f851d/html5/thumbnails/12.jpg)
Running time of GS?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
• Running Time:• A straightforward implementation
requires ≈ "# operations, ≈ "$ space
![Page 13: CS3000: Algorithms & Data Paul Handkhoury.northeastern.edu/home/hand/teaching/cs3000... · Gale-Shapley Algorithm 1st 2nd j1 c1 c2 j2 c2 c1 1st 2nd c1 j2 j1 c2 j1 j2 What matching](https://reader033.vdocuments.site/reader033/viewer/2022042513/5f3b5496217f31163e0f851d/html5/thumbnails/13.jpg)
Better data structureGale-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
• Running Time:• A careful implementation requires ≈ "#
operations, ≈ "# space
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 14: CS3000: Algorithms & Data Paul Handkhoury.northeastern.edu/home/hand/teaching/cs3000... · Gale-Shapley Algorithm 1st 2nd j1 c1 c2 j2 c2 c1 1st 2nd c1 j2 j1 c2 j1 j2 What matching](https://reader033.vdocuments.site/reader033/viewer/2022042513/5f3b5496217f31163e0f851d/html5/thumbnails/14.jpg)
NotesforinstructorStudentsmayignorebecausetheyarerepeatedelsewhere
![Page 15: CS3000: Algorithms & Data Paul Handkhoury.northeastern.edu/home/hand/teaching/cs3000... · Gale-Shapley Algorithm 1st 2nd j1 c1 c2 j2 c2 c1 1st 2nd c1 j2 j1 c2 j1 j2 What matching](https://reader033.vdocuments.site/reader033/viewer/2022042513/5f3b5496217f31163e0f851d/html5/thumbnails/15.jpg)
Asymptotic Analysis
![Page 16: CS3000: Algorithms & Data Paul Handkhoury.northeastern.edu/home/hand/teaching/cs3000... · Gale-Shapley Algorithm 1st 2nd j1 c1 c2 j2 c2 c1 1st 2nd c1 j2 j1 c2 j1 j2 What matching](https://reader033.vdocuments.site/reader033/viewer/2022042513/5f3b5496217f31163e0f851d/html5/thumbnails/16.jpg)
• Predicting the wall-clock time of an algorithm is basically impossible.• What machine will actually run the algorithm?• Impossible to exactly count “operations”?• Which data will it be applied to?
• What do we do instead?• We compare how the algorithm scales with lots
of data.
Analyzing run time of algorithms
![Page 17: CS3000: Algorithms & Data Paul Handkhoury.northeastern.edu/home/hand/teaching/cs3000... · Gale-Shapley Algorithm 1st 2nd j1 c1 c2 j2 c2 c1 1st 2nd c1 j2 j1 c2 j1 j2 What matching](https://reader033.vdocuments.site/reader033/viewer/2022042513/5f3b5496217f31163e0f851d/html5/thumbnails/17.jpg)
Common computational complexity rates(and what they mean in time)
![Page 18: CS3000: Algorithms & Data Paul Handkhoury.northeastern.edu/home/hand/teaching/cs3000... · Gale-Shapley Algorithm 1st 2nd j1 c1 c2 j2 c2 c1 1st 2nd c1 j2 j1 c2 j1 j2 What matching](https://reader033.vdocuments.site/reader033/viewer/2022042513/5f3b5496217f31163e0f851d/html5/thumbnails/18.jpg)
Common computational complexity rates(and what they mean in time)
Activity:
Suppose 1 million write an essay for a standardized test each year. You have code that takes two essays as input and outputs if there is plagiarism. You want to determine if there is any plagiarism by comparing all possible pairs of essays. Roughly how long will it take?
![Page 19: CS3000: Algorithms & Data Paul Handkhoury.northeastern.edu/home/hand/teaching/cs3000... · Gale-Shapley Algorithm 1st 2nd j1 c1 c2 j2 c2 c1 1st 2nd c1 j2 j1 c2 j1 j2 What matching](https://reader033.vdocuments.site/reader033/viewer/2022042513/5f3b5496217f31163e0f851d/html5/thumbnails/19.jpg)
Common computational complexity rates(and what they mean in time)
Activity:
Suppose someone’s password was an arbitrary sequence of 50 bits. Someone wants to hack it by trying all possible passwords. Roughly how long will this take?
![Page 20: CS3000: Algorithms & Data Paul Handkhoury.northeastern.edu/home/hand/teaching/cs3000... · Gale-Shapley Algorithm 1st 2nd j1 c1 c2 j2 c2 c1 1st 2nd c1 j2 j1 c2 j1 j2 What matching](https://reader033.vdocuments.site/reader033/viewer/2022042513/5f3b5496217f31163e0f851d/html5/thumbnails/20.jpg)
• “Big-Oh” Notation: ! " = $ % " if there exists & ∈ 0,∞ and "+ ∈ ℕ such that ! " ≤ & ⋅ % "for every " ≥ "+.• Asymptotic version of ! " ≤ % "• Roughly equivalent to lim3→5
6 37 3 < ∞
Asymptotic Order Of Growth
![Page 21: CS3000: Algorithms & Data Paul Handkhoury.northeastern.edu/home/hand/teaching/cs3000... · Gale-Shapley Algorithm 1st 2nd j1 c1 c2 j2 c2 c1 1st 2nd c1 j2 j1 c2 j1 j2 What matching](https://reader033.vdocuments.site/reader033/viewer/2022042513/5f3b5496217f31163e0f851d/html5/thumbnails/21.jpg)
• “Big-Oh” Notation: ! " = $ % " if there exists & ∈ 0,∞ and "+ ∈ ℕ such that ! " ≤ & ⋅ % "for every " ≥ "+.• Asymptotic version of ! " ≤ % "• Roughly equivalent to lim3→5
6 37 3 < ∞
Asymptotic Order Of Growth
• Activity: Which of these statements are true?• 3": + " = $ ":• "< = $ ":• 10"> = $ "?• log: " = $ logBC "• " log:(":) = $ " log: "
![Page 22: CS3000: Algorithms & Data Paul Handkhoury.northeastern.edu/home/hand/teaching/cs3000... · Gale-Shapley Algorithm 1st 2nd j1 c1 c2 j2 c2 c1 1st 2nd c1 j2 j1 c2 j1 j2 What matching](https://reader033.vdocuments.site/reader033/viewer/2022042513/5f3b5496217f31163e0f851d/html5/thumbnails/22.jpg)
• Constant factors can be ignored• ∀" > 0 "% = ' %
• Smaller exponents are Big-Oh of larger exponents• ∀( > ) %* = ' %+
• Any logarithm is Big-Oh of any polynomial • ∀(, - > 0 log1+ % = ' %2
• Any polynomial is Big-Oh of any exponential• ∀ ( > 0, ) > 1 %+ = ' )4
• Lower order terms can be dropped• %1 + %6/1 + % = ' %1
Big-Oh Rules
![Page 23: CS3000: Algorithms & Data Paul Handkhoury.northeastern.edu/home/hand/teaching/cs3000... · Gale-Shapley Algorithm 1st 2nd j1 c1 c2 j2 c2 c1 1st 2nd c1 j2 j1 c2 j1 j2 What matching](https://reader033.vdocuments.site/reader033/viewer/2022042513/5f3b5496217f31163e0f851d/html5/thumbnails/23.jpg)
• “Big-Omega” Notation: ! " = Ω % " if there exists & ∈ 0,∞ and "+ ∈ ℕ s.t. ! " ≥ & ⋅ % "for every " ≥ "+.• Asymptotic version of ! " ≥ % "• Roughly equivalent to lim2→4
5 26 2 > 0
• “Big-Theta” Notation: ! " = Θ % " if there exists &9 ≤ &; ∈ 0,∞ and "+ ∈ ℕ such that c; ⋅ % " ≥ ! " ≥ &9 ⋅ % " for every " ≥ "+.• Asymptotic version of ! " = % "• Roughly equivalent to lim2→4
5 26 2 ∈ 0,∞
Asymptotic Order Of Growth
![Page 24: CS3000: Algorithms & Data Paul Handkhoury.northeastern.edu/home/hand/teaching/cs3000... · Gale-Shapley Algorithm 1st 2nd j1 c1 c2 j2 c2 c1 1st 2nd c1 j2 j1 c2 j1 j2 What matching](https://reader033.vdocuments.site/reader033/viewer/2022042513/5f3b5496217f31163e0f851d/html5/thumbnails/24.jpg)
• We usually write running time as a Big-Theta• Exact time per operation doesn’t appear• Constant factors do not appear• Lower order terms do not appear
• Examples:• 30 log& ' + 45 = Θ log '• -' log& 2' = Θ ' log '• ∑0123 4 = Θ '&
Asymptotic Running Times
![Page 25: CS3000: Algorithms & Data Paul Handkhoury.northeastern.edu/home/hand/teaching/cs3000... · Gale-Shapley Algorithm 1st 2nd j1 c1 c2 j2 c2 c1 1st 2nd c1 j2 j1 c2 j1 j2 What matching](https://reader033.vdocuments.site/reader033/viewer/2022042513/5f3b5496217f31163e0f851d/html5/thumbnails/25.jpg)
• “Little-Oh” Notation: ! " = $ % " if for every & > 0 there exists ") ∈ ℕ s.t. ! " < & ⋅ % " for every " ≥ ").• Asymptotic version of ! " < % "• Roughly equivalent to lim2→4
5 26 2 = 0
• “Little-Omega” Notation: ! " = 7 % " if for every & > 0 there exists ") ∈ ℕ such that ! " > & ⋅ % " for every " ≥ ").• Asymptotic version of ! " > % "• Roughly equivalent to lim2→4
5 26 2 = ∞
Asymptotic Order Of Growth
![Page 26: CS3000: Algorithms & Data Paul Handkhoury.northeastern.edu/home/hand/teaching/cs3000... · Gale-Shapley Algorithm 1st 2nd j1 c1 c2 j2 c2 c1 1st 2nd c1 j2 j1 c2 j1 j2 What matching](https://reader033.vdocuments.site/reader033/viewer/2022042513/5f3b5496217f31163e0f851d/html5/thumbnails/26.jpg)
• Fill in the blank with the strongest statement that applies (!, Ω, Θ, %, &) : • 15 * log. * = ____ (log. *)• *. =______(5 *.+*)• 100* = ______ (5 *.+*)• 36789 :=2678< :
Activity