Top Banner
CS 374: Algorithms & Models of Computation, Spring 2015 NP Completeness Lecture 23 November 19, 2015 Chandra & Lenny (UIUC) CS374 1 Spring 2015 1 / 37
65

CS 374: Algorithms & Models of Computation

Jan 19, 2022

Download

Documents

dariahiddleston
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: CS 374: Algorithms & Models of Computation

CS 374: Algorithms & Models of Computation,

Spring 2015

NP CompletenessLecture 23November 19, 2015

Chandra & Lenny (UIUC) CS374 1 Spring 2015 1 / 37

Page 2: CS 374: Algorithms & Models of Computation

Part I

NP-Completeness

Chandra & Lenny (UIUC) CS374 2 Spring 2015 2 / 37

Page 3: CS 374: Algorithms & Models of Computation

P and NP and Turing Machines

1 P: set of decision problems that have polynomial timealgorithms.

2 NP: set of decision problems that have polynomial timeverification algorithms.

Many natural problems we would like to solve are in NP.

Every problem in NP has an exponential time algorithm (tryverifying each possible certificate).

P ⊆ NPSo some problems in NP are in P (example, shortest pathproblem)

Big Question: Does every problem in NP have an efficientalgorithm? Same as asking whether P = NP.

Chandra & Lenny (UIUC) CS374 3 Spring 2015 3 / 37

Page 4: CS 374: Algorithms & Models of Computation

“Hardest” Problems

QuestionWhat is the hardest problem in NP? How do we define it?

Towards a definition1 Hardest problem must be in NP.

2 Hardest problem must be at least as “difficult” as every otherproblem in NP.

Chandra & Lenny (UIUC) CS374 4 Spring 2015 4 / 37

Page 5: CS 374: Algorithms & Models of Computation

NP-Complete Problems

DefinitionA problem X is said to be NP-Complete if

1 X ∈ NP, and

2 (Hardness) For any Y ∈ NP, Y ≤P X.

Recall reduction: Y ≤P X means that an instance of Y can beefficiently modeled as an instance of X .

Chandra & Lenny (UIUC) CS374 5 Spring 2015 5 / 37

Page 6: CS 374: Algorithms & Models of Computation

NP-Complete Problems

DefinitionA problem X is said to be NP-Complete if

1 X ∈ NP, and

2 (Hardness) For any Y ∈ NP, Y ≤P X.

Recall reduction: Y ≤P X means that an instance of Y can beefficiently modeled as an instance of X .

Chandra & Lenny (UIUC) CS374 5 Spring 2015 5 / 37

Page 7: CS 374: Algorithms & Models of Computation

Solving NP-Complete Problems

PropositionSuppose X is NP-Complete. Then X can be solved in polynomialtime if and only if P = NP.

Proof.⇒ Suppose X can be solved in polynomial time

1 Let Y ∈ NP. We know Y ≤P X.2 We showed that if Y ≤P X and X can be solved in polynomial

time, then Y can be solved in polynomial time.3 Thus, every problem Y ∈ NP is such that Y ∈ P; NP ⊆ P.4 Since P ⊆ NP, we have P = NP.

⇐ Since P = NP, and X ∈ NP, we have a polynomial timealgorithm for X .

Chandra & Lenny (UIUC) CS374 6 Spring 2015 6 / 37

Page 8: CS 374: Algorithms & Models of Computation

NP-Hard Problems

DefinitionA problem X is said to be NP-Hard if

1 (Hardness) For any Y ∈ NP, we have that Y ≤P X.

An NP-Hard problem need not be in NP!

Example: Halting problem is NP-Hard (why?) but notNP-Complete.

Chandra & Lenny (UIUC) CS374 7 Spring 2015 7 / 37

Page 9: CS 374: Algorithms & Models of Computation

Consequences of proving NP-Completeness

If X is NP-Complete

1 Since we believe P 6= NP,

2 and solving X implies P = NP.

X is unlikely to be efficiently solvable.

At the very least, many smart people before you have failed to findan efficient algorithm for X .(This is proof by mob opinion — take with a grain of salt.)

Chandra & Lenny (UIUC) CS374 8 Spring 2015 8 / 37

Page 10: CS 374: Algorithms & Models of Computation

Consequences of proving NP-Completeness

If X is NP-Complete

1 Since we believe P 6= NP,

2 and solving X implies P = NP.

X is unlikely to be efficiently solvable.

At the very least, many smart people before you have failed to findan efficient algorithm for X .(This is proof by mob opinion — take with a grain of salt.)

Chandra & Lenny (UIUC) CS374 8 Spring 2015 8 / 37

Page 11: CS 374: Algorithms & Models of Computation

Consequences of proving NP-Completeness

If X is NP-Complete

1 Since we believe P 6= NP,

2 and solving X implies P = NP.

X is unlikely to be efficiently solvable.

At the very least, many smart people before you have failed to findan efficient algorithm for X .

(This is proof by mob opinion — take with a grain of salt.)

Chandra & Lenny (UIUC) CS374 8 Spring 2015 8 / 37

Page 12: CS 374: Algorithms & Models of Computation

Consequences of proving NP-Completeness

If X is NP-Complete

1 Since we believe P 6= NP,

2 and solving X implies P = NP.

X is unlikely to be efficiently solvable.

At the very least, many smart people before you have failed to findan efficient algorithm for X .(This is proof by mob opinion — take with a grain of salt.)

Chandra & Lenny (UIUC) CS374 8 Spring 2015 8 / 37

Page 13: CS 374: Algorithms & Models of Computation

NP-Complete Problems

QuestionAre there any “natural” problems that are NP-Complete?

AnswerYes! Many, many important problems are NP-Complete.

Chandra & Lenny (UIUC) CS374 9 Spring 2015 9 / 37

Page 14: CS 374: Algorithms & Models of Computation

Cook-Levin Theorem

Theorem (Cook-Levin)

SAT is NP-Complete.

Need to show

1 SAT is in NP.

2 every NP problem X reduces to SAT.

Will see proof in next lecture.

Steve Cook won the Turing award for his theorem.

Chandra & Lenny (UIUC) CS374 10 Spring 2015 10 / 37

Page 15: CS 374: Algorithms & Models of Computation

Cook-Levin Theorem

Theorem (Cook-Levin)

SAT is NP-Complete.

Need to show

1 SAT is in NP.

2 every NP problem X reduces to SAT.

Will see proof in next lecture.

Steve Cook won the Turing award for his theorem.

Chandra & Lenny (UIUC) CS374 10 Spring 2015 10 / 37

Page 16: CS 374: Algorithms & Models of Computation

Proving that a problem X is NP-Complete

To prove X is NP-Complete, show

1 Show that X is in NP.

2 Give a polynomial-time reduction from a known NP-Completeproblem such as SAT to X

SAT ≤P X implies that every NP problem Y ≤P X . Why?Transitivity of reductions:

Y ≤P SAT and SAT ≤P X and hence Y ≤P X .

Chandra & Lenny (UIUC) CS374 11 Spring 2015 11 / 37

Page 17: CS 374: Algorithms & Models of Computation

Proving that a problem X is NP-Complete

To prove X is NP-Complete, show

1 Show that X is in NP.

2 Give a polynomial-time reduction from a known NP-Completeproblem such as SAT to X

SAT ≤P X implies that every NP problem Y ≤P X . Why?

Transitivity of reductions:

Y ≤P SAT and SAT ≤P X and hence Y ≤P X .

Chandra & Lenny (UIUC) CS374 11 Spring 2015 11 / 37

Page 18: CS 374: Algorithms & Models of Computation

Proving that a problem X is NP-Complete

To prove X is NP-Complete, show

1 Show that X is in NP.

2 Give a polynomial-time reduction from a known NP-Completeproblem such as SAT to X

SAT ≤P X implies that every NP problem Y ≤P X . Why?Transitivity of reductions:

Y ≤P SAT and SAT ≤P X and hence Y ≤P X .

Chandra & Lenny (UIUC) CS374 11 Spring 2015 11 / 37

Page 19: CS 374: Algorithms & Models of Computation

3-SAT is NP-Complete

3-SAT is in NPSAT ≤P 3-SAT as we saw

Chandra & Lenny (UIUC) CS374 12 Spring 2015 12 / 37

Page 20: CS 374: Algorithms & Models of Computation

NP-Completeness via Reductions

1 SAT is NP-Complete due to Cook-Levin theorem

2 SAT ≤P 3-SAT

3 3-SAT ≤P Independent Set

4 Independent Set ≤P Vertex Cover

5 Independent Set ≤P Clique

6 3-SAT ≤P 3-Color

7 3-SAT ≤P Hamiltonian Cycle

Hundreds and thousands of different problems from many areas ofscience and engineering have been shown to be NP-Complete.

A surprisingly frequent phenomenon!

Chandra & Lenny (UIUC) CS374 13 Spring 2015 13 / 37

Page 21: CS 374: Algorithms & Models of Computation

NP-Completeness via Reductions

1 SAT is NP-Complete due to Cook-Levin theorem

2 SAT ≤P 3-SAT

3 3-SAT ≤P Independent Set

4 Independent Set ≤P Vertex Cover

5 Independent Set ≤P Clique

6 3-SAT ≤P 3-Color

7 3-SAT ≤P Hamiltonian Cycle

Hundreds and thousands of different problems from many areas ofscience and engineering have been shown to be NP-Complete.

A surprisingly frequent phenomenon!

Chandra & Lenny (UIUC) CS374 13 Spring 2015 13 / 37

Page 22: CS 374: Algorithms & Models of Computation

NP-Completeness via Reductions

Chandra & Lenny (UIUC) CS374 14 Spring 2015 14 / 37

Page 23: CS 374: Algorithms & Models of Computation

Part II

Reducing 3-SAT to IndependentSet

Chandra & Lenny (UIUC) CS374 15 Spring 2015 15 / 37

Page 24: CS 374: Algorithms & Models of Computation

Independent Set

Problem: Independent Set

Instance: A graph G, integer k .Question: Is there an independent set in G of size k?

Chandra & Lenny (UIUC) CS374 16 Spring 2015 16 / 37

Page 25: CS 374: Algorithms & Models of Computation

3SAT ≤P Independent Set

The reduction 3SAT ≤P Independent SetInput: Given a 3CNF formula ϕGoal: Construct a graph Gϕ and number k such that Gϕ has anindependent set of size k if and only if ϕ is satisfiable.

Gϕ should be constructable in time polynomial in size of ϕ

Importance of reduction: Although 3SAT is much more expressive, itcan be reduced to a seemingly specialized Independent Set problem.

Notice: We handle only 3CNF formulas – reduction would not workfor other kinds of boolean formulas.

Chandra & Lenny (UIUC) CS374 17 Spring 2015 17 / 37

Page 26: CS 374: Algorithms & Models of Computation

3SAT ≤P Independent Set

The reduction 3SAT ≤P Independent SetInput: Given a 3CNF formula ϕGoal: Construct a graph Gϕ and number k such that Gϕ has anindependent set of size k if and only if ϕ is satisfiable.Gϕ should be constructable in time polynomial in size of ϕ

Importance of reduction: Although 3SAT is much more expressive, itcan be reduced to a seemingly specialized Independent Set problem.

Notice: We handle only 3CNF formulas – reduction would not workfor other kinds of boolean formulas.

Chandra & Lenny (UIUC) CS374 17 Spring 2015 17 / 37

Page 27: CS 374: Algorithms & Models of Computation

3SAT ≤P Independent Set

The reduction 3SAT ≤P Independent SetInput: Given a 3CNF formula ϕGoal: Construct a graph Gϕ and number k such that Gϕ has anindependent set of size k if and only if ϕ is satisfiable.Gϕ should be constructable in time polynomial in size of ϕ

Importance of reduction: Although 3SAT is much more expressive, itcan be reduced to a seemingly specialized Independent Set problem.

Notice: We handle only 3CNF formulas – reduction would not workfor other kinds of boolean formulas.

Chandra & Lenny (UIUC) CS374 17 Spring 2015 17 / 37

Page 28: CS 374: Algorithms & Models of Computation

Interpreting 3SAT

There are two ways to think about 3SAT

1 Find a way to assign 0/1 (false/true) to the variables such thatthe formula evaluates to true, that is each clause evaluates totrue.

2 Pick a literal from each clause and find a truth assignment tomake all of them true

. You will fail if two of the literals you pickare in conflict, i.e., you pick xi and ¬xi

We will take the second view of 3SAT to construct the reduction.

Chandra & Lenny (UIUC) CS374 18 Spring 2015 18 / 37

Page 29: CS 374: Algorithms & Models of Computation

Interpreting 3SAT

There are two ways to think about 3SAT

1 Find a way to assign 0/1 (false/true) to the variables such thatthe formula evaluates to true, that is each clause evaluates totrue.

2 Pick a literal from each clause and find a truth assignment tomake all of them true

. You will fail if two of the literals you pickare in conflict, i.e., you pick xi and ¬xi

We will take the second view of 3SAT to construct the reduction.

Chandra & Lenny (UIUC) CS374 18 Spring 2015 18 / 37

Page 30: CS 374: Algorithms & Models of Computation

Interpreting 3SAT

There are two ways to think about 3SAT

1 Find a way to assign 0/1 (false/true) to the variables such thatthe formula evaluates to true, that is each clause evaluates totrue.

2 Pick a literal from each clause and find a truth assignment tomake all of them true

. You will fail if two of the literals you pickare in conflict, i.e., you pick xi and ¬xi

We will take the second view of 3SAT to construct the reduction.

Chandra & Lenny (UIUC) CS374 18 Spring 2015 18 / 37

Page 31: CS 374: Algorithms & Models of Computation

Interpreting 3SAT

There are two ways to think about 3SAT

1 Find a way to assign 0/1 (false/true) to the variables such thatthe formula evaluates to true, that is each clause evaluates totrue.

2 Pick a literal from each clause and find a truth assignment tomake all of them true. You will fail if two of the literals you pickare in conflict, i.e., you pick xi and ¬xi

We will take the second view of 3SAT to construct the reduction.

Chandra & Lenny (UIUC) CS374 18 Spring 2015 18 / 37

Page 32: CS 374: Algorithms & Models of Computation

The Reduction

1 Gϕ will have one vertex for each literal in a clause

2 Connect the 3 literals in a clause to form a triangle; theindependent set will pick at most one vertex from each clause,which will correspond to the literal to be set to true

3 Connect 2 vertices if they label complementary literals; thisensures that the literals corresponding to the independent set donot have a conflict

4 Take k to be the number of clauses

¬x1 ¬x2 ¬x1

x1 x3x3x2 x2 x4

Figure: Graph forϕ = (¬x1 ∨ x2 ∨ x3) ∧ (x1 ∨ ¬x2 ∨ x3) ∧ (¬x1 ∨ x2 ∨ x4)

Chandra & Lenny (UIUC) CS374 19 Spring 2015 19 / 37

Page 33: CS 374: Algorithms & Models of Computation

The Reduction

1 Gϕ will have one vertex for each literal in a clause2 Connect the 3 literals in a clause to form a triangle; the

independent set will pick at most one vertex from each clause,which will correspond to the literal to be set to true

3 Connect 2 vertices if they label complementary literals; thisensures that the literals corresponding to the independent set donot have a conflict

4 Take k to be the number of clauses

¬x1 ¬x2 ¬x1

x1 x3x3x2 x2 x4

Figure: Graph forϕ = (¬x1 ∨ x2 ∨ x3) ∧ (x1 ∨ ¬x2 ∨ x3) ∧ (¬x1 ∨ x2 ∨ x4)

Chandra & Lenny (UIUC) CS374 19 Spring 2015 19 / 37

Page 34: CS 374: Algorithms & Models of Computation

The Reduction

1 Gϕ will have one vertex for each literal in a clause2 Connect the 3 literals in a clause to form a triangle; the

independent set will pick at most one vertex from each clause,which will correspond to the literal to be set to true

3 Connect 2 vertices if they label complementary literals; thisensures that the literals corresponding to the independent set donot have a conflict

4 Take k to be the number of clauses

¬x1 ¬x2 ¬x1

x1 x3x3x2 x2 x4

Figure: Graph forϕ = (¬x1 ∨ x2 ∨ x3) ∧ (x1 ∨ ¬x2 ∨ x3) ∧ (¬x1 ∨ x2 ∨ x4)

Chandra & Lenny (UIUC) CS374 19 Spring 2015 19 / 37

Page 35: CS 374: Algorithms & Models of Computation

The Reduction

1 Gϕ will have one vertex for each literal in a clause2 Connect the 3 literals in a clause to form a triangle; the

independent set will pick at most one vertex from each clause,which will correspond to the literal to be set to true

3 Connect 2 vertices if they label complementary literals; thisensures that the literals corresponding to the independent set donot have a conflict

4 Take k to be the number of clauses

¬x1 ¬x2 ¬x1

x1 x3x3x2 x2 x4

Figure: Graph forϕ = (¬x1 ∨ x2 ∨ x3) ∧ (x1 ∨ ¬x2 ∨ x3) ∧ (¬x1 ∨ x2 ∨ x4)

Chandra & Lenny (UIUC) CS374 19 Spring 2015 19 / 37

Page 36: CS 374: Algorithms & Models of Computation

The Reduction

1 Gϕ will have one vertex for each literal in a clause2 Connect the 3 literals in a clause to form a triangle; the

independent set will pick at most one vertex from each clause,which will correspond to the literal to be set to true

3 Connect 2 vertices if they label complementary literals; thisensures that the literals corresponding to the independent set donot have a conflict

4 Take k to be the number of clauses

¬x1 ¬x2 ¬x1

x1 x3x3x2 x2 x4

Figure: Graph forϕ = (¬x1 ∨ x2 ∨ x3) ∧ (x1 ∨ ¬x2 ∨ x3) ∧ (¬x1 ∨ x2 ∨ x4)

Chandra & Lenny (UIUC) CS374 19 Spring 2015 19 / 37

Page 37: CS 374: Algorithms & Models of Computation

Correctness

Proposition

ϕ is satisfiable iff Gϕ has an independent set of size k (= number ofclauses in ϕ).

Proof.⇒ Let a be the truth assignment satisfying ϕ

1 Pick one of the vertices, corresponding to true literals under a,from each triangle. This is an independent set of theappropriate size. Why?

Chandra & Lenny (UIUC) CS374 20 Spring 2015 20 / 37

Page 38: CS 374: Algorithms & Models of Computation

Correctness

Proposition

ϕ is satisfiable iff Gϕ has an independent set of size k (= number ofclauses in ϕ).

Proof.⇒ Let a be the truth assignment satisfying ϕ

1 Pick one of the vertices, corresponding to true literals under a,from each triangle. This is an independent set of theappropriate size. Why?

Chandra & Lenny (UIUC) CS374 20 Spring 2015 20 / 37

Page 39: CS 374: Algorithms & Models of Computation

Correctness (contd)

Proposition

ϕ is satisfiable iff Gϕ has an independent set of size k (= number ofclauses in ϕ).

Proof.⇐ Let S be an independent set of size k

1 S must contain exactly one vertex from each clause2 S cannot contain vertices labeled by conflicting literals3 Thus, it is possible to obtain a truth assignment that makes in

the literals in S true; such an assignment satisfies one literal inevery clause

Chandra & Lenny (UIUC) CS374 21 Spring 2015 21 / 37

Page 40: CS 374: Algorithms & Models of Computation

Part III

NP-Completeness of HamiltonianCycle

Chandra & Lenny (UIUC) CS374 22 Spring 2015 22 / 37

Page 41: CS 374: Algorithms & Models of Computation

Directed Hamiltonian Cycle

Input Given a directed graph G = (V ,E) with n vertices

Goal Does G have a Hamiltonian cycle?

A Hamiltonian cycle is a cycle in the graph thatvisits every vertex in G exactly once

Chandra & Lenny (UIUC) CS374 23 Spring 2015 23 / 37

Page 42: CS 374: Algorithms & Models of Computation

Directed Hamiltonian Cycle

Input Given a directed graph G = (V ,E) with n vertices

Goal Does G have a Hamiltonian cycle?

A Hamiltonian cycle is a cycle in the graph thatvisits every vertex in G exactly once

Chandra & Lenny (UIUC) CS374 23 Spring 2015 23 / 37

Page 43: CS 374: Algorithms & Models of Computation

Is the following graph Hamiltonian?

(A) Yes.

(B) No.

Chandra & Lenny (UIUC) CS374 24 Spring 2015 24 / 37

Page 44: CS 374: Algorithms & Models of Computation

Directed Hamiltonian Cycle is NP-Complete

Directed Hamiltonian Cycle is in NP: Why?

Hardness: We will show3-SAT ≤P Directed Hamiltonian Cycle

Chandra & Lenny (UIUC) CS374 25 Spring 2015 25 / 37

Page 45: CS 374: Algorithms & Models of Computation

Reduction

Given 3-SAT formula ϕ create a graph Gϕ such that

Gϕ has a Hamiltonian cycle if and only if ϕ is satisfiable

Gϕ should be constructible from ϕ by a polynomial timealgorithm A

Notation: ϕ has n variables x1, x2, . . . , xn and m clausesC1,C2, . . . ,Cm.

Chandra & Lenny (UIUC) CS374 26 Spring 2015 26 / 37

Page 46: CS 374: Algorithms & Models of Computation

Reduction: First Ideas

Viewing SAT: Assign values to n variables, and each clause hasmultiple ways in which it can be satisfied.

Construct graph with 2n Hamiltonian cycles, where each cyclecorresponds to some boolean assignment.

Then add more graph structure to encode constraints onassignments imposed by the clauses.

Chandra & Lenny (UIUC) CS374 27 Spring 2015 27 / 37

Page 47: CS 374: Algorithms & Models of Computation

The Reduction: Phase I

Traverse path i from left to right iff xi is set to true

Each path has 3(m + 1) nodes where m is number of clauses inϕ; nodes numbered from left to right (1 to 3m + 3)

x2

x3

x1

x4

Chandra & Lenny (UIUC) CS374 28 Spring 2015 28 / 37

Page 48: CS 374: Algorithms & Models of Computation

The Reduction: Phase II

Add vertex cj for clause Cj . cj has edge from vertex 3j and tovertex 3j + 1 on path i if xi appears in clause Cj , and has edgefrom vertex 3j + 1 and to vertex 3j if ¬xi appears in Cj .

x2

x3

¬x1 ∨ ¬x2 ∨ ¬x3

x1

x1 ∨ ¬x2 ∨ x4

x4

Chandra & Lenny (UIUC) CS374 29 Spring 2015 29 / 37

Page 49: CS 374: Algorithms & Models of Computation

The Reduction: Phase II

Add vertex cj for clause Cj . cj has edge from vertex 3j and tovertex 3j + 1 on path i if xi appears in clause Cj , and has edgefrom vertex 3j + 1 and to vertex 3j if ¬xi appears in Cj .

x2

x3

¬x1 ∨ ¬x2 ∨ ¬x3

x1

x1 ∨ ¬x2 ∨ x4

x4

Chandra & Lenny (UIUC) CS374 29 Spring 2015 29 / 37

Page 50: CS 374: Algorithms & Models of Computation

The Reduction: Phase II

Add vertex cj for clause Cj . cj has edge from vertex 3j and tovertex 3j + 1 on path i if xi appears in clause Cj , and has edgefrom vertex 3j + 1 and to vertex 3j if ¬xi appears in Cj .

x2

x3

¬x1 ∨ ¬x2 ∨ ¬x3

x1

x1 ∨ ¬x2 ∨ x4

x4

”Buffer” vertices

Chandra & Lenny (UIUC) CS374 29 Spring 2015 29 / 37

Page 51: CS 374: Algorithms & Models of Computation

The Reduction: Phase II

Add vertex cj for clause Cj . cj has edge from vertex 3j and tovertex 3j + 1 on path i if xi appears in clause Cj , and has edgefrom vertex 3j + 1 and to vertex 3j if ¬xi appears in Cj .

x2

x3

¬x1 ∨ ¬x2 ∨ ¬x3

x1

x1 ∨ ¬x2 ∨ x4

x4

Chandra & Lenny (UIUC) CS374 29 Spring 2015 29 / 37

Page 52: CS 374: Algorithms & Models of Computation

The Reduction: Phase II

Add vertex cj for clause Cj . cj has edge from vertex 3j and tovertex 3j + 1 on path i if xi appears in clause Cj , and has edgefrom vertex 3j + 1 and to vertex 3j if ¬xi appears in Cj .

x2

x3

¬x1 ∨ ¬x2 ∨ ¬x3

x1

x1 ∨ ¬x2 ∨ x4

x4

Chandra & Lenny (UIUC) CS374 29 Spring 2015 29 / 37

Page 53: CS 374: Algorithms & Models of Computation

The Reduction: Phase II

Add vertex cj for clause Cj . cj has edge from vertex 3j and tovertex 3j + 1 on path i if xi appears in clause Cj , and has edgefrom vertex 3j + 1 and to vertex 3j if ¬xi appears in Cj .

x2

x3

¬x1 ∨ ¬x2 ∨ ¬x3

x1

x1 ∨ ¬x2 ∨ x4

x4

Chandra & Lenny (UIUC) CS374 29 Spring 2015 29 / 37

Page 54: CS 374: Algorithms & Models of Computation

The Reduction: Phase II

Add vertex cj for clause Cj . cj has edge from vertex 3j and tovertex 3j + 1 on path i if xi appears in clause Cj , and has edgefrom vertex 3j + 1 and to vertex 3j if ¬xi appears in Cj .

x2

x3

¬x1 ∨ ¬x2 ∨ ¬x3

x1

x1 ∨ ¬x2 ∨ x4

x4

Chandra & Lenny (UIUC) CS374 29 Spring 2015 29 / 37

Page 55: CS 374: Algorithms & Models of Computation

Correctness Proof

Propositionϕ has a satisfying assignment iff Gϕ has a Hamiltonian cycle.

Proof.⇒ Let a be the satisfying assignment for ϕ. Define Hamiltonian

cycle as follows

If a(xi ) = 1 then traverse path i from left to rightIf a(xi ) = 0 then traverse path i from right to leftFor each clause, path of at least one variable is in the “right”direction to splice in the node corresponding to clause

Chandra & Lenny (UIUC) CS374 30 Spring 2015 30 / 37

Page 56: CS 374: Algorithms & Models of Computation

Hamiltonian Cycle⇒ Satisfying assignment

Suppose Π is a Hamiltonian cycle in Gϕ

If Π enters cj (vertex for clause Cj ) from vertex 3j on path ithen it must leave the clause vertex on edge to 3j + 1 on thesame path i

If not, then only unvisited neighbor of 3j + 1 on path i is 3j + 2Thus, we don’t have two unvisited neighbors (one to enterfrom, and the other to leave) to have a Hamiltonian Cycle

Similarly, if Π enters cj from vertex 3j + 1 on path i then itmust leave the clause vertex cj on edge to 3j on path i

Chandra & Lenny (UIUC) CS374 31 Spring 2015 31 / 37

Page 57: CS 374: Algorithms & Models of Computation

Example

x2

x3

x1

x4

Chandra & Lenny (UIUC) CS374 32 Spring 2015 32 / 37

Page 58: CS 374: Algorithms & Models of Computation

Hamiltonian Cycle =⇒ Satisfying assignment

(contd)

Thus, vertices visited immediately before and after Ci areconnected by an edge

We can remove cj from cycle, and get Hamiltonian cycle inG − cj

Consider Hamiltonian cycle in G − {c1, . . . cm}; it traverseseach path in only one direction, which determines the truthassignment

Chandra & Lenny (UIUC) CS374 33 Spring 2015 33 / 37

Page 59: CS 374: Algorithms & Models of Computation

Hamiltonian Cycle

ProblemInput Given undirected graph G = (V ,E)

Goal Does G have a Hamiltonian cycle? That is, is there acycle that visits every vertex exactly one (except startand end vertex)?

Chandra & Lenny (UIUC) CS374 34 Spring 2015 34 / 37

Page 60: CS 374: Algorithms & Models of Computation

NP-Completeness

TheoremHamiltonian cycle problem for undirected graphs isNP-Complete.

Proof.The problem is in NP; proof left as exercise.

Hardness proved by reducing Directed Hamiltonian Cycle to thisproblem

Chandra & Lenny (UIUC) CS374 35 Spring 2015 35 / 37

Page 61: CS 374: Algorithms & Models of Computation

Reduction Sketch

Goal: Given directed graph G , need to construct undirected graphG ′ such that G has Hamiltonian cycle iff G ′ has Hamiltonian cycle

Reduction

Replace each vertex v by 3 vertices: vin, v , and vout

A directed edge (x, y) is replaced by edge (xout, yin)

bv

a

d

c

bo

vi

ao

v vo

di

ci

Chandra & Lenny (UIUC) CS374 36 Spring 2015 36 / 37

Page 62: CS 374: Algorithms & Models of Computation

Reduction Sketch

Goal: Given directed graph G , need to construct undirected graphG ′ such that G has Hamiltonian cycle iff G ′ has Hamiltonian cycle

ReductionReplace each vertex v by 3 vertices: vin, v , and vout

A directed edge (x, y) is replaced by edge (xout, yin)

bv

a

d

c

bo

vi

ao

v vo

di

ci

Chandra & Lenny (UIUC) CS374 36 Spring 2015 36 / 37

Page 63: CS 374: Algorithms & Models of Computation

Reduction Sketch

Goal: Given directed graph G , need to construct undirected graphG ′ such that G has Hamiltonian cycle iff G ′ has Hamiltonian cycle

ReductionReplace each vertex v by 3 vertices: vin, v , and vout

A directed edge (x, y) is replaced by edge (xout, yin)

bv

a

d

c

bo

vi

ao

v vo

di

ci

Chandra & Lenny (UIUC) CS374 36 Spring 2015 36 / 37

Page 64: CS 374: Algorithms & Models of Computation

Reduction Sketch

Goal: Given directed graph G , need to construct undirected graphG ′ such that G has Hamiltonian cycle iff G ′ has Hamiltonian cycle

ReductionReplace each vertex v by 3 vertices: vin, v , and vout

A directed edge (x, y) is replaced by edge (xout, yin)

bv

a

d

c

bo

vi

ao

v vo

di

ci

Chandra & Lenny (UIUC) CS374 36 Spring 2015 36 / 37

Page 65: CS 374: Algorithms & Models of Computation

Reduction: Wrapup

The reduction is polynomial time (exercise)

The reduction is correct (exercise)

Chandra & Lenny (UIUC) CS374 37 Spring 2015 37 / 37