0% found this document useful (0 votes)
35 views

Flat Unit 4

Uploaded by

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

Flat Unit 4

Uploaded by

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

Formal Languages and Automata Theory

UNIT-4
Turing Machine
Dr. K. Dhanasekaran
School of Computing
Department of Data Science and Business Systems
SRMIST KTR Campus, Chennai
Turing Machine-Definition
⚫ A Turing Machine accepts the recursively enumerable
language generated by type 0 grammars.
⚫ It was invented in 1936 by Alan Turing.
⚫ A Turing Machine (TM) is a mathematical model which
consists of an infinite length tape divided into cells on which
input is given.
⚫ It consists of a head which reads the input tape.
⚫ A state register stores the state of the Turing machine.
⚫ After reading an input symbol, it is replaced with another
symbol, its internal state is changed, and it moves from one cell
to the right or left.
⚫ If the TM reaches the final state, the input string is accepted,
otherwise rejected.
Turing Machine-Formal Definition
⚫ A TM can be formally described as a 7-tuple
(Q, X, ∑, δ, q0, B, F) where −
⚫ Q is a finite set of states
⚫ X is the tape alphabet
⚫ ∑ is the input alphabet
⚫ δ is a transition function;
δ : Q × X → Q × X × {Left_shift, Right_shift}.
⚫ q0 is the initial state
⚫ B is the blank symbol
⚫ F is the set of final states
Example of Turing machine
⚫ Turing machine M = (Q, X, ∑, δ, q0, B, F) with
⚫ Q = {q0, q1, q2, qf}
⚫ X = {a, b}
⚫ ∑ = {1}
⚫ q0 = {q0} Tape Present State Present State Present State
⚫ B = blank symbol alphabet ‘q0’ ‘q1’ ‘q2’
symbol
⚫ F = {qf }
a 1Rq1 1Lq0 1Lqf
⚫ δ is given by −
b 1Lq2 1Rq1 1Rqf

Here the transition 1Rq1 implies that the write symbol is 1,


the tape moves right, and the next state is q1.
Similarly, the transition 1Lq2 implies that the write symbol is 1,
the tape moves left, and the next state is q2.
Time and Space Complexity of a Turing
Machine
⚫ the time complexity: the measure of the number of times
the tape moves when the machine is initialized for some
input symbols
⚫ the space complexity: the number of cells of the tape
written.
⚫ Time complexity all reasonable functions −
T(n) = O(n log n)
⚫ TM's space complexity −
S(n) = O(n)
TM
⚫ A TM accepts a language if it enters into a final state for
any input string w.
⚫ A language is recursively enumerable (generated by
Type-0 grammar) if it is accepted by a Turing machine.
⚫ A TM decides a language if it accepts it and enters into a
rejecting state for any input not in the language.
⚫ A language is recursive if it is decided by a Turing
machine.
⚫ There may be some cases where a TM does not stop.
⚫ Such TM accepts the language, but it does not decide it.
Designing a Turing Machine
⚫ Example 1
⚫ Design a TM to recognize all strings consisting of an odd number
of α’s.
⚫ Solution
⚫ The Turing machine M can be constructed by the following moves −
⚫ Let q1 be the initial state.
⚫ If M is in q1; on scanning α, it enters the state q2 and
writes B (blank).
⚫ If M is in q2; on scanning α, it enters the state q1 and
writes B (blank).

⚫ From the above moves, we can see that M enters the state q1 if it
scans an even number of α’s, and

⚫ it enters the state q2 if it scans an odd number of α’s. Hence q2 is


the only accepting state.
⚫ Hence,
M = {{q1, q2}, {1}, {1, B}, δ, q1, B, {q2}}

⚫ δ is given by −

Tape alphabet Present State ‘q1’ Present State ‘q2’


symbol
α BRq2 BRq1
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.
Example 2 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.
Example 2 solution
⚫ Hence,
⚫ M = {{q0, q1, q2, q3, q4, qf}, {0,1, B}, {1, B}, δ, q0, B,
{qf}}
⚫ where δ is given by −

Tape Present Present Present Present Present


alphabet State State State State State
symbol ‘q0’ ‘q1’ ‘q2’ ‘q3’ ‘q4’
0 BRq1 BRq1 ORq2 - OLq4
1 1Rq2 1Rq2 1Rq2 - 1Lq4
B BRq1 BLq3 BLq4 OLqf BRqf
s2 Variants of Turing machines,
Simple Examples
⚫ Multi-track Turing machine

⚫ Multi-tape Turing machine

⚫ Multi-tape Multi-head TM Note: If any language is accepted by


TM that means any of its variant also
⚫ Two-way Infinite Tape TM accepts that language.

⚫ Non-Deterministic Turing Machine

⚫ Multi-dimensional Tape Turing Machine

⚫ Multi-head Turing Machine


Multi-track TM
⚫ In a standard n-tape Turing machine, n heads move independently
along n tracks.

⚫ A k-track Turing machine(for some k>0) has k-tracks and one R/W
head that reads and writes all of them one by one.

⚫ A k-track Turing Machine can be simulated by a single track Turing


machine

⚫ In multi-track TM, we have semi-infinite tape with multiple tracks.

⚫ Like a TM, This also has a finite control (or head) pointing to a
particular cell.
Example
Both TM and M- TM are having the same power
Multi-tape Turing Machine
⚫ It has multiple tapes and controlled by a single head.

⚫ This multi-tape Turing machine has same power like


multi-track Turing machine.

⚫ Move multiple heads independently.

⚫ Multi-tape Turing machine can be simulated by


single-tape Turing machine.
Multi-tape Multi-head Turing Machine
⚫ The multi-tape Turing machine has multiple tapes and
multiple heads.

⚫ Each tape controlled by separate head.

⚫ Multi-Tape Multi-head Turing machine can be simulated


by standard Turing machine.
Example
Exercise
⚫ Prove that multi-track Turing Machine can be simulated
by a Multi-Tape Turing Machine.
Two-way infinite tape Turing machine
⚫ Infinite tape of two-way infinite tape Turing machine is
unbounded in both directions left and right.

⚫ Two-way infinite tape Turing machine can be simulated


by one-way infinite Turing machine(standard Turing
machine).
Non-deterministic Turing Machine
⚫ A non-deterministic Turing machine has a single, one way
infinite tape.

⚫ For a given state and input symbol has atleast one choice
to move (finite number of choices for the next move), at
each state it has several choices of path that it might
follow for a given input string.

⚫ A non-deterministic Turing machine is equivalent to


deterministic Turing machine.
Non-deterministic Turing Machine
⚫ A non-deterministic Turing machine can be formally
defined as a 6-tuple (Q, X, ∑, δ, q0, B, F) where −
⚫ Q is a finite set of states
⚫ X is the tape alphabet
⚫ ∑ is the input alphabet
⚫ δ is a transition function;
⚫ δ : Q × X → P(Q × X × {Left_shift, Right_shift}).
⚫ q0 is the initial state
⚫ B is the blank symbol
⚫ F is the set of final states
Non-deterministic Turing Machine-Example
Multi-dimensional Tape Turing Machine
⚫ It has multi-dimensional tape where head can move any
direction that is left, right, up or down.

⚫ Multi dimensional tape Turing machine can be simulated


by one-dimensional Turing machine
Multi-head Turing Machine
⚫ A multi-head Turing machine contain two or more heads
to read the symbols on the same tape.

⚫ In one step all the heads sense the scanned symbols and
move or write independently.

⚫ Multi-head Turing machine can be simulated by single


head Turing machine.
Nondeterministic Turing Machines
⚫ In a Non-Deterministic Turing Machine, for every state and
symbol, there are a group of actions the TM can have.
⚫ So, here the transitions are not deterministic.
⚫ The computation of a non-deterministic Turing Machine is a tree
of configurations that can be reached from the start
configuration.
⚫ An input is accepted if there is at least one node of the tree
which is an accept configuration, otherwise it is not accepted.
⚫ If all branches of the computational tree halt on all inputs, the
non-deterministic Turing Machine is called a Decider and if for
some input, all branches are rejected, the input is also rejected.
Nondeterministic TMs-definition
⚫ A non-deterministic Turing machine can be formally
defined as a 6-tuple (Q, X, ∑, δ, q0, B, F) where −
⚫ Q is a finite set of states
⚫ X is the tape alphabet
⚫ ∑ is the input alphabet
⚫ δ is a transition function;
⚫ δ : Q × X → P(Q × X × {Left_shift, Right_shift}).
⚫ q0 is the initial state
⚫ B is the blank symbol
⚫ F is the set of final states
Nondeterministic TMs and equivalence with
deterministic TMs
⚫ prove:
⚫ Every nondeterministic Turing machine has an equivalent
Turing machine
⚫ Proof method:
⚫ Use the view of the NTM N's computation as a tree
⚫ Have a deterministic Turing machine D perform a
breadth-first search on this tree
Nondeterministic TMs and equivalence with
deterministic TMs
Nondeterministic TMs and equivalence with
deterministic TMs
Corollary
Check whether TM accepts palindrome or not
⚫ A string w is called palindrome if reading w from left to
right gives the same result as reading w from right to left.

⚫ An even palindrome has even number of symbols.


Check whether TM is palindrome or not
⚫ Input : abaaba
⚫ Output :YES
⚫ Input :abba
⚫ Output :YES
⚫ Input :abbaba
⚫ Output :NO
⚫ Input :Empty String or ε
⚫ Output :YES
Check whether TM is palindrome or not
⚫ Basic representation
Check whether TM is palindrome or not
⚫ Start of Computation :
The tape contains the input string w, the tape head is on
the leftmost symbol of w, and the Turing machine is in the
start state Q0.
Check whether TM is palindrome or not
⚫ Basic Idea :
⚫ The tape head reads the leftmost symbol of w, deletes this
symbol and “remembers” it by means of a state.
⚫ Then the tape head moves to rightmost symbol and tests whether it
is equal to the (already deleted) leftmost symbol.
⚫ If they are equal, then the rightmost symbol is deleted, the
tape head moves to the new leftmost symbol, and the whole
process is repeated .
⚫ Else the machine can’t reach the final state and the string will be
rejected.
Check whether TM is palindrome or not

Meanings of symbols used:


R, L – direction of movement of one unit on either side .
B-Blank
a, b-symbols whose combination string is to be tested
Check whether TM is palindrome or not
⚫ Working Procedure :
⚫ Step-1:
⚫ We start with Q0 state if we get a symbol “a” as input then there should also
be “a” at the ending of the string then only the string is palindrome and we
have to verify that.
⚫ We first make the current input “a” to B blank and go to state Q1 move rightwards to
traverse the string till we reach the end. Keep the input symbols a or b whichever
comes in ways should be remain unchanged.
⚫ We can reach the end of the string when we get Blank as the input symbol
then we change state to Q2 and test if the previous symbol is “a” then we
change to state Q3 and then only we will replace it by Blank and we have
successfully tested that string is palindrome till this point
⚫ now we will traverse back or leftwards (keeping a and b unchanged which comes in
way) on the string and till we get Blank which is the symbol which we made Blank at
the start and we change state to Q0.Now we repeat the same procedure for “b” as
input.
Check whether TM is palindrome or not
⚫ Step-2:
⚫ Till this point if the string was palindrome then it would
have returned to state Q0 after all iterations and if the string
was not palindrome then we would stuck at the states Q2 or
Q5
⚫ when stuck at these points we cant reach Q0 and hence can not
reach the final state or acceptance state Q7.
Check whether TM is palindrome or not
⚫ Step-3:
⚫ If the string was palindrome then there will be only Blank
symbol left and hence we test it at Q0 if we get blank hence
the string gets accepted and is palindrome,

⚫ at this point there is one more condition is included which is


of null string or as empty string is also palindrome hence
gets accepted.
Turing machine for subtraction
⚫ Problem-1: Draw a Turing machine which subtract two
numbers.
Turing machine for subtraction
⚫ Steps:
⚫ Step-1. If 0 found convert 0 into X and go right then convert
all 0’s into 0’s and go right.
⚫ Step-2. Then convert C into C and go right then convert all X
into X and go right.
⚫ Step-3. Then convert 0 into X and go left then convert all X
into X and go left.
⚫ Step-4. Then convert C into C and go left then convert all 0’s
into 0’s and go left then convert all X into X and go right and
repeat the whole process.
⚫ Step-5. Otherwise if C found convert C into C and go right
then convert all X into B and go right then convert 0 into 0 and
go left and then stop the machine.
Turing machine for subtraction

Here, q0 shows the initial state and q1, q2, q3, q4, q5are the transition states and q6
shows the final state.
And X, 0, C are the variables used for subtraction and R, L shows right and left.
Exercise
⚫ Problem-2: Draw a Turing machine which subtract two
numbers m and n, where m is greater then n.
Turing machine for multiplication
⚫ Problem: Draw a turing machine which multiply two
numbers.
Turing machine for multiplication
⚫ Steps:
⚫ Step-1. First ignore 0’s, C and go to right & then if B found convert it into C and go
to left.
⚫ Step-2. Then ignore 0’s and go left & then convert C into C and go right.
⚫ Step-3. Then convert all X into X and go right if 0 found convert it into X and go to
left otherwise if C found convert it into B and go to right and stop the machine.
⚫ Step-4. If then X found convert it into X and go left then C into C and left then Y
into Y and left.
⚫ Step-5. Then if B found convert it into B and right then if Y into 0 and right or if C
into C and right and go to step 3 and repeat the process otherwise if 0 found after 4th
step then convert it into Y and right then Y into Y and right then C into C and right
then 0 into 0 or X into X and right then C into C and right then 0 into 0 and right
then B into 0 and left then 0 into 0 and left then C into C and left then 0 into 0 or X
into X and left then C into C and left.
⚫ Step-6. Then repeat the 5th step.
Turing machine for multiplication

Here, q0 shows the initial state and q1, q2, ….., q10, q11are the transition states and
q12shows the final state.
And X, Y, 0, C are the variables used for multiplication and R, L shows right and left.
Closure properties of Turing machines
Closure properties of Turing machines
Closure properties of Turing machines
Closure properties of Turing machines
Closure properties of Turing machines
How turing machine can be used as enumerator
⚫ A Turing machine can produce all the strings of the
language accepted by that particular Turing machine. We
call such a Turing machine an enumerator.

⚫ how a Turing machine can work as an enumerator for the


language L={anbn : n≥0}
How turing machine can be used as enumerator
Turing machine codes
Turing machine codes
Turing machine codes
Turing machine codes
Turing machine codes
Turing machine codes
Unrestricted Grammars and Turing machine
Unrestricted Grammars and Turing machine
Unrestricted Grammars and Turing machine
Unrestricted Grammars and Turing machine
Unrestricted Grammars and Turing machine
Unrestricted Grammars and Turing machine
Unrestricted Grammars and Turing machine
References
⚫ Textbook
⚫ https://www.geeksforgeeks.org/turing-machine-to-check-wheth
er-given-string-is-even-palindrome-or-not/
⚫ https://www.geeksforgeeks.org/turing-machine-for-subtraction/
⚫ https://www.geeksforgeeks.org/turing-machine-for-multiplicati
on/
⚫ https://cse.iitkgp.ac.in/~abhij/course/theory/FLAT/Spring20/sli
des/UG-handout.pdf
⚫ https://www.cs.drexel.edu/~knowak/cs525_winter_2016_e/pre
sentation.pdf
⚫ https://www.tutorialspoint.com/automata_theory/non_determin
istic_turing_machine.htm
Thank you

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy