Homework 3 Solutions



Similar documents
One Minute To Learn Programming: Finite Automata

Regular Sets and Expressions

Solution to Problem Set 1

FORMAL LANGUAGES, AUTOMATA AND THEORY OF COMPUTATION EXERCISES ON REGULAR LANGUAGES

Example 27.1 Draw a Venn diagram to show the relationship between counting numbers, whole numbers, integers, and rational numbers.

0.1 Basic Set Theory and Interval Notation

Reasoning to Solve Equations and Inequalities

flex Regular Expressions and Lexical Scanning Regular Expressions and flex Examples on Alphabet A = {a,b} (Standard) Regular Expressions on Alphabet A

EQUATIONS OF LINES AND PLANES

Bayesian Updating with Continuous Priors Class 13, 18.05, Spring 2014 Jeremy Orloff and Jonathan Bloom

Words Symbols Diagram. abcde. a + b + c + d + e

Appendix D: Completing the Square and the Quadratic Formula. In Appendix A, two special cases of expanding brackets were considered:

Algebra Review. How well do you remember your algebra?

Binary Representation of Numbers Autar Kaw

CS99S Laboratory 2 Preparation Copyright W. J. Dally 2001 October 1, 2001

Regular Languages and Finite Automata

Section 5-4 Trigonometric Functions

1.2 The Integers and Rational Numbers

Vectors Recap of vectors

Math 135 Circles and Completing the Square Examples

Polynomial Functions. Polynomial functions in one variable can be written in expanded form as ( )

PROF. BOYAN KOSTADINOV NEW YORK CITY COLLEGE OF TECHNOLOGY, CUNY

FAULT TREES AND RELIABILITY BLOCK DIAGRAMS. Harry G. Kwatny. Department of Mechanical Engineering & Mechanics Drexel University

MATH 150 HOMEWORK 4 SOLUTIONS

Solutions for Selected Exercises from Introduction to Compiler Design

5.6 POSITIVE INTEGRAL EXPONENTS

Use Geometry Expressions to create a more complex locus of points. Find evidence for equivalence using Geometry Expressions.

4.11 Inner Product Spaces

Java CUP. Java CUP Specifications. User Code Additions You may define Java code to be included within the generated parser:

A.7.1 Trigonometric interpretation of dot product A.7.2 Geometric interpretation of dot product

Mathematics. Vectors. hsn.uk.net. Higher. Contents. Vectors 128 HSN23100

and thus, they are similar. If k = 3 then the Jordan form of both matrices is

Experiment 6: Friction

Multiplication and Division - Left to Right. Addition and Subtraction - Left to Right.

Integration by Substitution

Lecture 5. Inner Product

Warm-up for Differential Calculus

Protocol Analysis / Analysis of Software Artifacts Kevin Bierhoff

Quick Reference Guide: One-time Account Update

Lec 2: Gates and Logic

Pentominoes. Pentominoes. Bruce Baguley Cascade Math Systems, LLC. The pentominoes are a simple-looking set of objects through which some powerful

1.00/1.001 Introduction to Computers and Engineering Problem Solving Fall Final Exam

Answer, Key Homework 10 David McIntyre 1

Physics 43 Homework Set 9 Chapter 40 Key

9.3. The Scalar Product. Introduction. Prerequisites. Learning Outcomes

Or more simply put, when adding or subtracting quantities, their uncertainties add.

1. Definition, Basic concepts, Types 2. Addition and Subtraction of Matrices 3. Scalar Multiplication 4. Assignment and answer key 5.

I calculate the unemployment rate as (In Labor Force Employed)/In Labor Force

FUNCTIONS AND EQUATIONS. xεs. The simplest way to represent a set is by listing its members. We use the notation

Unit 6: Exponents and Radicals

19. The Fermat-Euler Prime Number Theorem

P.3 Polynomials and Factoring. P.3 an 1. Polynomial STUDY TIP. Example 1 Writing Polynomials in Standard Form. What you should learn

RIGHT TRIANGLES AND THE PYTHAGOREAN TRIPLETS

2 DIODE CLIPPING and CLAMPING CIRCUITS

Econ 4721 Money and Banking Problem Set 2 Answer Key

LINEAR TRANSFORMATIONS AND THEIR REPRESENTING MATRICES

Math 314, Homework Assignment Prove that two nonvertical lines are perpendicular if and only if the product of their slopes is 1.

Babylonian Method of Computing the Square Root: Justifications Based on Fuzzy Techniques and on Computational Complexity

9 CONTINUOUS DISTRIBUTIONS

, and the number of electrons is -19. e e C. The negatively charged electrons move in the direction opposite to the conventional current flow.

The remaining two sides of the right triangle are called the legs of the right triangle.

Factoring Polynomials

A Visual and Interactive Input abb Automata. Theory Course with JFLAP 4.0

Repeated multiplication is represented using exponential notation, for example:

AREA OF A SURFACE OF REVOLUTION

Unambiguous Recognizable Two-dimensional Languages

Graphs on Logarithmic and Semilogarithmic Paper

5 a LAN 6 a gateway 7 a modem

Chapter. Contents: A Constructing decimal numbers

NQF Level: 2 US No: 7480

MA Lesson 16 Notes Summer 2016 Properties of Logarithms. Remember: A logarithm is an exponent! It behaves like an exponent!

Basic Research in Computer Science BRICS RS Brodal et al.: Solving the String Statistics Problem in Time O(n log n)

MODULE 3. 0, y = 0 for all y

1 Fractions from an advanced point of view

Vectors. The magnitude of a vector is its length, which can be determined by Pythagoras Theorem. The magnitude of a is written as a.

Automated Grading of DFA Constructions

Automata theory. An algorithmic approach. Lecture Notes. Javier Esparza

New Internet Radio Feature

Lecture 3 Gaussian Probability Distribution

Geometry 7-1 Geometric Mean and the Pythagorean Theorem

6.2 Volumes of Revolution: The Disk Method

. At first sight a! b seems an unwieldy formula but use of the following mnemonic will possibly help. a 1 a 2 a 3 a 1 a 2

How fast can we sort? Sorting. Decision-tree model. Decision-tree for insertion sort Sort a 1, a 2, a 3. CS Spring 2009

Operations with Polynomials

Formal Languages and Automata Theory - Regular Expressions and Finite Automata -

Object Semantics Lecture 2

Small Businesses Decisions to Offer Health Insurance to Employees

Data Compression. Lossless And Lossy Compression

1. In the Bohr model, compare the magnitudes of the electron s kinetic and potential energies in orbit. What does this imply?

5.2. LINE INTEGRALS 265. Let us quickly review the kind of integrals we have studied so far before we introduce a new one.

Quick Reference Guide: Reset Password

Brillouin Zones. Physics 3P41 Chris Wiebe

Two hours UNIVERSITY OF MANCHESTER SCHOOL OF COMPUTER SCIENCE. Date: Friday 16 th May Time: 14:00 16:00

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.19), 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