CS 0 Forml Methods nd Models Dn Richrds, George Mson University, Fll 2016 Quiz Solutions Quiz 1, Propositionl Logic Dte: Septemer 8 1. Prove q (q p) p q p () (4pts) with truth tle. p q p q p (q p) p q q (q p) p q p T T F F T F T T T F F F T T T T F T T T T F T T F F T F F T F F () (4pts) lgericlly. q (q p) p q ((q p) p) q ((q p) p) (q (q p)) p q p Conditionl lw Lw of negtion Associtivity Susumption 2. (2pts) Prove (α β) γ ((α γ) (β γ)). (α β) γ γ (α β) (γ α) (γ β) (α γ) (β γ) Commuttivity Distriutivity Commuttivity This result cn lso e shown using truth tle. 1
Quiz 2, Rules of Inference Dte: Septemer 15 1. (4pts) Prove (( p q) q) p. 1 [( p q) q] Assumption 2 p q elimintion 1 q elimintion 1 4 p Modus tollens 2, 5 p Doule negtion 4 6 (( p q) q) p introduction 1,5 2. (6pts) Prove ((p q) r) (p (q r)). 1 [(p q) r] Assumption 2 [p] Assumption [q] Assumption 4 p q introduction 2, 5 r Modus ponens 1,4 6 q r introduction,5 7 p (q r) introduction 2,6 8 ((p q) r) (p (q r)) introduction 1,7 9 [p (q r)] Assumption 10 [p q] Assumption 11 p elimintion 10 12 q elimintion 10 1 q r Modus ponens 9,11 14 r Modus ponens 1,12 15 (p q) r introduction 10,14 16 (p (q r)) ((p q) r) introduction 9,15 17 ((p q) r) (p (q r)) introduction 8,16 2
Quiz, Predicte Logic Dte: Septemer 22 1. (4pts) Evlute i I + : j I i : j 2 = i. True. If i = 1, then I i = {1}, so j must e 1 s well. Since (1) 2 = 1, nd it is sufficient to find single vlue of i which stisfies the expression, the expression evlutes to true. 2. (6pts) Given ODD(i) nd P RIM E(i) ssert every even integer greter thn two is the sum of two primes. To express tht x is even nd greter thn 2 : ODD(x) (x > 2) To express tht x is the sum of two primes : y I + : z I + : PRIME(y) PRIME(z) (x = y +z) Altogether ( if ny x is n even integer greter thn 2, then it is the sum of two primes ): x I + : ( ODD(x) (x > 2)) ( y I + : z I + : PRIME(y) PRIME(z) (x = y +z)) Note: the ssertion given here is lso known s Goldch s conjecture. The conjecture hs no known proof s of yet, ut it hs een numericlly verified to e true for s fr s it hs een computtionlly fesile.
Quiz 4, Mthemticl Induction Dte: Septemer 29 1. (6pts) Prove y induction n i=0 i(i+1) = n(n+1)(n+2) for n 0 When n = 0, we hve 0 i=0 i(i+1) = 0(0+1) = 0 = 0(0+1)(0+2) = 0 Assume tht for some k 0, k i(i+1) = i=0 k(k +1)(k +2) We would like to prove the k +1 cse, k+1 i(i+1) = i=0 (k +1)((k +1)+1)((k +1)+2) = (k +1)(k +2)(k +) To do this, we egin with the left hnd side, nd sustitute the inductive hypothesis, k+1 i(i+1) = (k +1)((k +1)+1)+ i=0 = (k +1)((k +1)+1)+ = (k +1)(k +2) = + k i(i+1) i=0 k(k +1)(k +2) k(k +1)(k +2) (k +1)(k +2)(k +) This proves the inductive conclusion, thus y mthemticl induction, the theorem is proved. 4
2. (4pts) Give forml outline of the proof of x N : p(x). 1 p(0) Bse cse 2 [k N] Assumption [p(k)] Assumption 4 p(k +1) proof of IC 5 p(k) p(k +1) introduction,4 6 x N : p(x) p(x+1) introduction 2,5 7 x N : p(x) Mthemticl induction 1,6 5
Quiz 5, Progrm Verifiction Dte: Octoer 6 1. Assume n 0. i 0 s 1 while i < n do s 2 s i i+1 s s 6 () (pts) Give the loop invrint. (i n) (s = 4 i ) After initiliztion, i = 0 nd s = 1, so (0 n) (1 = 4 0 ). () (5pts) Prove the loop invrint. (i n) (s = 4 i ) (i < n) {s 2 s} (i n) (s = 2 4i ) (i < n) (i n) (s = 2 4i ) (i < n) {i i+1} (i n) (s = 2 4i 1 ) (i n) (s = 2 4i 1 ) {s s 6} (i n) (s = 2 4i 1 6 = 4 4 i 1 = 4 i ) (i n) (s = 4 i ) (i < n) {s 2 s;i i+1;s s 6} (i n) (s = 4i ) (c) (2pts) Apply the loop invrint. (i n) (s = 4 i ) (i < n) {s 2 s;i i+1;s s 6} (i n) (s = 4i ) (i n) (s = 4 i ){whilei < ndos 2 s;i i+1;s s 6}(i n) (s = 4i ) (i < n) 6
Quiz 6, Regulr Expressions Dte: Octoer 27 1. (5pts) Give regulr expression for Σ = {,}, L = {x x does not contin }. If string does not contin, nd there re s in the string, then they must either e preceded y n, or they must e prt of sustring of s t the eginning of the string. If (+) is the set of ll strings, then (+) is the set of strings in which every is preceded y n, nd the finl nswer is: (+) 2. (5pts) Give regulr expression for L = {x x contins n efore every }. ex. L, L There re two cses to consider, one in which there re no s nd one in which there re s. If there re no s, then the resulting string cn e expressed s. If there re s, then the string cn e constructed s follows. There is first, with t lest two s efore it, nd nything fter it. Thus, ( )(+). Altogether, the solution is: +( )(+) 7
Quiz 7, Regulr Grmmrs Dte: Novemer 1. (10pts) Convert this regulr expression into regulr grmmr with unit productions: +c. 6 + 4 5 c 2 1 P 1 = {S 1 A 1,A 1 Λ} P 2 = {S 2 Λ,S 2 S 1,S 1 A 1,A 1 S 2 } P = {S A,A Λ} P 4 = {S 4 S 2,S 2 S,S 2 S 1,S 1 A 1,A 1 S 2, S A,A Λ} P 5 = {S 5 ca 5,A 5 Λ} P 6 = {S 6 S 4,S 6 S 5,S 5 ca 5,A 5 Λ, S 4 S 2,S 2 S,S 2 S 1,S 1 A 1,A 1 S 2,S A,A Λ} Using P 6, the strt symol is S 6. 8
Quiz 8, Regulr Grmmr Conversion Dte: Novemer 10 1. (6pts) P = {S A,S B,A A,B S,A Λ}, convert to regulr expression, removing S then A then B. First, dd S, H, nd missing loopcks. S S S A S B S S A A A H B S B B H Λ Remove S. S S / S S / S A : S A S S / S S / S B : S B B S / S S / S A : B A B S / S S / S B : B B After removing S, the remining productions re: S A S B A A A H B A B (Λ+)B H Λ Remove A. S A / A A / A H : S H B A / A A / A H : B H After removing A, the remining productions re: S B S H B H B (Λ+)B H Λ 9
Remove B. S B / B (Λ+)B / B H : S () H After removing B, the remining productions re: S +() H H Λ Regulr expression: +() 2. (4pts) P = {S A,S B,B S,B A,A S}, convert to regulr grmmr without unit productions. Solution: S A S B B S B A A S B A S S B B B S 10
Quiz 9, Finite Automt Dte: Novemer 17 1. (5pts) Write DFA for L, Σ = {,,c}, L = {x if x hs n nd then x hs c}, c L, c L. Solution: q 0 - no s, s, or cs q - s ut no s or cs q - s ut no s or cs q - s nd s ut no cs q c - t lest one c q c strt q 0 c q c,,c c q, c q 11
2. (5pts) Write n NFA for L, Σ = {,}, L = {x the rd chrcter is the sme s the rd from the end}, L. Solution:,, strt,,,,,,, 12
Quiz 10, Finite Automt nd Regulr Lnguges Dte: Novemer 22 1. (pts)provel = {x xhsthesmenumerofsss}isnotregulr. Solution: Let S = { i i 0}. Let x = i nd y = j e ny pir of distinct elements in S (thus, i j). If z = i, then x nd y re distinguishle, ecuse xz L ut yz / L. Since there is n infinite numer of elements in S, ll of which re mutully distinguishle, then it is impossile to express L s regulr lnguge. 2. (4pts) Convert this into the corresponding regulr grmmr. strt S A B C Solution: S A A A A B B S B B B C B Λ C A. (pts) Prove A B = {x x A or x B, ut not oth} is closed for regulr lnguges. Solution: Let s first rewrite A B in more workle form. The set of x which reonlyinautnotb is(a B), whilethesetofxwhichreonlyinb ut not A is (B A). Altogether, this mkes A B = (A B) (B A). Since A nd B re oth regulr, nd since union, intersection, nd set complement re ll closed under regulr lnguges, then the result of n expression using only those opertions must lso e regulr. 1
Quiz 11, Context Free Grmmrs Dte: Decemer 1 1. (5pts) Give CFG for L = { i j c k j > i+k +}. If j > i+k +, then j = i+k +(l+4), for some l 0. Thus, i j c k cn e rewritten s i i+k+l+4 c k, which equls ( i i )( l )( 4 )( k c k ). ( i i ) cn e generted y A A Λ. ( l ) cn e generted y B B Λ. 4 is simply. ( k c k ) cn e generted y C Cc Λ. Altogether, this gives: S ABC A A Λ B B Λ C Cc Λ 2. (5pts) Give CFG for the set of fully prenthesized propositions using,, nd vriles,, e.g. (( ( )) ). Two different solution types would get credit. The set of fully prenthesized propositions: S ( S) (S S) (S S) The set of prenthesized expressions (which is not the sme s fully prenthesized propositions, ut still receives credit) is: S S S S S S (S) 14
Quiz 12, Pushdown Automt Dte: Decemer 8 1. (10pts) Give stte trnsition digrm for PDA for L = {x x is lnced for [,],{,}}. Solution: For every strting [, push B for squre rcket, nd for every strting {, push C for curly rce. If closing rcket or rce is encountered, pop the corresponding symol from the stck. If there re multiple consecutive sets of lnced rckets nd rces, we do not wnt the stck to ecome empty fter the first set, so when dding new opening rcket or rce, one option will e to preseve the strt symol. The solution cn e implemented in single stte. We use PDA model which does not include ccept sttes, so solutions which use ccept sttes will not receive full credit. strt [,S/B [,S/BS [,B/BB [,C/BC ],B/Λ {,S/C {,S/CS {,B/CB {,C/CC },C/Λ 15