Upload
trinhdung
View
214
Download
2
Embed Size (px)
Citation preview
15-251: Great Theoretical Ideas in Computer Science
Stable Matchings +
P vs NP
Lecture 13
Question: How do we pair them?
Dating Scenario
There are n boys and n girls
Each girl has her own ranked preference list of all the boys
Each boy has his own ranked preference list of the girls
The lists have no ties
3,5,2,1,4
1
5,2,1,4,3
2
4,3,5,1,2
3
1,2,3,4,5
4
2,3,4,1,5
5
1
3,2,5,1,4
2
1,2,5,3,4
3
4,3,2,1,5
4
1,3,4,2,5
5
1,2,4,5,3
Rogue Couples
Suppose we match all the boys and girls
Now suppose that some boy and some girl prefer each other to the people to whom they are paired
They will be called a rogue couple
A pairing of boys and girls is called stable if it contains no rogue couples
Stable Pairings
3,2,1
1
2,1,3
2
3,1,2
3
1
3,2,1
2
1,2,3
3
3,2,1
The Traditional Marriage Algorithm
For each day that some boy gets a “No” do:
Morning
• Each girl stands on her balcony
• Each boy proposes to the best girl whom he has not yet crossed off
Afternoon (for girls with at least one suitor)
• To today’s best: “Maybe, return tomorrow”
• To any others: “No, I will never marry you”
Evening
• Any rejected boy crosses the girl off his list
If no boys get a “No”, each girl marries the boy to whom she just said “maybe”
3,5,2,1,
4
1
5,2,1,4,3
4,3,5,1,2
3
1,2,3,4,5
4
2,3,4,1,5
5
1
3,2,5,1,4
2
1,2,5,3,4
3
4,3,2,1,5
4
1,3,4,2,5
5
1,2,4,5,3
2
Traditional Marriage Algorithm Example
3,5,2,1,
4
1
5,2,1,4,3
4,3,5,1,2
3
1,2,3,4,5
4
2,3,4,1,5
5
1
3,2,5,1,4
2
1,2,5,3,4
3
4,3,2,1,5
4
1,3,4,2,5
5
1,2,4,5,3
2
Traditional Marriage Algorithm Example
3,5,2,1,
4
1
5,2,1,4,3
4,3,5,1,2
3
1,2,3,4,5
4
2,3,4,1,5
5
1
3,2,5,1,4
2
2,5,3,4
3
4,3,2,1,5
4
3,4,2,5
5
1,2,4,5,3
2
Traditional Marriage Algorithm Example
3,5,2,1,
4
1
5,2,1,4,3
4,3,5,1,2
3
1,2,3,4,5
4
2,3,4,1,5
5
1
3,2,5,1,4
2
2,5,3,4
3
4,3,2,1,5
4
3,4,2,5
5
1,2,4,5,3
2
Traditional Marriage Algorithm Example
3,5,2,1,
4
1
5,2,1,4,3
4,3,5,1,2
3
1,2,3,4,5
4
2,3,4,1,5
5
1
3,2,5,1,4
2
2,5,3,4
3
4,3,2,1,5
4
3,4,2,5
5
1,2,4,5,3
2
Traditional Marriage Algorithm Example
3,5,2,1,
4
1
5,2,1,4,3
4,3,5,1,2
3
1,2,3,4,5
4
2,3,4,1,5
5
1
2,5,1,4
2
2,5,3,4
3
4,3,2,1,5
4
3,4,2,5
5
1,2,4,5,3
2
Traditional Marriage Algorithm Example
3,5,2,1,
4
1
5,2,1,4,3
4,3,5,1,2
3
1,2,3,4,5
4
2,3,4,1,5
5
1
2,5,1,4
2
2,5,3,4
3
4,3,2,1,5
4
3,4,2,5
5
1,2,4,5,3
2
Traditional Marriage Algorithm Example
3,5,2,1,
4
1
5,2,1,4,3
4,3,5,1,2
3
1,2,3,4,5
4
2,3,4,1,5
5
1
2,5,1,4
2
2,5,3,4
3
4,3,2,1,5
4
3,4,2,5
5
1,2,4,5,3
2
Traditional Marriage Algorithm Example
3,5,2,1,
4
1
5,2,1,4,3
4,3,5,1,2
3
1,2,3,4,5
4
2,3,4,1,5
5
1
5,1,4
2
2,5,3,4
3
4,3,2,1,5
4
3,4,2,5
5
1,2,4,5,3
2
Traditional Marriage Algorithm Example
3,5,2,1,
4
1
5,2,1,4,3
4,3,5,1,2
3
1,2,3,4,5
4
2,3,4,1,5
5
1
5,1,4
2
2,5,3,4
3
4,3,2,1,5
4
3,4,2,5
5
1,2,4,5,3
2
Traditional Marriage Algorithm Example
Does TMA Always Terminate?
It might encounter a situation where the algorithm does not specify what to do next.
It might keep on going for an infinite number of days
Does TMA Always Terminate?
It might encounter a situation where the algorithm does not specify what to do next
It might keep on going for an infinite number of days
cannot happen, every day some name is crossed off
this could happen if some boy crosses all names off his list!
we’ll show it doesn’t…
Improvement Lemma: If a girl has a boy on a string, then she will always have someone at least as good on a string (or for a husband)
She would only let go of him in order to “maybe” someone better
She would only let go of that guy for someone even better
She would only let go of that guy for someone even better
AND SO ON…
Corollary: Each girl will marry her absolute favorite of the boys who visit her during the TMA
Contradiction
Lemma: No boy can be rejected by all the girls
Proof (by contradiction):
Suppose boy b is rejected by all the girls
At that point:
Each girl must have a suitor other than b
(By Improvement Lemma, once a girl has a suitor she will always have at least one)
The n girls have n suitors, and b is not among them. Thus, there are at least n+1 boys
Theorem: The TMA always terminates in at most n2 days
A “master list” of all n of the boys lists starts with a total of n x n = n2 girls on it
Each day that at least one boy gets a “No”, so at least one girl gets crossed off the master list
Therefore, the number of days is bounded by the original size of the master list
Great! We know that TMA terminates and produces a matching
But is it stable?
g b
g*
I rejected you when you came to my balcony.
Now I’ve got someone better
Theorem: The matching T produced by TMA is stable.
Let b and g be any couple in T, and b prefers g* to g. We claim that g* prefers her husband to b.
During TMA, b proposed to g* before he proposed to g. Hence, at some point g* rejected b for someone
she preferred to b. By the Improvement lemma, the person she married
was also preferable to b. Thus, every boy will be rejected by any girl
he prefers to his wife. T is stable.
Opinion Poll
Forget TMA for a Moment…
How should we define “the optimal girl for boy b”?
Flawed Attempt: “The girl at the top of b’s list”
, ,
, ,
She is the best girl he can conceivably get in a stable world. Presumably, she might
be better than the girl he gets in the stable pairing output by TMA
The Optimal Girl
A boy’s optimal girl is the highest ranked girl for whom there is some stable pairing
in which the boy gets her
A boy’s pessimal girl is the lowest ranked girl for whom there is some stable pairing
in which the boy gets her
The Pessimal Girl
She is the worst girl he can conceivably get in a stable world
, , , ,
, , , ,
, , , ,
Dating Heaven and Hell A pairing is male-optimal if every boy gets
his optimal mate. This is the best of all possible worlds for every boy
simultaneously
A pairing is male-pessimal if every boy gets his pessimal mate. This is the worst of
all possible worlds for every boy simultaneously
BTW, is a male-optimal pairing stable? How about a male-pessimal one?
Dating Heaven and Hell A pairing is female-optimal if every girl gets
her optimal mate. This is the best of all possible stable worlds for every girl
simultaneously
A pairing is female-pessimal if every girl gets her pessimal mate. This is the worst of
all possible stable worlds for every girl simultaneously
not clear if either exists!
we’ll show that both exist…
The Naked Mathematical Truth!
The Traditional Marriage Algorithm always produces
a male-optimal, female-pessimal pairing
Theorem: TMA produces a male-optimal pairing
Suppose, for a contradiction, that some boy gets rejected by his optimal girl during TMA
At time t, boy b got rejected by his optimal girl g because she said “maybe” to a preferred b*
Therefore, b* likes g at least as much as his optimal
Let t be the earliest time at which this happened
By the definition of t, b* had not yet been rejected by his optimal girl
Some boy b got rejected by his optimal girl g because she said “maybe” to a preferred b*. b* likes g at least as much as his optimal girl
Also, there must exist a stable pairing S in which b and g are married (by def. of “optimal”)
b* wants g more than his wife in S:
g wants b* more than her husband in S:
Contradiction
g is at least as good as his optimal and he is not matched to g in stable pairing S
b is her husband in S and she rejects him for b* in TMA
g
Suppose TMA
is not male-optimal
b
b*
g b
b* g’
likes b*
more than b
likes g*
at least
as much as
his optimal
(not been
rejected by
optimal yet)
consider the
first moment
in TMA when
some boy
is rejected by
his optimal girl
since g is b’s optimal,
there is a stable matching S
where g and b are matched
g’ at most
as good as
his optimal
likes b*
more than b
contradicts
stability of S!!!
Theorem: The TMA pairing, T, is female-pessimal
We know it is male-optimal. Suppose there is a stable pairing S where some girl g does worse than in T
Let b be her mate in T
Let b* be her mate in S
By assumption, g likes b better than her mate b* in S
b likes g better than his mate in S (we already know that g is his optimal girl)
Therefore, S is not stable Contradiction
Theorem: The TMA pairing, T, is female-pessimal
suppose not
T
g
…and a stable matching S
that matches g-b*,
where b* is worse for g
b g
b*
b
g is his
optimal girl
prefers b
to b*
contradicts
stability of S!!!
(boy-optimal)
there exists
a girl g…
TMA = Gale Shapley Alg.
David Gale Not Shapley
Gale Shapley and Roth
David Gale Alvin E. Roth
Gale and Roth won the 2012 Nobel Price in Economics for applying stable matchings to assigning medical students to hospitals
not Lloyd Shapley
Theorem: The Gale Shapley Algorithm terminates after O(n2) iterations and produces a stable matching which is male-optimal and female-pessimal.
P vs. NP
$1,000,000
— the prize for solving any
of the Millennium Prize Problems
Millennium Prize Problems
1. Birch and Swinnerton-Dyer Conjecture
2. Hodge Conjecture
3. Existence & smoothness for Navier–Stokes
4. Poincaré Conjecture
5. P vs. NP
6. Riemann Hypothesis
7. Yang–Mills existence and mass gap
Millennium Prize Problems
1. Birch and Swinnerton-Dyer Conjecture
2. Hodge Conjecture
3. Existence & smoothness for Navier–Stokes
4. Poincaré Conjecture
5. P vs. NP
6. Riemann Hypothesis
7. Yang–Mills existence and mass gap
The only one with
philosophical &
metamathematical
implications.
Millennium Prize Problems
1. Birch and Swinnerton-Dyer Conjecture
2. Hodge Conjecture
3. Existence & smoothness for Navier–Stokes
4. Poincaré Conjecture
5. P vs. NP
6. Riemann Hypothesis
7. Yang–Mills existence and mass gap
Solved in 2003
by Grisha Perelman.
What is the P vs. NP problem?
Sudoku
Sudoku 3×3 × 3×3
Sudoku 4×4 × 4×4
Sudoku 4×4 × 4×4
No-Promises Sudoku
5
This one has no solution.
4×4 × 4×4
No-Promises Sudoku
5
This one has multiple solutions.
4×4 × 4×4
No-Promises Sudoku n×n × n×n
Given a partially filled n×n×n×n Sudoku grid,
output YES or NO: can it be validly completed?
Naive decision algorithm:
For each empty cell (≤ n4), try each possible digit.
Check if that’s a valid solution. Overall time ≈ nn4.
Smart decision algorithm: ???
Verifying a proposed solution: Time O(n4).
No-Promises Sudoku n×n × n×n
Naive decision algorithm: Time ≈ nn4.
Verifying a proposed solution: Time O(n4).
For n = 100 (meaning 10,000×10,000 grids):
Verifying a solution: ≈ 100M steps.
Your cell phone can do this in 1 second.
Naive algorithm: a number with ≈ 200M digits.
Insanely larger than # of quarks in the universe.
No-Promises Sudoku n×n × n×n
Question:
Is there a fixed constant c and an algorithm A
such that A solves the decision problem in
time O(nc)?
This is equivalent to
the P vs. NP problem!
Is this famous $1,000,000 problem
really about Sudoku?? Yes and no.
Here’s how P vs. NP is usually (informally) stated:
Let L be an algorithmic task.
Suppose there is an efficient algorithm
for verifying solutions to L.
Is there always also an efficient algorithm
for finding solutions to L?
“L∈NP”
“L∈P”
Isn’t Sudoku just one particular instance
of this question?
Let L be an algorithmic task.
Suppose there is an efficient algorithm
for verifying solutions to L.
Is there always also an efficient algorithm
for finding solutions to L?
“L∈NP”
“L∈P”
We’ll see: It’s true for all problems
if and only if it’s true for Sudoku!
Let’s develop these notions formally…
We’ll start by describing some
sample algorithmic problems.
3-Coloring
Input: A graph
Task: Decide if there is a 3-coloring.
If so, find one.
Circuit-Sat
Input: A boolean circuit C
Task: Decide if there is a 0/1
setting to the input
wires which “satisfies” C
(makes output wire 1).
If so, find such a setting.
AND OR
NOT AND
OR
x1 x2 x3
1 0 0
Hamiltonian Cycle
Input: A graph
Task: Decide if there is a Hamiltonian Cycle
in it, meaning a cycle that visits each
vertex exactly once. If so, find one.
Bipartite Perfect Matching
Input: A bipartite graph
Task: Decide if there is a perfect matching.
If so, find one.
Input: A partially filled
n2×n2 Sudoku grid
Task: Decide if there is a valid Sudoko
completion. If so, find one.
(No-Promises) Sudoku n×n × n×n
Decision vs. Search
Each of these problems was of the form,
“Does a solution exist? If so, find one.”
Decision problem Search problem
For simplicity, we focus on decision problems.
(Given a decision algorithm, it’s usually
easy to use it to solve the search problem.)
Reducing search to decision
AND OR
NOT AND
OR
x1 x2 x3
Example: Circuit-Sat
Suppose you have a good
decision alg. for Circuit-Sat.
How can you get a good alg.
for solving the search problem?
Hint:
Try fixing x1 to 0, fixing x1 to 1,
and running the decision alg. in both cases.
Decision problems as languages
Given G, does it have
a Hamiltonian cycle?
Given G, does
it have a perfect matching?
Given circuit C, does it have
a “satisfying” input string?
Given graph G, is it
3-colorable?
Given partially filled Sudoku
grid S, can it be completed?
Decision problems Languages in {0,1}*
HAM-CYCLE = {⟨G⟩ : G contains a
Hamiltonian cycle}
PMATCH = {⟨G⟩ : G has a
perfect matching}
CIRCUIT-SAT = {⟨C⟩ : C has a
satisfying input}
3-COL = {⟨G⟩ : G is 3-colorable}
SUDOKU = {⟨S⟩ : S can be validly
completed}
Given TM M and input x,
does M(x) halt? HALTS = {⟨M,x⟩ : M(x) halts}
Decision problems as languages
Languages in {0,1}*
PMATCH = {⟨G⟩ : G has a
perfect matching}
CIRCUIT-SAT = {⟨C⟩ : C has a
satisfying input}
3-COL = {⟨G⟩ : G is 3-colorable}
SUDOKU = {⟨S⟩ : S can be validly
completed}
HALTS = {⟨M,x⟩ : M(x) halts}
No Turing Machine
(or Java algorithm)
can decide this one.
Is there a TM
(or Java algorithm)
which decides
the others?
Of course!
HAM-CYCLE = {⟨G⟩ : G contains a
Hamiltonian cycle}
Efficiency
HAM, PMATCH, CIRCUIT-SAT, 3-COL, SUDOKU
can all be decided by “trying all possibilities”.
E.g., there is a naive algorithm for deciding
3-COL which runs in ≈ 3|V|
time.
We care about more than just
“Is there an algorithm?”
We care about
“Is there a reasonably ‘efficient’ algorithm?”
Polynomial time
50 years of computer science experience
shows it’s a very compelling definition:
• It’s independent of the “machine model”:
poly-time on a TM = poly-time on a RAM
= poly-time in Java = poly-time in Python
• It’s “robust”: plug a poly-time subroutine
into a poly-time algorithm: still poly-time.
• Empirically, it seems that most natural
problems with poly-time algorithms also
have efficient-in-practice algorithms.
Polynomial time
P
The set of all languages L such that
there is a fixed constant c
and an algorithm (TM) A
such that A decides L and
A runs in time O(|x|c) on all inputs x.
=
Definitions:
Decision/search problems
P
Algorithms and analysis:
Gale-Shapley Stable Matching Algorithm and Analysis
Study Guide