Complexity Classes
Complexity Classes
For example, consider the problem of counting frequencies of all elements in an array of positive numbers.
A pseudo-polynomial time solution for this is to first find the maximum value, then iterate from 1 to
maximum value and for each value, find its frequency in array. This solution requires time according to
maximum value in input array, therefore pseudo-polynomial. On the other hand, an algorithm whose time
complexity is polynomial in the number of elements in array (not value) is considered as polynomial time
algorithm.
Some NP-Complete problems have pseudo-polynomial time solutions. For example, Dynamic
Programming Solutions of 0-1 Knapsack, Subset-Sum and Partition problems are Pseudo-Polynomial. NP
complete problems that can be solved using a pseudo-polynomial time algorithms are called weakly NP-
complete.
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.
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:
2. 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.
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:
1. 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.
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:
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:
2. 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.
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:
2. 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.
3. A problem A is in NP-hard if, for every problem L in NP, there exists a polynomial-time reduction
from L to A.
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:
1. NP-complete problems are special as any problem in NP class can be transformed or reduced into
NP-complete problems in polynomial time.
2. If one could solve an NP-complete problem in polynomial time, then one could also solve any NP
problem in polynomial time.
Many problems are hard to solve, but they have the property that it easy to authenticate the solution if
one is provided.
Consider the Hamiltonian cycle problem. Given an undirected graph G, does G have a cycle that visits each
vertex exactly once? There is no known polynomial time algorithm for this dispute.
Note: - It means you can't build a Hamiltonian cycle in a graph with a polynomial time
even if there is no specific path is given for the Hamiltonian cycle with the particular
vertex, yet you can't verify the Hamiltonian cycle within the polynomial time
Fig:Hamiltonian Cycle
Let us understand that a graph did have a Hamiltonian cycle. It would be easy for someone to convince of
this. They would similarly say: "the period is hv3, v7, v1....v13i.
We could then inspect the graph and check that this is indeed a legal cycle and that it visits all of the
vertices of the graph exactly once. Thus, even though we know of no efficient way to solve the Hamiltonian
cycle problem, there is a beneficial way to verify that a given cycle is indeed a Hamiltonian cycle.
Definition of Certificate: - A piece of information which contains in the given path of a vertex is known
as certificate.
Reductions:
The class NP-complete (NPC) problems consist of a set of decision problems (a subset of
class NP) that no one knows how to solve efficiently. But if there were a polynomial
solution for even a single NP-complete problem, then every problem in NPC will be
solvable in polynomial time. For this, we need the concept of reductions.
Suppose there are two problems, A and B. You know that it is impossible to solve problem
A in polynomial time. You want to prove that B cannot be explained in polynomial time.
We want to show that (A ∉ P) => (B ∉ P)
Consider an example to illustrate reduction: The following problem is well-known to be
NPC:
3-color: Given a graph G, can each of its vertices be labeled with one of 3 different colors
such that two adjacent vertices do not have the same label (color).
Coloring arises in various partitioning issues where there is a constraint that two objects
cannot be assigned to the same set of partitions. The phrase "coloring" comes from the
original application which was in map drawing. Two countries that contribute a common
border should be colored with different colors.
It is well known that planar graphs can be colored (maps) with four colors. There exists a
polynomial time algorithm for this. But deciding whether this can be done with 3 colors
is hard, and there is no polynomial time algorithm for it.