Turing Machines - Stanford CS Theory

advertisement
Turing Machine
CS154
FINITE
STATE
q10
CONTROL
Turing Machines
AI
N
P
U
T
INFINITE TAPE
read
write
move
read
→ , R
0 → 0, R
write
move
→ , R
0 → 0, R
qaccept
0 → 0, R
→ , R
0 → 0, R
→ , R
qreject
Language = {0}
qaccept
0 → 0, R
→ , L
0 → 0, R
→ , L
This Turing machine recognizes the
language {0}
Deciding the language L = { w#w | w ∈ {0,1}* }
Turing Machines versus DFAs
TM can both write to and read from the tape
STATE
q1,FIND qGO LEFT
q1,#FIND # q#, FIND
q0, FIND
q0,FIND
The head can move left and right
The input doesn’t have to be read entirely,
and the computation can continue further
(even, forever) after all input has been read
Accept and Reject take immediate effect
0
1
0
X
1
X
#
1
0
#
0
X
1
1
1
1. If there’s no # on the tape, reject.
2. While there is a bit to the left of #,
Replace the first bit with X, and check if the first bit
to the right of the # is identical. (If not, reject.)
Replace that bit with an X too.
3. If there’s a bit to the right of #, then reject else accept
1
Definition: A Turing Machine is a 7-tuple
T = (Q, Σ, Γ, δ, q0, qaccept, qreject), where:
Turing Machine Configurations
Q is a finite set of states
q7
Σ is the input alphabet, where ∉ Σ
Γ is the tape alphabet, where ∈ Γ and Σ ⊆ Γ
δ : Q × Γ → Q × Γ × {L, R}
q0 ∈ Q is the start state
1
1
qreject ∈ Q is the reject state, and qreject ≠ qaccept
Let C1 and C2 be configurations of M
We say C1 yields C2 if, after running M in C1
for one step, M is then in configuration C2
Suppose δ(q1, b) = (q2, c, L)
Then aaq1bb yields aq2acb
Suppose δ(q1, a) = (q2, c, R)
Then cabq1a yields cabcq2
Let w ∈ Σ* and M be a Turing machine
M accepts w if there are configs C0, C1, ..., Ck, s.t.
• C 0 = q0 w
• Ci yields Ci+1 for i = 0, ..., k-1, and
• Ck contains the accepting state qaccept
A language is called recognizable
(recursively enumerable) if some TM
recognizes it
A language is called decidable (recursive) if
some TM decides it
recognizable
languages
decidable
languages
1
0
0
0
1
1
0
corresponds to the configuration:
qaccept ∈ Q is the accept state
Defining Acceptance and Rejection
0
11010q700110
A TM M recognizes a language L iff M accepts
all and only those strings in L
A language L is called recognizable or
recursively enumerable (r.e.)
iff some TM recognizes L
A TM M decides a language L iff M accepts all
strings in L and rejects all strings not in L
A language L is called decidable or recursive
iff some TM decides L
n
{ 02 | n ≥ 0 }
PSEUDOCODE:
1. Sweep from left to right, cross out every other 0
2. If in stage 1, the tape had only one 0, accept
3. If in stage 1, the tape had an odd number of 0’s,
reject
4. Move the head back to the first input symbol.
5. Go to stage 1.
Why does this work?
Idea: Every time we return to stage 1, the
number of 0’s on the tape is halved.
2
x → x, L
0 → 0, L
n
{ 02 | n ≥ 0 }
C = {aibjck | k = i*j, and i, j, k ≥ 1}
q2
→ , R
PSEUDOCODE:
→ , L
x → x, R
x → x, R
q0
q1
0 → , R
x → x, R
→ , R
q3
0 → x, R
→ , R
qreject
0 → 0, R
0 → x, R
qaccept
q4
x → x, R
→ , R
C = {aibjck | k = i*j, and i, j, k ≥ 1}
aabbbcccccc
xabbbcccccc
xayyyzzzccc
xabbbzzzccc
xxyyyzzzzzz
1. If the input doesn’t match a*b*c*, reject.
2. Move the head back to the leftmost symbol.
3. Cross off an a, scan to the right until b.
Sweep between b’s and c’s, crossing off one of
each until all b’s are crossed off.
If all c’s get crossed off while doing this, reject.
4. Uncross all the b’s.
If there’s another a left, then repeat stage 3.
If all a’s are crossed out,
Check if all c’s are crossed off.
If yes, then accept, else reject.
Multitape Turing Machines
FINITE
STATE
CONTROL
k
δ : Q × Γk → Q × Γk × {L,R}k
Theorem: Every Multitape Turing Machine can be
transformed into a single tape Turing Machine
Theorem: Every Multitape Turing Machine can be
transformed into a single tape Turing Machine
1
1
0
0
#
0
0
FINITE
STATE
CONTROL
FINITE
STATE
CONTROL
0
FINITE
STATE
CONTROL
.
1
0
0
#
.
#
.
#
FINITE
STATE
CONTROL
1
.
0
.
#
.
#
3
Theorem: Every Multitape Turing Machine can be
transformed into a single tape Turing Machine
Theorem: Every Multitape Turing Machine can be
transformed into a single tape Turing Machine
1
1
0
0
FINITE
STATE
CONTROL
FINITE
STATE
CONTROL
0
0
FINITE
STATE
CONTROL
1
0
.
0
#
.
#
.
#
Nondeterministic TMs
Theorem: Every nondeterministic Turing
machine N can be transformed into a single
tape Turing Machine M that recognizes the
same language.
Proof Idea (more details in Sipser):
M(w):
For all strings C ∈ {Q ∪ Γ ∪ #}* in lex. order,
Check if C = C0# < #Ck where
C0, < ,Ck is some accepting computation
history for N on w. If so, accept.
FINITE
STATE
CONTROL
1
0
.
0
.
#
#
.
#
We can encode a TM as a bit string.
n states
start
state
reject
state
0n10m10k10s10t10r10u1
m tape symbols
(first k are input
symbols)
accept
state
blank
symbol
( (p, a), (q, b, L) ) = 0p10a10q10b10
( (p, a), (q, b, R) ) = 0p10a10q10b11
Similarly, we can encode DFAs and NFAs
as bit strings.
So we can define the following languages:
ADFA = { (B, w) | B is a DFA that accepts string w }
ADFA = { (D, w) | D is a DFA that accepts string w }
Theorem: ADFA is decidable
Proof Idea: Directly simulate D on w!
ANFA = { (N, w) | N is an NFA that accepts string w }
Theorem: ANFA is decidable. (Why?)
ANFA = { (B, w) | B is an NFA that accepts string w }
ATM = { (C, w) | C is a TM that accepts string w }
(Can define (x, y) := 0|x|1xy)
ATM = { (M, w) | M is a TM that accepts string w }
ATM is recognizable but not decidable
4
Universality of Turing Machines
The Church-Turing Thesis
Theorem: There is a universal Turing machine U
that can take as input
- the code of an arbitrary TM M
- an input string w,
such that U(M, w) accepts iff M(w) accepts.
Everyone’s
Intuitive Notion = Turing Machines
of Algorithms
This is a fundamental property:
Turing machines can run their own code!
This thesis is tested every time you write
a program that does something!
Note that DFAs/NFAs do not have this property.
That is, ADFA is not regular.
A language is called recognizable or
recursively enumerable, (or r.e.)
if some TM recognizes it
A TM decides a language if it accepts all
strings in the language and rejects all strings
not in the language
A language is called decidable (or recursive)
if some TM decides it
Recall: Given L ⊆ Σ *, define ¬L := Σ * - L
Theorem: L is decidable
iff both L and ¬L are recognizable
Given:
a TM M1 that recognizes A and
a TM M2 that recognizes ¬A,
we can build a new machine M that decides A
How? Any ideas?
M1 always accepts x, when x is in A
M2 always accepts x, when x is not in A
w ∈ Σ*
w ∈ Σ*
A TM recognizes a language if it accepts all
and only those strings in the language
TM
TM
w∈L?
yes
no
accept
reject
L is decidable
(recursive)
w∈L?
yes
no
accept reject or no output
L is recognizable
(recursively enumerable)
Theorem: L is decidable if both L and ¬L
are recursively enumerable
There are languages over {0,1}
that are not decidable
Assuming the Church-Turing Thesis,
this means there are problems that
NO computing device can solve
We can prove this using a counting argument.
We will show there is no onto function from
the set of all Turing Machines to the set of all
languages over {0,1}. (But it works for any Σ)
That is, every mapping from Turing machines to
languages must “miss” some language.
5
Download