Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
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.
Let choose '1' as the starting and ending point for salesman.
2)
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 :
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
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.