Turing Machines COMP 455 – 002, Spring 2019
Total Page:16
File Type:pdf, Size:1020Kb
Turing Machines COMP 455 – 002, Spring 2019 Jim Anderson (modified by Nathan Otterness) 1 Turing Machines We want to study computable functions, or algorithms. In particular, we will look at algorithms for answering certain questions. ❖A question is decidable if and only if an algorithm exists to answer it. Example question: Is the complement of an arbitrary Such an algorithm CFL also a CFL? does exist for any ❖This question is undecidable—there is no algorithm specific CFL. that takes an arbitrary CFL and outputs “yes” if the complement is a CFL and “no” otherwise. Jim Anderson (modified by Nathan Otterness) 2 Undecidability Example We can give an informal “proof” showing that an undecidable problem exists. Let’s consider: The “Hello World” problem: Given a program 푃 and an input to that program 퐼, print “yes” if 푃 prints “hello world” when run with input 퐼 and “no” otherwise. ❖푃 can be any program—including one that is very convoluted! Jim Anderson (modified by Nathan Otterness) 3 Undecidability Example Suppose a program, 퐻, exists that solves the “hello world” problem: 푃 “yes” 퐻 퐼 “no” Now consider another program, 퐻1, which is the same as 퐻, but prints “hello world” instead of “no”: 푃 “yes” 퐻1 퐼 “hello world” Jim Anderson (modified by Nathan Otterness) 4 Undecidability Example 푃 “yes” 퐻 퐼 1 “hello world” Next, construct a program 퐻2 that is identical to 퐻1 but only takes a single input, 푃, that it uses as both 푃 and 퐼 in 퐻1: “yes” 푃 퐻 2 “hello world” Another way to “yes” represent 퐻2: 푃 퐻 1 “hello world” Jim Anderson (modified by Nathan Otterness) 5 Undecidability Example Now consider what happens when we run 퐻2 with itself as input: “yes” 퐻 퐻 2 2 “hello world” We have reached a paradox: Suppose 퐻2 prints “hello world” when fed 퐻2 as input. This means that 퐻2 must also print “yes” when fed 퐻2 as input! Jim Anderson (modified by Nathan Otterness) 6 Turing Machines To be able to rigorously do proofs like the “hello world” example, we need a formal model for defining computable functions. The Turing Machine has become the accepted model for formalizing functions. Jim Anderson (modified by Nathan Otterness) 7 Turing Machines A TM can be defined by a 7-tuple 푄, Σ, Γ, 훿, 푞0, 퐵, 퐹 ❖푄: A finite set of states. A TM’s input starts out on the ❖Γ: The tape alphabet. “tape”, so the tape alphabet ❖Σ: The input alphabet. Σ ⊂ Γ. must include the input alphabet. ❖퐵: The blank tape symbol. (퐵 ∈ Γ, and 퐵 ∉ Σ) ❖훿: The next move function: 푄 × Γ → 푄 × Γ × 퐿, 푅 . ❖푞0: The start state. 훿 returns a new state, 훿 takes a state a new tape symbol, ❖퐹: The set of final states. and tape symbol and whether to move left or right. Jim Anderson (modified by Nathan Otterness) 8 Turing Machines Conceptually, a Turing Machine looks like this: All of the rest of the tape initially Non-blank tape symbols is filled with “blank” symbols. (input symbols, etc.) … 퐵 퐵 푋1 … 푋푖 … 푋푛 퐵 퐵 … Tape head The “finite control” just refers to the part of the TM Finite Control that keeps track of the state. Initially, the input starts out on the tape, and the tape head starts at the leftmost input symbol. Jim Anderson (modified by Nathan Otterness) 9 Example: Turing Machine Accepting 0푛1푛 Input string: 0011 … 퐵 퐵 0 0 1 1 퐵 퐵 퐵 … Finite Control State = 푞0 Initial configuration: In start state, at leftmost input symbol on the tape. Jim Anderson (modified by Nathan Otterness) 10 Example: Turing Machine Accepting 0푛1푛 … 퐵 퐵 푋0 푋0 1푌 1푌 퐵 퐵 퐵 … Finite Control State =푞0 푞120 푞120 푞3 푞4 A Turing Machine halts when it can’t make any more moves. Jim Anderson (modified by Nathan Otterness) 11 Example: Formal Notation 푄, Σ, Γ, 훿, 푞0, 퐵, 퐹 , where: 푄 = 푞0, 푞1, 푞2, 푞3, 푞4 , Σ = 0, 1 , Γ = 0, 1, 푋, 푌, 퐵 , 퐹 = 푞4 , And 훿 is defined on the following slide… Jim Anderson (modified by Nathan Otterness) 12 Example: Formal Notation 훿 is as follows: ❖ 훿 푞0, 0 = 푞1, 푋, 푅 : Change the leftmost 0 to X ❖ 훿 푞0, 푌 = 푞3, 푌, 푅 : There are no more 0s ❖ 훿 푞 , 0 = 푞 , 0, 푅 : Skip over 0s 1 1 “Seek” to the leftmost 1. ❖ 훿 푞1, 푌 = 푞1, 푌, 푅 : Skip over Ys ❖ 훿 푞1, 1 = 푞1, 푌, 퐿 : Change the leftmost 1 to Y ❖ 훿 푞2, 0 = 푞2, 0, 퐿 : Skip over 0s “Seek” to the ❖ 훿 푞 , 푌 = 푞 , 푌, 퐿 : Skip over Ys 2 2 leftmost 0. ❖ 훿 푞2, 푋 = 푞0, 푋, 푅 : Move right of the rightmost X ❖ 훿 푞3, 푌 = 푞3, 푌, 푅 : Make sure there are no more 1s ❖ 훿 푞3, 퐵 = 푞4, 퐵, 푅 : There were no more 1s, accept. Jim Anderson (modified by Nathan Otterness) 13 Instantaneous Descriptions for TMs As with a PDA, we can also give an instantaneous description (ID) for a Turing Machine. This indicates both the location of the An ID for a TM has the following form: 훼1푞훼2. tape head and the ❖푞 corresponds to both the state of the TM and current state (푞). the position of the tape head (푞 is written directly before the tape symbol the head is on). ❖훼1훼2 = the tape’s current contents, and only We can’t write an contains the non-blank portion, except in cases infinite number of cells like 훼퐵퐵퐵푞 or 푞퐵퐵퐵훼. without either infinite input or an infinite The depicted portion of the tape is always finite. number of moves. Jim Anderson (modified by Nathan Otterness) 14 Notation for TM Moves Left moves. Suppose 훿 푞, 푋푖 = 푝, 푌, 퐿 . Then, ❖푋1 … 푋푖−1푞푋푖 … 푋푛├ 푋1 … 푋푖−2푝푋푖−1푌 … 푋푛. 푀 ❑The tape head moved from 푋푖 to 푋푖−1. ❑The state changed from 푞 to 푝. ❑푋푖 was replaced by 푌. We moved left past the start of ❖Special case where 푖 = 1: the tape content, so we need to show the extra blank symbol (퐵). ❑푞푋1푋2 … 푋푛├ 푝퐵푌푋2 … 푋푛 푀 We replaced the rightmost tape ❖Special case where 푖 = 푛 and 푌 = 퐵: symbol (푋푛) with a 퐵, so we no ❑푋1 … 푋푛−1푞푋푛├ 푋1 … 푋푛−2푝푋푛−1 longer include it in the ID. 푀 Jim Anderson (modified by Nathan Otterness) 15 Notation for TM Moves Right moves. Suppose 훿 푞, 푋푖 = 푝, 푌, 푅 . Then, ❖푋1 … 푋푖−1푞푋푖 … 푋푛├ 푋1 … 푋푖−1푌푝푋푖+1 … 푋푛. 푀 ❑The tape head moved from 푋푖 to 푋푖+1. ❑The state changed from 푞 to 푝. ❑푋푖 was replaced by 푌. The 퐵 is included here just ❖Special case where 푖 = 푛: to make it clear what the tape head is pointing at. ❑푋1 … 푋푛+1푞푋푛├ 푋1 … 푋푛−1푌푝퐵. 푀 ❖Special case where 푖 = 1 and 푌 = 퐵: ❑푞푋1 … 푋푛├ 푝푋2 … 푋푛. 푀 Jim Anderson (modified by Nathan Otterness) 16 The Language Defined by a TM The language accepted by a Turing Machine 푀 is defined as: ∗ ∗ 퐿 푀 ≡ {푤 | 푤 ∈ Σ and 푞0푤 ├ 훼1푝훼2 for some 푝 ∈ 퐹, and 푀 ∗ 훼1, 훼2 ∈ Γ }. Assumption: If 푀 accepts 푤, 푀 halts. Notation used for moves (similar to a PDA): 푖 ∗ 푖 ∗ ❖ ├, ├, ├, ├, ├. 푀 푀 ❑The 푖 indicates “exactly 푖 moves” ❑The ∗ indicates “any number of moves” ❑The (optional) 푀 below the ├ indicates that TM 푀 made the moves. Jim Anderson (modified by Nathan Otterness) 17 Recursively Enumerable Languages A language that is accepted by a Turing Machine is called recursively enumerable (RE). If a string 푤 is in 퐿 푀 , then 푀 eventually halts and accepts 푤. If 푤 ∉ 퐿 푀 , then one of two things can happen: ❖푀 halts without accepting 푤, or ❖푀 never halts. Jim Anderson (modified by Nathan Otterness) 18 Recursive Languages A language that is accepted by a TM that halts on all inputs is called recursive. This is the accepted formal definition of “algorithm”. ❖For a recursive language, we can always algorithmically determine membership—but we can’t necessarily do this for RE languages. A problem that is solved by a TM that always halts is called decidable. ❖We will discuss decidable problems in much more detail later! Jim Anderson (modified by Nathan Otterness) 19 TM Construction Technique: Extra Storage Storage in the finite control: You can keep track of a finite amount of extra data by incorporating it into the state of a TM. Example: Keep track of an additional symbol: … … … … … If the “extra data” can be 퐴 or 퐵, and the “state” can be 푞0 or 푞1, then the actual Finite Control states of the TM can be State = 푞0 Extra data = 퐴 {[푞0, 퐴], [푞1, 퐴], [푞0, 퐵], [푞1, 퐵]}. Jim Anderson (modified by Nathan Otterness) 20 Storage in the Finite Control: Example Example: Scan the input and ensure the first symbol doesn’t appear a second time. 푀 = 푄, 0, 1 , 0, 1, 퐵 , 훿, 푞0, 퐵 , 퐵, 퐹 푄 = 푞0, 푞1 × 0, 1, 퐵 퐹 = 푞1, 퐵 ► 훿([푞0, 퐵], 0) = ([푞1, 0], 0, 푅) ► 훿([푞0, 퐵], 1) = ([푞1, 1], 1, 푅) ► 훿([푞1, 0], 1) = ([푞1, 0], 1, 푅) ► 훿([푞1, 1], 0) = ([푞1, 1], 0, 푅) ► 훿([푞1, 0], 퐵) = ([푞1, 퐵], 0, 퐿) ► 훿([푞1, 1], 퐵) = ( 푞1, 퐵 , 0, 퐿) Note: This doesn’t change the fact that the TM has a finite number of states! Jim Anderson (modified by Nathan Otterness) 21 Storage in the Finite Control: Example 훿 푞0, 퐵 , 0 = 푞1, 0 , 0, 푅 훿 푞 , 0 , 1 = 푞 , 0 , 1, 푅 If the first symbol 1 1 훿 푞 , 0 , 퐵 = 푞 , 퐵 , 0, 퐿 was a 0, “store” a 0.