Homework 3 Solutions

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

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

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.

Some Theory of Computation Exercises Week 1

AUTOMATA AND LANGUAGES. Definition 1.5: Finite Automaton

Let's start with an example:

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

1 Nondeterministic Finite Automata

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

Deterministic Finite Automata

CHAPTER 1 Regular Languages. Contents

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

Harvard University Computer Science 121 Midterm October 23, 2012

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

First Midterm Examination

Minimal DFA. minimal DFA for L starting from any other

1 From NFA to regular expression

input tape head moves current state

CS103B Handout 18 Winter 2007 February 28, 2007 Finite Automata

Worked out examples Finite Automata

NFAs continued, Closure Properties of Regular Languages

Finite Automata-cont d

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

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

Designing finite automata II

CS 330 Formal Methods and Models

Convert the NFA into DFA

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

Chapter 2 Finite Automata

Name Ima Sample ASU ID

First Midterm Examination

Lecture 08: Feb. 08, 2019

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.

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

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

Converting Regular Expressions to Discrete Finite Automata: A Tutorial

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

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

Formal languages, automata, and theory of computation

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

3 Regular expressions

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

Formal Languages and Automata

CS 330 Formal Methods and Models

CMSC 330: Organization of Programming Languages

CSE396 Prelim I Answer Key Spring 2017

NFAs continued, Closure Properties of Regular Languages

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

CS415 Compilers. Lexical Analysis and. These slides are based on slides copyrighted by Keith Cooper, Ken Kennedy & Linda Torczon at Rice University

Theory of Computation Regular Languages

Nondeterminism and Nodeterministic Automata

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

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

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.

Finite-State Automata: Recap

Lecture 3: Equivalence Relations

SWEN 224 Formal Foundations of Programming WITH ANSWERS

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

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

Lecture 09: Myhill-Nerode Theorem

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

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

Nondeterminism. Nondeterministic Finite Automata. Example: Moves on a Chessboard. Nondeterminism (2) Example: Chessboard (2) Formal NFA

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

Fundamentals of Computer Science

State Minimization for DFAs

Table of contents: Lecture N Summary... 3 What does automata mean?... 3 Introduction to languages... 3 Alphabets... 3 Strings...

1.4 Nonregular Languages

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

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

Thoery of Automata CS402

Coalgebra, Lecture 15: Equations for Deterministic Automata

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

Languages & Automata

CISC 4090 Theory of Computation

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

Non-deterministic Finite Automata

Scanner. Specifying patterns. Specifying patterns. Operations on languages. A scanner must recognize the units of syntax Some parts are easy:

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

Java II Finite Automata I

NFA DFA Example 3 CMSC 330: Organization of Programming Languages. Equivalence of DFAs and NFAs. Equivalence of DFAs and NFAs (cont.

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

Midterm 1 Practice. CS 350 Fall 2018 gilray.org/classes/fall2018/cs350/

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

Closure Properties of Regular Languages

Non-deterministic Finite Automata

FABER Formal Languages, Automata and Models of Computation

378 Relations Solutions for Chapter 16. Section 16.1 Exercises. 3. Let A = {0,1,2,3,4,5}. Write out the relation R that expresses on A.

CMSC 330: Organization of Programming Languages. DFAs, and NFAs, and Regexps (Oh my!)

CS 310 (sec 20) - Winter Final Exam (solutions) SOLUTIONS

Finite Automata Theory and Formal Languages TMV027/DIT321 LP4 2018

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

ɛ-closure, Kleene s Theorem,

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

CS 330 Formal Methods and Models Dana Richards, George Mason University, Spring 2016 Quiz Solutions

Lexical Analysis Finite Automate

Bases for Vector Spaces

Solutions Problem Set 2. Problem (a) Let M denote the DFA constructed by swapping the accept and non-accepting state in M.

CS375: Logic and Theory of Computing

Parse trees, ambiguity, and Chomsky normal form

Transcription:

CS 341: Foundtions of Computer Science II Prof. Mrvin Nkym Homework 3 Solutions 1. Give NFAs with the specified numer of sttes recognizing ech of the following lnguges. In ll cses, the lphet is Σ = {,1}. () The lnguge {w Σ w ends with } with three sttes.,1 1 2 3 () The lnguge {w Σ w contins the sustring 11, i.e., w = x11y for some x,y Σ } with five sttes.,1,1 1 1 1 2 3 4 5 (c) The lnguge {w Σ w contins t lest two s, or exctly two 1s} with six sttes.,1 1,1 ε 2 3 4 1 1 5 1 6 (d) The lnguge {ε} with one stte. 1

1 (e) The lnguge 1 with three sttes. 1 ε 1 2 3 2. () Show y giving n exmple tht, if M is n NFA tht recognizes lnguge C, swpping the ccept nd non-ccept sttes in M doesn t necessrily yield new NFA tht recognizes C. Answer: The NFA M elow recognizes the lnguge C = {w Σ w ends with }, where Σ = {,1}.,1 1 2 3 Swpping the ccept nd non-ccept sttes of M gives the following NFA M :,1 1 2 3 Note tht M ccepts the string 1 C = {w w does not end with }, so M does not recognize the lnguge C. () Is the clss of lnguges recognized y NFAs closed under complement? Explin your nswer. Answer: The clss of lnguges recognized y NFAs is closed under complement, which we cn prove s follows. Suppose tht C is lnguge recognized y some NFA M, i.e., C = L(M). Since every NFA hs n equivlent DFA (Theorem 1.39), there is DFA D such tht L(D) = L(M) = C. By prolem 3 on Homework 2, we then know there is nother DFA D tht recognizes the lnguge L(D). Since 2

every DFA is lso n NFA, this then shows tht there is n NFA, in prticulr D, tht recognizes the lnguge C = L(D). Thus, the clss of lnguges recognized y NFAs is closed under complement. 3. Use the construction given in Theorem 1.39 to convert the following NFA N into n equivlent DFA. ε 1 2 3, Answer: Let NFA N = (Q,Σ,δ,1,F), where Q =, Σ = {,}, 1 is the strt stte, F = {2}, nd the trnsition function δ s in the digrm of N. To construct DFA M = (Q,Σ,δ,q,F ) tht is equivlent to NFA N, first we compute the ε-closure of every suset of Q =. Then define Q = P(Q), so Set R Q ε-closure E(R) {1} {2} {2} {3} {3} {1,3} {2,3} {2,3} Q = {, {1}, {2}, {3},, {1,3}, {2,3}, }. The strt stte of M is then E({1}) =. The set of ccept sttes of M is F = { {2},, {2,3}, }. We define the trnsitions in the DFA M s in the following digrm: 3

{2,3}, Note tht we left out some of the sttes (e.g., {1}) in P(Q) from our digrm of the DFA M since they re not ccessile from the strt stte. Also, we hd to dd n rc from stte to itself lelled with, so tht this stte hs n rc leving it corresponding to ech symol in the lphet Σ, which is requirement for ny DFA. The lgorithm given in the notes nd textook will lwys correctly construct n equivlent DFA from given NFA, ut we don t lwys hve to go through ll the steps of the lgorithm to otin n equivlent DFA. For exmple, on this prolem, we egin y figuring out wht sttes the NFA cn e in without reding ny symols. In this cse, this is E({1}) = since 1 is the strting stte of the NFA, nd the NFA cn jump from 1 to 2 without reding ny symols y tking the ε-trnsition. Thus, we first crete DFA stte corresponding to the set : The stte is the strt stte of the DFA since this is where the NFA cn e without reding ny symols. The stte is lso n ccepting stte for the DFA since it contins 2, which is ccepting for the NFA. Now for DFA stte, determine where the NFA cn go on n from ech NFA stte within this DFA stte, nd where the NFA cn go on from ech NFA stte within this DFA stte. On n, the NFA cn go from stte 1 to stte 3; lso, the NFA cn go from stte 2 to 1, nd then it lso cn go further from 1 to 2 on the ε. So from NFA sttes 1 nd 2 on n, the NFA cn end up in sttes 1, 2, nd 3, so drw trnsition in the DFA from stte to new stte, which is n ccepting stte since it contins 2 F: 4

Similrly, to determine where the DFA moves on from DFA stte, determine ll the possiilities of where the NFA cn go from NFA sttes 1 nd 2 on. From stte 1, the NFA cn t go nywhere on ; lso, the NFA cn t go nywhere from stte 2 on. Thus, the NFA cn t go nywhere from sttes 2 nd 3 on, so we dd -edge in the DFA from stte to new DFA stte, which is not ccepting since it contins no ccept sttes of the NFA: Now every time we dd new DFA stte, we hve to determine ll the possiilities of where the NFA cn go on n from ech NFA stte within tht DFA stte, nd where the NFA cn go on from ech NFA stte within tht DFA stte. For DFA stte, we next determine where the NFA cn go on n from ech of the NFA sttes 1, 2 nd 3. From NFA stte 1, the NFA on n cn go to NFA stte 3; from NFA stte 2, the NFA on n cn go to NFA stte 1, nd then it cn lso further jump to 2 on ε; from NFA stte 3, the NFA on n cn go to NFA stte 2. Thus, if the NFA is in sttes 1, 2 nd 3, it cn go on n to sttes 1, 2 nd 3, so we dd to the DFA n -edge from to. Now we determine where the -edge from DFA stte goes to. To do this, we exmine wht hppens to the NFA from sttes 1, 2 nd 3 on. If the NFA is in stte 1, then there is nowhere to go on ; if the NFA is in stte 2, then there is nowhere to go on ; if the NFA is in stte 3, then the NFA cn go to 2 or 3 on. Hence, if the NFA is in sttes 1, 2 nd 3, the NFA on cn end in sttes 2 nd 3. Thus, in the DFA, drw n edge from stte to new stte {2,3}, which is ccepting since it contins 2 F: 5

{2,3} Now do the sme for DFA sttes {2,3} nd. If ny new DFA sttes rise, then we need to determine the nd trnsitions out of those sttes s well. We stop once every DFA stte hs n -trnsition nd -trnsition out of it. Accepting sttes in the DFA re ny DFA sttes tht contin t lest one ccepting NFA stte. We eventully end up with the DFA elow s efore: {2,3}, FortheDFAstte,thererenoversionsoftheNFAcurrentlyctive, i.e., ll threds hve crshed, so the NFA cnnot proceed nd the input string will not e ccepted. However, ccording to the definition of DFA, ech stte must hve edges leving it corresponding to ech symol in the lphet Σ. Thus, we dd loop from the DFA stte ck to itself leled with Σ, which in our cse is,. 4. Give regulr expressions tht generte ech of the following lnguges. In ll cses, the lphet is Σ = {,}. 6

() The lnguge {w Σ w is odd}. Answer: ( )(( )( )) () The lnguge {w Σ w hs n odd numer of s}. Answer: ( ) (c) The lnguge {w w contins t lest two s, or exctly two s}. Answer: ( ) (d) The lnguge {w Σ w ends in doule letter}. (A string contins doule letter if it contins or s sustring.) Answer: ( ) ( ) (e) The lnguge {w Σ w does not end in doule letter}. Answer: ε ( ) ( ) (f) The lnguge {w Σ w contins exctly one doule letter}. For exmple, hs exctly one doule letter, ut hs two doule letters. Answer: (ε )() () (ε ) (ε )() () (ε ) 5. Suppose we define restricted version of the Jv progrmming lnguge in which vrile nmes must stisfy ll of the following conditions: A vrile nme cn only use Romn letters (i.e.,,,..., z, A, B,..., Z) or Aric numerls (i.e.,, 1, 2,..., 9); i.e., underscore nd dollr sign re not llowed. A vrile nme must strt with Romn letter:,,..., z, A, B,..., Z The length of vrile nme must e no greter thn 8. A vrile nme cnnot e keyword (e.g., if). The set of keywords is finite. Let L e the set of ll vlid vrile nmes in our restricted version of Jv. () Let L e the set of strings stisfying the first 3 conditions ove; i.e., we do not require the lst condition. Give regulr expression for L. Answer: To simplify the regulr expression, we define Then regulr expression for L is = {,,...,z,a,b,...,z} Σ 2 = {,1,2,...,9}. ( Σ 2 ε) ( Σ 2 ε). } {{ } 7 times Note tht y including the ε in ech of the lst prts, we cn generte strings tht hve length strictly less thn 8. 7

() Prove tht L hs regulr expression, where L is the set of strings stisfying ll four conditions. Answer: We proved in Homework 1, prolem 4(), tht L is finite. Thus, L is regulr, so it hs regulr expression. Although the prolem didn t sk for it, we cn write regulr expression for L y listing ll of the strings in L nd putting in etween ech pir of consecutive strings. This works ecuse L is finite. (c) Give DFA for the lnguge L in prt (), where the lphet Σ is the set of ll printle chrcters on computer keyord (no control chrcters), except for prentheses to void confusion. Answer: Define nd Σ 2 s in prt (), nd let Σ 3 = Σ ( Σ 2 ) e ll of the other chrcters on computer keyord except for prentheses. Then DFA for L is s follows: Σ 2 Σ 2 1 2 3 4 9 Σ 2 Σ 3 Σ 3 Σ 3 Σ 3 Σ Σ 6. Define L to e the set of strings tht represent numers in modified version of Jv. The gol in this prolem is to define regulr expression nd n NFA for L. To precisely define L, let the set of digits e = {, 1, 2,..., 9}, nd define the set of signs to e Σ 2 = {+, -}. Then L = L 1 L 2 L 3, where L 1 is the set of ll strings tht re deciml integer numers. Specificlly, L 1 consists of strings tht strt with n optionl sign, followed y one or more digits. Exmples of strings in L 1 re 2, +9, nd -241. L 2 is the set of ll strings tht re floting-point numers tht re not in exponentil nottion. Specificlly, L 2 consists of strings tht strt with n optionl sign, followed y zero or more digits, followed y deciml point, nd end with zero or more digits, where there must e t lest one digit in the string. Exmples of strings in L 2 re 13.231, -28. nd.124. All strings in L 2 hve exctly one deciml point. L 3 is the set of ll strings tht re floting-point numers in exponentil nottion. Specificlly, L 3 consists of strings tht strt with string from L 1 or L 2, followed y E or e, nd end with string from L 1. Exmples of strings in L 3 re -8.1E-83, +8.E5 nd 1e+31. Assume tht there is no limit on the numer of digits in string in L. Also, we do not llow for the suffixes L, l, F, f, D, d, t the end of numers to denote types (long integers, flots, nd doules). Define Σ s the lphet of ll printle chrcters on computer keyord (no control chrcters), except for prentheses to void confusion. 8

() Give regulr expression for L 1. Also, give n NFA nd DFA for L 1 over the lphet Σ. Answer: A regulr expression for L 1 is R 1 = (+ - ε) Σ 1 where = {, 1, 2,..., 9} s previously defined. An NFA for L 1 is q 1 q 2 q 3 Define Σ 3 = Σ ( Σ 2 ) with Σ 2 = {-, +}, s efore. Then DFA for L 1 is 3 Σ 2 1 2 Σ Σ Σ 3 4 Σ Notice tht the DFA is more complicted thn the NFA. () Give regulr expression for L 2. Also, give n NFA for L 2 over the lphet Σ. Answer: A regulr expression for L 2 is R 2 = (+ - ε)( Σ 1.Σ 1.Σ 1 ) Note tht the regulr expression (+ - ε)σ 1.Σ 1 is not correct since it cn generte the strings., +. nd -., which re not vlid floting-point numers. An NFA for L 2 is r 1 r 2 r 3 r 5 r 4 9

(c) Give regulr expression for L 3. Also, give n NFA for L 3 over the lphet Σ. Answer: A regulr expression for L 3 is R 3 = (R 1 R 2 )(E e)r 1 where R 1 nd R 2 re defined in the previous prts. An NFA for L 3 is s 3 s 1 s 2 E, e s 4 s 5 s 6 s 7 s 8 (d) Give regulr expression for the lnguge L. Also, give n NFA for L over the lphet Σ. Answer: Note tht L = L 1 L 2 L 3, so regulr expression for L is R 4 = R 1 R 2 R 3 We cn construct n NFA for L y tking the union of the NFA s for L 1, L 2 nd L 3, s follows: 1

q 1 q 2 q 3 ε q ε r 1 r 2 r 3 r 5 r 4 ε s 3 s 1 s 2 E, e s 4 s 5 s 6 s 7 s 8 A simpler NFA for L is to tke the NFA for L 3 nd mke s 3 nd s 5 lso ccepting sttes in ddition to s 8. 11