+ All Categories
Home > Documents > Turing Machines -...

Turing Machines -...

Date post: 02-Oct-2020
Category:
Upload: others
View: 0 times
Download: 0 times
Share this document with a friend
51
Turing Machines Part One
Transcript
Page 1: Turing Machines - web.stanford.eduweb.stanford.edu/class/archive/cs/cs103/cs103.1202/lectures/20/Small20.pdfModeling This Idea: Turing Machines. A Simple Turing Machine q 0 q acc q

Turing MachinesPart One

Page 2: Turing Machines - web.stanford.eduweb.stanford.edu/class/archive/cs/cs103/cs103.1202/lectures/20/Small20.pdfModeling This Idea: Turing Machines. A Simple Turing Machine q 0 q acc q

Hello Condensed Slide Readers!

Today’s lecture consists almost exclusively of animations of Turing machines and TM constructions. We’ve presented a condensed version here, but we strongly recommend reading the full version of the slides today.

Hope this helps!

-Keith

Hello Condensed Slide Readers!

Today’s lecture consists almost exclusively of animations of Turing machines and TM constructions. We’ve presented a condensed version here, but we strongly recommend reading the full version of the slides today.

Hope this helps!

-Keith

Page 3: Turing Machines - web.stanford.eduweb.stanford.edu/class/archive/cs/cs103/cs103.1202/lectures/20/Small20.pdfModeling This Idea: Turing Machines. A Simple Turing Machine q 0 q acc q

What problems can we solve with a computer?

Page 4: Turing Machines - web.stanford.eduweb.stanford.edu/class/archive/cs/cs103/cs103.1202/lectures/20/Small20.pdfModeling This Idea: Turing Machines. A Simple Turing Machine q 0 q acc q

RegularLanguages CFLs

All Languages

Languages recognizable by

any feasible computing machine

Page 5: Turing Machines - web.stanford.eduweb.stanford.edu/class/archive/cs/cs103/cs103.1202/lectures/20/Small20.pdfModeling This Idea: Turing Machines. A Simple Turing Machine q 0 q acc q

All Languages

That same drawing, to scale.

Page 6: Turing Machines - web.stanford.eduweb.stanford.edu/class/archive/cs/cs103/cs103.1202/lectures/20/Small20.pdfModeling This Idea: Turing Machines. A Simple Turing Machine q 0 q acc q

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?

Page 7: Turing Machines - web.stanford.eduweb.stanford.edu/class/archive/cs/cs103/cs103.1202/lectures/20/Small20.pdfModeling This Idea: Turing Machines. A Simple Turing Machine q 0 q acc q

A Brief History Lesson

Page 8: Turing Machines - web.stanford.eduweb.stanford.edu/class/archive/cs/cs103/cs103.1202/lectures/20/Small20.pdfModeling This Idea: Turing Machines. A Simple Turing Machine q 0 q acc q

8172 8182 5482 9 01413 6295 8535 9 7+9585 4478 4028 8 7

111111

Page 9: Turing Machines - web.stanford.eduweb.stanford.edu/class/archive/cs/cs103/cs103.1202/lectures/20/Small20.pdfModeling This Idea: Turing Machines. A Simple Turing Machine q 0 q acc q

Modeling This Idea: Turing Machines

Page 10: Turing Machines - web.stanford.eduweb.stanford.edu/class/archive/cs/cs103/cs103.1202/lectures/20/Small20.pdfModeling This Idea: Turing Machines. A Simple Turing Machine q 0 q acc q

A Simple Turing Machine

q0

qacc

qrej

q1

start

a → , R☐, R

a → , R☐, R

→ ☐, R ☐, R, R

→ ☐, R ☐, R, R

qacc

qrej

This is the Turing machine’sfinite state control. It issues

commands that drive the operation of the machine.

This is the Turing machine’sfinite state control. It issues

commands that drive the operation of the machine.

Page 11: Turing Machines - web.stanford.eduweb.stanford.edu/class/archive/cs/cs103/cs103.1202/lectures/20/Small20.pdfModeling This Idea: Turing Machines. A Simple Turing Machine q 0 q acc q

A Simple Turing Machine

q0

qacc

qrej

q1

start

a → , R☐, R

a → , R☐, R

→ ☐, R ☐, R, R

→ ☐, R ☐, R, R

qacc

qrej

This is the TM’s infinite tape. Each tape cell holds a tape

symbol. Initially, all (infinitely many) tape symbols are blank.

This is the TM’s infinite tape. Each tape cell holds a tape

symbol. Initially, all (infinitely many) tape symbols are blank.

… …

Page 12: Turing Machines - web.stanford.eduweb.stanford.edu/class/archive/cs/cs103/cs103.1202/lectures/20/Small20.pdfModeling This Idea: Turing Machines. A Simple Turing Machine q 0 q acc q

A Simple Turing Machine

q0

qacc

qrej

q1

start

a → , R☐, R

a → , R☐, R

→ ☐, R ☐, R, R

→ ☐, R ☐, R, R

qacc

qrej

The machine is started with the input string written somewhere

on the tape. The tape head initially points to the first symbol

of the input string.

The machine is started with the input string written somewhere

on the tape. The tape head initially points to the first symbol

of the input string.

a a a a… …

Page 13: Turing Machines - web.stanford.eduweb.stanford.edu/class/archive/cs/cs103/cs103.1202/lectures/20/Small20.pdfModeling This Idea: Turing Machines. A Simple Turing Machine q 0 q acc q

A Simple Turing Machine

q0

qacc

qrej

q1

start

a → , R☐, R

a → , R☐, R

→ ☐, R ☐, R, R

→ ☐, R ☐, R, R

qacc

qrej

a a a a… …

q0

Like DFAs and NFAs, TMs begin execution in their

start state.

Like DFAs and NFAs, TMs begin execution in their

start state.

Page 14: Turing Machines - web.stanford.eduweb.stanford.edu/class/archive/cs/cs103/cs103.1202/lectures/20/Small20.pdfModeling This Idea: Turing Machines. A Simple Turing Machine q 0 q acc q

a a a… …

A Simple Turing Machine

q0

qacc

qrej

q1

start

a → , R☐, R

a → , R☐, R

→ ☐, R ☐, R, R

→ ☐, R ☐, R, R

qacc

qrej

a

q0

At each step, the TM only looks at

the symbol immediately under

the tape head.

At each step, the TM only looks at

the symbol immediately under

the tape head.

Page 15: Turing Machines - web.stanford.eduweb.stanford.edu/class/archive/cs/cs103/cs103.1202/lectures/20/Small20.pdfModeling This Idea: Turing Machines. A Simple Turing Machine q 0 q acc q

a a a… …

A Simple Turing Machine

q0

qacc

qrej

q1

start

a → , R☐, R → ☐, R ☐, R, R

qacc

qrej

a

a → , R☐, R → ☐, R ☐, R, R

q0

These two transitions originate at the current

state. We’re going to choose one of them to

follow.

These two transitions originate at the current

state. We’re going to choose one of them to

follow.

Page 16: Turing Machines - web.stanford.eduweb.stanford.edu/class/archive/cs/cs103/cs103.1202/lectures/20/Small20.pdfModeling This Idea: Turing Machines. A Simple Turing Machine q 0 q acc q

a a a… …

A Simple Turing Machine

q0

qacc

qrej

q1

start

a → , R☐, R → ☐, R ☐, R, R

qacc

qrej

a

a → , R☐, R → ☐, R ☐, R, R

q0

Each transition has the form

read → write, dir

and means “if symbol read is under the tape head, replace it with write and move the tape head in direction dir (L or R). The symbol denotes a blank cell.☐ symbol denotes a blank cell.

Each transition has the form

read → write, dir

and means “if symbol read is under the tape head, replace it with write and move the tape head in direction dir (L or R). The symbol denotes a blank cell.☐ symbol denotes a blank cell.

Page 17: Turing Machines - web.stanford.eduweb.stanford.edu/class/archive/cs/cs103/cs103.1202/lectures/20/Small20.pdfModeling This Idea: Turing Machines. A Simple Turing Machine q 0 q acc q

→ ☐, R ☐, R, R

a a a… …

A Simple Turing Machine

q0

qacc

qrej

q1

start

a → , R☐, R → ☐, R ☐, R, R

qacc

qrej

a → , R☐, R

q1

Each transition has the form

read → write, dir

and means “if symbol read is under the tape head, replace it with write and move the tape head in direction dir (L or R). The symbol denotes a blank cell.☐ symbol denotes a blank cell.

Each transition has the form

read → write, dir

and means “if symbol read is under the tape head, replace it with write and move the tape head in direction dir (L or R). The symbol denotes a blank cell.☐ symbol denotes a blank cell.

Page 18: Turing Machines - web.stanford.eduweb.stanford.edu/class/archive/cs/cs103/cs103.1202/lectures/20/Small20.pdfModeling This Idea: Turing Machines. A Simple Turing Machine q 0 q acc q

→ ☐, R ☐, R, R

a a a… …

A Simple Turing Machine

q0

qacc

qrej

q1

start

a → , R☐, R → ☐, R ☐, R, R

qacc

qrej

a → , R☐, R

q1

Page 19: Turing Machines - web.stanford.eduweb.stanford.edu/class/archive/cs/cs103/cs103.1202/lectures/20/Small20.pdfModeling This Idea: Turing Machines. A Simple Turing Machine q 0 q acc q

… …

→ ☐, R ☐, R, R

A Simple Turing Machine

q0

qacc

qrej

q1

start

qacc

qrej

a → , R☐, R

→ ☐, R ☐, R, Ra → , R☐, R

q0

Unlike a DFA or NFA, a TM doesn’t stop after reading all the input characters. We keep

running until the machine explicitly says to stop.

Unlike a DFA or NFA, a TM doesn’t stop after reading all the input characters. We keep

running until the machine explicitly says to stop.

Page 20: Turing Machines - web.stanford.eduweb.stanford.edu/class/archive/cs/cs103/cs103.1202/lectures/20/Small20.pdfModeling This Idea: Turing Machines. A Simple Turing Machine q 0 q acc q

… …

→ ☐, R ☐, R, R

A Simple Turing Machine

q0

qacc

qrej

q1

start

qacc

qrej

a → , R☐, R

→ ☐, R ☐, R, Ra → , R☐, R

This special state is an accepting state. When a TM enters an accepting state, it immediately stops running and accepts whatever the

original input string was (in this case, aaaa).

This special state is an accepting state. When a TM enters an accepting state, it immediately stops running and accepts whatever the

original input string was (in this case, aaaa).

Page 21: Turing Machines - web.stanford.eduweb.stanford.edu/class/archive/cs/cs103/cs103.1202/lectures/20/Small20.pdfModeling This Idea: Turing Machines. A Simple Turing Machine q 0 q acc q

… …

→ ☐, R ☐, R, R

A Simple Turing Machine

q0

qrej

q1

start

qrej

a → , R☐, R

→ ☐, R ☐, R, Ra → , R☐, R

This special state is an accepting state. When a TM enters an accepting state, it immediately stops running and accepts whatever the

original input string was (in this case, aaaa).

This special state is an accepting state. When a TM enters an accepting state, it immediately stops running and accepts whatever the

original input string was (in this case, aaaa).

qacc

Page 22: Turing Machines - web.stanford.eduweb.stanford.edu/class/archive/cs/cs103/cs103.1202/lectures/20/Small20.pdfModeling This Idea: Turing Machines. A Simple Turing Machine q 0 q acc q

a a a… …a

→ ☐, R ☐, R, R

a

A Simple Turing Machine

q0

qrej

q1

start

qrej

a → , R☐, R

→ ☐, R ☐, R, Ra → , R☐, R

qacc

q0

Page 23: Turing Machines - web.stanford.eduweb.stanford.edu/class/archive/cs/cs103/cs103.1202/lectures/20/Small20.pdfModeling This Idea: Turing Machines. A Simple Turing Machine q 0 q acc q

… …

→ ☐, R ☐, R, R

A Simple Turing Machine

q0

q1

start

a → , R☐, R

→ ☐, R ☐, R, Ra → , R☐, R

qacc

This special state is a rejecting state. When a TM

enters a rejecting state, it immediately stops running and rejects whatever the

original input string was (in this case, aaaaa).

This special state is a rejecting state. When a TM

enters a rejecting state, it immediately stops running and rejects whatever the

original input string was (in this case, aaaaa).

qrej

Page 24: Turing Machines - web.stanford.eduweb.stanford.edu/class/archive/cs/cs103/cs103.1202/lectures/20/Small20.pdfModeling This Idea: Turing Machines. A Simple Turing Machine q 0 q acc q

… …

→ ☐, R ☐, R, R

A Simple Turing Machine

q0

q1

start

a → , R☐, R

→ ☐, R ☐, R, Ra → , R☐, R

qacc

qrej

q0

If the TM is started on the empty string ε, the entire tape is blank and the tape head is positioned at some arbitrary

location on the tape.

If the TM is started on the empty string ε, the entire tape is blank and the tape head is positioned at some arbitrary

location on the tape.

Page 25: Turing Machines - web.stanford.eduweb.stanford.edu/class/archive/cs/cs103/cs103.1202/lectures/20/Small20.pdfModeling This Idea: Turing Machines. A Simple Turing Machine q 0 q acc q

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.

Page 26: Turing Machines - web.stanford.eduweb.stanford.edu/class/archive/cs/cs103/cs103.1202/lectures/20/Small20.pdfModeling This Idea: Turing Machines. A Simple Turing Machine q 0 q acc q

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 ⊊ Γ. 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 ∉ Σ.☐ symbol denotes a blank cell. ☐ symbol denotes a blank cell.

● At startup, the Turing machine begins with an infinite tape of symbols with the input written at ☐ symbol denotes a blank cell.some location. The tape head is positioned at the start of the input.

Page 27: Turing Machines - web.stanford.eduweb.stanford.edu/class/archive/cs/cs103/cs103.1202/lectures/20/Small20.pdfModeling This Idea: Turing Machines. A Simple Turing Machine q 0 q acc q

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...

Page 28: Turing Machines - web.stanford.eduweb.stanford.edu/class/archive/cs/cs103/cs103.1202/lectures/20/Small20.pdfModeling This Idea: Turing Machines. A Simple Turing Machine q 0 q acc q

→ ☐, R ☐, R, R

Determinism

q0

qrej

q1

start

qrej

a → , R☐, R

→ ☐, R ☐, R, Ra → , R☐, R

qacc

● 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.

Page 29: Turing Machines - web.stanford.eduweb.stanford.edu/class/archive/cs/cs103/cs103.1202/lectures/20/Small20.pdfModeling This Idea: Turing Machines. A Simple Turing Machine q 0 q acc q

→ ☐, R ☐, R, R

Determinism

q0

q1

start

a → , R☐, R

a → , R☐, R

qacc

● 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.

This machine is exactly the same as the

previous one.

This machine is exactly the same as the

previous one.

Page 30: Turing Machines - web.stanford.eduweb.stanford.edu/class/archive/cs/cs103/cs103.1202/lectures/20/Small20.pdfModeling This Idea: Turing Machines. A Simple Turing Machine q 0 q acc q

q0

q1

start → ☐, R ☐, R, L

a → b, Rb → a, R a → a, L

b → b, L → ☐, R ☐, R, L

qacc

qrej

Run the TM shown above on the input string bba.What will the tape look like when the TM finishes running?

Run the TM shown above on the input string bba.What will the tape look like when the TM finishes running?

Page 31: Turing Machines - web.stanford.eduweb.stanford.edu/class/archive/cs/cs103/cs103.1202/lectures/20/Small20.pdfModeling This Idea: Turing Machines. A Simple Turing Machine q 0 q acc q

q0

q1

start → ☐, R ☐, R, L

a → b, Rb → a, R a → a, L

b → b, L → ☐, R ☐, R, L

qacc

qrej

a b… …a

Page 32: Turing Machines - web.stanford.eduweb.stanford.edu/class/archive/cs/cs103/cs103.1202/lectures/20/Small20.pdfModeling This Idea: Turing Machines. A Simple Turing Machine q 0 q acc q

q0

q1

start → ☐, R ☐, R, L

a → b, Rb → a, R a → a, L

b → b, L → ☐, R ☐, R, L

qacc

qrej

If M is a Turing machine with input alphabet Σ, then the language of M, denoted ℒ(M), is the set

ℒ(M) = { w ∈ Σ* | M accepts w }

If M is a Turing machine with input alphabet Σ, then the language of M, denoted ℒ(M), is the set

ℒ(M) = { w ∈ Σ* | M accepts w }

What is (ℒ M), where M is the above TM?

What is (ℒ M), where M is the above TM?

Page 33: Turing Machines - web.stanford.eduweb.stanford.edu/class/archive/cs/cs103/cs103.1202/lectures/20/Small20.pdfModeling This Idea: Turing Machines. A Simple Turing Machine q 0 q acc q

q0

q1

start → ☐, R ☐, R, L

a → b, Rb → a, R a → a, L

b → b, L → ☐, R ☐, R, L

qacc

qrej

b a… …b

Although the tape ends with bba written on it, the original input string was aab. This shows that the TM

accepts aab, not bba.

So (ℒ M) = { w ∈ {a, b}* | w ends in b }

Although the tape ends with bba written on it, the original input string was aab. This shows that the TM

accepts aab, not bba.

So (ℒ M) = { w ∈ {a, b}* | w ends in b }

Page 34: Turing Machines - web.stanford.eduweb.stanford.edu/class/archive/cs/cs103/cs103.1202/lectures/20/Small20.pdfModeling This Idea: Turing Machines. A Simple Turing Machine q 0 q acc q

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.

Page 35: Turing Machines - web.stanford.eduweb.stanford.edu/class/archive/cs/cs103/cs103.1202/lectures/20/Small20.pdfModeling This Idea: Turing Machines. A Simple Turing Machine q 0 q acc q

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?

Page 36: Turing Machines - web.stanford.eduweb.stanford.edu/class/archive/cs/cs103/cs103.1202/lectures/20/Small20.pdfModeling This Idea: Turing Machines. A Simple Turing Machine q 0 q acc q

L = {0n1n | n ∈ ℕ }

0 0 0 1 1 1… …

… …

0 1 0… …

1 1 0 0… …

Page 37: Turing Machines - web.stanford.eduweb.stanford.edu/class/archive/cs/cs103/cs103.1202/lectures/20/Small20.pdfModeling This Idea: Turing Machines. A Simple Turing Machine q 0 q acc q

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.

Page 38: Turing Machines - web.stanford.eduweb.stanford.edu/class/archive/cs/cs103/cs103.1202/lectures/20/Small20.pdfModeling This Idea: Turing Machines. A Simple Turing Machine q 0 q acc q

Clear a1

Clear a1

Go toend

Checkfor 0

Go toend

Checkfor 0

Go tostartGo tostart

start

0 → , R☐, R 0 → 0, R 1 → 1, R

→ ☐, R ☐, R, L

1 → , L☐, R0 → 0, L 1 → 1, L

→ ☐, R ☐, R, R

qacc

qacc

→ ☐, R ☐, R, R

qacc

qrej

1 → , R☐, R

→ ☐, R ☐, R, R0 → 0, R

Page 39: Turing Machines - web.stanford.eduweb.stanford.edu/class/archive/cs/cs103/cs103.1202/lectures/20/Small20.pdfModeling This Idea: Turing Machines. A Simple Turing Machine q 0 q acc q

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?

Page 40: Turing Machines - web.stanford.eduweb.stanford.edu/class/archive/cs/cs103/cs103.1202/lectures/20/Small20.pdfModeling This Idea: Turing Machines. A Simple Turing Machine q 0 q acc q

Find0/1Find0/1

Gohome

→ ☐ → ☐ ☐ → ☐, R Gohome

Find0Find0

Find1

start

Find1

0 → ×, R

0 → 0, R× → ×, R

1 → ×, L

0 → 0, L 1 → 1, L × → ×, L

× → ×, R

1 → ×, R

1 → 1, R× → ×, R

0 → ×, L

Accept!

→ ◻ → ◻ ◻ → ◻, R

Remember that all missing transitions

implicitly reject.

Remember that all missing transitions

implicitly reject.

Page 41: Turing Machines - web.stanford.eduweb.stanford.edu/class/archive/cs/cs103/cs103.1202/lectures/20/Small20.pdfModeling This Idea: Turing Machines. A Simple Turing Machine q 0 q acc q

Time-Out for Announcements!

Page 42: Turing Machines - web.stanford.eduweb.stanford.edu/class/archive/cs/cs103/cs103.1202/lectures/20/Small20.pdfModeling This Idea: Turing Machines. A Simple Turing Machine q 0 q acc q

Preliminary Exam Solutions

● We’ve posted a set of preliminary exam solutions to the course website.

● It contains– solutions to all the exam questions,or at least, one set of

solutions;– a recap about how to compute your raw score so far and

how to extrapolate;– grade cutoffs from past quarters; and– statistics on all the problem sets.

● Remember: 44% of your grade is still completely under your control at this point. Think about your ability to grow, not where you currently stand.

Page 43: Turing Machines - web.stanford.eduweb.stanford.edu/class/archive/cs/cs103/cs103.1202/lectures/20/Small20.pdfModeling This Idea: Turing Machines. A Simple Turing Machine q 0 q acc q

Problem Set Seven

● Problem Set Seven is due this Friday at 2:30PM.– As always, if you have questions, feel free

to stop by office hours or ask on Piazza!● We’re working to get Problem Set Six

returned ASAP; stay tuned!

Page 44: Turing Machines - web.stanford.eduweb.stanford.edu/class/archive/cs/cs103/cs103.1202/lectures/20/Small20.pdfModeling This Idea: Turing Machines. A Simple Turing Machine q 0 q acc q

Your Questions

Page 45: Turing Machines - web.stanford.eduweb.stanford.edu/class/archive/cs/cs103/cs103.1202/lectures/20/Small20.pdfModeling This Idea: Turing Machines. A Simple Turing Machine q 0 q acc q

“Hey Keith, do you ever think, "You know what? These guys took a midterm this week and did great. How about no homework!"

Wouldn't that be nice.”

I’m aware that there’s a lot going on in the quarter right now, and I wanted to thank all of you for putting in the effort and rising to the occasion. Hang in there – we’re really impressed by how far you’ve all come!

I’m aware that there’s a lot going on in the quarter right now, and I wanted to thank all of you for putting in the effort and rising to the occasion. Hang in there – we’re really impressed by how far you’ve all come!

Page 46: Turing Machines - web.stanford.eduweb.stanford.edu/class/archive/cs/cs103/cs103.1202/lectures/20/Small20.pdfModeling This Idea: Turing Machines. A Simple Turing Machine q 0 q acc q

Back to CS103!

Page 47: Turing Machines - web.stanford.eduweb.stanford.edu/class/archive/cs/cs103/cs103.1202/lectures/20/Small20.pdfModeling This Idea: Turing Machines. A Simple Turing Machine q 0 q acc q

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?

Page 48: Turing Machines - web.stanford.eduweb.stanford.edu/class/archive/cs/cs103/cs103.1202/lectures/20/Small20.pdfModeling This Idea: Turing Machines. A Simple Turing Machine q 0 q acc q

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, if n = 0, then m is not a multiple of n.– Otherwise, m is a multiple of n iff m ≥ n andm – n is a multiple of n.

● Idea: Repeatedly subtract n from m until m becomes zero (good!) or drops below zero (bad!)

Page 49: Turing Machines - web.stanford.eduweb.stanford.edu/class/archive/cs/cs103/cs103.1202/lectures/20/Small20.pdfModeling This Idea: Turing Machines. A Simple Turing Machine q 0 q acc q

EdgeCase

Checkm≟0

Checkm≟0

Unmark

Next0

Unmark

Next0

BackhomeBackhome

Crossoff 1

To End

Crossoff 1

To End

BackhomeBackhome

→ ◻ → ◻ ◻ → ◻, R

0 → 0, R 1 → 1, L

0 → 0, L

→ ☐ → ☐ ☐ → ☐, R 0 → ×, R

× → ×, R0 → 0, R1 → 1, R

→ ◻ → ◻ ◻ → ◻, L

1 → , L☐ → ☐× → ×, L 0 → 0, L 1 → 1, L

× → ×, R → ☐ → ☐ ☐ → ☐, L

1 → 1, L

× → 0, L → ☐ → ☐ ☐ → ☐, R

Accept!

→ ◻ → ◻ ◻ → ◻, R

EdgeCase

0 → 0, R

start

→ ☐ → ☐ ☐ → ☐, R

Page 50: Turing Machines - web.stanford.eduweb.stanford.edu/class/archive/cs/cs103/cs103.1202/lectures/20/Small20.pdfModeling This Idea: Turing Machines. A Simple Turing Machine q 0 q acc q

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.

Page 51: Turing Machines - web.stanford.eduweb.stanford.edu/class/archive/cs/cs103/cs103.1202/lectures/20/Small20.pdfModeling This Idea: Turing Machines. A Simple Turing Machine q 0 q acc q

Next Time

● TM Subroutines– Combining multiple TMs together!

● The Church-Turing Thesis– Just how powerful are Turing machines?

● R and RE Languages– What does it mean to solve a problem?


Recommended