Download - Turing Machines - Rice Universityyz2/COMP481/Chapter17.pdf · Turing Machine Machine Move • Change symbol under tape head • Move tape head • Change state • Analog Machine?

Transcript
Page 1: Turing Machines - Rice Universityyz2/COMP481/Chapter17.pdf · Turing Machine Machine Move • Change symbol under tape head • Move tape head • Change state • Analog Machine?

Turing Machines

Page 2: Turing Machines - Rice Universityyz2/COMP481/Chapter17.pdf · Turing Machine Machine Move • Change symbol under tape head • Move tape head • Change state • Analog Machine?

Part I: Definitions and Properties

Page 3: Turing Machines - Rice Universityyz2/COMP481/Chapter17.pdf · Turing Machine Machine Move • Change symbol under tape head • Move tape head • Change state • Analog Machine?

Finite State Automata

Deterministic Automata (DFA)

• M = {Q, Σ, δ , q0, F}

-- Σ = Symbols

-- Q = States

-- q0 = Initial State

-- F = Accepting States ⊆ Q

-- δ :Q × Σ→Q = Transition Functions

Analog Machine

• Hard Wired

• No Code

Page 4: Turing Machines - Rice Universityyz2/COMP481/Chapter17.pdf · Turing Machine Machine Move • Change symbol under tape head • Move tape head • Change state • Analog Machine?

Push-Down Automata

Push Down Automata (PDA)

• Finite State Automata with a Stack

• Stack can be used for Counting

• Accept ⇔ Halts in an Accepting State AND Stack is Empty

• PDA more Powerful than FSA

• Non-Deterministic (Analog?) Machine

Page 5: Turing Machines - Rice Universityyz2/COMP481/Chapter17.pdf · Turing Machine Machine Move • Change symbol under tape head • Move tape head • Change state • Analog Machine?

Turing Machine

Informal Definition

• Control Unit

• States

• Doubly infinite tape

-- input

-- output

-- memory

• Tape reader

• Initial state

• (See Picture)

Page 6: Turing Machines - Rice Universityyz2/COMP481/Chapter17.pdf · Turing Machine Machine Move • Change symbol under tape head • Move tape head • Change state • Analog Machine?

Turing Machine

• Machine with states -- finite automata• Tape -- mostly blank• Tape head -- on leftmost nonblank symbol

Page 7: Turing Machines - Rice Universityyz2/COMP481/Chapter17.pdf · Turing Machine Machine Move • Change symbol under tape head • Move tape head • Change state • Analog Machine?

Turing Machine

Formal Definition

M = {Q, Σ, H ,δ , s0}

• Q = set of states• s0 = initial state

• H = halting states ⊂ Q

• Σ = tape alphabet

• δ : (Q − H )× Σ→Q × Σ × {R, L}

-- go to new state

-- write new symbol on tape -- replace old symbol

-- move tape head to left or right

-- list of 5-tuples -- (s, x; s∗, x∗ , L / R)

Page 8: Turing Machines - Rice Universityyz2/COMP481/Chapter17.pdf · Turing Machine Machine Move • Change symbol under tape head • Move tape head • Change state • Analog Machine?

Turing Machine

Machine Move

• Change symbol under tape head

• Move tape head

• Change state

• Analog Machine?

Hardware and Software

• Turing Machine = Computer (Hardware)

• 5-tuples = Program (Software)

Functions

• M maps the input x to the output y and then halts.

• M is a partial function because M might not halt.

Page 9: Turing Machines - Rice Universityyz2/COMP481/Chapter17.pdf · Turing Machine Machine Move • Change symbol under tape head • Move tape head • Change state • Analog Machine?

Diagrams of Turing Machines

Diagrams

• States = Circles

• Arrows = Transition Functions

-- From Current State to Next State

• Labels on Arrows = x / y / (L  or  R)

-- Read x

-- Write y

-- Move Left or Right

Page 10: Turing Machines - Rice Universityyz2/COMP481/Chapter17.pdf · Turing Machine Machine Move • Change symbol under tape head • Move tape head • Change state • Analog Machine?

Configurations of Turing Machines

(q,wL ,x, wR ) ≡ (q,wLx wR )

-- q = current state

-- x = current symbol on tape

-- wL = string of non blank symbols to the left of x

-- wR = string of non blank symbols to the right of x

Page 11: Turing Machines - Rice Universityyz2/COMP481/Chapter17.pdf · Turing Machine Machine Move • Change symbol under tape head • Move tape head • Change state • Analog Machine?

What a Turing Machine Can Do

• Halt -- Accept or Reject

• Loop -- Cycle

• Diverge -- Never Halt or Cycle

Page 12: Turing Machines - Rice Universityyz2/COMP481/Chapter17.pdf · Turing Machine Machine Move • Change symbol under tape head • Move tape head • Change state • Analog Machine?

`Languages and Turing Machines

Decidable Languages

• M Halts in Accept State ⇔ x ∈L

• M Halts in Reject State ⇔ x ∉L

Semi Decidable Languages

• M Halts in Accept State ⇔ x ∈L

• M may Loop or Diverge for x ∉L

Page 13: Turing Machines - Rice Universityyz2/COMP481/Chapter17.pdf · Turing Machine Machine Move • Change symbol under tape head • Move tape head • Change state • Analog Machine?

Decidable Languages

L = {anbncn} -- Not Context Free

L = {wc w|w ∈{a,b}∗} -- Not Context Free

Page 14: Turing Machines - Rice Universityyz2/COMP481/Chapter17.pdf · Turing Machine Machine Move • Change symbol under tape head • Move tape head • Change state • Analog Machine?

Power of Turing Machine

Lemma: L = {anbncn} can be recognized by a Turing Machine

(L is not context free -- pumping theorem)

Proof: If Tape Empty, Halt and Accept

• Otherwise, Replace first a with 1

• Move Right to first b and Replace with 2

-- If no b or if c encountered first, Halt and Reject

• Move Right to first c and Replace with 3

-- If no c or if a encountered first, Halt and Reject

• Continue until all a’s converted to 1’s

If no b’s or c’s remain, Halt and Accept

Otherwise, Halt and Reject

Page 15: Turing Machines - Rice Universityyz2/COMP481/Chapter17.pdf · Turing Machine Machine Move • Change symbol under tape head • Move tape head • Change state • Analog Machine?

Functions and Turing Machines

Turing Machines as Functions

• Input = Initial String (Non Blanks) on Tape

-- x ∈X = Domain

• Output = Final String on Tape when Machine Halts

-- y ∈Y = Range

• Function

-- M : X →Y

-- M (x) = y

• Computable (Recursive) Functions

-- Functions that can be Computed by Some Turing Machine

Page 16: Turing Machines - Rice Universityyz2/COMP481/Chapter17.pdf · Turing Machine Machine Move • Change symbol under tape head • Move tape head • Change state • Analog Machine?

Examples

Language Recognition

• x ∈Σ∗ = Domain = Strings

• Output: Range = {TRUE, FALSE}

• M recognizes L if and only if

-- M Halts and Writes TRUE on the Tape when x ∈L

-- M Halts and Writes FALSE on the Tape when x ∉L

Functions on Natural Numbers

• Encode Natural Numbers as Binary (or Decimal) Strings

-- Input and Output are Binary (Decimal) Strings

• Example: Adding 1 to a Binary Number

-- Carrying Function

Page 17: Turing Machines - Rice Universityyz2/COMP481/Chapter17.pdf · Turing Machine Machine Move • Change symbol under tape head • Move tape head • Change state • Analog Machine?

Part II: Equivalent Machines

Page 18: Turing Machines - Rice Universityyz2/COMP481/Chapter17.pdf · Turing Machine Machine Move • Change symbol under tape head • Move tape head • Change state • Analog Machine?

Equivalent Machines for Simplifying Proofs

1. Multiple Tapes

2. Nondeterminism

3. One Way Tape

4. Two Stacks

Page 19: Turing Machines - Rice Universityyz2/COMP481/Chapter17.pdf · Turing Machine Machine Move • Change symbol under tape head • Move tape head • Change state • Analog Machine?

Multiple Tape Turing Machine

Description

• Input on Tape 1

-- All Other Tapes Start Blank

ª Output on Tape 1

-- Contents of All Other Tapes Ignored

• Move all Tape Heads Simultaneously

-- Left. Right. or Stay in Same Location

• Halt

Page 20: Turing Machines - Rice Universityyz2/COMP481/Chapter17.pdf · Turing Machine Machine Move • Change symbol under tape head • Move tape head • Change state • Analog Machine?

Simulating Multiple Tapes with One Tape

Tape Alphabet

• Introduce New Tape Symbols to Represent with a Single Symbol

-- Contents of Each Position on Each Tape

-- Location of Each Tape Head -- 0, 1

Program

• Replace Original Tape Symbols by New Tape Symbols

• Locate and Store (in State) Symbols at Current Head Locations

• Change Symbols to Simulate Each Head Move

• Replace New Tape Symbols by Corresponding Original Tape Symbols

• Halt

Page 21: Turing Machines - Rice Universityyz2/COMP481/Chapter17.pdf · Turing Machine Machine Move • Change symbol under tape head • Move tape head • Change state • Analog Machine?

Nondeterministic Machines

Non Deterministic Finite State Automata

• Simultaneous Transitions to Many Possible Different States

• No More Powerful than Deterministic Finite State Automata

Non Deterministic Push Down Automata

• Simultaneous Transitions to Many Possible Different States and Stacks

• More Powerful than Deterministic Push Down Automata

Page 22: Turing Machines - Rice Universityyz2/COMP481/Chapter17.pdf · Turing Machine Machine Move • Change symbol under tape head • Move tape head • Change state • Analog Machine?

Nondeterministic Turing Machine

Simultaneous Transitions

• Many Possible Different States

• Multiple Possible Head Directions

• Writing Many Possible Different Symbols on Current Tape Location

Accept or Reject

• Accept if at Least One Computation Accepts

• Reject if ALL Computations Reject

Page 23: Turing Machines - Rice Universityyz2/COMP481/Chapter17.pdf · Turing Machine Machine Move • Change symbol under tape head • Move tape head • Change state • Analog Machine?

Decidable and Semi-Decidable -- Languages

Decidable (M Decides L)

• Finite Number of Paths for Each String

• Each Path Halts

• M Accept w ⇔ w∈L

Semi-Decidable (M Semi-Decides L)

• M Accept w on at Least 1 Path ⇔ w∈L

Page 24: Turing Machines - Rice Universityyz2/COMP481/Chapter17.pdf · Turing Machine Machine Move • Change symbol under tape head • Move tape head • Change state • Analog Machine?

Non-Determinism and Functions

M Compute F

• For Each w in Domain F

-- Every Path Halts

-- Final String is the Same F(w) for Every Path

Page 25: Turing Machines - Rice Universityyz2/COMP481/Chapter17.pdf · Turing Machine Machine Move • Change symbol under tape head • Move tape head • Change state • Analog Machine?

Simulating Nondeterminism

Depth First Search

• No

• Why?

Breadth First Search

• Yes

• Why?

Page 26: Turing Machines - Rice Universityyz2/COMP481/Chapter17.pdf · Turing Machine Machine Move • Change symbol under tape head • Move tape head • Change state • Analog Machine?

Simulating 1 Two Way Tape with 3 One Way Tapes

Tapes

• Tape #1 = Characters to the Right of Initial Position

• Tape #2 = Characters Introduced to the Left of Initial Position

• Tape #3 = Number of Operations in Unary (All 1’s)

Simulation

• Initialize Tape #1 to Initial Characters; Tape #2 and Tape #3 Blank

• Simulate All Moves to Right of Initial Position on Tape #1

• Simulate All Moves to Left of Initial Position on Tape #2

• Shift Tape #1 to Right by Number of Steps on Tape #3

• Move Characters from Tape #2 to Tape #1

Page 27: Turing Machines - Rice Universityyz2/COMP481/Chapter17.pdf · Turing Machine Machine Move • Change symbol under tape head • Move tape head • Change state • Analog Machine?

Turing Machines and Push Down Automata

Theorems

1. Every Push Down Automaton can be Simulated with a Turing Machine.

2. Every Turing Machine can be Simulated by a Push Down Automaton

with 2 Stacks.

3. Turing Machines are more powerful than Push Down Automata

with 1 Stack.

Page 28: Turing Machines - Rice Universityyz2/COMP481/Chapter17.pdf · Turing Machine Machine Move • Change symbol under tape head • Move tape head • Change state • Analog Machine?

Theorem 1

Every Push Down Automaton can be Simulated by a Non-Deterministic

Turing Machine with 2 Tapes

Simulation

• Tape #1 = Characters in String

• Read Only

• Move Only to Right

• Tape #2 = Characters on Stack

• Pop -- Scan Left Replacing Symbols by Blanks

• Push -- Scan Right Replacing Blanks by Symbols

Page 29: Turing Machines - Rice Universityyz2/COMP481/Chapter17.pdf · Turing Machine Machine Move • Change symbol under tape head • Move tape head • Change state • Analog Machine?

Theorem 2

Every Turing Machine can be Simulated by a Push Down Automaton

with 2 Stacks

Simulation

• Stack #1 = Characters to Left of Head and Under Reading Head

• Stack #2 = Characters to Right of Reading Head

• Move Left Move Right

Pop Stack #1 Pop Stack #2

Push Stack #2 Push Stack #1

Page 30: Turing Machines - Rice Universityyz2/COMP481/Chapter17.pdf · Turing Machine Machine Move • Change symbol under tape head • Move tape head • Change state • Analog Machine?

Theorem 3

Turing Machines are more Powerful than Push Down Automaton with 1 Stack.

Proof

Turing Machines can Recognize the Language L = {anbncn} .

Page 31: Turing Machines - Rice Universityyz2/COMP481/Chapter17.pdf · Turing Machine Machine Move • Change symbol under tape head • Move tape head • Change state • Analog Machine?

Simulating a Digital Computer with a Turing Machine

Seven Tapes

• Tape #1 = Memory = Program + Data

• Tape #2 = Program Counter = Index into Memory in Tape #1

• Tape #3 = Address Register

• Tape #4 = Accumulator

• Tape #5 = Operation Code of Current Instruction

• Tape #6 = Input File

• Tape #7 = Output File

Page 32: Turing Machines - Rice Universityyz2/COMP481/Chapter17.pdf · Turing Machine Machine Move • Change symbol under tape head • Move tape head • Change state • Analog Machine?

Part III: Universal Turing Machines

Page 33: Turing Machines - Rice Universityyz2/COMP481/Chapter17.pdf · Turing Machine Machine Move • Change symbol under tape head • Move tape head • Change state • Analog Machine?

Encoding Turing Machines

States

• Number the States in Any Order

• Code the Numbers in Binary

-- Accepting State = y-binary

-- Rejecting State = n-binary

-- Other States = q-binary

Symbols

• Number the Symbols in Any Order

• Code the Numbers in Binary

• Assign a-binary to Each Symbol

Page 34: Turing Machines - Rice Universityyz2/COMP481/Chapter17.pdf · Turing Machine Machine Move • Change symbol under tape head • Move tape head • Change state • Analog Machine?

Encoding Turing Machines (continued)

Transition Functions

• (State, Input Character, New State, Output Character, Move)

• (q-binary, a-binary, q-binary, a-binary, L or R)

Turing Machine

• List of Transition Functions

Page 35: Turing Machines - Rice Universityyz2/COMP481/Chapter17.pdf · Turing Machine Machine Move • Change symbol under tape head • Move tape head • Change state • Analog Machine?

Number of Turing Machines

How Many Turing Machines?

• Turing Machine ⇔ Finite List of 5-tuples

-- δ :Q × Σ→Q × Σ × {R, L}

-- (q, x; q∗, x∗ , L / R)

• Number of finite lists of 5-tuples in countable

-- # Turing machines with 1 state is countable

-- # TM with 1 state = # 5-tuples = 2 | Σ | 2

-- # Turing machines with n states is countable

-- # TM with n states = # 5-tuples = 2n2 | Σ | 2

• Countable union of countable sets is countable

• Number of Turing machines is countable

Page 36: Turing Machines - Rice Universityyz2/COMP481/Chapter17.pdf · Turing Machine Machine Move • Change symbol under tape head • Move tape head • Change state • Analog Machine?

Non Computable Functions

Existence of Non Computable Functions -- Counting Argument

• There are only countably many Turing machines

-- Turing machines can be listed in Lexicographic order

• There are uncountably many functions f : N → {0,1}{ }⇔ P(N )

Existence of Non Computable Functions -- Diagonalization Argument

• List the Turing Machines

• List all Possible Input Strings

•     Set    M (wi ) = 0      if  Mi (wi ) =1                          =1       if  Mi(wi ) = 0

• M not in List ⇒ Exist Non-Computable Functions

Page 37: Turing Machines - Rice Universityyz2/COMP481/Chapter17.pdf · Turing Machine Machine Move • Change symbol under tape head • Move tape head • Change state • Analog Machine?

Universal Turing Machine

Input

• Encoding of Any Turing Machine M

-- Also could input just the Number encoding M in Lexicographic Order

• Encoding of Input Tape for M

3 Tapes

• Tape #1 = Tape of Input for M

• Tape #2 = Encoding of (Transition Functions) for M

• Tape #3 = Current State of M

Page 38: Turing Machines - Rice Universityyz2/COMP481/Chapter17.pdf · Turing Machine Machine Move • Change symbol under tape head • Move tape head • Change state • Analog Machine?

Universal Turing Machine (continued)

Simulation

• Find Current State of M -- Tape #3

• Find Current Character -- Tape #1

• Find Appropriate Transition Function for State and Character -- Tape #2

• Apply Transition Function

-- Update Tape #1 and Tape #3

• Continue Until in Halting State

Conclusion

• Universal Turing Machine with input <M, w> reports same result as

Turing Machine M on Input w

Page 39: Turing Machines - Rice Universityyz2/COMP481/Chapter17.pdf · Turing Machine Machine Move • Change symbol under tape head • Move tape head • Change state • Analog Machine?

Universal Turing Machine as Digital Computer

Machine Move

• Change symbol under tape head -- Tape #1

• Move tape head -- Tape #1

• Change state -- Tape #3

Hardware

• Universal Turing Machine

Software

• Transition Functions = Program

• Data = Input String