58
Turing Machines Part One

Turing Machines - Stanford Universityweb.stanford.edu/class/archive/cs/cs103/cs103.1184/...The Turing Machine A Turing machine consists of three parts: A finite-sttite iconntont that

  • Upload
    others

  • View
    3

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Turing Machines - Stanford Universityweb.stanford.edu/class/archive/cs/cs103/cs103.1184/...The Turing Machine A Turing machine consists of three parts: A finite-sttite iconntont that

Turing MachinesPart One

Page 2: Turing Machines - Stanford Universityweb.stanford.edu/class/archive/cs/cs103/cs103.1184/...The Turing Machine A Turing machine consists of three parts: A finite-sttite iconntont that

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 - Stanford Universityweb.stanford.edu/class/archive/cs/cs103/cs103.1184/...The Turing Machine A Turing machine consists of three parts: A finite-sttite iconntont that

What problems can we solve with a computer?

Page 4: Turing Machines - Stanford Universityweb.stanford.edu/class/archive/cs/cs103/cs103.1184/...The Turing Machine A Turing machine consists of three parts: A finite-sttite iconntont that

RegularLanguages CFLs

All Languages

Languages recognizable by

any feasible computing machine

Page 5: Turing Machines - Stanford Universityweb.stanford.edu/class/archive/cs/cs103/cs103.1184/...The Turing Machine A Turing machine consists of three parts: A finite-sttite iconntont that

All Languages

That same drawing, to scale.

Page 6: Turing Machines - Stanford Universityweb.stanford.edu/class/archive/cs/cs103/cs103.1184/...The Turing Machine A Turing machine consists of three parts: A finite-sttite iconntont that

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

fnitely many states but unbounded memory?

Page 7: Turing Machines - Stanford Universityweb.stanford.edu/class/archive/cs/cs103/cs103.1184/...The Turing Machine A Turing machine consists of three parts: A finite-sttite iconntont that

A Brief History Lesson

Page 8: Turing Machines - Stanford Universityweb.stanford.edu/class/archive/cs/cs103/cs103.1184/...The Turing Machine A Turing machine consists of three parts: A finite-sttite iconntont that

A Simple Turing Machine

q0

qacc

qrej

q1

start

a → , R☐

a → , R☐

→ ☐ ☐, R

→ ☐ ☐, R

qacc

qrej

This is the Turing machine’sfinite isttite iconntont. It issues

commands that drive the operation of the machine.

This is the Turing machine’sfinite isttite iconntont. It issues

commands that drive the operation of the machine.

Page 9: Turing Machines - Stanford Universityweb.stanford.edu/class/archive/cs/cs103/cs103.1184/...The Turing Machine A Turing machine consists of three parts: A finite-sttite iconntont that

A Simple Turing Machine

q0

qacc

qrej

q1

start

a → , R☐

a → , R☐

→ ☐ ☐, R

→ ☐ ☐, R

qacc

qrej

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

stymbnt. Initially, all (infnitely many) tape symbols are blank.

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

stymbnt. Initially, all (infnitely many) tape symbols are blank.

… …

Page 10: Turing Machines - Stanford Universityweb.stanford.edu/class/archive/cs/cs103/cs103.1184/...The Turing Machine A Turing machine consists of three parts: A finite-sttite iconntont that

A Simple Turing Machine

q0

qacc

qrej

q1

start

a → , R☐

a → , R☐

→ ☐ ☐, R

→ ☐ ☐, R

qacc

qrej

The machine is started with the input isttoing written somewhere

on the tape. The tipe iheid initially points to the frst symbol

of the input string.

The machine is started with the input isttoing written somewhere

on the tape. The tipe iheid initially points to the frst symbol

of the input string.

a a a a… …

Page 11: Turing Machines - Stanford Universityweb.stanford.edu/class/archive/cs/cs103/cs103.1184/...The Turing Machine A Turing machine consists of three parts: A finite-sttite iconntont that

A Simple Turing Machine

q0

qacc

qrej

q1

start

a → , R☐

a → , R☐

→ ☐ ☐, R

→ ☐ ☐, R

qacc

qrej

a a a a… …

q0

Like DFAs and NFAs, TMs begin execution in their

sttiot isttite.

Like DFAs and NFAs, TMs begin execution in their

sttiot isttite.

Page 12: Turing Machines - Stanford Universityweb.stanford.edu/class/archive/cs/cs103/cs103.1184/...The Turing Machine A Turing machine consists of three parts: A finite-sttite iconntont that

a a a… …

A Simple Turing Machine

q0

qacc

qrej

q1

start

a → , R☐

a → , R☐

→ ☐ ☐, R

→ ☐ ☐, R

qacc

qrej

a

q0

At each step, the TM only looks at

the symbol immediately under

the tipe iheid.

At each step, the TM only looks at

the symbol immediately under

the tipe iheid.

Page 13: Turing Machines - Stanford Universityweb.stanford.edu/class/archive/cs/cs103/cs103.1184/...The Turing Machine A Turing machine consists of three parts: A finite-sttite iconntont that

a a a… …

A Simple Turing Machine

q0

qacc

qrej

q1

start

a → , R☐ → ☐ ☐, R

qacc

qrej

a

a → , 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 14: Turing Machines - Stanford Universityweb.stanford.edu/class/archive/cs/cs103/cs103.1184/...The Turing Machine A Turing machine consists of three parts: A finite-sttite iconntont that

→ ☐ ☐, R

a a a… …

A Simple Turing Machine

q0

qacc

qrej

q1

start

a → , R☐ → ☐ ☐, R

qacc

qrej

a

a → , R☐

q0

Each transition has the form

oeid i→ iwoite, idio

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

Each transition has the form

oeid i→ iwoite, idio

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

Page 15: Turing Machines - Stanford Universityweb.stanford.edu/class/archive/cs/cs103/cs103.1184/...The Turing Machine A Turing machine consists of three parts: A finite-sttite iconntont that

→ ☐ ☐, R

a a a… …

A Simple Turing Machine

q0

qacc

qrej

q1

start

a → , R☐ → ☐ ☐, R

qacc

qrej

a → , R☐

q1

Each transition has the form

oeid i→ iwoite, idio

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

Each transition has the form

oeid i→ iwoite, idio

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

Page 16: Turing Machines - Stanford Universityweb.stanford.edu/class/archive/cs/cs103/cs103.1184/...The Turing Machine A Turing machine consists of three parts: A finite-sttite iconntont that

… …

→ ☐ ☐, R

A Simple Turing Machine

q0

qacc

qrej

q1

start

qacc

qrej

a → , R☐

→ ☐ ☐, Ra → , 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 17: Turing Machines - Stanford Universityweb.stanford.edu/class/archive/cs/cs103/cs103.1184/...The Turing Machine A Turing machine consists of three parts: A finite-sttite iconntont that

… …

→ ☐ ☐, R

A Simple Turing Machine

q0

qrej

q1

start

qrej

a → , R☐

→ ☐ ☐, Ra → , R☐

This special state is an icocoepting isttite. 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 icocoepting isttite. 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 18: Turing Machines - Stanford Universityweb.stanford.edu/class/archive/cs/cs103/cs103.1184/...The Turing Machine A Turing machine consists of three parts: A finite-sttite iconntont that

… …

→ ☐ ☐, R

A Simple Turing Machine

q0

q1

start

a → , R☐

→ ☐ ☐, Ra → , R☐

qacc

This special state is a ioejecoting isttite. 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 ioejecoting isttite. 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 19: Turing Machines - Stanford Universityweb.stanford.edu/class/archive/cs/cs103/cs103.1184/...The Turing Machine A Turing machine consists of three parts: A finite-sttite iconntont that

… …

→ ☐ ☐, R

A Simple Turing Machine

q0

q1

start

a → , R☐

→ ☐ ☐, Ra → , 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 20: Turing Machines - Stanford Universityweb.stanford.edu/class/archive/cs/cs103/cs103.1184/...The Turing Machine A Turing machine consists of three parts: A finite-sttite iconntont that

The Turing Machine

● A Turing machine consists of three parts:● A finite-sttite iconntont that issues commands,● an infinite itipe for input and scratch space, and● a tipe iheid 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 21: Turing Machines - Stanford Universityweb.stanford.edu/class/archive/cs/cs103/cs103.1184/...The Turing Machine A Turing machine consists of three parts: A finite-sttite iconntont that

Input and Tape Alphabets

● A Turing machine has two alphabets:● An input iitphibet Σ. All input strings are written

in the input alphabet.● A tipe iitphibet Γ, 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 btink istymbnt, denoted . You are guaranteed ∉ Σ.☐ ☐

● At startup, the Turing machine begins with an infnite tape of symbols with the input written at ☐some location. The tape head is positioned at the start of the input.

Page 22: Turing Machines - Stanford Universityweb.stanford.edu/class/archive/cs/cs103/cs103.1184/...The Turing Machine A Turing machine consists of three parts: A finite-sttite iconntont that

Accepting and Rejecting States

● Unlike DFAs, Turing machines do not stop processing the input when they fnish reading it.

● Turing machines decide when (and if!) they will accept or reject their input.

● Turing machines can enter infnite loops and never accept or reject; more on that later...

Page 23: Turing Machines - Stanford Universityweb.stanford.edu/class/archive/cs/cs103/cs103.1184/...The Turing Machine A Turing machine consists of three parts: A finite-sttite iconntont that

→ ☐ ☐, R

Determinism

q0

qrej

q1

start

qrej

a → , R☐

→ ☐ ☐, Ra → , R☐

qacc

● Turing machines are deteoministtico: for every combination of a (non-accepting, non-rejecting) state q and a tape symbol a ∈ Γ, there must be exactly one transition defned 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 24: Turing Machines - Stanford Universityweb.stanford.edu/class/archive/cs/cs103/cs103.1184/...The Turing Machine A Turing machine consists of three parts: A finite-sttite iconntont that

→ ☐ ☐, R

Determinism

q0

q1

start

a → , R☐

a → , R☐

qacc

● Turing machines are deteoministtico: for every combination of a (non-accepting, non-rejecting) state q and a tape symbol a ∈ Γ, there must be exactly one transition defned 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 25: Turing Machines - Stanford Universityweb.stanford.edu/class/archive/cs/cs103/cs103.1184/...The Turing Machine A Turing machine consists of three parts: A finite-sttite iconntont that

q0

q1

start → ☐ ☐, L

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

b → b, L → ☐ ☐, L

qacc

qrej

Answer at PollEv.com/cs103 ortext CS103 to 22333 once to join, then A, B, C, D, or E.

Answer at PollEv.com/cs103 ortext CS103 to 22333 once to join, then A, B, C, D, or E.

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

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

… b b a …A.

…… a a bD.

… a a b …B.

… b b a …C.

E. None of these, or two or more of these.

Page 26: Turing Machines - Stanford Universityweb.stanford.edu/class/archive/cs/cs103/cs103.1184/...The Turing Machine A Turing machine consists of three parts: A finite-sttite iconntont that

q0

q1

start → ☐ ☐, L

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

b → b, L → ☐ ☐, L

qacc

qrej

If M is a Turing machine with input alphabet Σ, then the tinguige inf iM, denoted ℒ(M), is the set

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

If M is a Turing machine with input alphabet Σ, then the tinguige inf iM, denoted ℒ(M), is the set

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

Answer at PollEv.com/cs103 ortext CS103 to 22333 once to join, then A, B, C, or D.

Answer at PollEv.com/cs103 ortext CS103 to 22333 once to join, then A, B, C, or D.

Let M be the above TM, and assume itsinput alphabet is {a, b}. What is (ℒ M)?

A. { w ∈ {a, b}* | w ends in a }B. { w ∈ {a, b}* | w ends in b }C. ØD. None of these, or two or more of these.

Let M be the above TM, and assume itsinput alphabet is {a, b}. What is (ℒ M)?

A. { w ∈ {a, b}* | w ends in a }B. { w ∈ {a, b}* | w ends in b }C. ØD. None of these, or two or more of these.

Page 27: Turing Machines - Stanford Universityweb.stanford.edu/class/archive/cs/cs103/cs103.1184/...The Turing Machine A Turing machine consists of three parts: A finite-sttite iconntont that

q0

q1

start → ☐ ☐, L

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

b → b, L → ☐ ☐, 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 28: Turing Machines - Stanford Universityweb.stanford.edu/class/archive/cs/cs103/cs103.1184/...The Turing Machine A Turing machine consists of three parts: A finite-sttite iconntont that

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 29: Turing Machines - Stanford Universityweb.stanford.edu/class/archive/cs/cs103/cs103.1184/...The Turing Machine A Turing machine consists of three parts: A finite-sttite iconntont that

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 30: Turing Machines - Stanford Universityweb.stanford.edu/class/archive/cs/cs103/cs103.1184/...The Turing Machine A Turing machine consists of three parts: A finite-sttite iconntont that

L = {0n1n | n ∈ ℕ }

0 0 0 1 1 1… …

… …

0 1 0… …

1 1 0 0… …

Page 31: Turing Machines - Stanford Universityweb.stanford.edu/class/archive/cs/cs103/cs103.1184/...The Turing Machine A Turing machine consists of three parts: A finite-sttite iconntont that

A Recursive Approach

● The string ε is in L.● The string 0w1 is in L if w is in L.

● Any string starting with 1 is not in L.

● Any string ending with 0 is not in L.

Page 32: Turing Machines - Stanford Universityweb.stanford.edu/class/archive/cs/cs103/cs103.1184/...The Turing Machine A Turing machine consists of three parts: A finite-sttite iconntont that

Clear a1

Clear a1

Go toend

Checkfor 0

Go toend

Checkfor 0

Go tostartGo tostart

start

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

→ ☐ ☐, L

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

→ ☐ ☐, R

qacc

qacc

→ ☐ ☐, R

qacc

qrej

1 → , R☐

→ ☐ ☐, R0 → 0, R

Page 33: Turing Machines - Stanford Universityweb.stanford.edu/class/archive/cs/cs103/cs103.1184/...The Turing Machine A Turing machine consists of three parts: A finite-sttite iconntont that

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 34: Turing Machines - Stanford Universityweb.stanford.edu/class/archive/cs/cs103/cs103.1184/...The Turing Machine A Turing machine consists of three parts: A finite-sttite iconntont that

0 1 1 0… …1

A Caveat

How do we know that this blank isn't one of

the infnitely many blanks after our input

string?

How do we know that this blank isn't one of

the infnitely many blanks after our input

string?

Page 35: Turing Machines - Stanford Universityweb.stanford.edu/class/archive/cs/cs103/cs103.1184/...The Turing Machine A Turing machine consists of three parts: A finite-sttite iconntont that

One Solution

× 0 0 × 1 1 0… …1

Page 36: Turing Machines - Stanford Universityweb.stanford.edu/class/archive/cs/cs103/cs103.1184/...The Turing Machine A Turing machine consists of three parts: A finite-sttite iconntont that

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 37: Turing Machines - Stanford Universityweb.stanford.edu/class/archive/cs/cs103/cs103.1184/...The Turing Machine A Turing machine consists of three parts: A finite-sttite iconntont that

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

Page 38: Turing Machines - Stanford Universityweb.stanford.edu/class/archive/cs/cs103/cs103.1184/...The Turing Machine A Turing machine consists of three parts: A finite-sttite iconntont that

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 fnite-state control.

● The fnite-state control can only remember one of fnitely many things, but that might be all that you need!

Page 39: Turing Machines - Stanford Universityweb.stanford.edu/class/archive/cs/cs103/cs103.1184/...The Turing Machine A Turing machine consists of three parts: A finite-sttite iconntont that

Time-Out for Announcements!

Page 40: Turing Machines - Stanford Universityweb.stanford.edu/class/archive/cs/cs103/cs103.1184/...The Turing Machine A Turing machine consists of three parts: A finite-sttite iconntont that

Second Midterm Exam

● You’re done with the second midterm exam! Woohoo!

● We’ll be grading the exam this weekend. Unfortunately, we will not be able to get grades back before Friday.

● Have questions? Feel free to ask in ofice hours or on Piazza!

Page 41: Turing Machines - Stanford Universityweb.stanford.edu/class/archive/cs/cs103/cs103.1184/...The Turing Machine A Turing machine consists of three parts: A finite-sttite iconntont that

Problem Set Seven

● Problem Set Seven is due this Friday at 2:30PM.

● As always, if you have questions, feel free to stop by ofice hours or ask on Piazza!

Page 42: Turing Machines - Stanford Universityweb.stanford.edu/class/archive/cs/cs103/cs103.1184/...The Turing Machine A Turing machine consists of three parts: A finite-sttite iconntont that

Problem Set Six Scores

0 – 26 27 – 30 31 – 34 35 – 38 39 – 42 43 – 46 47 – 50 51 – 54 55 –

75th Percentile: 50 / 56 (89%)50th Percentile: 46 / 56 (82%)25th Percentile: 41 / 56 (73%)

75th Percentile: 50 / 56 (89%)50th Percentile: 46 / 56 (82%)25th Percentile: 41 / 56 (73%)

Page 43: Turing Machines - Stanford Universityweb.stanford.edu/class/archive/cs/cs103/cs103.1184/...The Turing Machine A Turing machine consists of three parts: A finite-sttite iconntont that

Back to CS103!

Page 44: Turing Machines - Stanford Universityweb.stanford.edu/class/archive/cs/cs103/cs103.1184/...The Turing Machine A Turing machine consists of three parts: A finite-sttite iconntont that

Another TM Design

● We just designed a TM for this language over Σ = {0, 1}:

L = { w ∈ Σ* | w has the same number of 0s and 1s }

● Let's do a quick review of how it worked.

Page 45: Turing Machines - Stanford Universityweb.stanford.edu/class/archive/cs/cs103/cs103.1184/...The Turing Machine A Turing machine consists of three parts: A finite-sttite iconntont that

The Solution

× 0 0 × 1 1 0… …1

Page 46: Turing Machines - Stanford Universityweb.stanford.edu/class/archive/cs/cs103/cs103.1184/...The Turing Machine A Turing machine consists of three parts: A finite-sttite iconntont that

A Diferent Idea

Page 47: Turing Machines - Stanford Universityweb.stanford.edu/class/archive/cs/cs103/cs103.1184/...The Turing Machine A Turing machine consists of three parts: A finite-sttite iconntont that

0 0 0 1 1 1 0… …1

A Diferent Strategy

Could we sort the characters of

this string?

Could we sort the characters of

this string?

Page 48: Turing Machines - Stanford Universityweb.stanford.edu/class/archive/cs/cs103/cs103.1184/...The Turing Machine A Turing machine consists of three parts: A finite-sttite iconntont that

…0 0 0 1 1 1… 01

A Diferent Strategy

Observation 1: A string of 0s and 1s is sorted if it matches the regex

0*1*.

Observation 1: A string of 0s and 1s is sorted if it matches the regex

0*1*.

Page 49: Turing Machines - Stanford Universityweb.stanford.edu/class/archive/cs/cs103/cs103.1184/...The Turing Machine A Turing machine consists of three parts: A finite-sttite iconntont that

…0 0 0 1 1 1… 01

A Diferent Strategy

Observation 2: A string of 0s and 1s is not

sorted if it contains 10 as a substring.

Observation 2: A string of 0s and 1s is not

sorted if it contains 10 as a substring.

Page 50: Turing Machines - Stanford Universityweb.stanford.edu/class/archive/cs/cs103/cs103.1184/...The Turing Machine A Turing machine consists of three parts: A finite-sttite iconntont that

0 0 0 1 1 1 1… …0

A Diferent Strategy

Idea: Repeatedly fnd a copy of 10 and replace

it with 01.

Idea: Repeatedly fnd a copy of 10 and replace

it with 01.

Page 51: Turing Machines - Stanford Universityweb.stanford.edu/class/archive/cs/cs103/cs103.1184/...The Turing Machine A Turing machine consists of three parts: A finite-sttite iconntont that

Let's Build It!

Page 52: Turing Machines - Stanford Universityweb.stanford.edu/class/archive/cs/cs103/cs103.1184/...The Turing Machine A Turing machine consists of three parts: A finite-sttite iconntont that

0*0*

0 0 1 1 0 0… …

start

0 → 0, R 0*1*1 → 1, R

1 → 1, R

0 → ?, ?

Answer at PollEv.com/cs103 ortext CS103 to 22333 once to join, then A, B, C, D, or E.

Answer at PollEv.com/cs103 ortext CS103 to 22333 once to join, then A, B, C, D, or E.

Based on we want this TM to do, what should this transition say?

A. 0 → 0, RB. 0 → 1, RC. 0 → 0, LD. 0 → 1, LE. None of these, or two or more of these.

Based on we want this TM to do, what should this transition say?

A. 0 → 0, RB. 0 → 1, RC. 0 → 0, LD. 0 → 1, LE. None of these, or two or more of these.

Page 53: Turing Machines - Stanford Universityweb.stanford.edu/class/archive/cs/cs103/cs103.1184/...The Turing Machine A Turing machine consists of three parts: A finite-sttite iconntont that

0* 0*1*0*

GoHome

Fix01

GoHome

0*1*

Fix01

1 → 1, R

0 0 0 0 1… …

start

0 → 0, R 1 → 1, R

0 → 1, L

1

1 → 0, L0 → 0, L 1 → 1, L

→ ◻ ◻, R

Our ultimate goal here was to sort everything so we could hand it of to the machine to check for 0n1n. Let's rewind the

tape head back to the start.

Our ultimate goal here was to sort everything so we could hand it of to the machine to check for 0n1n. Let's rewind the

tape head back to the start.

Page 54: Turing Machines - Stanford Universityweb.stanford.edu/class/archive/cs/cs103/cs103.1184/...The Turing Machine A Turing machine consists of three parts: A finite-sttite iconntont that

0*1*0* 0*1*0*

GoHome

Fix01

GoHome

Fix01

1 → 1, R

start

0 → 0, R 1 → 1, R

0 → 1, L

1 → 0, L0 → 0, L 1 → 1, L

→ ◻ ◻, R

ToStart

→ ◻ ◻, L

0 → 0, L 1 → 1, L

Start0n1n

→ ◻ ◻, R

This is just a placeholder. Imagine snapping in the entire TM for 0n1n into this diagram, putting the start state in the dashed

area.

This is just a placeholder. Imagine snapping in the entire TM for 0n1n into this diagram, putting the start state in the dashed

area.

Page 55: Turing Machines - Stanford Universityweb.stanford.edu/class/archive/cs/cs103/cs103.1184/...The Turing Machine A Turing machine consists of three parts: A finite-sttite iconntont that

0* 0*1*

Fix01

GoHome

ToStart

Start0n1n

start

1 → 1, R 1 → 1, R0 → 0, R

0 → 1, L

1 → 0, L0 → 0, L 1 → 1, L

→ ◻ ◻, R

→ ◻ ◻, L → ◻ ◻, L

0 → 0, L 1 → 1, L

→ ◻ ◻, R

This TM will sort any sequence of 0s and 1s, but it might take a while.

Fun problem: design a TM that sorts a string of 0s and 1s, but does so while taking

way fewer steps than this machine.

This TM will sort any sequence of 0s and 1s, but it might take a while.

Fun problem: design a TM that sorts a string of 0s and 1s, but does so while taking

way fewer steps than this machine.

Page 56: Turing Machines - Stanford Universityweb.stanford.edu/class/archive/cs/cs103/cs103.1184/...The Turing Machine A Turing machine consists of three parts: A finite-sttite iconntont that

TM Subroutines

● A TM subroutine is a Turing machine that, instead of accepting or rejecting an input, does some sort of processing job.

● TM subroutines let us compose larger TMs out of smaller TMs, just as you'd write a larger program using lots of smaller helper functions.

● Here, we saw a TM subroutine that sorts a sequence of 0s and 1s into ascending order.

Page 57: Turing Machines - Stanford Universityweb.stanford.edu/class/archive/cs/cs103/cs103.1184/...The Turing Machine A Turing machine consists of three parts: A finite-sttite iconntont that

TM Subroutines

● Typically, when a subroutine is done running, you have it enter a state marked “done” with a dashed line around it.

● When we're composing multiple subroutines together – which we'll do in a bit – the idea is that we'll snap in some real state for the “done” state.

Page 58: Turing Machines - Stanford Universityweb.stanford.edu/class/archive/cs/cs103/cs103.1184/...The Turing Machine A Turing machine consists of three parts: A finite-sttite iconntont that

What other subroutines can we make?