• Please give details of your calculation. A direct answer... not counted. • Your answers must be in English.
Transcription
• Please give details of your calculation. A direct answer... not counted. • Your answers must be in English.
• Please give details of your calculation. A direct answer without explanation is not counted. • Your answers must be in English. • Please carefully read problem statements. • During the exam you are not allowed to borrow others’ class notes. • Try to work on easier questions first. Problem 1 (15 pts) Consider the following DFA: 1 0, 1 0 0 start 1 0 2 1 3 4 1 (a) Convert this DFA to a regular expression using GNFA. The order to remove states must be 1, 2, 3, and then 4. (b) Try to simplify the obtained regular expression to Σ∗ 001Σ∗ . You need to show details. Answer (a) First, add a start state and an accept state: 1 0, 1 0 0 start qstart 1 2 0 1 3 4 qaccept 1 Then remove state 1: 11*0 start qstart 1*0 2 0, 1 0 0 1 3 1 4 qaccept Remove state 2: 0, 1 0 start qstart 1*0(11*0)*0 1 3 4 qaccept Remove state 3: 0, 1 start qstart 1*0(11*0)*00*1 4 qaccept Finally, remove state 4: start 1*0(11*0)*00*1(0∪1)* qstart qaccept The corresponding regular expression is 1*0(11*0)*00*1(0∪1)*. (b) Since 0*(11*0) = (011*)*0, we have 1*0(11*0)*00*1(0∪1)* = 1*(011*)*000*1(0∪1)*. And 000*1 = 0*001, so 1*(011*)*000*1(0∪1)* = 1*(011*)*0*001(0∪1)*. Finally, for all strings in {w|w ∈ Σ∗ , w does not contain 001}, there should be at least one ‘1’ after each ‘0’ except the ‘0’s at the end of the strings. That means 1*(011*)0* = {w|w ∈ Σ∗ , w does not contain 001}, so we have 1*(011*)*0*001(0∪1)* = Σ∗ 001Σ∗ . Another Solution: If we use Σ∗ 001Σ∗ as input of the DFA, after the first Σ∗ , it must in state 1, 2, 3, or 4. And from all states, it will go to state 4 after string 001. 2 1 2 3 4 0 0 0 0 0 2 0 3 0 3 0 4 3 3 4 4 1 1 1 1 4 4 4 4 So the DFA accepts all strings in Σ∗ 001Σ∗ . Besides, 1*0(1*10)*00*1(0∪1)* = 1*(011*)*0*001(0∪1)* ⊂ Σ∗ 001Σ∗ . Then we have Σ∗ 001Σ∗ ⊂ 1*0(1*10)*00*1(0∪1)* ⊂ Σ∗ 001Σ∗ . That is Σ∗ 001Σ∗ = 1*0(1*10)*00*1(0∪ 1)*. Problem 2 (15 pts) We would like to check if φ∗ = {}. (a) Give an NFA for φ using the smallest number of states. (b) Following how we construct A∗ for any given NFA A, give the NFA of φ∗ . (c) Transfer the NFA of φ∗ to DFA (using the power set as state sets). We assume Σ = {0, 1}. (d) Simplify DFA obtained in (c) and give the formal definition of this DFA. Answer (a) start 3 1 (b) start 1 2 (c) start {1, 2} {1} 0, 1 0, 1 {2} 0, 1 φ 0, 1 q2 0, 1 (d) Simplified DFA: start 0, 1 q1 Definition of the DFA: (Q, Σ, δ, qstart , F ), where 1. Q = {q1 , q2 } 2. Σ = {0, 1} 3. qstart = q1 4. δ: q1 q2 0 q2 q2 1 q2 q2 5. F = {q1 } Problem 3 (5 pts) Give a DFA with five states such that the language is {0}. We assume Σ = {0, 1}. You don’t need to give a formal definition (i.e., a state diagram is enough). 4 Answer start 0 q1 q2 1 q3 0, 1 q4 0, 1 0, 1 0, 1 q5 Problem 4 (10 pts) Is the following language regular? {xyxR : x, y ∈ Σ∗ } Assume Σ = {0, 1}. xR means the reverse of x. Answer Yes. Since Σ∗ = {xyxR : y ∈ Σ∗ , x = ∈ Σ∗ } ⊆ {xyxR : x, y ∈ Σ∗ } and {xyxR : x, y ∈ Σ∗ } ⊆ Σ∗ , we have {xyxR : x, y ∈ Σ∗ } = Σ∗ is a regular language. Common Mistakes We can not prove xyxR is regular by saying “x, y, and xR are regular, and the set of regular languages is closed under the operation of concatenation.” Otherwise, we can prove xxR is regular by the same way, but it’s not. Problem 5 (10 pts) If L is regular, is the language {w|w ∈ L and wR ∈ L} regular? We assume Σ = {0, 1}. xR means the reverse of x. 5 Answer Yes. Since both L and LR are regular languages (homework 2, 1.31), two DFAs M1 = {Q1 , Σ, δ1 , qs1 , F1 } and M2 = {Q2 , Σ, δ2 , qs2 , F2 } accept L and LR , respectively. Then we can construct another DFA M3 = {Q1 × Q2 , Σ, δ3 , (qs1 , qs2 ), F1 × F2 }, which accepts L ∩ LR , where δ3 ((q1 , q2 ), σ) = (δ1 (q1 , σ), δ2 (q2 , σ)) ∀(q1 , q2 ) ∈ Q1 × Q2 , σ ∈ Σ. So {w|w ∈ L and w ∈ LR } = {w|w ∈ L and wR ∈ L} is regular. Common Mistakes • {w|w ∈ L and w ∈ LR } = 6 {w|w ∈ L or w ∈ LR }. • You can not just prove LR is regular. Problem 6 (15 pts) Is the language L = {1k |k ∈ N ∪ {0} but k is not a prime number} regular? You can assume Σ = {1} and need to show all the details. Answer No. We use the following Lemma 1 to simplify the proof. Lemma 1. If L is a regular language, then so is Lc . Proof. If L is a regular language, then there exists a DFA M = {Q, Σ, δ, qs , F } which accepts L. Then M 0 = {Q, Σ, δ, qs , Q\F } is a DFA which accepts Lc , so Lc is also regular. Then we can show that Lc is not regular. For any p > 0, there exists a prime number n > p + 1 and a string s = 1n ∈ Lc . For all x, y, z ∈ N ∪ {0} s.t. s = 1n = 1x 1y 1z with y > 0 and x + y ≤ p, we have x + z ≥ z > 1 and 1x (1y )x+z 1z = 1(y+1)(x+z) ∈ / Lc . So Lc is not a regular language (by pumping lemma), and neither is L (by Lemma 1). 6 Problem 7 (10 pts) Consider the DFA in Problem 1. Give a CFG for the language of this DFA. Requirement: # variables ≤ 4, # rules ≤ 9. Answer Q1 Q2 Q3 Q4 → → → → 1Q1 1Q1 0Q3 0Q4 | | | | S A B → A001B → 0A | 1A | → 0B | 1B | S A → A001A → 0A | 1A | 0Q2 0Q3 1Q4 1Q4 | , where Q1 is the starting state. Other Solutions: 1. 2. Problem 8 (15 pts) Modify the following CFG to Chomsky normal form: S → Y → aSb | bY | Ya bY | aY | Answer step 1: Add a rule S0 → S: S0 S Y → S → aSb | bY | Ya → bY | aY | step 2: Remove rule Y → : 7 S0 S Y → S → aSb | bY | Ya | b | a → bY | aY | b | a S0 S Y → aSb | bY | Ya | b | a → aSb | bY | Ya | b | a → bY | aY | b | a step 3: Remove rule S0 → S: step 4: Add three states A, B, and Sb : S0 S Y A B Sb → → → → → → ASb | BY | YA | b | a ASb | BY | YA | b | a BY | AY | b | a a b SB Problem 9 (5 pts) Consider the Following PDA. Draw the tree to process the string 0011. Your tree must be complete. Note that we mean a tree to process an input string. We don’t mean a parse tree of CFG. start q1 , →$ q2 0, → 0 , → q4 , $ → 8 q3 1, 0 → Answer q1 q2 ,$ q3 ,$ 0 q2 ,0$ q3 ,0$ 0 q2 ,00$ q3 ,00$ 1 q3 ,0$ 1 q3 ,$ Common Mistakes A hierarchy is needed. 9 q4 q4