This preview shows page 1. Sign up to view the full content.
Unformatted text preview: CSC236H: Introduction to the Theory of Computation Homework 5 Solutions
1. Give a contextfree grammar for each of the following languages. (a) L1 = {0n 1m 0m 1n  n, m ≥ 0}. Solution. S → 0S 1  C C → 1C 0  (b) L2 = {an bm ck  n, m, k ≥ 0 and n = m + k}. Solution. S → aSc  B B → aBb  (c) L3 = {an bm ck  n, m, k ≥ 0 and n = 2m + 3k}. Solution. S → aaaSc  B B → aaBb  (d) L4 = {an bm  0 ≤ n ≤ m ≤ 2n}. Solution. S → aSbb  B B → aBb  2. In a string w ∈ {0, 1}∗ , deﬁne a block to be a maximal length consecutive sequence of 0s or 1s. For example, in the string 0111000011001 there are six blocks, three blocks of 0s of lengths 1, 4, and 2, and three blocks of 1s of lengths 3, 2, and 1. Let L be the language consisting of all strings that have (somewhere) two blocks of zeros of the same length. So, for example, 011001110 ∈ L but 000110011 ∈ L. (a) Prove that L is not regular. Solution. By pumping lemma, assuming parameter n: Let x = 0n 10n . Any partition of x = uvw should satisfy uv  ≤ n and v  ≥ 1. Therefore, we should have u = 0j , v = 0k and w = 0 10n , such that j + k + = n, and k ≥ 1. If we pump v to 0, then we will get the string x = 0j + 10n , and since k ≥ 1, we know that j + = n. Therefore, by deﬁnition, x ∈ L. Hence, L does not satisfy the pumping lemma, and therefore is not regular. (b) Show that L is contextfree by giving a grammar for L. Solution. There are diﬀerent ways of doing this; here is one (let us call this grammar G): S → B  A1B  B 1A  A1B 1A B → 0B 0  010  01A10 A → 1A  0A  (c) Prove that your grammar of part (b) is correct. Solution. The goal is to prove that L = L(G). We will do this by showing the following two subgoals: (1) L(G) ⊆ L: The goal is to prove that every string that G derives has two blocks of zeros of equivalent length. Again, there are several ways of doing this. I like the following approach because of its nice and clean structure: lemma 1: A generates all possible strings over the alphabet {0, 1} (basically, the set {0, 1}∗ ). proof of lemma 1: This is an easy induction proof. The induction is on the length of the string. 1 The base case is , and the induction step is proved by considering the two simple cases of wether the strings begins with a 0 or a 1. lemma 2: B generates all strings with two blocks of zeros of the same length where the ﬁrst one appears at the beginning and the second one appears at the end of the string (let’s call this property P ). proof of lemma 2: by induction on the number of derivations n: Basis: n = 1, B ⇒ 010, which satisﬁes P . Induction Hypothesis: If B ⇒n v then v satisﬁes P . G Induction Step: prove that B ⇒n+1 w then w satisﬁes P . G If B ⇒n+1 w then B ⇒ u ⇒n w (separating the ﬁrst step from the rest). There are two options G G (ruling out 010 which was covered by the basis) for what u can be, based on the production rules for B: i. u = 0B 0: By induction hypothesis, 0B 0 ⇒n 0v 0 where v satisﬁes P . But, if v satisﬁes p then G 0v 0 (which is w) also satisﬁes P (by deﬁnition of P ). So, this case is ﬁne. ii. u = 01A10: By lemma 1, A generates some string u in {0, 1}∗ (in any number of derivation steps, including n). Therefore, B ⇒n+1 01u10. But, 01u10 satisﬁes P since it has a block of zeros of G length 1 at the beginning and at the end. So, this case is ﬁne as well. The main goal: S generates strings which have two blocks of zeros of equivalent length somewhere (meaning they belong to L). proof: Goal: assume S ⇒∗ w. Then, S ⇒ u ⇒∗ w. There are four options for what u can be: G G i. u = B : By lemma 2, we know then that B ⇒n w where w has two blocks of zeros of equivalent G length (speciﬁcally at the beginning and at the end), and therefore w ∈ L. ii. u = A1B : By lemma 1, we know that A ⇒∗ x where x ∈ {0, 1}∗ . By lemma 2, we know that G B ⇒∗ v such that v has two equivalent length blocks of zeros at the beginning and at the end. G We have w = x1v . The block of zeros at the end of v remains intact, and the 1 in the middle makes sure that the block of zeros at the beginning of v remains a block of zeros (of the same length) in x1v . Thus, w has two equivalent length blocks of zeros, and therefore, w ∈ L. iii. u = B 1A: Similar to the above case. iv. u = A1B 1A: Similar to the above case. Note that this last case did not need and induction proof, since the induction happens in the proofs of lemmas 1 and 2. (2) L ⊆ L(G): we will need the reverse of lemmas 1 and 2 for this direction. Reverse Lemma 1: any string w ∈ {0, 1}∗ can be generated by A. proof: by simple induction on the length of the string. Reverse Lemma 2: any string w with equivalent length blocks of zeros at its beginning and end can be generated by B . proof: By strong induction on the length of the string. Basis: w = 0, is vacuously true. Induction Hypothesis: If v satisﬁes P (from lemma 2) and v  < n, then B ⇒∗ v . G Induction step: If w satisﬁes P and w = n, then there are three options for what w can look like: i. The equivalent length blocks of zeros in w have length exactly 1. There are two subcases for this case: A. w = 010: in this case B ⇒w by using the production rule B → 010. G B. w = 01v 10 such that v ∈ {0, 1}∗ . In this case, B ⇒∗ w, by using the production rule G B → 01A10 and reverse lemma 1. ii. The equivalent length blocks of zeros in w have length greater than 1: in this case, we can remove one zero from the beginning and one zero from the end of w, and the remaining string will still have two equivalent length blocks of zeros at the beginning and at the end. In other words, we can write w = 0v 0 such that v satisﬁes P , and v  < n. Therefore, by induction hypothesis, B ⇒∗ v . Using the production rule B → 0B 0, we know that B ⇒∗ w. G G The main goal: The goal is to prove that if w ∈ L can be generated by G (that is, S ⇒∗ w). G By deﬁnition, w has two blocks of zeros of equivalent length (let us call this ). Therefore, we have w = x0 y 0 z such that one of the following is true: i. x = ∧ y = : Then w = 0 y 0 is a string with equivalent length blocks of zeros at its beginning and end, and therefore it can be generated by B . Since we have the production rule S → B , then it can be generated by S as well. 2 ii. x = x 1 ∧ y = , where x ∈ {0, 1}∗ : Then w = x 10 y 0 . Since we have the production rule S → A1B , and A can derive x (by reverse lemma 1), and B can derive 0 y 0 (by reverse lemma 2), then S can derive w. iii. x = ∧ y = 1y , where y ∈ {0, 1}∗ : similar to the above case. iv. x = x 1 ∧ y = 1y , where x , y ∈ {0, 1}∗ : similar to the above case. Homework Problem Set 2 Solutions
CS 375 – Fall 2002 Note that this did not need and induction proof, since the induction happens in the proofs of reverse lemmas 1 and 2. Problem 1the NFA deﬁned by the deﬁned by the below, with p the initial state and rthe initial state and r the 3. Consider Consider the NFA transition table transition table below, with p the only accepting state. only accepting state.
state p q state r ps input 0 {p, s} {r, s input}0 {r } {p, s} {} input 1 {q } {q } input 1 {s} {q } {q } q Construct a DFA that accepts the same language using the technique discussed in class. The states of your r {r } { s} deterministic machine should be the sets of states of the nondeterministic machine. s {} {q } Solution. In the diagram below, we have shortened the set representation by removing the braces and “,”s. Thus, for example, the label rs corresponds to the set {r, s}. Also notice that not all subsets of {p, q, r, s} have corresponding states in this ﬁgure. This is because we usingremoved the states that were in class. The states of Construct a DFA that accepts the same language have the technique discussed unreachable from the start state as they were useless. your deterministic machine should be the sets of states of the nondeterministic machine. {r, s} {q } Solution. p
1
1 0 0 0 1 ps
0 0 1 rs 0 r
0, 1 1 q 1 sq
1 s 0 ∅ 4. Give NFAs with the speciﬁed number of states recognizing each of the following languages. In the above diagram, we have shortened the set representation by removing the braces and “,”s. Thus for (a) The language {w  w ends with 00} with three states. example rs corresponds to the set {r, s}. Also notice that not all subsets of {p, q, r, s} have corresponding Solution. states in this ﬁgure. This is because we have removed the states that were unreachable from the start state as they were useless. 0,1 Problem 2 For a language L deﬁne CYCLE(L) = {yx  xy ∈ L}. Show that if L is regular, CYCLE(L) is also regular. 0 0 Solution. Suppose L is regular and let D be a DFA that accepts L. We construct an NFA N that accepts CYCLE(L). {Let w contains, the ,substringthe state w = of0101y for some x andthe with ﬁve states. let (b) The language w  Q = {q0 . . . qs } be 0101, i.e., set x D with q0 being y } start state, and F ⊆ Q be its set of accepts states. The state set of N will be {v0 , vi,j , vi,j  0 ≤ i, j ≤ s}. The start Solution. state of N is v0 and the accept states are {vi,i  0 ≤ i ≤ s}. The transition function of N is described as follows. N has transitions0,1 v0 to vi,i for all 0 ≤ i ≤ s. For all 0 ≤ j, k 0,1 , and for all a ∈ Σ, if from ≤s δD (qj , a) = qk , then vi,k ∈ δN (vi,j , a) and vi,k ∈ δN (vi,j , a), for all 0 ≤ i ≤ s. Also, for each 0 ≤ k ≤ s such that qk ∈ F , we add an epsilon transition between vi,k and vi,0 for each 0 ≤ i ≤ s. 0 1 0 1 In other words, in our NFA, for each state qi of D we keep two copies of D, which we will call Di and Di . The start state of N , without looking at the input, nondeterministically picks the pairs of copies corresponding to some state, say qi of D, number of 0s, to exactlyvtwo namely, six states. corresponding to (c) The language {w  w contains an even and moves or state i,i , 1s} with the state
Solution. 1
3 1 0 ε 0 0 1 0 0 ε 1 1 (d) The language 0∗ 1∗ 0∗ 0 with three states. Solution. 0 1 0 ε
5. Give regular expressions for: (a) All binary strings with exactly two 1’s. Solution. 0∗ 10∗ 10∗ 0 (b) All binary strings with a double symbol (contains 00 or 11) somewhere. Solution. (0 + 1)∗ (00 + 11)(0 + 1)∗ (c) All binary strings that contain both 00 and 11 as substrings. Solution. (0 + 1)∗ 00(0 + 1)∗ 11(0 + 1)∗ + (0 + 1)∗ 11(0 + 1)∗ 00(0 + 1)∗ (d) All binary strings without a double symbol anywhere. Solution. (1 + )(01)∗ (0 + ) 4 ...
View
Full
Document
This note was uploaded on 04/11/2010 for the course CSC CSC236 taught by Professor Farzanazadeh during the Spring '10 term at University of Toronto Toronto.
 Spring '10
 FarzanAzadeh

Click to edit the document details