MELJUN CORTES Automata Lecture Designing Finite Automata 1

Embed Size (px)

Citation preview

  • 8/21/2019 MELJUN CORTES Automata Lecture Designing Finite Automata 1

    1/16

     

    Theory of Computation (With Automata Theory)

    Designing Deterministic Finite Automata *Property o f STIPage 1 of 16

    TOPIC TITLE: Designing Deterministic Finite Automata

    Specific Objectives:

     At the end of the topic session, the students are expected to:

    Cognit ive:

    1. Design a DFA that will be able to recognize a given set of stringsor language.

    Affective:

    1. Listen to others with respect.

    2. Participate in class discussions actively.

    MATERIALS/EQUIPMENT:

    o  topic slideso  OHP

    TOPIC PREPARATION:

    o  Have the students review related topics that discussed inprevious courses.

    o  Prepare the slides to be presented in class.

    o  It is imperative for the instructor to incorporate various kinds ofteaching strategies while discussing the suggested topics. 

    o  Prepare additional examples on the topic to be presented. 

  • 8/21/2019 MELJUN CORTES Automata Lecture Designing Finite Automata 1

    2/16

     

    Theory of Computation (With Automata Theory)

    Designing Deterministic Finite Automata *Property o f STIPage 2 of 16

    Designing Deterministic Finite AutomataPage 1 of 29

    Designing Deterministic Finite Automata

    Designing DFAs

    Case Study 1: Design a DFA that can accept all strings over the alphabet    = {0,1} with an odd number of 1s.

    The DFA in this example should be able to recognize the language L = {w   w  hasan odd number of 1s}.

    Recall in the previous discussions that the states of a DFA represent something themachine must “remember” about the input string.   So in order to determine howmany states there are in the DFA to be designed, one must try to establish firstwhat the things the DFA must remember are.

    In the given case study, the DFA must determine if there are an odd number of 1sin the input string. One is tempted to say that the DFA must simply remember allthe symbols it has received so far and then from there, determine if the input string

    has an odd number of 1s. This is well and good. However, by doing so, themachine that should be able to do this must have a tremendous amount of memorysince there can be literally hundreds of symbols in the input string. Thecorresponding DFA will also have hundreds of states.

    In designing DFAs, a different line of thinking is needed.

    Upon the arrival of each symbol, the machine or DFA does not necessarily have toremember the exact number of 1s it has received in order to determine if it has anodd number of 1s.

     At any point of time, the DFA only has to remember if it currently had seen an oddnumber of 1s or even. Assume that the current number of 1s is even. If the nextinput symbol is a 1, then the DFA only has to remember that the number of 1s in

    the input string so far is now odd. If the next input symbol is 0, then the DFA onlyhas to remember that the number of 1s so far is still even.

    For example:

    1. Assume no input symbol has arrived so far. So the DFA remembers thatit has an even number of 1s (none so far).

    2. Assume the first input symbol is 0. So the DFA remembers that it has aneven number of 1s (none so far).

    3. Assume the second input symbol is 1. So the DFA remembers that it hasan odd number of 1s (one so far).

    4. Assume the third input symbol is 1. So the DFA remembers that it has aneven number of 1s (two so far).

    5. Assume the fourth input symbol is 0. So the DFA remembers that it hasan even number of 1s (two so far).

    6. Assume the fifth input symbol is 1. So the DFA remembers that it has anodd number of 1s (three so far).

    7. Assume the sixth input symbol is 1. So the DFA remembers that it has aneven number of 1s (four so far).

     A thousand strings could have been received by the DFA, and yet, all it has toremember is whether it currently had seen an odd or even number of 1s.

  • 8/21/2019 MELJUN CORTES Automata Lecture Designing Finite Automata 1

    3/16

     

    Theory of Computation (With Automata Theory)

    Designing Deterministic Finite Automata *Property o f STIPage 3 of 16

    Designing Deterministic Finite AutomataPage 2 of 29

    Therefore, the DFA only has to remember two things:

    1. Whether the number of 1s in the input string at any point in time is odd.

    2. Whether the number of 1s in the input string at any point in time is even.

    Since there are only two things that the DFA has to remember, then this impliesthat only two states are needed—one to represent the fact that the current numberof 1s is odd, and the other for even.

    The next step now is to determine the start state and the final state or states.

    Designing Deterministic Finite AutomataPage 3 of 29

     At the start of the computation, there are no input symbols yet. That means that atthe start, the number of 1s will be even (there are no 1s yet).

    The state in which the number of 1s is even will then be designated as the startstate.

    Since the acceptable strings are those with an odd number of 1s, the “odd” statewill be the final state.

    The next step now is to determine when the state transitions are carried out.

    Let the two states be:

    q0 = current number of 1s is even.

    q1 = current number of 1s is odd.

  • 8/21/2019 MELJUN CORTES Automata Lecture Designing Finite Automata 1

    4/16

     

    Theory of Computation (With Automata Theory)

    Designing Deterministic Finite Automata *Property o f STIPage 4 of 16

    Designing Deterministic Finite AutomataPage 4 of 29

     Assume that the DFA is in state q0 (input string so far has an even number of 1s).

    In state q0, the input string so far has an even number of 1s. So If theDFA receives a 0, it remains in that state since the number of 1s is stilleven.

    If it receives a 1, it moves to state q1 since the number of 1s is now odd.

     Assume that the DFA is in state q1 (input string so far has an odd number of 1s):

    In state q1, the input string so far has an odd number of 1s. So If the DFAreceives a 0, it remains in that state since the number of 1s is still odd.

    If it receives a 1, it moves to state q0 since the number of 1s is now even.

    Designing Deterministic Finite AutomataPage 5 of 29

    Since all possible inputs for each state had been considered, all that is needed nowis to draw the state diagram based on the derived conclusions. The state diagramfor the required DFA will then be

    1

    1

    00

    q 1q 0

     

  • 8/21/2019 MELJUN CORTES Automata Lecture Designing Finite Automata 1

    5/16

     

    Theory of Computation (With Automata Theory)

    Designing Deterministic Finite Automata *Property o f STIPage 5 of 16

    Designing Deterministic Finite AutomataPage 6 of 29

    Case Study 2: Design a DFA that can accept all strings over the alphabet    = {0,1} that contains the substring 011.

    The DFA in this example should be able to recognize the language L = {w   011 is asubstring of w }.

     As in case study 1, it is tempting to say that the DFA has to remember all the inputsymbols received and then determine if 001 is a substring of the input string. Thiswill again require a huge amount of memory and hence a very high number ofstates.

    Instead, the DFA only has to remember whether the substring 011 has beenencountered without remembering the previous symbols that arrived before thesubstring and the succeeding symbols that will arrive after the substring.

    Designing Deterministic Finite AutomataPage 7 of 29

    The DFA would have to remember only the following things at any given time:

    1. The DFA has to remember that it has not encountered the firstsymbol of the substring so far.

    2. The DFA has to remember that it has encountered the first symbol ofthe substring, which is 0.

    3. The DFA has to remember that it has encountered the first twosymbols of the substring, which are 01.

    4. The DFA has to remember that it has encountered all three symbolsof the substring, which are 011.

    The DFA will then have only four states. The next step now is to determine thestart state and the final state or states.

  • 8/21/2019 MELJUN CORTES Automata Lecture Designing Finite Automata 1

    6/16

     

    Theory of Computation (With Automata Theory)

    Designing Deterministic Finite Automata *Property o f STIPage 6 of 16

    Designing Deterministic Finite AutomataPage 8 of 29

     At the start of the computation, there are no input symbols yet. This implies that atthe start, the DFA has not yet encountered the first symbol of the substring 011.

    The state where the DFA has not yet encountered any of the symbols of thesubstring will then be designated as the start state.

    The state where it has encountered all symbols of the substring will be the finalstate.

    The next step now is to determine when the state transitions are carried out.

    Let the four states of the DFA be:

    1. q0 = the first symbol of the substring had not arrived.

    2. q1 = the first symbol (0) had arrived.

    3. q2 = the first two symbols (01) had arrived.

    4. q3 = all three symbols (011) had arrived.

    Designing Deterministic Finite AutomataPage 9 of 29

     Assume that the DFA is in state q0 (the DFA had not encountered the first symbolin the substring).

    In state q0, the DFA had not yet encountered the first symbol of thesubstring which is 0. So If the DFA receives a 0, it moves to state q1 since it had encountered the f irst symbol of the substring.

    If it receives a 1, it stays in state q0 since the DFA had not encounteredthe first symbol of the substring.

  • 8/21/2019 MELJUN CORTES Automata Lecture Designing Finite Automata 1

    7/16

     

    Theory of Computation (With Automata Theory)

    Designing Deterministic Finite Automata *Property o f STIPage 7 of 16

    Designing Deterministic Finite AutomataPage 10 of 29

     Assume that the DFA is in state q1 (only the first symbol, 0, had been encountered).

    In state q1, the DFA had only encountered the first symbol of thesubstring which is 0. So, If the DFA receives a 0, this means that thesubstring encountered so far is 00. These are not the first two symbols of

    the desired substring 011. Not all is lost though. The newly arrived 0 cannow be the new first symbol of the substring. The previous 0 can be“forgotten” by the DFA since it has no more bearing on futurecomputations. So what is now the new state of the DFA? Since it stillhad encountered only the first symbol of the substring 011, then it stays instate q1.

    If it receives a 1, the DFA has now 01 in its memory. It therefore movesto state q2  since the two symbols of the substring have already beenencountered.

    Designing Deterministic Finite AutomataPage 11 of 29

     Assume that the DFA is in state q2 (the first two symbols, 01, had beenencountered):

    In state q2, the DFA had only encountered the first two symbols of thesubstring which are 01. If the DFA receives a 0 next, the substringencountered so far is 010. These are not the three symbols of thedesired substring. Again, not all is lost. The newly arrived 0 can now bethe new first symbol of the substring. The previous 01 can similarly be“forgotten” by the DFA since it has no more bearing on futurecomputations. Technically, the DFA still had encountered only the first

    symbol of the substring 011. It therefore goes to state q1.

    If it receives a 1, the DFA had now encountered 011. It therefore movesto state q3  since the three symbols of the substring had already beenencountered.

  • 8/21/2019 MELJUN CORTES Automata Lecture Designing Finite Automata 1

    8/16

     

    Theory of Computation (With Automata Theory)

    Designing Deterministic Finite Automata *Property o f STIPage 8 of 16

    Designing Deterministic Finite AutomataPage 12 of 29

     Assume that the DFA is in state q3 (the substring 011 had been encountered):

    Since the substring 011 has already been encountered. It no longermatters what the succeeding inputs will be. The string will henceforth be

    accepted. The DFA will not change state upon the arrival of succeeding0s and 1s. It will remain at state q3 which is a final state.

    The state diagram for the required DFA is then

    0

    01

    q 0   q 3q 1   q 21   1

    0

    0, 1

     

    Designing Deterministic Finite AutomataPage 13 of 29

    Case Study 3: Design a DFA that accepts all strings over the alphabet    = {0, 1}that starts and ends with 01.

    The DFA in this example should be able to recognize the language L = {w   w startsand ends with 01}.

    The DFA to be designed must be able to detect if the input string has a prefix whichis 01 and a suffix which is also 01.

    To process the input strings, the DFA must first wait for the arrival of the first twosymbols. If the first symbols are 01, the DFA must remember this and then

    proceed to wait for the arrival of the last two symbols. If the last two symbols arealso equal to 01, then the string is accepted.

    If the first two symbols are not equal to 01, it no longer matters what thesucceeding inputs are. The DFA will just reject the string.

    The DFA to be designed should remember the following:

    1. The state where it had not yet encountered the first symbol (0) of theprefix 01.

    2. The state where it had encountered the first symbol (0) of the prefix01.

  • 8/21/2019 MELJUN CORTES Automata Lecture Designing Finite Automata 1

    9/16

     

    Theory of Computation (With Automata Theory)

    Designing Deterministic Finite Automata *Property o f STIPage 9 of 16

    Designing Deterministic Finite AutomataPage 14 of 29

    3. The state where it had encountered the two symbols (01) of theprefix 01.

    4. The state where it had already encountered the prefix 01 and it hadencountered the first symbol (0) of the suffix 01.

    5. The state where it had already encountered the prefix 01 and the twosymbols (01) of the suffix 01.

    6. The state where the input string did not start with the prefix 01. If thestring did not start with a 01, then it would be useless for the DFA todetermine if it would end with a 01.

    The state where it had not yet encountered the first symbol (0) of the prefix 01 willbe designated as the start state since this is the state where there are no inputsymbols yet.

    The state where it had encountered the prefix 01 and the two symbols (01) of thesuffix 01 is the final state.

    Designing Deterministic Finite AutomataPage 15 of 29

    The DFA will have 6 states:

    1. q0 = the first symbol of the prefix 01 had not arrived.

    2. q1 = the first symbol of the prefix 01 had arrived.

    3. q2 = the two symbols of the prefix 01 had arrived.

    4. q3 = the two symbols of the prefix 01 had been received and the firstsymbol of the suffix 01 had arrived.

    5. q4 = the two symbols of the prefix 01 had been received and the twosymbols of the suffix 01 had arrived.

    6. q5 = the input string did not start with the prefix 01.

    The next step is to determine when the state transitions are carried out.

  • 8/21/2019 MELJUN CORTES Automata Lecture Designing Finite Automata 1

    10/16

     

    Theory of Computation (With Automata Theory)

    Designing Deterministic Finite Automata *Property o f STIPage 10 of 16

    Proof of InductionPage 16 of 29

     Assume that the DFA is in state q0  (the first symbol of the prefix 01 had notarrived):

    In state q0, the DFA had not yet encountered the first symbol of the prefix01. If the DFA receives a 0, it moves to state q1 since it had encountered

    the first symbol of the substring.

    If it receives a 1, it goes to state q5 since it is now impossible for the inputstring to start with the prefix 01.

    Designing Deterministic Finite AutomataPage 17 of 26

     Assume that the DFA is in state q1 (the first symbol of the prefix 01 had arrived):

    In state q1, the DFA had encountered the first symbol of the prefix 01which is 0. If the DFA next receives a 0, it now has 00 as the first twosymbols of the string. This means that it is now impossible for the inputstring to start with the prefix 01. The DFA then goes to state q5.

    If it receives a 1, the DFA now has 01 as the first two symbols of thestring. The DFA then goes to state q2 since the two symbols of the prefix01 had been received.

  • 8/21/2019 MELJUN CORTES Automata Lecture Designing Finite Automata 1

    11/16

     

    Theory of Computation (With Automata Theory)

    Designing Deterministic Finite Automata *Property o f STIPage 11 of 16

    Designing Deterministic Finite AutomataPage 18 of 29

     Assume that the DFA is now in state q2  (the two symbols of the prefix 01 hadarrived):

    In state q2, the DFA had encountered the prefix 01. Assume now that a 0arrives. This newly-arrived 0 can potentially be the first symbol of the

    suffix 01. The DFA therefore goes to state q3.

    If it receives a 1, nothing really changed since the only thing that issignificant as of now is that DFA had encountered the required prefix 01.Therefore, the DFA stays in state q2.

    Designing Deterministic Finite AutomataPage 19 of 29

     Assume that the DFA is in state q3  (the two symbols of the prefix 01 had beenreceived and the first symbol of the suffix 01 had arrived):

    In state q3, the DFA had encountered the prefix 01 and the first symbol ofthe suffix 01 which is a 0. Assume now that a 0 arrives. At this point intime, the last two symbols the DFA has encountered are 00. Obviously,this is not the required suffix. However, the newly-arrived 0 can be thefirst symbol of the suffix 01. The previous zero may simply be forgottensince it becomes immaterial. The state of the DFA does not reallychange. So the DFA stays at state q3.

    If it receives a 1, the last two symbols the DFA has received are 01. Thiscould be the required suffix. The DFA then goes to state q4.

  • 8/21/2019 MELJUN CORTES Automata Lecture Designing Finite Automata 1

    12/16

     

    Theory of Computation (With Automata Theory)

    Designing Deterministic Finite Automata *Property o f STIPage 12 of 16

    Designing Deterministic Finite AutomataPage 20 of 29

     Assume that the DFA is in state q4  (the two symbols of the prefix 01 had beenreceived and the two symbols of the suffix 01 had arrived):

    In state q4, the DFA had already encountered the prefix 01 and the suffix01. If no other symbols arrive, then the string is accepted. However, if a

    0 arrives, then this means that the previous two symbols (01) are not thetrue suffix of the input string. These become immaterial and DFA canforget about them. The newly-arrived 0 now becomes the first symbol ofthe potentially true suffix. The DFA therefore moves to state q3.

    Similarly, if the DFA receives a 1, the previous two symbols (01) becomeimmaterial again. The newly-arrived 1 also becomes immaterial since itcannot be the start of the potentially true suffix. Therefore it is alsoforgotten. Hence, at this point in time, the DFA only has the requiredprefix. It therefore goes to state q2.

    Designing Deterministic Finite AutomataPage 21 of 29

     Assume that the DFA is in state q5 (the input string did not start with the prefix 01):

    In state q5, the DFA knows that the string did not start with the requiredprefix 01. It is therefore useless now for the DFA to track the arrivingsymbols because the string will not be accepted anyway. So upon thearrival of succeeding 0s and 1s, the DFA will not change state and willremain at state q5

    The state diagram for the required DFA then becomes

    0

    1

    1

    q 0   q 1   q 21   0

    0

    q 4q 3

    0

    1

    1

    q 5

    0

    0, 1

     

  • 8/21/2019 MELJUN CORTES Automata Lecture Designing Finite Automata 1

    13/16

     

    Theory of Computation (With Automata Theory)

    Designing Deterministic Finite Automata *Property o f STIPage 13 of 16

    Designing Deterministic Finite AutomataPage 22 of 26

     Additional Exercises:

    1. Design a DFA that accepts all strings over the alphabet    = {0, 1}with no more than three 1s.

    2. Design a DFA that accepts all strings over the alphabet    = {0, 1}with an even number of 0s and an odd number of 1s.

    3. Design a DFA that accepts all strings over the alphabet    = {0, 1}that starts or ends with a 01.

    It is not advised that the instructor let the students work on all the exercises at onceand then later present to them all the solutions.

    Instead, it is highly recommended that the students work on them one at a time.Meaning, have them work on one example for 5 to 10 minutes. Then present tothem the solution for that exercise. After that, have them work on the secondexercise for another 5 to 10 minutes and then present to them the solution. In thisway, the students learn from their mistakes before proceeding to solve the nextexercise.

    Designing Deterministic Finite AutomataPage 23 of 29

    For Exercise 1:

    Design a DFA that accepts all strings over the alphabet    = {0, 1} with no morethan three 1s.

    The DFA will have 5 states:

    1. q0 = there are no 1s received.

    2. q1 = there is one 1 received.

    3. q2 = there are two 1s received

    4. q3 = there are three 1s received.

    5. q4 = there are four or more 1s received.

    The start state is q0 while the final states are q0, q1, q2, and q3.

  • 8/21/2019 MELJUN CORTES Automata Lecture Designing Finite Automata 1

    14/16

     

    Theory of Computation (With Automata Theory)

    Designing Deterministic Finite Automata *Property o f STIPage 14 of 16

    Designing Deterministic Finite AutomataPage 24 of 29

    The state diagram of the DFA for exercise 1 will be:

    1 1   1

    0

    1q 0   q 4

    0

    q 1

    0

    q 2

    0

    q 3

    0, 1

     

    Designing Deterministic Finite AutomataPage 25 of 29

    For Exercise 2

    Design a DFA that accepts all strings over the alphabet    = {0, 1} with an evennumber of 0s and an odd number of 1s.

    The DFA will have 4 states:

    1. q0 = the number of 0s is even and the number of 1s is even.

    2. q1 = the number of 0s is even and the number of 1s is odd.

    3. q2 = the number of 0s is odd and the number of 1s is even.

    4. q3 = the number of 0s is odd and the number of 1s is odd.

  • 8/21/2019 MELJUN CORTES Automata Lecture Designing Finite Automata 1

    15/16

     

    Theory of Computation (With Automata Theory)

    Designing Deterministic Finite Automata *Property o f STIPage 15 of 16

    Designing Deterministic Finite AutomataPage 26 of 29

    The start state is q0 while the final state is q1.

    The DFA for exercise 2 will be:

    q 0   q 2

    1

    q 3q 1

    1

    1

    1

    0 0

    00  

    Designing Deterministic Finite AutomataPage 27 of 29

    For Exercise 3

    Design a DFA that accepts all strings over the alphabet    = {0, 1} thatstarts or ends with an 01.

    This is similar to the design example except that instead of a stringstarting and ending with the substring 01, the DFA should accept stringsstarting OR ending with the substring 01.

    One way of processing the strings is to determine first if the prefix 01 ispresent. If it is, then the DFA stops at an accept state, regardless if thereare still incoming input strings.

  • 8/21/2019 MELJUN CORTES Automata Lecture Designing Finite Automata 1

    16/16

     

    Theory of Computation (With Automata Theory)

    Designing Deterministic Finite Automata *Property o f STIPage 16 of 16

    Designing Deterministic Finite AutomataPage 28 of 29

    If the string did not start with a prefix 01, then the DFA proceeds todetermine if it will end with the suffix 01.

    The DFA will have 6 states:

    1. q0 = the first symbol of the prefix 01 had not arrived.

    2. q1 = the first symbol of the prefix 01 had arrived.

    3. q2 = the two symbols of the prefix 01 had arrived.

    4. q3 = the prefix 01 was not encountered.

    5. q4 = the prefix 01 was not encountered and the first symbol of thesuffix 01 had arrived.

    6. q5 = the prefix 01 was not encountered and the two symbols ofthe suffix 01 had arrived.

    Designing Deterministic Finite AutomataPage 29 of 29

    The state diagram for the required DFA:

    q 30

    0

    q 5q 4

    0

    1

    1

    0q 0   q 1

    1

    1

    q 2

    0, 1

    0

    1

     

    [Designing Deterministic Finite Automata, Pages 1 –29 of 29]