cs154, lecture 7 - wordpress.com...definition: a turing machine is a 7-tuple t = (q, Σ, Γ, , q 0,...

42
CS154, Lecture 7: Turing Machines

Upload: others

Post on 25-Jun-2020

4 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: CS154, Lecture 7 - WordPress.com...Definition: A Turing Machine is a 7-tuple T = (Q, Σ, Γ, , q 0, q accept, q reject), where: Q is a finite set of states Γis the tape alphabet,

CS154, Lecture 7:

Turing Machines

Page 2: CS154, Lecture 7 - WordPress.com...Definition: A Turing Machine is a 7-tuple T = (Q, Σ, Γ, , q 0, q accept, q reject), where: Q is a finite set of states Γis the tape alphabet,

Turing Machine (1936)

INFINITE REWRITABLE TAPE

IA

FINITE STATE CONTROL

N P U T …

Page 3: CS154, Lecture 7 - WordPress.com...Definition: A Turing Machine is a 7-tuple T = (Q, Σ, Γ, , q 0, q accept, q reject), where: Q is a finite set of states Γis the tape alphabet,

Turing Machine (1936)

Page 4: CS154, Lecture 7 - WordPress.com...Definition: A Turing Machine is a 7-tuple T = (Q, Σ, Γ, , q 0, q accept, q reject), where: Q is a finite set of states Γis the tape alphabet,
Page 5: CS154, Lecture 7 - WordPress.com...Definition: A Turing Machine is a 7-tuple T = (Q, Σ, Γ, , q 0, q accept, q reject), where: Q is a finite set of states Γis the tape alphabet,

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 7 - WordPress.com...Definition: A Turing Machine is a 7-tuple T = (Q, Σ, Γ, , q 0, q accept, q reject), where: Q is a finite set of states Γis the tape alphabet,

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 7 - WordPress.com...Definition: A Turing Machine is a 7-tuple T = (Q, Σ, Γ, , q 0, q accept, q reject), where: Q is a finite set of states Γis the tape alphabet,

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 7 - WordPress.com...Definition: A Turing Machine is a 7-tuple T = (Q, Σ, Γ, , q 0, q accept, q reject), where: Q is a finite set of states Γis the tape alphabet,

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 7 - WordPress.com...Definition: A Turing Machine is a 7-tuple T = (Q, Σ, Γ, , q 0, q accept, q reject), where: Q is a finite set of states Γis the tape alphabet,

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 7 - WordPress.com...Definition: A Turing Machine is a 7-tuple T = (Q, Σ, Γ, , q 0, q accept, q reject), where: Q is a finite set of states Γis the tape alphabet,

Turing Machine Configurations

Page 11: CS154, Lecture 7 - WordPress.com...Definition: A Turing Machine is a 7-tuple T = (Q, Σ, Γ, , q 0, q accept, q reject), where: Q is a finite set of states Γis the tape alphabet,

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 7 - WordPress.com...Definition: A Turing Machine is a 7-tuple T = (Q, Σ, Γ, , q 0, q accept, q reject), where: Q is a finite set of states Γis the tape alphabet,

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 7 - WordPress.com...Definition: A Turing Machine is a 7-tuple T = (Q, Σ, Γ, , q 0, q accept, q reject), where: Q is a finite set of states Γis the tape alphabet,
Page 14: CS154, Lecture 7 - WordPress.com...Definition: A Turing Machine is a 7-tuple T = (Q, Σ, Γ, , q 0, q accept, q reject), where: Q is a finite set of states Γis the tape alphabet,

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 7 - WordPress.com...Definition: A Turing Machine is a 7-tuple T = (Q, Σ, Γ, , q 0, q accept, q reject), where: Q is a finite set of states Γis the tape alphabet,

Step 1

{ 0 | n ≥ 0 }2n

Step 2

Step 3

Step 4

Page 16: CS154, Lecture 7 - WordPress.com...Definition: A Turing Machine is a 7-tuple T = (Q, Σ, Γ, , q 0, q accept, q reject), where: Q is a finite set of states Γis the tape alphabet,

q00000

q1000

xq300

x0q40

x0xq3

x0q2x

xq20x

q2x0x

q2x0x

{ 0 | n ≥ 0 }2n

Page 17: CS154, Lecture 7 - WordPress.com...Definition: A Turing Machine is a 7-tuple T = (Q, Σ, Γ, , q 0, q accept, q reject), where: Q is a finite set of states Γis the tape alphabet,

Multitape Turing Machines

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

Page 18: CS154, Lecture 7 - WordPress.com...Definition: A Turing Machine is a 7-tuple T = (Q, Σ, Γ, , q 0, q accept, q reject), where: Q is a finite set of states Γis the tape alphabet,

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

Page 19: CS154, Lecture 7 - WordPress.com...Definition: A Turing Machine is a 7-tuple T = (Q, Σ, Γ, , q 0, q accept, q reject), where: Q is a finite set of states Γis the tape alphabet,

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

Page 20: CS154, Lecture 7 - WordPress.com...Definition: A Turing Machine is a 7-tuple T = (Q, Σ, Γ, , q 0, q accept, q reject), where: Q is a finite set of states Γis the tape alphabet,

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

Page 21: CS154, Lecture 7 - WordPress.com...Definition: A Turing Machine is a 7-tuple T = (Q, Σ, Γ, , q 0, q accept, q reject), where: Q is a finite set of states Γis the tape alphabet,

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

Page 22: CS154, Lecture 7 - WordPress.com...Definition: A Turing Machine is a 7-tuple T = (Q, Σ, Γ, , q 0, q accept, q reject), where: Q is a finite set of states Γis the tape alphabet,

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

Page 23: CS154, Lecture 7 - WordPress.com...Definition: A Turing Machine is a 7-tuple T = (Q, Σ, Γ, , q 0, q accept, q reject), where: Q is a finite set of states Γis the tape alphabet,

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

Page 24: CS154, Lecture 7 - WordPress.com...Definition: A Turing Machine is a 7-tuple T = (Q, Σ, Γ, , q 0, q accept, q reject), where: Q is a finite set of states Γis the tape alphabet,

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 7 - WordPress.com...Definition: A Turing Machine is a 7-tuple T = (Q, Σ, Γ, , q 0, q accept, q reject), where: Q is a finite set of states Γis the tape alphabet,

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 7 - WordPress.com...Definition: A Turing Machine is a 7-tuple T = (Q, Σ, Γ, , q 0, q accept, q reject), where: Q is a finite set of states Γis the tape alphabet,

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 7 - WordPress.com...Definition: A Turing Machine is a 7-tuple T = (Q, Σ, Γ, , q 0, q accept, q reject), where: Q is a finite set of states Γis the tape alphabet,

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 7 - WordPress.com...Definition: A Turing Machine is a 7-tuple T = (Q, Σ, Γ, , q 0, q accept, q reject), where: Q is a finite set of states Γis the tape alphabet,

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 7 - WordPress.com...Definition: A Turing Machine is a 7-tuple T = (Q, Σ, Γ, , q 0, q accept, q reject), where: Q is a finite set of states Γis the tape alphabet,

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 7 - WordPress.com...Definition: A Turing Machine is a 7-tuple T = (Q, Σ, Γ, , q 0, q accept, q reject), where: Q is a finite set of states Γis the tape alphabet,

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 7 - WordPress.com...Definition: A Turing Machine is a 7-tuple T = (Q, Σ, Γ, , q 0, q accept, q reject), where: Q is a finite set of states Γis the tape alphabet,

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 7 - WordPress.com...Definition: A Turing Machine is a 7-tuple T = (Q, Σ, Γ, , q 0, q accept, q reject), where: Q is a finite set of states Γis the tape alphabet,

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 7 - WordPress.com...Definition: A Turing Machine is a 7-tuple T = (Q, Σ, Γ, , q 0, q accept, q reject), where: Q is a finite set of states Γis the tape alphabet,

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 7 - WordPress.com...Definition: A Turing Machine is a 7-tuple T = (Q, Σ, Γ, , q 0, q accept, q reject), where: Q is a finite set of states Γis the tape alphabet,

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 7 - WordPress.com...Definition: A Turing Machine is a 7-tuple T = (Q, Σ, Γ, , q 0, q accept, q reject), where: Q is a finite set of states Γis the tape alphabet,

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 7 - WordPress.com...Definition: A Turing Machine is a 7-tuple T = (Q, Σ, Γ, , q 0, q accept, q reject), where: Q is a finite set of states Γis the tape alphabet,

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 7 - WordPress.com...Definition: A Turing Machine is a 7-tuple T = (Q, Σ, Γ, , q 0, q accept, q reject), where: Q is a finite set of states Γis the tape alphabet,

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

Languages over {0,1}

Page 38: CS154, Lecture 7 - WordPress.com...Definition: A Turing Machine is a 7-tuple T = (Q, Σ, Γ, , q 0, q accept, q reject), where: Q is a finite set of states Γis the tape alphabet,

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 7 - WordPress.com...Definition: A Turing Machine is a 7-tuple T = (Q, Σ, Γ, , q 0, q accept, q reject), where: Q is a finite set of states Γis the tape alphabet,

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 7 - WordPress.com...Definition: A Turing Machine is a 7-tuple T = (Q, Σ, Γ, , q 0, q accept, q reject), where: Q is a finite set of states Γis the tape alphabet,

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 7 - WordPress.com...Definition: A Turing Machine is a 7-tuple T = (Q, Σ, Γ, , q 0, q accept, q reject), where: Q is a finite set of states Γis the tape alphabet,

{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 7 - WordPress.com...Definition: A Turing Machine is a 7-tuple T = (Q, Σ, Γ, , q 0, q accept, q reject), where: Q is a finite set of states Γis the tape alphabet,

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