turing machines part ii · 2016-08-03 · computer vs. turing machine a turing machine has an...
TRANSCRIPT
Turing MachinesPart II
COMP2600 — Formal Methods for Software Engineering
Katya Lebedeva
Australian National University
Semester 2, 2016
Slides created by Katya Lebedeva
COMP 2600 — Turing Machines 1
Why computer programs are not well-suited to develop a theory of com-putation?
• complex language constructs
• finite memory
• “state” of the computation is complicated to represent
• would need to show that the results for a specific programming languageare in fact general
We resort to an abstract computing device, the Turing Machine (TM). It
• represents a simple and universal programming language
• has an unbounded memory
• has states of computation that are easy to describe
• can simulate any known computing device
COMP 2600 — Turing Machines 2
Definition
A Turing Machine has the form (Q,q0,F, Γ,Σ,Λ, δ), where
• Q is the finite set of states
q0 ∈ Q is the initial state
F ⊆ Q is the set of final/accepting states
• Γ is the tape alphabet (the finite set of tape symbols)
Σ⊆ Γ is the input alphabet (the finite set of input symbols)
Λ ∈ Γ/Σ is the blank symbol
• δ is a (partial) transition function
δ : Q×Γ → Q×Γ×{L,R,S}
COMP 2600 — Turing Machines 3
Transitions
δ(q,x) = (p,y,d) means that
if M is in state q and the tape head is over symbol x, then
• changes state to p
• replaces x with y on the tape
• moves the head in the direction given by d
Note: the TM is deterministic, i.e. for each (q,x) we have at most one move.
δ(q,x) can be undefined for some q and x.
COMP 2600 — Turing Machines 4
Initialisation
• some input (a finite string w over Σ) is written on the tape
• every other tape cell is a blank symbol Λ
• the tape head is positioned on the left-most symbol of w
• the state is the start state q0
Acceptance
w ∈ Σ∗ is accepted by M, if M, when started with w on the tape eventually
enters a final state.
COMP 2600 — Turing Machines 5
Halting
A state q is halting for a symbol x ∈ Γ if δ(q,x) is not defined.
A state q is halting if it is halting for all symbols in Γ.
We can assume without loss of generality that all final states are halting.
Rejection
M rejects a string w in either of the following cases
• it halts in a non-final state
• it executes transitions forever, without ever reaching a final state
(in this case we say “M loops on w”)
COMP 2600 — Turing Machines 6
Automata vs. Turing Machines
Finite State Automata and Pushdown Automata scan over w (from left to right)
and accept/reject when they reach the end of w.
Turing Machines can move back and forth over w and accept/reject when they
halt or reject when they loop forever.
COMP 2600 — Turing Machines 7
Computer vs. Turing Machine
• A Turing Machine has an infinite tape.
• Physical computing devices have finite memory, so they can only exist in
a finite number of states. They are Finite State Automata. The number of
states can be very, very large: for example 24294967296
Physical computers can only approximate Turing Machines :-(
If a Turing Machine has a finite tape we can simulate it with an FSA:
Just take the finite state control and replicate it for every possible string that
could exist on the tape.
Combinatorial explosion! For a tape containing 16 binary symbols, we need
at least 216 states in our FSA!
COMP 2600 — Turing Machines 8
Random Access Memory saves the day
• An FSA has no auxiliary store/memory...
... this makes them less expressive.
• A PDA has an auxiliary store...
... but we can only directly access the symbol on the top of the stack.
• A Turing Machine is infinite...
... but we have to access the tape sequentially.
• My laptop is finite...
... but accessing a store location requires only a small, constant time.
... that’s usually enough.
COMP 2600 — Turing Machines 9
Using States to Remember a Tape Symbol
S T
U0
U1
Λ
Λ R,
0
Λ R,
1
Λ R,
Λ
0 L,
Λ
1 L,
0
0 R,
1
1 R,
0
0 R,
1
1 R,
1
1 L,
0
0 L,
Given a string of 0 or 1 surrounded
by blanks, this machine repeatedly
forever erases the leftmost bit, and
writes it on the right hand end. (Not
so useful, but illustrates the point)
We use the choice of states U0
or U1 to remember which symbol
has been erased and is to be written
Note: We begin in state S, the head points at the first symbol of the givenword. The first transition will move the head to the left, replacing the firstsymbol 1 with 1 or 0 with 0.
COMP 2600 — Turing Machines 10
Example
Define a TM that accepts exactly all strings in
L = {w#∗wt | w ∈ {0,1}+, t ∈ {0,1,#}∗}
Initially
. . . Λ Λ . . . w . . . # . . . # . . . w . . . . . . t . . . Λ Λ . . .
Idea:
• remember leftmost symbol and erase it
• move to leftmost symbol after #’s
• if the two don’t match, reject
otherwise replace the symbol by #, move left and start again
COMP 2600 — Turing Machines 11
. . . Λ Λ . . . w . . . # . . . # . . . w . . . . . . t . . . Λ Λ . . .
M = (Q,q0,F,Γ,Σ,Λ,δ)
Q = {q0,q1,q2,q3,q4,q5,q6,q7}
F = {q7}
Γ = {0,1,#,Λ}
Σ = {0,1,#}
COMP 2600 — Turing Machines 12
. . . Λ Λ . . . w . . . # . . . # . . . w . . . . . . t . . . Λ Λ . . .
• erase 0 and start looking for matching 0
δ(q0,0) = (q1,Λ,R)
• erase 1 and start looking for matching 1
δ(q0,1) = (q2,Λ,R)
COMP 2600 — Turing Machines 13
. . . Λ Λ . . . w . . . # . . . # . . . w . . . . . . t . . . Λ Λ . . .
• skip over 0’s and 1’s until first # is found (remembering 0)
δ(q1,0) = (q1,0,R)
δ(q1,1) = (q1,1,R)
δ(q1,#) = (q3,#,R)
• skip over 0’s and 1’s until first # is found (remembering 1)
δ(q2,0) = (q2,0,R)
δ(q2,1) = (q2,1,R)
δ(q2,#) = (q4,#,R)
COMP 2600 — Turing Machines 14
. . . Λ Λ . . . w . . . # . . . # . . . w . . . . . . t . . . Λ Λ . . .
• skip over #’s, look for 0 and replace it with #
δ(q3,#) = (q3,#,R)
δ(q3,0) = (q5,#,L)
Note: if right after #’s a 1 is found, M rejects
• skip over #’s, look for 1 and replace it with #
δ(q4,#) = (q4,#,R)
δ(q4,0) = (q5,#,L)
Note: if right after #’s a 0 is found, M rejects
COMP 2600 — Turing Machines 15
. . . Λ Λ . . . w . . . # . . . # . . . w . . . . . . t . . . Λ Λ . . .
• Move left skipping #’s:
δ(q5,#) = (q5,#,L)
If to the left of #’s a 0 or 1 is found, move to q6 to skip them too:
δ(q5,0) = (q6,0,L)
δ(q5,1) = (q6,1,L)
If to the left of #’s a Γ is found, accept:
δ(q5,Λ) = (q7,Λ,R)
COMP 2600 — Turing Machines 16
. . . Λ Λ . . . w . . . # . . . # . . . w . . . . . . t . . . Λ Λ . . .
• Move left skipping over 0’s and 1’s and restart
δ(q6,0) = (q6,0,L)
δ(q6,1) = (q6,1,L)
δ(q6,Λ) = (q0,Λ,R)
COMP 2600 — Turing Machines 17
COMP 2600 — Turing Machines 18
q0start q1
q2
q3
q4
q5 q7
q6
0Λ,R
1Λ,R
00,R
11,R
00,R
11,R
##,R
##,R
##,R
##,R
0#,L
1#,L
##,L
Λ
Λ,R
00,L
11,L
00,L
11,L
Λ
Λ,R
COMP 2600 — Turing Machines 19
Instanteneous Descriptions
A configuration of a TM is represented as α1qα2,where α1,α2 ∈ Γ∗ and q ∈ Q
Meaning
• non-blanc portion of the tape contains α1α2
• the machine is in state q
• the head is positioned on left-most symbol of α2
. . . Λ Λ α1 α2 Λ Λ . . .
ID1 ` ID2 means that there is a transition of M from ID1 to ID2
ID1 `∗ ID2 means that there is a sequence of transitions of M
that leads from ID1 to ID2
COMP 2600 — Turing Machines 20
Transitions for 01##01:
q001##01 `
q11##01 `
1q1##01 `
1#q3#01 `
1##q301 `
1#q5##1 `
1q5###1 `
q51###1 `
q6Λ1###1 `
continued on the next slide!
COMP 2600 — Turing Machines 21
q01###1 `
q2###1 `
#q4##1 `
##q4#1 `
###q41 `
##q5## `
#q5### `
q5#### `
q5Λ#### `
q7#### `
COMP 2600 — Turing Machines 22
Programming Techniques for TMs
Some notational conventions make it easier to write TM programs.
Idea: Use structured states and tape symbols.
Storage in the state
Idea: State names are tuples of the form [q,D1, . . . ,Dk]
each Di acts as a stored symbol
q is the control portion of the state
COMP 2600 — Turing Machines 23
Example
M = (Q,q0,F,Γ,Σ,Λ,δ)
E = 01∗+10∗
L(E) = {0,01,011, . . . ,1,10,100, . . .}
Q= {[qi,x] | i ∈ {a,b},x ∈ {0,1,−}}= {[qa,0], [qa,1], [qa,−], [qb,0], [qb,1], [qb,−]}
q0 = [qa,−]
F = {[qb,−]}
Γ = {0,1,Λ}
Σ = {0,1}
COMP 2600 — Turing Machines 24
Meaning of [qi,x]:
• control portion
qa M has not yet read the first input symbol
qb M has read the first input symbol
• data portion
x is the first input symbol read
COMP 2600 — Turing Machines 25
Transitions:
• M remembers in [q1,x] that it has read x
δ([qa,−],x) = ([qb,x],x,R) for x ∈ {0,1}
• M moves right as long as it sees a symbol different from the first symbol
δ([qb,0],1) = ([qb,0],1,R)δ([qb,1],0) = ([qb,1],0,R)
• M accepts when it reaches the first blanc symbol
δ([qb,x],Λ) = ([qb,−],Λ,R) for x ∈ {0,1}
COMP 2600 — Turing Machines 26
Multiple tracks
Idea: View tape as having multiple tracks, i.e. each symbol in Γ has multiple
components:
. . . 0 # Λ . . .
. . . 1 0 0 . . .
. . . a a c . . .
The symbols on the tape are
[01a
],
[#0a
]and
[Λ
0c
].
COMP 2600 — Turing Machines 27
Example L = {ww | w ∈ {0,1}+}
Two stages
• find the midpoint
• match symbols
To find the midpoint, view the tape as 2 tracks:
. . . Λ Λ Λ ∗ Λ Λ . . .
. . . 0 1 1 0 1 1 . . .
Γ = {[ΛΛ
],[Λ0
],[Λ1
],[*1
],[*0
]}
Idea: Put markers ∗ on two out-most symbols and move them inwards!
COMP 2600 — Turing Machines 28
Extensions of the basic TM — Multi-tape TM
A Multi-tape TM is like an ordinary Turing machine with several tapes.
Each tape has its own head.
Initially the input w is on tape 1 with the head on the left-most symbol. The
other tapes are all blank.
Transitions specify behaviour of each head independently:
δ(q,x1, . . . ,xk) = (p,(y1,d1), . . . ,(yk,dk))
wherexi is the symbol under head iyi is the new symbol under head idi direction in which head i moves
Note: we can simulate a k-tape TM with a 1-tape TM.
COMP 2600 — Turing Machines 29
Extensions of the basic TM — Non-deterministic TM (NTM)
In a (deterministic) TM δ(q,x) is unique or undefined.
In a NTM, δ(q,x) is a finite set of triplets
δ(q,x) = {(p1,y1,d1), . . . ,(pm,ym,dm)}
At each step, a NTM can non-deterministically choose which transition to
make.
As for NFA’s a NTM accepts if there is a sequence of guesses (non-deterministic
choice) that leads to a final state.
COMP 2600 — Turing Machines 30
Languages and TMs
The language recognised by a TM M
L(M) = {w ∈ Σ∗ | q0w `∗ α1 pα2 with p ∈ F,α1,α2 ∈ Γ
∗}
The class of languages accepted by TMs is called recursively enumerablelanguages.
• for a string w in the language the TM halts on input w in a final state
• for a string w not in the language the TM
– either halts in a non-final state
– or loops forever
Those languages for which the TM halts (regardless of whether it accepts ornot) are called recursive languages.
COMP 2600 — Turing Machines 31
Recursive Languages
A language L is recursive if L = L(M) for some TM M such that
• if w ∈ L, then M accepts (and therefore halts)
• if w is not in L, then M eventually halts, although it never enters an ac-cepting state
A TM of this type corresponds to an algorithm: a well defined sequence ofsteps that always finishes and produces an answer.
If we think of a language L as a “problem”, then problem L is called decidableif it is a recursive language; and it is called undecidable if it is not a recursivelanguage.
Recursive (or “decidable”) languages are not only recursively enumerable butare also accepted by a TM that always halts regardless of whether or not itaccepts.
COMP 2600 — Turing Machines 32