Final Exam 1, CS154. April 21, 2010

Similar documents
Final Exam 2, CS154. April 25, 2010

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

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

Theory of Computation Dr. Weiss Extra Practice Exam Solutions

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

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

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

Theory Bridge Exam Example Questions Version of June 6, 2008

CS 44 Exam #2 February 14, 2001

To illustrate what is intended the following are three write ups by students. Diagonalization

R10 SET a) Construct a DFA that accepts an identifier of a C programming language. b) Differentiate between NFA and DFA?

CS164: Midterm I. Fall 2003

Theory of Computations Spring 2016 Practice Final Exam Solutions

Computer Sciences Department

1 Parsing (25 pts, 5 each)

Recursively Enumerable Languages, Turing Machines, and Decidability

Regular Languages (14 points) Solution: Problem 1 (6 points) Minimize the following automaton M. Show that the resulting DFA is minimal.

Turing Machine Languages

Theory of Programming Languages COMP360

QUESTION BANK. Formal Languages and Automata Theory(10CS56)

Theory of Computation, Homework 3 Sample Solution

CpSc 421 Final Solutions

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

CS402 - Theory of Automata Glossary By

CS/ECE 374 Fall Homework 1. Due Tuesday, September 6, 2016 at 8pm

CT32 COMPUTER NETWORKS DEC 2015

Problems, Languages, Machines, Computability, Complexity

Finite Automata Theory and Formal Languages TMV027/DIT321 LP4 2018

Introduction to Automata Theory. BİL405 - Automata Theory and Formal Languages 1

Multiple Choice Questions

Outline. Language Hierarchy

(a) R=01[((10)*+111)*+0]*1 (b) ((01+10)*00)*. [8+8] 4. (a) Find the left most and right most derivations for the word abba in the grammar

CSC-461 Exam #2 April 16, 2014

Finite Automata Theory and Formal Languages TMV027/DIT321 LP4 2016

From Theorem 8.5, page 223, we have that the intersection of a context-free language with a regular language is context-free. Therefore, the language

Midterm I (Solutions) CS164, Spring 2002

Actually talking about Turing machines this time

Skyup's Media. PART-B 2) Construct a Mealy machine which is equivalent to the Moore machine given in table.

Lecture 5: The Halting Problem. Michael Beeson

Structure of Programming Languages Lecture 3

Homework 1 Due Tuesday, January 30, 2018 at 8pm

CISC 4090 Theory of Computation

Midterm Exam. CSCI 3136: Principles of Programming Languages. February 20, Group 2

Reflection in the Chomsky Hierarchy

Context-Free Languages & Grammars (CFLs & CFGs) Reading: Chapter 5

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

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

1. (10 points) Draw the state diagram of the DFA that recognizes the language over Σ = {0, 1}

6.045J/18.400J: Automata, Computability and Complexity. Practice Quiz 2

t!1; R 0! 1; R q HALT

PS3 - Comments. Describe precisely the language accepted by this nondeterministic PDA.

14.1 Encoding for different models of computation

Draw a diagram of an empty circular queue and describe it to the reader.

CS52 - Assignment 10

Compiler Construction

Decidable Problems. We examine the problems for which there is an algorithm.

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

Lexical Analysis. COMP 524, Spring 2014 Bryan Ward

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

Complexity Theory. Compiled By : Hari Prasad Pokhrel Page 1 of 20. ioenotes.edu.np

Languages and Automata

Today, we will dispel the notion that Java is a magical language that allows us to solve any problem we want if we re smart enough.

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

III. Supplementary Materials

(Refer Slide Time: 0:19)

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

CSE 105 THEORY OF COMPUTATION

COMPUTABILITY THEORY AND RECURSIVELY ENUMERABLE SETS

The Big Picture. Chapter 3

CSE 105 THEORY OF COMPUTATION

Automata Theory TEST 1 Answers Max points: 156 Grade basis: 150 Median grade: 81%

UNIT I PART A PART B

COMP Logic for Computer Scientists. Lecture 25

CS/B.Tech/CSE/IT/EVEN/SEM-4/CS-402/ ItIauIafIaAblll~AladUnrtel1ity

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.

Intermediate Algebra. Gregg Waterman Oregon Institute of Technology

CS402 Theory of Automata Solved Subjective From Midterm Papers. MIDTERM SPRING 2012 CS402 Theory of Automata

COMP 330 Autumn 2018 McGill University


Turing Machines, continued

Ambiguous Grammars and Compactification

Last lecture CMSC330. This lecture. Finite Automata: States. Finite Automata. Implementing Regular Expressions. Languages. Regular expressions

CS311 / MATH352 - AUTOMATA AND COMPLEXITY THEORY

CPSC 121 Some Sample Questions for the Final Exam Tuesday, April 15, 2014, 8:30AM

Introduction to the Theory of Computation, Sipser, PWS, ISBN X, 1996

YOUR NAME PLEASE: *** SOLUTIONS ***

Problem Set 1. Solution. CS4234: Optimization Algorithms. Solution Sketches

Material from Recitation 1

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

Chapter 3. Set Theory. 3.1 What is a Set?

ECS 120 Lesson 16 Turing Machines, Pt. 2

CSE450. Translation of Programming Languages. Lecture 20: Automata and Regular Expressions

Theory of Computation

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

CS21 Decidability and Tractability

THEORY OF COMPUTATION

CS 164 Programming Languages and Compilers Handout 9. Midterm I Solution

for (i=1; i<=100000; i++) { x = sqrt (y); // square root function cout << x+i << endl; }

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

CS61A Lecture 38. Robert Huang UC Berkeley April 17, 2013

Transcription:

Final Exam 1, CS154 April 21, 2010 Exam rules. The exam is open book and open notes you can use any printed or handwritten material. However, no electronic devices are allowed. Anything with an on-off switch must be turned off. There is one exception: you can use a music player as long as nobody else can hear it and you just put it on shuffle and do not use its controls during the exam. About the problems. These problems are similar to your homework problems. The same conventions about alphabets, etc. that were used on the homework will apply here. Scoring. Ten problems, 10 points each, total 100 points. 1. Give a regular expression matching exactly those strings accepted by the following NFA: 1

Answer: 2

2. Convert the following NFA to an equivalent DFA: Answer: 3

3. Indicate which of the following tasks can be done with a finite automaton (FA), and which can be done with a PDA but not with a FA. Label each choice with FA or PDA. In each example, valid just means, has the right form for, e.g. a valid U.S. phone number has a certain form involving digits, parentheses, and dashes, regardless of whether it actually connects to a working telephone; a valid credit card number satisfies certain relations between the digits but does not have to belong to an individual with good credit. Do not attempt to explain your answers. (a) recognizing whether a string is a syntactically correct (compilable) Java program. (b) recognizing whether a string is a valid SJSU course identifier (such as CS 154). Here valid means that the department exists, but not necessarily that the course number represents a real course; but the course number must be between 0 and 300. (c) recognizing whether a string is a valid U.S. phone number. (d) recognizing whether a string is a valid U.S. postal code (ZIP code); for example 95003 and 95003-4612 are both valid. (e) recognizing whether a string is a valid social security number Answer: (a) requires a PDA, but the other four can be done with an FA. The point is the same for all four: the legal strings have a fixed maximum length, so we can remember what has been seen so far in a state, and carry out any computation at all based on what has been seen so far. An alternative but less practical reason is that the sets of strings in question are all finite, and in principle every finite set is regular, but you wouldn t want to check for validity that way! 4. Convert the following grammar to an equivalent PDA: S asbb SS ɛ Answer: The start state is 0, the only accepting state is 1. The rules are as follows 0, ɛ, ɛ 1, S 1, ɛ, S ɛ, asbb 1, ɛ, S ɛ, SS 1, ɛ, S ɛ, ɛ 1, a, a 1, ɛ 1, b, b 1, ɛ 4

5. A restaurant menu can be thought of as a single string, which contains some newline characters. It is composed of a restaurant name, followed by a newline, followed by sections, such as Appetizers, Soups, Salads, Entrees, Desserts, and Drinks. Each section has a title, followed by a newline and then a newline-separated list of items, ending in another newline. A section must have at least one item in it, and every menu must have at least one section. Each item is composed of a tab character, then the name of the item, optionally followed by more information in parentheses, another tab character, and a price. The price has to end in.95, but before the price can come one or two digits (not three-let s not even think about such prices). For example, here is a short menu following these rules: Luigi s Chicago Restaurant Appetizers Pakora (deep-fried vegetables in batter) 4.95 Ginger sushi (with pink ginger) 6.95 Soups Minestrone 5.95 In this problem you are asked to give a grammar based on the rules given in English above, that generates legal menus. The item names and parenthetical information can be any non-empty strings composed of letters. Answer: The start symbol is Menu. The rules are M enu RestaurantN ame N ewline SectionList SectionList Section SectionList ɛ Section SectionT itle N ewline ItemList ItemList Item Newline ItemList Newline Item T ab ItemName P rice T ab ItemName (Info) P rice P rice Digit Digit.95 Digit.95 ItemName String Info String RestaurantN ame String SectionT itle String String Letter Letter String T ab \t Newline \n 5

6. For α a string of a s and b s, let f(α) be the string α with a changed to b and b changed to a. For example f(aab) = bba. Please use the pumping lemma to prove that the set L of αf(α) such that α is any string of a s and b s is not regular. For example, aabbba is in L because the last half, bba, is f of the first half. Common mistake to avoid: Your proof will be (completely) wrong if it contains variables that are introduced out of thin air, such as mentioning a letter k without saying where it came from or what it means. Answer: Assume, for proof by contradiction, that L is regular. Let N be given by the pumping lemma. Choose w = a N b N. Then w is in L. According to the pumping lemma we have w = xyz with xy N and y 1 and xy j z L for j = 0, 1, 2,.... Since xy N and the first N characters of w are a, we must have y = a k, and k > 0 since y 1. Then xy 2 z = a N+k b N, which must belong to L by the pumping lemma, but which does not belong to L since its first half contains no b but its last half does contain an a. That completes the proof. 7. Please write a Turing machine (program) that erases the middle character of its input, if the input is of odd length, and terminates without changing the input, if the input is of even length. For example, if started with aaaabbbb, it terminates without changing the input, if started with aaaabaaaa it terminates with aaaaaaaa on the tape. Please comment your code so it won t take me all day to figure it out. If you do not comment your code intelligibly, you will not get credit. Assume the input alphabet is {a, b}. Answer: 0,a->1,A,R 0,b->1,B,R 1,a->1,a,R 1,b->1,b,R 1,_->2,_,L 1,A->2,a,L 1,B->2,b,L 2,a->3,A,L 2,b->3,B,L 2,A->9,_,H 3,a->3,a,L 3,b->3,b,L 3,A->0,a,R 3,B->0,b,R 0,A->9,a,H 0,B->9,b,H // leave a marker to mark our place in the first half, // and remember what was under the marker // move right to end or to marker // back up to last character // replace previous marker with original character // leave right marker // that s the middle character so erase it (and stop) // move left // replace marker with original character, move right // and loop to the beginning // even length string, replace original character and halt 6

8. True or false: (a) It s impossible to write a Java program that takes Java programs e as inputs, and produces the same output as program e would produce. (Here the output of a Java program is what, if anything, it prints to the console.) False. We can write a program in Java that simulates the operation of a Turing machine supplied as input. Indeed there is such a program (but in C#) in the textbook. (b) It s impossible to write a Java program that takes Turing machines e as inputs, and prints out 1 if Turing machine e halts (with no input) and 0 if it does not. True. This is the unsolvability of the halting problem. (c) It s impossible to write a Java program that takes Java programs e as inputs, and prints out 1 if program e would eventually print something to the console, and 0 if not. True. How long should we wait before deciding that it s never going to print? Actually, we can prove the impossibility by reducing the halting problem to this set: Define, for programs e, the program F (e) that first does what e does, but suppressing any printout, and then, if and when e halts, it prints done. Now e halts if and only if F (e) prints something. Hence the set in this question is not recursive. (d) It s impossible to write a Turing machine that would take Java programs e as inputs, and produce the same output as program e would produce. False. We can write a Java interpreter in Turing machine language. There are some technicalities involved here but the principle is simply the Church- Turing thesis, that whatever can be computed can be computed on a Turing machine. (e) It s impossible to write a Java program that takes as input a Java program e and a number k, and outputs 1 if program e (without input) outputs k (before printing anything else, although it may go on outputting other characters after printing the digits of k), and 0 otherwise. True. Again, how long should we wait before deciding that e is not going to output k? The argument given under part (c) can easily be adapted, replacing done by k. 7

9. Please classify the following sets as either recursive or r.e. (recursively enumerable), as in the homework. You do not have to justify or explain your answers. (a) The set of doubled strings, i.e. those of the form αα for some α. (Recursive). (b) The set of strings rejected by a particular finite automaton. (Recursive). (c) The set of strings accepted by a particular Turing machine (assume here that I have listed a machine with several thousand instructions that I do not expect you to read and analyze). (Recursively enumerable, and for some Turing machines, not recursive. (d) The set of strings of digits α that occur somewhere in the decimal expansion of π. For example, the string 4159 occurs, since π starts out 3.1415923. (Recursively enumerable. Whether the set is actually recursive or not is beyond current mathematical knowledge.) (e) The set of Java programs that produce at least 1000 characters of output to the console. (Recursively enumerable; this set is certainly not recursive, as discussed in the previous question.) 8

10. Consider the Partition Problem. The input is a finite set of positive integers. The problem is to determine of a set A whether it can be partitioned into two disjoint sets B and C such that the sum of the elements in B is equal to the sum of the elements in C. For example, if A = {1, 3, 5, 7}, we could take B = {3, 5} and C = {1, 7}, but if A = {1, 3, 5, 78}, it is not possible to partition A as required, since whichever set contains 78 will have too large a sum. Note, numbers are represented in ordinary decimal notation for purposes of this problem. Is this problem in P, or is it in N P, or is it in Co-N P? Briefly explain your answer. Answer: It is in N P, since we can have a non-deterministir Turing machine generate a subset B, then (deterministically and in polynomial time) compute the sum of the numbers in B, the sum of the numbers in A but not in B, and see if they are equal. 9