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