csc 3130: automata theory and formal languages

23
CSC 3130: Automata theory and formal languages Andrej Bogdanov http://www.cse.cuhk.edu.hk/ ~andrejb/csc3130 The Chinese University of Hong Kong More variants of Turing Machines Fall 2009

Upload: mistico-harold

Post on 30-Dec-2015

21 views

Category:

Documents


1 download

DESCRIPTION

Fall 2009. The Chinese University of Hong Kong. CSC 3130: Automata theory and formal languages. More variants of Turing Machines. Andrej Bogdanov http://www.cse.cuhk.edu.hk/~andrejb/csc3130. What does a computer look like?. CPU. instruction memory. PC. 0000. arithmetic logical unit. - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: CSC 3130: Automata theory and formal languages

CSC 3130: Automata theory and formal languages

Andrej Bogdanov

http://www.cse.cuhk.edu.hk/~andrejb/csc3130

The Chinese University of Hong Kong

More variants of Turing Machines

Fall 2009

Page 2: CSC 3130: Automata theory and formal languages

CPU

1010

0100

0000

0000

registe

rs

R0

R1

R2

R3

arithmeticlogical unit

0000PC

What does a computer look like?

data memory

instruction memory

peripheraldevices

Page 3: CSC 3130: Automata theory and formal languages

What does a computer look like?

CPU0000

1010

0100

0000

0000

registe

rs

R0

R1

R2

R3

data memory

instruction memory

arithmeticlogical unit

PC 0000 load 00010001 write R30010 store R50011 add R50100 jpos 0011 ...

0110

0000 0001 0010 0011

0110 0110 0110 0110

0100

Page 4: CSC 3130: Automata theory and formal languages

Instruction set

load x

load Rk

store Rk

read Rk

write Rk

jump n

jpos n

Put the value x into R0

add Rk

Copy the value of Rk into R0

Copy the value of R0 into Rk

Copy the value at memory location Rk into R0

Copy the value of R0 into memory location Rk

Add R0 and Rk, and put result in R0

Set PC to n

Set PC to n, if R0 is positive

jzero n Set PC to n, if R0 is zero

Page 5: CSC 3130: Automata theory and formal languages

Random access machines

• It has registers that can store arbitrary values, aprogram counter, and a random-access memory

load -7 R0 := -7write R2 M[R2] := R0 store R1 R1 := R0 add R1 R0 := R0 + R5

jzero 3 if R0 = 0 then PC := 3accept

instruction meaning012345

0PC

0R0

0R1

0R2

2M 1 2 2 00 1 2 3 4

programcounter

registers

memory…

Page 6: CSC 3130: Automata theory and formal languages

Random access machines

load -7 R0 := -7write R2 M[R2] := R0 save R1 R1 := R0 add R1 R0 := R0 + R1

jzero 3 if R0 = 0 then PC := 3accept

instruction meaning012345

0PC

0R0

0R1

0R2

0M 0 0 0 00 1 2 3 4

-7

-7

-7

-14

12345

• The instructions are indexed by the program counter

Page 7: CSC 3130: Automata theory and formal languages

Random access machines

• Simulating a Turing Machine on a RAM:

Random access machines are equivalent to Turing Machines

PC

2R0

1M 2 1 0 0 …

M …2 11

head

tape

Page 8: CSC 3130: Automata theory and formal languages

save R1 handle for state q0

0

Simulating a TM on a RAM

M

q0

q1

qacc

1/2R…

save R1 save head position read R1 read tape contents x add -1jzero 6 if x = 1 goto line 6

load 2 new value of cellwrite R1 write in memoryload R1 recall head positionadd 1 move head to rightjump 30 go to state q1

program0123

6789

10

…2 11 save R1 handle for state q1

…accept handle for state qacc

30

200

Page 9: CSC 3130: Automata theory and formal languages

Simulating a RAM on a Turing Machine• The configuration of a RAM consists of

– Program counter– Contents of registers– Indices and contents of all nonempty memory cells

14PC

3R0

-7R1

5R2

2M 0 1 2 01 2 3 4

configuration = (14, 3, -7, 5, (0, 2), (2, 1), (3, 2))

0

Page 10: CSC 3130: Automata theory and formal languages

Simulating a RAM on a Turing Machine• The TM has a simulation tape and a scratch tape

• The simulation tape stores RAM configuration

• The TM has a set of states corresponding to eachprogram instruction of the RAM

• The TM tape is updates according to RAM instruction

(14,3,-7,5,(0,2),(2,1),(3,2))M

Page 11: CSC 3130: Automata theory and formal languages

Simulating a RAM on a Turing Machine• Example: load R1 (14,3,-7,5,(0,2),(2,1),(3,2))

2. Write R1 to conf tape

c

1. Copy R1 to scratch tape -7s

(14,-,-7,5,(0,2),(2,1),(3,2))

-7

c

s

(14,-,-7,5,(0,2),(2,1),(3,2))c

(14,-,-7,5,(0,2),(2,1),(3,2) )c

.

(14,- ,-7,5,(0,2),(2,1),(3,2))c

.

.

Make more spaceas needed

(14,-7,-7,5,(0,2),(2,1),(3,2))c3. Erase scratch tape

Page 12: CSC 3130: Automata theory and formal languages

Nondeterministic Turing Machine

• The transition function is nondeterministic:

• The language recognized by N are those strings that can lead N to end in qacc

N

…0 1 0

: (Q – {qacc, qrej}) → subsets of (Q {L, R})

Page 13: CSC 3130: Automata theory and formal languages

Equivalence of NTM and TM

• Let us look more deeply into NTMs

Nondeterministic Turing Machines recognize the same languages as Turing Machines

q3

q51/2R

q61/1L1/1R

Nondeterministic choices can be numbered

The first m steps of the computationare then fully specified by a sequenceof m numbers

1

2

3

Page 14: CSC 3130: Automata theory and formal languages

Simulating a nondeterministic TM

N

…1 00

k = maximum number ofnondeterministic choices in any state

M …1 00

…1 00

…3 21

input tape

2

simulation tape

address tape

Address tape specifies nondeterministic choices of N

First, input is copied from input tape to simulation tapeThen, M simulates N using address tape data

’ = ∪ {1,..., k}

Page 15: CSC 3130: Automata theory and formal languages

How to use the address tape

Suppose N accepts x when nondeterminism = 11221321

Then we want to make sure the address tape contains the string 11221321 at some point in its execution

N

…1 00

M …1 00

…1 00

…3 21

input tape

2

simulation tape

address tape

To ensure this we try all possibilities for the address tape

Page 16: CSC 3130: Automata theory and formal languages

Simulating a nondeterministic TM

…1 00

…1 00

…3 21

input tape x

2

simulation tape z

address tape a

Initially: x = input of N a is emptyFor all possible strings a: Copy x to z. Simulate N on input z using a as the nondeterminism If a specifies an invalid choice or simulation runs out of choices, abort simulation. If N enters its accept state, accept and halt. If N enters its reject state, abort simulation.

Description of M:

Page 17: CSC 3130: Automata theory and formal languages

Simulating a nondeterministic TM

…1

Try all possible choices of the address tape:

…2

…11

…21

M

q0

q1

qacc

1/1R

0/0

R0/1R

input: 10

12

1

abortsimulation runs out of choices at q0

abortsimulation runs out of choices at q1

abortimpossible choice at q0

abortout of choices at q0

accept

address tape

Page 18: CSC 3130: Automata theory and formal languages

Simulating a nondeterministic TM

…1

Try all possible choices of the address tape:

…2

…11

…21

M

q0

q1

qacc

1/1R

2/0

R

0/0L

input: 10

1 1

abortsimulation runs out of choices at q0

abortsimulation runs out of choices at q1

abortimpossible choice at q0

abortout of choices at q0

address tape

0/0L

2

1

abortout of choices at q1

Simulation will loop forever

Page 19: CSC 3130: Automata theory and formal languages

Correctness of simulation

• If N accepts x:

For all possible strings a: Copy x to z. Simulate N on input z using a as the nondeterminism If a specifies an invalid choice or simulation runs out of choices, abort simulation. If N enters its accept state, accept and halt. If N enters its reject state, abort simulation.

Eventually, M will encounter the correct a

Description of M:

So M will accept

Provided that all previoussimulations halt!

Page 20: CSC 3130: Automata theory and formal languages

Correctness of simulation

• Claim: Simulation step always halts (never loops)

For all possible strings a: Copy x to z. Simulate N on input z using a as the nondeterminism If a specifies an invalid choice or simulation runs out of choices, abort simulation. If N enters its accept state, accept and halt. If N enters its reject state, abort simulation.

Description of M:

Since a is finite, the simulation will eventuallyrun out of choices

Page 21: CSC 3130: Automata theory and formal languages

Correctness of simulation

• If N does not accept x, then for every a:

For all possible strings a: Copy x to z. Simulate N on input z using a as the nondeterminism If a specifies an invalid choice or simulation runs out of choices, abandon simulation. If N enters its accept state, accept and halt. If N enters its reject state, abort simulation.

Description of M:Either N will loop, so simulation runs out of choices

Or N rejects, so simulation isabandoned anyway

In either case, simulation fails, so M loops forever!

Page 22: CSC 3130: Automata theory and formal languages

Context-free languages are recognizable• Since Nondeterministic Turing Machines

are more general than Pushdown Automata, it follows that

• In fact

Context-free languages are recognizable (by TMs)

Context-free languages are decidable (by TMs)

Page 23: CSC 3130: Automata theory and formal languages

Recap of computational models

DFA

NFA

PDA TM

NTM

RAMtoy computers