Daa R20 Unit 5
Daa R20 Unit 5
Daa R20 Unit 5
Decision Problems:
Example-1:
o Consider graph G = (V, E), find if there is a path from vertex ‘X’ to vertex ‘Y’ whose
length is at most 10.
Observation:
The above example problems are called decision problems, because the problem has
solution (answer), i.e. is either YES or NO.
Some decision problems can be solved efficiently, using time polynomial to the size of
the input of that problem
Generally ‘P’ is used to denote the set of all these polynomial-time solvable problems
Example:
Find the shortest path from vertex ‘X’ to vertex ‘Y’ for a graph G.
o First apply Dijkstra's algorithm and then give the correct answer
One more categorization of decision problems is to observe if the problem can be verified
in time polynomial toward the input size.
Example:
For the same graph G, if there is a path from ‘X’ to ‘Y’ with length is less than 10, then:
1. Find the sequence of vertices without repetition in any path from ‘X’ to ‘Y’ with
length is less than 10.
3. Given a set of numbers, can be divide them into two groups such that their sum is the
same (equal)?
For some problems, directly we cannot provide solution in polynomial time. Such
problems can be represented by ‘NP’.
o ‘P’ is polynomial-time
Examples:
Polynomial problems: searching, sorting and string editing – these problems need the
time O(log n), O(n log n) and O(mn) respectively.
NP-complete problems:
Example:
The decision versions of the Travelling sales person, graph coloring problem and
Hamiltonian cycle problem are NP-complete problems.
All NP-complete problems are NP-hard but some NP-hard problems are not known to be
NP-complete.
Deterministic algorithm:
The class ‘P’ consists of all problems that can be solved in polynomial time, O(Nk), by
deterministic computers.
A deterministic algorithm generates the same output for a problem instance every time
the algorithm is run
search(element, A, i)
if (A[i] == x)
then return i;
else
Let us say, input A = {7, 5, 3, 8, 9, 3} and x = 9 then the search begins from 7, then 5,
then 3 and so on until a successful search or element not found.
The complexity of the algorithm is O(n) since the input is not sorted.
Non-deterministic computer:
A nondeterministic computer has more than one output state and it “chooses” the correct
one, is known as “nondeterministic choice”.
o Stage-I: a method makes a guess about the possible solution for problem A.
o Stage-II: a method checks if the guessed solution is really a solution for problem
A.
STAGE-1:
NonDeterministicSearch(x, A)
{
i := NDS(x, A, 1); // calling function NDS()
return i;
STAGE-2:
NDS(x, A, i)
if (A[i] == x) then
return i;
else
Example-2:
Nondeterministic algorithm to sort the given element of an array A[1..n] is as shown below:
Algorithm NDSORT(A, B)
for i := 1 to n do
if (B[j]!= 0) then
failure();
endfor
write(B);
success();
Note:
Many optimization problems can be stated as decision problems with the property that:
o The decision problem can be solved in polynomial time if and only if the
optimization problem can be solved in polynomial time.
Time complexity of a nondeterministic algorithm is O(f(n)), for all input size ‘n’ that
result in successful completion and the time required is at most cf(n) where ‘c’ and ‘n0’
are positive constants
Example-1:
Algorithm NDKnapsack( p, w, n, m, r, x)
for i := 1 to n do
failure();
else
success();
The time complexity of this algorithm appears to be O(n)+O(m), where, ‘m’ is the length of
the input.
Hence, the algorithm’s time complexity is polynomial in the size of the input.
Example-2:
A formula is in conjunctive normal form (CNF) if it is of the form ci where, clause ci = lij
A formula is in disjunctive normal form (DNF) if it is of the form ci where, clause ci = lij
For example, (x3 ) (x1 ) is in CNF, then the DNF is (x1 x2) (x3 )
The satisfiability (SAT) problem is to search out whether or not a formula is true for a few
assignment of truth values to the variables.
Algorithm NDSAT(E, n)
success();
else
failure();
}
NP-complete problems are the “hardest” problems in NP, if any NP-complete problem
can be solved in polynomial time, then all NP-complete problems can be solved in
polynomial time and in fact every problem in NP can be solved in polynomial.
Reducibility:
NP-Hard problems:
NP-Complete problems:
The decision problem HALTING returns TRUE, if, for a given input ‘I’ and a given
deterministic algorithm ‘A’; the algorithm ‘A’ terminates, otherwise it loops forever. There is no
algorithm of any type to solve HALTING.
o Construct an algorithm ‘A’ whose input is the encoding of a boolean circuit ‘B’
o ‘A’ tries all possible (2n, where ‘n’ is the number of input lines to the circuit)
combinations to simulate the circuit, and looks to see if the output is 1 for any of
the combinations. If it is, then ‘A’ halts. If not, then ‘A’ goes into an infinite loop
It is clear from the construction that ‘A’ halts exactly when ‘B’ is satisfiable, and loops
forever exactly when ‘B’ is not satisfiable.
Note: We only need to consider the length of the code in algorithm ‘A’ and not it’s
running time, in order to have polynomial time reducibility.
We know that the HALTING problem is undecidable, i.e., there is no algorithm to solve
HALTING. By using this fact, we can prove by contradiction that HALTING is not in NP.
Let us say a polynomial time verification algorithm for HALTING, given an input string
‘x’ and an algorithm ‘AHNP’ such that A(x, y) = 1, with ‘x’ being an encoding of an
instance of HALTING that returns TRUE and ‘y’ being a certificate that is polynomial
length in ‘x’
Given such an AHNP(x, y), we can look at the input string xhalt whenever AHNP(xhalt,
y)=1 and conclude that xhalt includes the representation of an algorithm Ahalt that will halt
The algorithm AHNP can be modified to recognize encodings of all algorithms ‘A’ that do
terminate. But this contradicts the assumption that HALTING is undecidable.
Cook’s theorem
The Cook's theorem (Cook–Levin theorem) states that the boolean satisfiability
problem is NP-complete, i.e., any problem in NP can be reduced in polynomial time by
a deterministic Turing machine.
Proof:
Let us say a NP decision problem D. And polynomial function P and a Turing machine M for
given any instance I (of length ‘n’) of D, together with a candidate certificate c, will check in
time no greater than P(n).
Let us consider the machine M with q states (0, 1, 2, .., q-1) and a tape alphabet (a1, a2, ..,
as)
Initially, the tape is inscribed with the problem instance on the squares 1, 2, .., n and the
putative certificate on the squares –m, .., -2, -1
Assume that, the machine halts scanning square 0, and that the symbol in this square at
that stage will be a1 if and only if the candidate certificate is a true certificate
Here, m <= P(n); since with a problem instance of length ‘n’ the computation is
completed in at most P(n) steps; during this process, the Turing machine head cannot
move more than P(n) steps to the left of its starting point
1. For i = 0, 1, .., P(n) and j = 0, 1, 2, .., q-1 the proposition Qij says that after ‘i‘
computation steps, M is in state ‘j’.
2. For i = 0, 1, .., P(n), j = -P(n), …, P(n) and k =1, 2, ..,s; the proposition Sijk says that
after ‘i’ computation steps, square ‘j’ of the tape contains the symbol ak.
3. For i = 0, 1, .., P(n), j = -P(n), …, P(n), the proposition Tij says that after ‘i’ computation
steps, the machine M is scanning square ‘j’ of the tape.
1. At each computation step, M is in at least one state. For each i = 0, 1, .., P(n) we have
the clause Qi0 V Qi1 V … Qi(q-1) giving (P(n)+1)q = O(P(n)) literals altogether.
2. At each computation step, M is in at most one state. For each i = 0, 1, .., P(n) and for
each pair j, k of distinct states, we have the clause (Qij ^ Qik), giving a total of q*(q-
1)*(P(n) + 1) = O(P(n)) literals altogether.
3. At each step, each tape square contains at least one alphabet symbol. For each i = 0, 1, ..,
P(n), and -P(n) j P(n) we have the clause Sij1 v Sij2 v … Sijs; giving
(P(n)+1)*(2P(n)+1)*s = O(P(n)2) literals altogether.
4. In every step, every tape square has at most one alphabet symbol. For every i = 0, 1, ..,
P(n), and -P(n) j P(n), and every distinct pair ak,al of symbols we have the clause (Sijk ^
Sijl); giving a total of (P(n)+1)*(2P(n)+1)*s*(s-1) = O(P(n)2) literals altogether.
5. In every step, the tape is scanning at least one square. For each i = 0, 1, .., P(n), we have
the clause Ti(-P(n)) v Ti(1-P(n)) v … Ti(P(n)-1) v TiP(n); giving (P(n)+1)*(2P(n)+1)=O(P(n)2)
literals altogether.
6. In every step, the tape is scanning at most one square. For each i = 0, 1, .., P(n) and each
distinct pair j, k of tape squares from -P(n) to P(n), we have the clause (Tij ^ Tik); giving a
total of 2P(n)*(2P(n)+1)*(P(n)+1)=O(P(n)3) literals.
7. Initially, the machine is in state 1 scanning square 1. This is expressed by the two clauses
Q01 and T01 giving just two literals.
8. After the first step, at every step the configuration is determined the preceding step by the
functions T, U, and D. For every i = 0, 1, .., P(n), -P(n) j P(n), k = 0, 1, .., q-1 and l = 1,
2, .., s we have the clauses
The fourth of these clauses ensures that the contents of any tape square ther
These clauses contribute a total of (12s + 3)(P(n) + 1)(2P(n) + 1)q = O(P(n)2) literals.
9. Initially, the string ai1 ai2 … ain defining the problem instance ‘I’ is inscribed on squares
1, 2, …, n of the tape. This is expressed by the ‘n’ clauses S01i1, S02i2, …, S0nin a total of ‘n’
literals.
10. By the P(n)th step, the machine has reached the halt state, and is then scanning square0,
which contains the symbol a1. This is expressed by the three clauses QP(n)0, SP(n)01 and
TP(n)0 giving another 3 literals.
Altogether the number of literals involved in these clauses is O(P(n)3). Thus, the procedure for
setting up these clauses, given the original machine M and the instance ‘I’ of problem D, can be
accomplished in polynomial time.
We must now show that we have succeeded in converting the problem D into SAT.
This means that there is some sequence of symbols that can be placed initially on squares
-P(n), -P(n-1), .., -1 of the tape so that all the clauses above are satisfied
Conversely, suppose ‘I’ is a negative instance of D. In that case there is no certificate for ‘I’,
which means that whatever symbols are placed on squares -P(n), -P(n-1), .., -1 of the tape, when
the computation halts the machine will not be scanning a1 on square0. This means that the set of
clauses above is not satisfiable, and hence constitutes a negative instance of SAT.
Thus from the instance ‘I’ of problem D is constructed in polynomial time, a set of
clauses which constitute a positive instance of SAT if and only ‘I’ is a positive instance
of D.
And since D was an arbitrary NP problem it follows that any NP problem can be
converted to SAT in polynomial time.