CS 330 Formal Methods and Models

Similar documents
CS 330 Formal Methods and Models

CS 330 Formal Methods and Models

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

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

First Midterm Examination

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

Let's start with an example:

Finite Automata Theory and Formal Languages TMV027/DIT321 LP4 2018

Finite Automata-cont d

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

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.

Theory of Computation Regular Languages

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

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

Worked out examples Finite Automata

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

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

AUTOMATA AND LANGUAGES. Definition 1.5: Finite Automaton

Some Theory of Computation Exercises Week 1

CS375: Logic and Theory of Computing

Chapter 2 Finite Automata

Lecture 08: Feb. 08, 2019

Homework 3 Solutions

Automata and Languages

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

Harvard University Computer Science 121 Midterm October 23, 2012

Non-deterministic Finite Automata

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

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

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

CS 275 Automata and Formal Language Theory

1.4 Nonregular Languages

Formal Languages and Automata

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

SWEN 224 Formal Foundations of Programming WITH ANSWERS

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

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

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

Finite Automata. Informatics 2A: Lecture 3. John Longley. 22 September School of Informatics University of Edinburgh

Nondeterminism and Nodeterministic Automata

Lexical Analysis Finite Automate

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

1 From NFA to regular expression

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

Formal languages, automata, and theory of computation

Minimal DFA. minimal DFA for L starting from any other

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.

Deterministic Finite Automata

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

4 Deterministic Büchi Automata

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

1.3 Regular Expressions

Non-deterministic Finite Automata

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

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

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

CHAPTER 1 Regular Languages. Contents

Name Ima Sample ASU ID

Convert the NFA into DFA

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

CSE396 Prelim I Answer Key Spring 2017

CS103B Handout 18 Winter 2007 February 28, 2007 Finite Automata

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

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

Converting Regular Expressions to Discrete Finite Automata: A Tutorial

NFAs continued, Closure Properties of Regular Languages

Formal Language and Automata Theory (CS21004)

2.4 Linear Inequalities and Interval Notation

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

1 Nondeterministic Finite Automata

CS375: Logic and Theory of Computing

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

Thoery of Automata CS402

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

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

State Minimization for DFAs

FABER Formal Languages, Automata and Models of Computation

Lecture 6 Regular Grammars

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

CISC 4090 Theory of Computation

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

p-adic Egyptian Fractions

CMSC 330: Organization of Programming Languages

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

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

Parse trees, ambiguity, and Chomsky normal form

Coalgebra, Lecture 15: Equations for Deterministic Automata

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

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

Closure Properties of Regular Languages

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

Normal Forms for Context-free Grammars

ɛ-closure, Kleene s Theorem,

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

Today s Topics Automata and Languages

Automata and Languages

(e) if x = y + z and a divides any two of the integers x, y, or z, then a divides the remaining integer

CS311 Computational Structures Regular Languages and Regular Grammars. Lecture 6

Transcription:

CS 330 Forml Methods nd Models Dn Richrds, section 003, George Mson University, Fll 2017 Quiz Solutions Quiz 1, Propositionl Logic Dte: Septemer 7 1. Prove (p q) (p q), () (5pts) using truth tles. p q p q q p q (p q) (p q) (p q) T T T F F T T T F F T T F T F T F F T F T F F F T T F T () (5pts) using lger. (p q) (p q) (p q) ( p q) (p q) ( p q) (p q) ( p q) (p q) (p q) conditionl lw DeMorgn s lw lw of negtion lw of negtion ((p q) (p q)) ((p q) (p q)) iconditionl lw (p q) (p q) (p q) (p q) TRUE idempotence conditionl lw excluded middle 1

Quiz 2, Rules of Inference Dte: Septemer 14 1. (10pts) Prove (p q) (p q), using inference rules. 1 [p q] Assumption 2 p elimintion, 1 3 q elimintion, 1 4 [p q] Assumption 5 q Modus ponens 4,2 6 F ALSE Contrdiction 3,5 7 (p q) Reduction to surdity 4,6 8 (p q) (p q) introduction 1,7 9 [ (p q)] Assumption 10 [ (p q)] Assumption 11 [p] Assumption 12 [q] Assumption 13 p q introduction 11,12 14 F ALSE contrdiction 10,13 15 q Reduction to surdity 12,14 16 p q introduction 11,15 17 F ALSE Contrdiction 9,16 18 (p q) Reduction to surdity 10,17 19 (p q) elimintion 18 20 (p q) (p q) introduction 9,19 21 (p q) (p q) introduction 8,20 2

Quiz 3, Predicte Logic Dte: Septemer 21 1. (5pts) Assert tht n rry A[1,..., n] increses nd decreses once. if j is to the left of i, then the series increses going to the right of j : (j < i) (A[j] < A[j +1]) if j is to the right of i, then the series decreses coming from the left of j : (i < j) (A[j 1] > A[j]) there is point i such tht the series is incresing everywhere to the left of i, nd decresing everywhere to the right of i : i I n :( j I n : (j < i) (A[j] < A[j +1]) ( j I n : (i < j) (A[j 1] > A[j])) This ssumes tht either the first or second prt cn e sequence of one element, i.e. the whole sequence cn e just incresing or decresing. Otherwise, insert i 1 i n fter the first colon. 2. (5pts) Assert tht grph hs 2 vertices such tht every vertex is connected to one of the two. G = V nd Edge(x,y). vertex z is connected to either vertex x or vertex y : Edge(z,x) Edge(z,y) every vertex z, if it isn t x or y itself, is connected to x or y : z V : ((z x) (z y)) (Edge(z,x) Edge(z,y)) there is pir of vertices, x nd y, such tht every vertex is connected to one of the two : x V : y V : z V : ((z x) (z y)) (Edge(z,x) Edge(z,y)) 3

Quiz 4, Mthemticl Induction Dte: Septemer 28 1. (5pts) Prove 2 2n 1 is divisile y 3, n 1 (i.e. m N : 2 2n 1 = 3m) (Hint: 4 = 3+1). When n = 1, 2 2n 1 = 2 2 1 = 3, which is divisile y 3, thus proving the se cse. Assume tht for some k 1, 2 2k 1 is divisile y 3, so for some integer m k, 2 2k 1 = 3m k We would like to prove the k +1 cse, 2 2(k+1) 1 = 3m k+1 (for some integer m k+1 ) To do this, we egin with the left hnd side, nd work until we cn sustitute the inductive hypothesis, 2 2(k+1) 1 = 2 2k 2 2 1 = 4 2 2k 1 = 3 2 2k +2 2k 1 (from the hint) = 3 2 2k +3m k (y the inductive hypothesis) = 3(2 2k +m k ) = 3m k+1 (if we let m k+1 = 2 2k +m k ) This proves the inductive conclusion, thus y mthemticl induction, the theorem is proved. 4

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, we hve S n = S 0 = 0 = 2 n 1 = 2 0 1 = 1 1 = 0 Assume tht for some k 0, S k = 2 k 1 We would like to prove the k +1 cse, S k+1 = 2 k+1 1 To do this, we egin with the left hnd side, S k+1 = 2S k +1 (from the recursive definition) = 2(2 k 1)+1 (from the inductive hypothesis) = 2 2 k 2+1 = 2 k+1 1 This proves the inductive conclusion, thus y mthemticl induction, the theorem is proved. 5

Quiz 5, Progrm Verifiction Dte: Octoer 10 1. (5pts) Stte, prove, nd use the loop invrint for the following code, ssuming n 0. i 0 s 1 while i < n do s 5 2 s i i+1 s 6 s Solution: i 0 s 1 // (s = 15 i ) (i n) while i < n do // (s = 15 i ) (i n) (i < n) s 5 2 s // (s = 5 2 15i ) (i n) (i < n) i i+1 // (s = 5 2 15i 1 ) (i n) s 6 s // (s = 6 5 2 15i 1 = 15 15 i 1 = 15 i ) (i n) // (s = 15 i ) (i n) (i < n) Note tht (i n) (i < n) implies i = n, so s = 15 n t the end. 6

2. (5pts) Stte, prove, nd use the loop invrint for the following code, ssuming n 0. m n y 1 z x while m < 0 do if ODD(m) then y y z z z z m FLOOR(m/2) Solution: m n y 1 z x // (yz m = x n ) (m 0) while m < 0 do // (yz m = x n ) (m 0) (m > 0) if ODD(m) then y y z // (yz 2 m 2 = x n ) (m 0) (m > 0) z z z // (yz m 2 = x n ) (m 0) (m > 0) m FLOOR(m/2) // (yz m = x n ) (m 0) // (yz m = x n ) (m 0) (m > 0) Note tht (m 0) (m > 0) implies m = 0, so y = x n t the end. 7

Quiz 6, Mthemticl Induction II Dte: Octoer 24 1. (5pts) Prove n i=0 i = n+1 1 1, 1, n 0. When n = 0, n i=0 i = 0 i=0 i = 0 = 1, nd n+1 1 1 = 0+1 1 1 = 1 1 = 1, which proves the se cse. Assume tht for some k 0, k i=0 i = k+1 1 1 with 1. We would like to prove the k +1 cse, k+1 i=0 i = (k+1)+1 1 1 To do this, we egin with the left hnd side, nd work until we cn sustitute the inductive hypothesis, k+1 i = k+1 + i=0 k i=0 i = k+1 + k+1 1 1 = k+1 ( 1)+ k+1 1 1 = k+1+1 1 1 (from the inductive hypothesis) This proves the inductive conclusion, thus y mthemticl induction, the theorem is proved. 8

2. (5pts) Let S n+1 = S n + ( 1 2) n, n 0, S0 = 0. Prove S n = 2 ( 1 2) n 1, n 0. When n = 0, 2 ( 1 2) n 1 = 2 ( 1 2) 0 1 = 2 2 = 0 = S0 = S n, which proves the se cse. Assume tht for some k 0, S k = 2 ( 1 2) k 1. Wewouldliketoprovethek+1cse, S k+1 = 2 ( 1 2) (k+1) 1 = 2 ( 1 2) k. To do this, we egin with the left hnd side, nd work until we cn sustitute the inductive hypothesis, ( ) 1 k S k+1 = S k + (from the recursive definition) 2 ( ) 1 k 1 ( ) 1 k = 2 + (from the inductive hypothesis) 2 2 ( ) 1 k ( ) 1 k = 2 2 + 2 2 ( ) 1 k = 2 2 This proves the inductive conclusion, thus y mthemticl induction, the theorem is proved. 9

Quiz 7, Regulr Expressions Dte: Novemer 2 1. (3pts) Write ll strings of length 6 in L(r), r = ((+) ). L(r) = {,,,,,,,,,,,,,,,,,,, } 2. (3pts) Simplify (0+1) 0(0+1) +(0+1) 00(0+1). Note tht (0+1) 00(0+1) (0+1) 0(0+1), which llows us to simplify the sum to (0 + 1) 0(0 + 1). Since the string must hve first zero, this expression cn e further simplified to 1 0(0+1). 3. (4pts) Give r, L(r) = {x x contins ut not }. The set of ll strings of s nd s is given y ( + ), so the set of strings without consecutive s which does not end on n is given y ( + ). Similrly, the set of strings without consecutive s which does not egin with n is given y ( + ). Comining with the required gives the solution: r = (+) (+) 10

Quiz 8, Regulr Grmmrs Dte: Novemer 7 1. (6pts) Convert (+) into regulr grmmr with unit productions. 4 3 + 1 2 P 1 = {S 1 A 1,A 1 Λ} P 2 = {S 2 A 2,A 2 Λ} P 3 = {S 3 S 1,S 3 S 2,S 1 A 1,A 1 Λ,S 2 A 2,A 2 Λ} P 4 = {S 4 Λ,S 4 S 3,S 3 S 1,S 3 S 2, S 1 A 1,A 1 S 4,S 2 A 2,A 2 S 4 } Using P 4 s the finl nswer, the strt symol is S 4. 11

2. (4pts) Convert into regulr grmmr: {S A,S B,A A,A B,B Λ,B A}. Solution: S A S B A A A B B Λ B A S A S Λ S B B A B B 12

Quiz 9, Regulr Grmmr Conversion Dte: Novemer 14 1. (6pts) Convert {S S, S B, A B, A S, B A, B Λ} into regulr expression. First dd S, H, nd missing loopcks. S S S B S S A B A S A A B A B H B B H Λ To remove S then A then B, egin y removing S. S S / S S / S B : S B A S / S S / S B : A B After removing S the remining productions re: S B A + B A A B A B H B B H Λ Remove A B A / A A / A + B : B (+ )B After removing A, the remining productions re: S B B H B Λ+(+ )B H Λ Remove B S B / B Λ+(+ )B / B H : Regulr expression: ((+ )) 13

2. (6pts) Convert {S S, S B, A B, A S, B A, B Λ} into deterministic regulr grmmr. V {S} V {S} V {S} V {B} V {S,B} V {S} V {S,B} V {A,B} V {A} V {S,B} V {A} V V {A,B} V {S,B} V {A,B} V {A} V {B} V V {B} V {A} V {B} Λ V {S,B} Λ V {A,B} Λ 14

Quiz 10, Finite Automt Dte: Novemer 21 1. (5pts) Build DFA for Σ = {,,c}, L = {x x contins t most 2 s nd t lest two cs}. strt q 0,0 q 1,0 q 2,0 c c c q 0,1 q 1,1 q 2,1 c c c q 0,2 q 1,2 q 2,2,c,c,c 15

2. (5pts) Build DFA for Σ = {,}, L = {x x contins efore ny }. q 0 - no s or s seen q - lst seen (ut not ), no or yet q - lst pir seen is, no or yet q - lst seen (ut not ), no or yet q - lst pir seen is, no or yet q s - seen first q t - seen first (trp stte; does not need to e shown) strt q 0 q q q s, q q q t, 16

Quiz 11, Finite Automt II Dte: Novemer 30 1. (3pts) Give DFA for L(P), P =. strt, 2. (4pts) Give DFA for L(r), r = (+). strt 3. (3pts) Prove L = { i j i > j} is not regulr. Let S e the set S = { i i 0}. Pick ny distinct pir x = i nd y = j from S, nd without loss of generlity ssume tht i > j (since x nd y re different, then one of them hs to e the longer one, so we ll cll x the longer one). Let z = j, so z is the sme length s y nd shorter thn x. It follows tht xz = i j L ut yz = j j / L. This mens tht no mtter which distinct pir of elements re picked from S, there is some string which cn e ppended to ech of them which will llow them to e distinguished y stte mchine. Since every string in S is distinguishle y stte mchine, ny stte mchine which recognizes the lnguge must hve n infinite numer of sttes to ccommodte for ech of the strings in S. No finite utomt cn hve n infinite numer of sttes, therefore the lnguge L is not regulr. 17

Quiz 12, Context-Free Grmmrs Dte: Decemer 7 1. (5pts) Give CFG for { i j c k j = 2i+3k}. Since j = 2i+3k, the resulting string cn e rewritten s i 2i+3k c k = ( i 2i )( 3k c k ). Let A generte strings of the form i 2i nd C generte strings of the form 3k c k. Then the following CFG will generte the lnguge: S AC A A Λ C Cc Λ 2. (5pts) Give CFG for { i j c k i+j > k}. We note tht since i + j > k, we cn find some i nd j such tht i +j = k nd oth i i nd j j. Thus, i = i +n nd j = j +m for some non-negtive n nd m. Furthermore, i+j = (i +n)+(j +m) = (i +j )+(n+m) = k +(n+m), so n+m must e greter thn zero. Theresultingstringcnerewritten i +n j +m c i +j = i ( n m )( j c j )c i. Let A generte strings of the form n, B generte strings of the form m, X generte nonempty strings of the form n m, nd C generte strings of the form j c j. Then the following CFG will generte the lnguge: S Sc XC A A Λ B B Λ X AB AB C Cc Λ 18