It 503 (a) Toc Unit 5 Notes (1)
It 503 (a) Toc Unit 5 Notes (1)
Unit V
Syllabus: Turing machines - basics and formal definition, language acceptability by TM, examples of TM,
variants of TMs – multi-tape TM, NDTM, Universal Turing Machine, offline TMs, equivalence of single
tape and multi-tape TMs. Recursive and recursively enumerable languages, decidable and undecidable
problems – examples, halting problem, reducibility. Introduction of P, NP, NP complete, NP hard
problems and Examples of these problems.
____________________________________________________________________________________
Unit Objective: Designing Turing machines, understanding the working of various types of Turing
machines and study P and NP type problem.
Unit-V: Turing Machine
Introduction:
Turing machine is considered as a simple model of a real computer. Turing machines can be used to
accept all context-free languages, but also languages such as L = {a mbncmn: m ≥ 0, n ≥ 0} which is not
class of language comes under regular and context free. Every problem that can be solved on a real
computer can also be solved by a Turing machine.
B B a a a b b a B B
Tape Head
Γ is a finite set of tape alphabet; this alphabet also contains the blank symbol B, and Σ ⊆ Γ,
Σ is a finite set of input alphabet; the blank symbol B is not contained in Σ,
q100 |- 0q10 |- 00q1 |- 0q201 |- q2001 |- q2b001 |- q3001 |- q401 |- 0q41 |- 01q4 |- 010q5 |- 01q200 |-*-
q5000
Example 2:
Design a Turing Machine that reads a string representing a binary number and erases all leading 0’s in
the string. However, if the string comprises of only 0’s, it keeps one 0.
Solution:
Let us assume that the input string is terminated by a blank symbol, B, at each end of the string.
The Turing Machine, M, can be constructed by the following moves:
Let q0 be the initial state.
If M is in q0, on reading 0, it moves right, enters the state q1 and erases 0. On reading 1, it
enters the state q2 and moves right.
If M is in q1, on reading 0, it moves right and erases 0, i.e., it replaces 0’s by B’s. On reaching the
leftmost 1, it enters q2 and moves right. If it reaches B, i.e., the string comprises of only 0’s, it
moves left and enters the state q3.
If M is in q2, on reading either 0 or 1, it moves right. On reaching B, it moves left and enters the
state q4. This validates that the string comprises only of 0’s and 1’s.
If M is in q3, it replaces B by 0, moves left and reaches the final state qf.
If M is in q4, on reading either 0 or 1, it moves left. On reaching the beginning of the string, i.e.,
when it reads B, it reaches the final state qf.
Hence, M = {{q0, q1, q2, q3, q4, qf}, {0,1, B}, {1, B}, δ, q0, B, {qf}} where δ is given by:
Tape Alphabet Present State Present State Present State Present State Present State
Symbol “q0” “q1” “q2” “q3” “q4”
0 BRq1 BRq1 0Rq2 - 0Lq4
1 1Rq2 1Rq2 1Rq2 - 1Lq4
B BRq1 BLq3 BLq4 0Lqf BRqf
Table 5.3: Example of Turing Machine
A Multi-tape Turing machine can be formally described as a 7-tuple M = (Q,Σ, Γ, δ, q0, B, F) where −
Q is a finite set of states
Σ is set of input alphabet
Γ is the tape alphabet
B is the blank symbol
δ is a relation on states and symbols where δ: Q × Xk → Q × (X × {Left_shift, Right_shift,
No_shift })k where there is k number of tapes
q0 is the initial state
F is the set of final states
Note: Every Multi-tape Turing machine has an equivalent single-tape Turing machine.
Non-Turing
Acceptable
Language
Turing Acceptable
Language
Decidable Language
Undecidable Language:
Non-Turing
Acceptable
Language
Undecidable
Language
Decidable
Language
Figure 5.10: Undecidable Language
For an undecidable language, there is no Turing Machine which accepts the language and makes a
decision for every input string w (TM can make decision for some input string though). A decision
problem P is called “undecidable” if the language L of all yes instances to P is not decidable.
Undecidable languages are not recursive languages, but sometimes, they may be recursively
enumerable languages.
Example:
The halting problem of Turing machine
The mortality problem
The mortal matrix problem
The Post correspondence problem, etc.
Reducibility:
Language A is reducible to language B (represented as A≤B) if there exists a function f which will
convert strings in A to strings in B as:
w ɛ A <=> f(w) ɛ B
Theorem 1: If A≤B and B is decidable then A is also decidable.
Theorem 2: If A≤B and A is undecidable then B is also undecidable.
P & NP Problems:
If a problem can be solved in polynomial time, it is said to belong to the P class of problems. P-type
problems are tractable.
For some intractable problems, user can verify that the solution is correct using a P-type algorithm. For
example, user can verify that a given solution to the TSP visits every city. These problems are referred
to as Non-deterministic Polynomial problems or NP-type problems. The challenge for programmers is
to find a P-type solution to NP-type problems.
P Problems:
As the name says these problems can be solved in polynomial time, i.e.; O(n), O(n2) or O(nk) where k is
a constant.
NP Problems:
Some people think NP as Non-Polynomial. But actually it is Non-deterministic Polynomial time. i.e.;
“yes” instances of these problems can be solved in polynomial time by a non-deterministic Turing
machine and hence can take up to exponential time (some problems can be solved in sub-exponential
but super polynomial time) by a deterministic Turing machine. In other words these problems can be
verified (if a solution is given, say if it is correct or wrong) in polynomial time. Examples include all P
problems. One example of a problem not in P but in NP is Integer Factorization.
From the figure 5.10, it’s clear that NPC problems are the hardest problems in NP while being the
simplest ones in NPH. i.e.; NP∩NPH=NPC
Given a general problem, we can say it’s in NPC, if and only if we can reduce it to some NP problem
(which shows it is in NP) and also some NPC problem can be reduced to it (which shows all NP
problems can be reduced to this problem).
Also, if a NPH problem is in NP, then it is NPC
Examples of NP Problems:
Boolean Satisfiability Problem:
Boolean Satisfiability or simply SAT is the problem of determining if a Boolean formula is satisfied or
unsatisfied.
Satisfied: If the Boolean variables can be assigned values such that the formula turns out to be TRUE,
then we say that the formula is satisfied.
Unsatisfied: If it is not possible to assign such values, then we say that the formula is unsatisfied.
2-SAT Problem:
2-SAT is a special case of Boolean Satisfiability Problem and can be solved in polynomial time.
To understand this better, first let us see what is Conjunctive Normal Form (CNF) or also known
as Product of Sums (POS).
CNF: CNF is a conjunction (AND) of clauses, where every clause is a disjunction (OR).
F = (A1 V B1) ^ (A2 V B2) ^ (A3 V B3) …………………..(Am V Bm)
Now, 2-SAT limits the problem of SAT to only those Boolean formula which are expressed as a
CNF with every clause having only 2 terms(also called 2-CNF)
For 2-SAT problem the CNF value is TRUE, if value of every clause is TRUE. Let one of the clause
be(A V B) so we can say (A V B) = TRUE in following two conditions
If A = 0, B must be 1 i.e. ( A’ => B )
If B = 0, A must be 1 i.e. ( B’ => A)
Thus (A V B) is true equivalent to (A’ => B) ^ (B’ => A)
Vertex Cover Problem is a known NP Complete problem, i.e., there is no polynomial time solution for
this unless P = NP. Although There can be an approximate polynomial time algorithms to solve the
problem. Following is a simple approximate algorithm.
Given a 𝑔𝑟𝑎𝑝ℎ 𝐺 and integer k, construct a 𝑔𝑟𝑎𝑝ℎ 𝐺’ such that 𝐺 has a vertex cover of size k iff 𝐺’
Definition: Vertex cover is set of vertices that touch all edges in the graph.
vertex cover, can be used to make a graph with a Hamiltonian Cycle graph. Since creating such a graph
can be done under polynomial time, simply replace edges with widgets and make proper connections,
we have a reduction from Vertex Cover to Hamiltonian Cycle. This means that finding whether a graph
has a Hamiltonian Cycle or not is NP Hard. As we have seen earlier it’s also in NP, therefore,
Hamiltonian Cycle is an NP Complete Problem.
f is a function V×V → Z, t ∈ Z,
not exceed t.}
Proof:
First, we have to prove that TSP belongs to NP. If we want to check a tour for credibility, we check that
the tour contains each vertex once. Then we sum the total cost of the edges and finally we check if the
cost is minimum. This can be completed in polynomial time thus TSP belongs to NP.
Secondly we prove that TSP is NP-hard. One way to prove this is to show that Hamiltonian cycle <= TSP
(given that the Hamiltonian cycle problem is NP-complete). Assume G = (V, E) to be an instance of
where E′ = {(i, j):i, j ∈ V and i ≠ j}. Thus, the cost function is defined as:
Hamiltonian cycle. An instance of TSP is then constructed. We create the complete graph = (V, ≤ P G ′ E ′)
Now suppose that a Hamiltonian cycle h exists in G. It is clear that the cost of each edge in h is 0 in G ′ as
each edge belongs to E. Therefore, h has a cost of 0 in G ′. Thus, if graph G has a Hamiltonian cycle then
graph G′ has a tour of 0 costs.
Conversely, we assume that G’ has a tour h’ of cost at most 0. The cost of edges in E’ are 0 and 1 by
definition. So each edge must have a cost of 0 as the cost of h’ is 0. We conclude that h’ contains only
edges in E.
So we have proven that G has a Hamiltonian cycle if and only if G’ has a tour of cost at most 0. Thus TSP
is NP-complete.