### Examples of Turing machines computing(slides)

```Turing Machines
Slides adapted from Costas Busch - RPI
1
Doing math with Turing machines
Can a Turing machine do some mathematical
operations?
Fall 2006
Costas Busch - RPI
2
Making one pass through the input, check if it is
the right form, that is a+b+c+
everything)
For each a
Cross it out
Scan until you hit a b
For each b, mark it
Scan until you hit c
Cross out the c
If you do not hit any cs, reject
Restore the bs to unmarked state
If all a’s are crossed out, check if all c’s have also
been crossed out
If yes – accept
If no - reject
3
Configuration
Assume q is a state and u and v are strings
The configuration uqv means
Current tape content is uv
Current state is q
Current head location is the first symbol of v
u
Fall 2006
v
Costas Busch - RPI
4
Configuration of a Turing Machine
As a Turing Machine computes 3 things
happen
• The state can change
• The tape contents can change
• The R/W head location can change
A current snapshot of the machine =
configuration.
Fall 2006
Costas Busch - RPI
5
Yielding a configuration
Moving from one configuration to another is
called yielding a configuration
Fall 2006
Costas Busch - RPI
6
Writing transitions in a Turing machine
state diagram
Current char -> new char, direction
The new char is written first and then you
move left or right
Costas Busch - RPI
7
A Turing machine going into a loop

is being used for the blank character
b  b, L
a  a, R
q0
  , L
Costas Busch - RPI
q1
8
Time 0
  a b a  
q0
b  b, L
a  a, R
q0
  , L
Costas Busch - RPI
q1
9
Time 1
  a b a  
q0
b  b, L
a  a, R
q0
  , L
Costas Busch - RPI
q1
10
Time 2
  a b a  
q0
b  b, L
a  a, R
q0
Fall 2006
  , L
Costas Busch - RPI
q1
11
Time 2
  a b a  
q0
  a b a  
q0
Time 4
  a b a  
q0
Time 5
  a b a  
q0
Costas Busch - RPI
Infinite loop
Time 3
12
Because of the infinite loop:
•The accepting state cannot be reached
•The machine never halts
•The input string is rejected
Costas Busch - RPI
13
If a language L is accepted
by a Turing machine M
then we say that L is:
•Turing Recognizable
Other names used:
•Turing Acceptable
•Recursively Enumerable
Fall 2006
Costas Busch - RPI
14
If a language L is decided (no
looping, only rejection or acceptance)
by a Turing machine M
then we say that L is:
•Turing Decidable
Other names used:
•Recursive
Costas Busch - RPI
15
Computing Functions
with
Turing Machines
Not really covered in Sipser
Fall 2006
Costas Busch - RPI
16
A function
Domain:
has:
f (w)
Result Region:
D
f (w)
w D
Fall 2006
Costas Busch - RPI
S
f ( w)  S
17
A function may have many parameters:
Example:
f ( x, y )  x  y
Fall 2006
Costas Busch - RPI
18
Integer Domain
Decimal:
5
Binary:
101
Unary:
11111
We prefer unary representation:
easier to manipulate with Turing machines
Fall 2006
Costas Busch - RPI
19
Definition:
f
A function
is computable if
there is a Turing Machine M such that:
Initial configuration

w
Final configuration


qf
q0
initial state
For all
Fall 2006
f (w) 
accept state
w D Domain
Costas Busch - RPI
20
In other words:
f
A function
is computable if
there is a Turing Machine M such that:

q0 w  q f f ( w)
Initial
Halts
Configuration at
For all
Fall 2006
Final
Configuration
w D Domain
Costas Busch - RPI
21
Example
The function
f ( x, y )  x  y is computable
x, y
are integers
Turing Machine:
Fall 2006
Input string:
x0 y
unary
Output string:
xy 0
unary
Costas Busch - RPI
22
x
Start
 1 1

y
1 0 1  1 
q0
initial state
The 0 is the delimiter that
separates the two numbers
Fall 2006
Costas Busch - RPI
23
y
x
Start
 1 1
1 0 1  1 

q0 initial state
x y
Finish
 1 1

1 1 0 
q f final state
Fall 2006
Costas Busch - RPI
24
The 0 here helps when we use
the result for other operations
x y
Finish
 1 1

1 1 0 
q f final state
Fall 2006
Costas Busch - RPI
25
Turing machine for function
1 1, R
f ( x, y )  x  y
1 1, R
1 1, L



,
L
0

1
,
R
1
0
,
L
q
q0
q3
q1
2
  , R
Fall 2006
Costas Busch - RPI
q4
26
Execution Example:
x  11 (=2)
Time 0
y
x
 1 1 0 1 1 
y  11 (=2)
q0
Final Result
x y
 1 1 1 1 0 
Fall 2006
Costas Busch - RPI
q4
27
Time 0
 1 1 0 1 1 
q0
1 1, R
1 1, R
1 1, L



,
L
0

1
,
R
1
0
,
L
q
q0
q3
q1
2
  , R
Fall 2006
Costas Busch - RPI
q4
28
Time 1
 1 1 0 1 1 
q0
1 1, R
1 1, R
1 1, L



,
L
0

1
,
R
1
0
,
L
q
q0
q3
q1
2
  , R
Fall 2006
Costas Busch - RPI
q4
29
Time 2
 1 1 0 1 1 
q0
1 1, R
1 1, R
1 1, L



,
L
0

1
,
R
1
0
,
L
q
q0
q3
q1
2
  , R
Fall 2006
Costas Busch - RPI
q4
30
Time 3
 1 1 1 1 1 
q1
1 1, R
1 1, R
1 1, L



,
L
0

1
,
R
1
0
,
L
q
q0
q3
q1
2
  , R
Fall 2006
Costas Busch - RPI
q4
31
Time 4
 1 1 1 1 1 
q1
1 1, R
1 1, R
1 1, L



,
L
0

1
,
R
1
0
,
L
q
q0
q3
q1
2
  , R
Fall 2006
Costas Busch - RPI
q4
32
Time 5
 1 1 1 1 1 
q1
1 1, R
1 1, R
1 1, L



,
L
0

1
,
R
1
0
,
L
q
q0
q3
q1
2
  , R
Fall 2006
Costas Busch - RPI
q4
33
Time 6
 1 1 1 1 1 
q2
1 1, R
1 1, R
1 1, L



,
L
0

1
,
R
1
0
,
L
q
q0
q3
q1
2
  , R
Fall 2006
Costas Busch - RPI
q4
34
Time 7
 1 1 1 1 0 
q3
1 1, R
1 1, R
1 1, L



,
L
0

1
,
R
1
0
,
L
q
q0
q3
q1
2
  , R
Fall 2006
Costas Busch - RPI
q4
35
Time 8
 1 1 1 1 0 
q3
1 1, R
1 1, R
1 1, L



,
L
0

1
,
R
1
0
,
L
q
q0
q3
q1
2
  , R
Fall 2006
Costas Busch - RPI
q4
36
Time 9
 1 1 1 1 0 
q3
1 1, R
1 1, R
1 1, L



,
L
0

1
,
R
1
0
,
L
q
q0
q3
q1
2
  , R
Fall 2006
Costas Busch - RPI
q4
37
Time 10
 1 1 1 1 0 
q3
1 1, R
1 1, R
1 1, L



,
L
0

1
,
R
1
0
,
L
q
q0
q3
q1
2
  , R
Fall 2006
Costas Busch - RPI
q4
38
Time 11
 1 1 1 1 0 
q3
1 1, R
1 1, R
1 1, L



,
L
0

1
,
R
1
0
,
L
q
q0
q3
q1
2
  , R
Fall 2006
Costas Busch - RPI
q4
39
Time 12
 1 1 1 1 0 
q4
1 1, R
1 1, R
1 1, L



,
L
0

1
,
R
1
0
,
L
q
q0
q3
q1
2
HALT & accept
Fall 2006
Costas Busch - RPI
  , R
q4
40
Another Example
The function
f ( x)  2 x
x
is computable
is integer
Turing Machine:
Input string:
Output string:
Fall 2006
x
unary
xx
unary
Costas Busch - RPI
41
x
Start
 1 1

1 
q0 initial state
2x
Finish
 1 1

1 1 1 
q f accept state
Fall 2006
Costas Busch - RPI
42
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
Fall 2006
Costas Busch - RPI
43
Turing Machine for
1  \$, R
f ( x)  2 x
1 1, L
1 1, R
q0   , L q1 \$  1, R
  , R
q3
Fall 2006
q2
  1, L
Costas Busch - RPI
44
Start
Example
 1 1 
Finish
 1 1 1 1 
q0
q3
1  \$, R
1 1, L
1 1, R
q0   , L q1 \$  1, R
Fall 2006
  , R
q3
q2
  1, L
Costas Busch - RPI
45
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
Fall 2006
Costas Busch - RPI
46
How to add 1 to a binary number?
What is 11000 + 1 = ?
What is 10111 + 1 = ?
The pattern that emerges is …..
Fall 2006
Costas Busch - RPI
47
How to subtract 1 from a binary number?
Fall 2006
Costas Busch - RPI
48
Putting it all together
•
•
Assume we have some kind of separator character
between the two inputs to add
First input is x and the second input is y
// algorithm repeatedly subtracts one from x, adds one to y until x is zero
repeat until x == 0, then HALT {
// use subtractor , like a function call, start at leftmost \$
subtract 1 from x
// use adder , like a function call, start at middle \$
// simple TM to read and write the same character
go back to the first \$
}
Fall 2006
Costas Busch - RPI
49
Church-Turing Thesis
Lamba Calculus? – Alonzo Church
Turing Machines
The Church Turing thesis shows that
What is an algorithm?
Algorithm = something that a Turing machine
can do/something that can be done via lambda
calculus
Fall 2006
Costas Busch - RPI
50
Next time
Moving on to complexity and algorithms
Fall 2006
Costas Busch - RPI
51
```