Chapter 9: Turing Machines

Chapter 9: Turing Machines∗ Peter Cappello Department of Computer Science University of California, Santa Barbara Santa Barbara, CA 93106 cappello@cs....
0 downloads 3 Views 74KB Size
Chapter 9: Turing Machines∗ Peter Cappello Department of Computer Science University of California, Santa Barbara Santa Barbara, CA 93106 [email protected]

• Please read the corresponding chapter before attending this lecture. • These notes are supplemented with figures, and material that arises during the lecture in response to questions. • Please report any errors in these notes to [email protected]. I’ll fix them immediately. ∗ Based on An Introduction to Formal Languages and Automata, 3rd Ed., Peter Linz, Jones and Bartlett Publishers, Inc.

1

Turing machines are the outcome of a quest to understand the limits of computation. 9.1 The Standard Turing Machine • The principal difference between a Turing machine (TM) and less capable machine models, such as pushdown automata, is the TM’s storage. • A TM has a tape for storage. • Like the stack of a PDA, the tape is not bounded.

2

Definition of a Turing Machine

Def. 9.1: A Turing machine M is defined by M = (Q, Σ, Γ, δ, q0, 2, F ), where Q is the set of internal states, Γ is a finite tape alphabet, 2 ∈ Γ is a special symbol, called blank, Σ ⊆ Γ − {2} is the input alphabet, δ : Q × Γ 7→ Q × Γ × {L, R} is the transition function, q0 ∈ Q is the initial state, F ⊆ Q is the set of final states. • δ is a partial function.

3

• When the machine enters a state with no defined transition, it halts. • We assume that there is no transition from any final state. Example: Consider the TM defined by Q Σ Γ F

= = = =

{q0 , q1}, {a, b} {a, b, 2} {q1 }

and δ(q0, a) = (q0 , b, R), δ(q0, b) = (q0 , b, R), δ(q0, 2) = (q1 , 2, L). What does it do on input babba?

4

Example 9.3: Consider a TM, whose: • Q, Σ, and Γ are as defined in the previous example •F =Ø • δ is given by δ(q0, a) δ(q0, b) δ(q0, 2) δ(q1, a) δ(q1, b) δ(q1, 2)

= = = = = =

What does it do on input babba?

5

(q1 , a, R), (q1 , b, R), (q1 , 2, R) (q0 , a, L), (q0 , b, L), (q0 , 2, L)

Def.: A standard Turing Machine has the following features: • Its tape is unbounded in both directions. • It is deterministic: δ defines at most 1 transition for each state. • There is no input file: its input is on its tape when it starts. A computation’s state is given by an instantaneous description (ID): • Tape contents between the leftmost & rightmost non-blank symbols. • Its internal state. • The position of the read/write head. This is displayed as a1a2 · · · ak−1 q ak ak+1 · · · an, where q is to the left of the read/write head: The TM is in internal state q, reading ak .

6

• A sequence of IDs, I1, I2, . . . , In is depicted I1 ` I2 ` . . . ` In or I 1 `∗ I 2 I n . • Illustrate this, using the infinite loop example. • This notation is summarized by the following definition. Def. 9.2 • Let M = (Q, Σ, Γ, δ, q0, 2, F ) be a TM. • Let a1 · · · ak−1 q ak ak+1 · · · an, with ai ∈ Γ, q ∈ Q be an ID of M . a1 · · · ak−1 q ak ak+1 · · · an ` a1 · · · ak−1 b p ak+1 · · · an is possible if and only if δ(q, ak ) = (p, b, R). a1 · · · ak−1 q ak ak+1 · · · an ` a1 · · · p ak−1 bak+1 · · · an is possible if and only if δ(q, ak ) = (p, b, L).

7

• M halts, when starting from initial configuration x1 q x2, x1 q x2 `∗ y1 p ay2, where δ(p, a) is undefined. • The sequence of configurations from the initial configuration to the halting configuration is called a computation. • Example shows that a TM may never halt. • This is depicted by x1 q x2 `∗ ∞, where x1 q x2 is the initial state.

8

Turing Machines as Language Acceptors

Def. 9.3: Let M = (Q, Σ, Γ, δ, q0, 2, F ) be a TM. The language accepted by M is L(M ) = {w ∈ Σ+ : q0w `∗ x1 qf x2, for qf ∈ F, and x1, x2 ∈ Γ∗}. • To ensure that M can detect the end-of-input, w cannot contain the 2 symbol. •w∈ / L(M ) when M either: – halts in a non-final state, or – never halts.

9

Example: For Σ = {0, 1}, construct a TM that accepts L((0 + 1)∗0). • When reading an a, provisionally accept and move right; • When reading a b, provisional reject and move right; • When reading a 2, accept, if in the provisionally accept state, else reject. (diagram) • M = ({a, r, A, R}, {0, 1}, {0, 1, 2}, δ, r, 2, {A}), where δ is given by δ(r, 0) δ(r, 1) δ(a, 0) δ(a, 1) δ(a, 2) δ(r, 2)

= = = = = =

10

(a, 0, R), (r, 1, R), (a, 0, R), (r, 1, R), (A, 2, R), (R, 2, R).

Example 9.7: For Σ = {a, b}, make a TM accepting L(anbn : n > 0). • Starting at leftmost a, replace it with x; • Go right over a and y symbols until, reading a b; • replace b with y; • Go left until x is encountered; • Go right; // we are now in initial state • if encounter a y, scan right while encountering y symbols • if encounter a 2, accept. • M = ({q0 , q1, q2, q3, q4 }, {a, b}, {a, b, 2}, δ, q0 , 2, {q4 }), where δ is

11

// replace the leftmost a with x and go right until a b is read δ(q0, a) δ(q1, a) δ(q1, y) δ(q1, b)

= = = =

(q1, x, R), (q1, a, R), (q1, y, R), (q2, y, L).

// go left until an x is read; go right 1 space δ(q2, y) = (q2 , y, L), δ(q2, a) = (q2 , a, L), δ(q2, x) = (q0 , x, R). // termination sequence δ(q0, y) = (q3, y, R), δ(q3, y) = (q3, y, R), δ(q3, 2) = (q4, 2, R).

12

• After 1 pass, the TM has accomplished q0 aa · · · abb · · · b `∗ x q0 a · · · ayb · · · b. After 2 passes, the TM has accomplished q0 aa · · · abb · · · b `∗ xx q0 a · · · ayyb · · · b. • For aabb, the TM goes through the following configuration sequence: q0 aabb ` ` ` ` `

x q1 abb ` xa q1 bb ` x q2ayb q2xayb ` x q0 ayb ` xx q1 yb xxy q1 b ` xx q2 yy ` x q2 xyy xx q0 yy ` xxy q3 y ` xxyy q32 xxyy2 q42.

• Try some examples of strings not in the language.

13

Example 9.8: For Σ = {a, b}, make a TM accepting L(anbncn : n > 0). One can extend the strategy used in the previous example to ensure that both b and c symbols match in number to a symbols.

14