8 Turing Machines
8 Turing Machines
𝑃 “yes”
𝐻
𝐼 “no”
Another way to
represent 𝐻2 : 𝑃 “yes”
𝐻1
“hello world”
Jim Anderson (modified by Nathan Otterness) 5
Undecidability Example
Now consider what happens when we run 𝐻2 with
itself as input:
“yes”
𝐻2 𝐻2
“hello world”
We have reached a paradox:
Suppose 𝐻2 prints “hello world” when fed 𝐻2 as
input. This means that 𝐻2 must also print “yes”
when fed 𝐻2 as input!
… 𝐵 𝐵 𝑋1 … 𝑋𝑖 … 𝑋𝑛 𝐵 𝐵 …
Tape head The “finite control” just
refers to the part of the TM
Finite Control that keeps track of the state.
Initially, the input starts out on the tape, and the tape
head starts at the leftmost input symbol.
Jim Anderson (modified by Nathan Otterness) 9
Example: Turing Machine Accepting 0𝑛 1𝑛
Input string: 0011
… 𝐵 𝐵 0 0 1 1 𝐵 𝐵 𝐵 …
Finite Control
State = 𝑞0
Initial configuration:
In start state, at
leftmost input symbol
on the tape.
0 𝑋
… 𝐵 𝐵 𝑋 0 1𝑌 1𝑌 𝐵 𝐵 𝐵 …
Finite Control
State = 𝑞0 𝑞120 𝑞120 𝑞3 𝑞4
… 𝐵 ¢ 1 0 1 1 $ 𝐵 𝐵 …
… 𝐵 𝐵 𝐵 𝐵 0 1 𝐵 𝐵 𝐵 …
… 𝐵 𝐵 0 0 0 0 0 𝐵 𝐵 …
Finite Control
In the above figure, the “real” input is between the ¢ and $ on
the first track; the other tracks are used for scratch storage.
Tape symbols are tuples, i.e., ¢, 𝐵, 𝐵 , 1, 𝐵, 0 , $, 𝐵, 0 , etc.
This doesn’t change the fact that there are a finite number of
possible tape symbols!
Jim Anderson (modified by Nathan Otterness) 23
Multiple Tracks: Example
We will use multiple tracks and storage in the
finite control to write a TM that accepts the
language 𝐿 = 𝑤𝑐𝑤 | 𝑤 ∈ 𝐚 + 𝐛 ∗ . Any string 𝑤, followed by a “c”,
This TM will work by “checking off” followed by a second copy of 𝑤.
corresponding symbols in each copy of 𝑤.
► 𝛿 𝑞4 , 𝐵 , ∗, 𝑑 = 𝑞4 , 𝐵 , ∗, 𝑑 , 𝐿 ► 𝛿 𝑞8 , 𝐵 , ∗, 𝑑 = 𝑞8 , 𝐵 , ∗, 𝑑 , 𝑅
► 𝛿 𝑞4 , 𝐵 , 𝐵, 𝑐 = 𝑞5 , 𝐵 , 𝐵, 𝑐 , 𝐿 ► 𝛿 𝑞8 , 𝐵 , 𝐵, 𝐵 = 𝑞9 , 𝐵 , 𝐵, 𝐵 , 𝐿
► 𝛿 𝑞4 , 𝐵 , ∗, 𝑑 = 𝑞4 , 𝐵 , ∗, 𝑑 , 𝐿
► 𝛿 𝑞4 , 𝐵 , 𝐵, 𝑐 = 𝑞5 , 𝐵 , 𝐵, 𝑐 , 𝐿
► 𝛿 𝑞1 , 𝐵 , 𝐵, 𝑑 = 𝑞2 , 𝑑 , ∗, 𝑑 , 𝑅
The point of state 𝑞3 is to move
► 𝛿([𝑞2 , 𝑑], 𝐵, 𝑒 ) = ([𝑞2 , 𝑑], 𝐵, 𝑒 , 𝑅)
right until we reach the first
► 𝛿([𝑞2 , 𝑑], [𝐵, 𝑐]) = ([𝑞3 , 𝑑], [𝐵, 𝑐], 𝑅) unchecked symbol in the right-
hand string.
► 𝛿 𝑞3 , 𝑑 , ∗, 𝑒 = 𝑞3 , 𝑑 , ∗, 𝑒 , 𝑅
► 𝛿 𝑞3 , 𝑑 , 𝐵, 𝑑 = 𝑞4 , 𝐵 , ∗, 𝑑 , 𝐿 Once this symbol is reached,
check it off. (The TM will die here
► 𝛿 𝑞4 , 𝐵 , ∗, 𝑑 = 𝑞4 , 𝐵 , ∗, 𝑑 , 𝐿 if the symbol doesn’t match the
► 𝛿 𝑞4 , 𝐵 , 𝐵, 𝑐 = 𝑞5 , 𝐵 , 𝐵, 𝑐 , 𝐿
one in the finite-control storage.)
► 𝛿 𝑞1 , 𝐵 , 𝐵, 𝑑 = 𝑞2 , 𝑑 , ∗, 𝑑 , 𝑅
► 𝛿 𝑞8 , 𝐵 , ∗, 𝑑 = 𝑞8 , 𝐵 , ∗, 𝑑 , 𝑅
► 𝛿 𝑞8 , 𝐵 , 𝐵, 𝐵 = 𝑞9 , 𝐵 , 𝐵, 𝐵 , 𝐿
► 𝛿 𝑞5 , 𝐵 , 𝐵, 𝑑 = 𝑞6 , 𝐵 , 𝐵, 𝑑 , 𝐿
► 𝛿 𝑞5 , 𝐵 , ∗, 𝑑 = 𝑞7 , 𝐵 , ∗, 𝑑 , 𝑅
In state 𝑞7 , everything in
the left-hand string has ► 𝛿 𝑞6 , 𝐵 , 𝐵, 𝑑 = 𝑞6 , 𝐵 , 𝐵, 𝑑 , 𝐿
been checked off, so we’re ► 𝛿 𝑞6 , 𝐵 , ∗, 𝑑 = 𝑞1 , 𝐵 , ∗, 𝑑 , 𝑅
getting ready to make sure
everything in the right- ► 𝛿 𝑞7 , 𝐵 , 𝐵, 𝑐 = 𝑞8 , 𝐵 , 𝐵, 𝑐 , 𝑅
hand string is checked off,
too. ► 𝛿 𝑞8 , 𝐵 , ∗, 𝑑 = 𝑞8 , 𝐵 , ∗, 𝑑 , 𝑅
State 𝑞7 ’s main purpose is ► 𝛿 𝑞8 , 𝐵 , 𝐵, 𝐵 = 𝑞9 , 𝐵 , 𝐵, 𝐵 , 𝐿
to move right past the 𝑐.
Jim Anderson (modified by Nathan Otterness) 37
Multiple Tracks: Example
(Let 𝑑 ∈ 𝑎, 𝑏 and 𝑒 ∈ 𝑎, 𝑏 .)
► 𝛿 𝑞5 , 𝐵 , 𝐵, 𝑑 = 𝑞6 , 𝐵 , 𝐵, 𝑑 , 𝐿
► 𝛿 𝑞5 , 𝐵 , ∗, 𝑑 = 𝑞7 , 𝐵 , ∗, 𝑑 , 𝑅
In state 𝑞8 , move right while
checking that everything in ► 𝛿 𝑞6 , 𝐵 , 𝐵, 𝑑 = 𝑞6 , 𝐵 , 𝐵, 𝑑 , 𝐿
the right-hand string is ► 𝛿 𝑞6 , 𝐵 , ∗, 𝑑 = 𝑞1 , 𝐵 , ∗, 𝑑 , 𝑅
checked off (if any input
symbol is not checked off, ► 𝛿 𝑞7 , 𝐵 , 𝐵, 𝑐 = 𝑞8 , 𝐵 , 𝐵, 𝑐 , 𝑅
execution will die).
► 𝛿 𝑞8 , 𝐵 , ∗, 𝑑 = 𝑞8 , 𝐵 , ∗, 𝑑 , 𝑅
If we made it past all the input ► 𝛿 𝑞8 , 𝐵 , 𝐵, 𝐵 = 𝑞9 , 𝐵 , 𝐵, 𝐵 , 𝐿
symbols without dying, go to Side note: we already accept here, so it
state 𝑞9 (which is accepting). doesn’t matter whether we move L or R now.
Jim Anderson (modified by Nathan Otterness) 38
Multiple Tracks: Example
Here’s the TM as a
transition diagram:
► 𝛿 𝑞1 , 𝐴1 , 𝐴2 , 𝐴3 = 𝑞1 , 𝐴2 , 𝐴3 , 𝐴1 , 𝑅 , 𝐴1 , 𝐴2 , and 𝐴3 ∈ Γ − 𝐵, 𝑋
► 𝛿 𝑞1 , 𝐴1 , 𝐴2 , 𝐵 = 𝑞1 , 𝐴2 , 𝐵 , 𝐴1 , 𝑅 , 𝐴1 and 𝐴2 ∈ Γ − 𝐵, 𝑋
► 𝛿 𝑞1 , 𝐴1 , 𝐵 , 𝐵 = 𝑞2 , 𝐵, 𝐵 , 𝐴1 , 𝐿 , 𝐴1 ∈ Γ − 𝐵, 𝑋
► 𝛿 𝑞2 , 𝐵, 𝐵 , 𝐴 = 𝑞2 , 𝐵, 𝐵 , 𝐴, 𝐿 , 𝐴 ∈ Γ − 𝐵, 𝑋
► 𝛿 𝑞1 , 𝐴1 , 𝐴2 , 𝐴3 = 𝑞1 , 𝐴2 , 𝐴3 , 𝐴1 , 𝑅 , 𝐴1 , 𝐴2 , and 𝐴3 ∈ Γ − 𝐵, 𝑋
► 𝛿 𝑞1 , 𝐴1 , 𝐴2 , 𝐴3 = 𝑞1 , 𝐴2 , 𝐴3 , 𝐴1 , 𝑅 , 𝐴1 , 𝐴2 , and 𝐴3 ∈ Γ − 𝐵, 𝑋
► 𝛿 𝑞1 , 𝐴1 , 𝐴2 , 𝐵 = 𝑞1 , 𝐴2 , 𝐵 , 𝐴1 , 𝑅 , 𝐴1 and 𝐴2 ∈ Γ − 𝐵, 𝑋
► 𝛿 𝑞1 , 𝐴1 , 𝐵 , 𝐵 = 𝑞2 , 𝐵, 𝐵 , 𝐴1 , 𝐿 , 𝐴1 ∈ Γ − 𝐵, 𝑋
… 𝐵 𝐵 0 0𝑋 1𝑋 1 𝐵 𝐵 𝐵 𝐵 …
𝐴1 , 𝐴2 , 𝐴3 , 𝐴 ∉ 𝑋, 𝐵 :
Finite Control 𝛿 𝑞1 , 𝐵, 𝐵 , 𝐴1 = 𝑞1 , 𝐵, 𝐴1 , 𝑋, 𝑅
State = 𝑞12 𝛿 𝑞1 , 𝐵, 𝐴1 , 𝐴2 = 𝑞1 , 𝐴1 , 𝐴2 , 𝑋, 𝑅
Buffer = 𝐵 , 𝐵 𝛿 𝑞1 , 𝐴1 , 𝐴2 , 𝐴3 = 𝑞1 , 𝐴2 , 𝐴3 , 𝐴1 , 𝑅
𝛿 𝑞1 , 𝐴1 , 𝐴2 , 𝐵 = 𝑞1 , 𝐴2 , 𝐵 , 𝐴1 , 𝑅
𝛿 𝑞1 , 𝐴1 , 𝐵 , 𝐵 = 𝑞2 , 𝐵, 𝐵 , 𝐴1 , 𝐿
Done! 𝛿 𝑞2 , 𝐵, 𝐵 , 𝐴 = 𝑞2 , 𝐵, 𝐵 , 𝐴, 𝐿
The “copy”
subroutine goes here.
𝑘 tapes … … ……… … …… …
… … ……… … …… …
Tape for 𝑀1 : 𝐴0 𝐴1 𝐴2 …
¢ 𝐴−1 𝐴−2 … The ¢ helps us
identify the
Jim Anderson (modified by Nathan Otterness)
leftmost cell. 65
Restricted TMs: Semi-infinite Tape
Tape for 𝑀2 : … 𝐴−2 𝐴−1 𝐴0 𝐴1 𝐴2 …
Tape for 𝑀1 : 𝐴0 𝐴1 𝐴2 …
¢ 𝐴−1 𝐴−2 …
Formally, 𝑀1 = 𝑄1 , Σ1 , Γ1 , 𝛿1 , 𝑞1 , 𝐵, 𝐹1 .
❖ 𝑄1 contains all 𝑞, 𝑈 and 𝑞, 𝐷 for all 𝑞 ∈ 𝑄2 .
❑ We’re storing 𝑈 or 𝐷 in the finite control to remember
whether 𝑀1 is on the upper “𝑈”, or lower “𝐷” track.
❑ 𝑄1 also contains 𝑞1 by itself.
Tape for 𝑀1 : 𝐴0 𝐴1 𝐴2 …
¢ 𝐴−1 𝐴−2 …
Formally, 𝑀1 = 𝑄1 , Σ1 , Γ1 , 𝛿1 , 𝑞1 , 𝐵, 𝐹1 .
❖ Σ1 contains all 𝑎, 𝐵 , where 𝑎 ∈ Σ2 .
❖ 𝐹1 is 𝑞, 𝑈 , 𝑞, 𝐷 | 𝑞 ∈ 𝐹2 .
❖𝐵 is 𝐵, 𝐵 .
Tape for 𝑀1 : 𝐴0 𝐴1 𝐴2 …
¢ 𝐴−1 𝐴−2 …
The definition of 𝛿1 :
𝛿1 𝑞1 , 𝑎, 𝐵 = 𝑞, 𝑈 , 𝑋, ¢ , 𝑅 if 𝛿2 𝑞2 , 𝑎 = 𝑞, 𝑋, 𝑅
❖ This is the case when the first move of 𝑀2 is right.
𝛿1 𝑞1 , 𝑎, 𝐵 = 𝑞, 𝐷 , 𝑋, ¢ , 𝑅 if 𝛿2 𝑞2 , 𝑎 = 𝑞, 𝑋, 𝐿
❖ This is the case when the first move of 𝑀2 is left.
Jim Anderson (modified by Nathan Otterness) 68
Restricted TMs: Semi-infinite Tape
Tape for 𝑀2 : … 𝐴−2 𝐴−1 𝐴0 𝐴1 𝐴2 …
Tape for 𝑀1 : 𝐴0 𝐴1 𝐴2 …
¢ 𝐴−1 𝐴−2 …
The definition of 𝛿1 (continued):
For all 𝑋, 𝑌 ∈ Γ1 , with 𝑌 ≠ ¢, and 𝐴 = 𝐿 or 𝑅: If we’re in the lower track
in 𝑀1 we need to move in
𝛿1 𝑞, 𝑈 , 𝑋, 𝑌 = 𝑝, 𝑈 , 𝑍, 𝑌 , 𝐴 if 𝛿2 𝑞, 𝑋 = 𝑝, 𝑍, 𝐴 . the opposite direction from
❖ This simulates 𝑀2 on the upper track. how 𝑀2 would move.
𝛿1 𝑞, 𝐷 , 𝑋, 𝑌 = 𝑝, 𝐷 , 𝑋, 𝑍 , 𝐴 if 𝛿2 𝑞, 𝑌 = 𝑝, 𝑍, 𝐴ҧ
❖ This simulates 𝑀2 on the lower track.
Jim Anderson (modified by Nathan Otterness) 69
Restricted TMs: Semi-infinite Tape
Tape for 𝑀2 : … 𝐴−2 𝐴−1 𝐴0 𝐴1 𝐴2 …
Tape for 𝑀1 : 𝐴0 𝐴1 𝐴2 …
¢ 𝐴−1 𝐴−2 …
The definition of 𝛿1 (continued):
𝛿1 𝑞, 𝑈 , 𝑋, ¢ = 𝛿1 𝑞, 𝐷 , 𝑋, ¢ = 𝑝, 𝐶 , 𝑌, ¢ , 𝑅 ,
if 𝛿2 𝑞, 𝑋 = 𝑝, 𝑌, 𝐴 , where
𝑈 if 𝐴 = 𝑅
𝐶=ቊ
𝐷 if 𝐴 = 𝐿
This allows 𝑀1 to switch tracks if it is currently at the leftmost cell.
Jim Anderson (modified by Nathan Otterness) 70
Restricted TMs: Two-Stack Machine
Imagine a PDA that can make decisions and modify two
stacks rather than one. This is called a two-stack machine.
Theorem 8.13: A two-stack machine can simulate a TM.
Proof sketch:
TM tape content: … 𝐴1 𝐴2 𝐴3 𝐴4 𝐴5 𝐴6 …
Tape head
𝐴3 𝐴4
Store tape contents in
two stacks like this: 𝐴2 𝐴5 See Section 8.5.2 of the book for
𝐴1 𝐴6 a much more detailed proof.
Jim Anderson (modified by Nathan Otterness) 71
Restricted TMs: Counter Machines
A counter machine can be thought of as a multi-stack PDA,
with only two stack symbols and some restrictions:
❖ 𝑍0 , serving as the “bottom of stack marker”
❖𝑋
❖ Initially,
only 𝑍0 is on each stack.
❖ Only 𝑋’s can be pushed or popped from the stack.
Essentially, each stack is a counter. The PDA can only do
three things with each stack:
❖ Increase the “counter” by pushing more 𝑋’s.
❖ Decrement the “counter” by popping a single 𝑋.
❖ Check if the “counter” is 0 by seeing if 𝑍0 is on top of the
stack.
Jim Anderson (modified by Nathan Otterness) 72
Restricted TMs: Three-Counter Machines