You are on page 1of 32

Theory of Computation

Lecture #32
Sarmad Abbasi
Virtual University

Sarmad Abbasi (Virtual University)

Theory of Computation

1 / 23

Lecture 32: Overview

Review of NP-completeness results


Hamiltonian Path Problem

Sarmad Abbasi (Virtual University)

Theory of Computation

2 / 23

NP-completeness results so far

SAT is NP-complete. (Cook-Levin)

3SAT is NP-complete. (SAT p 3SAT)

IS is NP-complete. (3SAT p IS)

CLIQUE is NP-complete (IS p CLIQUE)

VC is NP-complete (CLIQUE p VC)

Sarmad Abbasi (Virtual University)

Theory of Computation

3 / 23

NP-completeness

Sarfarz Show figure 1.

Sarmad Abbasi (Virtual University)

Theory of Computation

4 / 23

NP-completeness

Our goal now is to add one more problem to this picture. We want to
show that the hamiltonian path problem is also NP-Complete. The
picture will then look like this.

Sarmad Abbasi (Virtual University)

Theory of Computation

5 / 23

NP-completeness

Sarfarz Show figure 2.

Sarmad Abbasi (Virtual University)

Theory of Computation

6 / 23

NP-completeness

As shown in the figure we will reduce the hamiltonian path problem to


this problem.

Sarmad Abbasi (Virtual University)

Theory of Computation

7 / 23

NP-completeness

Let G = (V , E) be a directed graph and s, t V be two vertices. A


Hamiltonian path from s to t is a path that
1

Starts and s.

Ends at t

visits all the vertices of the graph exactly once.

Sarmad Abbasi (Virtual University)

Theory of Computation

8 / 23

NP-completeness

Sarfarz Show figure 3. Does this graph have a hamiltonian path from s
to t.

Sarmad Abbasi (Virtual University)

Theory of Computation

9 / 23

NP-completeness

Sarfarz Show figure 4. Yes, it does have a hamiltonian path from s to t.

Sarmad Abbasi (Virtual University)

Theory of Computation

10 / 23

NP-completeness

Let us define
HAMPATH{< G, s, t >: G has a hamiltonian path from s to t}.
Thus the computational question is that given a graph G and two
vertices s and t. Does there exist a hamiltonian path from s to t.

Sarmad Abbasi (Virtual University)

Theory of Computation

11 / 23

NP-completeness

We can easily show that HAMPATH is in NP.

Sarmad Abbasi (Virtual University)

Theory of Computation

12 / 23

NP-completeness

Theorem
3SAT p HAMPATH
Consequently HAMPATH is NP-complete.

Sarmad Abbasi (Virtual University)

Theory of Computation

13 / 23

NP-completeness

Given a boolean formula we will define a graph G and two vertices


s and t such that
G has a hamiltonian path from s to t
if and only if

is satisfiable.

Sarmad Abbasi (Virtual University)

Theory of Computation

14 / 23

NP-completeness

Let us take
= C1 C2 Cm
and x1 , . . . , xn be the variables in .
We describe how to construct G .

Sarmad Abbasi (Virtual University)

Theory of Computation

15 / 23

NP-completeness

For each variable xi we create a diamond shape structure as shown in


the figure.

Sarmad Abbasi (Virtual University)

Theory of Computation

16 / 23

NP-completeness

Show figure. This figure shows the diamond shape structure that has
to be created for each node xi .

Sarmad Abbasi (Virtual University)

Theory of Computation

17 / 23

NP-completeness

For each Clause Cj we create a vertex and label it Cj . Show figure.


Two special vertices s and t are added to the graph.

Sarmad Abbasi (Virtual University)

Theory of Computation

18 / 23

NP-completeness

Now, we show how to connect the diamond vertices to the clause


vertices. Each xi has two vertices for each clause Cj . So, the diamond
vertices are as follows:
Show figure. As you can see the vertices are paired up. Two verices
each correspond to a clause. We label them CjL and CjR for left and
right.

Sarmad Abbasi (Virtual University)

Theory of Computation

19 / 23

NP-completeness

Now, let us discuss how to connect the vertices in the diamond to the
clauses. Here is the rule.
If xi appears in Cj then:
Connect the vertex in the diamond for xi that is labeled CjL with Cj .
Also, connect Cj with CjR . Formally, all the following edges
(CjL , Cj ) and (Cj , CjR ).
Show figure.

Sarmad Abbasi (Virtual University)

Theory of Computation

20 / 23

NP-completeness

Here is another rule


If xi appears in Cj then:
Connect the vertex in the diamond for xi that is labeled CjR with Cj .
Also, connect Cj with CjL . Formally, all the following edges
(CjR , Cj ) and (Cj , CjL ).
Note that this is backwards. Show figure.

Sarmad Abbasi (Virtual University)

Theory of Computation

21 / 23

NP-completeness

This finishes the construction of the graph. Let us look at a complete


picture for the following formula
=

Sarmad Abbasi (Virtual University)

Theory of Computation

22 / 23

NP-completeness

Show complete picture

Sarmad Abbasi (Virtual University)

Theory of Computation

23 / 23

NP-completeness

What can we say about this graph. Well, I will convince you that:
1

If is satisfiable then this graph has a hamiltonian path from s to t.

If this graph has a hamiltonian path from s to t then must be


satisfiable.

Sarmad Abbasi (Virtual University)

Theory of Computation

24 / 23

NP-completeness

Actually if we ignore the clause vertices then finding a hamiltonian


cycle in this graph is easy.
We can go from each diamond in two ways.

Sarmad Abbasi (Virtual University)

Theory of Computation

25 / 23

NP-completeness

The idea is if we traverse a diamond from left to right then xi is set to


true and if we traverse it from right to left then xi is set to false.

Sarmad Abbasi (Virtual University)

Theory of Computation

26 / 23

NP-completeness

Thus the 2n assingments correspond to travesing the diamonds in 2n


possible ways. Let us say we have the assignment
x1 = ture, x2 = false, z3 = true then we will traverse the graph as
shown. Show figure.

Sarmad Abbasi (Virtual University)

Theory of Computation

27 / 23

NP-completeness

Note that if we are traversing the diamond for xi from Left to right then
we can pick up a clause Cj if xi appears in Cj . This is due to
construction. This is because we have an edge from CjL to Cj and back
to CjR . Show figure.

Sarmad Abbasi (Virtual University)

Theory of Computation

28 / 23

NP-completeness

Note that if we are traversing the diamond for xi from Right to Left then
we can pick up a clause Cj if xi appears in Cj . This is again by
construction. In this case we have an edge from CjR to Cj and back to
CjL .

Sarmad Abbasi (Virtual University)

Theory of Computation

29 / 23

NP-completeness

Suppose that we have a satisfying assignment for which satisfies all


the clauses. Then we can pick up all the clauses by traversing the
graph in the way specified by the satisfying assgnment.

Sarmad Abbasi (Virtual University)

Theory of Computation

30 / 23

NP-completeness

So, if is satisfiable. Take a satisfying assignment of .


1

Start from s.

Traverse each diamond for xi in the order specified by the


assignment. If xi is set true go from left to right. If it is set false go
from right to left.

Along the way pick up any Cj that has not been already picked up.

This yeilds our hamiltonain path from s to t.

Sarmad Abbasi (Virtual University)

Theory of Computation

31 / 23

NP-completeness

Lemma
If is satisfiable then G has a hamiltonian path from s to t.
We have to prove the converse of this theorem next time

Sarmad Abbasi (Virtual University)

Theory of Computation

32 / 23

You might also like