Mid 2 Daa
Mid 2 Daa
I) NP Hard
II) NP Complete.
NP-hard class
An NP-hard problem is at least as hard as the hardest problem in NP and it is the class of the
problems such that every problem in NP reduces to NP-hard.
Features:
1. Halting problem.
2. Qualified Boolean formulas.
3. No Hamiltonian cycle.
For example:
The halting problem is famously NP-hard but not NP-complete. This means it's at least as hard
as any problem in the NP class, but unlike NP-complete problems, it isn't guaranteed to have a
solution verifiable in polynomial time.
• Left Gear (Halter): This gear represents a hypothetical program that can solve the
halting problem. It takes two inputs: a program (right gear) and its input (text on a strip).
• Right Gear (Verifier): This gear represents a program trying to solve an NP problem
(e.g., Boolean satisfiability). It has a slot for the input related to the NP problem.
NP-complete class
A problem is NP-complete if it is both NP and NP-hard. NP-complete problems are the hardest
problems in NP.
In mathematics, if all statements of a system can be explained, it is said to be complete.
NP complete problems are powerful problems that can solve all problems of class NP and
hence these problems are said to be NP complete.
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.
For Example:
To prove VC is NP, find a verifier which is a subset of vertices which is VC and that can be
verified in polynomial time. For a graph of n vertices it can be proved in O(n2). Thus, VC is
NP.
Now consider the “clique” problem which is NPC and reduce it into VC to prove NPC. Clique
of a graph G is a subset of vertices such that these vertices form a complete subgraph in the
given graph G.
The two graphs titles (a) and (b) are given below for the VC problem −
Consider graph (a), here clique is {a,b,c,d}.
For the graph (b), we can say that the vertex cover is {s,t} which covers all the edges of the
(b). This {s,t} = {a,b,c,d,s,t} – {Clique of (a)} Thus conversely, we can say that we can reduce
clique into the VC problem and conversely can find both VC and clique of a given undirected
graph. This means VC is NP-complete reducible.
Given a set of numbers, find all possible subsets whose sum equals a given target sum.
This problem is often solved using techniques such as backtracking, dynamic programming, or
bitmasking.
Que: Types of Complexity Classes
Complexity classes categorize computational problems based on their inherent difficulty and
the amount of computational resources required to solve them. Here are some common
complexity classes:
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:
3. 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
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.
2. Problems of NP can be verified by a Turing machine in polynomial time.
Example:
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.
Complexity
Characteristic feature
Class
All NP-hard problems are not in NP and it takes a long time to check
NP-hard
them.
Cook’s theorem
An SAT(formula-SAT or simply SAT) takes a Boolean expression F and checks whether the
given expression(or formula) is satisfiable.
A Boolean expression is said to be satisfactory for some valid assignments of variables if the
evaluation comes to be true.
Boolean variable: A variable, say x, that can have only two values, true or false, is called a
boolean variable.
Literal: A literal can be a logical variable, say x, or the negation of it, that is x or x̄; x is called
a positive literal, and x̄ is called the negative literal.
Expressions: One can combine all the preceding clauses using a Boolean operator to form an
expression.
CNF form: An expression is in CNF form(conjunctive normal form) if the set of clauses are
separated by an AND (^), operator, while the literals are connected by an OR (v) operator.
3-CNF-SAT(3-SAT)
This problem contains the expression is in a conjunctive normal form and that every clause
should contain exactly three literals.
This problem is also about assigning n assignments of truth values to n variables of the Boolean
expression such that the output of the expression is true.
In simple words, given an expression in 3-CNF, a 3-SAT problem is to check whether the given
expression is satisfiable.
If we consider,
f = (x1 V x̄2 V x3) ∧ (x1 V x2 V x̄3 )
it is the Boolean satisfiability problem of 3 literals.. So every literal can take either 0 or 1 as
input Values.
i.e., x1 = 0 or 1; x2 = 0 or 1; x3 = 0 or 1.. we are having 3 literals and every literal with 2 inputs
. So we can have a total of 2n = 23 = 8 possible outcomes.
Create an empty path array and add vertex 0 to it. Add other vertices, starting from the
vertex 1. Before adding a vertex, check for whether it is adjacent to the previously added
vertex and not already added. If we find such a vertex, we add the vertex as part of the
solution. If we do not find a vertex then we return false.
Illustrations:
Let’s find out the Hamiltonian cycle for the following graph:
• As cycle is not found in path {0, 3, 1, 4, 2}. So, return from node 2, node 4.
• Now, explore another option for node 1 (i.e node 2)
• When it hits the base condition again check for Hamiltonian cycle
• As node 4 is not the neighbour of node 0, again cycle is not found then return.