Download as pdf or txt
Download as pdf or txt
You are on page 1of 76

CS 310: Algorithms

Instructor: Naveed Bhatti and Basit Shafiq


Webpage: naveedanwarbhatti.github.io
https://web.lums.edu.pk/~basit
Lecture slides

• Lecture slides are not the only course content


• Discussions and explanation in between are equally important
• To extract maximum benefit, take notes (like the good old days!).
• I will selectively show/use slides
Algorithm Definition

“ An algorithm is a finite, definite, effective


procedure,
with some input and some output. ”
— Donald Knuth

Slide credit: Kevin Wayne. Theory of Algorithms (COS423)


www.cs.princeton.edu/courses/archive/spring18/cos423
Algorithm Definition

“ An algorithm is a finite, definite, effective


procedure,
with some input and some output. ”
— Donald Knuth

Slide credit: Kevin Wayne. Theory of Algorithms (COS423)


www.cs.princeton.edu/courses/archive/spring18/cos423
Algorithm Definition

“ An algorithm is a finite, definite, effective


procedure,
with some input and some output. ”
— Donald Knuth

Slide credit: Kevin Wayne. Theory of Algorithms (COS423)


www.cs.princeton.edu/courses/archive/spring18/cos423
Algorithm Definition

“ An algorithm is a finite, definite, effective


procedure,
with some input and some output. ”
— Donald Knuth

Slide credit: Kevin Wayne. Theory of Algorithms (COS423)


www.cs.princeton.edu/courses/archive/spring18/cos423
Why study algorithms?

For fun and profit.


Why study algorithms?

Internet. Web search, packet routing, distributed file sharing, ...


Biology. Human genome project, protein folding, …
Computers. Circuit layout, databases, caching, networking, compilers, …
Computer graphics. Movies, video games, virtual reality, …
Security. Cell phones, e-commerce, voting machines, …
Multimedia. MP3, JPG, DivX, HDTV, face recognition, …
Social networks. Recommendations, news feeds, advertisements, …
Physics. Particle collision simulation, n-body simulation, …

Slide credit: Kevin Wayne. Theory of Algorithms (COS423)


www.cs.princeton.edu/courses/archive/spring18/cos423
Chapter 1:
Introduction and Some Representative Problems

12
A First Problem: Stable Matching

• Nicely illustrates many of the themes we will be emphasizing

• Target Applications:
• Selecting teaching assistants (TA) for courses – TA-course matching problem
• Matching medical students to hospitals
• Matching employers to applicants for job hiring
• College admission – matching students to colleges
• Content delivery networks – assigning users to web servers

13
A First Problem: Stable Matching

• Nicely illustrates many of the themes we will be emphasizing

• Target Applications:
• Selecting teaching assistants (TA) for courses – TA-course matching problem
• Matching medical students to hospitals
• Matching employers to applicants for job hiring
• College admission – matching students to colleges
• Content delivery networks – assigning users to web servers

14
Stability Matching – Formulating the Problem

TA-course matching problem


• Suppose:
• n courses are being offered by the CS Department in the current semester
• Due to budget constraints only one TA can be assigned to each course – n TAs for n
courses
• Each course instructor ranks all the TA applicants in the order of his/her preference
(preference list of the course instructor)
• Each TA applicant ranks all the courses in the order of his/her preference
(preference list of TA applicant)
• Issues
• An applicant accepting TAship offer for a course may later quit it for a different
course that is ranked higher in his/her preference list
• A course instructor may withdraw an offer accepted by an applicant to hire another
TA who is ranked higher in the instructor’s preference list
• Unstable matching

15
TA-course matching problem

Goal. Given a set of preferences among course instructors and TA


applicants, design a self-reinforcing TA selection process

Unstable pair. Course instructor c and TA applicant a form an unstable pair


if both:
c prefers a to an already selected TA
a prefers c to the already assigned course instructor

course instructor A TA A
Unstable Pair
(course instr A, TA B)

course instructor B TA B
16
TA-course matching problem

Goal. Given a set of preferences among course instructors and TA


applicants, design a self-reinforcing TA selection process

Unstable pair. Course instructor c and TA applicant a form an unstable pair


if both:
c prefers a to an already selected TA
a prefers c to the already assigned course instructor

Stable assignment. Assignment with no unstable pairs.


Prevents TAs quitting the assigned courses
Prevent instructor withdrawing TAship offer
17
TA course matching problem - Example

Input. A set of n course instructors C and a set of n TA applicants A


• Each course instructor 𝑐 ∈ 𝐶 ranks TA applicants
• Each TA applicant 𝑎 ∈ 𝐴 ranks courses

1st 2nd 3rd 1st 2nd 3rd


CS 100 Alice Bob Charlie Alice CS 200 CS 100 CS 300
CS 200 Bob Alice Charlie Bob CS 100 CS 200 CS 300
CS 300 Alice Bob Charlie Charlie CS 100 CS 200 CS 300
Course instructors’ preference list TA applicants’ preference list

18
Perfect matching

• Def. A matching 𝑀 is a set of ordered pairs 𝑐 − 𝑎 with 𝑐 ∈ 𝐶and 𝑎 ∈


𝐴 such that:
• Each course instructor 𝑐 ∈ 𝐶 appears in at most one pair of 𝑀
• Each TA applicant 𝑎 ∈ 𝐴 appears in at most one pair of 𝑀

1st 2nd 3rd 1st 2nd 3rd


CS 100 Alice Bob Charlie Alice CS 200 CS 100 CS 300
CS 200 Bob Alice Charlie Bob CS 100 CS 200 CS 300
CS 300 Alice Bob Charlie Charlie CS 100 CS 200 CS 300
Course instructors’ preference list TA applicants’ preference list

M = {(CS100 – Charlie)}
19
Perfect matching

• Def. A matching 𝑀 is a set of ordered pairs 𝑐 − 𝑎 with 𝑐 ∈ 𝐶and 𝑎 ∈


𝐴 such that:
• Each course instructor 𝑐 ∈ 𝐶 appears in at most one pair of 𝑀
• Each TA applicant 𝑎 ∈ 𝐴 appears in at most one pair of 𝑀

1st 2nd 3rd 1st 2nd 3rd


CS 100 Alice Bob Charlie Alice CS 200 CS 100 CS 300
CS 200 Bob Alice Charlie Bob CS 100 CS 200 CS 300
CS 300 Alice Bob Charlie Charlie CS 100 CS 200 CS 300
Course instructors’ preference list TA applicants’ preference list

M = {(CS100 – Charlie)}
M = {(CS100 – Charlie), (CS200 – Bob)}
20
Perfect matching

Def. A matching 𝑀 is perfect if 𝑀 = 𝐶 = 𝐴 = 𝑛

• Each course instructor 𝑐 ∈ 𝐶 appears in one pair of 𝑀


• Each TA applicant 𝑎 ∈ 𝐴 appears in one pair of 𝑀

1st 2nd 3rd 1st 2nd 3rd


CS 100 Alice Bob Charlie Alice CS 200 CS 100 CS 300
CS 200 Bob Alice Charlie Bob CS 100 CS 200 CS 300
CS 300 Alice Bob Charlie Charlie CS 100 CS 200 CS 300
Course instructors’ preference list TA applicants’ preference list

A perfect matching M = {(CS100 – Charlie), (CS200 – Bob), (CS300 – Alice)}


21
Unstable pair

• Def. Given a perfect matching 𝑀, course instructor 𝑐 and a TA


applicant 𝑎 form an unstable pair if both
• 𝑐 prefers 𝑎 to the assigned TA
• 𝑎 prefers 𝑐 to the assigned course
M = {(CS100 – Charlie), (CS200 – Bob), (CS300 – Alice)}

1st 2nd 3rd 1st 2nd 3rd


CS 100 Alice Bob Charlie Alice CS 200 CS 100 CS 300
CS 200 Bob Alice Charlie Bob CS 100 CS 200 CS 300
CS 300 Alice Bob Charlie Charlie CS 100 CS 200 CS 300
Course instructors’ preference list TA applicants’ preference list

Can you find any unstable pair?


22
Unstable pair

• Def. Given a perfect matching 𝑀, course instructor 𝑐 and a TA


applicant 𝑎 form an unstable pair if both
• 𝑐 prefers 𝑎 to the assigned TA
• 𝑎 prefers 𝑐 to the assigned course
M = {(CS100 – Charlie), (CS200 – Bob), (CS300 – Alice)}

1st 2nd 3rd 1st 2nd 3rd


CS 100 Alice Bob Charlie Alice CS 200 CS 100 CS 300
CS 200 Bob Alice Charlie Bob CS 100 CS 200 CS 300
CS 300 Alice Bob Charlie Charlie CS 100 CS 200 CS 300
Course instructors’ preference list TA applicants’ preference list

(CS100 – Bob) and (CS100 – Alice) are unstable pair


23
Unstable pair – Through Bipartite Graph

• Def. Given a perfect matching 𝑀, course instructor 𝑐 and a TA


applicant 𝑎 form an unstable pair if both
• 𝑐 prefers 𝑎 to the assigned TA
• 𝑎 prefers 𝑐 to the assigned course
M = {(CS100 – Charlie), (CS200 – Bob), (CS300 – Alice)}

1st 2nd 3rd 1st 2nd 3rd


CS 100 Alice Bob Charlie Alice CS 200 CS 100 CS 300
CS 200 Bob Alice Charlie Bob CS 100 CS 200 CS 300
CS 300 Alice Bob Charlie Charlie CS 100 CS 200 CS 300
Course instructors’ preference list TA applicants’ preference list

(CS100 – Bob) and (CS100 – Alice) are unstable pair


24
Unstable pair – Through Bipartite Graph
1st 2nd 3rd 1st 2nd 3rd
CS 100 Alice Bob Charlie Alice CS 200 CS 100 CS 300
CS 200 Bob Alice Charlie Bob CS 100 CS 200 CS 300
CS 300 Alice Bob Charlie Charlie CS 100 CS 200 CS 300
M = {(CS100 – Charlie), (CS200 – Bob), (CS300 – Alice)}

CS100 Alice

CS200 Bob

CS300 Charlie

(CS100 – Alice) are unstable pair


25
Unstable pair – How to resolve?
1st 2nd 3rd 1st 2nd 3rd
CS 100 Alice Bob Charlie Alice CS 200 CS 100 CS 300
CS 200 Bob Alice Charlie Bob CS 100 CS 200 CS 300
CS 300 Alice Bob Charlie Charlie CS 100 CS 200 CS 300
M = {(CS100 – Charlie), (CS200 – Bob), (CS300 – Alice)}

CS100 Alice
an unstable pair
𝑐 − 𝑎 could each
improve by joint CS200 Bob
action

CS300 Charlie

(CS100 – Alice) are unstable pair


26
Unstable pair – How to resolve?
1st 2nd 3rd 1st 2nd 3rd
CS 100 Alice Bob Charlie Alice CS 200 CS 100 CS 300
CS 200 Bob Alice Charlie Bob CS 100 CS 200 CS 300
CS 300 Alice Bob Charlie Charlie CS 100 CS 200 CS 300
M = {(CS100 – Alice), (CS200 – Bob)}

CS100 Alice

CS200 Bob

CS300 Charlie

27
Unstable pair – How to resolve?
1st 2nd 3rd 1st 2nd 3rd
CS 100 Alice Bob Charlie Alice CS 200 CS 100 CS 300
CS 200 Bob Alice Charlie Bob CS 100 CS 200 CS 300
CS 300 Alice Bob Charlie Charlie CS 100 CS 200 CS 300
M = {(CS100 – Alice), (CS200 – Bob), (CS300 – Charlie)}

CS100 Alice

CS200 Bob

CS300 Charlie

No unstable pair
Perfect Matching and Stable Assignment 28
Stable matching – Live Poll 1

Given matching M = {(CS100 – Alice),


(CS200 – Charlie), (CS300 – Bob)}, which
of the following pair is unstable?
A. (CS100 - Bob)
B. (CS200 – Alice)
C. (CS300 – Charlie)
D. None of the above
1st 2nd 3rd 1st 2nd 3rd
CS 100 Alice Bob Charlie Alice CS 200 CS 100 CS 300
CS 200 Bob Alice Charlie Bob CS 100 CS 200 CS 300
CS 300 Alice Bob Charlie Charlie CS 100 CS 200 CS 300
Course instructors’ preference list TA applicants’ preference list
29
Stable Matching – Moving Towards Algorithm Design
1st 2nd 3rd 1st 2nd 3rd
CS 100 Alice Bob Charlie Alice CS 200 CS 100 CS 300
CS 200 Bob Alice Charlie Bob CS 100 CS 200 CS 300
CS 300 Alice Bob Charlie Charlie CS 100 CS 200 CS 300

Select Course c ∈ C CS100 Alice


CS 100

CS200 Bob

CS300 Charlie

31
Stable Matching – Moving Towards Algorithm Design
1st 2nd 3rd 1st 2nd 3rd
CS 100 Alice Bob Charlie Alice CS 200 CS 100 CS 300
CS 200 Bob Alice Charlie Bob CS 100 CS 200 CS 300
CS 300 Alice Bob Charlie Charlie CS 100 CS 200 CS 300

Select Course c ∈ C CS100 Alice


CS 100

Select Highest Preference a ∈ TA of c


Alice CS200 Bob

CS300 Charlie

32
Stable Matching – Moving Towards Algorithm Design
1st 2nd 3rd 1st 2nd 3rd
CS 100 Alice Bob Charlie Alice CS 200 CS 100 CS 300
CS 200 Bob Alice Charlie Bob CS 100 CS 200 CS 300
CS 300 Alice Bob Charlie Charlie CS 100 CS 200 CS 300

Select Course c ∈ C CS100 Alice


CS 100

Select Highest Preference a ∈ TA of c


Alice CS200 Bob

If a is free then assign


(CS 100 – Alice) CS300 Charlie

33
Stable Matching – Moving Towards Algorithm Design
1st 2nd 3rd 1st 2nd 3rd
CS 100 Alice Bob Charlie Alice CS 200 CS 100 CS 300
CS 200 Bob Alice Charlie Bob CS 100 CS 200 CS 300
CS 300 Alice Bob Charlie Charlie CS 100 CS 200 CS 300

Select Course c ∈ C CS100 Alice


CS 200

Select Highest Preference a ∈ TA of c


CS200 Bob

If a is free then assign


CS300 Charlie

34
Stable Matching – Moving Towards Algorithm Design
1st 2nd 3rd 1st 2nd 3rd
CS 100 Alice Bob Charlie Alice CS 200 CS 100 CS 300
CS 200 Bob Alice Charlie Bob CS 100 CS 200 CS 300
CS 300 Alice Bob Charlie Charlie CS 100 CS 200 CS 300

Select Course c ∈ C CS100 Alice


CS 200

Select Highest Preference a ∈ TA of c


Bob CS200 Bob

If a is free then assign


CS300 Charlie

35
Stable Matching – Moving Towards Algorithm Design
1st 2nd 3rd 1st 2nd 3rd
CS 100 Alice Bob Charlie Alice CS 200 CS 100 CS 300
CS 200 Bob Alice Charlie Bob CS 100 CS 200 CS 300
CS 300 Alice Bob Charlie Charlie CS 100 CS 200 CS 300

Select Course c ∈ C CS100 Alice


CS 200

Select Highest Preference a ∈ TA of c


Bob CS200 Bob

If a is free then assign


(CS 200 – Bob) CS300 Charlie

36
Stable Matching – Moving Towards Algorithm Design
1st 2nd 3rd 1st 2nd 3rd
CS 100 Alice Bob Charlie Alice CS 200 CS 100 CS 300
CS 200 Bob Alice Charlie Bob CS 100 CS 200 CS 300
CS 300 Alice Bob Charlie Charlie CS 100 CS 200 CS 300

Select Course c ∈ C CS100 Alice


CS 300

Select Highest Preference a ∈ TA of c


CS200 Bob

If a is free then assign


CS300 Charlie

37
Stable Matching – Moving Towards Algorithm Design
1st 2nd 3rd 1st 2nd 3rd
CS 100 Alice Bob Charlie Alice CS 200 CS 100 CS 300
CS 200 Bob Alice Charlie Bob CS 100 CS 200 CS 300
CS 300 Alice Bob Charlie Charlie CS 100 CS 200 CS 300

Select Course c ∈ C CS100 Alice


CS 300

Select Highest Preference a ∈ TA of c


Alice CS200 Bob

If a is free then assign


CS300 Charlie

38
Stable Matching – Moving Towards Algorithm Design
1st 2nd 3rd 1st 2nd 3rd
CS 100 Alice Bob Charlie Alice CS 200 CS 100 CS 300
CS 200 Bob Alice Charlie Bob CS 100 CS 200 CS 300
CS 300 Alice Bob Charlie Charlie CS 100 CS 200 CS 300

Select Course c ∈ C CS100 Alice


CS 300

Select Highest Preference a ∈ TA of c


Alice CS200 Bob
We need to add
If a is free then assign additional condition
Already assigned CS300 Charlie

39
Stable Matching – Moving Towards Algorithm Design
1st 2nd 3rd 1st 2nd 3rd
CS 100 Alice Bob Charlie Alice CS 200 CS 100 CS 300
CS 200 Bob Alice Charlie Bob CS 100 CS 200 CS 300
CS 300 Alice Bob Charlie Charlie CS 100 CS 200 CS 300

Now what? We need to


Select Course c ∈ C clarify this condition
CS100 Alice
CS 300

Select Highest Preference a ∈ TA of c


Alice CS200 Bob

If a is free then assign


Already assigned CS300 Charlie

Else if a prefers c’ to c then c remains free

40
Stable Matching – Moving Towards Algorithm Design
1st 2nd 3rd 1st 2nd 3rd
CS 100 Alice Bob Charlie Alice CS 200 CS 100 CS 300
CS 200 Bob Alice Charlie Bob CS 100 CS 200 CS 300
CS 300 Alice Bob Charlie Charlie CS 100 CS 200 CS 300

While Course c ∈ C is free CS100 Alice


CS 300

Select Highest Preference a ∈ TA of c


CS200 Bob

If a is free then assign


CS300 Charlie

Else if a prefers c’ to c then c remains free

41
Stable Matching – Moving Towards Algorithm Design
1st 2nd 3rd 1st 2nd 3rd
CS 100 Alice Bob Charlie Alice CS 200 CS 100 CS 300
CS 200 Bob Alice Charlie Bob CS 100 CS 200 CS 300
CS 300 Alice Bob Charlie Charlie CS 100 CS 200 CS 300

While Course c ∈ C is free CS100 Alice


CS 300

Select Highest Preference a ∈ TA of c


Alice CS200 Bob
Stuck in loop. We need
If a is free then assign to modify this condition
Already assigned CS300 Charlie

Else if a prefers c’ to c then c remains free

42
Stable Matching – Moving Towards Algorithm Design
1st 2nd 3rd 1st 2nd 3rd
CS 100 Alice Bob Charlie Alice CS 200 CS 100 CS 300
CS 200 Bob Alice Charlie Bob CS 100 CS 200 CS 300
CS 300 Alice Bob Charlie Charlie CS 100 CS 200 CS 300

While Course c ∈ C is free CS100 Alice


CS 300
Select Highest Preference a ∈ TA of c to whom c has
not yet proposed CS200 Bob
Bob
If a is free then assign
CS300 Charlie

Else if a prefers c’ to c then c remains free

43
Stable Matching – Moving Towards Algorithm Design
1st 2nd 3rd 1st 2nd 3rd
CS 100 Alice Bob Charlie Alice CS 200 CS 100 CS 300
CS 200 Bob Alice Charlie Bob CS 100 CS 200 CS 300
CS 300 Alice Bob Charlie Charlie CS 100 CS 200 CS 300

While Course c ∈ C is free CS100 Alice


CS 300
Select Highest Preference a ∈ TA of c to whom c has
not yet proposed CS200 Bob
Bob
If a is free then assign
Already assigned CS300 Charlie

Else if a prefers c’ to c then c remains free

44
Stable Matching – Moving Towards Algorithm Design
1st 2nd 3rd 1st 2nd 3rd
CS 100 Alice Bob Charlie Alice CS 200 CS 100 CS 300
CS 200 Bob Alice Charlie Bob CS 100 CS 200 CS 300
CS 300 Alice Bob Charlie Charlie CS 100 CS 200 CS 300

While Course c ∈ C is free CS100 Alice


CS 300
Select Highest Preference a ∈ TA of c to whom c has
not yet proposed CS200 Bob
Charlie
If a is free then assign
CS300 Charlie

Else if a prefers c’ to c then c remains free

45
Stable Matching – Moving Towards Algorithm Design
1st 2nd 3rd 1st 2nd 3rd
CS 100 Alice Bob Charlie Alice CS 200 CS 100 CS 300
CS 200 Bob Alice Charlie Bob CS 100 CS 200 CS 300
CS 300 Alice Bob Charlie Charlie CS 100 CS 200 CS 300

While Course c ∈ C is free CS100 Alice


CS 300
Select Highest Preference a ∈ TA of c to whom c has
not yet proposed CS200 Bob
Charlie
If a is free then assign
(CS 300 – Charlie) CS300 Charlie

Else if a prefers c’ to c then c remains free

46
Stable Matching – Moving Towards Algorithm Design
1st 2nd 3rd 1st 2nd 3rd
CS 100 Alice Bob Charlie Alice CS 200 CS 100 CS 300
CS 200 Bob Alice Charlie Bob CS 100 CS 200 CS 300
CS 300 Alice Bob Charlie Charlie CS 100 CS 200 CS 300

While Course c ∈ C is free CS100 Alice


CS 300
Select Highest Preference a ∈ TA of c to whom c has
not yet proposed CS200 Bob
Charlie
If a is free then assign
(CS 300 – Charlie) CS300 Charlie

Else if a prefers c’ to c then c remains free


M = {(CS100 – Alice), (CS200 – Bob), (CS300 – Charlie)}
47
Stable Matching – Moving Towards Algorithm Design
1st 2nd 3rd 1st 2nd 3rd
CS 100 Alice Bob Charlie Alice CS 200 CS 100 CS 300
CS 200 Bob Alice Charlie Bob CS 100 CS 200 CS 300
CS 300 Alice Bob Charlie Charlie CS 100 CS 200 CS 300

While Course c ∈ C is free CS100 Alice


CS 300
Select Highest Preference a ∈ TA of c to whom c has
not yet proposed CS200 Bob
Charlie
If a is free then assign
(CS 300 – Charlie) CS300 Charlie
Observation 1. Course instructors make offer to TA applicants in decreasing order of
preference
Else if a prefers c’ to c then c remains free
M = {(CS100 – Alice), (CS200 – Bob), (CS300 – Charlie)}
48
Stable Matching – Moving Towards Algorithm Design
1st 2nd 3rd 1st 2nd 3rd
CS 100 Alice Bob Charlie Alice CS 200 CS 100 CS 300
CS 200 Bob Alice Charlie Bob CS 100 CS 200 CS 300
CS 300 Alice Bob Charlie Charlie CS 100 CS 200 CS 300

While Course c ∈ C is free CS100 Alice


CS 300
Select Highest Preference a ∈ TA of c to whom c has
not yet proposed CS200 Bob
Charlie
If a is free then assign
(CS 300 – Charlie) CS300 Charlie

Else if a prefers c’ to c then c remains free


M = {(CS100 – Alice), (CS200 – Bob), (CS300 – Charlie)}
49
Stable Matching – Moving Towards Algorithm Design
1st 2nd 3rd 1st 2nd 3rd
CS 100 Alice Bob Charlie Alice CS 200 CS 100 CS 300
CS 200 Bob Alice Charlie Bob CS 100 CS 200 CS 300
CS 300 Alice Bob Charlie Charlie CS 100 CS 200 CS 300

While Course c ∈ C is free CS100 Alice


CS 300
Select Highest Preference a ∈ TA of c to whom c has
not yet proposed CS200 Bob
Charlie
If a is free then assign What if a prefer
(CS 300 – Charlie) c to c’ ? CS300 Charlie

Else if a prefers c’ to c then c remains free

Let try new example – Worst Case Scenario


50
Stable Matching – Moving Towards Algorithm Design 1
1st 2nd 3rd 1st 2nd 3rd
CS 100 Alice Bob Charlie Alice CS 200 CS 300
100 CS 100
300
CS 200 Bob Alice Charlie Bob CS 300
100 CS 100
200 CS 200
300
CS 300 Alice Bob Charlie Charlie CS 100 CS 200 CS 300

While Course c ∈ C is free CS100 Alice


CS 100
Select Highest Preference a ∈ TA of c to whom c has
not yet proposed CS200 Bob

If a is free then assign


CS300 Charlie

Else if a prefers c’ to c then c remains free

51
Stable Matching – Moving Towards Algorithm Design 1
1st 2nd 3rd 1st 2nd 3rd
CS 100 Alice Bob Charlie Alice CS 200 CS 300
100 CS 100
300
CS 200 Bob Charlie
Alice Alice
Charlie Bob CS 300
100 CS 100
200 CS 200
300
CS 300 Alice Bob Charlie Charlie CS 100 CS 200 CS 300

While Course c ∈ C is free CS100 Alice


CS 100
Select Highest Preference a ∈ TA of c to whom c has
not yet proposed CS200 Bob
Alice
If a is free then assign
CS300 Charlie

Else if a prefers c’ to c then c remains free

52
Stable Matching – Moving Towards Algorithm Design 1
1st 2nd 3rd 1st 2nd 3rd
CS 100 Alice Bob Charlie Alice CS 200 CS 300
100 CS 100
300
CS 200 Bob Charlie
Alice Alice
Charlie Bob CS 300
100 CS 100
200 CS 200
300
CS 300 Alice Bob Charlie Charlie CS 100 CS 200 CS 300

While Course c ∈ C is free CS100 Alice


CS 100
Select Highest Preference a ∈ TA of c to whom c has
not yet proposed CS200 Bob
Alice
If a is free then assign
(CS 100 – Alice) CS300 Charlie

Else if a prefers c’ to c then c remains free

53
Stable Matching – Moving Towards Algorithm Design 2
1st 2nd 3rd 1st 2nd 3rd
CS 100 Alice Bob Charlie Alice CS 200 CS 300
100 CS 100
300
CS 200 Bob Charlie
Alice Alice
Charlie Bob CS 300
100 CS 100
200 CS 200
300
CS 300 Alice Bob Charlie Charlie CS 100 CS 200 CS 300

While Course c ∈ C is free CS100 Alice


CS 200
Select Highest Preference a ∈ TA of c to whom c has
not yet proposed CS200 Bob

If a is free then assign


CS300 Charlie

Else if a prefers c’ to c then c remains free

54
Stable Matching – Moving Towards Algorithm Design 2
1st 2nd 3rd 1st 2nd 3rd
CS 100 Alice Bob Charlie Alice CS 200 CS 300
100 CS 100
300
CS 200 Bob Charlie
Alice Alice
Charlie Bob CS 300
100 CS 100
200 CS 200
300
CS 300 Alice Bob Charlie Charlie CS 100 CS 200 CS 300

While Course c ∈ C is free CS100 Alice


CS 200
Select Highest Preference a ∈ TA of c to whom c has
not yet proposed CS200 Bob
Bob
If a is free then assign
CS300 Charlie

Else if a prefers c’ to c then c remains free

55
Stable Matching – Moving Towards Algorithm Design 2
1st 2nd 3rd 1st 2nd 3rd
CS 100 Alice Bob Charlie Alice CS 200 CS 300
100 CS 100
300
CS 200 Bob Charlie
Alice Alice
Charlie Bob CS 300
100 CS 100
200 CS 200
300
CS 300 Alice Bob Charlie Charlie CS 100 CS 200 CS 300

While Course c ∈ C is free CS100 Alice


CS 200
Select Highest Preference a ∈ TA of c to whom c has
not yet proposed CS200 Bob
Bob
If a is free then assign
(CS 200 – Bob) CS300 Charlie

Else if a prefers c’ to c then c remains free

56
Stable Matching – Moving Towards Algorithm Design 3
1st 2nd 3rd 1st 2nd 3rd
CS 100 Alice Bob Charlie Alice CS 200 CS 300
100 CS 100
300
CS 200 Bob Charlie
Alice Alice
Charlie Bob CS 300
100 CS 100
200 CS 200
300
CS 300 Alice Bob Charlie Charlie CS 100 CS 200 CS 300

While Course c ∈ C is free CS100 Alice


CS 300
Select Highest Preference a ∈ TA of c to whom c has
not yet proposed CS200 Bob

If a is free then assign


CS300 Charlie

Else if a prefers c’ to c then c remains free

57
Stable Matching – Moving Towards Algorithm Design 3
1st 2nd 3rd 1st 2nd 3rd
CS 100 Alice Bob Charlie Alice CS 200 CS 300
100 CS 100
300
CS 200 Bob Charlie
Alice Alice
Charlie Bob CS 300
100 CS 100
200 CS 200
300
CS 300 Alice Bob Charlie Charlie CS 100 CS 200 CS 300

While Course c ∈ C is free CS100 Alice


CS 300
Select Highest Preference a ∈ TA of c to whom c has
not yet proposed CS200 Bob
Alice
If a is free then assign
CS300 Charlie

Else if a prefers c’ to c then c remains free

58
Stable Matching – Moving Towards Algorithm Design 3
1st 2nd 3rd 1st 2nd 3rd
CS 100 Alice Bob Charlie Alice CS 200 CS 300
100 CS 100
300
CS 200 Bob Charlie
Alice Alice
Charlie Bob CS 300
100 CS 100
200 CS 200
300
CS 300 Alice Bob Charlie Charlie CS 100 CS 200 CS 300

While Course c ∈ C is free CS100 Alice


CS 300
Select Highest Preference a ∈ TA of c to whom c has
not yet proposed CS200 Bob
Alice
If a is free then assign
Already assigned CS300 Charlie

Else if a prefers c’ to c then c remains free

59
Stable Matching – Moving Towards Algorithm Design 3
1st 2nd 3rd 1st 2nd 3rd
CS 100 Alice Bob Charlie Alice CS 200 CS 300
100 CS 100
300
CS 200 Bob Charlie
Alice Alice
Charlie Bob CS 300
100 CS 100
200 CS 200
300
CS 300 Alice Bob Charlie Charlie CS 100 CS 200 CS 300

While Course c ∈ C is free CS100 Alice


CS 300
Select Highest Preference a ∈ TA of c to whom c has
not yet proposed CS200 Bob
Alice
If a is free then assign
Already assigned CS300 Charlie

Else if a prefers c’ to c then c remains free


Else if a prefers c to c’ then assign a to c and c’ gets free
60
Stable Matching – Moving Towards Algorithm Design 3
1st 2nd 3rd 1st 2nd 3rd
CS 100 Alice Bob Charlie Alice CS 200 CS 300
100 CS 100
300
CS 200 Bob Charlie
Alice Alice
Charlie Bob CS 300
100 CS 100
200 CS 200
300
CS 300 Alice Bob Charlie Charlie CS 100 CS 200 CS 300

While Course c ∈ C is free CS100 Alice


CS 300
Select Highest Preference a ∈ TA of c to whom c has
not yet proposed CS200 Bob
Alice
If a is free then assign
Already assigned CS300 Charlie

Else if a prefers c’ to c then c remains free


Else if a prefers c to c’ then assign a to c and c’ gets free
61
Stable Matching – Moving Towards Algorithm Design 4
1st 2nd 3rd 1st 2nd 3rd
CS 100 Alice Bob Charlie Alice CS 200 CS 300
100 CS 100
300
CS 200 Bob Charlie
Alice Alice
Charlie Bob CS 300
100 CS 100
200 CS 200
300
CS 300 Alice Bob Charlie Charlie CS 100 CS 200 CS 300

While Course c ∈ C is free CS100 Alice


CS 100
Select Highest Preference a ∈ TA of c to whom c has
not yet proposed CS200 Bob

If a is free then assign


CS300 Charlie

Else if a prefers c’ to c then c remains free


Else if a prefers c to c’ then assign a to c and c’ gets free
62
Stable Matching – Moving Towards Algorithm Design 4
1st 2nd 3rd 1st 2nd 3rd
CS 100 Alice Bob Charlie Alice CS 200 CS 300
100 CS 100
300
CS 200 Bob Charlie
Alice Alice
Charlie Bob CS 300
100 CS 100
200 CS 200
300
CS 300 Alice Bob Charlie Charlie CS 100 CS 200 CS 300

While Course c ∈ C is free CS100 Alice


CS 100
Select Highest Preference a ∈ TA of c to whom c has
not yet proposed CS200 Bob
Bob
If a is free then assign
CS300 Charlie

Else if a prefers c’ to c then c remains free


Else if a prefers c to c’ then assign a to c and c’ gets free
63
Stable Matching – Moving Towards Algorithm Design 4
1st 2nd 3rd 1st 2nd 3rd
CS 100 Alice Bob Charlie Alice CS 200 CS 300
100 CS 100
300
CS 200 Bob Charlie
Alice Alice
Charlie Bob CS 300
100 CS 100
200 CS 200
300
CS 300 Alice Bob Charlie Charlie CS 100 CS 200 CS 300

While Course c ∈ C is free CS100 Alice


CS 100
Select Highest Preference a ∈ TA of c to whom c has
not yet proposed CS200 Bob
Bob
If a is free then assign
Already assigned CS300 Charlie

Else if a prefers c’ to c then c remains free


Else if a prefers c to c’ then assign a to c and c’ gets free
64
Stable Matching – Moving Towards Algorithm Design 4
1st 2nd 3rd 1st 2nd 3rd
CS 100 Alice Bob Charlie Alice CS 200 CS 300
100 CS 100
300
CS 200 Bob Charlie
Alice Alice
Charlie Bob CS 300
100 CS 100
200 CS 200
300
CS 300 Alice Bob Charlie Charlie CS 100 CS 200 CS 300

While Course c ∈ C is free CS100 Alice


CS 100
Select Highest Preference a ∈ TA of c to whom c has
not yet proposed CS200 Bob
Bob
If a is free then assign
Already assigned CS300 Charlie

Else if a prefers c’ to c then c remains free


Else if a prefers c to c’ then assign a to c and c’ gets free
65
Stable Matching – Moving Towards Algorithm Design 5
1st 2nd 3rd 1st 2nd 3rd
CS 100 Alice Bob Charlie Alice CS 200 CS 300
100 CS 100
300
CS 200 Bob Charlie
Alice Alice
Charlie Bob CS 300
100 CS 100
200 CS 200
300
CS 300 Alice Bob Charlie Charlie CS 100 CS 200 CS 300

While Course c ∈ C is free CS100 Alice


CS 200
Select Highest Preference a ∈ TA of c to whom c has
not yet proposed CS200 Bob

If a is free then assign


CS300 Charlie

Else if a prefers c’ to c then c remains free


Else if a prefers c to c’ then assign a to c and c’ gets free
66
Stable Matching – Moving Towards Algorithm Design 5
1st 2nd 3rd 1st 2nd 3rd
CS 100 Alice Bob Charlie Alice CS 200 CS 300
100 CS 100
300
CS 200 Bob Charlie
Alice Alice
Charlie Bob CS 300
100 CS 100
200 CS 200
300
CS 300 Alice Bob Charlie Charlie CS 100 CS 200 CS 300

While Course c ∈ C is free CS100 Alice


CS 200
Select Highest Preference a ∈ TA of c to whom c has
not yet proposed CS200 Bob
Alice
If a is free then assign
CS300 Charlie

Else if a prefers c’ to c then c remains free


Else if a prefers c to c’ then assign a to c and c’ gets free
67
Stable Matching – Moving Towards Algorithm Design 5
1st 2nd 3rd 1st 2nd 3rd
CS 100 Alice Bob Charlie Alice CS 200 CS 300
100 CS 100
300
CS 200 Bob Charlie
Alice Alice
Charlie Bob CS 300
100 CS 100
200 CS 200
300
CS 300 Alice Bob Charlie Charlie CS 100 CS 200 CS 300

While Course c ∈ C is free CS100 Alice


CS 200
Select Highest Preference a ∈ TA of c to whom c has
not yet proposed CS200 Bob
Alice
If a is free then assign
Already Assigned CS300 Charlie

Else if a prefers c’ to c then c remains free


Else if a prefers c to c’ then assign a to c and c’ gets free
68
Stable Matching – Moving Towards Algorithm Design 5
1st 2nd 3rd 1st 2nd 3rd
CS 100 Alice Bob Charlie Alice CS 200 CS 300
100 CS 100
300
CS 200 Bob Charlie
Alice Alice
Charlie Bob CS 300
100 CS 100
200 CS 200
300
CS 300 Alice Bob Charlie Charlie CS 100 CS 200 CS 300

While Course c ∈ C is free CS100 Alice


CS 200
Select Highest Preference a ∈ TA of c to whom c has
not yet proposed CS200 Bob
Alice
If a is free then assign
Already Assigned CS300 Charlie

Else if a prefers c’ to c then c remains free


Else if a prefers c to c’ then assign a to c and c’ gets free
69
Stable Matching – Moving Towards Algorithm Design 6
1st 2nd 3rd 1st 2nd 3rd
CS 100 Alice Bob Charlie Alice CS 200 CS 300
100 CS 100
300
CS 200 Bob Charlie
Alice Alice
Charlie Bob CS 300
100 CS 100
200 CS 200
300
CS 300 Alice Bob Charlie Charlie CS 100 CS 200 CS 300

While Course c ∈ C is free CS100 Alice


CS 300
Select Highest Preference a ∈ TA of c to whom c has
not yet proposed CS200 Bob

If a is free then assign


CS300 Charlie

Else if a prefers c’ to c then c remains free


Else if a prefers c to c’ then assign a to c and c’ gets free
70
Stable Matching – Moving Towards Algorithm Design 6
1st 2nd 3rd 1st 2nd 3rd
CS 100 Alice Bob Charlie Alice CS 200 CS 300
100 CS 100
300
CS 200 Bob Charlie
Alice Alice
Charlie Bob CS 300
100 CS 100
200 CS 200
300
CS 300 Alice Bob Charlie Charlie CS 100 CS 200 CS 300

While Course c ∈ C is free CS100 Alice


CS 300
Select Highest Preference a ∈ TA of c to whom c has
not yet proposed CS200 Bob
Bob
If a is free then assign
CS300 Charlie

Else if a prefers c’ to c then c remains free


Else if a prefers c to c’ then assign a to c and c’ gets free
71
Stable Matching – Moving Towards Algorithm Design 6
1st 2nd 3rd 1st 2nd 3rd
CS 100 Alice Bob Charlie Alice CS 200 CS 300
100 CS 100
300
CS 200 Bob Charlie
Alice Alice
Charlie Bob CS 300
100 CS 100
200 CS 200
300
CS 300 Alice Bob Charlie Charlie CS 100 CS 200 CS 300

While Course c ∈ C is free CS100 Alice


CS 300
Select Highest Preference a ∈ TA of c to whom c has
not yet proposed CS200 Bob
Bob
If a is free then assign
Already Assigned CS300 Charlie

Else if a prefers c’ to c then c remains free


Else if a prefers c to c’ then assign a to c and c’ gets free
72
Stable Matching – Moving Towards Algorithm Design 6
1st 2nd 3rd 1st 2nd 3rd
CS 100 Alice Bob Charlie Alice CS 200 CS 300
100 CS 100
300
CS 200 Bob Charlie
Alice Alice
Charlie Bob CS 300
100 CS 100
200 CS 200
300
CS 300 Alice Bob Charlie Charlie CS 100 CS 200 CS 300

While Course c ∈ C is free CS100 Alice


CS 300
Select Highest Preference a ∈ TA of c to whom c has
not yet proposed CS200 Bob
Bob
If a is free then assign
Already Assigned CS300 Charlie

Else if a prefers c’ to c then c remains free


Else if a prefers c to c’ then assign a to c and c’ gets free
73
Stable Matching – Moving Towards Algorithm Design 7
1st 2nd 3rd 1st 2nd 3rd
CS 100 Alice Bob Charlie Alice CS 200 CS 300
100 CS 100
300
CS 200 Bob Charlie
Alice Alice
Charlie Bob CS 300
100 CS 100
200 CS 200
300
CS 300 Alice Bob Charlie Charlie CS 100 CS 200 CS 300

While Course c ∈ C is free CS100 Alice


CS 100
Select Highest Preference a ∈ TA of c to whom c has
not yet proposed CS200 Bob

If a is free then assign


CS300 Charlie

Else if a prefers c’ to c then c remains free


Else if a prefers c to c’ then assign a to c and c’ gets free
74
Stable Matching – Moving Towards Algorithm Design 7
1st 2nd 3rd 1st 2nd 3rd
CS 100 Alice Bob Charlie Alice CS 200 CS 300
100 CS 100
300
CS 200 Bob Charlie
Alice Alice
Charlie Bob CS 300
100 CS 100
200 CS 200
300
CS 300 Alice Bob Charlie Charlie CS 100 CS 200 CS 300

While Course c ∈ C is free CS100 Alice


CS 100
Select Highest Preference a ∈ TA of c to whom c has
not yet proposed CS200 Bob
Charlie
If a is free then assign
CS300 Charlie

Else if a prefers c’ to c then c remains free


Else if a prefers c to c’ then assign a to c and c’ gets free
75
Stable Matching – Moving Towards Algorithm Design 7
1st 2nd 3rd 1st 2nd 3rd
CS 100 Alice Bob Charlie Alice CS 200 CS 300
100 CS 100
300
CS 200 Bob Charlie
Alice Alice
Charlie Bob CS 300
100 CS 100
200 CS 200
300
CS 300 Alice Bob Charlie Charlie CS 100 CS 200 CS 300

While Course c ∈ C is free CS100 Alice


CS 100
Select Highest Preference a ∈ TA of c to whom c has
not yet proposed CS200 Bob
Charlie
If a is free then assign
(CS 100 - Charlie) CS300 Charlie

Else if a prefers c’ to c then c remains free


Else if a prefers c to c’ then assign a to c and c’ gets free
76
Stable Matching – Moving Towards Algorithm Design 7
1st 2nd 3rd 1st 2nd 3rd
CS 100 Alice Bob Charlie Alice CS 200 CS 300
100 CS 100
300
CS 200 Bob Charlie
Alice Alice
Charlie Bob CS 300
100 CS 100
200 CS 200
300
CS 300 Alice Bob Charlie Charlie CS 100 CS 200 CS 300

While Course c ∈ C is free CS100 Alice


CS 100
Select Highest Preference a ∈ TA of c to whom c has
not yet proposed CS200 Bob
Charlie
If a is free then assign
(CS 100 - Charlie) CS300 Charlie
Observation 2. Once a TA applicant is assigned to a course (matched), the applicant never
becomes unassigned
Else if a prefers (unmatched);
c’ to c then c remains free only “trades up”
Else if a prefers c to c’ then assign a to c and c’ gets free
77
Stable Matching – Moving Towards Algorithm Design 7
1st 2nd 3rd 1st 2nd 3rd
CS 100 Alice Bob Charlie Alice CS 200 CS 300
100 CS 100
300
CS 200 Bob Charlie
Alice Alice
Charlie Bob CS 300
100 CS 100
200 CS 200
300
CS 300 Alice Bob Charlie Charlie CS 100 CS 200 CS 300

While Course c ∈ C is free CS100 Alice


CS 100
Select Highest Preference a ∈ TA of c to whom c has
not yet proposed CS200 Bob
Charlie
If a is free then assign
(CS 100 - Charlie) CS300 Charlie

Else if a prefers c’ to c then c remains free No unstable pair


Else if a prefers c to c’ then assign a to c and c’ gets free
78
Gale-Shapely deferred acceptance algorithm

GALE-SHAPELY (preference lists of course instructors and TA applicants)

1 INITIALIZE 𝑀 to empty matching


2 While Course c ∈ C is free
3 𝑎 ← Select Highest Preference a ∈ TA of c to whom c has not yet offered
4 If a is free then assign
5 Add 𝑐 − 𝑎 to matching 𝑀
6 Else if a prefers c to c’ then assign a to c and c’ gets free
7 Replace 𝑐 ′ − 𝑎 with 𝑐 − 𝑎 in matching 𝑀
8 Else if a prefers c’ to c then c remains free
9 𝑎 rejects offer made by 𝑐

11 RETURN stable matching M

79
Gale-Shapely deferred acceptance algorithm

GALE-SHAPELY (preference lists of course instructors and TA applicants)

1 INITIALIZE 𝑀 to empty matching


2 WHILE ( Course c ∈ C is free and has not offered to every applicant )
3 𝑎 ← Select Highest Preference a ∈ TA of c to whom c has not yet offered
4 IF (𝒂 has not received TAship offer before) Lloyd Shapley

5 Add 𝑐 − 𝑎 to matching 𝑀 In 2012, Nobel Memorial


Prize in Economic Sciences
6 ELSE IF (𝒂 𝐩𝐫𝐞𝐟𝐞𝐫𝐬 𝒄 𝐭𝐨 a previously offered course by instructor 𝒄′) was awarded to Lloyd S.
7 Replace 𝑐 ′ − 𝑎 with 𝑐 − 𝑎 in matching 𝑀 Shapley "for the theory of
stable allocations and the
8 ELSE practice of market design”.
9 𝑎 rejects offer made by 𝑐

11 RETURN stable matching M

80

You might also like