Mihir Bellare, UCSD CSE 200: Computability and Complexity 1
Turing machines Turing Machines (TMs), configurations, yield relation - - PowerPoint PPT Presentation
Turing machines Turing Machines (TMs), configurations, yield relation - - PowerPoint PPT Presentation
Turing machines Turing Machines (TMs), configurations, yield relation Decidable, recognizable and co-recognizable languages Computable Functions TM Example Reference: Sipser, Chapter 3 Mihir Bellare, UCSD 1 CSE 200: Computability and Complexity
Mihir Bellare, UCSD CSE 200: Computability and Complexity 2
You are probably familiar with several programming languages Examples: Python, JavaScript, Java, C#, C, C++, Go, Rust, Assembly, machine code, … Turing Machines (TMs) are just a new programming language
Why do we need yet another programming language?
TMs are a very LOW LEVEL programming language TMs can be precisely and easily mathematically formalized
few instructions, few features
TMs make it easier to pose and answer questions ABOUT programs.
+
Hard to write programs!
+
Mihir Bellare, UCSD CSE 200: Computability and Complexity 3
You are probably familiar with several programming languages Examples: Python, JavaScript, Java, C#, C, C++, Go, Rust, Assembly, machine code, … Turing Machines (TMs) are just a new programming language
Why do we need yet another programming language?
TMs are a very LOW LEVEL programming language TMs can be precisely and easily mathematically formalized
few instructions, few features
TMs make it easier to pose and answer questions ABOUT programs.
+
We will write our programs in high-level pseudo-code, invoking Church’s Thesis to say this can be turned into a TM
+ +
Mihir Bellare, UCSD CSE 200: Computability and Complexity 4
control q
current state
1 1 1 1 ⊔ ⊔
infinite tape
. . .
head moves across tape blank symbol
01101q10
configuration Turing machine schematic
In one move, the machine: — Reads symbol in square under tape head — Writes, replacing with some symbol — Moves the tape head one step either left or right — Transitions to another state
σ σ τ p
At any point, the machine — Is in some state , and — Its tape head is at some square
q
The choice of — What symbol to write — the state to transition into — In what direction to move is made as a function of , in a way specified by the transition function of a particular machine.
τ p q, σ δ
The machine begins in a special start state . It halts, either accepting or rejecting, by entering special associated states . This is the only way to halt.
q0 qa, qr
Mihir Bellare, UCSD CSE 200: Computability and Complexity 5
Formal definition of a Turing machine
A Turing machine (TM) is a 7-tuple , where are finite sets and 1. is the set of states, with 2. is the input alphabet, with 3. is the tape alphabet, with 4. is the transition function 5. is the start state 6. is the accept state 7. is the reject state
M = (Q, Σ, Γ, δ, q0, qa, qr) Q, Σ, Γ Q Q ∩ Γ = ∅ Σ ⊔ ∉ Σ Γ Σ ∪ { ⊔ } ⊆ Γ δ : Q × Γ → Q × Γ × {L, R} q0 ∈ Q qa ∈ Q qr ∈ Q∖{qa}
Mihir Bellare, UCSD CSE 200: Computability and Complexity 6
Parse as For do ; While do ; ; If then else While do ; Return
M (Q, Σ, Γ, δ, q0, qa, qr) q ← q0 j = 1,…, |w| T[j] ← w[j] T[|w| + 1] ← ⊔ i ← 1 (q ∉ {qa, qr}) (p, τ, D) ← δ(q, T[i]) T[i] ← τ q ← p (D = L) i ← max(1,i − 1) i ← i + 1 j ← 1 (T[j] ≠ ⊔ ) y[j] ← T[j] j ← j + 1 y
A simple way to describe how a TM executes is by writing code that runs it The following code executes TM
- n input
:
M = (Q, Σ, Γ, δ, q0, qa, qr) w ∈ Σ*
Array stores the content of the tape Variable stores the current state Variable stores the current position of the tape head
T q i
Copy input into first part of tape array
w T
Loop until current state is
- r
qa qr
Write to tape and update current state Move tape head We get here only if the computation halted In that case, we return the output of the computation
y
EXEC( )
M, w
Later, we will view this as a universal TM, writing for the above EXEC( ) For now, it is a way to describe/understand the semantics of TM execution
U(⟨M, w⟩) M, w
Mihir Bellare, UCSD CSE 200: Computability and Complexity 7
For and , we write for the configuration of in which — tape contents is — current state is — tape head is scanning first symbol of
u, v ∈ Γ* q ∈ Q uqv M uv q v
Formal definition of a Turing machine
A Turing machine (TM) is a 7-tuple , where are finite sets and 1. is the set of states, with 2. is the input alphabet, with 3. is the tape alphabet, with 4. is the transition function 5. is the start state 6. is the accept state 7. is the reject state
M = (Q, Σ, Γ, δ, q0, qa, qr) Q, Σ, Γ Q Q ∩ Γ = ∅ Σ ⊔ ∉ Σ Γ Σ ∪ { ⊔ } ⊆ Γ δ : Q × Γ → Q × Γ × {L, R} q0 ∈ Q qa ∈ Q qr ∈ Q∖{qa}
The computation of is a sequence of configurations
M
For , and
u, v ∈ Γ* a, b, c ∈ Γ p, q ∈ Q
if
uaqbv ⇒M upacv δ(q, b) = (p, c, L)
if
uqbv ⇒M ucpv δ(q, b) = (p, c, R)
is called the yield relation
⇒M
The starting configuration, on input , is
w ∈ Σ* q0w
We say is an accepting configuration if
uqv q = qa
We say is a rejecting configuration if
uqv q = qr
if
εqbv ⇒M εpcv δ(q, b) = (p, c, L)
Mihir Bellare, UCSD CSE 200: Computability and Complexity 8
We write if there is an integer and a sequence of configurations such that: — — and — for We refer to as the number of steps.
C ⇒*
M C′
n ≥ 1 C1, …, Cn+1 C1 = C Cn+1 = C′ Ci ⇒M Ci+1 i = 1,…, n n
We say accepts if there is an accepting configuration such that
M(w) C′ q0w ⇒*
M C′
We say rejects if there is a rejecting configuration such that
M(w) C′ q0w ⇒*
M C′
We also write if , in which case the number of steps is 0.
C ⇒*
M C′ C = C′
That’s a lot of definitions …
Hang in there, just a few more … ! We say halts if it either accepts or rejects
M(w)
We say loops if it doesn’t halt
M(w)
Mihir Bellare, UCSD CSE 200: Computability and Complexity 9
is the language
- f binary representations of the even positive integers
L = { x ∈ {0,1}* : MSB(x) = 1 and LSB(x) = 0 }
A language over is a subset
Σ L ⊆ Σ*
is the language of the Reachability Problem.
L = { ⟨G⟩ : G = ([n], E) is a graph over vertex set [n] with a path from 1 to n}
is the language of the Halting Problem.
L = { ⟨M, x⟩ : M is a TM that halts on input x}
Input: Question: Is ?
x x ∈ L
Decision problem associated to language :
L
Recall …
Examples of languages
Mihir Bellare, UCSD CSE 200: Computability and Complexity 10
We say that TM decides language if for all we have: — If then accepts — If then rejects
M L w ∈ Σ* w ∈ L M(w) w ∉ L M(w)
We say that language is decidable (or recursive) if there exists a TM that decides
L M L
We say that TM recognizes language if for all we have: — If then accepts — If then does not accept
M L w ∈ Σ* w ∈ L M(w) w ∉ L M(w)
So what does it do? Reject?
Possibly, but that is not the interesting case. The interesting case is that it DOES NOT HALT. That is, it LOOPS.
A language over is a subset
Σ L ⊆ Σ*
Input: Question: Is ?
x x ∈ L
Decision problem associated to language :
L
Recall …
We say that language is recognizable (or recursively enumerable) if there exists a TM that recognizes
L M L
We say that language is co-recognizable (or co-ecursively enumerable) if is recognizable
L L
Mihir Bellare, UCSD CSE 200: Computability and Complexity 11
We say that has output if accepts and its final configuration is When this happens, we write
M(w) y M(w) qay M(w) = y
We say that TM computes function if for all we have:
M f : Σ* → Σ * w ∈ Σ* M(w) = f(w)
We say that function is computable if there exists a TM that computes
f : Σ* → Σ * M f
Computability of Functions
Mihir Bellare, UCSD CSE 200: Computability and Complexity 12
Once in your life, you should write a TM program. It is OK if you don’t do it more than once Claim: The language is decidable.
L
TM Example
Let be the language of binary representations of the even positive integers
L = { x ∈ {0,1}* : MSB(x) = 1 and LSB(x) = 0 } q0 q1 qa qr
Proof: We give a TM that decides
M = (Q, Σ, Γ, δ, q0, qa, qr) L
0 → 0,R 1 → 1,R 0 → 0,R ⊔ → ⊔ , R
q2
⊔ → ⊔ , L 1 → 1,R 0 → 0,R 1 → 1,R ⊔ → ⊔ , R Q = {q0, q1, q2, qa, qr} Σ = {0,1} Γ = {0,1,⊔}
q σ δ(q, σ)
q0 q0
q1
q0
q1
⊔ 1 1 ⊔
q1
(qr,0,R) (q1,1,R) (qr, ⊔ ,R) (q1,0,R) (q1,1,R) (q2, ⊔ ,L)
q0110 ⇒M 1q110 ⇒M 11q10 ⇒M 110q1 ⇒M 11q20 ⇒M 110qa
Example:
Mihir Bellare, UCSD CSE 200: Computability and Complexity 13
On the Internet, one can find many Turing machine simulators. You can write Turing machines and watch them run. Snapshots of a few of the webpages follow. (The URL is shown on the snapshot.) But there are many more. Try it, could be fun!
Mihir Bellare, UCSD CSE 200: Computability and Complexity 14
Mihir Bellare, UCSD CSE 200: Computability and Complexity 15
Mihir Bellare, UCSD CSE 200: Computability and Complexity 16