Turing Machines: Part One
Turing Machines: Part One
Part One
Hello
Hello Condensed
Condensed Slide
Slide Readers!
Readers!
Today’s
Today’s lecture
lecture consists
consists almost
almost exclusively
exclusively
of
of animations
animations of
of Turing
Turing machines
machines and
and TMTM
constructions.
constructions. We’ve
We’ve presented
presented aa condensed
condensed
version
version here,
here, but
but we
we strongly
strongly recommend
recommend
reading
reading the
the full
full version
version of
of the
the slides
slides
today.
today.
Hope
Hope this
this helps!
helps!
-Keith
-Keith
What problems can we solve with a computer?
Languages
recognizable by
Regular
Languages
CFLs any feasible
computing
machine
All Languages
That same drawing, to scale.
All Languages
The Problem
●
Finite automata accept precisely the
regular languages.
●
We may need unbounded memory to
recognize context-free languages.
●
e.g. { anbn | n ∈ ℕ } requires unbounded
counting.
●
How do we build an automaton with
finitely many states but unbounded
memory?
A Brief History Lesson
A Simple Turing Machine
qacc
☐ → ☐, R
a → ☐, R
start
q0 q1
a → ☐, R
☐ → ☐, R
This
Thisisisthe
theTuring
Turingmachine’s
machine’s
finite state control.
finite state control.ItItissues
issues
qrej
commands that drive
commands that drive thethe
operation
operationofofthe
themachine.
machine.
A Simple Turing Machine
qacc
☐ → ☐, R
a → ☐, R
start
q0 q1
a → ☐, R
☐ → ☐, R
This
Thisisisthe TM’sinfinite
theTM’s infinitetape.
tape.
Each tape cell holds a
Each tape cell holds a tape tape qrej
symbol.
symbol.Initially,
Initially,all
all(infinitely
(infinitely
many)
many) tape symbols areblank.
tape symbols are blank.
… …
A Simple Turing Machine
qacc
☐ → ☐, R
a → ☐, R
start
q0 q1
a → ☐, R
The
Themachine
machineisisstarted
startedwith
withthe
the ☐ → ☐, R
input
inputstring
stringwritten
writtensomewhere
somewhere
on the tape. The tapehead
on the tape. The tape head
initially points to the first symbol
initially points to the first symbol qrej
ofofthe
theinput
inputstring.
string.
… a a a a …
A Simple Turing Machine
qacc
☐ → ☐, R
a → ☐, R
start
q0 q1
a → ☐, R
Like ☐ → ☐, R
LikeDFAs
DFAsandand
NFAs, TMs begin
NFAs, TMs begin
execution
executioninintheir
start
their qrej
startstate.
state.
… a a a a …
A Simple Turing Machine
qacc
☐ → ☐, R
a → ☐, R
start
q0 q1
a → ☐, R
At ☐ → ☐, R
Ateach
eachstep,
step,the
the
TM
TM only looksat
only looks at
the symbol
the symbol
immediately qrej
immediatelyunder
under
the tape head.
the tape head.
… a a a a …
A Simple Turing Machine
qacc
☐ → ☐, R
a → ☐, R
start
q0 q1
a → ☐, R
These ☐ → ☐, R
Thesetwo
twotransitions
transitions
originate
originate at thecurrent
at the current
state.
state. We’re goingto
We’re going
choose
to qrej
chooseoneoneofofthem
themtoto
follow.
follow.
… a a a a …
A Simple Turing Machine
qacc
☐ → ☐, R
a → ☐, R
start
q0 q1
a → ☐, R
Each
Eachtransition
transitionhas
hasthe form☐
theform → ☐, R
read
read→→write,
write,dir
dir
and
andmeans
means“if symbolread
“ifsymbol readisisunder
underthe
thetape
tapehead,qreplace
head, withwrite
replaceititwith
rej writeand
and
move tape head in direction dir
move the tape head in direction dir (L or R). The ☐ symbol denotes a blankcell.
the (L or R). The ☐ symbol denotes a blank cell.
… a a a a …
A Simple Turing Machine
qacc
☐ → ☐, R
a → ☐, R
start
q0 q1
a → ☐, R
Each
Eachtransition
transitionhas
hasthe form☐
theform → ☐, R
read
read→→write,
write,dir
dir
and
andmeans
means“if symbolread
“ifsymbol readisisunder
underthe
thetape
tapehead,qreplace
head, withwrite
replaceititwith
rej writeand
and
move tape head in direction dir
move the tape head in direction dir (L or R). The ☐ symbol denotes a blankcell.
the (L or R). The ☐ symbol denotes a blank cell.
… a a a …
A Simple Turing Machine
qacc
☐ → ☐, R
a → ☐, R
start
q0 q1
a → ☐, R
☐ → ☐, R
Unlike
UnlikeaaDFA
DFAor orNFA,
NFA,aaTMTM
doesn’t
doesn’t stop after readingall
stop after reading all
the input characters. We keep
the input characters. We keep
qrej
running
runninguntil
untilthe
themachine
machine
explicitly
explicitly says tostop.
says to stop.
… …
A Simple Turing Machine
qacc
☐ → ☐, R
a → ☐, R
start
q0 q1
This a → ☐, R
Thisspecial
specialstate
stateisisan
an ☐ → ☐, R
accepting state.
accepting state. When aTM
When a TM
enters
enters an accepting state,itit
an accepting state,
immediately
immediatelystops
stopsrunning
running qrej
and
and accepts whateverthe
accepts whatever the
original
original input string was(in
input string was (in
this case, aaaa).
this case, aaaa).
… …
A Simple Turing Machine
qacc
☐ → ☐, R
a → ☐, R
start
q0 q1
a → ☐, R
This
Thisspecial
specialstate
stateisisaa ☐ → ☐, R
rejecting
rejectingstate.
state.When
WhenaaTM TM
enters a rejecting state,
enters a rejecting state, itit
immediately
immediatelystops
stopsrunning
running qrej
and
and rejects whateverthe
rejects whatever the
original
original input string was(in
input string was (in
this case, aaaaa).
this case, aaaaa).
… …
A Simple Turing Machine
qacc
☐ → ☐, R
a → ☐, R
start
q0 q1
IfIfthe
theTM
TMisisstarted
startedon
onthe a → ☐, R
empty
the ☐ → ☐, R
empty string ε, the entiretape
string ε, the entire tape
isisblank
blank and the tape headisis
and the tape head
positioned
positionedat atsome
somearbitrary
location on the
arbitrary
tape.
qrej
location on the tape.
… …
The Turing Machine
●
A Turing machine consists of three parts:
●
A finite-state control that issues commands,
●
an infinite tape for input and scratch space, and
●
a tape head that can read and write a single tape
cell.
●
At each step, the Turing machine
●
writes a symbol to the tape cell under the tape
head,
●
changes state, and
●
moves the tape head to the left or to the right.
Input and Tape Alphabets
●
A Turing machine has two alphabets:
●
An input alphabet Σ. All input strings are written
in the input alphabet.
●
A tape alphabet Γ, where Σ ⊊ Γ. The tape alphabet
contains all symbols that can be written onto the
tape.
●
The tape alphabet Γ can contain any number of
symbols, but always contains at least one blank
symbol, denoted ☐. You are guaranteed ☐ ∉ Σ.
●
At startup, the Turing machine begins with an
infinite tape of ☐ symbols with the input written at
some location. The tape head is positioned at the
start of the input.
Accepting and Rejecting States
●
Unlike DFAs, Turing machines do not
stop processing the input when they
finish reading it.
●
Turing machines decide when (and if!)
they will accept or reject their input.
●
Turing machines can enter infinite loops
and never accept or reject; more on that
later...
Determinism
●
Turing machines are deterministic: for every combination
of a (non-accepting, non-rejecting) state q and a tape
symbol a ∈ Γ, there must be exactly one transition defined
for that combination of q and a.
●
Any transitions that are missing implicitly go straight to a
rejecting state. We’ll use this later to simplify our designs.
qacc
☐ → ☐, R a → ☐, R
This
Thismachine
machineisis
start
q0 q1 exactly
exactlythe
the
same as the
same as the
previous
previousone.
a → ☐, R one.
Designing Turing Machines
●
Despite their simplicity, Turing machines
are very powerful computing devices.
●
Today's lecture explores how to design
Turing machines for various languages.
Designing Turing Machines
● Let Σ = {0, 1} and consider the language
L = {0n1n | n ∈ ℕ }.
●
We know that L is context-free.
●
How might we build a Turing machine
for it?
L = {0n1n | n ∈ ℕ }
… 0 0 0 1 1 1 …
… …
… 0 1 0 …
… 1 1 0 0 …
A Recursive Approach
●
The string ε is in L.
● The string 0w1 is in L iff w is in L.
● Any string starting with 1 is not in L.
● Any string ending with 0 is not in L.
☐ → ☐, R
0 → 0, R
0 → 0, L Go to 1 → ☐, L Clear a
1 → 1, L start 1
st ☐ → ☐, R ☐ → ☐, L
a rt
1 → ☐, R Check 0 → ☐, R Go to 0 → 0, R
qqacc
rej for 0 end 1 → 1, R
☐ → ☐, R
qacc
Another TM Design
● We've designed a TM for {0n1n | n ∈ ℕ}.
● Consider this language over Σ = {0, 1}:
L = { w ∈ Σ* | w has the same number
of 0s and 1s }
●
This language is also not regular, but it
is context-free.
●
How might we design a TM for it?
1 → 1, R
× → ×, R
1 → ×, R Find
0 → ×, L
st 0
a rt
0 → 0, L
Find ☐ → ☐, R Go
× → ×, R 0/1 home 1 → 1, L
× → ×, L
0 → ×, R 1 → ×, L
◻ → ◻, R Find
1
Accept! 0 → 0, R
× → ×, R
Remember
Remember that that all
all
missing
missing transitions
transitions
implicitly
implicitly reject.
reject.
Constant Storage
●
Sometimes, a TM needs to remember some
additional information that can't be put on
the tape.
●
In this case, you can use similar
techniques from DFAs and introduce extra
states into the TM's finite-state control.
●
The finite-state control can only remember
one of finitely many things, but that might
be all that you need!
Time-Out for Announcements!
Problem Set Five
●
Problem Set Five has been graded.
Here’s the score distribution:
●
(Nice job, everyone!)
●
As always, feel free to reach out to us if
you have any questions!
Problem Sets
●
Problem Set Six was due at the start of class
today.
– You can use late days to extend the deadline up
through Monday, but be careful about doing this
given that the midterm is on Tuesday.
●
Problem Set Seven goes out today. It’s due
next Friday at the start of class.
– Play around with regular expressions, properties
of regular languages, the limits of regular
languages, and the Myhill-Nerode theorem!
Midterm Exam Logistics
●
The second midterm exam is next Tuesday, May 23rd, from
7:00PM – 10:00PM. Locations are divvied up by last
(family) name:
– Abb – Pag: Go to Hewlett 200.
– Par – Tak: Go to Sapp 114.
– Tan – Val: Go to Hewlett 101.
– Var – Yim: Go to Hewlett 102.
– You – Zuc: Go to Hewlett 103.
●
You’re responsible for Lectures 00 – 13 and topics covered
in PS1 – PS5. Later lectures and problem sets won’t be
tested. The focus is on PS3 – PS5 and Lectures 06 – 13.
●
The exam is closed-book, closed-computer, and limited-note.
You can bring a double-sided, 8.5” × 11” sheet of notes with
you to the exam, decorated however you’d like.
Your Questions
“What is your favourite mathematical
theorem? Walk us through a proof.”
One
One of of my
my favorite
favorite theorems
theorems isis aa tiny
tiny
little
little one
one that
that II like
like not
not because
because it’s
it’s
super
super deep,
deep, but
but because
because the
the setup
setup isis
just
just so
so beautiful.
beautiful. Let
Let me
me show
show you!
you!
“Recommendations for thought-provoking
short stories and books, or just stories you
have heard that made you challenge your
existence / perception of reality?”
I’ve
I’ve mentioned
mentioned “Before
“Before thethe Law”
Law” on
on one
one ofof the
the
practice
practice exams
exams and
and highly
highly recommend
recommend it.
it. It’s
It’s aa great
great
short
short read
read to
to talk
talk about.
about.
Other
Other ones:
ones: “Scott
“Scott and
and Scurvy,”
Scurvy,” “Story
“Story of
of Your
Your Life,”
Life,”
“Post-Operative
“Post-Operative Check,”
Check,” and
and the
the Pulitzer-Prize
Pulitzer-Prize winning
winning
article
article “The
“The Fighter”
Fighter” from
from last
last year.
year.
Back to CS103!
Another TM Design
●
Consider the following language over
Σ = {0, 1}:
L = {0n1m | n, m ∈ ℕ and
m is a multiple of n }
●
Is this language regular?
●
How might we design a TM for this
language?
An Observation
●
We can recursively describe when one
number m is a multiple of n:
– If m = 0, then m is a multiple of n.
– Otherwise, m is a multiple of n iff m – n is a
multiple of n.
●
Idea: Repeatedly subtract n from m
until m becomes zero (good!) or drops
below zero (bad!)
st
a rt
☐ → ☐, R
Unmark × → 0, L
Edge
Case
× → ×, R
0 → 0, R
☐ → ☐, L
0 → 0, R 1 → 1, R
1 → 1, L × → ×, R
◻ → ◻, R 0 → 0, L ◻ → ◻, R ◻ → ◻, L
☐ → ☐, R × → ×, L
Back 1 → ☐, L Cross
Accept! 0 → 0, L home off 1
1 → 1, L
… 1 1 …
Concepts from Today
●
Turing machines are a generalization of finite
automata equipped with an infinite tape.
●
It's often helpful to think recursively when
designing Turing machines.
●
It's often helpful to introduce new symbols
into the tape alphabet.
●
Watch for edge cases that might lead to
infinite loops – though we'll say more about
that later on.
Next Time
●
TM Subroutines
– Combining multiple TMs together!
●
The Church-Turing Thesis
– Just how powerful are Turing machines?