First Midterm Examination

Similar documents
First Midterm Examination

Harvard University Computer Science 121 Midterm October 23, 2012

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

Name Ima Sample ASU ID

Formal languages, automata, and theory of computation

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

Minimal DFA. minimal DFA for L starting from any other

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

Worked out examples Finite Automata

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

Homework 3 Solutions

CS375: Logic and Theory of Computing

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.

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

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

Convert the NFA into DFA

Nondeterminism and Nodeterministic Automata

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

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

CS 275 Automata and Formal Language Theory

Lecture 08: Feb. 08, 2019

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

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

Finite Automata-cont d

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.

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

Theory of Computation Regular Languages

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

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

CS 330 Formal Methods and Models

Lexical Analysis Finite Automate

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

Closure Properties of Regular Languages

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

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

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

Myhill-Nerode Theorem

AUTOMATA AND LANGUAGES. Definition 1.5: Finite Automaton

1 From NFA to regular expression

Designing finite automata II

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

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

Let's start with an example:

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

1.4 Nonregular Languages

CSC 311 Theory of Computation

Finite Automata Theory and Formal Languages TMV027/DIT321 LP4 2018

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

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

CS 330 Formal Methods and Models

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

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

I. Theory of Automata II. Theory of Formal Languages III. Theory of Turing Machines

CMSC 330: Organization of Programming Languages

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

CSC 473 Automata, Grammars & Languages 11/9/10

Chapter 2 Finite Automata

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

Finite-State Automata: Recap

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

Coalgebra, Lecture 15: Equations for Deterministic Automata

Formal Languages and Automata

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

Deterministic Finite Automata

CS 330 Formal Methods and Models

set is not closed under matrix [ multiplication, ] and does not form a group.

1 Nondeterministic Finite Automata

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

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

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.

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

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

SWEN 224 Formal Foundations of Programming WITH ANSWERS

Automata and Languages

80 CHAPTER 2. DFA S, NFA S, REGULAR LANGUAGES. 2.6 Finite State Automata With Output: Transducers

Thoery of Automata CS402

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

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

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

3 Regular expressions

Languages & Automata

FABER Formal Languages, Automata and Models of Computation

Converting Regular Expressions to Discrete Finite Automata: A Tutorial

Parse trees, ambiguity, and Chomsky normal form

CS 275 Automata and Formal Language Theory

CSCI FOUNDATIONS OF COMPUTER SCIENCE

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

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

Lecture 09: Myhill-Nerode Theorem

CHAPTER 1 Regular Languages. Contents

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

Linear Inequalities. Work Sheet 1

1.3 Regular Expressions

Context-Free Grammars and Languages

Bases for Vector Spaces

State Minimization for DFAs

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

Recursively Enumerable and Recursive. Languages

Tutorial Automata and formal Languages

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

Transcription:

24-25 Fll Semester First Midterm Exmintion ) Give the stte digrm of DFA tht recognizes the lnguge A over lphet Σ = {, } where A = {w w contins or } 2) The following DFA recognizes the lnguge B over lphet Σ = {, }. Descrie B verlly., q q 2 q 3 q 4 q 5 q 6 3) Convert the following NFA to DFA:, q q 2 q 3 ε 4) For the regulr expression [ ( ) ], find n equivlent NFA. 5) Show tht the lnguge A = { 2n n+2 c n 2 n 2} is not regulr.

Answers ), 2) Contins t lest two s OR If it strts with zero, chnges symols t lest 3 times, if it strts with, chnges symols t lest 4 times. 3), q q 23

4) ε ε 5) Assume A is regulr. Let pumping length e p. Consider s = 2p p+2 c p 2 According to pumping lemm, we cn find x, y, z such tht s = xyz. We lso know tht xy p therefore y consists of s only. Suppose y = k. In this cse xy i z = 2p+ik p+2 c p 2 / A We hve contrdiction, so A is not regulr.

24-25 Fll Semester Second Midterm Exmintion ) Find context free grmmr tht recognizes the lnguge over Σ = {, } consisting of strings of odd length where first, middle nd lst symols re the sme. 2) Find PDA tht recognizes the lnguge { n m m > n}. 3) Consider the following lnguges over Σ = {,, c, d}: { n m c m d n } { n n c m d m } { n m c n d m } ) Which one is non-context free? ) Show tht it is not using pumping lemm. 4) Descrie Turing Mchine deciding the lnguge A = { n n2 n }. 5) Wht lnguge does the following Turing mchine recognize? Is it decider? (Input lphet is: Σ = {, }). Sweep from left to right. IF there is ny fter the first, REJECT. 2. Move hed to strt. Serch for. IF found, cross it. (Replce y ) ELSE, Go to 6. 3. Serch for. IF found, cross it. ELSE, REJECT. 4. Serch for. IF found, cross it. ELSE, REJECT. 5. Go to 2. 6. Move hed to strt. Serch for. IF found, ACCEPT. ELSE, REJECT.

Answers ) S A B A A A A A B B B B B OR S A B A CAC B CBC C 2), ε, ε ε, ε $ ε, ε ε ε, $ ε, ε ε, ε ε 3) ) { n m c n d m } ) Suppose the lnguge is context free. Let p e the pumping length. Choose s s s = n m c n d n where m, n > p. According to pumping lemm, we cn find v, y such tht n m c n d n = uvxyz If v or y contin more thn one type of symol, clerly uv 2 xy 2 z / A ecuse symols re out of order. Therefore v nd y cn consist of single symol only. In this cse, we hve two choices. v must consist of s nd y must consist of c s, or v must consist of s nd y must consist of d s. Both cses violte the rule vxy < p Therefore we cnnot pump this string so the lnguge is not context free.

4). Sweep from left to right. IF there is no, or no, or if they re out of order, REJECT. 2. Move hed to strt. Serch for. IF found, Mrk it. Move right until lnk. Write #. Go to 2. //Write s mny # s s there re s. 3. Serch for #. IF found Mrk it. Unmrk ll mrked s. Shuttle etween s nd s. Mrk one for ech one. IF is not found, Go to 3. IF is not found, REJECT. 4. ELSE (# not found) Move hed to strt. Serch for. IF found, REJECT. ELSE, ACCEPT. 5) It is decider, it cn not enter ny infinite loops. Its lnguge is: { n m m 2n + }.

24-25 Fll Semester Finl Exmintion ) Give the stte digrm of DFA tht recognizes the lnguge A over lphet Σ = {, } where w 2 nd the first two nd the lst two digits of w re identicl. For exmple: A, A ut / A, / A 2) Convert the following grmmr into Chomsky norml form: S AA A B B ε B A 3) Let A e the lnguge in {, } mde of strings where the numer of zeros is t lest 3 times the numer of ones. Descrie Turing Mchine recognizing A. 4) You re given set of n distinct positive integers. You wnt to determine if there re two integers p, q in the set such tht p = q 2. Write n lgorithm in pseudo-code for this prolem. Show tht it is in P. 5) Consider the following prolem: Given grph, is there wy to prtition the vertices into 4 susets such tht no two elements in suset re connected? Show tht this prolem is in NP.

Answers ) 2) First eliminte B A, then eliminte A ε nd then rek triples to otin: S Y A AY AC A XB Y D XA Y E Y Y B Y X C Y A D BY E AY X Y

3). Move hed to strt. Serch for. IF found, cross it. (Replce y ) ELSE, ACCEPT. 2. Repet 3 times: Move hed to strt. Serch for. IF found, cross it. (Replce y ) ELSE, REJECT. 3. Go to. 4) INPUT Integer A[], A[2],..., A[n] For i = to n For j = to n If A[i] A[i] == A[j] Return TRUE EndIf EndFor EndFor Return FALSE This lgorithm clerly does Θ(n 2 ) opertions, so it is in P. 5) Suppose there re n vertices. Then, there re t most n(n ) 2 = Θ(n 2 ) edges. We cn check given solution y checking ll edges one y one. (We will return FALSE if the two vertices connected y the edge elong to the sme suset) Therefore given solution cn e verified in Θ(n 2 ) opertions. The prolem is in NP.

Nme-Surnme:..24 ID Numer: CLASSWORK Give the stte digrm of DFA tht recognizes the lnguge A over lphet Σ = {, } where A = {w w contins ut does not contin }.,

Nme-Surnme: 2..24 ID Numer: CLASSWORK Let: A = {Strings of length t lest 3 whose second nd third items re the sme} over lphet Σ = {, }. Give the stte digrm of the DFA tht recognizes this lnguge.,,,

Nme-Surnme: 5..24 ID Numer: CLASSWORK 2 Find regulr expression equivlent to the lnguge recognized y the following NFA: q q 2 q 3 q 4 q 5 ( ) ( )

Nme-Surnme: 6..24 ID Numer: CLASSWORK 2 Find regulr expression equivlent to the lnguge recognized y the following NFA: q q 2 q 3 q 4 [ () ] ( ) ( )

Nme-Surnme: 5..24 ID Numer: CLASSWORK 3 ) Find PDA tht recognizes the lnguge { n 2 n+2 n } ) Find CFG generting the sme lnguge., ε, ε ε, ε $, ε, ε ε, $ ε S S 2 2

Nme-Surnme: 6..24 ID Numer: CLASSWORK 3 ) Find PDA tht recognizes the lnguge { n 2 c 3 d n n } ) Find CFG generting the sme lnguge., ε ε, ε $, ε ε, ε ε c, ε ε ε, $ ε c, ε ε c, ε ε d, ε S Sd 2 c 3

Nme-Surnme: 2..24 ID Numer: CLASSWORK 4 Use the pumping lemm to show tht the following lnguge is not context free: A = { n c 4n n n } Suppose A is context free. Let p e the pumping length. Choose s s s = p c 4p p. According to pumping lemm, we cn findv, y such tht p c 4p p = uvxyz If v or y contin or c, clerly uv 2 xy 2 z / A ecuse it is not in the given formt. Therefore v nd y cn consist of s only. But in this cse, we cn pump t most two of the prts including. The size of the third prt will remin the sme, so pumped string will not e in A. For exmple, v =, y = will result in uv 2 xy 2 z = p+ c 4p+4 p / A Therefore we cnnot pump this string nd A is not context free.

Nme-Surnme: 3..24 ID Numer: CLASSWORK 4 Use the pumping lemm to show tht the following lnguge is not context free: A = { n m c 2n d 2m m, n } Suppose A is context free. Let p e the pumping length. Choose s s s = n m c 2n d 2m nd n, m p. According to pumping lemm, we cn find v, y such tht n m c 2n d 2m = uvxyz If v or y contin more thn one type of symol, clerly uv 2 xy 2 z / A ecuse it is not in the given formt. Therefore v nd y cn consist of single symol only. If v consists of s, then y must consist of (twice s mny) c s. If v consists of s, then y must consist of (twice s mny) d s. In othe cses, vxy > p nd the third condition of pumping lemm is violted. Therefore we cnnot pump this string nd A is not context free.

Nme-Surnme: 9..24 ID Numer: CLASSWORK 5 Descrie Turing Mchine tht gives the output x m, x m,..., x 2, x for the input x, x 2,..., x m, x m.. Move to Strt. Find the first element tht is not crossed. Cll it. Cross it. If not found, Go To 4. 2. Move right until meeting lnk or cross. Move left. Red the element. Cll it. 3. Swp nd if they re different. Cross nd. Go To. 4. Sweep from left to right. Restore ll crossed elements. Return.

Nme-Surnme: 2..24 ID Numer: CLASSWORK 5 Descrie Turing Mchine tht gives the output x, x 2,..., x m, x, x 2,..., x m for the input x, x 2,..., x m.. Move to Strt. Find the first element tht is not crossed. Cross it. If not found, Go To 4. 2. Move right until meeting lnk. 3. Write the element found in. Cross it. Go To. 4. Sweep from left to right. Restore ll crossed elements. Return.

Nme-Surnme: 26..24 ID Numer: CLASSWORK 6 Let A e the lnguge in {, } mde of strings contining n equl numer of s nd s. Descrie Turing Mchine recognizing A. Is it decider?. Move hed to strt. Serch for. 2. IF there s, Cross it. Move hed to strt. Serch for. IF there is, Cross it. Go to. ELSE REJECT. 3. IF there s no, Move hed to strt. Serch for. IF there is, REJECT. ELSE ACCEPT. This mchine stops fter finitely mny steps, ecuse loops re repeted (t most) s mny times s the numer of symols in the input. It is decider.

Nme-Surnme: 27..24 ID Numer: CLASSWORK 6 Let B e the lnguge in {, } mde of strings contining more s thn s. Descrie Turing Mchine recognizing B. Is it decider?. Move hed to strt. Serch for. 2. IF there s, Cross it. Move hed to strt. Serch for. IF there is, Cross it. Go to. ELSE ACCEPT. 3. IF there s no, REJECT. This mchine stops fter finitely mny steps, ecuse loops re repeted (t most) s mny times s the numer of symols in the input. It is decider.

Nme-Surnme:.2.24 ID Numer: CLASSWORK 7 Let A e the set of ll 2 2 mtrices with entries from Q. Show tht A is countle. We know tht Q is countle. Suppose list of Q is {q, q 2,...}. We cn count Q Q y mking n infinite tle nd counting digonlly. Suppose we otin list S = {s, s 2,...}. Now mke tle for S S. The list will give elements of A = Q Q Q Q.

Nme-Surnme:.2.24 ID Numer: CLASSWORK 7 Let B e the set of ll finite strings sed on the lphet {,,..., 9}. Show tht B is countle. The following gives list of ll finite strings tht cn e written with this lphet: String. 9. 99.

Nme-Surnme: 24.2.24 ID Numer: CLASSWORK 8 STRING MATCHING: You re given string of length n. You re lso given word ( second string) of length k, where k n. You wnt to determine if the word occurs in the string. Show tht the string mtching prolem is in P. INPUT String A[n], Word B[k] For i = to n k + test = TRUE For j = to k If A[i + j ] B[j] test = FALSE Brek EndIf EndFor If test == TRUE Return TRUE EndFor Return FALSE This lgorithm uses nk steps therefore it is Θ(n 2 ). So it is in P.

Nme-Surnme: 25.2.24 ID Numer: CLASSWORK 8 You re given n distinct positive integers where n 3. You wnt to find the third lrgest integer. Show tht this prolem is in P. INPUT A[n] For i = to 3 mx = A[] For j = 2 to n If A[j] > mx mx = A[j] index = j EndIf EndFor A[index] = EndFor Return mx This lgorithm uses 3n steps therefore it is Θ(n). So it is in P.

Nme-Surnme: 24.2.24 ID Numer: CLASSWORK 9 The SHORTEST PATH prolem is defined s follows: You re given weighted undirected grph G, nd nodes s nd t in the grph nd numer W. Is there pth from s to t with weight W? Show tht this prolem is in NP. A given solution contins n edges. We cn check ech edge in n steps if ech vertex hs its list of edges. Then we hve to check the sum. Therefore we do (n ) 2 +n opertions, so verifying lgorithm is Θ(n 2 ). This is polynomil, so the prolem is in NP.

Nme-Surnme: 25.2.24 ID Numer: CLASSWORK 9 The MINIMUM SPANNING TREE prolem is defined s follows: You re given weighted undirected grph G nd numer W. Is there spnning tree with weight W? Show tht this prolem is in NP. A given solution contins Θ(n) edges. We cn check ech edge in Θ(n) steps if ech vertex hs its list of edges. (If we hve n unsorted list of edges, this will require n 2 opertions ut still, it is polynomil) Then we hve to check the sum nd lso we hve to check tht ll vertices re connected. Verifying lgorithm is Θ(n 2 ). This is polynomil, so the prolem is in NP. Prepred y: Dr. Emre Sermutlu (25)