Harvard University Computer Science 121 Midterm October 23, 2012

Similar documents
First Midterm Examination

Assignment 1 Automata, Languages, and Computability. 1 Finite State Automata and Regular Languages

Theory of Computation Regular Languages. (NTU EE) Regular Languages Fall / 38

Theory of Computation Regular Languages

CS 373, Spring Solutions to Mock midterm 1 (Based on first midterm in CS 273, Fall 2008.)

First Midterm Examination

Homework 4. 0 ε 0. (00) ε 0 ε 0 (00) (11) CS 341: Foundations of Computer Science II Prof. Marvin Nakayama

Name Ima Sample ASU ID

CS 301. Lecture 04 Regular Expressions. Stephen Checkoway. January 29, 2018

Formal languages, automata, and theory of computation

CS 311 Homework 3 due 16:30, Thursday, 14 th October 2010

Lecture 08: Feb. 08, 2019

Finite-State Automata: Recap

Talen en Automaten Test 1, Mon 7 th Dec, h45 17h30

1. For each of the following theorems, give a two or three sentence sketch of how the proof goes or why it is not true.

5. (±±) Λ = fw j w is string of even lengthg [ 00 = f11,00g 7. (11 [ 00)± Λ = fw j w egins with either 11 or 00g 8. (0 [ ffl)1 Λ = 01 Λ [ 1 Λ 9.

Minimal DFA. minimal DFA for L starting from any other

Grammar. Languages. Content 5/10/16. Automata and Languages. Regular Languages. Regular Languages

Chapter Five: Nondeterministic Finite Automata. Formal Language, chapter 5, slide 1

Fundamentals of Computer Science

Finite Automata Theory and Formal Languages TMV027/DIT321 LP4 2018

Finite Automata-cont d

Homework 3 Solutions

Regular Language. Nonregular Languages The Pumping Lemma. The pumping lemma. Regular Language. The pumping lemma. Infinitely long words 3/17/15

Closure Properties of Regular Languages

NFAs and Regular Expressions. NFA-ε, continued. Recall. Last class: Today: Fun:

1.3 Regular Expressions

CHAPTER 1 Regular Languages. Contents

1 Nondeterministic Finite Automata

CMPSCI 250: Introduction to Computation. Lecture #31: What DFA s Can and Can t Do David Mix Barrington 9 April 2014

FORMAL LANGUAGES, AUTOMATA AND COMPUTABILITY. FLAC (15-453) - Spring L. Blum

AUTOMATA AND LANGUAGES. Definition 1.5: Finite Automaton

NFAs continued, Closure Properties of Regular Languages

1 From NFA to regular expression

CHAPTER 1 Regular Languages. Contents. definitions, examples, designing, regular operations. Non-deterministic Finite Automata (NFA)

More on automata. Michael George. March 24 April 7, 2014

Revision Sheet. (a) Give a regular expression for each of the following languages:

State Minimization for DFAs

Some Theory of Computation Exercises Week 1

Worked out examples Finite Automata

Lexical Analysis Finite Automate

Automata and Languages

1 Structural induction, finite automata, regular expressions

Regular expressions, Finite Automata, transition graphs are all the same!!

Chapter 2 Finite Automata

CS375: Logic and Theory of Computing

Anatomy of a Deterministic Finite Automaton. Deterministic Finite Automata. A machine so simple that you can understand it in less than one minute

12.1 Nondeterminism Nondeterministic Finite Automata. a a b ε. CS125 Lecture 12 Fall 2014

Let's start with an example:

The University of Nottingham SCHOOL OF COMPUTER SCIENCE A LEVEL 2 MODULE, SPRING SEMESTER LANGUAGES AND COMPUTATION ANSWERS

Regular Expressions (RE) Regular Expressions (RE) Regular Expressions (RE) Regular Expressions (RE) Kleene-*

Intermediate Math Circles Wednesday, November 14, 2018 Finite Automata II. Nickolas Rollick a b b. a b 4

Lecture 09: Myhill-Nerode Theorem

Nondeterminism and Nodeterministic Automata

CSE : Exam 3-ANSWERS, Spring 2011 Time: 50 minutes

1.4 Nonregular Languages

3 Regular expressions

Exercises Chapter 1. Exercise 1.1. Let Σ be an alphabet. Prove wv = w + v for all strings w and v.

CS:4330 Theory of Computation Spring Regular Languages. Equivalences between Finite automata and REs. Haniel Barbosa

a,b a 1 a 2 a 3 a,b 1 a,b a,b 2 3 a,b a,b a 2 a,b CS Determinisitic Finite Automata 1

Coalgebra, Lecture 15: Equations for Deterministic Automata

Deterministic Finite Automata

CS 330 Formal Methods and Models

11.1 Finite Automata. CS125 Lecture 11 Fall Motivation: TMs without a tape: maybe we can at least fully understand such a simple model?

Formal Languages and Automata

CS 275 Automata and Formal Language Theory

GNFA GNFA GNFA GNFA GNFA

Non-deterministic Finite Automata

Automata Theory 101. Introduction. Outline. Introduction Finite Automata Regular Expressions ω-automata. Ralf Huuck.

Converting Regular Expressions to Discrete Finite Automata: A Tutorial

Non-Deterministic Finite Automata. Fall 2018 Costas Busch - RPI 1

12.1 Nondeterminism Nondeterministic Finite Automata. a a b ε. CS125 Lecture 12 Fall 2016

CMSC 330: Organization of Programming Languages

Formal Language and Automata Theory (CS21004)

CSCI 340: Computational Models. Kleene s Theorem. Department of Computer Science

Java II Finite Automata I

ɛ-closure, Kleene s Theorem,

NFAs continued, Closure Properties of Regular Languages

Context-Free Grammars and Languages

Chapter 4 Regular Grammar and Regular Sets. (Solutions / Hints)

Compiler Design. Fall Lexical Analysis. Sample Exercises and Solutions. Prof. Pedro C. Diniz

CS103 Handout 32 Fall 2016 November 11, 2016 Problem Set 7

1. For each of the following theorems, give a two or three sentence sketch of how the proof goes or why it is not true.

CISC 4090 Theory of Computation

Types of Finite Automata. CMSC 330: Organization of Programming Languages. Comparing DFAs and NFAs. NFA for (a b)*abb.

Regular Languages and Applications

Non Deterministic Automata. Linz: Nondeterministic Finite Accepters, page 51

PART 2. REGULAR LANGUAGES, GRAMMARS AND AUTOMATA

Homework Solution - Set 5 Due: Friday 10/03/08

SWEN 224 Formal Foundations of Programming WITH ANSWERS

Convert the NFA into DFA

Non-deterministic Finite Automata

Types of Finite Automata. CMSC 330: Organization of Programming Languages. Comparing DFAs and NFAs. Comparing DFAs and NFAs (cont.) Finite Automata 2

Finite Automata. Informatics 2A: Lecture 3. Mary Cryan. 21 September School of Informatics University of Edinburgh

input tape head moves current state

Myhill-Nerode Theorem

1 Structural induction

Kleene s Theorem. Kleene s Theorem. Kleene s Theorem. Kleene s Theorem. Kleene s Theorem. Kleene s Theorem 2/16/15

For convenience, we rewrite m2 s m2 = m m m ; where m is repeted m times. Since xyz = m m m nd jxyj»m, we hve tht the string y is substring of the fir

CSCI 340: Computational Models. Transition Graphs. Department of Computer Science

Transcription:

Hrvrd University Computer Science 121 Midterm Octoer 23, 2012 This is closed-ook exmintion. You my use ny result from lecture, Sipser, prolem sets, or section, s long s you quote it clerly. The lphet is Σ = {, } except where otherwise stted. You hve 80 minutes. The prolems totl 75 points. Use pen nd write your nme on ll lueooks you use. Good luck! PROBLEM 1 (1+1+1+1+1 points) For ech of the following, sy whether it is string, lnguge, or neither. No justifiction necessry. (A) Σ (B) ε (C) (D) { } (E) {ε} (A) Lnguge (B) String (C) Lnguge (D) Neither (E) Lnguge Consider the following NFA N: PROBLEM 2 (3+4+3+5 points), q 1 q 2 q 0 q 3 q 4, (A) Which of the following strings re ccepted y N?,,. (B) Write out the forml 5-tuple for N. (C) Descrie in English the lnguge L(N). (D) Convert the NFA to DFA using the Suset Construction. (You my simply drw the stte digrm of the DFA, omitting unrechle sttes.) (A) Yes, Yes, No. (B) ({q 0, q 1, q 2, q 3, q 4 }, {, }, δ, q 0, {q 2, q 4 }), with δ given y ε q 0 {q 1 } {q 3 } q 1 {q 1, q 2 } {q 1 } q 2 q 3 {q 3 } {q 3, q 4 } q 4 (C) All strings over Σ = {, } of length t lest two where the first nd lst chrcters re the sme.

(D) {q 1 } {q 1, q 2 } {q 0 } {q 3 } {q 3, q 4 } PROBLEM 3 (5 points) For string w {, }, define w to e the string where ll s re replced with s nd vice-vers. For exmple, =. Give context-free grmmr for the lnguge {w {, } : w R = w}. Solution: Denote w s w = σ 1 σ 2 σ n, where σ i Σ = {, }. Then w R = w if nd only if σ i = σ n i+1 for every i. We cn design CFG s follows. G = ({S}, {, }, {S S, S S, S ɛ}, S). The min ide is tht, in ech step, if G genertes σ in the first position, then it must generte σ in the lst position. PROBLEM 4 (4+4+4+4 points) TRUE or FALSE? Justify your nswers in one or two sentences. (A) If L 1 is regulr nd L 2 L 1, then L 2 is regulr (B) L((( ) ) ) (C) There exists lnguge L such tht L is uncountle (D) The grmmr S SS is miguous (A) flse: Let L 1 = Σ nd L 2 = { n n : n N}. We proved in clss tht L 2 is non-regulr, nd L 2 L 1, so the nswer must e flse. (B) flse: We see tht ecuse the regulr expression is of the form ((X) ), where X =, we know tht ny string in L is either ε or it ends in. (C) flse: Every lnguge is countle (shown in clss) nd L is still lnguge.

(D) true: The string hs two different prse trees: S S / \ / \ S S S S / \ / \ S S S S PROBLEM 5 (6+6+6+6 points) For ech of the following lnguges, sy whether it is regulr, context-free, oth, or neither. Briefly justify your nswers. (A) L = { i j : i j = 2012} (B) L = { i j : i + j = 2012} (C) L = { n n n : n 0} (D) L = {w : w contins oth om nd romney s sustrings}, over lphet Σ = {,,..., z} (A) context-free, not regulr. This is very similr to { n n : n 0}. In fct, it is { n+2012 n : n 0}. We cn use this to show tht it is nonregulr: Consider L 1 = { 2012 } nd L 2 = { n n : n < 2012}. Both L 1 nd L 2 re finite nd hence regulr. But L L 1 L 2 = { n n : n 0}, which is known non-regulr lnguge. Becuse the regulr lnguges re closed under conctention nd union, then it must e the cse tht L is not regulr. L is, however, context free. We cn devise grmmr S 2012 T, T T, T ε to produce L. Becuse L cn e generted y context-free grmmr, L is context-free. (B) oth. All strings in this lnguge re 2012 chrcters long. Becuse the lnguge hs lrgest string, tht mens tht the lnguge is finite nd hence regulr. And if it is regulr, then it is lso context-free. (C) neither. It suffices to show tht L is not context-free, s this implies tht it is lso nonregulr. We cn prove tht L is not context-free y n ppliction of the CF Pumping Lemm, similr to the proof tht { n n c n } is not context-free from lecture. Let p e the CF pumping length for L, nd consider the string s = p p p. By the CF pumping lemm, s cn e prtitioned into s = uvxyz where v or y is nonempty nd uv i xy i z L for ll i N. If either v or y contin two different symols, then uv 2 xy 2 z is not of the form, nd hence is not in L. If v nd y ech contin only single symol, then uv 2 xy 2 z will not hve n equl numer of symols in ll three segments (the initil s, the s, nd the finl s), s we will e pumping only one or two of the three segments. This is contrdiction, so L cnnot e CF. (D) oth. L is regulr ecuse it is the intersection of the two regulr lnguges L(Σ omσ ) nd L(Σ romneyσ ), nd the clss of regulr lnguges is closed under intersection. L is context free ecuse every regulr lnguge is context free. PROBLEM 6 (10 points) For string w {, }, define odd(w) to e the string consisting of the symols in odd-numered positions in w. Tht is, if w = w 1 w 2 w 3 w n, then odd(w) = w 1 w 3 w 5 w m, where m is either n or n 1 depending on whether n is odd or even.

Show tht if L is regulr, then so is {w {, } : odd(w) L}. Solution: Suppose L is regulr; then there is DFA M recognizing L. We construct new DFA M s follows: For every old trnsition δ(q 1, σ) = q 2, we crete new stte q, delete the old trnsition, nd crete the trnsitions δ(q 1, σ) = q nd δ(q, σ ) = q 2 for every σ Σ. For exmple: q1 q2 q1 q', q2 Also, if q 2 is n ccept stte, we mke q n ccept stte. This mkes sure tht we ccept oth odd nd even length strings. Now we justify tht this DFA M recognizes L = {w : odd(w) L}. First, if odd(w) L, then there ws n ccepting pth in M on every odd letter in w, so M must hve n ccepting pth on w (since the odd letters go through sttes corresponding to M, nd the even letters cn e nything). Second, if w is ccepted y M, then the odd letters of w must hve followed trnsitions on the sttes corresponding to M, so odd(w) would e ccepted y M, so odd(w) L nd thus w L. Since M is DFA recognizing L, L must e regulr. (Note: mny students ttempted to show tht {odd(w) : w L} is regulr. Unfortuntely, while this is still true, it is trickier to do.) PROBLEM 7 (CHALLENGE! 1 points) Prove tht if R is regulr expression, then L(R) stisfies the pumping lemm with pumping length equl to R +1, where R is the size (or length) of R. (You cn omit the xy p condition.) Solution: Intuitively, if regulr expression R mtches string w with w > R, then Kleene str must hve een used to do the mtching. We cn simply let y e the (nonempty) prt of w tht mtches the Kleene str. Then y n still mtches the Kleene str. Formlly, we prove this y structurl induction on R. Bse cse: If R =, R = ε, or R = σ for some σ Σ, then, since L(R) contins no strings of length t lest R + 1, the result is vcuously true. Induction step: Let R e regulr expression with R > 1. Suppose the result holds for regulr expressions smller thn R. By the definition of regulr expressions, we cn write R in one of the following wys. In ech cse, R 1 nd R 2 re regulr expressions tht re smller thn R nd, using the induction hypothesis, we cn show tht the result holds for R. R = R 1 R 2 : Let w L(R) with w > R. Then either w L(R 1 ) nd w > R 1 or w L(R 2 ) nd w > R 2. In the first cse, we cn write w = xyz with y ε nd xy n z L(R 1 ) L(R) for ll n 0. The other cse is similr. Either wy, the result holds for R.

R = R 1 R 2 : Let w L(R) with w > R. Then we cn write w = w 1 w 2 with w 1 L(R 1 ) nd w 2 L(R 2 ). Either w 1 > R 1 or w 2 > R 2. In the first cse, let w 1 = xyz with y ε nd xy n z L(R 1 ) for ll n 0; then w = xy(zw 2 ) nd xy n zw 2 L(R 1 ) L(R 2 ) = L(R) for ll n 0. The second cse is similr nd, either wy, the result holds for R. R = R1 : If w L(R) nd w ε, then we cn write w = w 1w 2 w k where w i L(R 1 ) {ε} for ll i. Set x = ε, y = w 1, nd z = w 2 w k. Since y L(R 1 ) {ε}, y n L(R 1 ) for ll n 0. Thus xy n z = w1 nw 2 w k L(R1 ) = L(R) nd, hence, the result holds for R. THE END REMEMBER TO PUT YOUR NAME ON YOUR WORK.