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

Similar documents
CSCI 3130: Formal Languages and Automata Theory Lecture 16 The Chinese University of Hong Kong, Fall 2010

Lecture 7: Primitive Recursion is Turing Computable. Michael Beeson

Recursively Enumerable Languages, Turing Machines, and Decidability

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

p x i 1 i n x, y, z = 2 x 3 y 5 z

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

Variants of Turing Machines

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

CS21 Decidability and Tractability

Handout 9: Imperative Programs and State

14.1 Encoding for different models of computation

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

Material from Recitation 1

ECS 120 Lesson 16 Turing Machines, Pt. 2

CISC 4090 Theory of Computation

(a) (4 pts) Prove that if a and b are rational, then ab is rational. Since a and b are rational they can be written as the ratio of integers a 1

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.

4.1 Review - the DPLL procedure

Does this program exist? Limits of computability. Does this program exist? A helper function. For example, if the string program is

Theory of Languages and Automata

Recitation 4: Elimination algorithm, reconstituted graph, triangulation

COMP 382: Reasoning about algorithms

The Satisfiability Problem [HMU06,Chp.10b] Satisfiability (SAT) Problem Cook s Theorem: An NP-Complete Problem Restricted SAT: CSAT, k-sat, 3SAT

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

CS 531: Notes II. January 31, 2014

Introduction to Computers & Programming

1. Draw the state graphs for the finite automata which accept sets of strings composed of zeros and ones which:

1. (a) O(log n) algorithm for finding the logical AND of n bits with n processors

CpSc 421 Final Solutions

1. Suppose you are given a magic black box that somehow answers the following decision problem in polynomial time:

CSE 105 THEORY OF COMPUTATION

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

Functional Programming in Haskell Prof. Madhavan Mukund and S. P. Suresh Chennai Mathematical Institute

CSE 105 THEORY OF COMPUTATION

ISA 562: Information Security, Theory and Practice. Lecture 1

/633 Introduction to Algorithms Lecturer: Michael Dinitz Topic: Priority Queues / Heaps Date: 9/27/17

Treaps. 1 Binary Search Trees (BSTs) CSE341T/CSE549T 11/05/2014. Lecture 19

CS422 - Programming Language Design

Harvard School of Engineering and Applied Sciences CS 152: Programming Languages

Actually talking about Turing machines this time

CS6160 Theory of Computation Problem Set 2 Department of Computer Science, University of Virginia

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

CMPSCI611: The SUBSET-SUM Problem Lecture 18

Solutions to Homework 10

CS 125 Section #10 Midterm 2 Review 11/5/14

Lecture 4: 3SAT and Latin Squares. 1 Partial Latin Squares Completable in Polynomial Time

More Simulations. CS154 Chris Pollett Apr 25, 2007.

ALGORITHMS EXAMINATION Department of Computer Science New York University December 17, 2007

Homework 4 Solutions

Computability and Complexity

Lecture 1 Contracts : Principles of Imperative Computation (Fall 2018) Frank Pfenning

AXIOMS FOR THE INTEGERS

An Interesting Way to Combine Numbers

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

Horn Formulae. CS124 Course Notes 8 Spring 2018

MITOCW watch?v=4dj1oguwtem

Limited Automata and Unary Languages

COS 126 Written Exam 2 (Spring 2015)

Introduction to Algorithms / Algorithms I Lecturer: Michael Dinitz Topic: Approximation algorithms Date: 11/18/14

Linear Time Unit Propagation, Horn-SAT and 2-SAT

Lecture 7: Counting classes

Diagonalization. The cardinality of a finite set is easy to grasp: {1,3,4} = 3. But what about infinite sets?

Enumerations and Turing Machines

Excerpt from "Art of Problem Solving Volume 1: the Basics" 2014 AoPS Inc.

Harvard School of Engineering and Applied Sciences CS 152: Programming Languages

Catalan Numbers. Table 1: Balanced Parentheses

2.8 Universal Turing Machines and the Halting Problem

/633 Introduction to Algorithms Lecturer: Michael Dinitz Topic: Approximation algorithms Date: 11/27/18

Lec-5-HW-1, TM basics

Theory Bridge Exam Example Questions Version of June 6, 2008

Problem One: A Quick Algebra Review

6.001 Notes: Section 4.1

1. Lexical Analysis Phase

CS 374 Fall 2014 Homework 2 Due Tuesday, September 16, 2014 at noon

Note that in this definition, n + m denotes the syntactic expression with three symbols n, +, and m, not to the number that is the sum of n and m.

Lecture 1 Contracts. 1 A Mysterious Program : Principles of Imperative Computation (Spring 2018) Frank Pfenning

arxiv: v1 [cs.cc] 9 Jan 2015

The Resolution Algorithm

On the computability of graph Turing machines

Announcements. Lab Friday, 1-2:30 and 3-4:30 in Boot your laptop and start Forte, if you brought your laptop

Computability Theory

Discrete Mathematics for CS Spring 2008 David Wagner Note 13. An Introduction to Graphs

CSC 220: Computer Organization Unit 12 CPU programming

Week - 01 Lecture - 03 Euclid's Algorithm for gcd. Let us continue with our running example of gcd to explore more issues involved with program.

Lecture 10 Notes Linked Lists

The LC3's micro-coded controller ("useq") is nothing more than a finite-state machine (FSM). It has these inputs:

6. Advanced Topics in Computability

Definition: A context-free grammar (CFG) is a 4- tuple. variables = nonterminals, terminals, rules = productions,,

Insertion Sort: an algorithm for sorting an array

Some Hardness Proofs

1 Linear programming relaxation

CS446: Machine Learning Fall Problem Set 4. Handed Out: October 17, 2013 Due: October 31 th, w T x i w

SECTION A (40 marks)

Phil 320 Chapter 1: Sets, Functions and Enumerability I. Sets Informally: a set is a collection of objects. The objects are called members or

Name: CS 341 Practice Final Exam. 1 a 20 b 20 c 20 d 20 e 20 f 20 g Total 207

Outline. Language Hierarchy

Lecture 2: NP-Completeness

CS152: Programming Languages. Lecture 7 Lambda Calculus. Dan Grossman Spring 2011

COMP MIPS instructions 2 Feb. 8, f = g + h i;

6.001 Notes: Section 6.1

Transcription:

CS 125 Section #4 RAMs and TMs 9/27/16 1 RAM A word-ram consists of: A fixed set of instructions P 1,..., P q. Allowed instructions are: Modular arithmetic and integer division on registers; the standard model for a RAM machine does not have negative numbers, but you can verify that allowing signed integers doesn t chance the power of the model. Bitwise operations on registers. Loading a constant into a register. Transfering values between a register and a word in main memory (as addressed by another register). Conditional GOTO. MALLOC increase the size S of main memory by 1. This also increments the word size if the current word size is too small to address all of main memory. HALT. The program counter l {1,..., q}, telling us where we are in the program. statements, the counter increments after each instruction is executed. The space usage S, which starts at the size of the input. Except for GOTO The word size w; the initial word size is log 2 max(n + 1, k + 1), where n is the length of the input and k is the maximum constant appearing in the program or input. A constant number of registers R[0],..., R[r 1], each of which is a w-bit word. Main memory M[0],..., M[S 1], each of which is a w-bit word. The tuple (l, S, w, R, M) gives the configuration of the word-ram. We say that a word-ram solves a computational problem f : Σ 2 N if the machine which starts with input x halts with some output in f(x) as the set of characters in M[0],..., M[R[0] 1]. As we saw with Turing Machines, there are many adjustments we can make to a model which make it easier to reason about without significantly changing its power. Here s one for word-rams: Exercise. We define a 2-D word-ram model: Suppose that main memory, instead of being represented by a one-dimensional array of size S, is represented by a two-dimensional array of size S S. Consequently, saves or loads from main memory require addressing it using two registers; as before, MALLOC increases S by 1 (and consequently the size of memory by 2S + 1). Prove that a computational problem can be solved by a 2-D word-ram model in polynomial time if and only if it can be solved by regular word-ram model in polynomial time. In this problem, provide a formal description of how to convert between the two models. 1

Solution. ( = ) Intuition: To simulate a 1-D word-ram by a 2-D one, simply ignore all rows but the first, and whenever addressing some word in main memory, let the second index always equal 0. (That is, we keep one extra register in which we initially load the value 0, and then use that register only for the second index.) Formal description: To turn this idea into a formal description, given a 1-D word-ram program P = (P 1,..., P q ) with r registers, we have to give the code for an equivalent 2-D word-ram program that works along the lines discussed above. Define the following equivalent 2-D word-ram program P = (P 0, P 1,..., P q): P has r + 1 registers, and the instructions of P are obtained largely by those of P in the following way (where we go over all possible instruction schemas, using the ordering in the lecture notes on word-rams): 1. Let P 0 be R[r] = 0 2. For i = 0,..., q: (a) If P i is of the form R[i] m, let P i = P i; (b) If P i is of the form R[i] R[j] + R[k], let P i = P i; (c)... (d) If P i is of the form R[i] R[j]/R[k], let P i = P i. (e) If P i is of the form R[i] M[R[j]], let P i (f) If P i is of the form M[R[j]] R[i], let P i (g) If P i is of the form IF R[i] = 0, GOTO l, let P i (h)... be R[i] M[R[r]][R[j]] be M[R[r]][R[j]] R[i]. be IF R[i] = 0, GOTO l + 1. So, apart from GOTO, which we have to change because we added a new row in the beginning, we only change the instructions having to do with memory access, by modifying them so that they act as if the first row of the 2-D word-ram stands for the memory of the 1-D word-ram. Notice that since MALLOC on the 2-D word-ram restricts to the equivalent of MALLOC on the 1-D word-ram on the first row under our identification, no change is required in the way the MALLOC operation works. Running time and correctness: The fact that P is correct follows by a straightforward induction on the number of instructions: after t + 1 instructions of P have been executed, when we restrict the memory of P to the first row and the registers to the first r registers, we re in the same state as after t instructions of P have been executed. For running time, notice that if P takes t steps to halt on some input, P will take exactly t + 1 steps to halt. The wanted time bound follows directly. ( = ) Intuition: To simulate a 2-D word-ram by a 1-D one, we have to be careful about two things: simulating the MALLOC operation, and simulating the indexing into a two-dimensional array by indexing into a one-dimensional one. For the MALLOC operation, we use an extra register to keep track of the value of S; then, when the 2-D word-ram would do a malloc, we instead load 2S + 1 into a second extra register (we may need a third one to hold the constants for this computation), and make that many MALLOCs in our 1-D simulation, decrementing the register as we go. 2

A convenient way to deal with the second problem, meanwhile, when the dimensions S S are fixed, would be to let the entry M 2 [i][j] correspond to the entry M 1 [is + j]. However, S is not fixed in general, so if we want to keep with that nice formula we d have to reorganize the memory contents every time we MALLOC. To avoid this, we can index our 1-dimensional array in another way: let the cell [0][0] correspond to the cell 0, and then, think of expanding the square representing the 2-dimensional memory inductively, by increasing its current sidelength S by one, and appending the new cells [S][0], [S][1],..., [S][S], [S 1][S],..., [0][S] to the 1-dimensional memory in this order. We can write down concrete formulas for this identification: the cell [i][j] is represented by [i 2 + j] if i j and [j 2 + j + (j i)] otherwise. Formal description: We proceed in a way similar to what we did for easy direction; only now things require more careful adjusting. So, to turn our idea into a formal description, given a 1-D word-ram program P = (P 1,..., P q ) with r registers, we have to give the code for an equivalent 2-D word-ram program P that works along the lines discussed above. One trickier thing we have to deal with, and one that our high-level discussion ignored, is GOTO statements: since we ll be substituting single instructions to the 2-D word-ram with blocks of several instructions to the 1-D word-ram, we have to be careful about the numbering of lines in our program. So as we are converting the instructions (of which there are only constantly many, as we re working with a fixed program), we must keep track of how many of each kind we ve seen so far, and with how many instructions we re replacing each, so that we know at which line we are, and to which line we want to get. Since we ll be replacing each instruction to the 2-D word-ram of a given form with the same number of instructions to the 1-D word-ram, this is a very doable task that we can do even before we ve started conversion, just by counting the instructions of various types present in P. We ll assume we ve done that, and suppress the explicit formulas for conversion in the code below, by letting f(l) stand for the function giving the number of line l from the 2-D word-ram program in the 1-D word-ram program, and L stand for the current line number in the 1-D word-ram program (you can think of these functions as of lookup tables). We define the following 1-D word-ram program P : P has r + 7 registers, and the instructions of P are obtained from those of P in the following way (where we go over all possible instruction schemas, using the ordering in the lecture notes on word-rams): 1. Let P 0 2. Let P 1 3. Let P 2 be R[r] R[1] (initialize current memory usage S, as R[1] holds n) be R[r + 5] 0 (keep the constant 0 for ease of control flow) be R[r + 6] 1 (keep the constant 1) 4. For i = 0,..., q: (replace instructions of P by suitable equivalents) (a) If P i is of the form R[i] m, append P i to the current program P ; touch memory, or MALLOC, or GOTO, are left intact) (instructions that don t (b) If P i is of the form R[i] R[j] + R[k], append P i to the current program P (c)... (d) If P i is of the form R[i] R[j]/R[k], append P i to the current program P. 3

(e) If P i is of the form M[R[i]][R[j]] R[k] or R[k] M[R[i]][R[j]], we append the following several instructions to the current program P : i. R[r + 1] R[i] (prepare temporary variables) ii. R[r + 2] R[j] iii. R[r + 3] R[r + 1]/R[r + 2] (test if i < j) iv. IF R[r + 3] = 0, GOTO L + 4 computation) (in this case, i < j, so we skip ahead to the relevant v. R[r + 3] R[r + 1] R[r + 1] (this branch computes R[i] 2 + R[j] vi. R[r + 3] R[r + 3] + R[r + 2] vii. IF R[r + 5] = 0, GOTO L + 5 (this will always be true, as we keep R[r + 5] = 0) viii. R[r + 3] R[r + 2] R[r + 2] ix. R[r + 3] R[r + 3] + R[r + 2] x. R[r + 3] R[r + 3] + R[r + 2] xi. R[r + 3] R[r + 3] R[r + 1] (this branch computes R[j] 2 + R[j] + (R[j] R[i])) xii. M[R[r + 3]] R[k], or R[k] M[R[r + 3]], according to which the form of the command we re replacing. (f) If P i is of the form IF R[i] = 0, GOTO l, append IF R[i] = 0, GOTO f(l) to the current program P. (g) If P i is of the form MALLOC, append the following several instructions to the program: i. R[r + 4] R[r] (copy current memory usage S of the 2-D RAM, and increment it) ii. R[r] R[r] + R[r + 6]. (recall R[r + 6] = 1) iii. R[r + 4] R[r + 4] + R[r + 4] (compute 2S + 1) iv. R[r + 4] R[r + 4] + R[r + 6] v. IF R[r + 4] = 0, GOTO L + 4 (loop to do 2S + 1 MALLOC-s on 1-D word-ram) vi. MALLOC vii. R[r + 4] R[r + 4] R[r + 6] viii. IF R[r + 5] = 0, GOTO L 3 Running time and correctness: Correctness follows in a similar way as before; now the claim is that after t instructions in P, and the corresponding t instructions in P, the two machines are in the same state under our identification between the 2-dimensional memory and the 1-dimensional memory via the formulas we wrote down in the beginning. For running time, each instruction of P is simulated in O(1) instructions of P, except for MALLOC, which takes O(S) instructions, where S is the current sidelength of the square representing the memory of P. Since S can increase by at most 1 for each step of P, S is never more than the running time T (n) of P, hence the running time of P is never more than O(T (n) O(T (n))) = O(T 2 (n)). 4

2 Turing Machines Formally, a Turing machines is a 6-tuple M = (Q, Σ, Γ, δ, q 0, q halt ), where Q is a finite set of states. This means you can use your states to do finite counting (e.g. modulo 2), but not counting to arbitrarily large numbers. Sometimes, instead of having a single q halt state, people will talk about having a q reject and q accept state. Σ is the input alphabet. Γ is the tape alpabet, with Σ Γ and Γ Σ. δ : Q Γ Q Γ {L, R} is the transition function. As we saw in class, we can simulate a multiple tape Turing machine with a single tape ; in this case, if we have k tapes, the transition function is δ : Q Γ k Q Γ k {L, R} k. We ll often also allow an S for staying still; this does not change the power of the TM. q 0, q accept, q reject Q are the start state, accept state, and reject state. respectively A Turing machine configuration is a string uqv Γ QΓ that encodes (i) the state q of M, (ii) the tape contents uv ignoring trailing blanks, and (iii) the location of the head within the tape. The starting configuration is q 0 x. We say that a Turing Machine solves a computational problem f : Σ 2 (Γ/ ) if the Turing machine which starts with input x halts with some output in f(x) as the set of characters before the first. The extended Church-Turing Thesis says that every reasonable model of computation can be simulated on a Turing machine with only a polynomial slowdown. Exercise. Give the state diagram for a Turing machine which converts unary to binary (that is, given the input 1 x, it should return the binary representation of x). Solution. We give a two-tape Turing machine, which does the following: Adds a special start symbol $ to the beginning of the tape and shifts the input 1 character right (q 0 q S... q S q 1 ). Going left until it hits the $, counts the number of 1 s by incrementing on the second tape, keeping the answer written in reverse on the second tape (q 1 q 2 q 3 q 1 ). Each time it returns to q 1, the head of the second tape is at the first character after the $. It removes the $ at the start of the first tape and scrolls the head of the second tape to the end of its string (q 1 q m q r ). It copies the second tape backwards onto the first tape, halting when it reaches the $ at the start of the second tape (q r q halt ). 5

, Γ,, L, L q halt, $,, S, S 1, Γ 1,, R, S, Γ, Γ, S, R q 0 1, Γ $, $, R, R q S q 1 q m, Γ 1,, S, S $, Γ, Γ, L, R,,, S, L q r 1, Γ, Γ, L, S Γ, $ Γ, $, S, R,,, R, L q 2 q 3 Γ, 0 or Γ, 1, S, L Γ, 1 Γ, 0, S, R Γ, Γ $ Γ, Γ $, S, L The runtime of this machine is O(n log n), where n is the length of the input. Note that in the diagram above, we have omitted impossible arrows, e.g. ones that the machine will never follow given a valid input. Often, when we talk about computational problems, we talk about decision problems, where the output is in {0, 1}; this is the same idea as deciding whether the given string is in some language L Σ. However, in many cases the problem of finding a solution is not necessarily more difficult than the problem of determining whether there exists one. You re asked to prove a few of these on your homework; we ll look at one more of them here. Exercise. Show that there is a polynomial-time algorithm for 3-Satisfiability (given a boolean formula in 3-CNF form, determine whether there exists a satisfying assignment) if and only if the computational problem of finding a satisfying assignment (or returning nothing if none exists) is in P. For this problem, you can use high-level algorithm descriptions. Solution. ( = ) Clearly, if we can find a satisfying assignment, then we can determine whether there exists one. ( = ) Now suppose we have an algorithm A that decides in polynomial time whether a given 3-CNF formula φ has a satisfying assignment. How can we use A to find such an assignment? The idea is to set the variables one by one, and make sure that the remaining formula still has a satisfying assignment along the way. Algorithm: So, let φ i1,...,i t stand for the formula φ with partial assignment x 1 = i 1,..., x t = i t ; then φ i1,...,i t can easily be written in 3-CNF form (why? there is a detailed explanation in the more formal solution given below). If A(φ) returns FALSE, we return nothing. Otherwise, we run A(φ 1 ). If that gives 6

TRUE, we assign x 1 = 1 and repeat with the formula φ 1 instead of φ; if it gives FALSE, it must be the case that A(φ 0 ) is TRUE, so we repeat with φ 0 instead of φ. In the end we have a formula φ i1,...,i n which is satisfiable, but is also a constant, therefore x j = i j gives a satisfying assignment. Correctness and running time: From the above discussion, correctness is clear. The running time will be O(nT (n)) where T (n) is the running time of the decider for satisfiability, hence it is polynomial. More detail: TM implementation. Here s a more low-level solution to give you more practice with TMs: Suppose that we have some Turing Machine M, which, given an input, determines whether there exists a satisfying assignment. We go about actually finding a satisfying assignment as follows: Simulate M on the input (after copying the input to a second tape); if M returns false, then halt and return nothing. Now, try setting x 1 = 1 in the formula, e.g. copy the formula onto a second tape, removing any clauses which are satisfied (i.e. contain x 1 ) and removing any instances of x 1. If the latter results in any empty clauses, e.g. saying that this is not a satisfying assignment, set x 1 = 0. Run M on the output of the previous step; if M returns true, then set x 1 = 1 in the original copy of the formula. Otherwise, set x 1 = 0. Repeat the previous two steps with x 2,..., x n in sequence. solution. The final result will be a satisfying Here, we see that we simulate M at most n times, and that the length of the input to M is no more than the length of the input to our original problem. Meanwhile, all the other steps (copying the formula, setting the value of variable) take no more than linear time and run only polynomially many times, so they also run in polynomial time. Hence, this entire algorithm runs in polynomial time. There are many details that we don t address in the solution above because we are working at a higher level (e.g. clearing a tape so that we can reuse it in multiple simulations of M); however, you should convince yourself that all of these are doable, and none of these have a significant affect on the runtime. 7