0% found this document useful (0 votes)
8 views

14 Notes

Uploaded by

zenithmini1
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views

14 Notes

Uploaded by

zenithmini1
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 16

P, NP, CoNP, NP hard and NP complete

| Complexity Classes
Last Updated : 03 Oct, 2023



In computer science, there exist some problems whose solutions are not
yet found, the problems are divided into classes known as Complexity
Classes. In complexity theory, a Complexity Class is a set of problems
with related complexity. These classes help scientists to group problems
based on how much time and space they require to solve problems and
verify the solutions. It is the branch of the theory of computation that deals
with the resources required to solve a problem.
The common resources are time and space, meaning how much time the
algorithm takes to solve a problem and the corresponding memory usage.
 The time complexity of an algorithm is used to describe the number of
steps required to solve a problem, but it can also be used to describe
how long it takes to verify the answer.
 The space complexity of an algorithm describes how much memory is
required for the algorithm to operate.
Complexity classes are useful in organising similar types of problems.

Types of Complexity Classes


This article discusses the following complexity classes:
1. P Class
2. NP Class
3. CoNP Class
4. NP-hard
5. NP-complete
P Class
The P in the P class stands for Polynomial Time. It is the collection of
decision problems(problems with a “yes” or “no” answer) that can be
solved by a deterministic machine in polynomial time.
Features:
 The solution to P problems is easy to find.
 P is often a class of computational problems that are solvable and
tractable. Tractable means that the problems can be solved in theory
as well as in practice. But the problems that can be solved in theory but
not in practice are known as intractable.
This class contains many problems:
1. Calculating the greatest common divisor.
2. Finding a maximum matching.
3. Merge Sort
NP Class
The NP in NP class stands for Non-deterministic Polynomial Time. It is
the collection of decision problems that can be solved by a non-
deterministic machine in polynomial time.
Features:
 The solutions of the NP class are hard to find since they are being
solved by a non-deterministic machine but the solutions are easy to
verify.
 Problems of NP can be verified by a Turing machine in polynomial
time.
Example:
Let us consider an example to better understand the NP class. Suppose
there is a company having a total of 1000 employees having unique
employee IDs. Assume that there are 200 rooms available for them. A
selection of 200 employees must be paired together, but the CEO of the
company has the data of some employees who can’t work in the same
room due to personal reasons.
This is an example of an NP problem. Since it is easy to check if the given
choice of 200 employees proposed by a coworker is satisfactory or not i.e.
no pair taken from the coworker list appears on the list given by the CEO.
But generating such a list from scratch seems to be so hard as to be
completely impractical.
It indicates that if someone can provide us with the solution to the
problem, we can find the correct and incorrect pair in polynomial time.
Thus for the NP class problem, the answer is possible, which can be
calculated in polynomial time.
This class contains many problems that one would like to be able to solve
effectively:
1. Boolean Satisfiability Problem (SAT).
2. Hamiltonian Path Problem.
3. Graph coloring.
Co-NP Class
Co-NP stands for the complement of NP Class. It means if the answer to a
problem in Co-NP is No, then there is proof that can be checked in
polynomial time.
Features:
 If a problem X is in NP, then its complement X’ is also in CoNP.
 For an NP and CoNP problem, there is no need to verify all the
answers at once in polynomial time, there is a need to verify only one
particular answer “yes” or “no” in polynomial time for a problem to be in
NP or CoNP.
Some example problems for CoNP are:
1. To check prime number.
2. Integer Factorization.
NP-hard class
An NP-hard problem is at least as hard as the hardest problem in NP and
it is a class of problems such that every problem in NP reduces to NP-
hard.
Features:
 All NP-hard problems are not in NP.
 It takes a long time to check them. This means if a solution for an NP-
hard problem is given then it takes a long time to check whether it is
right or not.
 A problem A is in NP-hard if, for every problem L in NP, there exists a
polynomial-time reduction from L to A.
Some of the examples of problems in Np-hard are:
1. Halting problem.
2. Qualified Boolean formulas.
3. No Hamiltonian cycle.
NP-complete class
A problem is NP-complete if it is both NP and NP-hard. NP-complete
problems are the hard problems in NP.
Features:
 NP-complete problems are special as any problem in NP class can be
transformed or reduced into NP-complete problems in polynomial time.
 If one could solve an NP-complete problem in polynomial time, then
one could also solve any NP problem in polynomial time.
Some example problems include:
1. Hamiltonian Cycle.
2. Satisfiability.
3. Vertex cover.
Complexity
Characteristic feature
Class

P Easily solvable in polynomial time.

NP Yes, answers can be checked in polynomial time.

Co-NP No, answers can be checked in polynomial time.

All NP-hard problems are not in NP and it takes a long


NP-hard
time to check them.

NP-complete A problem that is NP and NP-hard is NP-complete.


P vs NP Problems
Last Updated : 11 Jan, 2024



In the world of computers and math, there's this puzzling


question: Can every problem we quickly check be solved quickly
too? We have two categories: P for problems with quick solutions,
and NP for problems where checking is fast, but solving might not
be. This article explores these P vs NP problems, trying to
understand why some things are easy to check but hard to figure
out, and why it matters for how computers work.
What is P problems?
Polynomial time problems, commonly known as P problems. The
solution of the problem can be found in polynomial time.
Example: Linear search, whose time complexity is O(N), where N
is the input size.
Key characteristics of P problems:
Characteristic Description

Problems in the P complexity class are


decision problems that can be efficiently
solved by a deterministic Turing machine
Definition in polynomial time.

Solutions can be found in polynomial


time, meaning the time required for
computation grows at most as a
Time Complexity polynomial function of the input size.

Efficient algorithms exist for solving P


problems, making them computationally
tractable and practical for a wide range of
Algorithmic Solutions applications.

The correctness of a solution can be


verified in polynomial time, ensuring that
the proposed solution is correct without
Polynomial Verification significant computational effort.
Characteristic Description

Examples of P problems include sorting


algorithms, searching algorithms, and
various problems with known efficient
Examples solutions.

What is NP problems?
Nondeterministic polynomial-time problems, commonly
known as NP problems. These problems have the special
property that, once a potential solution is provided, its
correctness can be verified quickly. However, finding the solution
itself may be computationally difficult.
Example: A well-known example of NP problems is prime
factorization. We can verify a factor of an integer in polynomial
time. However, we don’t know any polynomial time algorithm to
factorize a given integer.
Key characteristics of NP problems:
Characteristic Description

Nondeterministic Polynomial time


problems are computational problems for
which solutions can be verified efficiently
Definition in polynomial time.

Once a potential solution is provided, its


correctness can be verified quickly using
a nondeterministic algorithm in
Verification polynomial time.

While verification is efficient, finding a


solution may be computationally
challenging, with no known polynomial
Solution Computation time algorithm for general instances.

The Traveling Salesman Problem


Example (TSP) is a classic NP problem.

Difference between P vs NP problems:


Here's a detailed explanation of the differences between P and NP
problems:
Feature P Problems NP Problems

Efficient verification,
Efficiently solvable in
solution may not be found
polynomial time.
Solvability efficiently.

Efficient verification
algorithms are known, but
Polynomial time
efficient solution
algorithms are known.
algorithms are not
Time Complexity guaranteed.

Solutions can be found Solutions, once proposed,


Nature of Solutions efficiently. can be verified efficiently.

Decision or Often decision problems Can be decision or


Optimization (yes/no answers). optimization problems.

It is unknown whether NP
P is a subset of NP. is a proper subset of P or if
Known Relationship they are equal.

NP problems are considered


computationally hard; no
Problems in P are
efficient algorithm is
considered efficiently
currently known, making
solvable in practice.
them potentially impractical
Practical Implications for large instances.

One of the most significant


open problems in computer
science is whether P equals
P vs NP question is open.
NP. If P equals NP, all
problems in NP would also
P vs NP Question be in P.

Traveling
Sorting, searching,
Salesman, Boolean
shortest path problems.
Example Satisfiability.

Related Articles:
Introduction to NP-Complete Complexity
Classes
Last Updated : 15 May, 2024



NP-complete problems are a subset of the larger class of NP


(nondeterministic polynomial time) problems. NP problems
are a class of computational problems that can be solved in
polynomial time by a non-deterministic machine and can be
verified in polynomial time by a deterministic Machine. A
problem L in NP is NP-complete if all other problems in NP can
be reduced to L in polynomial time. If any NP-complete problem
can be solved in polynomial time, then every problem in NP can
be solved in polynomial time. NP-complete problems are the
hardest problems in the NP set.
A decision problem L is NP-complete if it follow the below two
properties:
1. L is in NP (Any solution to NP-complete problems can be
checked quickly, but no efficient solution is known).
2. Every problem in NP is reducible to L in polynomial time
(Reduction is defined below).
A problem is NP-Hard if it obeys Property 2 above and need not
obey Property 1. Therefore, a problem is NP-complete if it is
both NP and NP-hard.

NP-Complete Complexity Classes


Decision vs Optimization Problems
NP-completeness applies to the realm of decision problems. It
was set up this way because it’s easier to compare the difficulty
of decision problems than that of optimization problems. In
reality, though, being able to solve a decision problem in
polynomial time will often permit us to solve the corresponding
optimization problem in polynomial time (using a polynomial
number of calls to the decision problem). So, discussing the
difficulty of decision problems is often really equivalent to
discussing the difficulty of optimization problems.
For example, consider the vertex cover problem (Given a graph,
find out the minimum sized vertex set that covers all edges). It is
an optimization problem. The corresponding decision problem is,
given undirected graph G and k, is there a vertex cover of size k?
What is Reduction?
Let L1 and L2 be two decision problems. Suppose
algorithm A2 solves L2. That is, if y is an input for L2 then
algorithm A2 will answer Yes or No depending upon
whether y belongs to L2 or not.
The idea is to find a transformation from L1 to L2 so that
algorithm A2 can be part of algorithm A1 to solve L1.

Learning reduction, in general, is very important. For example, if


we have library functions to solve certain problems and if we can
reduce a new problem to one of the solved problems, we save a
lot of time. Consider the example of a problem where we have to
find the minimum product path in a given directed graph where
the product of the path is the multiplication of weights of edges
along the path. If we have code for Dijkstra’s algorithm to find the
shortest path, we can take the log of all weights and use
Dijkstra’s algorithm to find the minimum product path rather than
writing a fresh code for this new problem.
How to prove that a given problem is NP-
complete?
From the definition of NP-complete, it appears impossible to prove
that a problem L is NP-Complete. By definition, it requires us to
that show every problem in NP is polynomial time reducible to L.
Fortunately, there is an alternate way to prove it. The idea is to
take a known NP-Complete problem and reduce it to L. If a
polynomial-time reduction is possible, we can prove that L is NP-
Complete by transitivity of reduction (If an NP-Complete problem
is reducible to L in polynomial time, then all problems are
reducible to L in polynomial time).

What was the first problem proved as NP-


Complete?
There must be some first NP-Complete problem proved by the
definition of NP-Complete problems. SAT (Boolean satisfiability
problem) is the first NP-Complete problem proved by Cook (See
CLRS book for proof).
It is always useful to know about NP-Completeness even for
engineers. Suppose you are asked to write an efficient algorithm
to solve an extremely important problem for your company. After
a lot of thinking, you can only come up exponential time approach
which is impractical. If you don’t know about NP-Completeness,
you can only say that I could not come up with an efficient
algorithm. If you know about NP-Completeness and prove that the
problem is NP-complete, you can proudly say that the polynomial-
time solution is unlikely to exist. If there is a polynomial-time
solution possible, then that solution solves a big problem of
computer science many scientists have been trying for years.
NP-Complete problems and their proof for NP-
Completeness.
1. Prove that SAT is NP Complete
2. Prove that Sparse Graph is NP-Complete
3. Prove that KITE is NP-Complete
4. Prove that Hamiltonian Cycle is NP-Complete
5. Subset Sum is NP Complete
6. Prove that Collinearity Problem is NP Complete
7. Set partition is NP complete
8. Hitting Set problem is NP Complete
9. 3-coloring is NP Complete
10. Set cover is NP Complete
11. Optimized Longest Path is NP Complete
12. Double SAT is NP Complete
13. Prove that 4 SAT is NP complete
14. Prove that Dense Subgraph is NP Complete by
Generalisation
15. Prove that a problem consisting of Clique and Independent
Set is NP Complete
16. Prove that Almost-SAT is NP Complete
17. Prove that MAX-SAT is NP Complete
18. Prove Max2SAT is NP-Complete by Generalisation
19. Subset Equality is NP Complete
20. Hitting Set problem is NP Complete
NP-Hard Class
Last Updated : 19 Sep, 2023



A ‘P‘ problem is said to be NP-Hard when all ‘Q’ belonging in NP


can be reduced in polynomial time ( where k is some constant)
to ‘P’ assuming a solution for ‘P’ takes 1 unit time.
NP-Hard is a computational complexity theory that acts as a
defining property for the class of problems that are “at least as
hard as the hardest problems in NP”. P’s solution can be used to
solve ‘Q’ in polynomial time. For example, the Subset Sum
Problem, and Turing Halting problem is an NP-Hard problem.

Features of NP-Hard:
 All NP-hard problems are not in NP.
 It takes a long time to check them. This means if a solution for
an NP-hard problem is given, it takes a long time to check
whether it is right.
 A problem ‘P’ is NP-hard if there is a polynomial-time reduction
from ‘Q’ to ‘P’ for every problem ‘Q’ in NP.
Real-World Applications:
 Approximate Computing: In approximate computing, the
goal is to trade off accuracy for computational efficiency. Many
optimization problems in this domain, like optimizing
approximation algorithms, can be NP-hard. This implies that
finding optimal solutions might be impractical, and researchers
need to devise heuristic or approximation methods to get
close-to-optimal results.
 Cryptography: While most cryptographic algorithms involve
polynomial-time operations, certain cryptographic problems,
such as solving certain lattice-based problems in post-quantum
cryptography, are believed to be NP-hard. This is crucial for
ensuring the security of cryptographic systems.
 Data Mining: NP-hard problems often arise in data mining
tasks like clustering, feature selection, and association rule
mining. As data mining involves finding patterns and
relationships in large datasets, it’s common to encounter
optimization problems that are difficult to solve exactly.
 Routing/Vehicle Routing: Routing problems, like finding the
shortest path or optimizing routes for vehicles, are often NP-
hard. These problems arise in logistics, transportation, and
networking. Practical solutions involve approximation
algorithms and heuristics due to the complexity of real-world
scenarios.
Problems under NP-Hard Class:
 Traveling Salesman Problem (TSP)
 Subset Sum problem
 Knapsack Problem
 Boolean Satisfiability Problem (SAT)
 Graph Coloring Problem
 Independent Set Problem
 3-SAT Problem
 Vertex Cover Problem
 Bin Packing Problem
 Hamiltonian Cycle Problem
 Partition Problem
 Knights Tour Problem
 Quadratic Assignment Problem
 Clique Problem
Difference between NP hard and NP
complete problem
Last Updated : 05 Jan, 2023



Prerequisite: NP-Completeness
NP Problem:
The NP problems set of problems whose solutions are hard to find
but easy to verify and are solved by Non-Deterministic Machine in
polynomial time.
NP-Hard Problem:
A Problem X is NP-Hard if there is an NP-Complete problem Y,
such that Y is reducible to X in polynomial time. NP-Hard problems
are as hard as NP-Complete problems. NP-Hard Problem need not
be in NP class.
If every problem of NP can be polynomial time reduced to it called
as NP Hard.
A lot of times takes the particular problem solve and reducing
different problems.
example :
1. Hamiltonian cycle .
2. optimization problem .
3. Shortest path
NP-Complete Problem:
A problem X is NP-Complete if there is an NP problem Y, such that
Y is reducible to X in polynomial time. NP-Complete problems are
as hard as NP problems. A problem is NP-Complete if it is a part of
both NP and NP-Hard Problem. A non-deterministic Turing
machine can solve NP-Complete problem in polynomial time.
A problem is np-complete when it is both np and np hard
combines together.
this means np complete problems can be verified in polynomial
time.
Example:
1. Decision problems.
2. Regular graphs.
Difference between NP-Hard and NP-Complete:
NP-hard NP-Complete

NP-Hard problems(say X) can be


NP-Complete problems can be solved by a
solved if and only if there is a NP-
non-deterministic Algorithm/Turing
Complete problem(say Y) that can be
Machine in polynomial time.
reducible into X in polynomial time.

To solve this problem, it do not have To solve this problem, it must be both NP
to be in NP . and NP-hard problems.

Time is unknown in NP-Hard. Time is known as it is fixed in NP-Hard.

NP-Complete is exclusively a decision


NP-hard is not a decision problem.
problem.

Not all NP-hard problems are NP-


All NP-complete problems are NP-hard
complete.

Do not have to be a Decision problem. It is exclusively a Decision problem.

It is optimization problem used. It is Decision problem used.

Example: Determine whether a graph has a


Example: Halting problem, Vertex Hamiltonian cycle, Determine whether a
cover problem, etc. Boolean formula is satisfiable or not,
Circuit-satisfiability problem, etc.

You might also like