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