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