sample problems and soln

Introduction to the Theory of Computation

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: Theory of Computation - CSE 105, Fall 1998 Regular Languages Sample Problems and Solutions Notes: The alphabet in all problems is 0 1 0 2 1 4 unless explicitly mentioned otherwise. 1 Deterministic Finite Automata (DFA) Problem 1 - Designing DFAs 1 Define the language to be : contains either or as a consecutive substring 1 0 Strategy: We first try to determine what we need to remember about a string as we are reading it. In this case we would need the following states: a start state, a state in which we have just seen a . Then states that have just seen and . And finally a state that has seen either a or a . This will be our accept and , since all that matters is that we need state. Note that it suffices to have one state represent both to accept both. Construction: The following DFA recognizes . 3 0 0, 1 5 1 1 Figure 1: A DFA recognizing Give the state diagram of a DFA with at most five states that recognizes the language formal proof of the correctness of your construction. , and give a Correctness of Construction: We need to give a formal argument proving that the DFA constructed for accepts all and only those strings that are in . The proof, thus, has two subproblems : 2 ( ! " A ! " 1. Prove that for any string 2. Prove that for any string in , is accepted by the constructed DFA , is not accepted by the constructed DFA To get an idea for how to solve subproblem 1, recognize that we are trying to prove that a specific property (acceptance by the constructed DFA) holds for all elements of an infinite set ( ). This is exactly the type of problem for which proof by induction is designed (see page 23 of the text). Subproblem 2 fits the same model as subproblem 1, since the complement of is also an infinite set. Hence, we can prove the construction correct by giving inductive proofs for subproblems 1 and 2. Subproblem 1 - Proof by Induction In order to construct an inductive proof, we need to choose an appropriate variable to perform induction on. That is, we must find some function defined for all in that maps to the set of integers. As every string in has a length which is an integer value, the length function is suitable. Hence, let be the length of a string . We shall perform induction on the variable . # ! %$" Basis The constucted DFA accepts all # ! %&" in such that 3. When 2, the basis claim is vacuously true since the definition of states that all elements contain either or as a substring, both of which have a length of 3 2. or , as no other string of length 3 can possibly be in . We need When = 3, must be either to show that the constructed DFA accepts both and . From the start state, , the string moves the DFA along the following path of states : -- -- -- . Since is an accept state, the DFA accepts the string . Similarly, the string causes transitions along the path : -- -- -- . Again, since is an accept state, the DFA accepts the string . such that 3, we may conclude that Having shown that the constructed DFA accepts all in the basis of the induction is true. ' 476 ! 5 Induction Step Assume that the constucted DFA accepts all in constructed DFA accepts all strings in such that = + . 9 ' ! @8" 6 ! " 6 such that = 3. Then, the Let be any string in such that = + . Since , there exists some string in such that either = or = . The string is either in or not in . then, by the inductive hypothesis, it is accepted by the constructed DFA. The strings If is in and must also be accepted by the same DFA, since the DFA will be in accept state after processing and since there are no transitions from to another state. Hence, is accepted by the DFA whenever is accepted by it. A A A C DB A 2 ( A A A A 2 2 ( ! " 3 ( 0 ( )( )( 4# ! 2 ( 2 ( ' 1 ( 0 ( )( not in 2 ( ! If, on the other hand, is not in then, since we assumed is in , there exists some string in such that = and either = or = . Processing the prefix string, , may leave the DFA in any state (remember, we have not made any claims about the behavior of the DFA on strings that are not in ). Thus, we need to show that the accept state is reachable from any other state on input or . There are cases ( DFA states X choices for ), each of which the reader may easily verify leave the DFA in the accept state. It follows that is accepted by the DFA whether or not is in . Since, given in such = + , we have shown that the inductive hypothesis implies that is accepted by the constructed DFA, we may conclude that the constructed DFA accepts all in . Subproblem 2 - Proof by Induction The inductive proof for this subproblem is very similar to the one given for Subproblem 1. It is left as an exercise. C B A 4 A 4 1 1 0 Problem 2 - Designing DFAs 0 F 0 F Give the state diagram of a DFA that recognizes the language H 6 $G where is given by S T 0 2 1 1 3 0 F Figure 2: A DFA recognizing Q I RP6 : has 's for some 3 0 4 9 6 0 F ! " A A E A Problem 3 - Closure Properties of Regular Languages : Closure under Reversal , let = c Y X cfIe d@Y W X Y gc ( h Y c c 0 VU (Problem 1.24, page 88 of the text) For any string in reverse order, . For any language is . qi rp h c Proof idea: The basic idea is that we want to modify the DFA to "run backwards" on an input string of . We can do this by "reversing the transition arrows" in the original DFA. The original start state becomes the new final state. Similarly, we would like the original final states to become the new start states. However, since we are allowed only one start state, we need to add a new state which will also be our new start state with transitions to all the original final states. I s q I @ Construction: Construct a machine introduce. Define the new transition function such that for every i d u q I ( i Y u , where and is a new start state we , Correctness of Construction: Consider some . We are interested in the behaviour of on . By definition of we have . Assume that are the states in that in . Then by our construction we have that is a path on input in are followed on input . Since this is a valid path ending in an accept state, we have that accepts . on some . We must show that there is no Now, lets look at the behaviour of accepting path in on input . Again, by definition of , we have that . We argue on input then reversing it would yield an accepting by noting that if there was an accepting path in path in on input . However, since does not accept , we know that there can be no accepting path in on input . q Y h Y fh W ViX cej I Y Y c S SS Y c I W ghV` Y fh P S S S Y h W ( w( S S S W ( W g` w ( c I egb Y S S S S S S bVf Y eI W c h Y gc Y Y Y h h Y h Y fh Y fh 4 S S S Y h Also, let . We claim that is an NFA that accepts . Y c Y h qeI( w ( Y qi rh v4t u s Y c Want: To show that is regular. We do this by constructing an NFA c w ( Given: is regular. So there is a DFA S S S x y , the reverse of , written . Show that if , is the string is regular, so u s v%t 0 ba` S S S x W d$Y i u Y c Y gc that accepts . that accepts . Show that the class of regular languages is closed under this operation. l c Given: and are regular. is regular. l m ngc Want: To show that Proof idea: While we can solve this via DFA or NFA constructions, it is considerably easier to do this by invoking only known closure properties of regular languages. The main trick here is to observe that l q i Pgc 7q l ci Pn l q c Uhi o p l ci l m Unpngc Now we can use the fact that regular languages are closed under union, complementation and intersection to argue that is regular. 0 1 l q ci 7@r 0 l ci 7@r Construction: Consider the following languages: . We have . We claim that is regular. l c l m pngc 1 q ts , Correctness of Construction: Since and are regular and regular languages are closed under union, is regular. Since and are regular and regular languages are closed under intersection, is regular. is regular and regular languages are closed under complement, is regular. Since and are Since regular and regular languages are closed under intersection, is regular. Since we have that the class of regular languages is closed under the operation. 1 0 l m c n 1 m l c 0 5 l I ee c I fe : or but not both l m phnc l m nhc l If , c are languages, we define a new language k Problem 4 - Closure Properties of Regular Languages : Closure under , and Problem 5 - Closure Properties of Regular Languages: Closure under Hamming Distance v t6 h C u c c ci 0 w For any set and Main idea: If is regular, then there is a deterministic finite automaton that recognizes . To show that is regular, then, all we have to do is show that we can use to build a new machine that recognizes . The general idea is to take the DFA and transform it into an NFA that allows the option of flipping up to two input bits but otherwise processes an input string in exactly the same way as does. This can be done by having contain three copies of with the addition of transitions from copy 1 to copy 2 allowing the flipping of one bit and the addition of transitions from copy 2 to copy 3 allowing the flipping of one other bit. Then, in addition to accepting all strings accepted by , the new machine will also accept all strings in with either 1 or 2 bits flipped. Simple example: 0 Suppose 0 is the following machine: 1 1 1 0 6 c Let the language recognized by this machine be called : . Then the following machine will accept h fh c C h u ~c h h h h c 6 the set of strings of Hamming distance at most . ci )0 w from . Prove that if T 6 }v c i y |n{ z c i x w h c c i 0 w ci )0 w , define is regular, then so is 0 0 1 1 0 0 0 1 1 1 0 1 0 0 1 1 1 0 0 0 1 1 0 1 0 1 0 1 1 0 This machine accepts the same strings as the previous machine, but it allows up to two bits in the input string to be flipped. w ( 1 u x qi rph The formal construction: Given h , where . Do the same addition of transitions from machine 2 to machine 3, i.e. for each transition , add a new transition ; for each transition , add a new transition . ci 0 7 fh h c ci 0 w I h h ci 0 w I e Proof of correctness: we must argue that if To show that for a DFA that recognizes , the new NFA recognizes , accepts , and if , does not accept . Add the transitions necessary to allow bit flipping as follows. For each transition add a new transition . Likewise, for each transition transition . x u x x a0 a0 @ Td i x y u di u u x w)f ( 2 )r (i y u |di u q q Create the new machine that combines them so that , where and . First just copy the transition function: (e.g. if contains the transition , add the transition to ). Tdi x u Tdr i u w ( )f y 2 (i 0 x x x fh h gh |dy i u w )~ w ( &q ( 1 q 0 , , add a new y u x a0 dr i w y x y a0 @ w ( qi %rzfh v4t u s adi 0 1 Dh | 0 h gh a1 @ Make three copies of , named replacing each state of with h a and , where w |( qi rh y x uv4t s W T q SSS x y u s v4t , is created by adi 0 x a1 @ y adr 0 i u a0 , , then such that . If then , and the part of will accept since contains all the transitions and accept states of . If , then differs from in only one position, so can process up until it reaches the symbol at which differs from , switch to , and continue processing the rest of as if it was , finally accepting it. The same argument holds if , except the machine can move into an state at the state at the other point at which differs from . first point at which differs from , then into an If , then . That is, more than two bits in must be flipped to get a string . If this is the case, cannot possibly accept , since it allows at most two deviations from how the original machine processes it. ci 0 c I A A |{ i h }h c I 0 h A A i |n{ 1 9 fv h }h 9 ~ A i |n{ h fh 9p' A |n{ n` i c I A A i |{ 0 h A gh c I A e A c I PA ci T0 w I h If 8 w 2 Nondeterministic Finite Automata (NFA) Problem 6 - Designing NFAs Give the state diagram of an NFA with at most four states that recognizes the language : contains either or as a consecutive substring 0, 1 1 , where Strategy: In this problem we show how one can use nondeterminism to save states. We need to make sure that there is at least one accepting path for all the strings we intend to accept. And that there is no or accepting path for a string which is not in the language. Since we are interested in the presence of a as a substring, we can basically ignore a preamble of 's and 's that does not contain sequences we are interested in. This is shown by the self loop in the first state. We need a state to maintain information that we have just seen at least one . Another is needed to record that we have just seen at least one occurence or a . In some sense it was possible here to we collapse into one the two separate states used in of a the corresponding DFA to record this information. Finally we need an accepting state that records that we have found at least one occurence of a or a . Construction: The following NFA accepts . 0 0, 1 2 3 0 4 0, 1 Correctness of Construction: We can informally prove the correctness of our construction by observing that sandwiched between the start and final states that allow any combinations of 's and 's is a path that can be traversed on processing either a or a . Hence there is clearly at least one path from the start to the final state if the input string contains the desired substrings. To show that no string other than the ones that have the desired property are accepted, we argue that the only way to get from the start state to the final state is a path that can be traversed if only or appear as a substring. 9 Figure 3: An NFA recognizing Problem 7 - Converting an NFA to an equivalent DFA Problem 1.12b, page 85 of the text. Refer to Theorem 1.19, page 55 of text, and Example 1.21, page 57 of text. a b 1 2 a 3 b b a a, b 1, 2 1, 3 b b a b 2, 3 1, 2, 3 a a Figure 4: A DFA that is equivalent to the given NFA b b a b 1, 2 2, 3 1, 2, 3 a a Figure 5: The DFA above after removing unnecessary states 10 3 Regular Expressions (RE) Problem 8 - Designing Regular Expressions 0 F C Give the regular expression representing the language in Problem 1. It is easy to see what the expected RE is by looking at the NFA we designed earlier. The regular expression is . C i i C sU i Problem 9 - Designing Regular Expressions Give the regular expression representing the language 0 F in Problem 2. G We first characterize the strings that are in . These strings have one more That is all strings with just one , with four s, . . . , and so on, are in . The regular expression is . 0 C C C C C i C i C C C than some multiple of . Note: There are several possible correct solutions to such problems. For example another possible solution would be . C C C 7 C C i i 11 Problem 10 - Converting NFAs to REs Problem 1.16a, page 86 of the text. Refer to Lemma 1.29, page 66 of text, and Example 1.30, page 68 of text. a 1 b b s b 1 b a f (A) (B) 2 a a 2 s 1 (b a* b) U a s ((b a* b) U a)* b a* b a* f f (C) (D) Figure 6: The steps in converting the NFA given in (A) to a RE (transition label of (D)) 12 Problem 11 - Converting REs to NFAs Problem 1.14c, page 86 of the text. Refer to Lemma 1.32, page 69 of text, and Example 1.35, page 74 of text. : 1 0, 1 : s 1 13 C Figure 7: Building an NFA from the RE 4 Non-regular Languages Problem 12 - Application of Pumping Lemma Problem 1.16a, page 86 of the text. Prove that the following language is not regular: I I : is not a palindrome : is a palindrome We will prove that the language is not regular. We can then use the fact that regular languages are closed cannot be regular either. under complementation to argue that Let and assume towards a contradiction that is regular. We will use the pumping lemma to derive a contradiction. Refer to Theorem , page 78 of the text. Let be the "critical length" given by the theorem. Let us . This string is, by definition of the language, contained in the language. choose the string and clearly , the theorem says that it may be written as such that the Now since . Since , it three conditions of the theorem hold. Condition says that and for some . Say and . Condition must be that of the theorem holds for any . We choose . Now consider the string . This is , and condition says it is in . But condition of the theorem tells us , meaning . But then it is clear that is in fact not in (i.e. is not a palindrome), because and will have more leading 's than trailing 's. This is a contradiction. Hence it must be that is not regular. Now consider the language . Assume towards a contradiction that is regular. Since regular languages are closed under complementation, this would mean that is also regular. In view of the result is not regular. established earlier, this again is a contradiction. Hence it must be that Note: There are several ways of using the pumping lemma in proving the above. The trick is in choosing an initial string which makes our job easier. A `V4 1 F 1 1 F 1 F 9 X A 0 }A x A` T A `e c c 9 r @ v 6 v y A ` y G TG c 1 1 F S ) 1 c 14 1 F Proof: Consider the complement of the language 1 . 1 F ' H 7 ' r ' y bA y ) 0 x x x R n A gX ) x x ' y y c I e f 1 F Problem 13 Problem 1.28, page 89 of the text. Proof: In order to show that language is not regular, we're going to assume is regular and then use the whose length is at pumping lemma to show a contradiction. To do so, we first have to pick some string least , the pumping length. This string should not be trivial, however. For example, if we pick , we won't find any contradiction since the pumping lemma is going to work. So, let be . Since and , can be broken into three parts, , satisfying the following conditions: F 1. for each ' y bA y ; 2. 3. ; and . , for some . Then, when we pump up once ( ), which is clearly not in . Therefore, we have a contradiction and our initial was regular was wrong. 9 F v Dtn# p X A 0 a DbX y ' y A y v y A Because we have assumption that and 15 a I e& A XV% a RA I e X A y 7 y v y A 7` y I e$ 6 Miscellaneous Problems Problem 14 - All paths NFA Problem 1.31, page 89 of the text. We first characterize what it means for an all-paths-NFA to accept and reject. If the input is in the language, we require that all paths that may be followed end in accepting states. On the other hand, if the input is not in the language, we require that there exist at least one path that leads to a rejecting state. There are two directions to this proof. We start with the easier one of proving that if language then there exists an all-paths-NFA that accepts it. h is a regular Given: is regular. So there is a DFA that accepts . that accepts . Correctness of Construction: Here we make the observation that the DFA conforms to the defintion of the all-paths-NFA. That is when the input is in , there is only one possible path that accepts in a DFA for , and hence we can say that all possible paths are accepting. And when the input is not in , there is only one possible path that rejects, and hence we can say that there exists at least one path that rejects. Now for the other direction, Given: is a language accepted by some all-paths-NFA. is regular. Want: To show that Proof idea: We will construct an NFA that accepts . This would mean that is regular. We know that the class of regular languages is closed under complementation. Thus we will have proved that is regular. Construction: Construct the machine which is the same as except that the roles of accepting and . That is all accepting states in are rejecting states in and rejecting states have been reversed in vice-versa. We claim that is an NFA that accepts the language . Correctness of construction: Consider some . We want to accept . We have . This means that on input there is some path in which ends in a reject state. This in turn implies that there exists some path in for input which ends in an accept state and hence accepts . Now let us look at some . We want to reject . Again we have . This means that on input all paths in end in accept states. This implies that on input all paths in would end in reject states and thus rejects . I en h I e fh fh e I h h g I fh h h h h h h fh h h h Construction: We claim that fh Want: To construct an all-paths-NFA . 16 ...
View Full Document

This homework help was uploaded on 02/08/2008 for the course CSE 105 taught by Professor Paturi during the Summer '99 term at UCSD.

Ask a homework question - tutors are online