CS 3240 – Chapter 9
Turing Machines From Alan Turing, 1936 Turns out to be the LAST machine The only machine you'll ever need The Church-Turing Thesis All algorithms have a Turing Machine equivalent ▪ Some TMs are not algorithms, however 6/13/20162CS Chapter 9
We have shown that a n b n c n is not context free By the Pumping Lemma What if we add an extra stack to a PDA? 6/13/20163CS Chapter 9
input, pop1/pop2, push1/push2 6/13/20164CS Chapter 9
a n is accepted by an FA a n b n is accepted by a PDA a n b n c n is accepted by a 2PDA What about a n b n c n d n ? 6/13/20165CS Chapter 9
6/13/20166CS Chapter 9
An FA is a machine with no auxiliary memory A PDA is like an FA but with unlimited, restricted memory LIFO access A Turing Machine is like an FA but with unlimited, unrestricted memory We can move to any memory cell We can both read and write there 6/13/20167CS Chapter 9
LanguageMachineGrammar RegularFinite AutomatonRegular Expression, Regular Grammar Context-FreePushdown AutomatonContext-Free Grammar Recursively Enumerable Turing MachineUnrestricted Phrase- Structure Grammar 8CS Introduction
6/13/20169CS Chapter 9
6/13/201610CS Chapter 9
Control Unit (Deterministic) states (1 start state; 1 or more or halt states) transitions: input,output,move (L or R) I/O Tape (2-way, infinite) pre-populated with (finite) input output left on tape after processing special blank symbol ☐ ▪ not part of input alphabet ▪ used as a separator and/or a boundary 6/13/201611CS Chapter 9
Contents left on tape is ignored Typically has only one accepting state with no out-edges Any string that causes the TM to halt in an accepting state is in the TM’s language otherwise it is not in the language (an explicit reject state may also be used) 6/13/201612CS Chapter 9
Pseudocode: Replace the first a with an X Move right to first b, replace with Y Move to right first c, replace with Z Move left to right-most X Repeat the above for steps until no more a’s When no more a’s, skip over all Y’s and Z’s fail if anything other than a Y or Z is encountered Halt and accept or reject 6/13/201613CS Chapter 9
aabbcc Xabbcc XaYbcc XaYbZc XXYbZc XXYYZc XXYYZZ XXYYZZ ☐ XXYYZZ ☐☐ (accept) 6/13/201614CS Chapter 9
6/13/201615CS Chapter 9 Trace aabbcc
(q 0 )aabbcc X(q 1 )abbcc Xa(q 1 )bbcc XaY(q 2 )bcc XaYb(q 2 )cc XaY(q 3 )bZc Xa(q 3 )YbZc X(q 3 )aYbZc (q 3 )XaYbZc X(q 0 )aYbZc XX(q 1 )YbZc XXY(q 1 )bZc XXYY(q 2 )Zc XXYYZ(q 2 )c XXYY(q 3 )ZZ XXY(q 3 )YZZ XX(q 3 )YYZZ X(q 3 )XYYZZ XX(q 0 )YYZZ XXY(q 4 )YZZ XXYY(q 4 )ZZ XXYYZ(q 4 )Z XXYYZZ(q 4 ) ☐ XXYYZZ ☐ (q 5 ) ☐ (accept) 6/13/201616CS Chapter 9
A sequence of configurations in a TM leading to a halt state i.e., it doesn’t crash If the TM is an acceptor, it explicitly accepts If the TM represents some function, f, it leaves f(x) on the tape, for each valid input x 6/13/2016CS Chapter 917
6/13/2016CS Chapter 918
What would be a reasonable strategy? 6/13/2016CS Chapter 919
6/13/2016CS Chapter 920
Suppose f: D → R is a function from domain D to range R If there is a TM that transforms every d in D to f(d) That is, it reads d from the tape, does its work, halts, and leaves f(d) on the tape… Then f is Turing-computable This is the formal definition of an algorithm 6/13/201621CS Chapter 9
Must first decide how to encode numbers on a TM tape Then we must separate the numbers, so we can distinguish between them Plan: represent the numbers in unary notation then move the 1’s from one number to the other! ▪ i.e., just remove the separator, moving the 1’s together ▪ we’ll use zero as a separator ▪ see next slide… 6/13/201622CS Chapter 9
_ _ 6/13/201623CS Chapter 9
6/13/201624CS Chapter 9
Using unary notation, as usual 6/13/2016CS Chapter 925
6/13/201626CS Chapter 9
6/13/201627CS Chapter 9 Trace ababb
TMs typically have one start and one or two halt states (but can have many): one if the TM is a function two if the TM is an acceptor You can “call” a TM as a routine by setting up appropriate “linkage” return to the desired state in the calling machine make sure the tape workspace is in an acceptable configuration for your instructions 6/13/201628CS Chapter 9
Tape configuration: start: [x]0[y]0 finish: [x]0[y]0[xy] First, modify copy to accommodate a 0-delimiters: 0[y]0… → 0[y]0…[y] and finish by positioning at left of y Then, mark a 1 in x with a, copy y to end, and repeat; then restore x’s a’s to 1’s 6/13/201629CS Chapter 9
6/13/201630CS Chapter 9
6/13/2016CS Chapter 931
See Example 9.11 for an explanation of Comparer. See xplusyTM.py 6/13/201632CS Chapter 9
T = (Q, ∑, Γ, δ, q 0, H) Q = set of states ∑ = input alphabet Γ = tape alphabet includes ∑ and blank symbol ( ☐ ) q 0 = start state H = one or more halt states no out-arrows each typically represents a different meaning ▪ e.g., final vs. non-final 6/13/2016CS Chapter 933
6/13/2016CS Chapter 934 δ(q 0,1) = (q 0,1,R) δ(q 0,0) = (q 1,1,R) δ(q 1,1) = (q 1,1,R) δ(q 1, ☐ ) = (q 2, ☐,L) δ(q 2,1) = (q h, ☐,R)
δ ☐ 01 q0q0 (q 1,1,R)(q 0,1,R) q1q1 (q 2, ☐,L) (q 1,1,R) q2q2 (q h, ☐,R) 6/13/2016CS Chapter 935 δ(q 0,1) = (q 0,1,R) δ(q 0,0) = (q 1,1,R) δ(q 1,1) = (q 1,1,R) δ(q 1, ☐ ) = (q 2, ☐,L) δ(q 2,1) = (q h, ☐,R)
q 0,1,q 0,1,R q 0,0,q 1,1,R q 1,1,q 1,1,R q 1,_,q 2,_,L q 2,1,q h,_,R (_ = ☐ ; Use in Program 4) 6/13/2016CS Chapter 936
Input: A TM ▪ start state ▪ halt states ▪ quintuple format: state1,input,state2,output,direction An input string (initial tape contents) Output: A trace of the actions of the machine ▪ with the current state positioned at the read-write head Final state and tape contents 6/13/2016CS Chapter 937
TMs can make multiple passes over the data in multiple directions And can process an arbitrary amount of auxiliary data in addition to the original input They can give more meaningful output vs. just yes/no Interesting fact: TMs came first! 6/13/2016CS Chapter 938
TMs suffer from a vulnerability that FAs and PDAs do not: They are not guaranteed to halt They may hang (loop forever)! Depends on the nature of the computation More on this later 6/13/2016CS Chapter 939
“Any computation that can be carried out by mechanical means can be performed by some TM” Anything that can be done on any digital computer can be done by a TM it’s the “lowest-level” programming language No one has yet found a solvable problem for which a TM cannot be written No alternative computation model invented is more powerful then a TM 6/13/201640CS Chapter 9
The Turing Machine is the model for most programming as we know it Imperative Programming Defines a machine architecture Characterized by reading and writing memory Instructions and data share the same memory There are other models of computation Lambda Calculus (Functional paradigm; CS 4450) Post Systems, Markov Systems, Structured Prog… 6/13/2016CS Chapter 941