
COMPUTER SCIENCE SEDGEWICK/WAYNE Computer Science Computer 18. Turing Machines Science An Interdisciplinary Approach R O B E R T S E D G E W I C K Section 7.4 KEVIN WAYNE http://introcs.cs.princeton.edu Universality and computability Fundamental questions • What is a general-purpose computer? • Are there limits on the power of digital computers? • Are there limits on the power of machines we can build? Pioneering work at Princeton in the 1930s. David Hilbert Kurt Gödel Alsonzo Church Alan Turing 1862−1943 1906−1978 1903−1995 1912−1954 Asked the questions Solved the math Solved the decision Provided THE answers problem problem 2 Context: Mathematics and logic Principia Mathematics Mathematics. Any formal system powerful enough to express arithmetic. Peano arithmetic Zermelo-Fraenkel set theory . Complete. Can prove truth or falsity of any arithmetic statement. Consistent. Cannot prove contradictions like 2 + 2 = 5. Decidable. An algorithm exists to determine truth of every statement. Q. (Hilbert, 1900) Is mathematics complete and consistent? A. (Gödel's Incompleteness Theorem, 1931) NO (!!!) Q. (Hilbert's Entscheidungsproblem) Is mathematics decidable? A. (Church 1936, Turing 1936) NO (!!) 3 COMPUTER SCIENCE SEDGEWICK/WAYNE 18. Turing Machines •A simple model of computation •Universality •Computability •Implications CS.18.A.Turing.Machine Starting point Goals • Develop a model of computation that encompasses all known computational processes. • Make the model as simple as possible. Example: A familiar computational process. 1 0 1 0 3 1 4 2 Characteristics 7 1 8 2 • Discrete. 1 0 3 2 4 • Local. • States. 5 Previous lecture: DFAs A DFA is an abstract machine that solves a pattern matching problem. • A string is specified on an input tape (no limit on its length). • The DFA reads each character on input tape once, moving left to right. • The DFA lights "YES" if it recognizes the string, "NO" otherwise. Each DFA defines a set of strings (all the strings that it recognizes). NO YES b b a a b b a b b 6 This lecture: Turing machines A Turing machine (TM) is an abstract model of computation. • A string is specified on a tape (no limit on its length). • The TM reads and writes characters on the tape, moving left or right. • The TM lights "YES" if it recognizes the string, "NO" otherwise. • The TM may halt, leaving the result of the computation on the tape. NO HALT YES . 7 Previous lecture: DFA details and example A DFA is an abstract machine with a finite number of states, each labelled Y or N and transitions between states, each labelled with a symbol. One state is the start state. • Begin in the start state. • Read an input symbol and move to the indicated state. • Repeat until the last input symbol has been read. • Turn on the "YES" or "NO" light according to the label on the current state. NO a a a Does this DFA recognize Y b N b N this string? b YES b b a a b b a b b 8 This lecture: Turing Machine details and example A Turing Machine is an abstract machine with a finite number of states, each labelled Y, N, H, L, or R and transitions between states, each labelled with a read/write pair of symbols. • Begin in the designated start state. • Read an input symbol, move to the indicated state and write the indicated output. • Move tape head left if new state is labelled L, right if it is labelled R. • Repeat until entering a state labelled Y, N, or H ( and turn on associated light). NO 1:0 #:1 R #:# L H HALT 0:1 YES # # # 1 0 1 1 0 0 1 1 1 # # # 9 DFAs vs TMs Similarities • Simple model of computation. • Input on tape is a finite string with symbols from a finite alphabet. • Finite number of states. • State transitions determined by current state and input symbol. Differences DFAs TMs • Can read input symbols from the tape. • Can read from or write onto the tape. • Can only move tape head to the right. • Can move tape head either direction. • Tape is finite (a string). • Tape does not end (either direction). • One state transition per input symbol. • No limit on number of transitions. • Can recognize (turn on "YES" or "NO"). • Can also compute (with output on tape). 10 TM example 1: Binary decrementer NO 0:1 R #:# L 1:0 H HALT YES # # # 01 0 1 0 1 0 0 0 0 # # # Input 1 0 1 0 1 0 0 0 0 Output 1 0 1 0 0 1 1 1 1 11 TM example 1: Binary decrementer Q. What happens when we try to decrement 0? NO 0:1 #:# R #:# L 1:0 H HALT YES # # # 1# # # # # 0 0 0 0 # # # A. Doesn't halt! TMs can have bugs, too. Fix to avoid infinite loop. Check for #. 12 TM example 2: Binary incrementer Note: This adds a 1 at the left as the last step when incrementing 111...1 NO 1:0 #:1 R #:# L H HALT 0:1 YES # # # 10 0 1 0 0 1 1 1 1 # # # Input 1 0 1 0 0 1 1 1 1 Output 1 0 1 0 1 0 0 0 0 13 TM example 3: Binary adder (method) To compute x + y • Move right to right end of y. ... # # 1 0 1 1 + 1 0 1 0 # # ... • Decrement y. ... # # 1 0 1 1 + 1 0 0 1 # # ... • Move left to right end of x (left of +) . ... # # 1 0 1 1 + 1 0 0 1 # # ... • Increment x. ... # # 1 1 0 0 + 1 0 0 1 # # ... • Continue until y = 0 is decremented. ... # 1 0 1 0 1 + 1 1 1 1 # # ... Found + when seeking 1? Just decremented 0. • Clean up by erasing + and 1s. ... # 1 0 1 0 1 # # # # # # # ... Clean up 14 TM example 3: Binary adder # 1 0 1 1 + 1 0 1 0 # NO 0:1 Find + 1:0 # 1 0 1 1 + 1 0 0 1 # Decrement y L L HALT +:# +:+ 1:# Halt # 1 0 1 1 + 1 0 0 1 # YES R #:# H # 1 1 0 0 + 1 0 0 1 # Clean Up #:# 0:1 ... Find right end R L Increment x #:1 1 0 1 0 1 + 1 1 1 1 # 1:0 1 0 1 0 1 # # # # # # # # # 1 0 1 1 + 1 0 1 0 # # # 15 Simulating an infinite tape with two stacks Q. How can we simulate a tape that is infinite on both ends? A. Use two stacks, one for each end. private Stack<Character> left; private Stack<Character> right; private char read() # # # 1 0 1 1 + 1 0 1 0 # # # { if (right.isEmpty()) return '#'; return right.pop(); "tape head" is top of right stack } assumes write just 1 private char write(char c) after each { right.push(c); } read 1 move 1 right private void moveRight() + + { 1 1 char c = '#'; move if (!right.isEmpty()) c = right.pop(); 0 1 0 left left.push(c); 0 0 0 1 } 1 1 1 0 private void moveLeft() { # # # # char c = '#'; empty? assume # is there if (!left.isEmpty()) c = left.pop(); right.push(c); } 16 Simulating the operation of a Turing machine public class TM fixes bug { 0:1 #:# private int state; R #:# L 1:0 H private int start; 0 1 2 private String[] action; private ST<Character, Integer>[] next; private ST<Character, Character>[] out; action[] next[] out[] /* Stack code from previous slide */ 0 1 # 0 1 # public TM(In in) { /* Fill in data structures */ } 0 R 0 0 0 1 0 0 1 # public String simulate(String input) 1 L 1 1 2 2 1 1 0 # { state = start; 2 H 2 2 2 2 2 0 1 # for (int i = input.length()-1; i >= 0; i--) entries in gray are implicit in graphical representation right.push(input.charAt(i); while (action(state).equals("L") || action(state).equals("R")) % more dec.txt { 3 01# 0 char c = read(); R 0 0 1 0 1 # state = next[state].get(c); L 1 2 2 1 0 # write(out[state].get(c)); if (action[state].equals("R") moveRight(); H 2 2 2 0 1 # if (action[state].equals("L") moveLeft(); % java TM dec.txt } 000111 return action[state]; 000110 } 010000 public static void main(String[] args) 001111 { /* Similar to DFA's main() */ } 000000 } 111111 17 COMPUTER SCIENCE SEDGEWICK/WAYNE 18. Turing Machines •A simple model of computation •Universality •Computability •Implications CS.18.B.Turing.Universality Representing a Turing machine Turing's key insight. A TM is nothing more than a finite sequence of symbols. dec.txt decrementer TM 0:1 #:# 3 01# 0 R 0 0 1 0 1 # R #:# L 1:0 H L 1 2 2 1 0 # 0 1 2 H 2 2 2 0 1 # Implication. Can put a TM and its input on a TM tape. 1 1 0 0 0 3 0 1 # 0 R 0 0 1 0 1 # L 1 2 1 1 0 # H 2 2 2 0 1 # Profound implication. We can use a TM to simulate the operation of any TM. 19 Universal Turing machine (UTM) Universal Turing machine. A TM that takes as input NO HALT any TM and input for that TM on a TM tape. UTM YES . # # # 1 1 0 0 0 3 0 1 # 0 R 0 0 1 0 1 $ L 1 2 1 1 0 $ H 2 2 2 0 1 $ / input to decrementer TM decrementer TM Result. Whatever would happen if that TM were to NO run with that input (could loop or end in Y, N or H).
Details
-
File Typepdf
-
Upload Time-
-
Content LanguagesEnglish
-
Upload UserAnonymous/Not logged-in
-
File Pages47 Page
-
File Size-