Lecture 19 Turing Machines COT 4420 Theory of Computation Chapter 9 Slides from Prof. Busch Tape ...... A Turing Machine ...... Read-Write head Control Unit The Tape No boundaries -- infinite length ...... ...... Read-Write head The head moves Left or Right ...... ...... Read-Write head The head at each transition: 1. Reads a symbol 2. Writes a symbol 3. Moves Left or Right The Input String Input string ...... ☐ ☐ a b a c Blank symbol ☐ ☐ ☐ head Head starts at the leftmost position of the input string ...... States & Transitions Read q1 Write a → b, L Move Left q2 Move Right q1 a → b, R q2 ...... ☐ ☐ a b a c ☐ ☐ ☐ ...... ☐ ☐ ☐ ...... q1 ...... ☐ ☐ a b b c q2 q1 a → b, R q2 ...... ☐ ☐ a b a c ☐ ☐ ☐ ...... ☐ ☐ ☐ ...... q1 ...... ☐ ☐ a b b c q2 q1 a → b, L q2 ...... ☐ ☐ a b a c ☐ ☐ ☐ ...... q1 ...... ☐ ☐ a b a c g ☐ ☐ q2 q1 ☐ g, R q2 ...... Formal Definition of Turing Machine M = <Q, Σ, Γ, δ, q0, ☐, F> • • • • • • • Q: a finite set of internal states Σ: is the input alphabet ( Σ ⊆ Γ-{☐} ) Γ: finite set of symbols called the tape alphabet δ: transition function (Q × Γ Q × Γ × {L, R} ) q0: the initial state (q0 ∈ Q) ☐: is a special symbol called blank (☐ ∈ Γ) F: a set of final/accepting states (F ⊆ Q) Transition Function q1 a → b, R q2 δ (q1, a ) = (q2 , b, R) Determinism • Turing Machines are deterministic Not Allowed Allowed a → b, R q2 a → b, R q2 a → d, L q3 q1 q1 b → d, L q3 • No lambda transitions allowed • It is okay to not have transitions for some alphabet Halting The machine halts in a state if there is no transition to follow Halting Example 1: ...... ☐ ☐ a b b c ☐ ☐ ☐ ...... q1 q1 No transition from q1 HALT!!! Halting Example 2: ...... ☐ ☐ a b b c ☐ ☐ ☐ ...... q1 a → b, R q2 b → d, L q3 q1 No possible transition From q1 and symbol c HALT!!! Accept/Final States q1 q2 Allowed q1 q2 Not Allowed •Accepting states have no outgoing transitions •The machine halts and accepts Acceptance Accept Input string If machine halts in a final state Reject Input string If machine halts in a non-final state or If machine enters an infinite loop Observation: In order to accept an input string, it is not necessary to scan all the symbols in the string Turing Machine Example Input alphabet {a} Accepts the language: a* a → a, R q0 ☐☐,L q1 ☐ ☐ a a a ☐ ☐ ☐ q0 a → a, R q0 ☐☐,L q1 ☐ ☐ a a a ☐ ☐ ☐ q0 a → a, R q0 ☐☐,L q1 ☐ ☐ a a a ☐ ☐ ☐ q0 a → a, R q0 ☐☐,L q1 ☐ ☐ a a a ☐ ☐ ☐ q0 a → a, R q0 ☐☐,L q1 ☐ ☐ ☐ ☐ ☐ a a a q1 a → a, R q0 Halt & Accept ☐☐,L q1 Rejection Example ☐ ☐ a b a ☐ ☐ ☐ q0 a → a, R q0 ☐☐,L q1 ☐ ☐ a b a ☐ ☐ ☐ q0 No possible Transition Halt & Reject a → a, R q0 ☐☐,L q1 Infinite Loop Example A Turing machine for language a * +b( a + b) * b → b, L a → a, R q0 ☐☐,L q1 ☐ ☐ a b a ☐ ☐ ☐ q0 b → b, L a → a, R q0 ☐☐,L q1 ☐ ☐ a b a ☐ ☐ ☐ q0 b → b, L a → a, R q0 ☐☐,L q1 ☐ ☐ a b a ☐ ☐ ☐ q0 b → b, L a → a, R q0 ☐☐,L q1 Because of the infinite loop: • The accepting state cannot be reached • The machine never halts • The input string is rejected Turing Machine Example Turing machine for the language y → y, R q4 ☐☐,L q3 y → y, R q0 y → y, R a → a, R a → x, R q1 n n {a b } y → y, L a → a, L b → y, L x → x, R q2 ☐ a a b b ☐ q0 y → y, R q4 ☐☐,L q3 y → y, R q0 y → y, R a → a, R a → x, R q1 y → y, L a → a, L b → y, L x → x, R q2 ☐ x a b b ☐ q1 y → y, R q4 ☐☐,L q3 y → y, R q0 y → y, R a → a, R a → x, R q1 y → y, L a → a, L b → y, L x → x, R q2 ☐ x a b b ☐ q1 y → y, R q4 ☐☐,L q3 y → y, R q0 y → y, R a → a, R a → x, R q1 y → y, L a → a, L b → y, L x → x, R q2 ☐ x a y b ☐ q2 y → y, R q4 ☐☐,L q3 y → y, R q0 y → y, R a → a, R a → x, R q1 y → y, L a → a, L b → y, L x → x, R q2 ☐ x a y b ☐ q2 y → y, R q4 ☐☐,L q3 y → y, R q0 y → y, R a → a, R a → x, R q1 y → y, L a → a, L b → y, L x → x, R q2 ☐ x a y b ☐ q0 y → y, R q4 ☐☐,L q3 y → y, R q0 y → y, R a → a, R a → x, R q1 y → y, L a → a, L b → y, L x → x, R q2 ☐ x x y b ☐ q1 y → y, R q4 ☐☐,L q3 y → y, R q0 y → y, R a → a, R a → x, R q1 y → y, L a → a, L b → y, L x → x, R q2 ☐ x x y b ☐ q1 y → y, R q4 ☐☐,L q3 y → y, R q0 y → y, R a → a, R a → x, R q1 y → y, L a → a, L b → y, L x → x, R q2 ☐ x x y y ☐ q2 y → y, R q4 ☐☐,L q3 y → y, R q0 y → y, R a → a, R a → x, R q1 y → y, L a → a, L b → y, L x → x, R q2 ☐ x x y y ☐ q2 y → y, R q4 ☐☐,L q3 y → y, R q0 y → y, R a → a, R a → x, R q1 y → y, L a → a, L b → y, L x → x, R q2 ☐ x x y y ☐ q0 y → y, R q4 ☐☐,L q3 y → y, R q0 y → y, R a → a, R a → x, R q1 y → y, L a → a, L b → y, L x → x, R q2 ☐ x x y y ☐ q3 y → y, R q4 ☐☐,L q3 y → y, R q0 y → y, R a → a, R a → x, R q1 y → y, L a → a, L b → y, L x → x, R q2 ☐ x x y y ☐ q3 y → y, R q3 q4 ☐☐,L y → y, R q0 y → y, R a → a, R a → x, R q1 y → y, L a → a, L b → y, L x → x, R q2 ☐ x x y y ☐ q4 Halt & Accept y → y, R q4 ☐☐,L q3 y → y, R q0 y → y, R a → a, R a → x, R q1 y → y, L a → a, L b → y, L x → x, R q2 Observation: If we modify the machine for the language we can easily construct a machine for the language n n {a b } n n n {a b c } Instantaneous Description Configuration ☐ c a b a ☐ q1 Instantaneous description: ca q1 ba ☐ x a y b ☐ ☐ x a y b q0 q2 A Move: q2 xayb ⊢ x q0 ayb ☐ Time 4 ☐ Time 5 x a y b ☐ x a y b ☐ q0 q2 Time 6 ☐ ☐ Time 7 x x y b ☐ ☐ x x y b q1 q1 A computation q2 xayb ⊢ x q0 ayb ⊢ xx q1 yb ⊢ xxy q1 b ☐ q2 xayb ⊢ x q0 ayb ⊢ xx q1 yb ⊢ xxy q1 b Equivalent notation: * q2 xayb ⊢ xxy q1 b The Accepted Language For any Turing Machine M, the language accepted by M is L(M) = { w ∈ Σ+ : q0w ⊢ x1 qf x2 for some qf ∈ F, x1, x2 ∈ Γ*} If a language L is accepted by a Turing machine M then we say that L is: Turing Recognizable Other names used: •Turing Acceptable •Recursively Enumerable Computing Functions with Turing Machines Definition: f A function is computable if there is a Turing Machine M such that: Initial configuration w ☐ ☐ Final configuration f (w) ☐ qf q0 For all w∈ D Domain ☐ In other words: f A function is computable if there is a Turing Machine M such that: * q 0w ⊢ Initial Configuration For all qf f(w) Final Configuration w∈ D Domain Example Design a Turing machine that computes: f ( x, y ) = x + y • How to represent x and y on the tape? For simplicity we will use unary notation For example: x = 5 11111 x Start ☐ 1 1 y 1 0 1 1 ☐ q0 initial state The 0 is the delimiter that separates the two numbers y x Start ☐ 1 1 1 0 1 1 ☐ q0 x+ y Finish ☐ 1 1 qf 1 1 0 ☐ Turing machine for function 1 → 1, R f ( x, y ) = x + y 1 → 1, L 1 → 1, R ☐☐,L q 1 → 0, L 0 → 1 , R q0 q3 q1 2 ☐☐,R q4 ☐ 1 1 0 1 1 ☐ q0 1 → 1, R 1 → 1, L 1 → 1, R ☐☐,L q 1 → 0, L 0 → 1 , R q0 q3 q1 2 ☐☐,R q4 ☐ 1 1 0 1 1 ☐ q0 1 → 1, R 1 → 1, L 1 → 1, R ☐☐,L q 1 → 0, L 0 → 1 , R q0 q3 q1 2 ☐☐,R q4 ☐ 1 1 0 1 1 ☐ q0 1 → 1, R 1 → 1, L 1 → 1, R ☐☐,L q 1 → 0, L 0 → 1 , R q0 q3 q1 2 ☐☐,R q4 ☐ 1 1 1 1 1 ☐ q1 1 → 1, R 1 → 1, L 1 → 1, R ☐☐,L 0 → 1 , R 1 → 0 , L q q0 q3 q1 2 ☐☐,R q4 ☐ 1 1 1 1 1 ☐ q1 1 → 1, R 1 → 1, L 1 → 1, R ☐☐,L q 1 → 0, L 0 → 1 , R q0 q3 q1 2 ☐☐,R q4 ☐ 1 1 1 1 1 ☐ q1 1 → 1, R 1 → 1, L 1 → 1, R ☐☐,L q 1 → 0, L 0 → 1 , R q0 q3 q1 2 ☐☐,R q4 ☐ 1 1 1 1 1 ☐ q2 1 → 1, R 1 → 1, L 1 → 1, R ☐☐,L q 1 → 0, L 0 → 1 , R q0 q3 q1 2 ☐☐,R q4 ☐ 1 1 1 1 0 ☐ q3 1 → 1, R 1 → 1, L 1 → 1, R ☐☐,L q 1 → 0, L 0 → 1 , R q0 q3 q1 2 ☐☐,R q4 ☐ 1 1 1 1 0 ☐ q3 1 → 1, R 1 → 1, L 1 → 1, R ☐☐,L q 1 → 0, L 0 → 1 , R q0 q3 q1 2 ☐☐,R q4 ☐ 1 1 1 1 0 ☐ q3 1 → 1, R 1 → 1, L 1 → 1, R ☐☐,L q 1 → 0, L 0 → 1 , R q0 q3 q1 2 ☐☐,R q4 ☐ 1 1 1 1 0 ☐ q3 1 → 1, R 1 → 1, L 1 → 1, R ☐☐,L q 1 → 0, L 0 → 1 , R q0 q3 q1 2 ☐☐,R q4 ☐ 1 1 1 1 0 ☐ q3 1 → 1, R 1 → 1, L 1 → 1, R ☐☐,L q 1 → 0, L 0 → 1 , R q0 q3 q1 2 ☐☐,R q4 ☐ 1 1 1 1 0 ☐ q4 1 → 1, R 1 → 1, L 1 → 1, R ☐☐,L q 1 → 0, L 0 → 1 , R q0 q3 q1 2 ☐☐,R HALT & accept q4 Another Example The function f ( x) = 2 x is computable Turing Machine: Input string: Output string: x unary xx unary Turing Machine Pseudocode for f ( x) = 2 x • Replace every 1 with $ • Repeat: • Find rightmost $, replace it with 1 • Go to right end, insert 1 Until no more $ remain Example Start ☐ 1 1 Finish ☐ ☐ q0 1 1 1 1 q3 1 → $, R q0 1 → 1, L ☐☐,L 1 → 1, R $ → 1 , R q1 ☐☐,R q3 ☐1,L q2 ☐ Another Example The function f ( x, y ) = is computable Input: Output: x0 y 1 or 0 1 if x> y 0 if x≤ y Turing Machine Pseudocode: • Repeat Match a 1 from Until all of x with a 1 from y x or y is matched • If a 1 from x is not matched erase tape, write 1 else erase tape, write 0 ( x > y) ( x ≤ y) Combining Turing Machines Block Diagram input Turing Machine output Example: x + y if x > y f ( x, y ) = 0 x, y x, y Comparer if x ≤ y Adder x+ y Eraser 0 x> y x≤ y Turing’s Thesis Turing’s thesis (1930): Any computation carried out by mechanical means can be performed by a Turing Machine Arguments that support Turing’s Thesis • Anything that can be done on any existing digital computer can also be done by a Turing machine. • No one has yet been able to suggest a problem, solvable by what we intuitively consider an algorithm, for which a TM program cannot be written. • Alternative models have been proposed but none of them is more powerful than the TM model. Algorithm: An algorithm for a problem is a Turing Machine which solves the problem The algorithm describes the steps of the mechanical means This is easily translated to computation steps of a Turing machine An algorithm for a function f: D R is a Turing machine M, which given as input any d ∈ D on its tape, eventually halts with the correct answer f(d) ∈ R on its tape. * q0d ⊢M qf f(d) for all d ∈ D. qf ∈ F When we say: There exists an algorithm We mean: There exists a Turing Machine that executes the algorithm