Models of Computation - IUPUI Computer Science

Department of Computer and Information Science,
School of Science, IUPUI
CSCI 230
Models of Computation
- Turing Machines
Dale Roberts, Lecturer
Computer Science, IUPUI
E-mail: [email protected]
Dale Roberts
The Turing Machine (TM)
Turing Machine is a model of computing agent.
It is a pencil-and-paper type model that captures the essential
features of a computing agent.
A Turing machine consists of
1.
2.
3.
4.
a tape that extends infinitely in both directions
the tape is divided into cells, each cell can carry a symbol.
the symbols comes from a finite set of symbols called the alphabet
the alphabet consists of symbols : b (blank), 0, 1, and special
symbols X, Y, $
. . b b 0 1 1 b b .
1
5.
6.
the tape serve as memory
has a finite number of k states, 1, …k
Dale Roberts
Turing Machine actions depend on two inputs:
The current state of the machine
2. content of cell currently being read (input)
1.
A Turing machine can do only one operation at
a time. Each time an operation is done, three
actions may take place:
3.
4.
5.
write a symbol to cell
go into a new state
move one cell left or right
Dale Roberts
The Turing Machine (TM)
Example: Assume a Turing machine (TM) instruction:
if you are in state 1
and
you are reading symbol 0
then
write symbol 1 onto tape
go into state 2
move right
State Transition:
current state
1
0/1
R
input
R
W
Your movement
State: where you are now.
2
action (direction)
Written as: (1,0,1,R,2)
output
next state
output
. . b b 0 1 1 b b .
1
. . b b 1 1 1 b b .
12
input
Dale Roberts
The Turing Machine (TM)
Example: Design a Turing Machine which will invert the string of binary digits
if the input string is 10110 then the output string should be 01001
let us draw a state diagram
1/0
1
0/1
The TM instruction sets will be
(1,0,1,R,1)
(1,1,0,R,1)
Let the initial configuration be: ... b 1 0 1 1 0 b ...
... b 1 0 1 1 0 b ...
... b 0 0 1 1 0 b ...
... b 0 1 1 1 0 b ...
... b 0 1 0 1 0 b ...
... b 0 1 0 0 0 b ...
... b 0 1 0 0 1 b ...
Dale Roberts
Unary Representation for TM
Unary representation is used in order to do any arithmetic operations on a TM.
Unary representation looks as follows
0 1
1  11
2  111
3  1111
4  11111
Example: Design a Turing Machine to add 1 to any number
start in state 1
if the state is 1 and current input is 1, write 1 and move right and stay in state 1
if the current state is 1 and current input is b, write 1 and move to state 2 and move right
and HALT
TM instructions:
1/1
(1,1,1,R,1)
(1,b,1,R,2)
b/1
state 2 does not exist, so halt
1 R 2 Halt
Let the initial configuration be: ... b 1 1 1 b ...
s1 ... b 1 1 1 b ...
s1 ... b 1 1 1 b ...
s1 ... b 1 1 1 b ... 2 in unary representation
s1 ... b 1 1 1 b ...
s2 ... b 1 1 1 1 b ... 3 in unary representation
Dale Roberts
Example: Adding of two non-zero numbers (unary representation)
. . b 1 1 1 b 1 1 b . .
. . b b b 1 1 1 1 b . .
Initial Setup
Answer should be:
(1,1,b,R,2):
(2,1,b,R,3):
(3,1,1,R,3):
(3,b,1,R,4):
(4,1,1,R,4):
(4,b,b,R,5):
Erase leftmost 1 and move right
Erase second 1 and move right
pass over any 1’s until a blank is found
write 1 over the blank
pass over remaining 1’s
halt
S1... b 1 1 1 b 1 1 b...
1/1
1
1/b
R
2
1/b
R
3
S2... b b 1 1 b 1 1 b...
1/1
b/1
R
4
b/b
R
Halt
5
S3... b b b 1 b 1 1 b...
S3... b b b 1 b 1 1 b...
S4... b b b 1 1 1 1 b...
S4... b b b 1 1 1 1 b...
S4... b b b 1 1 1 1 b...
Dale Roberts
Example: Add two numbers, 2 + 3
initial setup would: a ‘b’ separate the two numbers.
... b 1 1 1 b 1 1 1 1 b ...
2
3
and the expected answer is
... b 1 1 1 1 1 1 b ...
5
Let the initial configuration be:
S1... b 1 1 1 b 1 1 1 1 b...
S1... b 1 1 1 b 1 1 1 1 b...
The TM instruction sets will be
(1,1,1,R,1)
(1,b,1,R,2)
(2,1,1,R,2)
(2,b,b,L,3)
(3,1,b,L,4)
(4,1,b,L,5)
1/1
R
1
b/1
R
1/1
R
2
S1... b 1 1 1 b 1 1 1 1 b...
S1... b 1 1 1 b 1 1 1 1 b...
S2... b 1 1 1 1 1 1 1 1 b...
S2... b 1 1 1 1 1 1 1 1 b...
S2... b 1 1 1 1 1 1 1 1 b...
S2... b 1 1 1 1 1 1 1 1 b...
S2... b 1 1 1 1 1 1 1 1 b...
S3... b 1 1 1 1 1 1 1 1 b...
b/b
L
3
1/b
L
4
1/b
L
5
Dale Roberts
S4... b 1 1 1 1 1 1 1 b b...
S5... b 1 1 1 1 1 1 b b b...
Example: Add two numbers, 2 + 3
try
(1,1,b,R,2)
(2,1,b,R,3)
(3,1,1,R,3)
(3,b,1,R,4)
if an initial configuration is
1/1
1
1/b
R
2
1/b
R
R
3
b/1
R
b b 1 1 1 b 1 1 1 1 b b ..
S1... b b 1 1 1 b 1 1 1 1 b b...
S2... b b b 1 1 b 1 1 1 1 b b...
S3... b b b b 1 b 1 1 1 1 b b...
S3... b b b b 1 b 1 1 1 1 b b...
S3... b b b b 1 1 1 1 1 1 b b...
S4
4
(halt)
Dale Roberts