330 likes | 367 Views
Understand the limitations of DFAs/PDAs and the power of Turing Machines in computing complex problems. Explore the quintuple defining a TM, transitions, configurations, and ERASE operation strategies. Discover the computation steps with examples.
E N D
The Definition of a Turing Machine Section 4.1 Mon, Nov 7, 2005
Shortcomings of DFAs and PDAs • DFAs and PDAs are good at recognizing simple patterns. • They can recognize many of the patterns that are used in computer programs. • But how would a DFA or a PDA compute the value of 2 + 2?
Shortcomings of DFAs and PDAs • The only possibility would be to design a DFA that would accept the string “2 + 2 = 4” and reject the string “2 + 2 = n” for any other value of n. • But this is not the same as computing 2 + 2. • A computer needs to be able to read a problem and then write the answer on an output tape.
Turing Machines • A Turing Machine will be similar to a DFA except that • It can both read and write on the tape. • It can move both right and left on the tape. • For example, a Turing Machine might read 2 + 2 on the tape, erase it, and write 4 in its place. • A Turing Machine does not need a stack since it can use a portion of the tape to simulate a stack.
Definition of a Turing Machine • A Turing machine is a quintuple (K, Σ, δ, s, H) where • K is a finite set of states. • Σ is a finite tape alphabet, including a left end marker and the blank, but not including the arrows ← and →. • sK is the start state. • H K is the set of halting states. • δ is the transition function δ : (K – H) Σ → K (Σ {, }) such that • for all qK – H, if δ(q, ) = (p, b), then b = . • for all q K – H and aΣ, if δ(q, a) = (p, b), then b.
Transitions • Each transition is of one of the following forms • (p, a) = (q, b), for some bΣ. • (p, a) = (q, ) • (p, a) = (q, ) where a, b Σ. • The two restrictions on guarantee that • The head will not drop off the left end of the tape. • The left end-marker will never be written to any other location.
a; b p q Transition Diagrams • In a diagram of a TM, the transition (p, a) = (q, b) will be denoted
The Turing Machine ERASE • Design a TM that will erase the content of the tape from the current position to the next blank space to the right. • Is it feasible to design a TM that will erase the tape from the current position to the left end-marker? • Is it feasible to design a TM that will erase the tape from the current position to the last non-blank symbol to the right? • Is it feasible to design a TM that will erase the entire tape?
The Turing Machine ERASE • Strategy: • Move to the right, erasing symbols as you go. • Pseudocode: • While the current symbol is not blank, • Write blank and move right.
; s q ; ; h The Turing Machine ERASE
The Turing Machine ERASE • Should the head be returned to its original position? • Strategy: • Write a $. • Move right, erasing symbols as you go. • Then move left, searching for the $. • Erase the $ and stop.
The Turing Machine ERASE • Pseudocode: • If current symbol is not blank, then • Write $ and moveright. • While the current symbol is not blank, • Write blank and moveright. • While the current symbol is not $, • Moveleft. • Write blank.
$; ; ; $ ; $; q r h p s ; ; The Turing Machine ERASE
Configurations • A configuration represents the current status of a Turing machine. • To describe fully that status, we must specify • The current state. • The current contents of the tape. • The current position of the head. • To describe the last two parts, we will specify • The tape contents to the left of the head, including the current symbol. • The tape contents to the right of the head.
Configurations • Thus, a configuration is a triple (q, w, u) where • qK. • w*. • u*(– {}) {e}. • We will abbreviate this as a pair (q, wau), where • qK. • w*. • a. • u*(– {}) {e}. • The underlined symbol a is the current symbol.
Configurations • As before, we will write C1MC2 if configuration C1yields configuration C2in one step. • And we will write C1M*C2 if configuration C1yields configuration C2 in any number of steps. • A sequence C0MC1M … MCn is called a computation.
Example • In the previous example, write the computation when the input is 001. • (s, 001) M (p, $01) M (q, $01) M (p, $ 1) M (q, $ 1) M (p, $ ) M (q, $ ) M (r, $ ) M (r, $ ) M (r, $) M (h, )
The Turing Machine INCR • Design a TM that will add 1 to the nonnegative integer on the input tape. • Strategy: • Move to the blank at the right end of the string. • As long as the cell to the left is 1, change it to a zero and move left. • If the cell to the left is a 0, change it to a 1 and halt. • Special case: The string is all 1s. • Change all but the first 1 to 0. • Add a 0 at the right end.
The Turing Machine INCR • Pseudocode • While symbol is not blank, • Move right. • Move left. • While symbol is 1, • Write 0. • Move left. • If symbol is 0, • Write 1.
The Turing Machine INCR • Else if symbol is , • Move right. • Write 1. • While symbol is not blank, • Move right. • Write 0. • While symbol is not , • Move left. • Move right.
;← ;→ 0;← 1; 0 ;← ;→ 0; 1 h ;→ ; 0 0; 1 ;→ The Turing Machine INCR
INCR INCR Modular Turing Machines • It will be much simpler to design larger Turing Machines if we can use smaller Turing Machines as building blocks. • For example, to design a Turing Machine that adds 2 to a nonnegative integer, we could use two copies of INCR as modules.
Basic Turing Machines • We begin with the basic Turing Machines representing the three basic actions. • Ma – Write the symbol a. • M – Move left. • M – Move right. • We will abbreviate these machines as a, L, and R. • Introduce one more machine S, abbreviated e, that does nothing. • Design the machines a, L, R, and S.
Combining Turing Machines • In structured programming, there are three basic structures. • Sequence – Execute the instructions in the linear order in which they are written. • Decision – Based on the outcome of a comparison, perform a conditional jump forward. • Loop – Based on the outcome of a comparison, perform a conditional jump backwards.
Sequence Structures • To combine two Turing Machines M1 and M2 in sequence, • From each halt state of M1 and for each tape symbol, create a transition to the initial state of M2. • Remove the halting status of the halt states of M1. • The combined machine will be written as M1M2 or M1 M2. • If a machine M is followed by itself n times in sequence, then we will write Mn.
Decision Structures • Let M1, M2, and M3 be Turing Machines and let a. • After executing M1, we will execute M2 if the current symbol is a, and we will execute M3 if the current symbol is not a. • To build such a machine, • From each halt state of M1, build a transition labeled a to the initial state of M2. • From each halt state of M1 and for each symbol other than a, build a transition to the initial state of M3.
M2 a M1 h a M3 Decision Structures • Create a new halt state h. • Add transitions for all symbols from the halt states of M2 and M3 to h. • Remove the halting status of the halt states of M1, M2, and M3. • This machine is represented as follows.
a M a h Loop Structures • A loop structure is a simple variation of a decision structure: the branch goes back to a previously executed Turing Machine instead of forward.
Some Useful Machines • Machines that search left or right for a specified symbol will be very useful. • Rx searches for the first cell containing x to the right of the current cell. • Rx searches for the first cell not containing x to the right of the current cell. • Lx searches for the first cell containing x to the left of the current cell. • Lx searches for the first cell not containing x to the left of the current cell.
$ S L $R $ Example • The Turing Machine ERASE.
Examples • Design the Turing Machine INCR as a modular machine. • Design a Turing Machine DECR that decrements a positive integer. If the input is 0, then it remains unchanged. • Design a Turing Machine COPY that duplicates the string to the right. That is, it converts w into ww. • Design a Turing Machine ABC that accepts the language {anbncn | n 0}.
Universal TM Program • Universal TM.exe