2. WHAT IS TURING MACHINE
• The mathematical models (FAs, TGs and PDAs) can decide
whether a string is accepted or not by them so, these
models are language identifiers. However, there are still
some languages which can not be accepted by them e.g.
there does not exist any FA, TG or PDA accepting any non-
CFLs. Alan Mathison Turing developed the machines called
Turing machines, which accept some non-CFLs as well, in
addition to CFLs.
MCS EVE 2012-14 5/24/2015
2
3. DEFINITION OF TURING MACHINE
• A Turing machine (TM) consists of the following:
• An alphabet Σ of input letters.
• An input TAPE partitioned into cells, having infinite many locations in
one direction. The input string is placed on the tape starting its first
letter on the cell i , the remaining cells of the TAPE are initially filled
with blanks. (∆’s)
• A tape Head can read the contents of cell on the TAPE in one step. It
can replace the character at any cell and can moved itself to the
next cell to the right or to the left of that it has just read. Initially the
TAPE Head is at the cell i. The TAPE Head can’t move to the left of cell
i. the location of the TAPE Head is denoted by ⌂ .
MCS EVE 2012-14 5/24/2015
3
4. DEFINITION (CONT…)
• An alphabet Γ of characters that can be printed on the TAPE by the
TAPE Head. Γ may include the letters of Σ. Even the TAPE Head can
print blank ∆, which means to erase some character from the TAPE.
• Finite set of states containing exactly one START state and some
(may be none) HALT states that cause execution to terminate when
the HALT states are entered.
• A program which is the set of rules, which show that which state is to
be entered when a letter is read form the TAPE and what character
is to be printed. This program is shown by the states connected by
directed edges labeled by triplet (letter, letter, direction). The first
letter is what tape head read and second letter is what it printed
before leave the state (may be replaced the letter with the printed
character). The direction tells the tape head whether to move one
cell to the right, R, or one cell to the left, L.MCS EVE 2012-14 5/24/2015
4
5. DETERMINISM V/S NON-DETERMINISM
• It can be seen that there may not any outgoing edge at certain states
for certain letter to be read from the tape, which creates non-
determinism in TM (just like TGs).
• It may also be seen that unlike TGs at certain states, there can’t be
more than one outgoing edge for certain letter to be read from the
tape.
• The machine crashes if there is no path for a letter to be read from the
tape and corresponding string will be rejected. Machine also crashes if
instruct it to move HEAD to left of the location i.
• To terminate execution of certain input string successfully, a HALT state
must be entered and then the corresponding string will be accepted
by the TM.
MCS EVE 2012-14 5/24/2015
5
6. REPRESENTATION OF TAPE AND TAPE HEAD
a b a ∆ ∆ ∆ ∆ ∞
i ii iii iv
⌂
TAPE
HEAD
MCS EVE 2012-14 5/24/2015
6
7. EXAMPLE OF TM
Let input string aba be run over this TM having ∑={a,b} then the
TAPE will look like..
a b a ∆ ∆ ∞
i ii iii iv
⌂
TAPE
HEAD
MCS EVE 2012-14 5/24/2015
7
8. EXAMPLE (CONT..)
Starting from STATR state, reading a from the TAPE and according to the
TM program, a will be printed i.e. a will replace a and TAPE head will
moved one cell right and TM entered in state 2.
a b a ∆ ∆ ∞
i ii iii iv
⌂
TAPE
HEAD
MCS EVE 2012-14 5/24/2015
8
9. EXAMPLE (CONT..)
At state 2, reading b from the TAPE and according to the TM program, b
will be printed i.e. b will replace b and TAPE head will moved one cell
right and TM entered in state 3.
a b a ∆ ∆ ∞
i ii iii iv
⌂
TAPE
HEAD
MCS EVE 2012-14 5/24/2015
9
10. EXAMPLE (CONT..)
At state 3, reading a from the TAPE and according to the TM program, a
will be printed i.e. a will replace a and TAPE head will moved one cell
right and state of TM remain unchanged.
a b a ∆ ∆ ∞
i ii iii iv
⌂
TAPE
HEAD
MCS EVE 2012-14 5/24/2015
10
11. EXAMPLE (CONT..)
At state 3, reading ⌂ from the TAPE and according to the TM program,
⌂ will be printed i.e. ⌂ will replace ⌂ and TAPE head will moved one cell
right and TM entered into the HALT state which terminate the program.
a b a ∆ ∆ ∞
i ii iii iv
⌂
TAPE
HEAD
MCS EVE 2012-14 5/24/2015
11
12. EXAMPLE (CONT..)
NON-Determinism: At state 2 there is no path for letter a which creates
non-determinism, if we read a at this state the machine will be crashed.
• The string aba is accepted by this TM. TM reaches at HALT state by
reading ∆. TM must read ∆ to terminate the program then the
string supposed to be accepted. This machine accepts the
language expressed by (a+b)b(a+b)*.
• If TM entered in HALT state before completing the string it doesn't
mean string is accepted.MCS EVE 2012-14 5/24/2015
12
13. EXAMPLE (CONT..)
• The following example can also be seen as:
1 2 3 3 HALT
aba aba aba aba∆
• This process called execution chain, also called process chain or trace of
execution.
MCS EVE 2012-14 5/24/2015
13
14. THEOREM
• Theorem:
• Every Regular Language is accepted by some TM.
• Example of EVEN-EVEN Language
MCS EVE 2012-14 5/24/2015
14
15. AN OTHER EXAMPLE
• We discuss earlier example of Regular Language which is
accepted by some TM.
• Lets see an example of NON-Regular language which is also
accepted by some TM.
• Language L={ anbn : n=1,2,3,….} over ∑={a,b}.
• Language L is non-regular there is no FA exist for this
language but a TM exist which accept the strings
generated from this language and reject those which are
not belong to that language.
MCS EVE 2012-14 5/24/2015
15
16. EXAMPLE (CONT…)
The conversion of a to A and b to B means that these letters has
been counted and we are going to find next one. A and B use
here as a marker.
Lets run string aabb generated from L to test the TM.
MCS EVE 2012-14 5/24/2015
16
17. a a b b ∆ ∞
⌂
At START state we read a convert it to A to
indicate it is counted and now move right to
find b the pair of counted a.
At state 2 we read a remain it unchanged go right to find b.
A a b b ∆ ∞
⌂
A a b b ∆ ∞
⌂
At state 2 now we read b change it to B because it is the pair of 1st a and move
left to find more left most a’s to complete the string.
A a B b ∆ ∞
⌂
Place string on TAPE
EXAMPLE (CONT…)
MCS EVE 2012-14 5/24/2015
17
18. At state 3 we read a remain it unchanged
and move left to find left most a.
At state 4 we read A which means there is no more left most
a’s so remain it unchanged and move right to read a.
A a B b ∆ ∞
⌂
A a B b ∆ ∞
⌂
At SATRT state now we read left most a change it to A and move right to find b
which is its pair.
A A B b ∆ ∞
⌂
A a B b ∆ ∞
⌂
EXAMPLE (CONT…)
MCS EVE 2012-14 5/24/2015
18
19. At state 2 we read B remain it unchanged
and move right to find b.
At state 2 we read b change it into B and move left to find
more a’s if exist.
A A B b ∆ ∞
⌂
A A B B ∆ ∞
⌂
At state 3 we read B remain it unchanged and move left to find more a’s if exist.
A A B b ∆ ∞
⌂
A A B B ∆ ∞
⌂
EXAMPLE (CONT…)
MCS EVE 2012-14 5/24/2015
19
20. At state 3 we read A remain it unchanged
and move right to terminate program
because there is no more a’s to scan.
At state 5 we read B two times remain them unchanged
and move right two times to terminate.
A A B B ∆ ∞
⌂
A A B B ∆ ∞
⌂
At state 5 we read ∆ remain it unchanged and right.
A A B B ∆ ∞
⌂
A A B B ∆ ∞
⌂
Now we entered in HALT state which means the
string is accepted by this TM.
EXAMPLE (CONT…)
MCS EVE 2012-14 5/24/2015
20
21. REMEMBER
• In previous example we take a string aabb which is generated by the
language anbn , we see that string is accepted by the TM that we
construct for language.
• It doesn't means the previous TM is the correct TM for our language.
• The TM which accept the strings from its corresponding language
should must reject those strings which are not belong to its
corresponding language.
• Try the following string yourself and check this is rejected by the TM or
not.
• aaabb must be rejected by the previous TM because it is not generated by its
corresponding language.
MCS EVE 2012-14 5/24/2015
21
22. ANOTHER EXAMPLE
• Another non-CFG language is language of anbnan over ∑={a,b}
MCS EVE 2012-14 5/24/2015
22
23. We run aabbaa on this machine to see the
execution..
Here is the tracing way..
STSRT 2 2
aabbaa *abbaa *abbaa
3 3 4
*abbaa *abbaa *abbaa
4 5 5
*abaaa *abaaa *abaaa∆
6 7 8
*abaaa *abaa *aba
8 8 8
*aba *aba *aba
START 2 3
*aba **ba **ba
4 5 5
**ba **aa **aa∆
6 7 8 START
**aa **a ** **∆
MCS EVE 2012-14 5/24/2015
23
26. MORE ON TURING MACHINES
• INSERT subprogram:
• Sometimes in the running of TM, we may want to insert a character into the string
on the TAPE exactly at the spot where the TAPE HEAD is pointing.
• This means that the newly inserted character will occupy this cell and every right
character on the TAPE will be shifted one cell right. The left character remains
unchanged.
• After insertion we want the TAPE HEAD moved on right of newly inserted
character.
• This subprogram not depend what the TM is doing. It is independent subprogram
can be called in any other TM by specifying the what character to be inserted at
what location.
MCS EVE 2012-14 5/24/2015
26
27. INSERT SUBPROGRAM(EXAMPLE)
• If the character ‘b’ is inserted at the cell where the TAPE HEAD is pointing as
shown below
then, it is expressed as
The function of subprogram INSERT ‘b’ can be observed from the following
diagram
∞ b X a b b X ∆ ∆∞
⌂
∞ b X a b b X ∆ ∆∞
⌂
INSERT b
∞ b X b a b b X ∆ ∆∞
⌂
MCS EVE 2012-14 5/24/2015
27
29. INSERT SUBPROGRAM(EXAMPLE)
• Let insert ‘b’ at current location of TAPE HEAD as we discuss earlier.. What we
do??
• we insert a character ‘Q’ as a marker then we shift all characters right one cell,
and then move back to ‘Q’ to replace it by ‘b’.
• Remember we take previous TAPE we discussed which have only characters a’s,
b’s, X’s.
• What the states of this subprogram doing??
• We start from state 1. In this state we read ‘a’ (either a, b or X) and then we write ‘Q’
and move TAPE HEAD right one cell.
• Here in previous cell where ‘Q’ is inserted we displaced a character… ‘a’ in this
example.
• We must want to remember what we displaced in inserting the ‘Q’.
• This require some memory to remember….
MCS EVE 2012-14 5/24/2015
29
30. INSERT SUBPROGRAM(EXAMPLE)
• In previous subprogram diagram state 2,3 and 4 are used as memory to
remember ‘a’, ‘b’ and ‘X’ respectively.
Remember ‘a’ is
displaced
Remember ‘b’ is
displaced
Remember ‘X’ is
displaced
MCS EVE 2012-14 5/24/2015
30
31. • In our example at state 1 we read ‘a’ and
replaced it with ‘Q’ and then entered in state 2
because ‘a’ is displaced, TAPE HEAD is also
moved one cell right.
• At state 2 we read ‘b’, we remember that we
must place ‘a’ that we displaced, so we write
an ‘a’, but now we realize that we have just
displaced a ‘b’ and entered in state 3 because
state 3 remember the ‘b’ is displaced. The TAPE
HEAD also moves one cell right.
⌂
∞ b X Q b b X ∆ ∆∞
∞ b X Q a b X ∆ ∆∞
⌂
2
3
4
7
5
6
MCS EVE 2012-14 5/24/2015
31
32. • At state 3 we read ‘b’ we replace it with
‘b’ and remain here, here we again
displaced another ‘b’, the TAPE HEAD
moves one cell right.
• At state 3 we read ‘X’ this time replaced
it with ‘b’ and entered in state 4 because
now we displaced ‘X’, the TAPE HEAD
move one cell right again.
• At state 4 we read ‘∆’ which means the
end of string, so we replace it last
displaced character which was ‘X’, we
entered state 5 now. TAPE HEAD also
move one cell right.
∞ b X Q a b X ∆ ∆∞
∞ b X Q a b b ∆ ∆∞
⌂
∞ b X Q a b b X ∆∞
⌂
⌂
2
3
4
7
5
6
MCS EVE 2012-14 5/24/2015
32
33. • At state 5 we read ∆ replace it with ∆
and entered in state 6 , move TAPE
HEAD left one cell.
• At state 6 either read a, b or X replace
with itself by moving left and remain at
state 6 until we read ‘Q’ .
• When we read ‘Q’ replace it with ‘b’
because our example is to insert ‘b’
and move the TAPE HEAD one cell right.
REMEMBER: This is independent subprogram
and we suppose only three characters on
the TAPE to reduce the complexity.
⌂
∞ b X b a b b X ∆ ∆∞
2
3
4
7
5
6
MCS EVE 2012-14 5/24/2015
33
34. MORE ON TURING MACHINE
• DELETE subprogram:
• Sometimes, a character is required to be DELETED on the TAPE exactly at the
spot where the TAPE Head is pointing, so that the other characters on the
right of the TAPE Head are moved one cell left.
• The characters to the left of the pointed cell are also required to remain as
such.
• The subprogram of deletion is independent and can be incorporated at any
time with any TM program specifying what character to be deleted at what
location.
MCS EVE 2012-14 5/24/2015
34
35. DELETE SUBPROGRAM(EXAMPLE)
• If the character ‘b’ is deleted at the cell where the TAPE HEAD is pointing as
shown below
then, it is expressed as
The function of subprogram DELETE ‘b’ can be observed from the following
diagram
∞ b X a b b X ∆ ∆∞
⌂
∞ b X a b b X ∆ ∆∞
⌂
DELETE b
∞ b X a b b X ∆ ∆ ∆∞
⌂
MCS EVE 2012-14 5/24/2015
35
37. DELETE SUBPROGRAM(EXAMPLE)
• What the states of this subprogram doing??
• We start from state 1. In this state we read ‘a’ (either a, b or X) and then we write ‘∆’
and move TAPE HEAD right until we reach at the end of the TAPE non- ∆.
• At once we read ∆ it means we are at the end of input now.
• Now we move left and replace what we displaced similar to INSERT subprogram
(it work inverse of INSERT subprogram).
• This require some memory to remember…. Similar to INSERT subprogram DELETE
subprogram also have some memory to remember what is displaced at some
location.
MCS EVE 2012-14 5/24/2015
37