Chapter 1 Turing Machines
Chapter 1 Turing Machines
Turing Machines
2021 RVU 1
Turing Machine
• A Turing machine is an automaton whose temporary
storage is a tape.
• It can be visualized as a single, one-dimensional array of
cells, each of which can hold a single symbol.
• This array extends indefinitely in both directions and is
therefore capable of holding an unlimited amount of
information.
• The information can be read and changed in any order.
• We will call such a storage device a tape because it is
analogous to the magnetic tapes used in older
computers.
• the tape is a read-write head that can travel right or left
on the tape and that can read and write a single symbol
on each move.
2021 RVU 2
Turing Machine
TM:
• More powerful
• Essentially a finite automaton but with
unlimited memory
• Although theoretical, can do everything a
general purpose computer of today can do
•If a TM can’t solve it, neither can a computer
2021 RVU 3
Turing Machines
• TM’s described
• Well before the days of modern computers but remains a
popular model for what is possible to compute on
today’s systems
• Advances in computing still fall under the TM model, so
even if they may run faster, they are still subject to the
same limitations
• A TM consists of a finite control (i.e. a finite state
automaton) that is connected to an infinite tape.
2021 RVU 4
• Turing Machine use a “tape”, which is actually infinite in
both the directions.
• The tape consists of a series of “squares”, each of which can
hold a single symbol.
• The “tape-head”, or “read-write head”, can read a symbol
from the tape, write a symbol to the tape and move one
square in either direction.
Every computer algorithm can be implemented as a Turing
machine
• There are two kinds of Turing Machine available.
(a)Deterministic Turing Machine.
(b)Non-deterministic Turing Machine.
2021 RVU 5
Actions of a Turing Machine
2021 RVU 6
A Turing Machine
Tape
...... ......
Read-Write head
Control Unit
2021 RVU 7
The Tape
No boundaries -- infinite length
...... ......
Time 1
...... a b k c ......
1. Reads a
2. Writes k
3. Moves Left
2021 RVU 9
Time 1
...... a b k c ......
Time 2
...... a f k c ......
1. Reads b
2. Writes f
3. Moves Right
2021 RVU 10
Turing Machine Defn:
Input Tape
alphabet alphabet
States
M (Q, , , , q0 , , F )
Transition Accept
function states
Initial blank
2021 state RVU 11
Turing Machine Formal Description
7-tuple: (Q, , Γ, δ, q0, qaccept, qreject)
Q: finite set of states
: input alphabet (cannot include blank symbol, _)
Γ: tape alphabet, includes and _
δ: transition function: Q Γ Q Γ {L, R}
q0: start state, q0 Q
qaccept: accepting state, qaccept Q
qreject: rejecting state, qreject Q
RVU 12
2021
A Turing machine M can also be defined by
M = (Q,Σ,Γ,δ,q0, ,F),
where
Q is the set of internal states,
Σ is the input alphabet
Γ is the finite set of symbols called the tape
alphabet,
δ is the transition function,
∈Γ is a special symbol called the blank,
q0 ∈ Q is the initial state,
F ⊆ Q is the set of final states.
2021 RVU 13
• In the definition of a Turing machine, we assume that Σ ⊆
Γ – { }, that is, that the input alphabet is a subset of the
tape alphabet, not including the blank.
• The transition function δ is defined as
δ : Q × Γ → Q × Γ × {L,R}.
• In general, δ is a partial function on Q × Γ; its
interpretation gives the principle by which a Turing
machine operates.
• The arguments of δ are the current state of the control unit
and the current tape symbol being read.
• The result is a new state of the control unit, a new tape
symbol, which replaces the old one, and a move symbol, L
or R.
• The move symbol indicates whether the read-write head
moves left or right one cell after the new symbol has been
written on the tape.
2021 RVU 14
The Input String
...... a b a c ......
head
q1 a b, R q2
(q1, a) (q2 , b, R)
2021 RVU 16
States and Transition Function
q1 c d, L q2
(q1, c) (q2 , d , L)
2021 RVU 17
States and Transition Function
Read Write
Move Left
q1 a b, L q2
Move Right
q1 a b, R q2
2021 RVU 18
Example:
Time 1
...... a b a c ......
q1
current state
q1 a b, R q2
2021 RVU 19
Time 1
...... a b a c ......
q1
Time 2
...... a b b c ......
q2
q1 a b, R q2
RVU 20
2021
Example:
Time 1
...... a b a c ......
q1
Time 2
...... a b b c ......
q2
q1 a b, L q2
2021 RVU 21
Example:
Time 1
...... a b a c ......
q1
Time 2
...... a b b c g ......
q2
q1 g, R q2
2021 RVU 22
standard Turing machine
Since one can make several different definitions of a Turing
machine,
it is worthwhile to summarize the main features of our model,
which we will call a standard Turing machine:
q1 q1
q3 a d, L q3
b d, L
...... a b a c ......
q1
a b, R q2 Allowed:
q1 No transition
for input symbol c
b d, L q3
2021 RVU 27
Outcome of Nondeterministic computation
• Accept
• If any branch of the computation accepts, then the
nondeterministic TM will accept
• Reject
• If all branches of the computation Halts and
Rejects(i.e., no branches accept, but all computation
halts), then the nondeterministic TM rejects.
• Loop
• Computation continues, but “accept” is never
encountered.
• Some branches in the computation history are
2021
infinite. RVU 28
Halting
• One way for a TM to accept input is to end in a final state.
• Another way is acceptance by halting.
• We say that a TM halts if it enters a state q, scanning a tape symbol X, and
there is no move in this situation; i.e. δ(q,X) is undefined.
• When a string is given as input, the TM always halts:
• The TM will accept if it is in L. The TM will Reject if it is not in L.
• Note that this definition of halting was not used in the transition diagram
for the TM we described earlier; instead that TM died on unspecified
input!
• It is possible to modify the prior example so that there is no unspecified
input except for our accepting state. An equivalent TM that halts exists
for a TM that accepts input via final state.
• In general, we assume that a TM always halts when it is in an accepting
state.
• it is not always possible to require that a TM halts even if it does not
accept the input. Turing machines that always halt, regardless of
accepting or not accepting, are good models of algorithms for decidable
problems. Such languages are called recursive.
2021 RVU 29
Halting Example 1:
...... a b a c ......
q1
q1 No transition from q1
HALT!!!
2021 RVU 30
Halting Example 2:
...... a b a c ......
q1
a b, R q2
No possible transition
q1 from q1 and symbol c
b d, L q3 HALT!!!
2021 RVU 31
Accepting States
q1 q2 Allowed
q1 q2 Not Allowed
If machine halts
in a non-accept state
Reject Input or
string
If machine enters
an infinite loop
2021 RVU 33
Observation:
In order to accept an input string,
it is not necessary to scan all the
symbols in the string
2021 RVU 34
Turing Machine Example
Input alphabet {a , b }
, L
q0 q1
2021 RVU 35
Time 0 a a a
q0
a a, R
, L
q0 q1
2021 RVU 36
Time 1 a a a
q0
a a, R
, L
q0 q1
2021 RVU 37
Time 2 a a a
q0
a a, R
, L
q0 q1
2021 RVU 38
Time 3 a a a
q0
a a, R
, L
q0 q1
2021 RVU 39
Time 4 a a a
q1
, L
q0 q1
2021 RVU 40
Rejection Example
Time 0 a b a
q0
a a, R
, L
q0 q1
2021 RVU 41
Time 1 a b a
q0
No possible Transition
a a, R Halt & Reject
, L
q0 q1
2021 RVU 42
A simpler machine for same language
but for input alphabet {a }
q0
2021 RVU 43
Time 0 a a a
q0
q0
, L
q0 q1
2021 RVU 45
Time 0 a b a
q0
b b, L
a a, R
, L
q0 q1
2021 RVU 46
Time 1 a b a
q0
b b, L
a a, R
, L
q0 q1
2021 RVU 47
Time 2 a b a
q0
b b, L
a a, R
, L
q0 q1
2021 RVU 48
Time 2 a b a
q0
Time 3 a b a
Infinite loop
q0
Time 4 a b a
q0
Time 5 a b a
2021 RVU
q0 49
Because of the infinite loop:
2021 RVU 50
2021 RVU 51
2021 RVU 52
Cont..
2021 RVU 53
Another Turing Machine Example
n n
{a b }
Turing machine for the language n 1
q4 y y, R y y, L
y y, R a a, R a a, L
, L
y y, R a x, R b y, L
q3 q0 q1 q2
2021
x x, R 54
RVU
Basic Idea:
Match a’s with b’s:
Repeat:
replace leftmost a with x
find leftmost b and replace it with y
Until there are no more a’s or b’s
2021 RVU 55
Time 0 a a b b
q0
q4 y y, R y y, L
y y, R a a, R a a, L
, L
y y, R a x, R b y, L
q3 q0 q1 q2
2021 RVU
x x, R 56
Time 1 x a b b
q1
q4 y y, R y y, L
y y, R a a, R a a, L
, L
y y, R a x, R b y, L
q3 q0 q1 q2
2021 RVU
x x, R 57
Time 2 x a b b
q1
q4 y y, R y y, L
y y, R a a, R a a, L
, L
y y, R a x, R b y, L
q3 q0 q1 q2
2021 RVU
x x, R 58
Time 3 x a y b
q2
q4 y y, R y y, L
y y, R a a, R a a, L
, L
y y, R a x, R b y, L
q3 q0 q1 q2
2021 RVU
x x, R 59
Time 4 x a y b
q2
q4 y y, R y y, L
y y, R a a, R a a, L
, L
y y, R a x, R b y, L
q3 q0 q1 q2
2021 RVU
x x, R 60
Time 5 x a y b
q0
q4 y y, R y y, L
y y, R a a, R a a, L
, L
y y, R a x, R b y, L
q3 q0 q1 q2
2021 RVU
x x, R 61
Time 6 x x y b
q1
q4 y y, R y y, L
y y, R a a, R a a, L
, L
y y, R a x, R b y, L
q3 q0 q1 q2
2021 RVU
x x, R 62
Time 7 x x y b
q1
q4 y y, R y y, L
y y, R a a, R a a, L
, L
y y, R a x, R b y, L
q3 q0 q1 q2
2021 RVU
x x, R 63
Time 8 x x y y
q2
q4 y y, R y y, L
y y, R a a, R a a, L
, L
y y, R a x, R b y, L
q3 q0 q1 q2
2021 RVU
x x, R 64
Time 9 x x y y
q2
q4 y y, R y y, L
y y, R a a, R a a, L
, L
y y, R a x, R b y, L
q3 q0 q1 q2
2021 RVU
x x, R 65
Time 10 x x y y
q0
q4 y y, R y y, L
y y, R a a, R a a, L
, L
y y, R a x, R b y, L
q3 q0 q1 q2
2021 RVU
x x, R 66
Time 11 x x y y
q3
q4 y y, R y y, L
y y, R a a, R a a, L
, L
y y, R a x, R b y, L
q3 q0 q1 q2
2021 RVU
x x, R 67
Time 12 x x y y
q3
q4 y y, R y y, L
y y, R a a, R a a, L
, L
y y, R a x, R b y, L
q3 q0 q1 q2
2021 RVU
x x, R 68
Time 13 x x y y
q4
Halt & Accept
q4 y y, R y y, L
y y, R a a, R a a, L
, L
y y, R a x, R b y, L
q3 q0 q1 q2
2021 RVU
x x, R 69
Observation:
If we modify the
machine for the language n n
{a b }
2021 RVU 70
Instantaneous description (ID):
Configuration
c a b a
q1
An ID of a Turing machine M is a string aqy, where
• q1 is the present state of M,
• the entire input string is split as ay,
• the first symbol of y is the current symbol b under the R/W head
and
• y has all the subsequent symbols of the input string, and
• the string a is the substring of the input string formed by all the
symbols to the left of a.
Representation of ID ca q ba1
2021 RVU 71
An “Instantaneous Description” or “Configuration” of a Turing
machine requires.
(a) the state the Turing machine is in
(b) the contents of the tape
(c) the position of the tape head on the tape.
2021 RVU 72
Time 4 Time 5
x a y b x a y b
q2 q0
2021 RVU 73
Time 4 Time 5
x a y b x a y b
q2 q0
Time 6 Time 7
x x y b x x y b
q1 q1
A computation
q2 xayb x q0 ayb xx q1 yb xxy q1 b
2021 RVU 74
q2 xayb x q0 ayb xx q1 yb xxy q1 b
Equivalent notation: q2 xayb xxy q1 b
2021 RVU 75
Initial configuration: q0 w
Input string
a a b b
q0
2021 RVU 76
The Accepted Language
For any Turing Machine M
L( M ) {w : q0 w x1 q f x2 }
2021 RVU 77
If a language L is accepted
by a Turing machine M
then we say that L is:
•Turing Recognizable
2021 RVU 79
Multitape Turing Machine
Theorem: A multitape TM is equivalent in power to an
ordinary TM.
Recall that two TM’s are equivalent if they recognize the
same language. We can show how to convert a multitape
TM, M, to a single tape TM, S:
• Say that M has k tapes.
• Create the TM S to simulate having k tapes by interleaving the
information on each of the k tapes on its single tape
• Use a new symbol # as a delimiter to separate the contents of
each tape
• S must also keep track of the location on each of the simulated
heads
• Write atype symbol with a * to mark the place where the head on
the tape would be
• The * symbols are new tape symbols that don’t exist with M
• The finite control must have the proper logic to distinguish say, x* and x and
realize both refer to the same thing, but one is the current tape symbol.
2021 RVU 80
Multitape Machine
… 0 1 0 1 0 B …
… a a a B …
M
… b a B …
2021 81
RVU
Single Tape Equivalent
•One final detail
•If at any point S moves one of the virtual
tape heads onto a #, then this action
signifies that M has moved the
corresponding head onto the previously
unread blank portion of that tape.
•To accommodate this situation, S writes a
blank symbol on this tape cell and shifts
the tape contents to the rightmost # by
one, adds a new #, and then continues
back where it left off
2021 RVU 82
Nondeterministic TM
•Replace the “DFA” part of the TM with an
“NFA”
• Each time we make a nondeterministic move, you
can think of this as a branch or “fork” to two
simultaneously running machines. Each machine
gets a copy of the entire tape. If any one of these
machines ends up in an accepting state, then the
input is accepted.
•Although powerful, nondeterminism does
not affect the power of the TM model
•Theorem: Every nondeterministic TM has an
equivalent deterministic TM.
2021 RVU 83
Equivalence of TM’s and Computers
•In one sense, a real computer has a finite amount
of memory, and thus is weaker than a TM.
•But, we can postulate an infinite supply of tapes,
disks, or some peripheral storage device to simulate
an infinite TM tape.
•Additionally, we can assume there is a human
operator to mount disks, keep them stacked neatly
on the sides of the computer, etc.
•Need to show both directions,
•a TM can simulate a computer and that
•a computer can simulate a TM
2021 RVU 84
Computer Simulate a TM
•This direction is fairly easy –
• Given a computer with a modern programming
language, certainly,
• we can write a computer program that emulates
the finite control of the TM.
•The only issue remains the infinite tape.
•Our program must map cells in the tape to
storage locations in a disk.
•When the disk becomes full, we must be able
to map to a different disk in the stack of disks
mounted by the human operator.
2021 RVU 85
TM Simulate a Computer
• The simulation is performed at the level of stored
instructions and accessing words of main memory.
• TM has one tape that holds all the used memory locations and
their contents.
• Other TM tapes hold the instruction counter, memory address,
computer input file, and scratch data.
• The computer’s instruction cycle is simulated by:
1. Find the word indicated by the instruction counter on the
memory tape.
2. Examine the instruction code (a finite set of options), and get
the contents of any memory words mentioned in the
instruction, using the scratch tape.
3. Perform the instruction, changing any words' values as
needed, and adding new address-value pairs to the memory
tape, if needed.
2021 RVU 86
TM/Computer Equivalence
•Anything a computer can do, a TM can do, and vice versa
•TM is much slower than the computer, though
•But the difference in speed is polynomial
•Each step done on the computer can be completed in
O(n2) steps on the TM.
•While slow, this is key information if we wish to make an
analogy to modern computers.
•Anything that we can prove using Turing machines
translates to modern computers with a polynomial time
transformation.
•Whenever we talk about defining algorithms to solve
problems, we can equivalently talk about how to
construct a TM to solve the problem.
• If a TM cannot be built to solve a particular problem, then it
means our modern computer cannot solve the problem
either. RVU 87
Turing Languages
•We use the simplicity of the TM model to prove
formally that there are specific problems (i.e.
languages) that the TM cannot solve. Three classes
of languages:
• Turing-decidable or recursive: TM can accept the strings
in the language and tell if a string is not in the language.
Sometimes these are called decidable problems.
• Turing-recognizable or recursively enumerable : TM can
accept the strings in the language but cannot tell for
certain that a string is not in the language. Sometimes
these are called partially-decidable.
• Undecidable : no TM can even recognize ALL members of
the language.
2021 RVU 88
Church-Turing Thesis
• As stated by Kleene:
Every effectively calculable function (effectively decidable
predicate) is general recursive.
2021 RVU 90
Church-Turing Thesis
• Any mechanical computation can be performed by a
Turing Machine
• There is a TM-n corresponding to every computable
problem
• We can model any mechanical computer with a TM
• The set of languages that can be decided by a TM is
identical to the set of languages that can be decided by
any mechanical computing machine
• If there is no TM that decides problem P, there is no
algorithm that solves problem P.