Languages and Finite Automata
Download
Report
Transcript Languages and Finite Automata
Introduction to
Turing Machines
0 01 1 10 1 1 1 1 1 1 0 1 1 0 1 1 0 0 0
1
Alan Turing -- 1912 – 1954
• Mathematician, a computer scientis
• Influantial
• Development of computer sciences
• provided an influential formalisation
• of the concept of the algorithm
• and computation with the
• Turing Machine
• Turing Test contribute to the debate of AI
• Can machines think?
•
•
•
•
One of the 100 more imp. People in 20th century
21st on BBC nation wide poll of the 100 Greatest britons
http://www.turing.org.uk/turing/
http://en.wikipedia.org/wiki/Alan_Turing/
2
The Turing Machine
10 01 1 10 1 1 1 1 1 1 0 1 1 0 1 1 0 0 0
A TM consists of an infinite length tape, on which
input is provided as a finite sequence of symbols.
A head reads the input tape. The TM starts at start
state s0. On reading an input symbol it optionally
replaces it with another symbol, changes its
internal state and moves one cell to the right or left.
3
The Turing Machine
A TM is defined as:
TM = <S, T, s0, d, H> where,
S
is a set of TM states
T
is a set of tape symbols
s0
is the start state
HS
is a set of halting states
d : S x T S x T x {L,R}
is the transition function
4
Tape
......
A Turing Machine
......
Read-Write head
Control Unit
5
The Tape
No boundaries -- infinite length
......
......
Read-Write head
The head moves Left or Right
6
......
......
Read-Write head
The head at each time step:
1. Reads a symbol
2. Writes a symbol
3. Moves Left or Right
7
Example:
Time 0
......
Time 1
......
1. Reads
a b a c
......
a b d c
......
a
2. Writes d
3. Moves Left
8
......
......
Time 1
a b s c
Time 2
a f s
c
......
......
1. Reads b
2. Writes f
3. Moves Right
9
The Input String
Input string
......
# # a
Blank symbol
b a c # # #
......
head
•Head starts at the leftmost position of the input string.
•The input string is never empty
10
States & Transitions
Read
q1
Write
a b, L
Move Left
q2
Move Right
q1
a b, R
q2
11
Example:
......
a
Time 1
b a c
......
q1 current state
......
a
Time 2
b d c
......
q2
q1
a d, R
q2
12
Example:
......
Time 1
a b a c
......
......
q1
......
Time 2
a b b c
q2
q1
a b, L
q2
13
Example:
......
Time 1
#
#
a b a c
#
#
......
#
......
q1
......
Time 2
#
#
a b b c
g
q2
q1
# g, L
q2
14
Determinism
Turing Machines are deterministic
Not Allowed
Allowed
a b, R
q2
a b, R
q2
a d, L
q3
q1
q1
b d, L
q3
No lambda transitions allowed
15
Partial Transition Function
Example:
......
#
# a
b
a c
# #
#
......
q1
a b, R
q2
q1
b d, L
q3
Allowed:
No transition
for input symbol
c
16
Halting
The machine halts if there are
no possible transitions to follow
......
#
# a
b
c
c
# #
#
......
q1
a b, R
q2
b d, L
q3
q1
No possible transition
HALT!!!
17
Final States
q1
q2
Allowed
q1
q2
Not Allowed
• Final states have no outgoing transitions
• In a final state the machine halts
18
Acceptance
Accept Input
If machine halts
in a final state
Reject Input
If machine halts
in a non-final state
or
If machine enters
an infinite loop
19
Turing Machine Example
A Turing machine that accepts the language:
aa *
a a, R
q0
, L
q1
20
Time 0
a a a
q0
a a, R
q0
, L
q1
21
Time 1
a a a
q0
a a, R
q0
, L
q1
22
Time 2
a a a
q0
a a, R
q0
, L
q1
23
Time 3
a a a
q0
a a, R
q0
, L
q1
24
Time 4
a a a
q1
a a, R
q0
Halt & Accept
, L
q1
25
Rejection Example
Time 0
a b a
q0
a a, R
q0
, L
q1
26
Time 1
a b a
q0
No possible Transition
Halt & Reject
a a, R
q0
, L
q1
27
Infinite Loop Example
A Turing machine
for language aa * b(a b) *
b b, L
a a, R
q0
, L
q1
28
Time 0
a b a
q0
b b, L
a a, R
q0
, L
q1
29
Time 1
a b a
q0
b b, L
a a, R
q0
, L
q1
30
Time 2
a b a
q0
b b, L
a a, R
q0
, L
q1
31
Time 2
a b a
q0
a b a
q0
Time 4
a b a
q0
Time 5
a b a
q0
Infinite loop
Time 3
32
Because of the infinite loop:
•The final state cannot be reached
•The machine never halts
•The input is not accepted
33
Another Turing Machine Example
Turing machine for the language
y y, R
q3
q4
, L
y y, R
q0
y y, R
a a, R
a x, R
q1
n n
{a b }
y y, L
a a, L
b y, L
q2
x x, R
34
a a b b
Time 0
q0
y y, R
q3
q4
, L
y y, R
q0
y y, R
a a, R
a x, R
q1
y y, L
a a, L
b y, L
q2
x x, R
35
x a b b
Time 1
q1
y y, R
q3
q4
, L
y y, R
q0
y y, R
a a, R
a x, R
q1
y y, L
a a, L
b y, L
q2
x x, R
36
x a b b
Time 2
q1
y y, R
q3
q4
, L
y y, R
q0
y y, R
a a, R
a x, R
q1
y y, L
a a, L
b y, L
q2
x x, R
37
x a y b
Time 3
q2
y y, R
q3
q4
, L
y y, R
q0
y y, R
a a, R
a x, R
q1
y y, L
a a, L
b y, L
q2
x x, R
38
x a y b
Time 4
q2
y y, R
q3
q4
, L
y y, R
q0
y y, R
a a, R
a x, R
q1
y y, L
a a, L
b y, L
q2
x x, R
39
x a y b
Time 5
q0
y y, R
q3
q4
, L
y y, R
q0
y y, R
a a, R
a x, R
q1
y y, L
a a, L
b y, L
q2
x x, R
40
x x y b
Time 6
q1
y y, R
q3
q4
, L
y y, R
q0
y y, R
a a, R
a x, R
q1
y y, L
a a, L
b y, L
q2
x x, R
41
x x y b
Time 7
q1
y y, R
q3
q4
, L
y y, R
q0
y y, R
a a, R
a x, R
q1
y y, L
a a, L
b y, L
q2
x x, R
42
x x y y
Time 8
q2
y y, R
q3
q4
, L
y y, R
q0
y y, R
a a, R
a x, R
q1
y y, L
a a, L
b y, L
q2
x x, R
43
x x y y
Time 9
q2
y y, R
q3
q4
, L
y y, R
q0
y y, R
a a, R
a x, R
q1
y y, L
a a, L
b y, L
q2
x x, R
44
x x y y
Time 10
q0
y y, R
q3
q4
, L
y y, R
q0
y y, R
a a, R
a x, R
q1
y y, L
a a, L
b y, L
q2
x x, R
45
x x y y
Time 11
q3
y y, R
q3
q4
, L
y y, R
q0
y y, R
a a, R
a x, R
q1
y y, L
a a, L
b y, L
q2
x x, R
46
x x y y
Time 12
q3
y y, R
q3
q4
, L
y y, R
q0
y y, R
a a, R
a x, R
q1
y y, L
a a, L
b y, L
q2
x x, R
47
x x y y
Time 13
q4
Halt & Accept
y y, R
q3
q4
, L
y y, R
q0
y y, R
a a, R
a x, R
q1
y y, L
a a, L
b y, L
q2
x x, R
48
Observation:
If we modify the
machine for the language
we can easily construct
a machine for the language
n n
{a b }
n n n
{a b c }
49
Formal Definitions
for
Turing Machines
50
Transition Function
q1
a b, R
q2
d (q1, a) (q2 , b, R)
51
Transition Function
q1
c d, L
q2
d (q1, c) (q2 , d , L)
52
Turing Machine:
States
Input
alphabet
Tape
alphabet
M (Q, , , d , q0 , , F )
Transition
function
Initial
state
Final
states
blank
53
Configuration
c a b a
q1
Instantaneous description:
ca q1 ba
54
Time 4
Time 5
x a y b
q2
A Move:
x a y b
q0
q2 xayb x q0 ayb
55
Time 4
x a y b
q2
Time 5
x a y b
q0
Time 6
x x y b
q1
Time 7
x x y b
q1
q2 xayb x q0 ayb xx q1 yb xxy q1 b
56
q2 xayb x q0 ayb xx q1 yb xxy q1 b
Equivalent notation:
q2 xayb xxy q1 b
57
Initial configuration:
q0 w
Input string
w
a a b b
q0
58
The Accepted Language
For any Turing Machine
L( M ) {w :
M
q0 w x1 q f x2 }
Initial state
Final state
59
Standard Turing Machine
The machine we described is the standard:
• Deterministic
• Infinite tape in both directions
•Tape is the input/output file
60
Computing Functions
with
Turing Machines
61
A function
Domain:
f (w)
Result Region:
D
f (w)
w D
has:
S
f ( w) S
62
A function may have many parameters:
Example:
Addition function
f ( x, y ) x y
63
Integer Domain
Decimal:
5
Binary:
101
Unary:
11111
We prefer unary representation:
easier to manipulate with Turing machines
64
Definition:
f
A function
is computable if
there is a Turing Machine M such that:
Initial configuration
w
q0 initial state
For all
Final configuration
f (w)
q f final state
w D Domain
65
In other words:
f
A function
is computable if
there is a Turing Machine M such that:
q0 w q f f ( w)
Initial
Configuration
For all
Final
Configuration
w D Domain
66
Example
The function
f ( x, y ) x y is computable
x, y
are integers
Turing Machine:
Input string:
x0 y
unary
Output string:
xy 0
unary
67
x
Start
1 1
y
1 0 1 1
q0
initial state
The 0 is the delimiter that
separates the two numbers
68
y
x
Start
1 1
1 0 1 1
q0 initial state
x y
Finish
1 1
1 1 0
q f final state
69
The 0 helps when we use
the result for other operations
x y
Finish
1 1
1 1 0
q f final state
70
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
q4
71
Execution Example:
x 11 (2)
y 11 (2)
Time 0
y
x
1 1 0 1 1
q0
Final Result
x y
1 1 1 1 0
q4
72
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
q4
73
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
q4
74
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
q4
75
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
q4
76
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
q4
77
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
q4
78
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
q4
79
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
q4
80
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
q4
81
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
q4
82
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
q4
83
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
q4
84
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
, R
q4
85
Another Example
The function
f ( x) 2 x
x
is computable
is integer
Turing Machine:
Input string:
Output string:
x
unary
xx
unary
86
x
Start
1 1
1
q0 initial state
2x
Finish
1 1
1 1 1
q f final state
87
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
88
Turing Machine for
1 $, R
f ( x) 2 x
1 1, L
1 1, R
q0 , L q1 $ 1, R
, R
q3
q2
1, L
89
Start
Example
1 1
Finish
1 1 1 1
q0
q3
1 $, R
1 1, L
1 1, R
q0 , L q1 $ 1, R
, R
q3
q2
1, L
90
Simple TM Examples
Turing Machine U+1:
Given a string of 1s on a tape (followed by
an infinite number of 0s), add one more 1 at the
end of the string.
#111100000000…….
#1111100000000……….
91
Simple TM Examples
TM: U+1
d(s0, 1) |-- (s0, 1, R)
d(s0, 0) |-- (h, 1, STOP)
#s0111100000…..
#1s011100000…..
#11s01100000…..
#111s0100000…..
#1111s000000…..
#11111h0000….. STOP
92
Another Example
The function
is computable
f ( x, y )
1
if
x y
0
if
x y
93
Turing Machine for
f ( x, y )
Input:
Output:
1
if
x y
0
if
x y
x0 y
1
or
0
94
Turing Machine Pseudocode:
• Repeat
Match a 1 from
Until all of
x with a 1 from y
x or y is matched
• If a 1 from x is not matched
erase tape, write 1
else
erase tape, write 0
( x y)
( x y)
95
Combining Turing Machines
96
Block Diagram
input
Turing
Machine
output
97
Example:
x y if x y
f ( x, y)
0
x, y
x, y
Comparer
if x y
Adder
x y
Eraser
0
x y
x y
98
Turing’s Thesis
Any mathematical problem solving that
can be described by a mechanical procedure
(algorithm) can be modeled by a
Turing machine.
All computers today perform only mechanical
problem solving. They are no more expressive
than a Turing machine.
99
Turing’s Thesis
Turing’s thesis is not a “theorem” there is no
“proof” for the thesis.
The theorem may be refuted by showing at least
one task that is performed by a digital computer
which cannot be performed by a Turing machine.
Many contentions have been made to this end.
However, till date there have not been any
conclusive evidence to refute Turing’s thesis.
100
Conclusions
TMs are at a level that is much below the assembly
language of any typical microprocessor.
So in the practical world, TMs are more useful in
what they cannot do rather than in what they
can.
101
Lab2
Write some simpleTuring machine programs
QUESTIONS?
102