cs154, lecture 7theorem: every multitape turing machine can be transformed into a single tape turing...

42
CS154, Lecture 7: Turing Machines

Upload: others

Post on 30-Sep-2020

1 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: CS154, Lecture 7Theorem: Every Multitape Turing Machine can be transformed into a single tape Turing Machine. Theorem: L is decidable iff both L and L are recognizable

CS154, Lecture 7:

Turing Machines

Page 2: CS154, Lecture 7Theorem: Every Multitape Turing Machine can be transformed into a single tape Turing Machine. Theorem: L is decidable iff both L and L are recognizable

Turing Machine (1936)

INFINITE REWRITABLE TAPE

IA

FINITE STATE CONTROL

N P U T …

Page 3: CS154, Lecture 7Theorem: Every Multitape Turing Machine can be transformed into a single tape Turing Machine. Theorem: L is decidable iff both L and L are recognizable

Turing Machine (1936)

Page 4: CS154, Lecture 7Theorem: Every Multitape Turing Machine can be transformed into a single tape Turing Machine. Theorem: L is decidable iff both L and L are recognizable
Page 5: CS154, Lecture 7Theorem: Every Multitape Turing Machine can be transformed into a single tape Turing Machine. Theorem: L is decidable iff both L and L are recognizable

Turing Machines versus DFAs

TM can both write to and read from the tape

The head can move left and right

The input doesn’t have to be read entirely,

Accept and Reject take immediate effect

and the computation can continue further (even, forever) after all input has been read

Page 6: CS154, Lecture 7Theorem: Every Multitape Turing Machine can be transformed into a single tape Turing Machine. Theorem: L is decidable iff both L and L are recognizable

0 → 0, R

read write move

→ , R

qaccept

qreject

0 → 0, R

→ , R

This Turing machine decides the language {0}

Σ = {0}

Page 7: CS154, Lecture 7Theorem: Every Multitape Turing Machine can be transformed into a single tape Turing Machine. Theorem: L is decidable iff both L and L are recognizable

0 → 0, R

read write move

→ , R

qaccept

0 → 0, R

→ , R

This Turing machine recognizes the language {0}

Σ = {0}

0 → 0, R

→ , L

Page 8: CS154, Lecture 7Theorem: Every Multitape Turing Machine can be transformed into a single tape Turing Machine. Theorem: L is decidable iff both L and L are recognizable

Deciding the language L = { w#w | w {0,1}* }

1. If there’s no # on the tape (or more than one #), reject.2. While there is a bit to the left of #,

Replace the first bit with X, and check if the first bit b to the right of the # is identical. (If not, reject.) Replace that bit b with an X too.

3. If there’s a bit to the right of #, then reject else accept

STATE

0 1 1 # 0 1 1

q0, FIND #

0

q1, FIND # q#, FIND

#

q0, FIND #

0 1X XX

and so on…

STATE

q0, FIND q1, FIND #

STATE

q1, FIND

STATE

q1, FIND #q#, FIND

1

STATE

q0, FIND

STATE

q1, FIND

STATEq1, FIND

STATE

qGO LEFT

STATEqGO LEFT

STATE STATESTATESTATE

Page 9: CS154, Lecture 7Theorem: Every Multitape Turing Machine can be transformed into a single tape Turing Machine. Theorem: L is decidable iff both L and L are recognizable

Definition: A Turing Machine is a 7-tuple T = (Q, Σ, Γ, , q0, qaccept, qreject), where:

Q is a finite set of states

Γ is the tape alphabet, where Γ and Σ Γ

q0 Q is the start state

Σ is the input alphabet, where Σ

: Q Γ→ Q Γ {L, R}

qaccept Q is the accept state

qreject Q is the reject state, and qreject qaccept

Page 10: CS154, Lecture 7Theorem: Every Multitape Turing Machine can be transformed into a single tape Turing Machine. Theorem: L is decidable iff both L and L are recognizable

Turing Machine Configurations

Page 11: CS154, Lecture 7Theorem: Every Multitape Turing Machine can be transformed into a single tape Turing Machine. Theorem: L is decidable iff both L and L are recognizable

Defining Acceptance and Rejection for TMs

Let C1 and C2 be configurations of a TM MDefinition: C1 yields C2 if M is in configuration C2 after running M in configuration C1 for one step

Suppose (q1, b) = (q2, c, L) Then aaq1bb yields aq2acbSuppose (q1, a) = (q2, c, R) Then cabq1a yields cabcq2

Let w Σ* and M be a Turing machineM accepts w if there are configs C0, C1, ..., Ck,

s.t.

• C0 = q0w• Ci yields Ci+1 for i = 0, ..., k-1, and • Ck contains the accept state qaccept

Page 12: CS154, Lecture 7Theorem: Every Multitape Turing Machine can be transformed into a single tape Turing Machine. Theorem: L is decidable iff both L and L are recognizable

A TM M recognizes a language Lif M accepts exactly those strings in L

A TM M decides a language L if M accepts all strings in L and rejects all strings not in L

A language L is recognizable(a.k.a. recursively enumerable) if some TM recognizes L

A language L is decidable (a.k.a. recursive) if some TM decides L

Page 13: CS154, Lecture 7Theorem: Every Multitape Turing Machine can be transformed into a single tape Turing Machine. Theorem: L is decidable iff both L and L are recognizable
Page 14: CS154, Lecture 7Theorem: Every Multitape Turing Machine can be transformed into a single tape Turing Machine. Theorem: L is decidable iff both L and L are recognizable

A Turing machine for deciding { 0 | n ≥ 0 }2n

1. Sweep from left to right, cross out every other 02. If in step 1, the tape had only one 0, accept3. If in step 1, the tape had an odd number of 0’s,

reject4. Move the head back to the first input symbol.5. Go to step 1.

Turing Machine PSEUDOCODE:

Why does this work?Idea: Every time we return to stage 1, the number of 0’s on the tape has been halved.

Page 15: CS154, Lecture 7Theorem: Every Multitape Turing Machine can be transformed into a single tape Turing Machine. Theorem: L is decidable iff both L and L are recognizable

Step 1

{ 0 | n ≥ 0 }2n

Step 2

Step 3

Step 4

Page 16: CS154, Lecture 7Theorem: Every Multitape Turing Machine can be transformed into a single tape Turing Machine. Theorem: L is decidable iff both L and L are recognizable

q00000

q1000

xq300

x0q40

x0xq3

x0q2x

xq20x

q2x0x

q2x0x

{ 0 | n ≥ 0 }2n

Page 17: CS154, Lecture 7Theorem: Every Multitape Turing Machine can be transformed into a single tape Turing Machine. Theorem: L is decidable iff both L and L are recognizable

Multitape Turing Machines

: Q Γk → Q Γk {L,R}k

Page 18: CS154, Lecture 7Theorem: Every Multitape Turing Machine can be transformed into a single tape Turing Machine. Theorem: L is decidable iff both L and L are recognizable

Theorem: Every Multitape Turing Machine can be transformed into a single tape Turing Machine

Page 19: CS154, Lecture 7Theorem: Every Multitape Turing Machine can be transformed into a single tape Turing Machine. Theorem: L is decidable iff both L and L are recognizable

Theorem: Every Multitape Turing Machine can be transformed into a single tape Turing Machine

Page 20: CS154, Lecture 7Theorem: Every Multitape Turing Machine can be transformed into a single tape Turing Machine. Theorem: L is decidable iff both L and L are recognizable

Theorem: Every Multitape Turing Machine can be transformed into a single tape Turing Machine

Page 21: CS154, Lecture 7Theorem: Every Multitape Turing Machine can be transformed into a single tape Turing Machine. Theorem: L is decidable iff both L and L are recognizable

Theorem: Every Multitape Turing Machine can be transformed into a single tape Turing Machine

Page 22: CS154, Lecture 7Theorem: Every Multitape Turing Machine can be transformed into a single tape Turing Machine. Theorem: L is decidable iff both L and L are recognizable

Theorem: Every Multitape Turing Machine can be transformed into a single tape Turing Machine

Page 23: CS154, Lecture 7Theorem: Every Multitape Turing Machine can be transformed into a single tape Turing Machine. Theorem: L is decidable iff both L and L are recognizable

Theorem: L is decidable iff both L and L are recognizable

Page 24: CS154, Lecture 7Theorem: Every Multitape Turing Machine can be transformed into a single tape Turing Machine. Theorem: L is decidable iff both L and L are recognizable

Recall: Given L Σ*, define L := Σ* \ L

How? Any ideas?M1 always accepts x, when x is in LM2 always accepts x, when x isn’t in L

Theorem: L is decidable iff both L and L are recognizable

Given:a TM M1 that recognizes L anda TM M2 that recognizes L, want to build a new machine M that decides L

Page 25: CS154, Lecture 7Theorem: Every Multitape Turing Machine can be transformed into a single tape Turing Machine. Theorem: L is decidable iff both L and L are recognizable

Simulate M1 (x) on one tape, M2 (x) on another. Exactly one of the two will accept If M1 accepts then accept If M2 accepts then reject

Recall: Given L Σ*, define L := Σ* \ L

Theorem: L is decidable iff both L and L are recognizable

Given:a TM M1 that recognizes L anda TM M2 that recognizes L, want to build a new machine M that decides L

Page 26: CS154, Lecture 7Theorem: Every Multitape Turing Machine can be transformed into a single tape Turing Machine. Theorem: L is decidable iff both L and L are recognizable

Theorem: Every nondeterministic Turing machine Ncan be transformed into a Turing Machine M that accepts precisely the same strings as N.

Nondeterministic Turing Machines

Proof Idea (more details in Sipser)Pick a natural ordering on all strings in {Q Γ #}*

M(w): For all strings D {Q Γ #}* in the ordering,Check if D = C0# #Ck where C0, …,Ck is some

accepting computation history for N on w. If so, accept.

Have multiple transitions for a state, symbol pair

Page 27: CS154, Lecture 7Theorem: Every Multitape Turing Machine can be transformed into a single tape Turing Machine. Theorem: L is decidable iff both L and L are recognizable

It is all Zeros and Ones

One of the most popular and overemphasized clichés about computer scientists. Proxy for: genius / nerd / insensitive / anti-social / …

(still bits are quite fundamental, “more than atoms”)

Page 28: CS154, Lecture 7Theorem: Every Multitape Turing Machine can be transformed into a single tape Turing Machine. Theorem: L is decidable iff both L and L are recognizable

Bit Strings Encoding

Encode a finite string in Σ* as a bit string: encode each character as log | Σ | bits.

For x Σ* define bΣ(x) to be its binary encoding

For x, y Σ*, to encode the pair of x and y can add as x, y over

Σ’ = Σ{,}.

Or sometimes better: (x, y) := 0|bΣ(x)|1 bΣ(x) bΣ(y)

Page 29: CS154, Lecture 7Theorem: Every Multitape Turing Machine can be transformed into a single tape Turing Machine. Theorem: L is decidable iff both L and L are recognizable

TM Encoding

Can encode a TM as a bit string: n (states), m (tape symbols), (first) k (are input symbols),

s (start state), t (accept state), r (reject state), u (blank symbol),

transition1, transition2, …

( (p, i), (q, j, L) ), ( (p, i), (q, j, R) ) , …

Similarly, we can encode DFAs and NFAs as bit strings

Page 30: CS154, Lecture 7Theorem: Every Multitape Turing Machine can be transformed into a single tape Turing Machine. Theorem: L is decidable iff both L and L are recognizable

Other ways to encode a TM exist:

0n10m10k10s10t10r10u1 …

n states

m tape symbols (first k are input symbols)

start state

accept state

reject state

blank symbol

( (p, i), (q, j, L) ) = 0p10i10q10j10

( (p, i), (q, j, R) ) = 0p10i10q10j100

Page 31: CS154, Lecture 7Theorem: Every Multitape Turing Machine can be transformed into a single tape Turing Machine. Theorem: L is decidable iff both L and L are recognizable

Binary languages about computations

ADFA = { (B, w) | B encodes a DFA over some Σ, and B accepts w Σ* }

ANFA = { (B, w) | B encodes an NFA, B accepts w }

ATM = { (M, w) | M encodes a TM, M accepts w }

Define the following languages over {0,1}:

Page 32: CS154, Lecture 7Theorem: Every Multitape Turing Machine can be transformed into a single tape Turing Machine. Theorem: L is decidable iff both L and L are recognizable

ATM = { (M, w) | M encodes a TM over some Σ, w encodes a string over Σ and M accepts w}

ATM = { (M, w) | M does not accept w }

Technical Note: We’ll use an decoding of pairs, TMs, and strings so that every binary string decodes to some pair (M, w)

If x {0,1}* doesn’t decode to (M,w) in the usual way, then we define that x decodes to the pair (D, ε)

where D is a “dummy” TM that accepts nothing. Then, we can define the complement of ATM very simply:

Page 33: CS154, Lecture 7Theorem: Every Multitape Turing Machine can be transformed into a single tape Turing Machine. Theorem: L is decidable iff both L and L are recognizable

Universal Turing Machines

Theorem: There is a Turing machine Uwhich takes as input:

(1) the code of an arbitrary TM M(2) an input string w

such that U accepts (M, w) M accepts w.

This is a fundamental property of TMs: There is a Turing Machine that can run arbitrary Turing Machine code!

Note that DFAs/NFAs do not have this property: ADFA and ANFA are not regular.

Page 34: CS154, Lecture 7Theorem: Every Multitape Turing Machine can be transformed into a single tape Turing Machine. Theorem: L is decidable iff both L and L are recognizable

The Church-Turing Thesis

Everyone’s Intuitive Notion of Algorithms

= Turing Machines

This is not a theorem – it is a falsifiable scientific hypothesis.

And it has and is still been tested

Page 35: CS154, Lecture 7Theorem: Every Multitape Turing Machine can be transformed into a single tape Turing Machine. Theorem: L is decidable iff both L and L are recognizable

ADFA = { (D, w) | D is a DFA that accepts string w }

Theorem: ADFA is decidable

Proof: A DFA is a special case of a TM. Run the universal U on (D, w) and output its answer.

ATM = { (M, w) | M is a TM that accepts string w }

Theorem: ATM is recognizable but not decidable!

ANFA = { (N, w) | N is an NFA that accepts string w }

Theorem: ANFA is decidable. (Why?)

Page 36: CS154, Lecture 7Theorem: Every Multitape Turing Machine can be transformed into a single tape Turing Machine. Theorem: L is decidable iff both L and L are recognizable

There are non-recognizable languages

Assuming the Church-Turing Thesis, this means there are problems that NO computing device can solve!

We can prove this using a counting argument: We will show there is no onto function from the set of all Turing Machines to the set of all languages over {0,1} (works for any finite Σ)

That is, every mapping from Turing machines to languages fails to cover all possible languages

Page 37: CS154, Lecture 7Theorem: Every Multitape Turing Machine can be transformed into a single tape Turing Machine. Theorem: L is decidable iff both L and L are recognizable

“There are more problems to solve than there are programs to solve them.”

Languages over {0,1}

Page 38: CS154, Lecture 7Theorem: Every Multitape Turing Machine can be transformed into a single tape Turing Machine. Theorem: L is decidable iff both L and L are recognizable

Let L be any set and 2L be the power set of L

Proof: Assume, for a contradiction, there is an onto function f : L → 2L

Define S = { x L | x f(x) } 2L

If f is onto, then there is a y L with f(y) = S

Suppose y S. By definition of S, y f(y) = S.Suppose y S. By definition of S, y f(y) = S.Contradiction!

Theorem: There is no onto function from L to 2L

f : A → B is not onto (9 b 2 B)(8 a 2 A)[f(a) b]

Page 39: CS154, Lecture 7Theorem: Every Multitape Turing Machine can be transformed into a single tape Turing Machine. Theorem: L is decidable iff both L and L are recognizable

Let f : L → 2L be an arbitrary function

Define S = { x L | x f(x) } 2 2L

For all x L, If x S then x f(x) [by definition of S]If x S then x f(x)

In either case, we have f(x) S. (Why?)Therefore f is not onto!

Let L be any set and 2L be the power set of L

Theorem: There is no onto function from L to 2L

f : A → B is not onto (9 b 2 B)(8 a 2 A)[f(a) b]

Page 40: CS154, Lecture 7Theorem: Every Multitape Turing Machine can be transformed into a single tape Turing Machine. Theorem: L is decidable iff both L and L are recognizable

What does this mean?

No function from L to 2L

can “cover” all the elements in 2L

No matter what the set L is, the power set 2L always has strictly larger cardinality than L

Page 41: CS154, Lecture 7Theorem: Every Multitape Turing Machine can be transformed into a single tape Turing Machine. Theorem: L is decidable iff both L and L are recognizable

{Turing Machines}

{0,1}*{Sets of strings of 0s and 1s}

{Languages over {0,1}}

Set M 2M

But there is no onto function from {Turing Machines}⊆M to 2^M. Contradiction!

Proof: Suppose all languages are recognizable. Then for all L, there’s a Turing machine M for recognizing L. Hence there is an onto R: {Turing Machines} → {Languages}

Thm:There are non-recognizable languages

Page 42: CS154, Lecture 7Theorem: Every Multitape Turing Machine can be transformed into a single tape Turing Machine. Theorem: L is decidable iff both L and L are recognizable

In the early 1900’s, logicians were trying todefine consistent foundations for mathematics.

Suppose X = “Universe of all possible sets”

Frege’s Axiom: Let f : X → {0,1}Then {S X | f(S) = 1} is a set.

Define F = { S X | S S }

Suppose F F. Then by definition, F F.So F F and by definition F F.This logical system is inconsistent!

Russell’s Paradox in Set Theory