Notes-NP Hard and NP Complete Notes
Notes-NP Hard and NP Complete Notes
Notes-NP Hard and NP Complete Notes
NP complete problems are problems whose status is unknown. No polynomial time algorithm has yet
been discovered for any NP complete problem, nor has anybody yet been able to prove that no
polynomial-time algorithm exist for any of them. The interesting part is, if any one of the NP complete
problems can be solved in polynomial time, then all of them can be solved.
P- Polynomial time solving . Problems which can be solved in polynomial time, which take time like O(n),
O(n2), O(n3). Eg: finding maximum element in an array or to check whether a string is palindrome or not.
so there are many problems which can be solved in polynomial time.
NP- Non deterministic Polynomial time solving. Problem which can't be solved in polynomial time like
TSP( travelling salesman problem) or An easy example of this is subset sum: given a set of numbers, does
there exist a subset whose sum is zero?. These problems can’t be solved in polynomial time by
deterministic algorithms but they can be solved in polynomial time by nondeterministic algorithms.
but NP problems are checkable in polynomial time means that given a solution of a problem , we can
check that whether the solution is correct or not in polynomial time.
Many of the problems which do not have polynomial time algorithms are computationally related.
A problem that is NP-complete has the property that it can be solved in polynomial time if and only if all
other NP-complete problems can also be solved in polynomial time. A problem is said to be NP-hard if
every problem in NP-complete set is reducible to it.
Reducibility- If we can convert one instance of a problem A into problem B (NP problem) then it means
that A is reducible to B.
A problem is said to be NP-hard if everything in NP can be transformed in polynomial time into it, and a
problem is NP-complete if it is both in NP and NP-hard. The NP-complete problems represent the
hardest problems in NP. If any NP-complete problem has a polynomial time algorithm, all problems in
NP do. Although a solution to an NP-complete problem can be verified "quickly", there is no known way
to find a solution quickly. That is, the time required to solve the problem using any currently
known algorithm increases rapidly as the size of the problem grows.
The above table shows the time taken by different algorithms with different time complexities.
Nondeterministic algorithms
Algorithms whose every operation is uniquely defined are called deterministic algorithms. Such
algorithms agree with the way programs are executed on a computer. In theory we can remove this
restriction on the outcome of every operation. We can allow algorithms to contain operations whose
outcomes are not uniquely defined but are limited to a specified set of possibilities. The machine
executing such operations is allowed to choose any one of these outcomes subject to a termination
condition. This leads to the concept of a non deterministic algorithm. To specify such algorithms, three
functions are used.
The assignment statement x:= Choice(1,n) could result in x being assigned one of the integers in the
range [1,n]. There is no rule specifying how this choice is to be made. The Failure() and Success() signals
are used to define a computation of the algorithm. Whenever there is a set of choices that leads to a
successful completion, then one such set of choices is always made and the algorithm terminates
successfully. A nondeterministic algorithm terminates unsuccessfully if and only if there exists no set
of choices leading to a success signal.
The computing times of Choice, Success and Failure are taken as O(1). A machine capable of executing
a nondeterministic algorithm in this way is called a nondeterministic machine (which does not exist in
practice).
The above algorithm outputs 0 if and only if there is no j such that A[j]=x; If ‘x’ is in A, Choice will
somehow find its index (how it finds is not known now but some researcher might find it in future). So
only if ‘x’ is not in A the algorithm terminates unsuccessfully.
The time complexity of this nondeterministic algorithm is O(1). It should be observed that if A is
unordered, every deterministic search algorithm (like linear search) has a time complexity of Ω(𝑛)
The nondeterministic sorting algorithm for sorting somehow determines the right position where every
element should be there after sorting ( this is done by the Choice function). The element is then placed
in second array B in that position. Finally in line 11 and 12 it is verified if every element in in B is less
than its next consecutive element in B if not the algorithm terminates unsuccessfully otherwise the
algorithm terminates successfully.
Definition: Any problem for which the answer is either zero or one is called a decision problem. An
algorithm for a decision problem is termed a decision algorithm. Any problem that involves the
identification of an optimal (either minimum or maximum) value of a given cost function is known as an
optimization problem. An optimization algorithm is used to solve an optimization problem.
Line 4 selects ‘k’ vertices from the graph. Lines 6 to 9 are used to find the vertices (distinct vertices) that
define the clique (again the choice function some how finds these vertices). Lines 11 and 12 are used to
check if there is an edge between every pair of vertices in S. If not the algorithm terminates
unsuccessfully otherwise the algorithm terminates successfully. The time complexity of this non
deterministic algorithm is determined by lines 11 and 12 in the algorithm which is O(n2) when adjacency
matrix is used.
A thief is robbing a store and can carry a maximal weight of m into his knapsack. There are n items and
weight of ith item is wi and the profit of selecting this item is pi. What items should the thief take to make
maximum profit?
Input is a Boolean formula. It has variables like x1,x2,x3 …. If a Boolean formula has only 3 unique
variables it is called 3-SAT. In Boolean formula x1,x2,x3… can take 0 or 1.
And – x1^ x2
Or – x1 V x2
So, the satisfiability problem is for what values of X1,X2,X3 a formula like the following is true
X1 X2 X3
3
Since there are 3 variables there are 2 possible combinations of values 0 0 0
0 0 1
If there are n variables we need to check 2n possible combinations of values. 0 1 0
0 1 1
So, finding the answer takes exponential time. 1 0 0
1 0 1
1 1 0
1 1 1
The above diagram shows the state space tree for 3-SAT problem. If satisfiability can be solved in
polynomial time all others can also be solved in polynomial time. If any one of them can be solved in
polynomial all can be solved in polynomial time.
Solution can be expressed as {x1, x2, x3} i.e. which can be one of the following as solution set i.e. either
{0,0,0} or {0,0,1} or {0,1,0} or {0,1,1}… -----------------------------------------------------------------------------(1)
If there are n objects there 2n combinations. This can be solved using state space tree using techniques
like branch and bound or backtracking. ------------------------------------------------------------- (2)
Since Satisfiability is NP-hard and if Satifiability reduces (symbol ∝) to 0/1 Knapsack then 0/1 Knapsack
will be NP-Hard.
Take an instance of satisfiabilty problem and convert it to 0/1 Knapsack problem. Conversion takes
polynomial time. If 0/1 Knapsack problem can be solved in polynomial time, then satisfiability problem
can also be solved in polynomial time. From (1) satisfiability can be reduced to 0/1 Knapsack . So, 0/1
Knapsack is also NP-hard.
Satisfiability is not only NP-hard but also has a non-deterministic polynomial time algorithm so it is NP-
complete.
Cooks theorem:
To find how satisfiability reduces to clique decision problem go through Abdul Bari Youtube channel.