72
15-251: Great Theoretical Ideas in Computer Science Stable Matchings + P vs NP Lecture 13

Stable Matchings P vs NP - Carnegie Mellon School of ...aada/courses/15251s16/www/slides/15251-s16-lecture...The Traditional Marriage Algorithm For each day that some boy gets a “No”

Embed Size (px)

Citation preview

Page 1: Stable Matchings P vs NP - Carnegie Mellon School of ...aada/courses/15251s16/www/slides/15251-s16-lecture...The Traditional Marriage Algorithm For each day that some boy gets a “No”

15-251: Great Theoretical Ideas in Computer Science

Stable Matchings +

P vs NP

Lecture 13

Page 2: Stable Matchings P vs NP - Carnegie Mellon School of ...aada/courses/15251s16/www/slides/15251-s16-lecture...The Traditional Marriage Algorithm For each day that some boy gets a “No”

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

Page 3: Stable Matchings P vs NP - Carnegie Mellon School of ...aada/courses/15251s16/www/slides/15251-s16-lecture...The Traditional Marriage Algorithm For each day that some boy gets a “No”

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

Page 4: Stable Matchings P vs NP - Carnegie Mellon School of ...aada/courses/15251s16/www/slides/15251-s16-lecture...The Traditional Marriage Algorithm For each day that some boy gets a “No”

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

Page 5: Stable Matchings P vs NP - Carnegie Mellon School of ...aada/courses/15251s16/www/slides/15251-s16-lecture...The Traditional Marriage Algorithm For each day that some boy gets a “No”

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

Page 6: Stable Matchings P vs NP - Carnegie Mellon School of ...aada/courses/15251s16/www/slides/15251-s16-lecture...The Traditional Marriage Algorithm For each day that some boy gets a “No”

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”

Page 7: Stable Matchings P vs NP - Carnegie Mellon School of ...aada/courses/15251s16/www/slides/15251-s16-lecture...The Traditional Marriage Algorithm For each day that some boy gets a “No”

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

Page 8: Stable Matchings P vs NP - Carnegie Mellon School of ...aada/courses/15251s16/www/slides/15251-s16-lecture...The Traditional Marriage Algorithm For each day that some boy gets a “No”

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

Page 9: Stable Matchings P vs NP - Carnegie Mellon School of ...aada/courses/15251s16/www/slides/15251-s16-lecture...The Traditional Marriage Algorithm For each day that some boy gets a “No”

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

Page 10: Stable Matchings P vs NP - Carnegie Mellon School of ...aada/courses/15251s16/www/slides/15251-s16-lecture...The Traditional Marriage Algorithm For each day that some boy gets a “No”

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

Page 11: Stable Matchings P vs NP - Carnegie Mellon School of ...aada/courses/15251s16/www/slides/15251-s16-lecture...The Traditional Marriage Algorithm For each day that some boy gets a “No”

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

Page 12: Stable Matchings P vs NP - Carnegie Mellon School of ...aada/courses/15251s16/www/slides/15251-s16-lecture...The Traditional Marriage Algorithm For each day that some boy gets a “No”

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

Page 13: Stable Matchings P vs NP - Carnegie Mellon School of ...aada/courses/15251s16/www/slides/15251-s16-lecture...The Traditional Marriage Algorithm For each day that some boy gets a “No”

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

Page 14: Stable Matchings P vs NP - Carnegie Mellon School of ...aada/courses/15251s16/www/slides/15251-s16-lecture...The Traditional Marriage Algorithm For each day that some boy gets a “No”

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

Page 15: Stable Matchings P vs NP - Carnegie Mellon School of ...aada/courses/15251s16/www/slides/15251-s16-lecture...The Traditional Marriage Algorithm For each day that some boy gets a “No”

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

Page 16: Stable Matchings P vs NP - Carnegie Mellon School of ...aada/courses/15251s16/www/slides/15251-s16-lecture...The Traditional Marriage Algorithm For each day that some boy gets a “No”

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

Page 17: Stable Matchings P vs NP - Carnegie Mellon School of ...aada/courses/15251s16/www/slides/15251-s16-lecture...The Traditional Marriage Algorithm For each day that some boy gets a “No”

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

Page 18: Stable Matchings P vs NP - Carnegie Mellon School of ...aada/courses/15251s16/www/slides/15251-s16-lecture...The Traditional Marriage Algorithm For each day that some boy gets a “No”

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…

Page 19: Stable Matchings P vs NP - Carnegie Mellon School of ...aada/courses/15251s16/www/slides/15251-s16-lecture...The Traditional Marriage Algorithm For each day that some boy gets a “No”

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…

Page 20: Stable Matchings P vs NP - Carnegie Mellon School of ...aada/courses/15251s16/www/slides/15251-s16-lecture...The Traditional Marriage Algorithm For each day that some boy gets a “No”

Corollary: Each girl will marry her absolute favorite of the boys who visit her during the TMA

Page 21: Stable Matchings P vs NP - Carnegie Mellon School of ...aada/courses/15251s16/www/slides/15251-s16-lecture...The Traditional Marriage Algorithm For each day that some boy gets a “No”

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

Page 22: Stable Matchings P vs NP - Carnegie Mellon School of ...aada/courses/15251s16/www/slides/15251-s16-lecture...The Traditional Marriage Algorithm For each day that some boy gets a “No”

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

Page 23: Stable Matchings P vs NP - Carnegie Mellon School of ...aada/courses/15251s16/www/slides/15251-s16-lecture...The Traditional Marriage Algorithm For each day that some boy gets a “No”

Great! We know that TMA terminates and produces a matching

But is it stable?

Page 24: Stable Matchings P vs NP - Carnegie Mellon School of ...aada/courses/15251s16/www/slides/15251-s16-lecture...The Traditional Marriage Algorithm For each day that some boy gets a “No”

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.

Page 25: Stable Matchings P vs NP - Carnegie Mellon School of ...aada/courses/15251s16/www/slides/15251-s16-lecture...The Traditional Marriage Algorithm For each day that some boy gets a “No”

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.

Page 26: Stable Matchings P vs NP - Carnegie Mellon School of ...aada/courses/15251s16/www/slides/15251-s16-lecture...The Traditional Marriage Algorithm For each day that some boy gets a “No”

Opinion Poll

Page 27: Stable Matchings P vs NP - Carnegie Mellon School of ...aada/courses/15251s16/www/slides/15251-s16-lecture...The Traditional Marriage Algorithm For each day that some boy gets a “No”

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”

, ,

, ,

Page 28: Stable Matchings P vs NP - Carnegie Mellon School of ...aada/courses/15251s16/www/slides/15251-s16-lecture...The Traditional Marriage Algorithm For each day that some boy gets a “No”

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

Page 29: Stable Matchings P vs NP - Carnegie Mellon School of ...aada/courses/15251s16/www/slides/15251-s16-lecture...The Traditional Marriage Algorithm For each day that some boy gets a “No”

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

Page 30: Stable Matchings P vs NP - Carnegie Mellon School of ...aada/courses/15251s16/www/slides/15251-s16-lecture...The Traditional Marriage Algorithm For each day that some boy gets a “No”

, , , ,

, , , ,

, , , ,

Page 31: Stable Matchings P vs NP - Carnegie Mellon School of ...aada/courses/15251s16/www/slides/15251-s16-lecture...The Traditional Marriage Algorithm For each day that some boy gets a “No”

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?

Page 32: Stable Matchings P vs NP - Carnegie Mellon School of ...aada/courses/15251s16/www/slides/15251-s16-lecture...The Traditional Marriage Algorithm For each day that some boy gets a “No”

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…

Page 33: Stable Matchings P vs NP - Carnegie Mellon School of ...aada/courses/15251s16/www/slides/15251-s16-lecture...The Traditional Marriage Algorithm For each day that some boy gets a “No”

The Naked Mathematical Truth!

The Traditional Marriage Algorithm always produces

a male-optimal, female-pessimal pairing

Page 34: Stable Matchings P vs NP - Carnegie Mellon School of ...aada/courses/15251s16/www/slides/15251-s16-lecture...The Traditional Marriage Algorithm For each day that some boy gets a “No”

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

Page 35: Stable Matchings P vs NP - Carnegie Mellon School of ...aada/courses/15251s16/www/slides/15251-s16-lecture...The Traditional Marriage Algorithm For each day that some boy gets a “No”

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

Page 36: Stable Matchings P vs NP - Carnegie Mellon School of ...aada/courses/15251s16/www/slides/15251-s16-lecture...The Traditional Marriage Algorithm For each day that some boy gets a “No”

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!!!

Page 37: Stable Matchings P vs NP - Carnegie Mellon School of ...aada/courses/15251s16/www/slides/15251-s16-lecture...The Traditional Marriage Algorithm For each day that some boy gets a “No”

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

Page 38: Stable Matchings P vs NP - Carnegie Mellon School of ...aada/courses/15251s16/www/slides/15251-s16-lecture...The Traditional Marriage Algorithm For each day that some boy gets a “No”

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…

Page 39: Stable Matchings P vs NP - Carnegie Mellon School of ...aada/courses/15251s16/www/slides/15251-s16-lecture...The Traditional Marriage Algorithm For each day that some boy gets a “No”

TMA = Gale Shapley Alg.

David Gale Not Shapley

Page 40: Stable Matchings P vs NP - Carnegie Mellon School of ...aada/courses/15251s16/www/slides/15251-s16-lecture...The Traditional Marriage Algorithm For each day that some boy gets a “No”

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

Page 41: Stable Matchings P vs NP - Carnegie Mellon School of ...aada/courses/15251s16/www/slides/15251-s16-lecture...The Traditional Marriage Algorithm For each day that some boy gets a “No”

Theorem: The Gale Shapley Algorithm terminates after O(n2) iterations and produces a stable matching which is male-optimal and female-pessimal.

Page 42: Stable Matchings P vs NP - Carnegie Mellon School of ...aada/courses/15251s16/www/slides/15251-s16-lecture...The Traditional Marriage Algorithm For each day that some boy gets a “No”

P vs. NP

Page 43: Stable Matchings P vs NP - Carnegie Mellon School of ...aada/courses/15251s16/www/slides/15251-s16-lecture...The Traditional Marriage Algorithm For each day that some boy gets a “No”

$1,000,000

— the prize for solving any

of the Millennium Prize Problems

Page 44: Stable Matchings P vs NP - Carnegie Mellon School of ...aada/courses/15251s16/www/slides/15251-s16-lecture...The Traditional Marriage Algorithm For each day that some boy gets a “No”

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

Page 45: Stable Matchings P vs NP - Carnegie Mellon School of ...aada/courses/15251s16/www/slides/15251-s16-lecture...The Traditional Marriage Algorithm For each day that some boy gets a “No”

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.

Page 46: Stable Matchings P vs NP - Carnegie Mellon School of ...aada/courses/15251s16/www/slides/15251-s16-lecture...The Traditional Marriage Algorithm For each day that some boy gets a “No”

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.

Page 47: Stable Matchings P vs NP - Carnegie Mellon School of ...aada/courses/15251s16/www/slides/15251-s16-lecture...The Traditional Marriage Algorithm For each day that some boy gets a “No”

What is the P vs. NP problem?

Page 48: Stable Matchings P vs NP - Carnegie Mellon School of ...aada/courses/15251s16/www/slides/15251-s16-lecture...The Traditional Marriage Algorithm For each day that some boy gets a “No”

Sudoku

Page 49: Stable Matchings P vs NP - Carnegie Mellon School of ...aada/courses/15251s16/www/slides/15251-s16-lecture...The Traditional Marriage Algorithm For each day that some boy gets a “No”

Sudoku 3×3 × 3×3

Page 50: Stable Matchings P vs NP - Carnegie Mellon School of ...aada/courses/15251s16/www/slides/15251-s16-lecture...The Traditional Marriage Algorithm For each day that some boy gets a “No”

Sudoku 4×4 × 4×4

Page 51: Stable Matchings P vs NP - Carnegie Mellon School of ...aada/courses/15251s16/www/slides/15251-s16-lecture...The Traditional Marriage Algorithm For each day that some boy gets a “No”

Sudoku 4×4 × 4×4

Page 52: Stable Matchings P vs NP - Carnegie Mellon School of ...aada/courses/15251s16/www/slides/15251-s16-lecture...The Traditional Marriage Algorithm For each day that some boy gets a “No”

No-Promises Sudoku

5

This one has no solution.

4×4 × 4×4

Page 53: Stable Matchings P vs NP - Carnegie Mellon School of ...aada/courses/15251s16/www/slides/15251-s16-lecture...The Traditional Marriage Algorithm For each day that some boy gets a “No”

No-Promises Sudoku

5

This one has multiple solutions.

4×4 × 4×4

Page 54: Stable Matchings P vs NP - Carnegie Mellon School of ...aada/courses/15251s16/www/slides/15251-s16-lecture...The Traditional Marriage Algorithm For each day that some boy gets a “No”

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).

Page 55: Stable Matchings P vs NP - Carnegie Mellon School of ...aada/courses/15251s16/www/slides/15251-s16-lecture...The Traditional Marriage Algorithm For each day that some boy gets a “No”

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.

Page 56: Stable Matchings P vs NP - Carnegie Mellon School of ...aada/courses/15251s16/www/slides/15251-s16-lecture...The Traditional Marriage Algorithm For each day that some boy gets a “No”

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!

Page 57: Stable Matchings P vs NP - Carnegie Mellon School of ...aada/courses/15251s16/www/slides/15251-s16-lecture...The Traditional Marriage Algorithm For each day that some boy gets a “No”

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”

Page 58: Stable Matchings P vs NP - Carnegie Mellon School of ...aada/courses/15251s16/www/slides/15251-s16-lecture...The Traditional Marriage Algorithm For each day that some boy gets a “No”

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!

Page 59: Stable Matchings P vs NP - Carnegie Mellon School of ...aada/courses/15251s16/www/slides/15251-s16-lecture...The Traditional Marriage Algorithm For each day that some boy gets a “No”

Let’s develop these notions formally…

We’ll start by describing some

sample algorithmic problems.

Page 60: Stable Matchings P vs NP - Carnegie Mellon School of ...aada/courses/15251s16/www/slides/15251-s16-lecture...The Traditional Marriage Algorithm For each day that some boy gets a “No”

3-Coloring

Input: A graph

Task: Decide if there is a 3-coloring.

If so, find one.

Page 61: Stable Matchings P vs NP - Carnegie Mellon School of ...aada/courses/15251s16/www/slides/15251-s16-lecture...The Traditional Marriage Algorithm For each day that some boy gets a “No”

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

Page 62: Stable Matchings P vs NP - Carnegie Mellon School of ...aada/courses/15251s16/www/slides/15251-s16-lecture...The Traditional Marriage Algorithm For each day that some boy gets a “No”

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.

Page 63: Stable Matchings P vs NP - Carnegie Mellon School of ...aada/courses/15251s16/www/slides/15251-s16-lecture...The Traditional Marriage Algorithm For each day that some boy gets a “No”

Bipartite Perfect Matching

Input: A bipartite graph

Task: Decide if there is a perfect matching.

If so, find one.

Page 64: Stable Matchings P vs NP - Carnegie Mellon School of ...aada/courses/15251s16/www/slides/15251-s16-lecture...The Traditional Marriage Algorithm For each day that some boy gets a “No”

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

Page 65: Stable Matchings P vs NP - Carnegie Mellon School of ...aada/courses/15251s16/www/slides/15251-s16-lecture...The Traditional Marriage Algorithm For each day that some boy gets a “No”

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.)

Page 66: Stable Matchings P vs NP - Carnegie Mellon School of ...aada/courses/15251s16/www/slides/15251-s16-lecture...The Traditional Marriage Algorithm For each day that some boy gets a “No”

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.

Page 67: Stable Matchings P vs NP - Carnegie Mellon School of ...aada/courses/15251s16/www/slides/15251-s16-lecture...The Traditional Marriage Algorithm For each day that some boy gets a “No”

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}

Page 68: Stable Matchings P vs NP - Carnegie Mellon School of ...aada/courses/15251s16/www/slides/15251-s16-lecture...The Traditional Marriage Algorithm For each day that some boy gets a “No”

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}

Page 69: Stable Matchings P vs NP - Carnegie Mellon School of ...aada/courses/15251s16/www/slides/15251-s16-lecture...The Traditional Marriage Algorithm For each day that some boy gets a “No”

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?”

Page 70: Stable Matchings P vs NP - Carnegie Mellon School of ...aada/courses/15251s16/www/slides/15251-s16-lecture...The Traditional Marriage Algorithm For each day that some boy gets a “No”

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.

Page 71: Stable Matchings P vs NP - Carnegie Mellon School of ...aada/courses/15251s16/www/slides/15251-s16-lecture...The Traditional Marriage Algorithm For each day that some boy gets a “No”

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.

=

Page 72: Stable Matchings P vs NP - Carnegie Mellon School of ...aada/courses/15251s16/www/slides/15251-s16-lecture...The Traditional Marriage Algorithm For each day that some boy gets a “No”

Definitions:

Decision/search problems

P

Algorithms and analysis:

Gale-Shapley Stable Matching Algorithm and Analysis

Study Guide