1 An Example CSCC63 Worksheet Turing Machines Goal. Design a turing machine, M that accepts only strings of the form {w#w w {0, 1} }. Idea. Describe in words how the machine would work. Read first symbol and cross it off (replace with new symbol x ), move right until #, keep moving right until first non-x symbol to verify same as first symbol seen earlier (remembered through states), cross it off and go back to leftmost non-x symbol to repeat. If more than one # or different symbols or more symbols on one side than the other, reject; otherwise, accept. State Diagram. Draw the state diagram. Use the notation (a, b, c) for transitions where a is the input symbol, b is the symbol to be printed and c is one of L or R for the direction to move the head. q1 (1, x, R) q3 q8 (_,,R) (0, x,r) q2 q5 q_accept q4 (1,x,L) (0,x,L) (x,,l) (0,,L) (1,,L) q6 (#,,L) q7 Note that (a,b,c) means on input a, print b and move c. 1
2 Turing Machine Formal Definition A Turing machine is a 7-tuple (Q, Σ, Γ, d, q 0, q accept, q reject ), where Q is a fixed, non-empty, finite set of states Σ is a fixed, non-empty, finite set of symbols (the input alphabet, with blank symbol not in Σ) Γ is a fixed, non-empty, finite set of symbols (the tape alphabet, with Σ subset of Γ and in Γ ). We use to denote a blank. q 0 Q is the start state (or initial state ) q accept Q is an accepting state q reject Q is a rejecting state (q reject q accept ) δ is the transition function. 3 Definitions δ : Q {q accept, q reject } Γ Q Γ {L, R} Configuration of a TM. The current state, tape content, and head position. Notation. uqv where q Q, strings u, v Γ. We use to denote a blank. q: is the current state, uv: the concatenation of u and v form the current tape content (non-blank portion) head position: head is on leftmost symbol of v. Observations. For all states q i, q j Q, symbols a, b, c Γ and strings u, v Γ, since the tape is infinite to the right, configuration uqv is equivalent to uqv, uqv, etc. if δ(q i, b) = (q j, c, R), then configuration uq i bv yields... ucq j v in one step of computation; if δ(q i, b) = (q j, c, L), then configuration uaq i bv yields... uq j acv and q i bv yields... q j cv (because head cannot move off left end). 4 Another Example. Given input string w L sum where L sum = {1 n 01 m 01 n+m n 1, m 0, n, m N}. Describe a Turing machine M sum that decides L sum. Decides means that the Turing machine halts and recognizes the language. Idea. 2
Add the first set of 1 s and then check whether they are equal. To add, simply walk through the 1s until a 0 is reached, change to a 1, then continue walking right until a 0 is reached, go left, replace the 1 with a 0, then return to the start. Now we need only check that the number of 1s in the the string 1 001 are equal. We already did this with our 1#1 example. High-level Algorithm. M sum = On input string w: 1. Sweep left to right, reading 1 s until a 0 is found (if no 0 reject). 2. Replace 0 with a 1 and move right. 3. Sweep left to right reading 1 s until a 0 is found (if no 0 reject). 4. At 0, go left and replace 1 by a 0. 5. Move left until start of tape found. 6. Use a procedure similar to the previous example but replace w#w with u00u where u {1}. 3
5 Variations of the Turing Machine 5.1 Multitape Allow more than 1 tape, each with an independent head. Initially, all tapes blank except tape 1 that contains input. In one step, current state and symbols are read on all tapes to determine the next state, symbols written and head movements for all tapes. Q. What is δ multi? For a regular TM, δ is defined to be: δ : Q Γ Q Γ {L, R}. δ multi : Q Γ k Q Γ k {L, R} k. Note: Different from k independent machines running concurrently! Q. How can we show that the original Turing machine is just as powerful as the multitape? keep track of contents of k tapes separated by special symbol, keep track of head positions by using additional symbols (duplicates of tape alphabet), and use additional states to remember k symbols to read/write (since k is fixed). Simulating one step of multi-tape machine requires single-tape machine to scan over its entire tape twice: once to read k tapes, once to update k tapes. Another Example: Define A = {0 2n n 0}, the language consisting of all strings of 0s whose length is a power of 2. Design a TM M that decides A. Main idea: M = On input string w : 1. 2. 3. 4. 5. 1. Sweep left to right across the tape, crossing off every other 0. 2. If in stage 1 the tap contained a single 0, accept. 3. If in stage 1 the tape contained more than a single 0 and the number of 0s was odd, reject. 4. Return the head to the left-hand end of the tape. 5. Go to stage 1. 4
6 Enumerators No input. Two tapes and a special state q print. Machine computes and whenever it enters state q print, string written on second tape is printed. The language enumerated by an enumerator is the set of strings printed during computation (could be infinite if computation never halts). Theorem 3.21 A language is Turing-recognizable if and only if some enumerator enumerates it. Q. What is this saying? That we can use enumerators and Turing machines interchangeably when talking about recognizable languages. Proof Idea. ( ) Given enumerator E for language A, construct TM M that recognizes A as follows: M = On input w: Explanation 1. Run E; every time a string is printed, compare it with w. 2. Accept if w is ever printed. Reject if E stops without printing w. If w A, then E eventually prints w so M accepts w. If w A, then E never prints w so M rejects or loops on w. ( ) Given TM M that recognizes A, construct enumerator E for A as follows: Let s 1, s 2, s 3,... be a complete list of all strings in Σ (e.g., in lexicographic order): E = Repeat for i = 1, 2, 3,... 1. Run M for i steps on each input string s 1, s 2,..., s i. 2. Print every string that is accepted. Explanation If w A, then M accepts w so E will eventually print w (infinitely many times why??). If w A, then M rejects or loops on w so E will never print w. 5