Concordia University Theoretical Computer … University Theoretical Computer Science Solution to...

9

Click here to load reader

Transcript of Concordia University Theoretical Computer … University Theoretical Computer Science Solution to...

Page 1: Concordia University Theoretical Computer … University Theoretical Computer Science Solution to Assignment 1 Winter 2015 1. The reversal of a string w, denoted by wR, is the string

Concordia UniversityTheoretical Computer Science

Solution to Assignment 1Winter 2015

1. The reversal of a string w, denoted by wR, is the string “spelled backwards”.For example (cat)R = tac. Reversal is defined inductively as follows:

ϵR = ϵ, (wa)R = a(wR).

(Here a is a symbol in the alphabet Σ, and w is a string in Σ∗.)

Let u, v ∈ Σ∗. Prove that(uv)R = vRuR.

Hint: Use induction on |v|.

Solution:

By induction on length of v,

Basis: if |v| = 0 then v = ϵ, and (uv)R = (uϵ)R = (u)R = uR = ϵuR = ϵRuR.

IH: If |v| = n we have (uv)R = vRuR

IS: Now we show that if |v| = n+ 1 then the claim in the inductive hypothesisis also true.

|v| = n+ 1 ⇒ v = xa, where |x| = n and a ∈ Σ.

Then (uv)R = (uxa)R = a(ux)R = a(xRuR) = (axR)uR = (xa)RuR = vRuR.

Page 2: Concordia University Theoretical Computer … University Theoretical Computer Science Solution to Assignment 1 Winter 2015 1. The reversal of a string w, denoted by wR, is the string

2. Let Σ = {a, b}. For each of the languages below, give an example of a string inthe language, and a string not in the language.

(a) L1 = {w ∈ Σ∗ : w = uuRu, for some u ∈ Σ2}Solution:

w = aaaaaa ∈ L1, since u = aa ⇒ w = uuRu

w = aa /∈ L1, since @u ∈ Σ2 such that w = aa = uuRu

(b) L2 = {w ∈ Σ∗ : ww = www}Solution:

w = ϵ ∈ L2, since ww = ϵ = www

w = aa /∈ L2, since ww = aaaa ̸= aaaaaa = www

(c) L3 = {w ∈ Σ∗ : uvw = wvu, for some u, v ∈ Σ∗}.Solution:

w = ab ∈ L3, since u = ab, v = aba ⇒ uvw = ababaab = wvu.

L3 = Σ∗, since for any w, we can choose u = w and v = ϵ.

(d) L4 = {w ∈ Σ∗ : www = uu, for some u ∈ Σ∗}.Solution:

w = aa ∈ L4, since u = aaa ⇒ www = aaaaaa = uu.

w = a /∈ L4, since www = aaa and @u such that uu = aaa.

Page 3: Concordia University Theoretical Computer … University Theoretical Computer Science Solution to Assignment 1 Winter 2015 1. The reversal of a string w, denoted by wR, is the string

3. Construct a DFA for each of the following languages.

(a) {w ∈ {a, b}∗ : bb appears at most once as a substring of w}

q5

q1 q2 q3 q4b b a

b

ba

a

a, b

a

(b) {w ∈ {a, b}∗ : bab is not a substring of w}

q4

q1 q2 q3b a

b

a

a b

a, b

(c) The set of strings that either begin or end (or both) with ab.

q1 q2

q4 q5

q6

q3a b

b a

a

ba

ba

a, b

b

Page 4: Concordia University Theoretical Computer … University Theoretical Computer Science Solution to Assignment 1 Winter 2015 1. The reversal of a string w, denoted by wR, is the string

(d) {w ∈ {a, b}∗ : w contains an odd number of a′s and ends in at least two b′s}

q

p r s

a

b

b

a aa

b

b

Give your DFA’s as transition diagrams.

a b

→ q p qp q rr q s∗s q s

Page 5: Concordia University Theoretical Computer … University Theoretical Computer Science Solution to Assignment 1 Winter 2015 1. The reversal of a string w, denoted by wR, is the string

4. Let L = {w ∈ {0, 1}∗ : w has an odd no. of 1’s }, and let A be the DFA withtabular representation:

A 0 1

→ p p q

⋆ q q p

Prove that L = L(A). Hint: Do the L(A) ⊆ L part of the proof by inductionon the the length of the string processed by A. You need a mutual inductionwith a claim for state p and a claim for state q.

Solution:

To prove w ∈ L(A) ⇒ w ∈ L we use an induction on length of w. We claimthat

(1) If δ̂(p, w) = p, then w has even number of 1’s.

(2) If δ̂(p, w) = q, then w has odd number of 1’s.

Basis: |w| = 0 then w = ϵ, so obviously (1) is true. Also, it is clear thatδ̂(p, ϵ) = p ̸= q, so (2) is vacuously true.

IH: (1) and (2) are true for any string w of length n.

IS: Now prove (1) and (2) for w = xa, where |x| = n and a ∈ Σ.

(1): If δ̂(p, xa) = p then δ̂(p, x) is p and a = 0 or q and a = 1, as can be seenfrom the transition diagram of A.

If δ̂(p, x) = p then by the IH (1), the string x has even number of 1’s. Thereforew = x0 which has even number of 1’s.

If δ̂(p, x) = q then by the IH (2), the string x has odd number of 1’s, so w = x1has even number of 1’s.

(2): If δ̂(p, xa) = q then δ̂(p, x) is p and a = 1 or q and a = 0, as can be seenfrom the transition diagram of A.

If δ̂(p, x) = p then by the IH (1), the string x has even number of 1’s. Thereforew = x1 which has odd number of 1’s.

If δ̂(p, x) = q then by the IH (2), the string x has odd number of 1’s, so w = x0has odd number of 1’s. This completes induction.

For the other direction, we have:

w ̸∈ L(A) ⇒ δ̂(p, w) = p

⇒ w has even number of 1’s

⇒ w ̸∈ L

So it never ends in a final state with an even number of 1’s. Therefore, thisdirection is also proved, that is L ⊆ L(A).

Page 6: Concordia University Theoretical Computer … University Theoretical Computer Science Solution to Assignment 1 Winter 2015 1. The reversal of a string w, denoted by wR, is the string

5. Construct an NFA for each of the following languages.

(a) The set of strings over {0, 1, . . . , 9}, such that the final digit has not ap-

peared before

q0

q1

q2

q3

q4

q5

q6

q7

q8

q9

qfqs

1, .., 9

0

0&2, ..., 9

1

0, 1&3, ..., 92

0, 1, 2&4, ..., 9 3

0, ..3&5, .., 9 4

0, ..., 4&6, ..., 9 5

0, ..., 5&7, ..., 9 60, ..., 6&8, 9

70, .., 7&9

80, ..., 8

9

0, ..., 9

1, .., 9

0&2, ..., 9

0, 1&3, ..., 9

0, 1, 2&4, ..., 90, 1, 2&4, ..., 9

0, ..3&5, .., 9

0, ..., 4&6, ..., 9

0, ..., 5&7, ..., 9

0, ..., 6&8, 9

0, .., 7&9

0, ..., 8

Page 7: Concordia University Theoretical Computer … University Theoretical Computer Science Solution to Assignment 1 Winter 2015 1. The reversal of a string w, denoted by wR, is the string

(b) The set of strings over {0, 1}, such that there are two 0’s separated by anumber of positions that is a multiple of 4. Note that 0 is an allowablemultiple of 4.

q0 q1

q4

q3

q3

q20 0

0, 10, 1

0, 10, 1

0, 1 0, 1

6. Let Σ = {a, b}.

(a) Construct an NFA that accepts the strings in Σ∗ where at least one of thelast two symbols is an a.

q0 q1

q3

q2a a, b

aa, b

a, b

Page 8: Concordia University Theoretical Computer … University Theoretical Computer Science Solution to Assignment 1 Winter 2015 1. The reversal of a string w, denoted by wR, is the string

(b) Convert your NFA to a DFA using the subset construction. Give the DFA

both in tabular form and as a transition diagram.

A B

C

E

D

b

b

b

ba

b

a

aa

a

a b

→ A C BB D CC D E

∗D D E∗E D B

A = {q0}B = {q0, q2}C = {q0, q1, q2}D = {q0, q1, q2, q3}E = {q0, q2, q3}

7. Let Σ = {0, 1}. Design ϵ-NFA’s for the following languages.

(a) The set of string that consists of either 01 repeated one or more times or

010 repeated one or more times.

q0

q1

q2

q4 q5 q6

q7

q3ϵ

10

ϵ

0 1

ϵ

Page 9: Concordia University Theoretical Computer … University Theoretical Computer Science Solution to Assignment 1 Winter 2015 1. The reversal of a string w, denoted by wR, is the string

(b) The set of strings such that at least one of the last ten positions is a 1.

A

B

C

E

F

G

H

D

I

T J

S

R

L

Q

M

P

N

O

K

0, 1

1

0, 1

0, 1

0, 1

0.1

0, 1

0, 1

0, 1

0, 1

0, 1

0, 1

0, 1

0, 1

0, 1

0, 1

0, 1

0, 1

0, 1

0, 1

1

1

1

1

1

1

1

1

1