21
Turing Machines Formal Concepts in Computer Science Marco Carbone ITU, March, 2009

Turing Machines · 2009-03-02 · Turing Machines (I) The most powerful model of computation is a Turing Machine (TM). Intuitively, at TM consists of a finite control (a state machine)

  • Upload
    others

  • View
    0

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Turing Machines · 2009-03-02 · Turing Machines (I) The most powerful model of computation is a Turing Machine (TM). Intuitively, at TM consists of a finite control (a state machine)

Turing MachinesFormal Concepts in Computer Science

Marco Carbone

ITU, March, 2009

Page 2: Turing Machines · 2009-03-02 · Turing Machines (I) The most powerful model of computation is a Turing Machine (TM). Intuitively, at TM consists of a finite control (a state machine)

Motivation

Models seen so far:

1. FA’s

2. Regular Expressions (=FA)

3. Grammars: Regular (=FA), Context-Free

Marco Carbone 2 ITU, March, 2009

Page 3: Turing Machines · 2009-03-02 · Turing Machines (I) The most powerful model of computation is a Turing Machine (TM). Intuitively, at TM consists of a finite control (a state machine)

Motivation

Models seen so far:

1. FA’s

2. Regular Expressions (=FA)

3. Grammars: Regular (=FA), Context-Free

• FA’s compute an output from a given inputi.e. given a string they compute accept or reject

• grammars and regular expressions generate all possible strings inthe language they describe

Marco Carbone 3 ITU, March, 2009

Page 4: Turing Machines · 2009-03-02 · Turing Machines (I) The most powerful model of computation is a Turing Machine (TM). Intuitively, at TM consists of a finite control (a state machine)

Motivation (II)

Today, we focus, again, on the computational aspect:

Example A program P (a compiler) takes another program P ′ asinput and computes an (executable) program P ′′.

Example A program P takes a pair, (P ′, ω), as input and decides ifthe program P ′ terminates on input ω.

Marco Carbone 4 ITU, March, 2009

Page 5: Turing Machines · 2009-03-02 · Turing Machines (I) The most powerful model of computation is a Turing Machine (TM). Intuitively, at TM consists of a finite control (a state machine)

Motivation (III)

• Some languages can’t be recognized by FA’s (and generated byregular grammars), e.g. A = {0n1n : n = 0, 1, 2, . . .} is notregular.

• But clearly we can construct a real computer program torecognize A, so we need a more powerfull computational modelthan FA’s.

• We shall here consider the ultimative computational model, it’scalled a Turing Machine

Marco Carbone 5 ITU, March, 2009

Page 6: Turing Machines · 2009-03-02 · Turing Machines (I) The most powerful model of computation is a Turing Machine (TM). Intuitively, at TM consists of a finite control (a state machine)

Overview

• Pushdown Automata

• Turing Machines (TM’s)

- Acceptance and Recognition

- Turing Decidable

- High-level definitions of TM’s

- Computing with TM’s

- Variants of TM’s

- The Halting Problem

Marco Carbone 6 ITU, March, 2009

Page 7: Turing Machines · 2009-03-02 · Turing Machines (I) The most powerful model of computation is a Turing Machine (TM). Intuitively, at TM consists of a finite control (a state machine)

Pushdown Automata

Pushdown automata (PDA’s) are like NFA’s, but contain also astack as memory.

(0,ε,1)

...

1

10

(1,0, ε)

(0,1,1)

PDA’s are equivalent to context free grammars.

Marco Carbone 7 ITU, March, 2009

Page 8: Turing Machines · 2009-03-02 · Turing Machines (I) The most powerful model of computation is a Turing Machine (TM). Intuitively, at TM consists of a finite control (a state machine)

Pushdown Automata (II)

A PDA can recognize A = {0n1n : n = 0, 1, 2, . . .} by pushing 0’s onthe stack until it reads a 1 from which point it pops 0’s from the stack,and accepts if the stack is empty when the whole input string is read.

Exercise. Draw a PDA that recognises the language A.

Some languages, e.g. B = {anbncn : n = 0, 1, 2, . . .} can’t begenerated by a context free grammar, and hence can’t be recognizedby a PDA. B can be generated by a context sensitive grammar.

We can easily construct a program to recognize B so what is thecomputational model equivalent to computer programs?

Marco Carbone 8 ITU, March, 2009

Page 9: Turing Machines · 2009-03-02 · Turing Machines (I) The most powerful model of computation is a Turing Machine (TM). Intuitively, at TM consists of a finite control (a state machine)

Turing Machines (I)The most powerful model of computation is a Turing Machine (TM).

Intuitively, at TM consists of a finite control (a state machine) and aninfinite tape initially containing its input.

Control?

abbbcc

A TM can

• read from and write on the tape

• move the read/write head left and right (one step at the time)

A TM terminates by entering an accept or a reject state, otherwise itgoes on forever.

TM’s can do whatever PDA’s (and FA’s) can do.

Marco Carbone 9 ITU, March, 2009

Page 10: Turing Machines · 2009-03-02 · Turing Machines (I) The most powerful model of computation is a Turing Machine (TM). Intuitively, at TM consists of a finite control (a state machine)

Turing Machines (II)

q0

reject

q2

q1

q3

accept

0 -> _, R

_ -> L

_, 0 -> L

1 -> _, L

_ -> R

_ -> R 1 -> R

0,1 -> R

0,1 -> L

The TM, M1, above recognizes A = {0n1n : n = 0, 1, 2, . . .}. E.g.we obtain:

q00011 ,tq1011 ,t0q111 ,t01q11 ,t011q1t ,t01q21 ,t0q31t ,tq301 , q3t01 ,tq001 , . . .

where t denotes the blank symbol.

Marco Carbone 10 ITU, March, 2009

Page 11: Turing Machines · 2009-03-02 · Turing Machines (I) The most powerful model of computation is a Turing Machine (TM). Intuitively, at TM consists of a finite control (a state machine)

Turing Machines (III)

Formally a TM, M , is a 7-tuple (Q,Σ,Γ, δ, q0, qaccept , qreject) where

Q is a finite set of states,Σ is a finite set, the input alphabet , with t 6∈ Σ,Γ is a finite set, the tape alphabet , where Σ ∪ {t} ⊆ Γ,δ : Q× Γ→ Q× Γ× {L,R} is the transition function,q0 ∈ Q is the start state, andqaccept ∈ Q is the accept state,qreject ∈ Q is the reject state.

M receives input a1a2 . . . , ak ∈ Σ∗ on the k leftmost cells of itstape, the rest of the tape is filled with t. The head is initially over theleftmost square.

δ(q, a) = (q′, b, L) means: being in state q and reading a, Mreplaces a with b, moves the head one place left (if not already at theleftmost end in which case it stays put), and goes to state q′.

Marco Carbone 11 ITU, March, 2009

Page 12: Turing Machines · 2009-03-02 · Turing Machines (I) The most powerful model of computation is a Turing Machine (TM). Intuitively, at TM consists of a finite control (a state machine)

Turing Machines (IV)

The configuration of a TM in state q and with ω1aω2 the tapecontent (with blanks after ω2) and with the head over a is denoted by

C = ω1qaω2

Example C = q00011 is a TM in state q0 with the head over the firstsquare containing 0 and with 011 as the remaining (non blank)symbols on the tape.

Given a configuration C then it may yield another configuration:

C = ωaqbω′ yields ωq′acω′ if δ(q, b) = (q′, c, L)C = qbb′ω′ yields q′cb′ω′ if δ(q, b) = (q′, c, L)C = ωqbb′ω′ yields ωcq′b′ω′ if δ(q, b) = (q′, c, R)

Example On slide 6, q00011 yields tq1011 which in turn yieldst0q111.

Marco Carbone 12 ITU, March, 2009

Page 13: Turing Machines · 2009-03-02 · Turing Machines (I) The most powerful model of computation is a Turing Machine (TM). Intuitively, at TM consists of a finite control (a state machine)

Turing RecognizableA halting configuration is an accepting configuration, ωqacceptω′, or arejecting configuration, ωqrejectω′.

Exercise Find a sequence of configurations beginning in the startingstate and ending with an accepting configuration for M1 on slide 6.Likewise, find a sequence leading to a rejecting configuration.

A TM M accepts input ω if there exists a sequence:

q0ω,C1, C2, . . . , Ck

where q0 is the initial state, Ci yields Ci+1, and Ck is an acceptingconfiguration.

The set of strings accepted by M , L(M), is the languagerecognized by M . A language is Turing recognizable if some TMrecognizes it.

Exercise Is B = {anbncn : n = 0, 1, 2, . . .} Turing recognizable?

Marco Carbone 13 ITU, March, 2009

Page 14: Turing Machines · 2009-03-02 · Turing Machines (I) The most powerful model of computation is a Turing Machine (TM). Intuitively, at TM consists of a finite control (a state machine)

Turing DecidableA TM may loop, because either it terminates in a halting (acceptingor rejecting) configuration, or it doesn’t terminate.

A TM may fail to accept an input by either entering a rejectingconfiguration or by looping.

A non-looping TM is a decider, it always accepts or rejects an input.

Exercise Argue why the TM M1 on slide 6 is a decider?

A decider that recognizes a language M is said to decide M . Alanguage is decidable if some TM decides it.

Example M1 decides A = {0n1n : n = 0, 1, 2, . . .}, so A isdecidable.

Exercise Is B = {anbncn : n = 0, 1, 2, . . .} dedicable? Justify youranswer.

Marco Carbone 14 ITU, March, 2009

Page 15: Turing Machines · 2009-03-02 · Turing Machines (I) The most powerful model of computation is a Turing Machine (TM). Intuitively, at TM consists of a finite control (a state machine)

Highlevel Definition of TM’sIt’s cumbersome to formally define a TM, even just drawing its statediagram.

Consider C = {aibjck : i + j = k, i, j ≥ 0}. Suppose we want toconstruct a TM, MC , that decides it. Then it would be moreconvenient to define as a shorthand:MC = “On input ω:i) scan ω to check if ω ∈ a∗b∗c∗, if not rejectii) return head to leftmost non-blank symbol, or accept if tape is blankiii) if head points to a or b write a blankiv) if head points to c rejectv) move head to rightmost non-blank symbolvi) if head points to c write a blank, else rejectvii) go to ii)”

We must be convinced that each step i)— vii) is really just ashorthand.

Marco Carbone 15 ITU, March, 2009

Page 16: Turing Machines · 2009-03-02 · Turing Machines (I) The most powerful model of computation is a Turing Machine (TM). Intuitively, at TM consists of a finite control (a state machine)

Computing with TM’sSuppose we want to do arithmetic with TM’s, say simple addition.

Let’s assume that in an addition n + m the number n is representedby an and m is represented by bm.

To ease our task, let’s assume a TM may have two tapes, tape 1 and2, where tape 1 is the input tape. The output, in case of accept, iswritten on tape 1 as anbmcn+m.

M+ = “ On input ω:i) scan ω to check if it belongs to a∗b∗, if not rejectii) erase tape 2 and copy contents of tape 1 to tape 2iii) run MC (from the previous slide) on tape 2iv) if MC accepts, acceptv) else write an extra c to the right of the contents on tape 1vi) go to ii) ”

Marco Carbone 16 ITU, March, 2009

Page 17: Turing Machines · 2009-03-02 · Turing Machines (I) The most powerful model of computation is a Turing Machine (TM). Intuitively, at TM consists of a finite control (a state machine)

Variants of TM’sTwo TM’s are equivalent if they recognize the same language. Thefollowing TM’s are equivalent to a standard TM:

- A TM with infinite tape in both directions

- A multi-tape TM

- A TM with multipe heads

- A TM with a two-dimensional tape

- A non-deterministic TMTM’s have an astonishing degree of robustness, and turn out to beequivalent to other general computational models say e.g. theLambda Calculus (by Church): the class of algorithms they allow isthe same.

Church-Turing Thesis The intuitive notion of an algorithm equals theTM notion of an algorithm.

Marco Carbone 17 ITU, March, 2009

Page 18: Turing Machines · 2009-03-02 · Turing Machines (I) The most powerful model of computation is a Turing Machine (TM). Intuitively, at TM consists of a finite control (a state machine)

The Halting Problem (I)Not all problems can be solved by a TM, so (Church-Turing Thesis)some problems can’t be solved by algorithms running on a computer.

Let 〈M〉 be a string encoding a TM M . Consider the UniversalTuring Machine:U = “ On input 〈M〉 and ω where M is a TM and ω it’s input:

i) simulate M on ω.ii) if M accepts, acceptiii) if M rejects, reject “

U can take any M (encoded as a string) and its input ω as input andsimulate M on ω.

U is a recognizer, but not a decider, for

Halt = {(〈M〉, ω) : M is a TM and M accepts ω}because U may loop if M loops on ω.

Marco Carbone 18 ITU, March, 2009

Page 19: Turing Machines · 2009-03-02 · Turing Machines (I) The most powerful model of computation is a Turing Machine (TM). Intuitively, at TM consists of a finite control (a state machine)

The Halting Problem (II)

Theorem Halt = {(〈M〉, ω) : M is a TM and M accepts ω} isundecidable.

Proof (by contradiction) Suppose H decides Halt . I.e. H on input〈M〉 and ω is defined by:

H(〈M〉, ω) =

{accept , if M accepts ωreject , if M doesn’t accept ω

Construct D s.t.

D = “ On input 〈M〉 where M is a TM:i) run H on 〈M〉 and 〈M〉.ii) if H accepts, rejectiii) if H rejects, accept “

Marco Carbone 19 ITU, March, 2009

Page 20: Turing Machines · 2009-03-02 · Turing Machines (I) The most powerful model of computation is a Turing Machine (TM). Intuitively, at TM consists of a finite control (a state machine)

The Halting Problem (III)

The definition of D can be rewritten as:

D(〈M〉) =

{accept , if M doesn’t accept 〈M〉reject , if M accepts 〈M〉

What if we run D with 〈D〉 as input?

D(〈D〉) =

{accept , if D doesn’t accept 〈D〉reject , if D accepts 〈D〉

I.e.

• if D accepts 〈D〉 then D doesn’t accept 〈D〉, and

• if D rejects 〈D〉 then D accepts 〈D〉.

so we have a contradiction, and hence D and neither H can exist.

Marco Carbone 20 ITU, March, 2009

Page 21: Turing Machines · 2009-03-02 · Turing Machines (I) The most powerful model of computation is a Turing Machine (TM). Intuitively, at TM consists of a finite control (a state machine)

Exercises

1) Solve the exercises on the previous slides.

2) Define a TM with tape alphabet 0, 1, and blank that given a stringin {0, 1}∗ as input replaces all 0’s on the tape with 1’s and otherwiseleaves the input unchanged. You aren’t supposed to give a high-leveldefinition.

3) Define a TM that decides all strings in {0, 1}∗ that contains an evennumber of 1’s. You aren’t supposed to give a high-level definition.

4) Give a high-level definition of a TM that recognizes the set{anb2n : n = 0, 1, 2, . . . , }.

Marco Carbone 21 ITU, March, 2009