Turing Machines Part Two

Similar documents
CS103 Handout 13 Fall 2012 May 4, 2012 Problem Set 5

Context-Free Grammars

Context-Free Grammars

I have read and understand all of the instructions below, and I will obey the Academic Honor Code.

CS103 Handout 50 Fall 2018 November 30, 2018 Problem Set 9

Theory of Computations Spring 2016 Practice Final Exam Solutions

14.1 Encoding for different models of computation

Midterm Exam II CIS 341: Foundations of Computer Science II Spring 2006, day section Prof. Marvin K. Nakayama

TAFL 1 (ECS-403) Unit- V. 5.1 Turing Machine. 5.2 TM as computer of Integer Function

CS103 Handout 14 Winter February 8, 2013 Problem Set 5

CS21 Decidability and Tractability

We can create PDAs with multiple stacks. At each step we look at the current state, the current input symbol, and the top of each stack.

CS103 Handout 42 Spring 2017 May 31, 2017 Practice Final Exam 1

CSE 105 THEORY OF COMPUTATION

CSE 105 THEORY OF COMPUTATION

Recursively Enumerable Languages, Turing Machines, and Decidability

Limitations of Algorithmic Solvability In this Chapter we investigate the power of algorithms to solve problems Some can be solved algorithmically and

Outline for Today. How can we speed up operations that work on integer data? A simple data structure for ordered dictionaries.

Skill 1: Multiplying Polynomials

CSC-461 Exam #2 April 16, 2014

CpSc 421 Final Solutions

CSE 105 THEORY OF COMPUTATION

Range Minimum Queries Part Two

CS 5 Nightly Wrapup College Canceled

Closure Properties of CFLs; Introducing TMs. CS154 Chris Pollett Apr 9, 2007.

Outline for Today. Towards Perfect Hashing. Cuckoo Hashing. The Cuckoo Graph. Analysis of Cuckoo Hashing. Reducing worst-case bounds

Finite Automata Theory and Formal Languages TMV027/DIT321 LP4 2016

PROFESSOR: Last time, we took a look at an explicit control evaluator for Lisp, and that bridged the gap between

PDA s. and Formal Languages. Automata Theory CS 573. Outline of equivalence of PDA s and CFG s. (see Theorem 5.3)

CISC 4090 Theory of Computation

Outline. Language Hierarchy

Problems, Languages, Machines, Computability, Complexity

CS103 Handout 16 Winter February 15, 2013 Problem Set 6

Introduction to Computers & Programming

CS 125 Section #4 RAMs and TMs 9/27/16

CS5371 Theory of Computation. Lecture 8: Automata Theory VI (PDA, PDA = CFG)

Context-Free Grammars

COMP 382: Reasoning about algorithms

CS103 Handout 35 Spring 2017 May 19, 2017 Problem Set 7

Formal Methods of Software Design, Eric Hehner, segment 24 page 1 out of 5

In our first lecture on sets and set theory, we introduced a bunch of new symbols and terminology.

CS103 Spring 2018 Mathematical Vocabulary

CSE 105 THEORY OF COMPUTATION

Actually talking about Turing machines this time

MITOCW watch?v=0jljzrnhwoi

We show that the composite function h, h(x) = g(f(x)) is a reduction h: A m C.

CSE143 Notes for Monday, 4/25/11


CS 44 Exam #2 February 14, 2001

Enumerations and Turing Machines

Turing Machines, continued

Theory of Computation Dr. Weiss Extra Practice Exam Solutions

6. Advanced Topics in Computability

Finite Automata Part Three

Computability via Recursive Functions

MITOCW watch?v=yarwp7tntl4

FUNctions. Lecture 03 Spring 2018

MITOCW watch?v=kz7jjltq9r4

Theory of Computation, Homework 3 Sample Solution

Denotational semantics

III. Supplementary Materials

Instructor: Craig Duckett. Lecture 04: Thursday, April 5, Relationships

1. [5 points each] True or False. If the question is currently open, write O or Open.

Introduction to Sets and Logic (MATH 1190)

05. Turing Machines and Spacetime. I. Turing Machines and Classical Computability.

Ambiguous Grammars and Compactification

Assignment 4 CSE 517: Natural Language Processing

Minimum Spanning Trees

Computability and Complexity

Post Experiment Interview Questions

University of Nevada, Las Vegas Computer Science 456/656 Fall 2016

Formal Methods of Software Design, Eric Hehner, segment 1 page 1 out of 5

Extra Practice Problems 2

More on Arrays CS 16: Solving Problems with Computers I Lecture #13

Turing Machine Languages

Theory of Languages and Automata

Outline for Today. How can we speed up operations that work on integer data? A simple data structure for ordered dictionaries.

CS143 Handout 20 Summer 2012 July 18 th, 2012 Practice CS143 Midterm Exam. (signed)

MITOCW watch?v=4dj1oguwtem

Turing Machines. A transducer is a finite state machine (FST) whose output is a string and not just accept or reject.

MITOCW ocw apr k

CS 4349 Lecture August 21st, 2017

MITOCW watch?v=flgjisf3l78

The CS 5 Post ALIEN INVASION!!!

x-fast and y-fast Tries

Finite Automata. Dr. Nadeem Akhtar. Assistant Professor Department of Computer Science & IT The Islamia University of Bahawalpur

CSE Theory of Computing Fall 2017 Project 4-Combinator Project

Exam 2. cs3102: Theory of Computation. Class 20: Busy Beavers

The Turing Machine. Unsolvable Problems. Undecidability. The Church-Turing Thesis (1936) Decision Problem. Decision Problems

CS 275 Automata and Formal Language Theory. First Problem of URMs. (a) Definition of the Turing Machine. III.3 (a) Definition of the Turing Machine

Turing Machine as Transducer. Turing Machines. Computation with Turing Machines. Computation with Turing Machines. Computation with Turing Machines

The Stack, Free Store, and Global Namespace

Graph Theory Part Two

Lecture 6,

CPSC 320 Sample Solution, Playing with Graphs!

Definition 2.8: A CFG is in Chomsky normal form if every rule. only appear on the left-hand side, we allow the rule S ǫ.

Announcements. Problem Set 3 due Friday, October 21. Alternate Midterm Times. Drop by office hours! Ask questions at

Variants of Turing Machines

Parameters and Objects

Compilers. Computer Science 431

Transcription:

Turing Machines Part Two

Recap from Last Time

The Turing Machine A Turing machine consists of three parts: A finite-state control that issues commands, an infinite tape for input and scratch space, and a tape head that can read and write a single tape cell. At each step, the Turing machine writes a symbol to the tape cell under the tape head, changes state, and moves the tape head to the left or to the right.

Input and Tape Alphabets A Turing machine has two alphabets: An input alphabet Σ. All input strings are written in the input alphabet. A tape alphabet Γ, where Σ Γ. The tape alphabet contains all symbols that can be written onto the tape. The tape alphabet Γ can contain any number of symbols, but always contains at least one blank symbol, denoted. You are guaranteed Σ. At startup, the Turing machine begins with an infnite tape of symbols with the input written at some location. The tape head is positioned at the start of the input.

A Simple Turing Machine, R q acc 1, R This This special accept state causes the the machine to to immediately accept. start q 0 q 1 Each Each transition of of the the form 1, R, R x y, y, D q rej means upon reading x, x, replace it it with with symbol y and and move the the tape tape head head in in direction D (which is is either L or or R). R). The The symbol represents the the blank symbol. This This special reject state causes the the machine to to immediately reject.

, R 0 0, R 0 0, L 1 1, L Go to start 1, L Clear a 1 start, R, L 1, R q acc rej Check for 0 0, R Go to end 0 0, R 1 1, R, R q acc

, R Unmark 0, L start, L 1 1, L, R, R 0 0, R 1 1, R 0 0, R Check m 0 1 1, L Back home, R 0, R Next 0 To End, R 0 0, L, R, L Accept!, L 0 0, L 1 1, L Back home 1, L Cross off 1

Main Question for Today: Just how powerful are Turing machines?

A Sample Problem

Composite Numbers A natural number n 2 is composite if it is the product of two natural numbers p and q, neither of which is n. Example: 6 = 3 2 is composite. 42 = 7 6 is composite 5 is not composite. Programming problem: write a method that determines whether a natural number is composite.

bool iscomposite(int n) n) { if if (n (n 1) 1) return false; int divisor = 2; 2; while (divisor n) n) { if if (n (n is is a multiple of of divisor) { return true; } divisor++; } return false; }

Question: Can we build a TM that determines whether a number is composite?

Back to Languages Let Σ = {1} and consider the following language L over Σ: L = { 1 n n is composite } This language is not regular (think about why this is). It's also not context-free (don't worry if you don't see why we didn't discuss this in CS103. ) Can we build a TM for it?

Check for ε 1 1, R Check 1 1, L for 1 start bool iscomposite(int n) n) { if if (n (n 1) 1) return false; int divisor = 2; 2; while (divisor n) n) { if if (n (n is is a multiple of of divisor) { return true; } divisor++; } return false; } 1 1 1 1 1

Check for ε 1 1, R Check 1 1, L for 1 start bool iscomposite(int n) n) { if if (n (n 1) 1) return false; int divisor = 2; 2; while (divisor n) n) { if if (n (n is is a multiple of of divisor) { return true; } divisor++; } return false; } 1 1 1 1 1

Reusing our TMs What does our TM need to be able to do? Check if divisor = n Check if n is a multiple of divisor. What TMs did we build last time? One that checks for 0 n 1 n One that checks for 0 m 1 n, where n is a multiple of m. Coincidence? I think not!

A Sketch of the TM 0 0 1 1 1 1 1 bool iscomposite(int n) n) { if if (n (n 1) 1) return false; int divisor = 2; 2; while (divisor n) n) { if if (n (n is is a multiple of of divisor) { return true; } divisor++; } return false; }

A Sketch of the TM 0 0 1 1 1 1 1 bool iscomposite(int n) n) { if if (n (n 1) 1) return false; int divisor = 2; 2; while (divisor n) n) { if if (n (n is is a multiple of of divisor) { return true; } divisor++; } return false; } Somehow, run run our our TM TM from last last time time to to see see if if the the string is is of of the the form 0 n n 11 n n.

A Sketch of the TM 0 0 1 1 1 1 1 bool iscomposite(int n) n) { if if (n (n 1) 1) return false; int divisor = 2; 2; while (divisor n) n) { if if (n (n is is a multiple of of divisor) { return true; } divisor++; } return false; } Somehow, run run our our TM TM from last last time time to to see see if if the the string is is of of the the form 0 m m 11 n n, where n is is a multiple of of m. m.

A Sketch of the TM 0 0 0 1 1 1 1 1 bool iscomposite(int n) n) { if if (n (n 1) 1) return false; int divisor = 2; 2; while (divisor n) n) { if if (n (n is is a multiple of of divisor) { return true; } divisor++; } return false; }

How can we get our new TM to run our old TMs?

Function Call and Return Think about actual programming for a minute. If you write a function that performs a task, another part of the code can call that function to use the functionality. Could we do something like that here in our TM?

Function Call and Return Space for main Arguments to fn1 Space for fn1 Arguments to fn2 Space for fn2 Internally, the memory for each function call is allocated on the stack: Each call allocates more space. Each return cleans up the space. Arguments are copied by value.

Combining TMs Claim: We can use the TM's tape to simulate function call and return. This means that we can build very complex TMs by assembling smaller subroutines and combining them together. The details are icky; we'll see them in a second.

Our TM, in More Depth 0 0 1 1 1 1 1 High-level idea: Pass the current string as a parameter to to the TM for 0 n 1 n.

Our TM, in More Depth 0 0 1 1 1 1 1 0 0 1 1 1 1 1 High-level idea: Pass the current string as a parameter to to the TM for 0 n 1 n.

Our TM, in More Depth 0 0 1 1 1 1 1 0 0 1 1 1 1 1 Now, go run the TM for 0 n 1 n by transitioning into its start state. It It never takes more than one step off the string in in either direction, so so it it has no idea there's anything else on the tape.

Our TM, in More Depth 1 1 1 0 0 1 1 1 1 1 High-level idea: Clean up the stack space to to return from the function call.

Our TM, in More Depth 1 1 1 0 0 1 1 1 1 1 High-level idea: Clean up the stack space to to return from the function call.

Our TM, in More Depth 0 0 1 1 1 1 1 Now, you can imagine that we do the same thing, but for the TM that checks if if the number of of 1's is is a multiple of of the number of of 0's.

Subroutines in TMs Just as complex programs are often broken down into smaller functions and classes, complex TMs are often broken down into smaller subroutines. Each subroutine performs some task that helps in the overall task. The TM is then described by giving a collection of subroutines and showing how they link up.

The Copy Subroutine This subroutine starts with the tape head at the start of a string of 0s and 1s: 0 0 1 1 1 1 1 It ends in this confguration: 0 0 1 1 1 1 1 0 0 1 1 1 1 1 We use the copy subroutine to let us run another TM on the current input without breaking it.

The Cleanup Subroutine This subroutine starts with the tape head between two characters delimiting TM workspace: 1 0 0 0 0 1 1 1 1 1 It ends in this confguration: 0 0 1 1 1 1 1 We use the cleanup subroutine to recover from the end of running a sub-tm.

Check for ε 1 1, R Check for 1 1 1, L To Front 1 1, L First 0 start Accept! 0, L yes done Multiple Sub no Cleanup Sub Reject! Add 0 Second 0 done Copy Sub done Cleanup Sub no Check 0 n 1 n yes 0 n 1 n done Copy Sub 0, L 0, L

TM Subroutines To represent a subroutine call inside a TM, just add a state with dashed edges with the name of the subroutine. If the subroutine is just a processing subroutine, have a single exit arrow. If the subroutine may exit along different paths, have multiple exit arrows.

Defining a Subroutine If you want to (or are asked to) design a TM subroutine, design it like a normal TM with one change: have special dashed states representing the exit of the subroutine. Check for ε 1 1, R Check for 1 1 1, L 1 1, L To Front done start

Time-Out for Announcements!

CFG Tool We have a tool available online you can use to develop and test out context-free grammars. Features include Autogeneration of strings in the language of your grammar. Automatic testing of whether a specific string can be generated. Doesn't support submission (sorry), but still highly recommended!

Midterm Logistics Midterm is next Thursday, May 21 from 7PM 10PM, location TBA. Cumulative, focusing primarily on topics from PS4 PS6. Covers material up through and including the lecture on CFGs; TMs will not be tested. Same format as last time: four questions, closed-book, closed-computer, open one double-sided 8.5 11 sheet of paper. Alternate exam: 4PM 7PM on Thursday, May 21. Contact us ASAP if you'd like to take the exam at an alternate time. Practice exam next Monday, May 18 th from 7PM 10PM; location TBA.

Extra Practice Problems We've released another set of extra practice problems (Practice Problems 4) to help you review for the exam. We'll release solutions and another set of practice problems on Monday.

I wish my CS Professor had known... We are working to make the CS department here at Stanford a more welcoming environment for all students, especially those who are racial, gender, socioeconomic, or other minority students. To assist with this, we are collecting responses from students that seek to share their experiences in CS courses with the members of the Stanford community. For this project, we are asking students who want to share things they wish their computer science professor knew about them or their own personal experiences in the classroom. Please use the following form to anonymously submit your experience. If you have any questions, comments, or concerns, contact us at ecortes@stanford.edu or vniu@stanford.edu.

Midterms Regraded All midterm regrade requests have been processed. They're available for pickup in the filing cabinet where we normally returned exams. Going forward, please only submit regrades if we deducted points for something that's actually correct. Please don't use regrades as a way to ask for more partial credit.

Your Questions!

I have no idea what all this 'language' stuff has to do with computation. I feel I understand it in isolation, but not in terms of how it ties in with figuring out what impossible problems are, how to find them, or how to even reason about them. We're just about to talk about this hang in in there!

What implications do the limits of computation have on our understanding of the universe? We're also going to to talk a bit bit about that soon. Some teasers: 1. 1. Various problems in in physics, chemistry, and biology can be be solved by by computers, but we we suspect that they can't be be solved efficiently at at the necessary scale. 2. 2. Certain economic models work if if and only if if specific computational problems can be be solved efficiently. 3. 3. There is is a fundamental distinction between something being true and us us being able to to know that it's true.

In powers of 2, how many atoms are in the universe? There are about 10 10 80 80 atoms in in the universe. Let's upper-bound that at at 10 10 100 100. Numbers you should know: 2 10 10 10 10 So 10 10 100 100 2 300 300. 3 3 This means that log ₂ (atoms in in universe) 300.

Back to CS103!

Main Question for Today: Just how powerful are Turing machines?

How Powerful are TMs? Regular languages, intuitively, are as powerful as computers with finite memory. TMs by themselves seem like they can do a fair number of tasks, but it's unclear specifically what they can do. Let's explore their expressive power.

Claim 1: Computers with unbounded memory can simulate Turing machines. Anything that can be done with a TM can also be done with an unboundedmemory computer.

, R 0 0, R 0 0, L 1 1, L start Go to start q₃ 1, L, R Clear a 1q₂, L 1, R q acc r Check for q₀0 0, R Go to end q₁ 0 0, R 1 1, R, R q acc a q₀ q₁ q₂ q₃ 0 q₁ R q r 1 R q a R q₁ 0 R q₁ 1 R q₂ L q r 0 R q₃ L q r R q₃ 0 L q₃ 1 L q₀ R

Simulating a TM To simulate a TM, the computer would need to be able to keep track of the finite-state control, the current state, the position of the tape head, and the tape contents. The tape contents are infinite, but that's because there are infinitely many blanks on both sides. We only need to store the interesting part of the tape (the parts that have been read from or written to so far.)

Claim 2: Turing machines can simulate computers with unbounded memory. Anything that can be done with an unbounded-memory computer can be done with a TM.

What We've Seen TMs can implement loops (basically, every TM we've seen). make function calls (subroutines). keep track of natural numbers (written in unary on the tape). perform elementary arithmetic (equality testing, multiplication, addition, division, etc.). perform if/else tests (different transitions based on different cases).

What Else Can TMs Do? Maintain variables. Have a dedicated part of the tape where the variables are stored. Each variable can be represented as a name (written one symbol at a time) followed by a value. Maintain arrays and linked structures. Divide the tape into different regions corresponding to memory locations. Represent arrays and linked structures by keeping track of the ID of one of those regions.

A CS107 Perspective Internally, computers execute by using basic operations like simple arithmetic, memory reads and writes, branches and jumps, register operations, etc. Each of these are simple enough that they could be simulated by a Turing machine.

A Leap of Faith It may require a leap of faith, but anything you can do a computer (excluding randomness and user input) can be performed by a Turing machine. The resulting TM might be colossal, or really slow, or both, but it would still faithfully simulate the computer. We're going to take this as an article of faith in CS103. If you curious for more details, come talk to me after class.

Just how powerful are Turing machines?

Effective Computation An effective method of computation is a form of computation with the following properties: The computation consists of a set of steps. There are fixed rules governing how one step leads to the next. Any computation that yields an answer does so in finitely many steps. Any computation that yields an answer always yields the correct answer. This is not a formal definition. Rather, it's a set of properties we expect out of a computational system.

The Church-Turing Thesis claims that every effective method of computation is either equivalent to or weaker than a Turing machine. This is not a theorem it is a falsifiable scientific hypothesis. And it has been thoroughly tested! - Ryan Williams

Regular Languages CFLs Problems Solvable by Any Feasible Computing Machine All Languages

Regular Languages CFLs Problems solvable by Turing Machines All Languages

Strings, Languages, Encodings, and Problems

What problems can we solve with a computer? What kind of computer?

What problems can we solve with a computer? What is a problem?

Languages and Problems We've been using formal languages as a way of modeling computational problems. However, the problems we encounter in The Real World don't look at all like language problems. Is this all theoretical nonsense? Or is there a reason for this? In theory, there's no difference between theory and practice. In practice, there is.

Decision Problems A decision problem is a type of problem where the goal is to provide a yes or no answer. Example: checking arithmetic. Given x, y, and z, is x+y=z? Example: detecting relationships. Given a family tree T and people x and y, is x a grandparent of y? Example: avoiding traffic. Given a transportation grid G annotated with traffic information, a start location s, a destination d, and a time limit t, is there a way to get from s to d within time t?

Solving Decision Problems Yes input Computational Device No

Solving Decision Problems Yes input Computational Device How How do do we we represent represent our our inputs? inputs? No

Strings and Objects Think about how my computer encodes the image on the right. Internally, it's just a series of zeros and ones sitting on my hard drive. All data on my computer can be thought of as (suitablyencoded) strings of 0s and 1s.

Strings and Objects A different sequence of 0s and 1s gives rise to the image on the right. Every image can be encoded as a sequence of 0s and 1s, though not all sequences of 0s and 1s correspond to images.

Strings and Objects Let Obj be some discrete, finite object (a string, a video, an image, a text file, etc.) Let Σ be some alphabet. We'll represent an encoding of Obj using the characters in Σ by writing Obj. Think of Obj like a file on disk it encodes complex data as a series of characters. A few remarks about encodings: We don't care how we encode the object, just that we can. The particular choice of alphabet isn't important. Given any alphabet, we can always find a way of encoding things. We'll assume we can perform reasonable operations on encoded objects.

Strings and Objects Given a group of objects Obj₁, Obj₂,, Objₙ, we can create a single string encoding all these objects. Think of it like a.zip file, but without the compression. We'll denote the encoding of all of these objects as a single string by Obj₁,, Objₙ. This lets us feed multiple inputs into our computational device at the same time.

Solving Decision Problems Yes input (some string) Turing Machine No How How do do we we specify specify the the behavior behavior we we want? want?

Specifying a Decision Problem Consider this decision problem: Given x, y, z N, determine whether x+y=z. With our computational model, we'll feed some string into a TM, and it then might come back with an answer (yes or no). Some strings are accepted, some are rejected, and some cause the machine to loop infinitely.

Specifying a Decision Problem Consider this decision problem: Given x, y, z N, determine whether x+y=z. If we give the input as x, y, z, the set of strings the TM should say YES to is { x, y, z x, y, z N and x + y = z } Notice that this is a language it's a set of strings!

Problems and Languages Key intuition: Every language corresponds to some decision problem. Example: { x, y x, y N and x ₃ y } is a language. It corresponds to the following decision problem: Given x, y N, do x and y leave the same remainder when divided by 3?

Problems and Languages Key intuition: Every language corresponds to some decision problem. Example: { D D is a DFA that accepts ε } is a language. It corresponds to the following decision problem: Given a DFA D, does D accept ε?

Problems and Languages Key intuition: Every language corresponds to some decision problem. Example: { G G is a planar graph } is a language. It corresponds to the following decision problem: Given a graph G, is G planar?

Problems and Languages Key intuition: Every decision problem on finite, discrete objects corresponds to some language. If Π is a problem and the inputs to Π are objects x₁, x₂,, xₙ, we can form the language { x₁, x₂,, xₙ the answer to problem Π, given inputs x₁,, xₙ, is yes. }

What All This Means Our goal is to speak of computers solving problems. We will model this by looking at TMs recognizing languages. For decision problems that we're interested in solving, this precisely captures what we're interested in capturing.