0% found this document useful (0 votes)
93 views100 pages

Unit 4

This document describes Turing machines, which are theoretical models of computation consisting of an infinite tape and a read/write head. A Turing machine is formally defined as an 8-tuple that specifies its states, tape symbols, transition function, start/accept/reject states, and more. The transition function defines the machine's behavior by specifying the next state, symbol to write, and head movement based on the current state and symbol read. The machine halts when no transition is possible, accepting the input if it halts in an accept state and rejecting otherwise. An example is given of a Turing machine that accepts the language consisting of strings of as.

Uploaded by

abcd efgh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
93 views100 pages

Unit 4

This document describes Turing machines, which are theoretical models of computation consisting of an infinite tape and a read/write head. A Turing machine is formally defined as an 8-tuple that specifies its states, tape symbols, transition function, start/accept/reject states, and more. The transition function defines the machine's behavior by specifying the next state, symbol to write, and head movement based on the current state and symbol read. The machine halts when no transition is possible, accepting the input if it halts in an accept state and rejecting otherwise. An example is given of a Turing machine that accepts the language consisting of strings of as.

Uploaded by

abcd efgh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 100

18CSC301T – FORMAL

LANGUAGE AND AUTOMATA

3rd Year CSE


SRM IST, Ramapuram
Turing Machines
Introduction
• TM’s described in 1936
– 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.

unit-4/Turning Machine 3
Turing Machine
• The tape consists of cells where each cell holds a symbol from the tape
alphabet. Initially the input consists of a finite-length string of symbols
and is placed on the tape. To the left of the input and to the right of the
input, extending to infinity, are placed blanks. The tape head is initially
positioned at the leftmost cell holding the input.

Finite control

… B B X 1 X2 … Xi Xn B B …

unit-4/Turning Machine 4
Turing Machine Details
• In one move the TM will:
– Change state, which may be the same as the current state
– Write a tape symbol in the current cell, which may be the
same as the current symbol
– Move the tape head left or right one cell
– The special states for rejecting and accepting take effect
immediately
• Formally, the Turing Machine is denoted by the 8-
tuple:
– M = (Q, , Γ, δ, q0, B, qa, qr)

unit-4/Turning Machine 5
Turing Machine Description
• Q = finite states of the control
•  = finite set of input symbols, which is a subset of Γ below
• Γ = finite set of tape symbols
• δ = transition function. δ(q,X) are a state and tape symbol X.
– The output is the triple, (p, Y, D)
– Where p = next state, Y = new symbol written on the tape, D =
direction to move the tape head
• q0= start state for finite control
• B = blank symbol. This symbol is in Γ but not in .
• qaccept = set of final or accepting states of Q.
• qreject = set of rejecting states of Q

unit-4/Turning Machine 6
Instantaneous Description
• Sometimes it is useful to describe what a TM does in terms of
its ID (instantaneous description), just as we did with the
PDA.
• The ID shows all non-blank cells in the tape, pointer to the
cell the head is over with the name of the current state
– use the turnstile symbol ├ to denote the move.
– As before, to denote zero or many moves, we can use ├*.
• For example, for the above TM on the input 10#10 we can
describe our processing as follows:
Ba10#10B ├ B1a0#10B ├ B10a#10B ├ B10#b10B ├ B10#b10B ├
B10#1b0B ├ B10#10bB ├ B10#1c0B ├* cB10#10B ├ Bf10#10B ├*
BXX#XXBl
• In this example the blanks that border the input symbols are
shown since they are used in the Turing machine to define
the borders of our input.
unit-4/Turning Machine 7
A Turing Machine
Tape
...... ......

Read-Write head
Control Unit

unit-4/Turning Machine 8
The Tape
No boundaries -- infinite length
...... ......

Read-Write head

The head moves Left or Right


unit-4/Turning Machine 9
...... ......

Read-Write head

The head at each time step:

1. Reads a symbol
2. Writes a symbol
3. Moves Left or Right
unit-4/Turning Machine 10
Example:
Time 0
...... a b a c ......

Time 1
...... a b k c ......

1. Reads a

2. Writes k
unit-4/Turning Machine 11
Time 1
...... a b k c ......

Time 2
...... a f k c ......

1. Reads
b f
2. Writes
3. Moves Right unit-4/Turning Machine 12
The Input String
Input string Blank symbol

...... a b a c ......

head
Head starts at the leftmost position
of the input string
Are treated as left and right brackets for the
input written on the tape.
unit-4/Turning Machine 13
States & Transitions
Read Write
Move Left

a b,
q1 q2
L

Move Right

a b,
q1 q2
R
unit-4/Turning Machine 14
Example:
Time 1
...... a b a c ......

q1
curren
t state

a b,
q1 q2
R
unit-4/Turning Machine 15
Time 1
...... a b a c ......

q1

Time 2
...... a b b c ......

q2
a b,
q1 q2
R unit-4/Turning Machine 16
Example:
Time 1
...... a b a c ......

q1

Time
...... a b2 b c ......

q2
a b,
q1 q2
L unit-4/Turning Machine 17
Example:
Time 1
...... a b a c ......

q1

Time 2
...... a b b c g ......

q2
g,
q1 q2
R unit-4/Turning Machine 18
Determinism
Turing Machines are deterministic

Allowed Not
Allowed
a b, R q2 q2
a b, R
q1 q1
b d, q3 a d, q3
L L
No lambda transitions allowed
unit-4/Turning Machine 19
Partial Transition
Function
Example:
...... a b a c ......

q1

a b, Allowed:
q2
R No transition
q1
for input symbol c
b d, q3
unit-4/Turning Machine 20
The machine halts if there are
Halting
no possible transitions to follow
Example:

...... a b a c ......

q1

a b, q2
R No possible transition
q1
HALT!!!
b d, q3
unit-4/Turning Machine 22
Final States
q1 q2 Allowed

q1 q2 Not
Allowed

• Final states have no outgoing transitions

• In a final state the machine halts


unit-4/Turning Machine 23
Turing Machines as Acceptors
• A Turing machine halts when it no longer has any
available moves. If it halts in a final state, it
accepts its input; otherwise, it rejects its
input.This is too easy, so let's repeat it in symbols:
• A Turing Machine T = (Q, , , , q0, #, F) accepts a
language L(M), where L(M) = (w +: q0w xiqfxj
for some qf F, xi, xj *}(Notice that this definition
assumes that the Turing machine starts with its
tape head positioned on the leftmost symbol.)
unit-4/Turning Machine 24
Turing Machines as Acceptors
• We said a Turing machine accepts its input if it
halts in a final state. There are two ways this
could fail to happen:
• The Turing machine could halt in a nonfinal
state, or The Turing machine could never stop
(in which case we say it is in an infinite loop. )
• If a Turing machine halts, the sequence of
configurations leading to the halt state is
called a computation.
unit-4/Turning Machine 25
Acceptance
If machine halts
Accept Input
in a final state

If machine halts
in a non-final state
Reject Input or
If machine enters
an infinite loop
unit-4/Turning Machine 26
Turing Machine Example
A Turing machine that accepts the language:
aa *

a a, R

q0 ,
q
L
1
unit-4/Turning Machine 27
Time 0 a a a

q0

a a,
R

q0 ,
q
L
1
unit-4/Turning Machine 28
Time 1 a a a

q0

a a,
R

q0 ,
q
L
1
unit-4/Turning Machine 29
Time 2 a a a

q0

a a,
R

q0 ,
q
L
1
unit-4/Turning Machine 30
Time 3 a a a

q0

a a,
R

q0 ,
q
L
1
unit-4/Turning Machine 31
Time 4 a a a

q1

a a, Halt & Accept


R

q0 ,
q
L
1
unit-4/Turning Machine 32
Rejection Example

Time 0 a b a

q0

a, R ,
q0 q
L
unit-4/Turning Machine 1 33
Time 1 a b a

q0

No possible Transition
a a, Halt & Reject
R

q0 ,
q
L
unit-4/Turning Machine 1 34
Infinite Loop Example

b b,
L
a a,
R ,
q0 q
L
1
unit-4/Turning Machine 35
Time 0 a b a

q0

b b, L
a a, R

q0 ,
q
L
1
unit-4/Turning Machine 36
Time 1 a b a

q0

b b, L
a a, R

q0 ,
q
L
1
unit-4/Turning Machine 37
Time 2 a b a

q0

b b, L
a a, R

q0 ,
q
L
1
unit-4/Turning Machine 38
Time 2 a b a

q0
Time 3 a b a

q0
Time 4 a b a

q0
Time 5 a b a

... Infinite Loop q0


unit-4/Turning Machine 39
Because of the infinite loop:

• The final state cannot be reached

•The machine never halts

•The input is not accepted

unit-4/Turning Machine 40
Another Turing Machine
Example n n
Turing machine for the language {a b }

q4 y y, y y,
y y, R L
R ,
a a, R a a,
y, R q aL
q3 y 0 x, b y,L q
q 2
R L
1
x
unit-4/Turning Machine
x, 41
Time 0 a a b b

q0

q4 y y, y y,
y y, R L
R ,
a a, R a a,
y, R q aL
q3 y 0 x, b y,L q
q 2
R L
1
x
unit-4/Turning Machine
x, 42
Time 1 x a b b

q1

q4 y y, y y,
y y, R L
R ,
a a, R a a,
y, R q aL
q3 y 0 x, b y,L q
q 2
R L
1
x
unit-4/Turning Machine
x, 43
Time 2 x a b b

q1

q4 y y, y y,
y y, R L
R ,
a a, R a a,
y, R q aL
q3 y 0 x, b y,L q
q 2
R L
1
x
unit-4/Turning Machine
x, 44
Time 3 x a y b

q2

q4 y y, y y,
y y, R L
R ,
a a, R a a,
y, R q aL
q3 y 0 x, b y,L q
q 2
R L
1
x
unit-4/Turning Machine
x, 45
Time 4 x a y b

q2

q4 y y, y y,
y y, R L
R ,
a a, R a a,
y, R q aL
q3 y 0 x, b y,L q
q 2
R L
1
x
unit-4/Turning Machine
x, 46
Time 5 x a y b

q0

q4 y y, y y,
y y, R L
R ,
a a, R a a,
y, R q aL
q3 y 0 x, b y,L q
q 2
R L
1
x
unit-4/Turning Machine
x, 47
Time 6 x x y b

q1

q4 y y, y y,
y y, R L
R ,
a a, R a a,
y, R q aL
q3 y 0 x, b y,L q
q 2
R L
1
x
unit-4/Turning Machine
x, 48
Time 7 x x y b

q1

q4 y y, y y,
y y, R L
R ,
a a, R a a,
y, R q aL
q3 y 0 x, b y,L q
q 2
R L
1
x
unit-4/Turning Machine
x, 49
Time 8 x x y y

q2

q4 y y, y y,
y y, R L
R ,
a a, R a a,
y, R q aL
q3 y 0 x, b y,L q
q 2
R L
1
x
unit-4/Turning Machine
x, 50
Time 9 x x y y

q2

q4 y y, y y,
y y, R L
R ,
a a, R a a,
y, R q aL
q3 y 0 x, b y,L q
q 2
R L
1
x
unit-4/Turning Machine
x, 51
Time 10 x x y y

q0

q4 y y, y y,
y y, R L
R ,
a a, R a a,
y, R q aL
q3 y 0 x, b y,L q
q 2
R L
1
x
unit-4/Turning Machine
x, 52
Time 11 x x y y

q3

q4 y y, y y,
y y, R L
R ,
a a, R a a,
y, R q aL
q3 y 0 x, b y,L q
q 2
R L
1
x
unit-4/Turning Machine
x, 53
Time 12 x x y y

q3

q4 y y, y y,
y y, R L
R ,
a a, R a a,
y, R q aL
q3 y 0 x, b y,L q
q 2
R L
1
x
unit-4/Turning Machine
x, 54
Time 13 x x y y

q4

Halt & Accept


q4 y y, y y,
y y, R L
R ,
a a, R a a,
y, R q aL
q3 y 0 x, b y,L q
q 2
R L
1
x
unit-4/Turning Machine
x, 55
Observation:

If we modify the
machine for the language n n
{a b }

we can easily construct


a machine for the language n n n
{a b c }

unit-4/Turning Machine 56
Turing Machines as Language Acceptors-
Example 2
• Design a TM to recognize the language of
strings of the form 0n1n2n

unit-4/Turning Machine 57
Computing Functions
with
Turing Machines
A function f (w) has:

Domain: D Result Region: S

f (w)
w D f ( w)  S

unit-4/Turning Machine 59
A function may have many parameters:

Example: Addition function

f ( x, y )  x  y

unit-4/Turning Machine 60
Integer Domain

Decimal: 5

Binary: 101

Unary: 11111

We prefer unary representation:

easier to manipulate with Turing machines


unit-4/Turning Machine 61
Definition:

A function f is computable if
there is a Turing Machine M such that:

Initial configuration Final configuration


 w   f (w) 

q0 qf
initial state accept state

For all w D Domain


unit-4/Turning Machine 62
In other words:
A function f is computable if
there is a Turing Machine M such that:


q0 w  q f f ( w)

Initial Halts at Final


Configuration Configuration

For all w D Domain


unit-4/Turning Machine 63
Example
The function f ( x, y )  x  y is computable

x, y are integers

Turing Machine:

Input string: x0 y unary

Output string: xy 0 unary


unit-4/Turning Machine 64
x y

Start  1 1  1 0 1  1 

q0
initial state

The 0 is the delimiter that


separates the two numbers

unit-4/Turning Machine 65
x y

Start  1 1  1 0 1  1 

q0 initial state

x y

Finish  1 1  1 1 0 

q f final state
unit-4/Turning Machine 66
The 0 here helps when we use
the result for other operations

x y

Finish  1 1  1 1 0 

q f final state
unit-4/Turning Machine 67
Turing machine for function f ( x, y )  x  y

1  1, R 1  1, R 1  1, L

q0 0  1, R q1   , L q2
1  0, L q3

  , R
unit-4/Turning Machine
q4 68
Execution Example: Time 0
x y
x  11 (=2)
 1 1 0 1 1 
y  11 (=2) q0

Final Result
x y
 1 1 1 1 0 

unit-4/Turning Machine
q4 69
Time 0  1 1 0 1 1 

q0

1  1, R 1  1, R 1  1, L

q0 0  1, R q1   , L q2
1  0, L q3

  , R
unit-4/Turning Machine
q4 70
Time 1  1 1 0 1 1 

q0

1  1, R 1  1, R 1  1, L

q0 0  1, R q1   , L q2
1  0, L q3

  , R
unit-4/Turning Machine
q4 71
Time 2  1 1 0 1 1 

q0

1  1, R 1  1, R 1  1, L

q0 0  1, R q1   , L q2
1  0, L q3

  , R
unit-4/Turning Machine
q4 72
Time 3  1 1 1 1 1 

q1

1  1, R 1  1, R 1  1, L

q0 0  1, R q1   , L q2
1  0, L q3

  , R
unit-4/Turning Machine
q4 73
Time 4  1 1 1 1 1 

q1

1  1, R 1  1, R 1  1, L

q0 0  1, R q1   , L q2
1  0, L q3

  , R
unit-4/Turning Machine
q4 74
Time 5  1 1 1 1 1 

q1

1  1, R 1  1, R 1  1, L

q0 0  1, R q1   , L q2
1  0, L q3

  , R
unit-4/Turning Machine
q4 75
Time 6  1 1 1 1 1 

q2

1  1, R 1  1, R 1  1, L

q0 0  1, R q1   , L q2
1  0, L q3

  , R
unit-4/Turning Machine
q4 76
Time 7  1 1 1 1 0 

q3

1  1, R 1  1, R 1  1, L

q0 0  1, R q1   , L q2
1  0, L q3

  , R
unit-4/Turning Machine
q4 77
Time 8  1 1 1 1 0 

q3

1  1, R 1  1, R 1  1, L

q0 0  1, R q1   , L q2
1  0, L q3

  , R
unit-4/Turning Machine
q4 78
Time 9  1 1 1 1 0 

q3

1  1, R 1  1, R 1  1, L

q0 0  1, R q1   , L q2
1  0, L q3

  , R
unit-4/Turning Machine
q4 79
Time 10  1 1 1 1 0 

q3

1  1, R 1  1, R 1  1, L

q0 0  1, R q1   , L q2
1  0, L q3

  , R
unit-4/Turning Machine
q4 80
Time 11  1 1 1 1 0 

q3

1  1, R 1  1, R 1  1, L

q0 0  1, R q1   , L q2
1  0, L q3

  , R
unit-4/Turning Machine
q4 81
Time 12  1 1 1 1 0 

q4

1  1, R 1  1, R 1  1, L

q0 0  1, R q1   , L q2
1  0, L q3

  , R
HALT & accept
unit-4/Turning Machine
q4 82
Another Example
The function f ( x)  2 x is computable

x is integer

Turing Machine:

Input string: x unary

Output string: xx unary


unit-4/Turning Machine 83
x

Start  1 1  1 

q0 initial state

2x

Finish  1 1  1 1 1 

q f accept state
unit-4/Turning Machine 84
Turing Machine Pseudocode for f ( x)  2 x

• Replace every 1 with $

• Repeat:
• Find rightmost $, replace it with 1

• Go to right end, insert 1

Until no more $ remain

unit-4/Turning Machine 85
Turing Machine for f ( x)  2 x

1  $, R 1  1, L 1  1, R

q0   , L q1 $  1, R q2
  , R   1, L
q3
unit-4/Turning Machine 86
Example
Start Finish
 1 1   1 1 1 1 
q0 q3
1  $, R 1  1, L 1  1, R

q0   , L q1 $  1, R q2
  , R   1, L
q3 unit-4/Turning Machine 87
Back to binary!
• How do we add two binary numbers on a Turing
machine?

• Instead of dealing with it directly, let us divide the


problem into 2 parts
• How to subtract 1 from a binary number
• How to add 1 to a binary number

• HW – think about how to do this without


subroutines/functions/methods
unit-4/Turning Machine 88
How to add 1 to a binary number?
•What is 11000 + 1 = ?

•What is 10111 + 1 = ?

•The pattern that emerges is …..

unit-4/Turning Machine 89
Multitape Turing Machines
• A multitape Turing machine is like an ordinary TM
but it has several tapes instead of one tape.
• Initially the input starts on tape 1 and the other tapes
are blank.
• The transition function is changed to allow for
reading, writing, and moving the heads on all the
tapes simultaneously.
– This means we could read on multiples tape and move in
different directions on each tape as well as write a
different symbol on each tape, all in one move.

unit-4/Turning Machine 90
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 a type 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.

unit-4/Turning Machine 91
Multitape Machine
… 0 1 0 1 0 B …

… a a a B …
M

… b a B …

Equivalent Single Tape Machine:

unit-4/Turning Machine 92
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.
– We can prove this theorem by simulating any nondeterministic TM, N,
with a deterministic TM, D.

unit-4/Turning Machine 93
Nondeterministic TM
• Visualize N as a tree with branches whenever we fork off to two (or more)
simultaneous machines.
– Use D to try all possible branches of N, sequentially.
– If D ever finds the accept state on one of these branches, then D accepts.
– It is quite likely that D will never terminate in the event of a loop if there is no
accepting state.
• Search be done in a breadth-first rather than depth-first manner.
– An individual branch may extend to infinity, and if we start down this branch
then D will be stuck forever when some other branch may accept the input.
• We can simulate N on D by a tape with a queue of ID’s and a scratch tape
for temporary storage.
– Each ID contains all the moves we have made from one state to the next, for
one “branch” of the nondeterministic tree.
– From the previous theorem, we can make as many multiple tapes as we like and
this is still equivalent to a single tape machine. Initially, D looks like the
following:

unit-4/Turning Machine 94
Nondeterministic TM
• ID1 is the sequence of moves we make from the start state. The *
indicates that this is the current ID we are executing.
• We make a move on the TM. If this move results in a “fork” by following
nondeterministic paths, then we create a new ID and copy it to the end of
the queue using the scratch tape.

Scratch Tape
D

… ID1* Queue of ID’s


unit-4/Turning Machine 95
Nondeterministic TM
• For example, say that in ID1 we have two nondeterministic
moves, resulting in ID2 and ID3:

Scratch Tape
D

… ID1* # ID2 # ID3

unit-4/Turning Machine 96
Nondeterministic TM
• After we’re done with a single move in ID1, which
may result in increasing the length of ID1 and storing
it back to the tape, we move on to ID2:

Scratch Tape
D

… ID1 # ID2* # ID3

unit-4/Turning Machine 97
Nondeterministic TM
• If any one of these states is accepting in an ID, then the
machine quits and accepts.
• If we ever reach the last ID, then we repeat back with the first
ID.

• Note that although the constructed deterministic TM is


equivalent to accepting the same language as a
nondeterministic TM, the deterministic TM might take
exponentially more time than the nondeterministic TM.
– It is unknown if this exponential slowdown is necessary. We’ll come
back to this in the discussion of P vs. NP.

• Theorem: Since any deterministic Turing Machine is also


nondeterministic (there just happens to be no nondeterministic
moves), there exists a nondeterministic TM for every
deterministic TM.
unit-4/Turning Machine 98
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

unit-4/Turning Machine 99
THANK YOU!!!

unit-4/Turning Machine 100

You might also like