csc 3130: automata theory and formal languages
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 PresentationTRANSCRIPT
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
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
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
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
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…
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
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
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
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
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
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
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})
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
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}
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
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:
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
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
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!
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
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!
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)
Recap of computational models
DFA
NFA
PDA TM
NTM
RAMtoy computers