COMPSCI 350 FC 2004 1 NOTES ON TURING MACHINES: I. Turing machines and Languages. Definition 1. A Turing machine (TM) is a 7-tuple (Q, , , , qs, qaccept, qreject), where Q, , are finite sets, and 1) Q is the set of states of the machine, 2) is the input alphabet ( U, the blank symbol, ) 3) is the tape alphabet (U , ). 4) : Q1 x Q x x {L, R} is the machine’s transition function, where Q1 = Q – { qaccept, qreject } 5) qs Q is the start state, usually taken to be q0 or q1. 6) qaccept Q is the accept state, and 7) qreject Q is the reject state (qS ≠ qaccept ≠ qreject). The conventions regulating Turing machines are as follows. A Turing machine begins in the start state qs looking at the left-most symbol of the input on a tape which is infinite to the right (apart from the input the tape is blank). If the machine’s transition function specifies that ( qs, ) = (qj, , R), the machine head then writes symbol in the square it is scanning, and moves Right into state qj. The machine then continues to follow the other instructions in the same way. If, for example, the machine’s transition function specifies that ( qj, ) = (qr, , L), the head writes in the square (overwriting the previously written copy of ) and moves left into state qr. (Note that we follow the convention that if the head is instructed to move left when the head is already on the leftmost square, it stays in the same place for that move and awaits the next instruction.) The machine halts when it enters either state qaccept or qreject . If it never enters those states, the computation continues for ever. Definition 2. (a) Given a Turing machine M, a configuration (‘cf’, for short) for M is a string uqv, where q is a state of M, and where u and v are strings over the machine’s tape alphabet such that the last symbol of v is not the blank symbol U. [uv represents the current tape contents of the machine, with anything on the tape to the right of uv being blank. We say that q is the state of the configuration, and the symbol to the right of q is the scanned symbol of the configuration. Intuitively, one configuration yields another, given a machine M, if we get to the second by applying the transition function of M to the state and scanned symbol of the first. We now define this idea formally. (b) If C and D are configurations of M, we say that C yieldsM D (C ├M D) if one of the following holds (where u, v * and a, b, c ): COMPSCI 350 FC 2004 2 1) C= uaqibv , D = uqk acv, and (qi, b)=( qk, c, L) 2) C= uqibv , D = ucqkv, and (qi, b)=( qk, c, R) 3) C = qibv , D = qk cv , and (qi, b)=( qk, c, L) [can’t go left, so leave head where it is] 4) C = uqi , D = ucqk, and (qi, U)=( qk, c, R) (c) Let C be a configuration. 1) C is the start configuration of M on input w if C = qsw. 2) C is an accepting configuration if the state of the configuration is qaccept. 3) C is a rejecting configuration if the state of the configuration is qreject. 4) C is a halting configuration if C is an accepting or rejecting configuration. Definition 3 (a) A Turing machine M with input alphabet accepts w, where w *, if there is a sequence of configurations C1, ..., Ck such that C1 is the start configuration of M on w, each Ci yieldsM Ci+1 (0< i < k), and Ck is an accepting configuration. (b) A Turing machine M with input alphabet rejects w, where w *, if there is a sequence of configurations C1, ..., Ck such that C1 is the start configuration of M on w, each Ci yieldsM Ci+1 (0< i < k), and Ck is a rejecting configuration. Note that our TMs are deterministic devices, so no machine ever accepts and rejects the same string w. (c) If M is a Turing machine with input alphabet , L(M) is the set of strings over accepted by M. Definition 4 (a) A Turing machine M with input alphabet is a decider if, for all strings w over , M either accepts or rejects w. (b) M decides language L if M is a decider and L=L(M). Definition 5 (a) A language L is Turing-recognisable if there is a Turing machine M such that L=L(M). (b) A language L is Turing-decidable if there is a Turing machine M such that M decides L. Note that it follows immediately that any Turing-decidable language is also Turing-recognisable. Example: Consider the language L = { w | w {0, 1}* and w contains at least two 0’s}. Show that L is Turing-decidable, and that the construction is correct. COMPSCI 350 FC 2004 3 Proof: Consider machine M = (Q, {0, 1}, {0, 1, U}, , q1, qaccept, qreject), where Q ={q1, q2, qaccept, qreject}, and transition function is as follows. [The accompanying notes help to explain the way the function behaves. Note that the transition function could also have been specified in a state diagram.] (q1, U) = (qreject, U, R) (no 0 spotted before the end of string is reached; reject) (q1, 0) = (q2, 0, R) (q2 is the state in which M has seen its first 0; now look for another 0) (q1, 1) = (q1, 1, R) (keep going until a 0 is found) (q2, U) = (qreject, U, R) (no second 0 spotted before the end of string is reached; reject) (q2, 0) = (qaccept, 0, R) (another 0 seen, so no need to look further, just accept) (q2, 1) = (q2, 1, R) (keep going until a second 0 is found) We now show that the construction is correct by proving that L = L(M), ie that both L L(M) and L(M) L. i) To show that L L(M), let w=w1w2...wn and suppose that w L. So w contains at least two 0’s. Let wk be the first 0, and wm the second 0 (1 k < m n). Consider the following cf’s for M. First, given i n, let ui be the substring of w before wi and vi the substring of w after wi. So ui = w1...wi-1 and vi = w1+1...wn. 1. For 1 i k, let Ci = ui q1 wi vi [this is the cf that ‘says’ that the machine is in state q1, scanning wi and where w = ui wi vi ; in particular, C1 = u1 q1 w1 v1 = q1w1w2...wn ; in this case ui is the empty string] 2. For k < j m, let Cj = uj q2wj vj 3. Let Cm+1 = umwmqaccept vm Note that C1 is the start configuration of M on w, and C m+1 is an accepting configuration. We show that M accepts w by showing that, in addition, each Ci yieldsM Ci+1 (for all i such that 1 i m). We show this for each part of our sequence of cf’s: a) For 1 i < k we have Ci = ui q1 wi vi and Ci+1 = ui wi q1 vi . But (q1, wi) = (q1, wi, R) since each such wi is a 1 [the first 0 is wk]. Hence in the case of 1 i < k Ci yieldsM Ci+1 b) Ck = uk q1 wk vi and Ck+1 = uk wk q2 vk But (q1, wk) = (q2, wk, R) since wk is a 0 [the first 0 in the string]. Hence Ck yieldsM Ck+1 c) For k < j < m, Cj = uj q2wj vj and Cj+1 = uj wj q2 vj But (q2, wj) = (q2, wj, R) since each such wj is a 1 [the next 0 after wk is wm]. Hence for k < j < m Cj yieldsM Cj+1 COMPSCI 350 FC 2004 4 d) Cm = um q2 wm vm and Cm+1 = um wm qaccept vm But (q2, wm) = (qaccept, wm, R) since wm is a 0 [the second 0 in the string]. Hence Cm yieldsM Cm+1. Thus Ci yieldsM Ci+1 whenever 1 i m. It follows that M accepts w, and hence that L L(M). ii) To show that L(M) L, let w=w1w2...wn and suppose that w L. Since w L, w contains at most one 0. We show that M does not accept w. a) Suppose w contains only 1’s. Let Ci = ui q1 wi vi if 1 i n, and let Cn+1 = un wn qreject . Note that for 1 i < n , Ci = ui q1 wi vi , Ci+1 = ui wi q1 vi and (q1, wi) = (q1, wi, R) [since each such wi is a 1]. Hence Ci yieldsM Ci+1 . In addition, Cn = un q1 wn and Cn+1 = un wn qreject. But (q1, wn) = (qreject, wn, R) [since wn is a 1]. Hence Cn yieldsM Cn+1. Thus for all 1 i n, Ci yieldsM Ci+1. Since in addition C1 is the start cf and Cn+1 a rejecting cf, it follows that M rejects w. (The limiting case is if w is the empty string, in which case C1 = q1 and C2 = U qreject.) b) Suppose w contains only one 0, wk. Let Ci = ui q1 wi vi if 1 i k, Cj = uj q2wj vj if k < j n, and Cn+1 = un wn qreject . As before, it is easy to show that Ci yieldsM Ci+1 whenever 1 i n . Since C1 is the start cf of M on w and since Cn+1 is a rejecting cf, it follows that M rejects w, so that w L(M). Hence L(M) L. It follows from i) and ii) that L = L(M).