Unit 5 NP
Unit 5 NP
NP-Completeness
5.1 Introduction:
Almost all the algorithms so far we have studied are polynomial type algorithms: on input of size n
k
their worst case time is O(n ) for some constant k. Now the question is that can all the problems are
solvable in polynomial time, the answer is No, it means that there are some problems that cannot be solved
in polynomial time such as turning machine “halting Problem”.
Generally we assume the problems that can be solved in polynomial time are “tractable “or easy problems
and those can not be solvable are “Intractable” or hard problems. So there exist some problems that cannot
be solved in polynomial time, these problems are called NP problems. We believe in theoretical computer
science that P ≠ NP; it means NP complete problems exist.
Examples:
Shortest Vs Longest simple path: we can find shortest path from single source in O(VE) time.
Finding the longest simple path between two vertices is NP problem.
Euler’s tour Vs Hamilton cycle: Euler’s tour of a directed graph is a cycle that traverses each edge
of G exactly once, although it may visit the vertex in more than once. It takes O (E) time. Hamilton
cycle of a directed graph is a cycle that contains all vertices (visited exactly once). It is NP complete
problem.
3-CNF satisfiability: A Boolean formula contains variables whose values are 0 or 1; Boolean
connectives such as (AND), (OR), and ¬ (NOT); and parentheses. A Boolean formula is satisfiable
if there is some assignment of the values 0 and 1 to its variables that causes it to evaluate to 1. A
Boolean formula is in k-conjunctive normal form, or k-CNF, if it is the AND of clauses of ORs of
exactly k variables or their negations. 3-CNF is NP problem.
*********************************************************************
1 |@2010 M r . S h a n k a r T h a w k a r , H O D I T , H C S T
Q. Define NP, NP-complete and NP-hard. Give examples of each.
The class of problem NP is divided into two types: NP-Complete and NP-Hard
NP-Complete: Any problem is NP complete if it is NP and NP-Hard. These problems have the property
that if any NP-complete problem can be solved in polynomial time the every NP-complete problem is
solved in polynomial time.
Example: Hamilton-cycle and vertex-cover problem.
NP-Hard: A problem L is NP-hard if and only if it is reducible. All NP-hard problems are NP-complete
but not all NP-complete problems are NP-hard.
Example: Set covering problem is NP-hard.
Class co-NP: class co-NP as the set of languages L such that the question of whether
NP is closed under complement can be rephrased as whether NP = co-NP. Since P is closed under
complement it follows that P NP ∩ co-NP. Once again, however, it is not known whether P = NP
∩ co-NP or whether there is some language in NP∩ co-NP
2 |@2010 M r . S h a n k a r T h a w k a r , H O D I T , H C S T
Figure: Four possibilities for relationships among complexity classes. In each diagram, one region
enclosing another indicates a proper-subset relation. (a) P = NP = co-NP. Most researchers regard this
possibility as the most unlikely. (b) If NP is closed under complement, then NP = co-NP, but it need not
be the case that P = NP. (c) P = NP∩co-NP, but NP is not closed under complement. (d) NP = co-NP
and P= NP∩co-NP.
*********************************************************************
3 |@2010 M r . S h a n k a r T h a w k a r , H O D I T , H C S T
As in above figure, the algorithm solve problem A in polynomial time.
1. Given an instance α of problem A, the reduction algorithm transforms it into an instance β of
problem B.
2. Run polynomial time decision algo for B on instance β.
3. Use the ans of β as ans for α.
*********************************************************************
In theoretical computer science we believe that P ≠ NP, it means NP complete problems exist ,these
problems have the interesting property that if any NP-complete problem can be solved in polynomial
time the every NP-complete problem is solved in polynomial time, i.e. P=NP. Despite years of study,
though no polynomial time algorithm has ever been discovered for any NP-complete problem.
The language (Problem) Hamilton cycle is NP-complete problem, if we solve Hamilton cycle in
polynomial time, then we could solve every problem in polynomial time.
Reducibility: Any problem Q can be reduce to problem Q’ such that solution of Q’ provide solution to
Q. for example a problem of solving the linear equation can be reduce to the problem of solving
quadratic equations Given ax+b=0 this can be transformed into 0x2+ax+b=0, so solution of this problem
provide the solution to ax+b=0
A language L1 is polynomial-time reducible to another language L2, denoted L1 ≤ p L2 if there exists a
Polynomial-time computable function f:{0, 1}* → {0, 1}* such that for all x belongs to {0, 1}*, x
belongs to L1 if and only if f(x) belongs to L2.
We call the function f the reduction function, and a polynomial-time algorithm F that computes a
reduction function f is called reduction algorithm. Following fig. shows the reduction of L1 into L2 in
polynomial time.
4 |@2010 M r . S h a n k a r T h a w k a r , H O D I T , H C S T
NP-Complete: Polynomial-time reductions provide a formal means for showing that one problem is at least
as hard as another, to within a polynomial-time factor. That is, if L1 ≤P L2, then L1 is not more than a polynomial
factor harder than L2, which is why the “less than or equal to” notation for reduction is mnemonic. We can now
define the set of NP-complete languages, which are the hardest problems in NP.
If a language L satisfies property 2, but not necessarily property 1, we say that L is NP-hard. We also define NPC
to be the class of NP-complete languages.
*********************************************************************
5.5 SAT
A Boolean formula is a formula where the variables and operations are Boolean (0/1):
(a+b+¬d+e)(¬a+¬c)(¬b+c+d+e)(a+¬c+¬e)
OR: +, AND: (), NOT: ¬
SAT: Given a Boolean formula S, is S satisfiable, that is, can we assign 0’s and 1’s to the variables so
that S is 1 (“true”)? Easy to see that CNF-SAT is in NP: Non-deterministically choose an assignment
of 0’s and 1’s to the variables and then evaluate each clause. If they are all 1 (“true”), then the
formula is satisfiable.
************************************************************************
5 |@2010 M r . S h a n k a r T h a w k a r , H O D I T , H C S T
For example, the circuit in the following Figure has the satisfying assignment x1 = 1, x2 = 1, x3 = 0,
and so it is satisfiable.
Proof: We assume that two inputs to polynomial time algorithm A that can verify CIRCUIT-SAT
problem. One of the inputs is Boolean circuit C and another is a certificate corresponding to the
assignment of Boolean values to the circuit.
The Algorithm A is constructed as: for each logic gate in circuit, it checks that the values
provided by the certificate on I/P wires is correctly computed as a function of values on the output
wires. Then if the output of entire circuit is 1, the algorithm o/p is 1. The certificate has a length
equals to the size of circuit i.e. polynomial in size. So CIRCUIT-SAT problem is verified in
polynomial time. It means that CIRCUIT-SAT is NP problem.
*********************************************************************
6 |@2010 M r . S h a n k a r T h a w k a r , H O D I T , H C S T
Q. Prove that SAT (formula SAT) is NPC.
and thus this formula φ belongs to SAT. The naive algorithm to determine whether an arbitrary
boolean formula is satisfiable does not run in polynomial time. There are 2n possible assignments in a
formula φ with n variables. If the length of φ is polynomial in n, then checking every assignment
requires (2n) time, which is super-polynomial in the length of φ. so SAT is NP problem.
For each wire xi in the circuit C, the formula φ has a variable xi . The proper operation of a
gate can now be expressed as a formula involving the variables of its incident wires. The
formula φ produced by the reduction algorithm is the AND of the circuit output variable with
the conjunction of clauses describing the operation of each gate.
For the circuit in the figure, the formula is
8 |@2010 M r . S h a n k a r T h a w k a r , H O D I T , H C S T
Q. Prove that 3-CNF is NP Complete (NPC)?
is in 3-CNF. The first of its three clauses is , which contains the three literals x1, ¬x1, and
¬x2.
In 3-CNF-SAT, The problem is whether a given boolean formula φ in 3-CNF is satisfiable or not.
1. 3-CNF is NP: To show that 3-CNF belongs to NP, we assume a certificate that consist of a
satisfying assignment for an input formula φ of 3-CNF. The verifying algorithm simply
replaces each variable in the formula with its corresponding value and then evaluates the
expression. This task is easily done in polynomial time. If the expression evaluates to 1, the
formula is satisfiable.
2. 3-CNP is NP-hard: We can prove that 3-CNP is NP-hard by reducing SAT (Boolean
formula) into 3-CNP i.e. SAT ≤P 3-CNF-SAT.The reduction algorithm can be broken into
three basic steps.
Step 1: Construct a binary “parse” tree for the input formula φ, with literals as leaves and
connectives as internal nodes. Following Figure shows such a parse tree for the formula
9 |@2010 M r . S h a n k a r T h a w k a r , H O D I T , H C S T
-Introduce a variable yi for the output of each internal node. Then, we rewrite the original formula φ
as the AND of the root variable and a conjunction of clauses describing the operation of each node.
For the formula φ , the resulting expression is
[Note: the symbol is called if and only if and its value is 1 if values of l-side=r-side, otherwise 0.]
10 |@2010 M r . S h a n k a r T h a w k a r , H O D I T , H C S T
-Using the truth-table entries that evaluate to 0, we build a formula in disjunctive normal form (or
DNF)—an OR of AND’s—
Step 3: transforms the formula so that each clause has exactly 3 distinct literals. The final 3-CNF
formula is constructed from the clauses of the CNF formula . For each clause Ci of , we
include the following clauses in :
-If Ci has 3 distinct literals, then simply include Ci as a clause of
-If Ci has 2 distinct literals, that is, if , where l1 and l2 are literals, then Include
; requirement that there be exactly 3 distinct literals fulfill.
We can see that the 3-CNF formula is in 3-CNF and the reduction is computed in polynomial
time.
11 |@2010 M r . S h a n k a r T h a w k a r , H O D I T , H C S T
Q. Prove that Clique is NP-Complete.
1. CLIQUE is NP: Assume an algorithm A that takes two Inputs. One is the Certificate that contain
vertices of V’ for G and another is the Graph G. An algorithm A verify in polynomial that
whether V’ is a clique or not by checking whether, for each pair u, v V’, the edge (u, v)
belongs to E.
o This graph can easily be computed from φ in polynomial time. Let us consider
12 |@2010 M r . S h a n k a r T h a w k a r , H O D I T , H C S T
then G is the graph shown in following Figure..
from the first clause, x3 from the second clause, and x3 from the third clause.
**************************************************************************************
13 |@2010 M r . S h a n k a r T h a w k a r , H O D I T , H C S T