Tutorial Nine
Tutorial Nine
Models of Computations
Tutorial Nine: Turing Machine
1
Quick Review: Computational Models
• FA (DFA and NFA)
• Good models for devices that have small memory.
• PDA
• Good models for devices that have unlimited stack (LIFO memory).
• Some tasks are beyond capabilities of these models.
• Solution: Generalized computational model Turing Machine
2
Quick Review: Turing Machine
• Similarity between Turing Machine and Push Down Automaton
• Both have a finite-state machine as a central component.
• Difference between Turing Machine and Push Down Automaton
• Where a pushdown automaton uses a stack (LIFO memory) for storage, a TM
has an unlimited and unrestricted memory.
3
Quick Review: Turing Machine(2)
• Two Main types of Turing Machine:
• One-way Infinite Tape Turing Machine.
• Uses a tape that extends indefinitely in one direction.
• Has a tape head that can read and write symbols and move around on the tape.
• Two-way Infinite Tape Turing Machine.
• Uses a tape, which is considered to be infinite in both directions.
• Has a tape that consists of a series of squares, each of which can hold a single symbol.
• The tape read-write head, can read a symbol from the tape, write a symbol to the tape,
and move one square in either direction.
4
Quick Review: Turing Machine(3)
• Because the Turing machine has to be able to find its input, and to
know when it has processed all of that input, we require:
• The tape is initially blank (every symbol is □ ) except possibly for a finite,
contiguous sequence of symbols.
• If there are initially nonblank symbols on the tape, the tape head is initially
positioned on one of them.
• Initially, the tape contains only the input string and is blank
everywhere else. If the machine needs to store information, it may
write this information on the tape.
5
Quick Review: Formal Definition of TM
6
Quick Review: TM Transition
R Move Right
L Move Left
N No MOVE
7
Quick Review: Transition on TM
8
Quick Review: Uses of Turing Machine
• TM can be used as acceptor
• Produce only accept/reject answer
• TM can be used as transducer
• Can produce more complicated output than just a YES/NO decision.
• The results left on the tape when TM finishes can be considered as OUTPUT
of Turing machine.
9
Remark
• Initially:
• If initial position of head on tape is not mentioned then assume it is position
on start/Left end of input string.
• If initial position of head on tape is not placed on start/Left end of input string
then it must be moved to be positioned there.
• After performing desired functionality:
• If not mentioned, leave head pointing on first blank symbol after your
computation.
• Other wise, we have to move the head to point to desired position.
10
Exercises(1)
1. Design a TM that moves right until it encounters a blank,
whereupon it halts. ={0,1}.
11
Illustration
12
Solution
□/ □, N
q0 halt
0/0, R
1/1, R
13
Exercises(2)
2. Design a TM that replaces the rightmost character in its string of
non-blank characters with a blank, whereupon it halts. ={0,1}
14
Illustration
15
Solution
0/ □, N
□/ □, L 1/ □, N
q0 q1 halt
0/0, R
1/1, R
16
Exercises(3)
3. Design a TM having input symbols ={0,1} , that inverts each
symbol in its input (i.e., replaces each 0 with 1 and vice versa ) then
halts. Initially the read/write head is located on one of the nonblank
symbols.
17
Illustration
18
Solution
□/ □, R □ / □, N
q0 q1 halt
0/0, L 0/1, R
1/1, L 1/0, R
19
Exercises(4)
4. Design a TM having input symbols ={0,1} , that inverts each
symbol in its input (i.e., replaces each 0 with 1 and vice versa ) then
halts with the head located at the left end of the input. Initially the
read/write head is located on the left end of the input.
20
Illustration
21
Solution
□/ □, L □ / □, R
q0 q1 halt
0/1, R 0/0, L
1/0, R 1/1, L
22
Exercises(5)
5. Design a TM which works as an eraser (i.e., clear all cells in the
input string). The read/write head in middle of string, cells are to be
cleared from right to left. Ʃ = {0,1}
23
Illustration
24
Solution
□/ □, L □ / □, N
q0 q1 halt
0/0, R 0/ □, L
1/1, R 1/ □, L
25
Exercises(6)
6. Design a TM that recognizes the set of all strings of 0’s and 1’s
containing at least one 1. The read/write head is located on the
leftmost symbol of the string. Ʃ = {0,1}
26
Illustration
27
Solution
1/ 1, R □ / □, N
q0 q1 halt
0/0, R 1/ 1, R
0/ 0, R
28
Exercises(7)
7. Design a TM that recognizes the language {1n , n = 1, 3 , 5 , . . .}.
29
Illustration
30
Solution
Read first 1 Odd number of 1’s Even number of 1’s
1/ 1, R
1/ 1, R
q0 q1 q2
□ / □, N 1/ 1, R
halt
31
Applications of Turing Machines(1)
8. Design a TM to recognize the language L = {anbn : n ≥ 0 }. Initially,
the head is positioned on one of the non-blank symbols.
32
Illustration
33
High Level Algorithm
1. Find left end of input string.
2. If tape is not empty, erase leftmost ‘a’ and go to step 4.
3. If tape is empty, accept (halt with accept).
4. Find the right end of the input.
5. Erase rightmost b and go to step 1.
34
Solution
35
Applications of Turing Machines(2)
9. Design a TM to recognize the language L = {aibj : i < j }. Initially, the
head is positioned on one of the non-blank symbols.
36
Illustration
37
High Level Algorithm
1. Find left end of input string.
2. If leftmost symbol is a, erase leftmost ‘a’ and go to step 4.
3. If leftmost symbol is ‘b’, skip remaining b’s and then (halt with
accept).
4. Find the right end of the input.
5. Erase rightmost b and go to step 1.
38
Solution
39
Applications of Turing Machines(3)
10. Design a TM to perform unary subtraction. Store the result in n.
40
Illustration
41
High Level Algorithm
• Initial contents of tape : 1 1 1 1 1 … 1 1 1 1… 1
n m
• Read/write head is located on first 1 in n
• Output : 11…1
n m
• Algorithm :
• Repeatedly remove first 1 in n and last 1 in m , i.e., decrement n . m , till m
becomes zero. The result is stored n
42
Solution
43
Applications of Turing Machines(4)
11. Write algorithm for TM that recognizes the language
44
Illustration
45
Solution
46
Applications of Turing Machines(5)
12. Write algorithm for TM that recognizes the language
47
Illustration
48
Solution
49