Turing Machines Wen-Guey Tzeng Computer Science Department National Chiao Tung University
Alan Turing One of the first to conceive a machine that can run computation mechanically without human intervention. We call algorithmic computation now. The first to conceive a universal machine that can run programs Break German Enigma Machine (encryption) in WWII Turing Award: the most prestigious award in CS Bio movie: The Imitation Game, 2015 1912-1954 2
TM Model 3
Definition A Turing machine (TM) M is defined by M=(Q,,,, q 0,, F) Q: the set of internal states : the input alphabet : the tape alphabet : Q Q {L,R}, the transition function : blank symbol q 0 Q: the initial state F Q: the set of final states Note: this TM is deterministic. 4
(q 0, a)=(q 1, d, R) Transition function 5
Example: M=({q 0, q 1 }, {a,b}, {a, b, },, q 0,, {q 1 }) (q 0, a)=(q 0, b, R) (q 0, b)=(q 0, b, R) (q 0, )=(q 1,, L) 6
(q 0, a)=(q 0, b, R) (q 0, b)=(q 0, b, R) (q 0, )=(q 1,, L) Transition graph 7
TM runs forever Check TM on input aab 8
Instantaneous description x 1 qx 2 = a 1 a 2 a k-1 q a k a k+1 a n Also, called configuration 9
Example: M=({q 0, q 1 }, {a,b}, {a, b, },, q 0,, {q 1 }) (q 0, a)=(q 0, b, R) (q 0, b)=(q 0, b, R) (q 0, )=(q 1,, L) ID: q 0 aa, bq 0 a, bbq 0, bq 1 b Move: q 0 aa bq 0 a bbq 0 bq 1 b (halt) 10
TM halts A TM on input w halts if q 0 w reaches a configuration that is not defined. A TM on an input w may not halt. (Run forever) Halting configuration: x 1 qx 2 (halt) Halting does not imply acceptance Accepting configuration: x 1 q f x 2 (halt) Must halt and enter a final state q f Acceptance implies halting Infinite loop (non-stop): x 1 qx 2 * 11
A computation A computation is a sequence of configurations that leads to a halt state. q 0 aa bq 1 b bbq 1 bq 1 b (halt) 12
Standard TM It is deterministic. There is only one tape. The tape is unbounded in both directions. All other cells are filled with blanks. No output devices. The input is on the tape in the beginning. The left string on the tape can be viewed as output. 13
TM as Language acceptors Transducers 14
TM as language acceptors A string w is accepted by M if M on input w enters a final state and halts. Note: no need to read the entire input The language accepted by M L(M)={w + : q 0 w * x 1 q f x 2 (halt), q f F, x 1, x 2 * } x 1 q f x 2 (halt) is a halt configuration. A language accepted by some TM is called a recursively enumerable (r.e.) set. 15
Example: Design a TM to accept the strings of form 00* The transition function (q 0, 0)=(q 1, 0, R) (q 1, 0)=(q 1, 0, R) (q 1, )=(q 2,, R) F={q 2 } 16
Example: Design a TM to accept L={a n b n : n 1} Idea: Loop Mark leftmost a as x in the tape Move all the way right to mark b as y After marking all a s, move all the way right to find 17
Example, w=aabb Mark leftmost a as x in the tape (q 0, a)=(q 1, x, R) Move all the way right to mark b as y (q 1, a)=(q 1, a, R) (q 1, y)=(q 1, y, R) (q 1, b)=(q 2, y, L)... a a b b... 18
Move back to find leftmost a (q 2, y)=(q 2, y, L) (q 2, a)=(q 2, a, L) (q 2, x)=(q 0, x, R) Repeat until all a s are marked as x s... x a y b... 19
After marking all a s as x s, move all the way right to find (q 0, y)=(q 3, y, R) (q 3, y)=(q 3, y, R) (q 3, )=(q 4,, R) If b is encountered during the action of moving right, TM halts on a non-final state.... x x y y... 20
Example: Design a TM to accept L={w {a,b} + : n a (w)=n b (w)} Idea: Loop Pose at the beginning of the string Mark leftmost a as x in the tape Move all the way right to find the first b and mark it as After marking all symbols as X, enter the final state 21
TM as transducers To compute a function f(w)=w, where w, w are strings q 0 w * q f w (halt) A function f:d R is TM-computable if there is a TM M=(Q,,,, q 0,, F) such that q 0 w * q f f(w) (halt) for all w D We will show in the following that TM can compute complicated functions 22
Example: (integer adder) Given two positive integer x and y, compute x+y x and y are unary-represented Eg. X=3, w(x)=111 Input: w(x) 0 w(y) Output: w(x+y) 0 That is, q 0 w(x)0w(y) q f w(x+y)0 23
The transition function (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 3, 0, L) (q 3, 1)=(q 3, 1, L) (q 3, )=(q 4,, R) Run q 0 111011 24
Example: (string copier) Design a TM to compute q 0 w * q f ww, w=1 + Steps 1. Replace every 1 by x 2. Find the rightmost x and replace it with 1 3. Travel to the right end and create 1 (in replace of ) 4. Repeat 2 and 3 until there are no x s 25
Run q 0 11 26
Examples: (comparer) Design a TM to compute q 0 w(x)0w(y) * q Y w(x)0w(y) if x y q 0 w(x)0w(y) * q N w(x)0w(y) if x<y 27
Combining TM for complicated tasks Design a TM to compute f(x,y)=x+y if x y, and f(x,y)=0 if x<y 28
q C,0 w(x)0w(y) * q A,0 w(x)0w(y) if x y * q E,0 w(x)0w(y) if x<y q A,0 w(x)0w(y) * q A,f w(x+y)0 q E,0 w(x)0w(y) * q E,f 0 29
Example: (if-then-else) if a then q j else q k (q i, a)=(q j0, a, R), for q i Q (q j0, c)=(q j, c, L), for all c (q i, b)=(q k0, b, R) for b -{a} (q k0, c)=(q k, c, L), for all c 30
Example: (integer multiplier) Design a TM for q 0 w(x)0w(y) * q f w(xy) Steps Loop until x contains no more 1 s Find a 1 in x and replace it with another a Replace leftmost 0 by 0y Replace all a s with 1 s 31
32
Macroinstructions subprogram A calls B 33
Turing s Thesis Mechanical computation Computation without human intervention Turing Thesis: any computation that can be carried out by mechanical means can be carried out by some Turing machine. Also called Church-Turing Thesis 34
Reasoning: Anything that can be done by an existing digital computer can be done by a TM No one has yet been able to suggest a problem, solvable by algorithms, but cannot be solved by some TM Other models are proposed. But, they are no more powerful than TM s. Even quantum computers are no more powerful than TM s in terms of the power language acceptance 35
Algorithms An algorithm for a functin f:d R is a TM such that q 0 d * q f f(d) for all d D 36