The Class NP

NP-completeness

CS 133 : Automata Theory and Computability Lecture Slides1 Time Complexity Classes

Nestine Hope S. Hernandez Algorithms and Complexity Laboratory Department of Computer Science University of the Philippines, Diliman [email protected]

Day 24

1 Reference: Intro to the Theory of Computation (2nd Ed), Sipser

The Class P

The Class NP

P vs NP

The Class P

The Class NP

NP-completeness Polynomial Time Reducibility and NP-complete problems

NP-completeness

The Class P

The Class NP

P vs NP

The Class P

The Class NP

NP-completeness Polynomial Time Reducibility and NP-complete problems

NP-completeness

The Class P

The Class NP

NP-completeness

From here on we focus on aspects of time complexity theory that are unaected by polynomial dierences in running time. We consider such dierences to be insignicant and ignore them. The decision to disregard polynomial dierences doesn't imply that we consider such dierences unimportant. On the contrary, we certainly do consider the dierence between time

n

and time

n3

to be an important one.

But some questions that do not depend on polynomial dierences are important too.

The Class P

The Class NP

NP-completeness

Denition P

is the class of language that are decidable in polynomial time on a

deterministic single-tape TM.

P=

[ k

T IM E(nk )

The Class P

The Class NP

NP-completeness

Denition P

is the class of language that are decidable in polynomial time on a

deterministic single-tape TM.

P=

[

T IM E(nk )

k

1.

P

is invariant for all models of computation that are

polynomially equivalent to the deterministic single-tape TM, and 2.

P

roughly corresponds to the class of problems that are

realistically solvable on a computer.

The Class P

The Class NP

Examples of Problems in

P AT H = {< G, s, t > | G is a directed graph

NP-completeness

P

that has a directed path from

s

to

t.}

The Class P

The Class NP

Examples of Problems in

P AT H = {< G, s, t > | G is a directed graph

NP-completeness

P

that has a directed path from

P AT H ∈ P

s

to

t.}

The Class P

The Class NP

NP-completeness

Examples of Problems in

P AT H = {< G, s, t > | G is a directed graph

P

that has a directed path from

s

to

t.}

P AT H ∈ P M = On and t:

input

< G, s, t >

1. Place a mark on node

where

G

is a directed graph with nodes

s

s.

2. Repeat the until no addtl nodes are marked:

B

Scan all the edges of

marked node 3. If

t

a

G.

If an edge

to an unmarked node

(a, b) is found going b, mark node b.

is marked, accept. Otherwise, reject.

from a

The Class P

The Class NP

Examples of Problems in

RELP RIM E = {< x, y > | x

and

y

NP-completeness

P

are relatively prime.}

The Class P

The Class NP

Examples of Problems in

RELP RIM E = {< x, y > | x

and

y

NP-completeness

P

are relatively prime.}

RELP RIM E ∈ P

The Class P

The Class NP

NP-completeness

Examples of Problems in

RELP RIM E = {< x, y > | x

and

y

P

are relatively prime.}

RELP RIM E ∈ P E=

On input

< x, y >

y = 0: x ← x mod y . Exchange x and y .

1. Repeat until

B B

where

Assign

2. Output

x.

x

and

y

are natural numbers in binary:

The Class P

The Class NP

NP-completeness

Examples of Problems in

RELP RIM E = {< x, y > | x

and

y

P

are relatively prime.}

RELP RIM E ∈ P E=

< x, y >

On input

where

x

and

y

are natural numbers in binary:

x

and

y

are natural numbers in binary:

y = 0: x ← x mod y . Exchange x and y .

1. Repeat until

B B

Assign

2. Output

R=

x.

On input

1. Run E on

< x, y >

where

< x, y >.

2. If the result is 1, accept. Otherwise, reject.

The Class P

The Class NP

P vs NP

The Class P

The Class NP

NP-completeness Polynomial Time Reducibility and NP-complete problems

NP-completeness

The Class P

The Class NP

NP-completeness

There are problems, including many interesting and useful ones, wherein polynomial time algorithms that solve them aren't known to exist.

The Class P

The Class NP

NP-completeness

There are problems, including many interesting and useful ones, wherein polynomial time algorithms that solve them aren't known to exist. Why have we been unsuccessful in nding polynomial time algorithms for these problems?

The Class P

The Class NP

NP-completeness

There are problems, including many interesting and useful ones, wherein polynomial time algorithms that solve them aren't known to exist. Why have we been unsuccessful in nding polynomial time algorithms for these problems? We don't know.

The Class P

The Class NP

NP-completeness

There are problems, including many interesting and useful ones, wherein polynomial time algorithms that solve them aren't known to exist. Why have we been unsuccessful in nding polynomial time algorithms for these problems? We don't know. Perhaps these problems have, as yet undiscovered, polynomial time algorithms that rest on unknown principles.

The Class P

The Class NP

NP-completeness

There are problems, including many interesting and useful ones, wherein polynomial time algorithms that solve them aren't known to exist. Why have we been unsuccessful in nding polynomial time algorithms for these problems? We don't know. Perhaps these problems have, as yet undiscovered, polynomial time algorithms that rest on unknown principles. Or possibly some of these problems simply cannot be solved in polynomial time.

The Class P

The Class NP

HAM P AT H = {< G, s, t > | G is a directed graph with a

Hamiltonian path from

NP-completeness

s

to

t.}

The Class P

The Class NP

HAM P AT H = {< G, s, t > | G is a directed graph with a

NP-completeness

Hamiltonian path from

s

to

t.}

We can easily obtain an exponential time algorithm for this problem by modifying the algorithm for

P AT H .

We need only add a check to

verify that the potential path is Hamiltonian.

The Class P

The Class NP

HAM P AT H = {< G, s, t > | G is a directed graph with a

NP-completeness

Hamiltonian path from

s

to

t.}

We can easily obtain an exponential time algorithm for this problem by modifying the algorithm for

P AT H .

We need only add a check to

verify that the potential path is Hamiltonian. No one knows whether

HAM P AT H

is solvable in polynomial time.

The Class P

The Class NP

HAM P AT H = {< G, s, t > | G is a directed graph with a

NP-completeness

Hamiltonian path from

s

to

t.}

We can easily obtain an exponential time algorithm for this problem by modifying the algorithm for

P AT H .

We need only add a check to

verify that the potential path is Hamiltonian. No one knows whether

HAM P AT H

But the HAMPATH problem is

is solvable in polynomial time.

polynomial veriable.

The Class P

The Class NP

HAM P AT H = {< G, s, t > | G is a directed graph with a

NP-completeness

Hamiltonian path from

s

to

t.}

We can easily obtain an exponential time algorithm for this problem by modifying the algorithm for

P AT H .

We need only add a check to

verify that the potential path is Hamiltonian. No one knows whether

HAM P AT H

But the HAMPATH problem is

is solvable in polynomial time.

polynomial veriable.

That is, even though we don't know of a fast way to determine whether a graph contains a Hamiltonian path, if such a path were discovered somehow, we could easily convince someone else of its existence, simply by presenting it.

The Class P

The Class NP

NP-completeness

Denition A verier for a language A is an algorithm V, where

A = {w | V

accepts

< w, c >

for some string

c}. w,

so

a polynomial time verier runs in polynomial time in the length of

w.

We measure the time of a verier only in terms of the length of

A language A is polynomially veriable if it has a polynomial time verier.

The Class P

The Class NP

NP-completeness

Denition A verier for a language A is an algorithm V, where

A = {w | V

accepts

< w, c >

for some string

c}. w,

so

a polynomial time verier runs in polynomial time in the length of

w.

We measure the time of a verier only in terms of the length of

A language A is polynomially veriable if it has a polynomial time verier.

Denition NP

is the class of languages that have polynomial time veriers.

The Class P

Theorem

The Class NP

A language is in NP i it is decided by some nondeterministic polynomial time TM.

NP-completeness

The Class P

The Class NP

NP-completeness

Theorem

A language is in NP i it is decided by some nondeterministic polynomial time TM. The following nondeterministic TM decides the

HAM P AT H

problem

in nondeterministic polynomial time.

N1 = On and t:

input

< G, s, t >

1. Write a list of nodes in

G.

m

where

numbers

G

is a directed graph with nodes

p1 , · · · , pm ,

where

m

is the number of

Each number in the list is nondeterministically

selected to be between

1

and

m.

2. Check for repetitions in the list. If any are found, reject. 3. Check whether

s = p1

t = pm .

i between 1 and m − 1, G. If any are not, reject.

4. For each edge of

and

passed, so accept.

if either fail, reject.

check whether

s

(pi , pi+1 )

is an

Otherwise, all tests have been

The Class P

The Class NP

NP-completeness

Denition N T IM E(t(n)) = {L|L is a language

decided by a

Corollary NP =

S

k

N T IM E(nk )

O(t(n))

time nondeterministic TM}.

The Class P

The Class NP

NP-completeness

Examples of Problems in NP

CLIQU E = {< G, k > |G

is an undirected graph with a

k -clique}

The Class P

The Class NP

NP-completeness

Examples of Problems in NP

CLIQU E = {< G, k > |G

is an undirected graph with a

k -clique}

Proof:

V for CLIQU E . << G, k >, c >:

The following is a verier

V =

On input string

1. Test whether 2. Test whether

c is a set of k nodes in G. G contains all edges connecting

3. If both pass, accept. Otherwise, reject.

nodes in

c.

The Class P

The Class NP

NP-completeness

Examples of Problems in NP

CLIQU E = {< G, k > |G

is an undirected graph with a

k -clique}

Proof:

V for CLIQU E . << G, k >, c >:

The following is a verier

V =

On input string

1. Test whether 2. Test whether

c is a set of k nodes in G. G contains all edges connecting

nodes in

c.

3. If both pass, accept. Otherwise, reject.

Alternative Proof: If you prefer to think of NP in terms of nondeterministic polynomial time Turing machines, you may prove this by giving one that decides

CLIQU E .

The Class P

The Class NP

NP-completeness

Examples of Problems in NP

CLIQU E = {< G, k > |G

is an undirected graph with a

k -clique}

Proof:

V for CLIQU E . << G, k >, c >:

The following is a verier

V =

On input string

1. Test whether 2. Test whether

c is a set of k nodes in G. G contains all edges connecting

nodes in

c.

3. If both pass, accept. Otherwise, reject.

Alternative Proof: If you prefer to think of NP in terms of nondeterministic polynomial time Turing machines, you may prove this by giving one that decides

CLIQU E . N = On input

string

< G, k >,

where

G

1. Nondeterministically select a subset 2. Test whether

G

is a graph:

c

of

k

nodes of

G.

contains all edges connecting nodes in

3. If yes, accept. Otherwise, reject.

c.

The Class P

The Class NP

NP-completeness

Examples of Problems in NP

SU BSET SU M = {< S, t > |S = {x1P , · · · , xk } and {y1 , · · · , yl } ⊆ {x1 , · · · , xk }, we have yi = t}.

for some

The Class P

The Class NP

NP-completeness

Examples of Problems in NP

SU BSET SU M = {< S, t > |S = {x1P , · · · , xk } and {y1 , · · · , yl } ⊆ {x1 , · · · , xk }, we have yi = t}.

for some

Proof:

V for SU BSET SU M . << S, t >, c >:

The following is a verier

V =

On input string

1. Test whether 2. Test whether

c is a collection of numbers that S contains all the numbers in c.

3. If both pass, accept. Otherwise, reject.

sum to

t.

The Class P

The Class NP

NP-completeness

Examples of Problems in NP

SU BSET SU M = {< S, t > |S = {x1P , · · · , xk } and {y1 , · · · , yl } ⊆ {x1 , · · · , xk }, we have yi = t}.

for some

Proof:

V for SU BSET SU M . << S, t >, c >:

The following is a verier

V =

On input string

1. Test whether 2. Test whether

c is a collection of numbers that S contains all the numbers in c.

sum to

t.

3. If both pass, accept. Otherwise, reject.

Alternative Proof: A nondeterministic polynomial time Turing machine for

SU BSET SU M is as follows. N = On input string < S, t >: 1. Nondeterministically select a subset 2. Test whether

c

c

of the numbers in

is a collection of numbers that sum to

3. If yes, accept. Otherwise, reject.

t.

S.

The Class P

The Class NP

P = NP ?

NP-completeness

The Class P

•

The Class NP

NP-completeness

There are problems that are in NP but that are not known to be in P.

The Class P

The Class NP

NP-completeness

•

There are problems that are in NP but that are not known to be

•

Does that mean that polynomial veriability is more powerful

in P. than polynomial decidability?

The Class P

The Class NP

NP-completeness

•

There are problems that are in NP but that are not known to be

•

Does that mean that polynomial veriability is more powerful

•

It is quite possible that P and NP could be equal.

in P. than polynomial decidability?

The Class P

The Class NP

NP-completeness

•

There are problems that are in NP but that are not known to be

•

Does that mean that polynomial veriability is more powerful

•

It is quite possible that P and NP could be equal. No one has yet

in P. than polynomial decidability? proven the existence of a single language in NP that is not in P.

The Class P

The Class NP

NP-completeness

•

There are problems that are in NP but that are not known to be

•

Does that mean that polynomial veriability is more powerful

•

It is quite possible that P and NP could be equal. No one has yet

in P. than polynomial decidability? proven the existence of a single language in NP that is not in P.

•

The question of whether

P = NP

is one of the greatest unsolved

problems in theoretical computer science!

The Class P

The Class NP

NP-completeness

•

There are problems that are in NP but that are not known to be

•

Does that mean that polynomial veriability is more powerful

•

It is quite possible that P and NP could be equal. No one has yet

in P. than polynomial decidability? proven the existence of a single language in NP that is not in P.

•

The question of whether

P = NP

is one of the greatest unsolved

problems in theoretical computer science!

•

The best method for solving languages in NP deterministically uses exponential time.

The Class P

The Class NP

NP-completeness

•

There are problems that are in NP but that are not known to be

•

Does that mean that polynomial veriability is more powerful

•

It is quite possible that P and NP could be equal. No one has yet

in P. than polynomial decidability? proven the existence of a single language in NP that is not in P.

•

The question of whether

P = NP

is one of the greatest unsolved

problems in theoretical computer science!

•

The best method for solving languages in NP deterministically uses exponential time.

N P ⊆ EXP T IM E =

[ k

k T IM E 2n

The Class P

The Class NP

NP-completeness

•

There are problems that are in NP but that are not known to be

•

Does that mean that polynomial veriability is more powerful

•

It is quite possible that P and NP could be equal. No one has yet

in P. than polynomial decidability? proven the existence of a single language in NP that is not in P.

•

The question of whether

P = NP

is one of the greatest unsolved

problems in theoretical computer science!

•

The best method for solving languages in NP deterministically uses exponential time.

N P ⊆ EXP T IM E =

[

k T IM E 2n

k but it is not yet known whether NP is contained in a smaller deterministic time complexity class.

The Class P

The Class NP

NP-completeness

•

There are problems that are in NP but that are not known to be

•

Does that mean that polynomial veriability is more powerful

•

It is quite possible that P and NP could be equal. No one has yet

in P. than polynomial decidability? proven the existence of a single language in NP that is not in P.

•

The question of whether

P = NP

is one of the greatest unsolved

problems in theoretical computer science!

•

The best method for solving languages in NP deterministically uses exponential time.

N P ⊆ EXP T IM E =

[

k T IM E 2n

k but it is not yet known whether NP is contained in a smaller deterministic time complexity class.

•

Stephen Cook and Leovin Levin: They discovered certain problems in NP whose individual complexity is related to that of the entire class.

The Class P

The Class NP

NP-completeness

•

There are problems that are in NP but that are not known to be

•

Does that mean that polynomial veriability is more powerful

•

It is quite possible that P and NP could be equal. No one has yet

in P. than polynomial decidability? proven the existence of a single language in NP that is not in P.

•

The question of whether

P = NP

is one of the greatest unsolved

problems in theoretical computer science!

•

The best method for solving languages in NP deterministically uses exponential time.

N P ⊆ EXP T IM E =

[

k T IM E 2n

k but it is not yet known whether NP is contained in a smaller deterministic time complexity class.

•

Stephen Cook and Leovin Levin: They discovered certain problems in NP whose individual complexity is related to that of the entire class. If a polynomial time algorithm exists for any of these problems, all problems in NP would be polynomial time solvable.

The Class P

The Class NP

P vs NP

The Class P

The Class NP

NP-completeness Polynomial Time Reducibility and NP-complete problems

NP-completeness

The Class P

The Class NP

NP-completeness

NP-completeness The phenomenon of NP-completeness is important for both theoretical and practical reasons.

•

theoretical: A researcher attempting to prove that

P = NP

only

needs to nd a polynomial time algorithm for an NP-complete problem to achieve this goal.

•

practical: May prevent wasting time searching for a nonexistent polynomial time algorithm to solve a particular problem.

The Class P

The Class NP

NP-completeness

NP-completeness The phenomenon of NP-completeness is important for both theoretical and practical reasons.

•

theoretical: A researcher attempting to prove that

P = NP

only

needs to nd a polynomial time algorithm for an NP-complete problem to achieve this goal.

•

practical: May prevent wasting time searching for a nonexistent polynomial time algorithm to solve a particular problem.

Note: The author (Michael Sipser, Introduction to the Theory of Computation) believes that P 6= N P , so proving that a problem is NP-complete is evidence of its nonpolynomiality.

The Class P

The Class NP

NP-completeness

Polynomial Time Reducibility When problem A is eciently reducible to problem B, an ecient solution to B can be used to solve A eciently.

The Class P

The Class NP

NP-completeness

Polynomial Time Reducibility When problem A is eciently reducible to problem B, an ecient solution to B can be used to solve A eciently.

Denition A function

f : Σ∗ → Σ∗

is a polynomial time computable function if

some polynomial time Turing machine M exists that halts with just

f (w)

on its tape, when started on any input

w.

The Class P

The Class NP

NP-completeness

Polynomial Time Reducibility When problem A is eciently reducible to problem B, an ecient solution to B can be used to solve A eciently.

Denition A function

f : Σ∗ → Σ∗

is a polynomial time computable function if

some polynomial time Turing machine M exists that halts with just

f (w)

on its tape, when started on any input

w.

Denition Language

A

is polynomial time mapping reducible, or simply

polynomial time reducible, to language B, written polynomial time computable function every

w, w ∈ A ⇐⇒ f (w) ∈ B.

f : Σ∗ → Σ∗

A ≤P B ,

if a

exists, where for

The Class P

The Class NP

NP-completeness

Polynomial Time Reducibility When problem A is eciently reducible to problem B, an ecient solution to B can be used to solve A eciently.

Denition A function

f : Σ∗ → Σ∗

is a polynomial time computable function if

some polynomial time Turing machine M exists that halts with just

f (w)

on its tape, when started on any input

w.

Denition Language

A

is polynomial time mapping reducible, or simply

A ≤P B , if a f : Σ∗ → Σ∗ exists, where function f is called the

polynomial time reducible, to language B, written polynomial time computable function every

w, w ∈ A ⇐⇒ f (w) ∈ B.

The

polynomial time reduction of A to B.

for

The Class P

The Class NP

NP-completeness

Polynomial Time Reducibility When problem A is eciently reducible to problem B, an ecient solution to B can be used to solve A eciently.

Denition A function

f : Σ∗ → Σ∗

is a polynomial time computable function if

some polynomial time Turing machine M exists that halts with just

f (w)

on its tape, when started on any input

w.

Denition Language

A

is polynomial time mapping reducible, or simply

A ≤P B , if a f : Σ∗ → Σ∗ exists, where function f is called the

polynomial time reducible, to language B, written polynomial time computable function every

w, w ∈ A ⇐⇒ f (w) ∈ B.

The

for

polynomial time reduction of A to B.

If one language is polynomial time reducible to a language already known to have a polynomial time solution, we obtain a polynomial time solution to the original language.

The Class P

Theorem

The Class NP

If A ≤P B and B ∈ P , then A ∈ P .

NP-completeness

The Class P

The Class NP

NP-completeness

Theorem

If A ≤P B and B ∈ P , then A ∈ P .

Proof Let M be the polynomial time algorithm deciding B and

f

be the

polynomial time reduction from A to B. We describe a polynomial time algorithm N deciding A as follows:

The Class P

The Class NP

NP-completeness

Theorem

If A ≤P B and B ∈ P , then A ∈ P .

Proof Let M be the polynomial time algorithm deciding B and

f

be the

polynomial time reduction from A to B. We describe a polynomial time algorithm N deciding A as follows:

N=

On input

1. Compute

w:

f (w)

2. Run M on input

f (w)

and output whatever M outputs.

The Class P

The Class NP

NP-completeness

Theorem

If A ≤P B and B ∈ P , then A ∈ P .

Proof Let M be the polynomial time algorithm deciding B and

f

be the

polynomial time reduction from A to B. We describe a polynomial time algorithm N deciding A as follows:

N=

On input

1. Compute

w:

f (w)

2. Run M on input

• w∈A

whenever

f (w)

and output whatever M outputs.

f (w) ∈ B

because

f

is a reduction from A to B

The Class P

The Class NP

NP-completeness

Theorem

If A ≤P B and B ∈ P , then A ∈ P .

Proof Let M be the polynomial time algorithm deciding B and

f

be the

polynomial time reduction from A to B. We describe a polynomial time algorithm N deciding A as follows:

N=

On input

1. Compute

w:

f (w)

2. Run M on input

• w∈A • M

f (w)

and output whatever M outputs.

whenever

f (w) ∈ B

because

f (w)

whenever

w∈A

accepts

f

is a reduction from A to B

The Class P

The Class NP

NP-completeness

Theorem

If A ≤P B and B ∈ P , then A ∈ P .

Proof Let M be the polynomial time algorithm deciding B and

f

be the

polynomial time reduction from A to B. We describe a polynomial time algorithm N deciding A as follows:

N=

On input

1. Compute

w:

f (w) f (w)

2. Run M on input

• w∈A • M •

and output whatever M outputs.

whenever

f (w) ∈ B

because

f (w)

whenever

w∈A

accepts

f

is a reduction from A to B

N runs in polynomial time because each of its two stages runs in polynomial time.

The Class P

The Class NP

Denition A language B is NP-complete if it satises two conditions: 1. B is in NP, and 2. every A in NP is polynomial time reducible to B.

NP-completeness

The Class P

The Class NP

Questions? See you next meeting!

NP-completeness