Sei sulla pagina 1di 6

What are NP, P, NP-complete and NP-Hard problems?

P:
P stands for Polynomial time
P is set of decision problems that can be solved by a deterministic Turing machine in Polynomial
time.
Ex: testing whether a number is prime.
NP:
NP stands for Non-deterministic Polynomial time
NP is set of decision problems that can be solved by a Non-deterministic Turing Machine in
Polynomial time.
P is subset of NP (any problem that can be solved by deterministic machine in polynomial time can
also be solved by non-deterministic machine in polynomial time).
Informally, NP is set of decision problems which can be solved by a polynomial time via a Lucky
Algorithm, a magical algorithm that always makes a right guess among the given set of choices
(Source Ref 1).
EX:
NP-complete:
NP-complete problems are the hardest problems in NP set. A decision problem L is NP-complete if:
1) L is in NP (Any given solution for NP-complete problems can be verified quickly, but there is no
efficient known solution).
2) Every problem in NP is reducible to L in polynomial time .
Ex:vector cover problem
NP-Hard :
A problem is NP-Hard if it follows property 2 mentioned above, doesnt need to follow property 1.
Therefore, NP-Complete set is also a subset of NP-Hard set.
EX: traveling salesman

What is Reduction:
Let L1 and L2# be two decision problems. Suppose algorithm A2# solves L2#. That is, if y is an
input for L#2 then algorithm A2# will answer Yes or No depending upon whether y belongs to# L#2
or not.
The idea is to find a transformation from L1 to L2# so that the algorithm A#2 can be part of an
algorithm A1 to solve L1.

Learning reduction in general is very important. For example, if we have library functions to solve
certain problem 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 minimum product path in a given
directed graph where product of path is multiplication of weights of edges along the path. If we
have code for Dijkstras algorithm to find shortest path, we can take log of all weights and use
Dijkstras algorithm to find the minimum product path rather than writing a fresh code for this new
problem.

TSP: (Traveling Salesman Problem)


Design the shortest , or minimal cost , route for a salesman who wants to travel EVERY cities
ONLY ONCE and ,lastly, backs to home city.
In graph, we need to find a tour that starts at a node, visits every other node exactly once, and
returns to the starting node.
Definition: Find a path through a weighted graph which starts and ends at the same vertex, includes
every other vertex exactly once, and minimizes the total cost of edges.
TSP is classify as NP-complete problem, that means no polynomial algorithm
Triangle-Inequality: The least distant path to reach a vertex j from i is always to reach j directly
from i, rather than through some other vertex k (or vertices),
i.e., dis(i, j) <= dis(i, k) + dist(k, j).
we can design an approximate algorithm for TSP that returns a tour whose cost is never more than
twice the cost of an optimal tour. The idea is to use Minimum Spanning Tree (MST). Following is
the MST based algorithm.
Algorithm:
1)

Let choose '1' as the starting and ending point for salesman.

2)

Construct MST from with '1' as root using Prims Algorithm.

3)

List vertices visited in preorder walk of the constructed MST and add '1' at the end.

example.
1. The first diagram is the given graph.
2. The second diagram shows MST constructed with 1 as root.
3. The preorder traversal of MST is 1-2-4-3.
4. Adding 1 at the end gives 1-2-4-3-1 which is the output of this algorithm.

Solution:
1. The first diagram is the given graph.

2. MST :

3. preorder traversal : 1-2-4-3 so the output is 1-2-4-3-1


4. output is:
So total cost is : 10+25+30+15=80

Vertex cover problem:


vertex cover of an undirected graph is a subset of its vertices such that for every edge (u, v) of the
graph, either u or v is in vertex cover. Although the name is Vertex Cover, the set covers all
edges of the given graph. Given an undirected graph, the vertex cover problem is to find minimum
size vertex cover.
Following are some examples.

Vertex Cover Problem is a known NP Complete problem.


Approximate Algorithm for Vertex Cover:
1) Initialize the result as {}
2) Consider a set of all edges in given graph. Let the set be E.
3) Do following while E is not empty
a) Pick an arbitrary edge (u, v) from set E and add 'u' and 'v' to result
b) Remove all edges from E which are either incident on u or v.
4) Return result

Rabinkrap algorithm:
1. It is a string searching algorithm created by Richard M. Karp and Michael O. Rabin (1987)
2. It uses hashing to find any one of a set of pattern strings in a text
3. Given a text txt[0..n-1] and a pattern pat[0..m-1], search and prints all occurrences of
pat[] in txt[]. You may assume that n > m.
Ex:
Examples:
1) Input:
txt[] = "THIS IS A TEST TEXT"
pat[] = "TEST"
Output:
Pattern found at index 10
2) Input:
txt[] = "AABAACAADAABAAABAA"
pat[] = "AABA"
Output:
Pattern found at index 0

Pattern found at index 9


Pattern found at index 13
Algorithm:

Example:
For example, suppose we are matching to a 5 digit pattern P = 31415 in radix d = 10 we choose q =
13, and we are comparing to string 14142. The hash code for the pattern is 7, and for the target is 8,
as shown in the previous example. The hashes do not match, so the two patterns cannot be the same.

Potrebbero piacerti anche