The Chinese University of Hong Kong Fall 2011 CSCI 3130: Formal languages and automata theory Turing Machines Andrej Bogdanov http://www.cse.cuhk.edu.hk/~andrejb/csc3130
Turing Machines control head a b b input blanks Can both read from and write to the tape Head can move both left and right Tape is infinite Has two special states accept and reject
Example L 1 = {w#w: w {a, b}*} Strategy: Read and remember the first symbol Cross it off (x) Read the first symbol past # If they don t match, reject If they do, cross it off abbaa#abbaa xbbaa#abbaa xbbaa#abbaa xbbaa#xbbaa
Example L 1 = {w#w: w {a, b}*} Strategy: Look for the first uncrossed symbol Cross it off (x) Read the first uncrossed symbol past # If they match, cross it off, else reject At the end, there should be only xs and #s xbbaa#xbbaa xxbaa#xbbaa xxbaa#xbbaa xxbaa#xxbaa xxxxx#xxxxx If not, reject; otherwise, accept.
How Turing Machines operate current state a/bl q 1 q 2 a b a Replace a with b, and move head left new state a/bl q 1 q 2 a b b
Formal Definition A Turing Machine is (Q, Σ, Γ, δ, q 0, q acc, q rej ): Q is a finite set of states; Σ is the input alphabet not containing the blank symbol Γ is the tape alphabet (Σ Γ) including q 0 in Q is the start state; q acc, q rej in Q are the accepting and rejecting state δ is the transition function δ: (Q {q acc, q rej }) Γ Q Γ {L, R} Turing Machines are deterministic
Configurations A configuration consists of the current state, the head position, and tape contents configuration q 1 a b a abq 1 a q 1 a/br q acc q acc a b b abbq acc
Configurations We say configuration C yields C if the TM can go from C to C in one step abq 1 a yields abbq acc The start configuration of the TM on input w is q 0 w An accepting configuration is one that contains q acc ; A rejecting configuration is one that contains q rej
The language of a Turing Machine We say M accepts x if there exists a sequence of configurations C 0, C 1,..., C k where C 0 is starting C i yields C i+1 C k is accepting The language recognized by M is the set of all strings that M accepts
Looping Something strange can happen in a Turing Machine: / R q 0 q acc q rej Σ = {0, 1} input: ε This machine never halts Inputs can be divided into three types q acc q rej accept reject loop
Halting We say M halts on x if there exists a sequence of configurations C 0, C 1,..., C k where C 0 is starting C i yields C i+1 C k is accepting or rejecting A TM M is a decider if it halts on every input Language L is decidable if it is recognized by a TM that halts on every input
Programming Turing Machines Description of Turing Machine: L 1 = {w#w: w {a, b}*} 1 2 3 4 5 6 7 8 Until you reach # and see a left-x Read and remember entry Write x Move right past # and past all xs If this entry is different, reject Otherwise Write x Move left past # and to right of first x If you see only xs followed by, accept xbbaa#xbbaa xxbaa#xbbaa xxbaa#xbbaa xxbaa#xxbaa xxbaa#xxbaa
Programming Turing Machines a/ar b/br x/xr L 1 = {w#w: w {a, b}*} q a1 x/xr #/#R 4 q a2 a/al b/bl x/xl a/al b/bl q 0 #/#R 1 q 1 / R 8 q acc q 2 #/#L 7 q 3 q rej everything else q b1 #/#R 4 q b2 a/ar b/br x/xr x/xr
Programming Turing Machines q 0 #/#R 1 a/ar b/br q a1 x/xr q 1 q b1 a/ar b/br #/#R / R 8 #/#R 4 4 x/xr q a2 q acc q b2 x/xr x/xr a/al b/bl x/xl q 2 #/#L 7 a/al b/bl q 3 input: aab#aab configurations: q 0 aab#aab xq a1 ab#aab xaq a1 b#aab xabq a1 #aab xab#q a2 aab xabq 2 #xab xaq 3 b#xab xq 3 ab#xab q 3 xab#xab xq 0 ab#xab
Programming Turing Machines L 2 = {a i b j c k : i j = k and i, j, k > 0 } High-level description of TM: For every a: Cross off the same number of bs and cs Uncross the crossed bs (but not the cs) Cross off this a If all as and cs are crossed off, accept. Σ = {a, b, c} Γ = {a, b, c, a, b, c, } aabbcccc aabbcccc aabbcccc aabbcccc aabbcccc aabbcccc aabbcccc aabbcccc
Programming Turing Machines L 2 = {a i b j c k : i j = k and i, j, k > 0 } Low-level description of TM: Scan input from left to right to check it looks like aa*bb*cc* Move the head to the first symbol of the tape For every a: how do we know? Cross off the same number of bs and cs Restore the crossed of bs (but not the cs) Cross off this a If all as and cs are crossed off, accept. how to do this?
Programming Turing Machines Implementation details: Move the head to the first symbol of the tape Put a special marker on top of first a Cross off the same number of bs and cs: Replace b by b Move right until you see a c Replace c by c Move left just past the last b If any bs are left, repeat Σ = {a, b, c} Γ = {a, b, c, a, b, c, a, a, } aabbcccc aabbcccc aaqbbcccc aabqbcccc aabbqcccc aabqbcccc aabbqcccc aabbcqccc aabbqcccc
Programming Turing Machines L 3 = {#x 1 #x 2...#x l : x i {0, 1}* and x i x j for each i j} High-level description of TM: #01#0011#1 On input w, For every pair of blocks x i and x j in w, Compare the blocks x i and x j If they are the same, reject. Otherwise, accept.
Programming Turing Machines L 3 = {#x 1 #x 2...#x l : x i {0, 1}* and x i x j for each i j} Low-level description: 0. If input is ε, or has exactly one #, accept. #01#0011#1 1. 2. Place a mark on the leftmost # (i.e. replace # by #) and move right Place another mark on next unmarked # (If there are no more #, accept) #01#0011#1 #01#0011#1
Programming Turing Machines L 3 = {#x 1 #x 2...#x l : x i {0, 1}* and x i x j for each i j} 3. current state: Compare the two strings to the right of marked #. If there are same, reject #01#0011#1 #01#0011#1 4. Move the right # to the right If not possible, move the left # to the next # and put the right # on the next If not possible, accept #01#0011#1 5. Repeat Step 3 #01#0011#1
Programming Turing Machines L 3 = {#x 1 #x 2...#x l : x i {0, 1}* and x i x j for each i j} 3. Compare the two strings to the right of marked #. If there are same, reject 4. Move the right # to the right If not possible, move the left # to the next # and put the right # on the next If not possible, accept #01#0011#1 #01#0011#1 accept
How to describe Turing Machines? Unlike for DFAs, NFAs, PDAs, we rarely give complete state diagrams of Turing Machines Usually we give a high-level description: A recipe about the workings of the Turing Machine Just like in earlier chapters, practice makes perfect!
Programming Turing Machines L 4 = { G : G is a connected undirected graph} Q: How do we feed a graph into a Turing Machine? A: We represent it by a string, e.g. 1 2 3 4 (1,2,3,4)((1,4),(2,3),(3,4)(4,2)) Convention for describing graphs: (nodes)(edges) no node must repeat edges are pairs (node 1,node 2 )
Programming Turing Machines L 4 = { G : G is a connected undirected graph} On input G, 0. Verify that G is the description of a graph (no vertex repeats; edges only go between nodes) 1. Mark the first node of G 2. Repeat until no new nodes are marked: For each node, mark it if it is attached to an already marked node x 1 2 x 3 4 x x 3. If all nodes are marked accept, otherwise reject.
Programming Turing Machines L 4 = { G : G is a connected undirected graph} (1,2,3,4)((1,4)(2,3)(3,4)(4,2)) (1,2,3,4)((1,4)(2,3)(3,4)(4,2)) (1,2,3,4)((1,4)(2,3)(3,4)(4,2)) 1 2 3 4 (1,2,3,4)((1,4)(2,3)(3,4)(4,2)) (1,2,3,4)((1,4)(2,3)(3,4)(4,2)) (1,2,3,4)((1,4)(2,3)(3,4)(4,2)) (1,2,3,4)((1,4)(2,3)(3,4)(4,2)) (1,2,3,4)((1,4)(2,3)(3,4)(4,2)) (1,2,3,4)((1,4)(2,3)(3,4)(4,2)) (1,2,3,4)((1,4)(2,3)(3,4)(4,2)) etc.
The Church-Turing Thesis All arguments [for the CT Thesis] which can be given are bound to be, fundamentally, appeals to intuition, and for this reason rather unsatisfactory mathematically. The arguments which I shall use are of three kinds: 1. A direct appeal to intuition 2. A proof of the equivalence of two definitions (In case the new definition has greater intuitive appeal) 3. Giving examples of large classes of numbers which are computable. 1936: On Computable Numbers, with an Application to the Entscheidungsproblem Section 9. The extent of the computable numbers