0% found this document useful (0 votes)
9 views47 pages

Complexity Class

Design and Analysis of Algorithms - Complexity Class

Uploaded by

nvnmarcos5
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views47 pages

Complexity Class

Design and Analysis of Algorithms - Complexity Class

Uploaded by

nvnmarcos5
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 47

Module 4

Introduction to Complexity Theory


P, NP, NPH and NPC
Tractable and Intractable Problems
• Tractable Problems :
Problems that can be solved in polynomial time, O(nk), where n is the size of
the problem and k is a constant.
These problems comes under the complexity class P
Def:- A problem is said to be computationally tractable if there exists an algorithm
to solve the problem which runs in polynomial time.

• Intractable Problems :
Problems that can be solved in exponential time, O(kn) (solved using
nondeterministic polynomial time algorithms)
These problems comes under the complexity class NP (Nondeterministic
Polynomial time)
Deterministic and Nondeterministic Algorithms
• Deterministic Algorithms • Nondeterministic Algorithms
Algorithms contains the result of Algorithms contain operations
every operation is uniquely whose outcome are not uniquely
defined. defined but are limited to a certain
set of possibilities.
Algorithm Lsearch(A,n,key) Algorithm Lsearch(A,n,key)
{ {
for i= 1 to n do i= choice(1,n);
if a[i]=key then if a[i]=key then
{ write i; return();} { write i; success();}
write 0; else{write 0; Failure();}
} }
Decision Problems and Optimization Problems

• Decision Problems :- the problems with answer is yes/no.


• Algorithm for decision problem is called decision algorithm
• Every decision problem that is solvable by a deterministic polynomial time algorithm
is also solvable by a polynomial time nondeterministic algorithm.

• Optimization problems : - any problem that involves the identification of an


optimal (minimum/maximum) of a given cost function.
• An optimization algorithm is used to solve optimization problem.

If an optimization problem is easy , its related decision problem is easy as well


(optimization problem is hard compared to its related decision problem).
Complexity classes of Problems : P and NP
• Class P : - Consist of all the problems that can be solved in polynomial
time, O(nk), where n is the size of input and k is a constant.
An algorithm A is of polynomial complexity if there exists a polynomial
p() such that the computing time of A is O(p(n)) for every input of size n

Eg: Linear search O(n)


Binary search O(log n)
Matrix multiplication, MCM O(n3)
Fractional Knapsack problem O(n)

We can define the complexity class P as a set of concrete decision


problems, that are polynomial time solvable.
A concrete problem are well defined with respect to inputs and solutions.
Complexity classes of Problems : P and NP
• Class NP(Nondeterministic Polynomial) :- NP is a set of all decision
problems solvabe by nondeterministic algorithms in polynomial time

• NP class consists of all those problems that are verifiable using a


polynomial time algorithm.
• Verifiable means if somebody gives a certificate of solution for such
problem, then verify that the certificate is correct or not in polynomial
time. That is the solution can be checked in polynomial time .
Eg: Travelling Salesman Problem
0/1 Knapsack Problem
Clique problem.
Satisfiability problem
Complexity classes of Problems : P and NP
• Is ?
P is a class of all problems that can be solved in polynomial time using
deterministic algorithm.
NP is the set of all decision problems solvable by nondeterministic
algorithms in polynomial time.
Deterministic algorithms are special case of nondeterministic
algorithms.
So P
NP
Complexity classes of Problems : NP Hard and NP Complete
Reduction
Definition:
Let L1 and L2 be to decision problems.
L1 reduces to L2 (L1 ≤p L2) if and only if there is a way to solve L1 by a
deterministic polynomial time algorithm using a deterministic
algorithm that solves L2 in polynomial time.

If we have a polynomial time algorithm for L2, then we can solve L1 in


polynomial time
Complexity classes of Problems : NP Hard and NP Complete
Reduction
Let L1 and L2 be to decision problems.
We want to solve L1 in polynomial time.
Suppose there is an algorithm A2 to solve L2 in polynomial time.
Reduction is to show that L2 is at least as hard as L1.
x I/P of L1 f(x) Yes/No for L1on
Transformation Algorithm A2 Yes/No for L2 on
f(x) f(x)
function f for L2
Complexity classes of Problems : NP Hard and NP Complete
NP Hard
Def: A problem l is NP Hard iff satisfiability reduced to L.

A problem L is NP Hard if all the problem in NP are polynomially reducible to L.


ie, all problems B in NP reducible to L
For all B Є NP , B ≤p L
NPH
NP
P

The problem L may or may not be NP.


Eg: Clique, TSP, 0/1 Knapsack
Complexity classes of Problems : NP Hard and NP Complete
NP Complete (NPC)
A problem L is NP Complete problem if it is an NP Hard and NP
problem.
NPC = NP Ո NPH

NPH
NP
P

Eg: Clique, TSP, 0/1 Knapsack NPC


Complexity classes of Problems : NP Hard and NP Complete

Strategy to adopt to show a problem L2 is NP Hard(NPH)

1. Pick a problem L1 already known to NP Hard


2. Show how to obtain (polynomial deterministic time) an instance I’ of L2 from
any instance I of L1 such that from the solution of I’ we can determine (in
polynomial deterministic time) the solution to instance I of L1.

I Poly. Time I’ Algorithm Poly. Time


Solution for I
transform for L2 transform

3. Conclude from step 2 that L1 ≤p L2


4. Conclude from step 1 and 3 and the transitivity of ≤p that L2 is NP Hard
Complexity classes of Problems : NP Hard and NP Complete

NP Complete (NPC)
A problem L is said to be in NP Complete if and only if it satisfies the
following two conditions.
1. L is in NP class
2. Every problem in NP is polynomial time reducible to L

To prove the first condition, we have to find an efficient verifier (verified


in polynomial time) ie, it is in NP.
To prove second condition, we have to reduce an NP problem( known
NP problem) polynomially to the given problem.
Clique Problem
A clique in a graph is a set of vertices where each vertex shares an
edge with every other vertex. Thus, a clique in a graph is a
subgraph which is a complete graph.
Clique Problem – NP Complete

Problem: Given a graph G(V, E) and an integer K, the problem


is to determine if the graph contains a clique of size K.

An instance of the problem is an input specified to the problem.


An instance of the Clique problem is a graph G (V, E) and a positive integer
K.
Clique Problem – NP Complete

Explanation:
The problem is to check whether a clique of size K exists in G.

The proof of NP-Complete consists of two parts:


1.The problem itself is in NP class
2.All other problems in NP class can be polynomial-time reducible to that.
Clique Problem – NP Complete

1. The Clique Decision Problem belongs to NP


If a problem belongs to the NP class, then it should have polynomial-
time verifiability, that is given a certificate, we should be able to
verify in polynomial time if it is a solution to the problem.

The certificate is a subset V’ of the vertices, which comprises the


vertices belonging to the clique.
We can validate this solution by checking that each pair of vertices
belonging to the solution are adjacent, by simply verifying that they
share an edge with each other.
This can be done in polynomial time, that is O(V +E) using below
algorithm
Clique Problem – NP Complete
flag=true
For every pair {u, v} in the subset V’:
Check that these two vertices {u, v} share an edge
If there is no edge
flag=false
break
If flag is true:
Solution is correct
Else: Solution is incorrect
4. Clique Problem (NP complete Problem)

• Given a graph G=(V,E). Clique G is a complete subgraph of G. ie, part


of a graph in which all the nodes are connected to each other.

• Clique Problem: - is to find the largest subset V’


• Clique Decision Problem: - checks whether a clique of size k, exist or
not.
4. Clique Problem (NP complete Problem)
Proof
Step 1 : prove clique is in NP
• Given inputs (G,k,C), where G the graph, k the size of clique and C the
set of vertices as certificate.
• The verification algorithm can check in polynomial time whether
there is an edge between any pair of vertices given in the list.
• The verification algorithm can verify the problem in polynomial time.
• Clique decision problem is in NP.
4. Clique Problem (NP complete Problem)
Proof Step 2 : prove clique is in NP Hard : Take a known NPH problem
and show that this problem can be reduced to the Clique
problem.
Let consider Vertex Cover problem for that

Now, we need to show that any instance (G, k) of the


Vertex cover problem can be reduced to an instance of
the Clique problem.
4. Clique Problem (NP complete Problem)
Consider the graph G’ which consists of all edges not in
G, but in the complete graph using all vertices in G (the
complement of G).
Now, the problem of finding whether a vertex cover of
size k exists in the graph G is the same as the problem of
finding whether there is a clique of size |V| – k in G’.
We need to show that this is indeed the case.
4. Clique Problem (NP complete Problem)
1. Given a graph G = (V, E) and integer k
2. Generate the complement graph G = (V, E)
3. Solve the problem CLIQUE(G, |V | − k)
4. If there is a solution, return Yes, else return No
4. Clique Problem (NP complete Problem)

graph G = (V, E) with vertex cover V − V ‘ = {w, z}. complement G with clique V ‘ = {u, v, x, y}.

To prove this reduction is correct, we need to show two things: • If there is a solution to
VERTEX-COVER(G, k), then there must be a solution to CLIQUE(G, |V | − k)

• if there is a solution to CLIQUE(G, |V | − k), then there must be a solution to VERTEX-


COVER(G, k)
Prove vertex cover is NP complete
Problem – Given a graph G(V, E) and a positive integer
k, the problem is to find whether there is a subset V’ of
vertices of size at most k, such that every edge in the
graph is connected to some vertex in V’.
Explanation:
An instance of the Vertex Cover problem is a graph G (V,
E) and a positive integer k, and the problem is to check
whether a vertex cover of size at most k exists in G.
Proof that vertex cover is NP complete
Proof that vertex cover is in NP
If any problem is in NP, then, given a ‘certificate’ (a
solution) to the problem and an instance of the problem
(a graph G and a positive integer k, in this case), we will
be able to verify (check whether the solution given is
correct or not) the certificate in polynomial time.
The certificate for the vertex cover problem is a
subset V’ of V, which contains the vertices in the
vertex cover.
We can check whether the set V’ is a vertex cover of size
k using the following strategy (for a graph G(V, E)):
Proof that vertex cover is NP complete
Proof that vertex cover is in NP

Count =0
for each vertex v in V’
remove all edges adjacent to v from set E
increment count by 1
if count = k and E is empty
the given solution is correct
else
the given solution is wrong

This can be done in polynomial time. Thus the vertex cover problem
is in the class NP
Proof that vertex cover is NP complete
Proof that vertex cover is NP Hard –
To prove that Vertex Cover is NP Hard, we take some
problem which has already been proven to be NP Hard,
and show that this problem can be reduced to the Vertex
Cover problem.
For this, consider the Clique problem, which is NP
Complete (and hence NP Hard).

Consider the problem of finding out whether there is a


clique of size k in the given graph.
Therefore, an instance of the clique problem is a graph G (V, E)
and a non-negative integer k, and we need to check for the
existence of a clique of size k in G.
Proof that vertex cover is NP complete
Proof that vertex cover is NP Hard –
Next, show that any instance (G, k) of the Clique
problem can be reduced to an instance of the
vertex cover problem.
Consider the graph G’ which consists of all edges not in
G, but in the complete graph using all vertices in G.
Let us call this the complement of G.
Now, the problem of finding whether a clique of
size k exists in the graph G is the same as the
problem of finding whether there is a vertex cover
of size |V| – k in G’.
Proof that vertex cover is NP complete
Proof that vertex cover is NP Hard –
Next, show that any instance (G, k) of the Clique
problem can be reduced to an instance of the
vertex cover problem.
Consider the graph G’ which consists of all edges not in
G, but in the complete graph using all vertices in G.
Let us call this the complement of G.
Now, the problem of finding whether a clique of
size k exists in the graph G is the same as the
problem of finding whether there is a vertex cover
of size |V| – k in G’.
Proof that vertex cover is NP complete
Proof that vertex cover is NP Hard –
Assume that there is a clique of size k in G.
Let the set of vertices in the clique be V’.
This means |V’| = k.
In the complement graph G’, let us pick any edge (u, v).
Then at least one of u or v must be in the set V – V’.
This is because, if both u and v were from the set V’, then
the edge (u, v) would belong to V’, which, in turn would
mean that the edge (u, v) is in G.
This is not possible since (u, v) is not in G. Thus, all edges
in G’ are covered by vertices in the set V – V’.
Proof that vertex cover is NP complete
Proof that vertex cover is NP Hard –
Now assume that there is a vertex cover V’’ of size |V| – k
in G’.
This means that all edges in G’ are connected to some
vertex in V’’. As a result, if we pick any edge (u, v) from
G’, both of them cannot be outside the set V’’.
This means, all edges (u, v) such that both u and v are
outside the set V’’ are in G, i.e., these edges constitute a
clique of size k.
Proof that vertex cover is NP complete
Proof that vertex cover is NP complete
4. Clique Problem (NP complete Problem)
Proof Step 2 : prove clique is in NP Hard : Take a known NP problem. Reduce
that problem to the given problem in polynomial time. Then we can say that
the given problem is NP Hard.

Take 3CNF as known NP problem.


G has k clique if and only if the formula is satisfiable.
Consider the following 3CNF Boolean formula
F=
A graph is produced from the above formula as follows.
1. Every vertex in a clause is connected to every other vertex in another

2. No edge can be connected to a vertex x and its negation


clause.
4. Clique Problem (NP complete Problem)

F=

Clause 2

𝑎 b c

a a

𝑏 b

𝑐 c

Clause 1 Clause 3
4. Clique Problem (NP complete Problem)
F is satisfiable iff G has a clique of size k= 3.
A clique of k corresponds to assignment of true to k literals in k
different clauses. b

Consider a clique in this clique a=b= =1


a
𝑐
Apply a=b= =1 to F
F=
= (1+0+1).(0+1+1).(1+1+1)
= 1.1.1 =1, F is Satisfiable
ie, 3CNF ≤p Clique. So clique is NP Hard.
4. Clique Problem (NP complete Problem)

It should also be observed that


An edge is between only non-contradictory nodes.
Hence, f is satisfiable iff there is noncontradictory assignment of
true to k literals.
This is possible iff G has a clique of size k.

Therefore, one can conclude that Clique problem is NP-Complete.


6. Vertex Cover Problem
• Given a graph G = (V,E) and a positive integer k, Find a subset V’ of
size k such that each edge in E is incident to at least one vertex in V’.

• A vertex cover of an undirected graph G = (V, E) is a subset V' V such


that if (u, v) E, then u V' or v V' (or both).
• That is, each vertex "covers" its incident edges, and a vertex cover for
G is a set of vertices that covers all the edges in E.
• The size of a vertex cover is the number of vertices in it.
6. Vertex Cover Problem
• For example, the graph has a vertex cover {w, z} of size 2
6. Vertex Cover Problem

• The vertex-cover problem is to find a vertex cover of


minimum size in a given graph.
• Restating this optimization problem as a decision problem,
we wish to determine whether a graph has a vertex cover
of a given size k.

VERTEX-COVER = {G, k : graph G has a vertex cover of size k}.


6. Vertex Cover Problem is NP-complete
1. Proof : Show that VERTEX-COVER NP.

Suppose we are given a graph G = (V, E) and an integer k.


The certificate we choose is the vertex cover V' V itself.
The verification algorithm confirms that |V'| = k, and then it checks, for
each edge (u, v) E, that u V' or v V’.
This verification can be performed straightforwardly in polynomial time.
So VERTEX-COVER NP
6. Vertex Cover Problem is NP-complete
2. Proof : Show that VERTEX-COVER NPH.

This can be proved by showing that


CLIQUE ≤P VERTEXCOVER.
This reduction is based on the notion of the "complement" of a graph.
Given an undirected graph G = (V, E), we define the complement of G as
= (V, ), where
= {(u,v):u,v, uand (u, v) ∉ E}.
The graph containing exactly those edges that are not in G.
6. Vertex Cover Problem is NP-complete

2
2
5
5
1 3
1 3

4
4

Graph G with Clique V’={1,2,3} Graph with vertex cover V-V’= {4,5}
6. Vertex Cover Problem is NP-complete
• The reduction algorithm takes as input an instance (G, k) of the clique
problem.
• It computes the complement of G, which is easily done in polynomial
time.
• The output of the reduction algorithm is the instance , of the vertex-
cover problem.
• To complete the proof, we show that this transformation is indeed a
reduction: the graph G has a clique of size k if and only if the graph
has a vertex cover of size |V | - k.
6. Vertex Cover Problem is NP-complete
• Suppose that G has a clique V' V with |V'| = k.
• We claim that V - V' is a vertex cover in .
• Let (u, v) be any edge in Ē.
• Then, (u, v) ∉ E, which implies that at least one of u or v does not
belong to V', since every pair of vertices in V' is connected by an edge
of E.
• Equivalently, at least one of u or v is in V - V', which means that edge
(u, v) is covered by V - V'. Since (u, v) was chosen arbitrarily from Ē,
every edge of Ē is covered by a vertex in V - V'. Hence, the set V - V',
which has size |V | - k, forms a vertex cover for .
6. Vertex Cover Problem is NP-complete
• So we have proven that CLIQUE can be reduced to VERTEX-COVER and
the reduction be done in polynomial time.
• That is to generate the complement graph, we only need a single scan over
all pairs of vertices in the original graph, and generate an edge if there is not
edge between any pair.
• This operation can be done in polynomial time.
• Since CLIQUE can be reduced to VERTEX-COVER in polynomial time, and CLIQUE
is NPH so, VERTEX-COVER∈ NPH

We can conclude that Vertex Cover is NP and also NPH, So vertex


cover is NP Complete

You might also like