Turing Machines Wen-Guey Tzeng Computer Science Department National Chiao Tung University 1 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, , , , q0, , F) – – – – – – – Q: the set of internal states : the input alphabet : the tape alphabet : QQ{L,R}, the transition function : blank symbol q0Q: the initial state FQ: the set of final states • Note: this TM is deterministic. 4 Transition function • (q0, a)=(q1, d, R) 5 Example: • M=({q0, q1}, {a,b}, {a, b, }, , q0, , {q1}) – (q0, a)=(q0, b, R) – (q0, b)=(q0, b, R) – (q0, )=(q1, , L) 6 Transition graph – (q0, a)=(q0, b, R) – (q0, b)=(q0, b, R) – (q0, )=(q1, , L) 7 TM runs forever • Check TM on input aab 8 Instantaneous description • x1qx2 = a1a2…ak-1 q akak+1…an • Also, called “configuration” 9 Example: • M=({q0, q1}, {a,b}, {a, b, }, , q0, , {q1}) – (q0, a)=(q0, b, R) – (q0, b)=(q0, b, R) – (q0, )=(q1, , L) • ID: q0aa, bq0a, bbq0, bq1b • Move: – q0aa ├ bq0b ├ bbq0├ bq1b (halt) 10 TM halts • A TM on input w “halts” if q0w reaches a configuration that is not defined. – A TM on an input w may not halt. (Run forever) • Halting configuration: x1qx2 (halt) – Halting does not mean acceptance • Accepting configuration: x1qfx2 (halt) – Must halt and enter a final state qf • Infinite loop (non-stop): x1qx2 ├* 11 A computation • A computation is a sequence of configurations that leads to a halt state. • q0aa ├ bq1b ├ bbq1├ bq1b (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. • The language accepted by M L(M)={w+: q0w├* x1qfx2 (halt), qfF, x1, x2* } – X1qfx2 (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 – (q0, a)=(q0, 0, R) – (q0, )=(q1, , R) 16 Example: • Design a TM to accept L={anbn : n1} • 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 • Mark leftmost ‘a’ as ‘x’ in the tape – (q0, a)=(q1, x, R) • Move all the way right to mark ‘b’ as ‘y’ – (q1, a)=(q1, a, R) – (q1, y)=(q1, y, R) – (q1, b)=(q2, y, L) 18 • Move back to mark leftmost ‘a’ as ‘x’ – (q2, y)=(q2, y, L) – (q2, a)=(q2, a, L) – (q2, x)=(q0, x, R) • After marking all a’s, move all the way right to find – (q0, y)=(q3, y, R) – (q3, y)=(q3, y, R) – (q3, )=(q4, , R) – If in the middle ‘b’ is encountered, TM stops. 19 • Run q0aabb 20 TM as transducers • To compute a function f(w)=w’, where w, w’ are strings – q0w├* qf w’ (halt) • A function f:DR is TM-computable if there is a TM M=(Q, , , , q0, , F) such that q0w├* qf f(w) (halt) for all wD 21 Example: • 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)0w(y) – Output: w(x+y)0 – That is, q0w(x)0w(y)├qf w(x+y)0 22 • The transition function • • • • • • • (q0, 1)=(q0, 1, R) (q0, 0)=(q1, 1, R) (q1, 1)=(q1, 1, R) (q1, )=(q2, , R) (q2, 1)=(q3, 0, R) (q3, 1)=(q3, 1, L) (q3, )=(q4, , R) • Run q0111011 23 Example: • Design a TM to compute q0w├* qf 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 24 Run q011 25 Examples: • Design a TM to compute q0w(x)0w(y)├* qY w(x)0w(y) if xy q0w(x)0w(y)├* qN w(x)0w(y) if x<y • Design a TM to compute q0w(x)0w(y)├* qf w(xy) 26 Combining TM for complicated tasks • Design a TM to compute f(x,y)=x+y if xy, and f(x,y)=0 if x<y 27 • qC,0w(x)0w(y) ├* qA,0 w(x)0w(y) if xy ├* qE,0 w(x)0w(y) if x<y • qA,0w(x)0w(y) ├* qA,f w(x+y)0 • qE,0w(x)0w(y) ├* qE,f 0 28 Example: • if a then qj else qk – (qi, a)=(qj0, a, R), for qiQ – (qj0, c)=(qj, c, L), for all c – (qi, b)=(qk0, b, R) for b-{a} – (qk0, c)=(qk, c, L), for all c 29 Example: • Design a TM for q0w(x)0w(y)├* qf 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 30 31 Macroinstructionssubprogram • A calls B 32 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” 33 • 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 34 Algorithms • An algorithm for a functin f:DR is a TM such that q0d├* qf f(d) for all dD 35
© Copyright 2024