14
SFWR ENG 2FA3. Solution to the Assignment #3 Total = 141, 100%= 130 The solutions below are often very detailed on purpose. Such level of details is not required from students’ solutions. Some questions have more than one solution. If you think your solution has been marked wrongly, write a short memo stating where marking in wrong and what you think is right, and resubmit to me during class, office hours, or just slip under the door to my office. 1[20]. Exercise 12.42 (page 247 of the Gries-Schneider textbook). Recall the checklist for proving loop correct: (a) P is true before execution of the loop. (b) P is a loop invariant: {P B} S {P}. (c) Execution of the loop terminates. (d) R holds upon termination: P ∧¬B = R. (a)[5] Recall F 0 = 0, F 1 = 1 and F n = F n-1 + F n-2 for n > 1, and additionally F -1 = 1, F 1 = F 0 + F -1 . {Q : n 0} k, b, c := 0, 1, 0; {invariant P :0 k n b = F k-1 c = F k } do k 6= n k, b, c := k + 1, c, b + c od {R : c = F n } Hence: (a) Since k, b, c := 0, 1, 0; sets k = 0, b = 1, c = 0 then P is clearly true before exe- cution of the loop. (b) It suffices to show that P[k, b, c := k + 1, c, b + c] P B. (0 k n b = F k-1 c = F k )[k, b, c := k + 1, c, b + c] ⇐⇒ h rules for concurrent substitution i 0 k + 1 n c = F k+1-1 b + c = F k+1 ⇐⇒ h arithmetic i -1 k n - 1 c = F k b + c = F k+1 ⇐⇒ h F k+1 = F k + F k-1 and arithmetic i -1 k < n c = F k b = F k-1 1

SFWR ENG 2FA3. Solution to the Assignment #3se2fa3/Assignments/2014/as3sol.pdfSFWR ENG 2FA3. Solution to the Assignment #3 Total = 141, 100%= 130 The solutions below are often very

Embed Size (px)

Citation preview

Page 1: SFWR ENG 2FA3. Solution to the Assignment #3se2fa3/Assignments/2014/as3sol.pdfSFWR ENG 2FA3. Solution to the Assignment #3 Total = 141, 100%= 130 The solutions below are often very

SFWR ENG 2FA3. Solution to the Assignment #3

Total = 141, 100%= 130The solutions below are often very detailed on purpose. Such level of details is not

required from students’ solutions. Some questions have more than one solution.If you think your solution has been marked wrongly, write a short memo stating

where marking in wrong and what you think is right, and resubmit to me during class,office hours, or just slip under the door to my office.

1[20]. Exercise 12.42 (page 247 of the Gries-Schneider textbook).

Recall the checklist for proving loop correct:

(a) P is true before execution of the loop.

(b) P is a loop invariant: {P∧B} S{P}.

(c) Execution of the loop terminates.

(d) R holds upon termination: P∧¬B =⇒ R.

(a)[5] Recall F0 = 0,F1 = 1 and Fn = Fn−1 +Fn−2 for n > 1, and additionallyF−1 = 1,F1 = F0 +F−1.

{Q : n≥ 0}k,b,c := 0,1,0;{invariant P : 0≤ k ≤ n∧b = Fk−1∧ c = Fk}do k 6= n→ k,b,c := k+1,c,b+ c od{R : c = Fn}

Hence:

(a) Since k,b,c := 0,1,0; sets k = 0,b = 1,c = 0 then P is clearly true before exe-cution of the loop.

(b) It suffices to show that P[k,b,c := k+1,c,b+ c]⇐ P∧B.

(0≤ k ≤ n∧b = Fk−1∧ c = Fk)[k,b,c := k+1,c,b+ c]

⇐⇒ 〈 rules for concurrent substitution 〉0≤ k+1≤ n∧ c = Fk+1−1∧b+ c = Fk+1

⇐⇒ 〈 arithmetic 〉−1≤ k ≤ n−1∧ c = Fk ∧b+ c = Fk+1

⇐⇒ 〈 Fk+1 = Fk +Fk−1 and arithmetic 〉−1≤ k < n∧ c = Fk ∧b = Fk−1

1

Page 2: SFWR ENG 2FA3. Solution to the Assignment #3se2fa3/Assignments/2014/as3sol.pdfSFWR ENG 2FA3. Solution to the Assignment #3 Total = 141, 100%= 130 The solutions below are often very

⇐ 〈 arithmetic and logic 〉0≤ k ≤ n∧b = Fk−1∧ c = Fk ∧ k 6= n ⇐⇒ P∧B

Hence P∧B =⇒ P[k,b,c := k+1,c,b+c], so P is a loop invariant for this loop.

(c) Execution of the loop clearly terminates as each time k is increased by 1 and theloop stops when k = n.

(d) We have P∧¬B ⇐⇒ P∧ (k = n) ⇐⇒(0≤ k ≤ n∧b = Fk−1∧ c = Fk)∧ k = n ⇐⇒b = Fn−1∧ c = Fn =⇒ (c = Fn) ⇐⇒ R.

Hence R holds upon termination.

(b)[5] Prove the correctness of the below algorithm

{Q : n≥ 0}x,k := 0,0;{invariant P : 0≤ k ≤ n∧ x = ∑(i | 0≤ i < k : b[i])}do k 6= n→ x,k := x+b[k],k+1 od{R : x = ∑(i | 0≤ i < n : b[i])}

Hence:

(a) Since x,k := 0,0 sets x = 0,k = 0 then P is clearly true before execution of theloop.

(b) It suffices to show that P[x,k := x+b[k],k+1]⇐ P∧ (k 6= n).

(0≤ k ≤ n∧ x = ∑(i | 0≤ i < k : b[i]))[x,k := x+b[k],k+1]

⇐⇒ 〈 rules for concurrent substitution in quantifiers 〉0≤ k+1≤ n∧ x+b[k] = ∑(i | 0≤ i < k+1 : b[i])

⇐⇒ 〈 arithmetic 〉−1≤ k ≤ n−1∧ x = ∑(i | 0≤ i < k+1 : b[i])−b[k]

⇐⇒ 〈 domain split for quantifiers 〉−1≤ k ≤ n−1∧ x = ∑(i | 0≤ i < k : b[i])+b[k]−b[k]

⇐ 〈 arithmetic and logic 〉(0≤ k ≤ n∧ x = ∑(i | 0≤ i < k : b[i]))∧ (k 6= n)

Hence P∧B =⇒ P[x,k := x+b[k],k+1], so P is a loop invariant for this loop.

(c) Execution of the loop clearly terminates as each time k is increased by 1 and theloop stops when k = n.

(d) We have P∧¬B ⇐⇒ P∧ (k = n) ⇐⇒(0≤ k ≤ n∧ x = ∑(i | 0≤ i < k : b[i]))∧ (k = n) ⇐⇒x = ∑(i | 0≤ i < n : b[i]) ⇐⇒ R.

Hence R holds upon termination.

2

Page 3: SFWR ENG 2FA3. Solution to the Assignment #3se2fa3/Assignments/2014/as3sol.pdfSFWR ENG 2FA3. Solution to the Assignment #3 Total = 141, 100%= 130 The solutions below are often very

(c)[5] Prove the correctness of the below algorithm

{Q : n≥ 0}x,k := 0,n;{invariant P : 0≤ k ≤ n∧ x = ∑(i | k ≤ i < n : b[i])}do k 6= 0→ x,k := x+b[k−1],k−1 od{R : x = ∑(i | 0≤ i < n : b[i])}

Hence:

(a) Since x,k := 0,0 sets x = 0,k = 0 then P is clearly true before execution of theloop.

(b) It suffices to show that P[x,k := x+b[k−1],k−1]⇐ P∧ (k 6= n).

(0≤ k ≤ n∧ x = ∑(i | k ≤ i < n : b[i]))[x,k := x+b[k−1],k−1]

⇐⇒ 〈 rules for concurrent substitution in quantifiers 〉0≤ k−1≤ n∧ x+b[k−1] = ∑(i | k−1≤ i < n : b[i])

⇐⇒ 〈 arithmetic 〉1≤ k ≤ n+1∧ x = ∑(i | k−1≤ i < n : b[i])−b[k−1]

⇐⇒ 〈 domain split for quantifiers 〉1≤ k ≤ n+1∧ x = ∑(i | k−1 < i < n : b[i])−b[k−1]+b[k−1]

⇐ 〈 arithmetic and logic 〉(0≤ k ≤ n∧ x = ∑(i | k ≤ i < n : b[i]))∧ (k 6= 0)

Hence P∧B =⇒ P[x,k := x− b[k− 1],k− 1], so P is a loop invariant for thisloop.

(c) Execution of the loop clearly terminates as each time k is decreased by 1 and theloop stops when k = 0.

(d) We have P∧¬B ⇐⇒ P∧ (k = n) ⇐⇒()∧ (k = 0) ⇐⇒x = ∑(i | 0≤ i < n : b[i]) ⇐⇒ R.

Hence R holds upon termination.

(d)[5] Prove the correctness of the below algorithm

{Q : 0≤ X ∧0≤ Y}x,y := X ,Y ;{invariant P : x gcd y = X gcd Y ∧0≤ x∧0≤ y}do y 6= 0→ x,y := y,x mod y od{R : X gcd Y = x}

Hence:

3

Page 4: SFWR ENG 2FA3. Solution to the Assignment #3se2fa3/Assignments/2014/as3sol.pdfSFWR ENG 2FA3. Solution to the Assignment #3 Total = 141, 100%= 130 The solutions below are often very

(a) Since x,y := X ,Y sets x = X ,y = Y then P is clearly true before execution of theloop.

(b) It suffices to show that P[x,y := y,x mod y]⇐ P∧ (y 6= 0).

(x gcd y = X gcd Y ∧0≤ x∧0≤ y)[x,y := y,x mod y]

⇐⇒ 〈 rules for concurrent substitution 〉(y gcd (x mod y) = X gcd Y ∧0≤ y∧0≤ x mod y

⇐⇒ 〈 since x gcd y = y gcd (x mod y), see page 248 ofGries-Schneider textbook 〉

(x mod y = X gcd Y ∧0≤ y∧0≤ x mod y

⇐ 〈 since x mod y < x 〉x gcd y = X gcd Y ∧0≤ x∧0≤ y

⇐ 〈 as α∧β⇒ α 〉(x gcd y = X gcd Y ∧0≤ x∧0≤ y)∧ (y 6= 0)

Hence P∧B =⇒ P[x,y := y,x mod y], so P is a loop invariant for this loop.

(c) Execution of the loop clearly terminates as y < x mod y so each time y is in-creased by at least 1 and the loop stops when y = 0.

(d) We have P∧¬B ⇐⇒ P∧ (y = 0) ⇐⇒(x gcd y = X gcd Y ∧0≤ x∧0≤ y)∧ (y = 0) ⇐⇒X gcd Y = x gcd y = x gcd 0 = x ⇐⇒ R.

Hence R holds upon termination.

2.[7] Let M be the following deterministic finite automaton: Q = {q0,q1,q2},Σ = {a,b}, q0 is the initial state, F = {q2}, and δ : Q×Σ→ Q

δ a bq0 q0 q1q1 q2 q1q2 q2 q0

(a)[2] Give the state diagram (graphical representation) of M

4

Page 5: SFWR ENG 2FA3. Solution to the Assignment #3se2fa3/Assignments/2014/as3sol.pdfSFWR ENG 2FA3. Solution to the Assignment #3 Total = 141, 100%= 130 The solutions below are often very

 

 

 

 

 

 

 

 

 

 

C q0

a

b

ba

a

b

q1

q2

(b)[4] Trace the computations (draw paths consisting of states and actions) that pro-cess the sequences:

i[1] abaaq0→ a→ q0→ b→ q1→ a→ q2→ a→ q2

ii[1] bbbabbq0→ b→ q1→ b→ q1→ b→ q1→ a→ q2→ b→ q0→ b→ q1

iii[1] bababaq0→ b→ q1→ a→ q2→ b→ q0→ a→ q0→ b→ q1→ a→ q2

iv[1] bbbaaq0→ b→ q1→ b→ q1→ b→ q1→ a→ q2→ a→ q2

c.[2] Which of the sequences from (b) are accepted by M?

abaa,bababa,bbbaa

3.[5] Let M be the following deterministic finite automaton: Q = {q0,q1,q2},Σ = {a,b}, q0 is the initial state, F = {q0}, and δ : Q×Σ→ Q

δ a bq0 q1 q0q1 q1 q2q2 q1 q0

(a)[2] Give the state diagram (graphical representation) of M

5

Page 6: SFWR ENG 2FA3. Solution to the Assignment #3se2fa3/Assignments/2014/as3sol.pdfSFWR ENG 2FA3. Solution to the Assignment #3 Total = 141, 100%= 130 The solutions below are often very

 

 

 

 

 

 

C q0

b

a

ab

b

q1

q2

a

(b)[1] Trace the computations (draw paths consisting of states and actions) that pro-cess the sequence babaab:

q0→ b→ q0→ a→ q1→ b→ q2→ a→ q1→ a→ q1→ b→ q2

(c)[2] Give an example of two sequences that are accepted by M and another twosequence that are not accepted by M.

abb - accepted, ba - rejected

3a.[15] Consider a typical vending machine that dispenses a person’s choice ofcandy after it has received a total of 30 cents in nickels (5 cents), dimes (10 cents),and quarters (25 cents). In this case, the automaton’s alphabet consists of three differ-ent sizes of coins, a state of the machine is the total amount of money that has beenreceived since the last candy was dispensed, the initial state is that of not having re-ceived any coins since the last candy was dispensed, and a final state is that of havingreceived at least 30 cents.

(a)[10] Assuming that the machine is not designed to make change and will thereforemerely consume any overpayment, define a finite deterministic automaton thatmodels the vending machine.

6

Page 7: SFWR ENG 2FA3. Solution to the Assignment #3se2fa3/Assignments/2014/as3sol.pdfSFWR ENG 2FA3. Solution to the Assignment #3 Total = 141, 100%= 130 The solutions below are often very

 

 

 

 

 

C 0 5

10

15

20

25 $30

nickel

dime

quarter

quarternickel

dime

quarternickeldime

dime

quarternickel

quarter

nickel quarter

dime

quarter

dimenickel

quarter

dime

nickel

(b)[5.] Give a finite deterministic automaton for a machine that returns change.

Similarly as (a) above. The state ‘≥ 30’ has to be replaced by the states ‘30 andno change’, ‘35-change 5’, ‘40-change 10’, ‘45-change 15’, ‘50-change 20’ and‘55-change 25’. For instance from the state ‘20’, nickel goes to ‘30’, dime to‘34’, quarter to ‘25’, etc.

4.[15] Design deterministic finite state automata for the following sets:

(c)[5] the set of strings x ∈ {0,1}∗ such that #0(x) is even and #1(x) is a multiple ofthree.

Note that for each natural number x, x mod 2 is either 0 or 1, while x mod 3 is 0,1 or 2.The automaton will have 6 states, denoted as (k, l), where k = 0,1 and l = 0,1,2. Thestate (k, l) is interpreted as a string that starts from the initial state and ends in (k, l)has k = #0(x) mod 2 and l = #1(x) mod 3.

The automaton is given below:

7

Page 8: SFWR ENG 2FA3. Solution to the Assignment #3se2fa3/Assignments/2014/as3sol.pdfSFWR ENG 2FA3. Solution to the Assignment #3 Total = 141, 100%= 130 The solutions below are often very

(d)[10] the set of strings over the alphabet {a,b} containing at least three occur-rences of three consecutive b’s, overlapping permitted (e.f., the string bbbb should beaccepted).

First note that the following automaton accepts all strings over the alphabet {a,b}containing at least one occurrence of three consecutive b’s, overlapping permitted.

Hence the full solutions looks as follows:

Note that the solutions with regular expressions or nondeterministic automata are moreintuitive:

(a+b)∗bbb(a+b)∗bbb(a+b)∗bbb(a+b)∗

and

8

Page 9: SFWR ENG 2FA3. Solution to the Assignment #3se2fa3/Assignments/2014/as3sol.pdfSFWR ENG 2FA3. Solution to the Assignment #3 Total = 141, 100%= 130 The solutions below are often very

5[16]. Consider the following two deterministic finite state automata.

Use the product construction to produce deterministic automata accepting:

(a)[8] the intersection of the two sets accepted by these automata.

(b)[8] the union of the two sets accepted by these automata.

9

Page 10: SFWR ENG 2FA3. Solution to the Assignment #3se2fa3/Assignments/2014/as3sol.pdfSFWR ENG 2FA3. Solution to the Assignment #3 Total = 141, 100%= 130 The solutions below are often very

6.[10] Let M = (Q,Σ,δ,s,F) be an arbitrary DFA. Prove by induction on |y| that forall strings x,y ∈ Σ∗ and q ∈ Q,

δ̂(q,xy) = δ̂(δ̂(q,x),y),

where δ̂ is the extended version of δ.Proof. It is clearly true for y = ε as for every q we have δ̂(q,ε) = q, soδ̂(δ̂(q,x),ε) = δ̂(q,x) = δ̂(q,xε).

Assume it holds for y and consider ya where a ∈ Σ.

δ̂(q,xya)= 〈 definition of δ̂ 〉δ(δ̂(q,xy),a)= 〈 inductive assumption for y 〉δ(δ̂(δ̂(q,x),y),a)= 〈 definition of δ̂ 〉δ̂(δ̂(q,x),ya)

So we are done.

7.[8] Convert the following nondeterministic automaton into a deterministic one (Ex-ercise 1 from page 302 of the Kozen textbook).

Solution:

10

Page 11: SFWR ENG 2FA3. Solution to the Assignment #3se2fa3/Assignments/2014/as3sol.pdfSFWR ENG 2FA3. Solution to the Assignment #3 Total = 141, 100%= 130 The solutions below are often very

8.[10] Show that for any A∈ Σ∗, if A is regular then so is rev A (Exercise 2 from page302 of the Kozen textbook).

The solution is illustrated by the following example. If the first automaton acceptsA then all automata after step 2 accept rev A.

In principle, we first reverse arrows, next make initial state final and all final state ini-tial. Now we have to solve the problem of multiple initial states. The simplest solutionis to use ε-moves, but we can live without them too.

Formal solutions is the following. Let M = (Q,Σ,δ,s0,F) be a deterministic au-tomaton such that L(M) = A.

Define a non-deterministic automaton Mrev = (Qrev,Σ,∆rev,s00,{s0}), where s0

0 /∈Q,Qrev = Q∪{s0

0}, and ∆rev : Qrev×Σ→ 2Qrevis defined as follows:

∆rev(p,a) = {q | δ(q,a) = p},

11

Page 12: SFWR ENG 2FA3. Solution to the Assignment #3se2fa3/Assignments/2014/as3sol.pdfSFWR ENG 2FA3. Solution to the Assignment #3 Total = 141, 100%= 130 The solutions below are often very

for all p ∈ Q and a ∈ Σ, and

∆rev(s0

0,a) = {q | ∃(p | p ∈ F : δ(q,a) = p},

for all a ∈ Σ.Clearly L(Mrev) = L(M) = A.This suffices for the proof as for every regular set A there exists a deterministic

automaton M such that L(M) = A. For the initial deterministic automaton this formalconstruction will give an automaton after step 3.

An extension of the formal construction for the initial non-deterministic automaton(not needed for the proof per se), is the following.

Let M = (Q,Σ,∆,s0,F) be a non-deterministic automaton such that L(M) = A.Define a non-deterministic automaton Mrev = (Qrev,Σ,∆rev,s0

0,{s0}), where s00 /∈Q,

Qrev = Q∪{s00}, and ∆rev : Qrev×Σ→ 2Qrev

is defined as follows:

∆rev(p,a) = {q | p ∈ ∆(q,a)},

for all p ∈ Q and a ∈ Σ, and

∆rev(s0

0,a) = {q | ∃(p | p ∈ F : p ∈ ∆(q,a)},

for all a ∈ Σ.

9.[10] Give regular expressions for each of the following subsets of {a,b}∗:

(b)[5] {x | x contains an odd number of b’s}.

a∗(ba∗ba∗)∗ba∗

(c)[5] {x | x contains an even number of a’s or an odd number of b’s}.

b∗(ab∗ab∗)∗+a∗(ba∗ba∗)∗ba∗

10.[15] Give deterministic finite automata accepting the sets of strings matching thefollowing regular expressions.

(a)[10] (000∗+111∗)∗

It helps to decipher this expression. Let A = φ((000∗+ 111∗)∗), where φ is inter-pretation. Then x∈ A if and only if x = ε or x = x1 . . .xn such that each xi either belongsto 000∗ or to 111∗ (more precisely either to φ(000∗) or φ(111∗), but we often identifya regular expression with the language it defines). In other words, each 0 must be fol-lowed by one ore more 0’s and each 1 must be followed by one ore more 1’s, whichleads to the following simple solution:

12

Page 13: SFWR ENG 2FA3. Solution to the Assignment #3se2fa3/Assignments/2014/as3sol.pdfSFWR ENG 2FA3. Solution to the Assignment #3 Total = 141, 100%= 130 The solutions below are often very

(b)[5] (01+10)(01+10)(01+10)

11.[11] Show that the following sets are not regular.

(a)[5] {anbm | n = 2m}

Recall the Pumping Lemma:

Lemma 1 (Most popular version)If A is a regular set, then there is a number p such that for every s ∈ A, if |s| ≥ p, thens = xyz and

1. for each i≥ 0, xyiz ∈ A

2. |y|> 0

3. |xy| ≤ p

We will use this lemma to show that A = {anbm | n = 2m} is not regular.

• We take p be the number from pumping lemma and consider s = apb2p. Hence|s|= 3p > p.

• Let x,y,z be such that s = xyz,|y|> 0, i.e. y 6= ε,and |xy| ≤ p.

• This means that y = ak where 1≤ k ≤ p.

• Now we have xy0z = ap−kb2p, and p− k 6= p, so xy0z /∈ A!

13

Page 14: SFWR ENG 2FA3. Solution to the Assignment #3se2fa3/Assignments/2014/as3sol.pdfSFWR ENG 2FA3. Solution to the Assignment #3 Total = 141, 100%= 130 The solutions below are often very

• Hence the Pumping Lemma is not satisfied, i.e. A is not regular.

(d)[6] The set PAREN of balanced strings of parentheses ( ).

Recall that if A and B are regular than A∩B is regular too.The set B = (∗)∗ is clearly regular (it is just a∗b∗ with a = ( and b =)).Hence if PAREN is regular then PAREN∩B is also regular.But PAREN∩B = {(n)n | n > 0}, and the set {(n)n | n > 0} is NOT regular (it is

just {anbn | n > 0} with a = ( and b =)).Therefor PAREN is not regular.

14