Languages and Finite Automata

Download Report

Transcript Languages and Finite Automata

Turing Machines
1
The Language Hierarchy
n n n ?
a b c
ww ?
Context-Free Languages
n n
R
a b
ww
Regular Languages
a*
a *b *
2
Languages accepted by
Turing Machines
ww
n n n
a b c
Context-Free Languages
n n
R
a b
ww
Regular Languages
a*
a *b *
3
A Turing Machine
Tape
......
......
Read-Write head
Control Unit
4
The Tape
No boundaries -- infinite length
......
......
Read-Write head
The head moves Left or Right
5
......
......
Read-Write head
The head at each time step:
1. Reads a symbol
2. Writes a symbol
3. Moves Left or Right
6
Example:
Time 0
......
a b a c
Time 1
......
1. Reads
2. Writes
a b k c
......
......
a
k
3. Moves Left
7
Time 1
......
a b k c
Time 2
......
1. Reads
2. Writes
a f
k c
......
......
b
f
3. Moves Right
8
The Input String
Input string
......
  a b a c
Blank symbol
  
......
head
Head starts at the leftmost position
of the input string
9
States & Transitions
Read
q1
Write
a  b, L
Move Left
q2
Move Right
q1
a  b, R
q2
10
Example:
Time 1
......
  a b a c
  
......
q1
current state
q1
a  b, R
q2
11
......
Time 1
  a b a c
  
......
  
......
q1
......
Time 2
  a b b c
q2
q1
a  b, 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, R
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 epsilon 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
17
Example:
......
  a b a c
  
......
q1
a  b, R
q2
q1
b  d, L
q3
No possible transition
HALT!!!
18
Final States
q1
q2
Allowed
q1
q2
Not Allowed
• Final states have no outgoing transitions
• In a final state the machine halts
19
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
20
Turing Machine Example
A Turing machine that accepts language a*
a  a, R
q0
  , L
q1
21
Time 0
  a a a  
q0
a  a, R
q0
  , L
q1
22
Time 1
  a a a  
q0
a  a, R
q0
  , L
q1
23
Time 2
  a a a  
q0
a  a, R
q0
  , L
q1
24
Time 3
  a a a  
q0
a  a, R
q0
  , L
q1
25
Time 4
  a a a  
q1
a  a, R
q0
Halt & Accept
  , L
q1
26
Rejection Example
Time 0
  a b a  
q0
a  a, R
q0
  , L
q1
27
Time 1
  a b a  
q0
No possible Transition
Halt & Reject
a  a, R
q0
  , L
q1
28
Infinite Loop Example
Another Turing machine for language a*
and is this one correct???
b  b, L
a  a, R
q0
  , L
q1
29
Time 0
  a b a  
q0
b  b, L
a  a, R
q0
  , L
q1
30
Time 1
  a b a  
q0
b  b, L
a  a, R
q0
  , L
q1
31
Time 2
  a b a  
q0
b  b, L
a  a, R
q0
  , L
q1
32
Time 2
  a b a  
q0
Time 3
  a b a  
q0
Time 4
  a b a  
q0
Time 5
  a b a  
q0
... Infinite Loop
33
Because of the infinite loop:
•The final state cannot be reached
•The machine never halts
•The input is not accepted
34
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
x  x, R
q2
35
 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
x  x, R
q2
36
 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
x  x, R
q2
37
 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
x  x, R
q2
38
 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
x  x, R
q2
39
 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
x  x, R
q2
40
 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
x  x, R
q2
41
 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
x  x, R
q2
42
 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
x  x, R
q2
43
 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
x  x, R
q2
44
 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
x  x, R
q2
45
 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
x  x, R
q2
46
 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
x  x, R
q2
47
 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
x  x, R
q2
48
 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
x  x, R
q2
49
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 }
50
Formal Definitions
for
Turing Machines
51
Transition Function
q1
a  b, R
q2
 (q1, a)  (q2 , b, R)
52
Transition Function
q1
c  d, L
q2
 (q1, c)  (q2 , d , L)
53
Turing Machine:
States
Input
alphabet
Tape
alphabet
M  (Q, , ,  , q0 , , F )
Transition
function
Initial
state
Final
states
blank
54
Configuration
  c a b a  
q1
Instantaneous description:
ca q1 ba
55
Time 4
Time 5
 x a y b  
q2
A Move:
 x a y b  
q0
q2 xayb  x q0 ayb
56
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
57
q2 xayb  x q0 ayb  xx q1 yb  xxy q1 b
Equivalent notation:
*
q2 xayb  xxy q1 b
58
Initial configuration:
q0 w
Input string
w
 a a b b  
q0
59
The Accepted Language
For any Turing Machine
L( M )  {w :
M
*
q0 w  x1 q f x2 }
Initial state
Final state
60
Standard Turing Machine
The machine we described is the standard:
• Deterministic
• Infinite tape in both directions
•Tape is the input/output file
61
Implementation-level descriptions
{w|w contains an equal number of 0s and 1s}
62
Implementation-level descriptions
{w|w contains an equal number of 0s and 1s}
On input string w:
63
Implementation-level descriptions
{w|w contains an equal number of 0s and 1s}
On input string w:
1) Scan the tape & mark the 1st 0 which is unmarked. If none is
found, go to #4. Otherwise, move the head back to the front
of the tape.
64
Implementation-level descriptions
{w|w contains an equal number of 0s and 1s}
On input string w:
1) Scan the tape & mark the 1st 0 which is unmarked. If none is
found, go to #4. Otherwise, move the head back to the front
of the tape.
2) Scan the tape & mark the 1st 1 which is unmarked. If none is
found, reject.
65
Implementation-level descriptions
{w|w contains an equal number of 0s and 1s}
On input string w:
1) Scan the tape & mark the 1st 0 which is unmarked. If none is
found, go to #4. Otherwise, move the head back to the front
of the tape.
2) Scan the tape & mark the 1st 1 which is unmarked. If none is
found, reject.
3) Move the head back to the front of the tape & go to #1.
66
Implementation-level descriptions
{w|w contains an equal number of 0s and 1s}
On input string w:
1) Scan the tape & mark the 1st 0 which is unmarked. If none is
found, go to #4. Otherwise, move the head back to the front
of the tape.
2) Scan the tape & mark the 1st 1 which is unmarked. If none is
found, reject.
3) Move the head back to the front of the tape & go to #1.
4) Move the head back to the front of the tape. Scan the tape to
see if any unmarked 1s remain. If none are found, accept;
otherwise, reject.
67
Try this one:
{w|w contains twice as many 0s as 1s}
On input string w:
1) Scan the tape & ...
(Hint, you can “mark” and “double mark”.)
68
Try this one:
{w|w = anbn, n  0}
On input string w:
1) Scan the tape & ...
69