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

Similar documents
CS 330 Formal Methods and Models

CS 330 Formal Methods and Models

CS 330 Formal Methods and Models

Let's start with an example:

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.

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

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

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

Some Theory of Computation Exercises Week 1

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

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

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

Lecture 08: Feb. 08, 2019

Homework 3 Solutions

SWEN 224 Formal Foundations of Programming WITH ANSWERS

First Midterm Examination

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

Minimal DFA. minimal DFA for L starting from any other

First Midterm Examination

Harvard University Computer Science 121 Midterm October 23, 2012

Formal languages, automata, and theory of computation

1 Nondeterministic Finite Automata

Chapter 2 Finite Automata

CS 275 Automata and Formal Language Theory

Finite Automata Theory and Formal Languages TMV027/DIT321 LP4 2018

Context-Free Grammars and Languages

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

CMSC 330: Organization of Programming Languages

p-adic Egyptian Fractions

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

AUTOMATA AND LANGUAGES. Definition 1.5: Finite Automaton

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

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

Worked out examples Finite Automata

Nondeterminism and Nodeterministic Automata

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

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

CS103B Handout 18 Winter 2007 February 28, 2007 Finite Automata

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

Converting Regular Expressions to Discrete Finite Automata: A Tutorial

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

CS311 Computational Structures Regular Languages and Regular Grammars. Lecture 6

CHAPTER 1 Regular Languages. Contents

State Minimization for DFAs

ɛ-closure, Kleene s Theorem,

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

Non-deterministic Finite Automata

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

Convert the NFA into DFA

1 From NFA to regular expression

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

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

Theory of Computation Regular Languages

Parse trees, ambiguity, and Chomsky normal form

Boolean algebra.

GNFA GNFA GNFA GNFA GNFA

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

Thoery of Automata CS402

Formal Language and Automata Theory (CS21004)

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

Formal Languages and Automata

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

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

CS 275 Automata and Formal Language Theory

NFAs continued, Closure Properties of Regular Languages

FABER Formal Languages, Automata and Models of Computation

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

Finite Automata-cont d

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

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

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

CS 314 Principles of Programming Languages

CISC 4090 Theory of Computation

Chapter 1, Part 1. Regular Languages. CSC527, Chapter 1, Part 1 c 2012 Mitsunori Ogihara 1

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.

Automata and Languages

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

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

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

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

Non-deterministic Finite Automata

Coalgebra, Lecture 15: Equations for Deterministic Automata

Reasoning and programming. Lecture 5: Invariants and Logic. Boolean expressions. Reasoning. Examples

Designing finite automata II

NFAs continued, Closure Properties of Regular Languages

1.4 Nonregular Languages

September 13 Homework Solutions

Name Ima Sample ASU ID

Lecture 2 : Propositions DRAFT

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

Lexical Analysis Finite Automate

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

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

CS 275 Automata and Formal Language Theory

Lecture 09: Myhill-Nerode Theorem

Finite-State Automata: Recap

CS375: Logic and Theory of Computing

1 Structural induction

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

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

Transcription:

CS 330 Forml Methods nd Models Dn Richrds, George Mson University, Spring 2016 Quiz Solutions Quiz 1, Propositionl Logic Dte: Ferury 9 1. (4pts) ((p q) (q r)) (p r), prove tutology using truth tles. p q r p q q r (p q) (q r) p r result T T T T T T T T T T F T F F F T T F T F T F T T T F F F T F F T F T T T T T T T F T F T F F T T F F T T T T T T F F F T T T T T 1

2. (4pts) ((p q) (q r)) (p r), prove tutology using lger. ((p q) (q r)) (p r) (( p q) ( q r)) ( p r) (( p q) ( q r)) ( p r) ( p q) ( q r) ( p r) (p q) (q r) ( p r) (p q) (q r) p r (p q) p (q r) r ((p q) p) ((q r) r) ((p p) ( q p)) ((q r) ( r r)) (T RUE ( q p)) ((q r) T RUE) ( q p) (q r) q p q r q q p r T RUE p r T RUE conditionl lw conditionl lw DeMorgn s lw DeMorgn s lw Associtive lw Commuttive lw Associtive lw Distriutive lw Lw of Excluded Middle Property of T RUE Associtive lw Commuttive lw Lw of Excluded Middle Property of T RUE 3. (2pts) Prove lgericlly: (p q r s) p q r s. Solve using multiple pplictions of DeMorgn s Lw. (p q r s) ((p q r) s) (p q r) s ((p q) r) s (p q) r s p q r s 2

Quiz 2, Rules of Inference Dte: Ferury 18 1. (4pts) Prove using nturl deduction, (p q) (p (p q)). 1 [p q] Assumption 2 [p] Assumption 3 q Modus ponens 1,2 4 p q introduction 2,3 5 p (p q) introduction 2,4 6 (p q) (p (p q)) introduction 1,5 2. (6pts) Prove using nturl deduction, p p (p q). 1 [p] Assumption 2 p q introduction 1 3 p (p q) introduction 1,2 4 p p (p q) introduction 1,4 5 [p (p q)] Assumption 6 p elimintion 5 7 p (p q) p introduction 5,6 8 p p (p q) introduction 4,7 3

Quiz 3, Predicte Logic Dte: Ferury 25 1. (2pts) Evlute i N : j I i : j 2 = i. T rue. Suppose tht i = 1. Then I i = {1}, so the only possile vlue of j would e 1. In tht cse, 1 2 = 1, so j 2 = i. 2. (4pts) Assert the elements of n element rry A increse to mximum nd then decrese to the end. For exmple, [2, 5, 17, 13, 12, 1]. To express tht the elements increse to mximum (sy, m), try every element which ppers efore the mximum increses to the susequent element : i I n : (i < m) (A i < A i+1 ) Similrly, the elements must decrese fter the mximum, so every element which ppers fter the mximum decreses from the previous element : i I n : (m < i) (A i 1 > A i ) Altogether: m I n : i I n : (i < m A i < A i+1 ) (m < i A i 1 > A i ) 3. (4pts) Assert every even integer > 2 is the sum of two primes. Use given predictes ODD(k) nd P RIM E(k). the integer i is n even integer > 2 : ODD(i) (i > 2) the numer i is the sum of two primes p nd q : p I : q I : P RIME(p) P RIME(q) (i = p + q) Altogether: i I :( ODD(i) (i > 2)) ( p I : q I : P RIME(p) P RIME(q) (i = p + q)) 4

Quiz 4, Mthemticl Induction Dte: Mrch 3 1. (5pts) Prove n i=0 i = 1 n+1, n 0, 1. 1 When n = 0, we hve 0 i=0 i = 0 = 1 = 1 0+1 1 = 1 1 = 1 Assume tht for some k 0, k i=0 i = 1 k+1 1, 1 We would like to prove the k + 1 cse, k+1 i = 1 (k+1)+1 1 i=0 To do this, we egin with the left hnd side, nd sustitute the inductive hypothesis, k+1 i = k+1 + i=0 k i=0 i = k+1 + 1 k+1 1 = (1 )k+1 1 + 1 k+1 1 = k+1 (k+1)+1 + 1 k+1 1 = 1 (k+1)+1 1 This proves the inductive conclusion, thus y mthemticl induction, the theorem is proved. 5

2. (5pts) Let S n+1 = 2S n + 1, n 0, S 0 = 0. Prove S n = 2 n 1, n 0. When n = 0, 2 0 1 = 1 1 = 0 = S 0. Assume tht for some k 0, S k = 2 k 1. We wnt to prove the k + 1 cse, S k+1 = 2 k+1 1. Beginning with the left hnd side, nd pplying the given reltionship S n+1 = 2S n + 1 s well s the inductive hypothesis, we get S k+1 = 2S k + 1 = 2(2 k 1) + 1 = 2 k+1 2 + 1 = 2 k+1 1 By mthemticl induction, the theorem in proved. 6

Quiz 5, Progrm Verifiction Dte: Mrch 17 1. Assume n 0. i 0 s 1 while i < n do s 2 3 s i i + 1 s s/2 () (3pts) Give the loop invrint. (i n) (s = 1 3 i ) After initiliztion, i = 0 nd s = 1, so (0 n) (1 = 1 3 0 ). () (5pts) Prove the loop invrint. (i n) (s = 1 3 i ) (i < n) {s 2 3 s} (i n) (s = 2 3 1 3 i = 2 ) (i < n) 3i+1 (i n) (s = 2 3 i+1 ) (i < n) {i i + 1} (i n) (s = 2 3 i ) (i n) (s = 2 3 i ) {s s/2} (i n) (s = 1 2 2 3 i = 1 3 i ) (i n) (s = 1 3 i ) (i < n) {s 2 3 s; i i + 1; s s/2} (i n) (s = 1 3 i ) (c) (2pts) Apply the loop invrint. (i n) (s = 1 3 i ) (i < n) {s 2 3 s; i i + 1; s s/2} (i n) (s = 1 3 i ) (i n) (s = 1 3 i ){while i < n do s 2 3 s; i i+1; s s/2}(i n) (s = 1 3 i ) (i < n) 7

Quiz 6, Progrm Verifiction Dte: Mrch 29 1. i 0 x 2 while i < n do x x x x i i + 1 () (3pts) Stte the loop invrint. (i n) (x = 2 3i ) After initiliztion, i = 0 nd x = 2, so (0 n) (2 = 2 30 ). () (4pts) Prove the loop invrint. (i n) (x = 2 3i ) (i < n) {x x x x} (i n) (x = 2 3i 2 3i 2 3i ) (i < n) (i n) (x = 2 3i+1 ) (i < n) {i i + 1} (i n) (x = 2 3i ) (i n) (x = 2 3i ) (i < n) {x x x x; i i + 1} (i n) (x = 2 3i ) Note tht 2 3i 2 3i 2 3i = 2 3 3i = 2 3i+1. (c) (3pts) Apply the loop invrint. (i n) (x = 2 3i ) (i < n) {x x x x; i i + 1} (i n) (x = 2 3i ) (i n) (x = 2 3i ){while i < n do x x x x; i i+1}(i n) (x = 2 3i ) (i < n) 8

Quiz 7, Finite Automt Dte: April 5 1. (5pts) Give FA tht ccepts L(P ), P =. strt, 2. (5pts) Give FA tht ccepts L, L = {x {, } every in x is followed lter y }. L, L, L, L q 0 strt q 1 q 3 q 2 q 0 : Lst chrcter seen ws not n, lst pir seen ws not. q 1 : Lst chrcter seen ws n, lst pir seen ws not. q 2 : Lst chrcter seen ws n, lst pir seen ws. q 3 : Lst chrcter seen ws, lst pir seen ws. 9

Quiz 8, Regulr Expressions Dte: April 12 1. (5pts) Give RE for L, L = {x x {, } nd every in x is followed y }. Note tht this is intended s the sme lnguge from the previous quiz. The strings in the lnguge cn e roken down into two cses. Either the string does not contin ny, in which cse it cn e nything else, or it does contin, in which cse the finl pir in the string must e. The set of strings which do not contin cn e expressed s follows: ( + ) ( + Λ) The set of strings whose finl pir is is suset of L, nd it includes ll of the strings in L which contin n (s well s some others). This set cn e creted y tking, plcing ny string efore it, nd string which does not contin fter it: ( + ) ( + ) ( + Λ) Altogether, the solution is: ( + ) ( + Λ) + ( + ) ( + ) ( + Λ) If we were to lterntely interpret the question s sying tht ny must e followed immeditely y, then we would insted hve: (( + ) ()) ( + ) ( + Λ) This is not the solution, ut is provided for illustrtion. 2. (5pts) Give RE for L, L = {x x {, } nd x does not contin }. Any in the string must not hve n in front of it. Therefore, if ppers in the string, either it must e isolted from other s, or it must pper s prt of sustring of s t the eginning of the string: ( + ) 10

Quiz 9, Regulr Expression Conversion Dte: April 19 1. (4pts) Write regulr grmmr for ( + )c. Grmmr for r 1 = : ({A, A 1 }, {,, c}, A 1, {A 1 A, A Λ}) Grmmr for r 2 = : ({B, B 1 }, {,, c}, B 1, {B 1 B, B Λ}) Grmmr for r 3 = c: ({C, C 1 }, {,, c}, C 1, {C 1 cc, C Λ}) Grmmr for r 4 = : ({A, A 1, D}, {,, c}, D, {D A 1, D Λ, A 1 A, A D}) Grmmr for r 5 = + : ({A, A 1, B, B 1, D, E}, {,, c}, E, {E D, E B 1, D A 1, D Λ, A 1 A, A D, B 1 B, B Λ}) Grmmr for r 6 = ( + )c: ({A, A 1, B, B 1, C, C 1, D, E, S}, {,, c}, S, {S E, E D, E B 1, D A 1, D C 1, A 1 A, A D, B 1 B, B C 1, C 1 cc, C Λ}) The following is not necessry for the solution, ut is provided for illustrtion: Regulr grmmr (no unit productions) for r = ( + )c: ({A, B, C, S}, {,, c}, S, {S A, S cc, S B, A A, A cc, B cc, C Λ}) 11

2. (6pts) Convert the mchine into regex, removing A then B then C. f strt A B C c e d Solution: Λ c Λ f strt A B C e Λ d + e c f strt B C d Λ c + d( + e) f strt ( + e) f C Λ strt ( + e) f(c + d( + e) f) 12

Quiz 10, Regulr Grmmrs Dte: April 26 1. (3pts) Convert into regulr grmmr with unit productions: + P ={S 1 A 1, A 1 Λ} P ={S 2 Λ, S 2 S 1, S 1 A 1, A 1 S 2 } P ={S 3 A 3, A 3 Λ} P + ={S 4 S 2, S 2 Λ, S 2 S 1, S 1 A 1, A 1 S 2, S 4 S 3, S 3 A 3, A 3 Λ} The strt symol is S 4. 2. (3pts) Convert into regulr grmmr: S A S B A A B S B Λ B S Solution: S A S A S B A A B S B Λ B S B B B A B A 13

3. (4pts) Convert into deterministic regulr grmmr: S A S B A B A Λ B S B A Solution: (note tht the two stte mchines shown here re for illustrtion only, nd re not prt of the solution) A strt S B strt V {S} V {A,B} V {S,A} V {B} V {S} V {A,B} V {B} V {S,A} V {A,B} V {B} V {A,B} V {S,A} V {A,B} Λ V {S,A} V {A,B} V {S,A} Λ 14

Quiz 11, Context-Free Grmmrs Dte: My 3 1. (4pts) Give CFG for L = { i j c k i < j + k}. If i < j +k, then there is some wy to rek up j nd k, sy j = n+m nd k = p + q, so tht i = n + p nd m, n, p, q 0. At lest one of m or q must e greter thn zero. If we do tht, then we re left with the grouping: i j c k = ( p n )( n m )(c q c p ) If we do this, we cn uild up the lnguge y pieces. Let B generte n n : B Λ B Let X generte m c q : X c X Xc Let Y generte ( n n )( m c q ) = n ( n m )c q = n j c q : Y BX Let C generte p ( n j c q )c p = ( p n ) j (c q c p ) = i j c k : C Y Y c Finlly, we hve: S C 15

2. (4pts) Give CFG for plindromes over Σ = {, } (for exmple, Λ,,, etc). If you tke ny plindrome, you cn deconstruct it y mtching the first nd lst chrcter nd removing them, nd repeting until you re left with either zero or one chrcters. We will do the reverse, to uild up plindrome recursively: to egin with, we know tht Λ,, nd re in the lnguge. Also, if string is plindrome, then it will remin plindrome if you dd the sme chrcter to the eginning nd the end. If L is the lnguge of plindromes, this gives: S Λ S S S 3. (2pts) Sometimes plindrome hs lnks emedded (for exmple, too hot to hoot ). Extend (2) to llow ritrry lnks; use (underscore) for lnk. This is essentilly the sme s the previous solution, except tht whenever you dd chrcters, you cn dditionlly dd spces: S Λ S X X X S S S S 16