Unit Vii
Unit Vii
FORMAL LANGUAGES & AUTOMATA THEORY Jaya Krishna, M.Tech, Asst. Prof.
UNIT-VII
TURING MACHINES
Introduction
Until now we have been interested primarily in simple classes of languages and the ways
that they can be used for relatively constrainted problems, such as analyzing protocols, searching
text, or parsing programs.
We begin with an informal argument, using an assumed knowledge of C programming, to
show that there are specific problems we cannot solve using a computer.
These problems are called “undecidable”. We then introduce a respected formalism for
computers called the Turing machine. While a Turing machine looks nothing like a PC, and would be
grossly inefficient should some startup company decide to manufacture and sell them, the Turing
machine long has been recognized as an accurate model for what any physical computing device is
capable of doing.
TURING MACHINE:
The purpose of the theory of undecidable problems is not only to establish the existence
of such problems – an intellectually exciting idea in its own right – but to provide
guidance to programmers about what they might or might not be able to accomplish
through programming.
The theory has great impact when we discuss the problems that are decidable, may
require large amount of time to solve them. These problems are called “intractable
problems” tend to present greater difficulty to the programmer and system designer
than do the undecidable problems.
We need tools that will allow us to prove everyday questions undecidable or intractable.
As a result we need to rebuild our theory of undecidability, based not on programs in C or
another language, but based on a very simple model of a computer called the Turing
machine.
This device is essentially a finite automaton that has a single tape of infinite length on
which it may read and write data.
One advantage of the Turing machine over programs as representation of what can be
computed is that the Turing machine is sufficiently simple that we can represent its
configuration precisely, using simple notation much like the ID’s of a PDA.
There is a tape divided into squares or cells; each cell can hold any one of a finite number
of symbols.
Initially the input, which is a finite length string of symbols chosen from the input
alphabet, is placed on the tape.
All other tape cells, extending infinitely to the left and right, initially hold a special symbol
called the blank.
The blank is a tape symbol, but not an input symbol, and there may be other tape
symbols besides the input symbols and the blank as well.
There is a tape head that is always positioned at one of the tape cells. The Turing
machine is said to be scanning that cell.
Initially the tape head is at the left most cells that hold the input.
A move of the Turing machine is a function of the state of the finite control and the tape
symbol scanned.
In one move, the Turing machine will:
1. Change state. The next state optionally may be the same as the current state.
2. Write a tape symbol in the cell scanned. This tape symbol replaces whatever
symbol was in that cell. Optionally the symbol written may be the same as the
symbol currently there.
3. Move the tape head left or right. In our formalism we require a move, and do not
allow the head to remain stationary. This restriction does not constrain what a
truing machine can compute, since any sequence of moves with a stationary head
could be condensed, along with the next tape head move, into a single state
change, a new tape symbol, and a move left or right.
The formal notation we shall use for a Turing Machine (TM) is similar to that used for
finite automata or PDA’s. We describe a TM by the 7 – tuple like
M = (Q, Σ, Γ, δ, q0, B, F)
Whose components have the following meanings:
Q: the finite set of states of the finite control
Σ: the finite set of input symbols
2. The tape head is scanning the ith symbol from the left.
3. X1X2 . . . Xn is the portion of the tape between the leftmost and the rightmost
nonblank. As an exception if the head is to the left of the leftmost nonblank or
the right of the rightmost nonblank, then some prefix or suffix of X 1X2 . . . Xn will
be blank, and i will be 1 or n, respectively.
We describe the Turing machine M = (Q, Σ, Γ, δ, q0, B, F) by the notation that was used
for PDA’s. When the Turing machine M is understood, we shall use just Ⱶ to reflect
moves.
Suppose δ(q, Xi) = (p, Y, L); i.e. the next move is leftward. Then
X1 X2 . . . Xi-1 q Xi Xi+1 . . . Xn X1 X2 . . . Xi-2 p Xi-1 Y Xi+1 . . . Xn
Notice how this move reflects the change to state p and the fact that the tape head is
now positioned at cell i-1. There are two important exceptions:
1. If i = 1, then M moves the blank to the left of X1. In that case,
X1X2 . . . Xn pBYX2. . . Xn
2. If i = n and Y = B then the symbol B written over X n joins the infinite sequence of
trailing blanks and does not appear in the next ID. Thus,
X1X2 . . . Xn-1 q Xn X1X2 . . . Xn-2 p Xn-1
Now suppose δ(q, Xi) = (p, Y, R); i.e. the next move is rightward. Then
X1 X2 . . . Xi-1 q Xi Xi+1 . . . Xn X1 X2 . . . Xi-1 Y p Xi+1 . . . Xn
Here the move reflects the fact that the head has moved to cell i+1. Again there are two
important exceptions:
1. If i = n, then i+1st cell holds a blank, and that cell was not part of the previous ID.
Thus we instead have
X1 X2 . . . Xn-1 q Xn X1 X2 . . . Xn-1 Y p B
2. If i = 1 and Y =B , then the symbol B written over X1 joins the infinite sequence of
leading blanks and does not appear in the next ID. Thus,
q X 1 X2 . . . X n p X2 . . . X n
Example- 7.1:
Let us design a TM and see how it behaves on a typical input. The TM we construct will
accept the language consisting of all palindromes of 0’s and 1’s.
Initially it is given a finite sequence of 0’s and 1’s on its tape, preceded and followed by
infinity of blanks.
Starting at the left end it checks the first symbol of the input if it is a 0 and changes it to
X. Similarly if it is a 1, it changes it to Y and moves the right until it sees a blank.
Now it moves left and checks whether the symbol read matches the one most recently
changed. If so it is also changed correspondingly and the machine move back left until it
finds the leftmost 0 or 1.
This process is continued by moving left and right alternately until all 0’s and 1’s have
been matched.
If the input is not a palindrome, the TM will not have a next move and hence will halt
without accepting. However if it is a palindrome it accepts.
The formal specification of TM M is
M = ({q0, q1, q2, q3, q4, q5, q6}, {0, 1}, {0, 1, X, Y, B}, δ, q0, B, {q6})
Where δ is given by the table as follows:
State 0 1 X Y B
2. If M sees an X or a Y, then it has changed all the 0’s to X’s and 1’s to Y’s; the input
was of a palindrome of even length, and hence M should accept. Thus M enters
state q6 and halts
In case M is in state q3 and reads an X or q4 and reads a Y instead of 0 and 1, it concludes
that the input was a palindrome of odd length. In this case it changes to state q6 and
halts.
On the other hand if M encounters a 1 in state q3 or 0 in state q4, then the input is not a
palindrome and so M halts without accepting.
Here is an example of an accepting computation by M. Its input is 1001. Initially M is in
state q0, scanning the first 1 i.e. M’s initial ID is q01001.the entire sequence of moves of
M is :
q01001 Ⱶ Yq2001 Ⱶ Y0q201 Ⱶ Y00q21 Ⱶ Y001q2 Ⱶ Y00q41 Ⱶ Y00q5Y Ⱶ Y0q50Y Ⱶ Yq500Y Ⱶ
q5Y00Y Ⱶ Yq000Y Ⱶ YXq10Y Ⱶ YX0q1Y Ⱶ YXq30Y Ⱶ YXq5XY Ⱶ Yq5XXY Ⱶ YXq0XY Ⱶ Yq6XXY
1/1
0/0 B/B
Y/Y
X/X
q1 q3 0/X
X/X
0/X Y/Y 0/0
q5 1/1
X/X
q6
q0 Y/Y
B/B 1/Y
X/X
1/Y Y/Y
B/B
q2 q4
X/X
Y/Y
0/0 X/X
1/1 Y/Y
Figure 3: Transition diagram for the Turing Machine which accepts the set of all palindromes over {0, 1}
COMPUTABLE FUNCTIONS
Computable functions are the basic objects of study in computability theory.
Computable functions are the formalized analogue of the intuitive notion of algorithm.
They are used to discuss computability without referring to any concrete model of
computation such as Turing machines or register machines.
Any definition, however, must make reference to some specific model of computation
but all valid definitions yield the same class of functions.
Particular models of computability that give rise to the set of computable functions are
the Turing-computable functions and the μ-recursive functions.
Before the precise definition of computable function, mathematicians often used the
informal term effectively calculable. This term has since come to be identified with the
computable functions.
Note that the effective computability of these functions does not imply that they can be
efficiently computed (i.e. computed within a reasonable amount of time).
In fact, for some effectively calculable functions it can be shown that any algorithm that
computes them will be very inefficient in the sense that the running time of the
algorithm increases exponentially with the length of the input.
According to the Church–Turing thesis, computable functions are exactly the functions
that can be calculated using a mechanical calculation device given unlimited amounts of
time and storage space.
Equivalently, this thesis states that any function which has an algorithm is computable.
Note that an algorithm in this sense is understood to be a sequence of steps a person
with unlimited time and an infinite supply of pen and paper could follow.
The basic characteristic of a computable function is that there must be a finite procedure
(an algorithm) telling how to compute the function.
COMPUTABLE SETS AND RELATION
A set A of natural numbers is called computable if there is a computable, total function f
such that for any natural number n, f(n) = 1 if n is in A and f(n) = 0 if n is not in A.
A set of natural numbers is called computably enumerable if there is a computable
function f such that for each number n, f(n) is defined if and only if n is in the set.
Thus a set is computably enumerable if and only if it is the domain of some computable
function.
The word enumerable is used because the following are equivalent for a nonempty
subset B of the natural numbers:
1. B is the domain of a computable function.
2. B is the range of a total computable function. If B is infinite then the function can
be assumed to be injective (or one to one).
If a set B is the range of a function f then the function can be viewed as an enumeration
of B, because the list f(0), f(1), ... will include every element of B.
In computability theory in computer science, it is common to consider formal languages.
An alphabet is an arbitrary set.
A word on an alphabet is a finite sequence of symbols from the alphabet; the same
symbol may be used more than once.
For example, binary strings are exactly the words on the alphabet {0, 1}.
A language is a subset of the collection of all words on a fixed alphabet.
For example, the collection of all binary strings that contain exactly 3 ones is a language
over the binary alphabet.
A key property of a formal language is the level of difficulty required to decide whether a
given word is in the language. Some coding system must be developed to allow a
computable function to take an arbitrary word in the language as input; this is usually
considered routine.
1. Each function with a finite domain; e.g., any finite sequence of natural numbers.
k
2. Each constant function f : N → N, f(n1,...nk) := n.
3. Addition f : N2 → N, f(n1,n2) := n1 + n2
4. The function which gives the list of prime factors of a number.
5. The greatest common divisor of two numbers is a computable function.
CHURCH HYPOTHESIS
In 1900 mathematician David Hilbert delivered a lecture, where he identified for every
twenty three mathematical problems and posed them as a challenge for the coming
century. The tenth problem on his list concerned algorithms.
According to Hilbert’s tenth problem, it was to devise an algorithm that tests whether a
polynomial has an integral root.
He did not use the term algorithm but rather a process according to which it can be
determined by a finite number of operations.
Hilbert explicitly asked that an algorithm be devised. Thus he apparently assumed that
such an algorithm must exist – someone need only find it.
As we now know, no algorithm exists for this task; it is algorithmically unsolvable.
The definition came in 1936 papers of Alonzo Church and Alan Turing. Church used a
notational system called the λ-calculus to define algorithms. Turing did it with his
machines.
These two definitions were shown to be equivalent. This connection between the
informal notion of algorithm and the precise definition has come to be called the Church-
Turing thesis.
In computability theory the Church-Turing thesis is a combined hypothesis about the
nature of functions whose values are effectively calculable or in more modern terms,
functions whose values are algorithmically computable.
In simple terms the church-Turing thesis states that a function is algorithmically
computable if and only if it is computable by a Turing machine.
The Church–Turing thesis is a statement that characterizes the nature of computation
and cannot be formally proven.
COUNTER MACHINES
A counter machine may be thought of in one of the two ways:
1. The counter machine has the same structure as the multistack machine but in
place of each stack is a counter. Counters hold any nonnegative integer, but we
can only distinguish between zero and nonzero counters. That is the move of the
counter machine depends on its state, input symbol, and which if any of the
counters are zero. In one move the counter machine can:
a. Change state.
b. Add or subtract 1 from any of its counters, independently. However a
counter is not allowed to become negative, so it cannot subtract 1 from a
counter that is currently 0.
2. A counter machine may also be regarded as a restricted multistack machine. The
restrictions are as follows:
a. There are only two stack symbols, which we shall refer to Z 0 (the bottom
of the stack marker) and X.
b. Z0 is initially on each stack.
c. We may replace Z0 only by a string of the form XiZ0 for some i ≥ 0.
d. We may replace X only by Xi for some i ≥ 0. That is Z0 appears only on the
bottom of each stack, and all other stack symbols, if any are X.
THE POWER OF COUNTER MACHINES
There are a few observations about the languages accepted by counter machines that
are obvious but worth stating:
1. Every language accepted by a counter machine is recursively enumerable. The
reason is that a counter machine is a special case of a stack machine, and a stack
machine is a special case of a Multitape Turing machine which accepts only
recursively enumerable languages.
2. Every languages accepted by a one-counter machine is a CFL. In fact the
languages of one-counter machines are accepted by deterministic PDA’s,
although the proof is surprisingly complex.
The difficulty in the proof stems from the fact that the multistack and counter machines
have an end marker $ at the end of their input.
It is a Turing machine with its input tape infinite in both the directions, the other
components being the same as that of the basic model.
MULTIHEAD TURING MACHINE
It is a single tape Turing machine having k heads reading symbols on the same tape. In
one step all the heads sense the scanned symbols and move or write independently.
A Multihead Turing machine can be simulated by single head Turing machine.
TWODIMENSIONAL TURING MACHINES
The Turing machine can have two dimensional tapes.
When the head is scanning the symbol it can move either left or right or up or down. The
smallest rectangle containing the non blank portion is m x n, then it has m rows and n
columns.
A one dimensional tape Turing machine which tries to simulate this 2 dimensional Turing
machine will have 2 tapes.