meljun cortes automata lecture equivalence of nfas and dfas part 1 1

Upload: meljun-cortes-mbampa

Post on 07-Aug-2018

218 views

Category:

Documents


0 download

TRANSCRIPT

  • 8/21/2019 MELJUN CORTES Automata Lecture Equivalence of Nfas and Dfas Part 1 1

    1/13

     

    Theory of Computation (With Automata Theory)

    Equivalence of NFAs and DFAs *Property o f STIPage 1 of 13

    TOPIC TITLE: Equivalence of NFAs and DFAs

    Specific Objectives:

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

    Cognit ive:

    1. Explain the rationale behind the equivalence between NFAs andDFAs.

    2. Apply the procedure for converting NFAs with no ε-transitions totheir equivalent DFAs.

    Affective:

    1. Listen to others with respect.2. Participate in class discussions actively.

    MATERIALS/EQUIPMENT:

    o  topic slides

    o  OHP

    TOPIC PREPARATION:

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

    o  Prepare the slides to be presented in class.o  It is imperative for the instructor to incorporate various kinds of

    teaching strategies while discussing the suggested topics. o  Prepare additional examples on the topic to be presented. 

  • 8/21/2019 MELJUN CORTES Automata Lecture Equivalence of Nfas and Dfas Part 1 1

    2/13

     

    Theory of Computation (With Automata Theory)

    Equivalence of NFAs and DFAs *Property o f STIPage 2 of 13

    NFA and DFA EquivalencePage 1 of 21

    Equivalence of NFAs and DFAs

    NFA and DFA Equivalence

    Introduction

    Consider the following DFA M 1 from the previous discussions.

    0 1q 0

    0 1

    q 2q 1

    0

    q 3

    0, 1

    1

     

     After analyzing this DFA, it can be said that DFA M 1  accepts strings over the

    alphabet    = {0, 1} that start with a 0 and end with a 1.

    NFA and DFA EquivalencePage 2 of 21

    Consider now the following NFA N 1 also from the previous discussions.

    0 1q 0

    0, 1

    q 2q 1

     

     After analyzing this NFA, it can be said that NFA N 1 also accepts strings over the

    alphabet    = {0, 1} that start with a 0 and end with 1.

    Since both can accept the same set of strings over the same alphabet, DFA M 1 and NFA N 1  recognize the same language. In other words, they have the samelanguage. That is,

    L(M 1) = L(N 1) = {w   w  starts with 0 and ends with 1}

    The language of M 1 is equal to the language of N 1, which is the set of all strings w  such that w  starts with a 0 and ends with a 1.

  • 8/21/2019 MELJUN CORTES Automata Lecture Equivalence of Nfas and Dfas Part 1 1

    3/13

     

    Theory of Computation (With Automata Theory)

    Equivalence of NFAs and DFAs *Property o f STIPage 3 of 13

    NFA and DFA EquivalencePage 3 of 21

    If an NFA and a DFA recognize the same language, they are said to be equivalent.Therefore, DFA M 1 is equivalent to NFA N 1.

    In general, any two machines (whether they are both DFAs, both NFAs, or one is aDFA while the other is an NFA) are equivalent if they recognize the same

    language.

    If a language is recognized by a DFA, then there is an NFA that will also recognizeit. This is quite obvious since all DFAs are also NFAs.

    But the question now is, if a language is recognized by an NFA, is there a DFA thatwill also recognize it?

    In other words, is there an equivalent DFA for any NFA?

    The answer is not quite obvious since not all NFAs are DFAs.

    To answer this question, a procedure must be constructed to convert any NFA toits equivalent DFA. If no procedure can be constructed, then the answer is no.

    NFA and DFA EquivalencePage 4 of 21

    Converting NFAs with no ε-Transitions into their Equivalent DFA

    The procedure that will be discussed here is applicable only to converting NFAswith no ε-transitions into their equivalent DFA.

    Consider NFA N 1 which has three states q0, q1, and q2. Recall that an NFA can bein one or more states at any given time. So at any time, the NFA may be in:

    1. state q0 only  – there is currently only one copy of the NFA and it is atstate q0.

    2. state q1 only  – there is currently only one copy of the NFA and it is atstate q1.

    3. state q2 only  – there is currently only one copy of the NFA and it is atstate q2.

    4. states q0 and q1  –  there are currently two copies of the NFA. Onecopy is at state q0 while the other is at state q1.

    5. states q0 and q2  –  there are currently two copies of the NFA. Onecopy is at state q0 while the other is at state q2.

    6. states q1 and q2  –  there are currently two copies of the NFA. Onecopy is at state q1 while the other is at state q2.

    7. states q0, q1, and q2  –  there are currently three copies of the NFA.One copy is at state q0,

     the second copy is at state q1, and the third

    copy is at state q2.8. dead state (stopped processing)  –  All copies of the NFA have

    stopped and ceased to exist.

  • 8/21/2019 MELJUN CORTES Automata Lecture Equivalence of Nfas and Dfas Part 1 1

    4/13

     

    Theory of Computation (With Automata Theory)

    Equivalence of NFAs and DFAs *Property o f STIPage 4 of 13

    NFA and DFA EquivalencePage 5 of 21

    Observe that the possible states the NFA can assume are q0 only, q1 only, q2 only,q0 and q1, q0 and q2, q1 and q2, q1 and q2 and q3, and the dead state.

    It can therefore be said that possible states the NFA can be is the power set of itsset of states Q. Recall that the power set is simply the set of all subsets. If Q  is

    the set of states of the given NFA N 1, then:

    Q = {q0, q1, q2}

    The power set of Q is then

    P (Q) = {, {q0}, {q1}, {q2}, {q0,q1}, {q0, q2}, {q1, q2}, {q0, q1, q2}}

    where the null set  corresponds to the dead state.

    Notice that each subset in the power set of Q corresponds to one of the states theNFA N 1 can assume at any given time.

    Since a DFA can only be in one state at any one time while an NFA can be in

    several states at a time, this mismatch can be resolved by constructing theequivalent DFA such that it will have a single state that will represent the situationwhere the NFA is in several states. In other words, the constructed DFA will havea single state for each element of the power set.

    For example, the state in which the NFA is in states q0 and q1 will be representedby a single state (that may be called q01) in its equivalent DFA. If the NFA is instates q0, q1, and q2 at the same time, then the equivalent DFA will have only onestate (that may be called q012) to represent that situation.

    If the NFA has n states, then the equivalent DFA will have 2n states since there will

    be 2n  possible subsets given a set with n  elements. This implies that the

    equivalent DFA will have a finite number of states since the number of states in anyNFA is also finite.

    In the example given, since the NFA N 1 has 3 states, its equivalent DFA will haveat most 2

    3 = 8 states.

    NFA and DFA EquivalencePage 6 of 21

    The states of the equivalent DFA will then be

    The next step now is to determine the start state and the final states of theequivalent DFA.

    The initial state of the NFA N 1  is q0 so the initial state of the equivalent DFA willalso be q0.

  • 8/21/2019 MELJUN CORTES Automata Lecture Equivalence of Nfas and Dfas Part 1 1

    5/13

     

    Theory of Computation (With Automata Theory)

    Equivalence of NFAs and DFAs *Property o f STIPage 5 of 13

    NFA and DFA EquivalencePage 7 of 21

    The final state of NFA N 1 is q2. This means that the NFA is in the final state if atleast one copy of the NFA is at state q2. So the NFA is in the final state if it is inany of the following states:

    1. q2 

    2. q0, q2 3. q1, q2 4. q0, q1, q2 

    In other words, the NFA will be in the final state if q2  is included in the subset.Therefore, the final states for the equivalent DFA are q2, q02, q12, and q012.

    Take note that not all of the states of the equivalent DFA are reachable from thestart state q0. These states are considered useless. It is thus possible that someof these states will eventually be removed.

    The states that will be removed will be determined upon the construction of thetransition function of the equivalent DFA.

    NFA and DFA EquivalencePage 8 of 21

    Construction of the transition function for the equivalent DFA for NFA N 1 

    The transition table for the equivalent DFA will be in the following form:

    The procedure to be followed is that for each state that is reachable from state q0,analyze to which state the equivalent DFA will go to given the arrival of each inputsymbol.

    Starting at state q0  (this is the DFA state that represents the situation where theNFA is at state q0 only):

    If input = 0, the NFA will go to state q1. So the NFA will be at state q1 

    only. This is represented by state q1 of the equivalent DFA.

    If input = 1, the NFA will go to a dead state since all processing will stop.This is represented by state qdead of the equivalent DFA.

    Updating the transition function to reflect these gives

  • 8/21/2019 MELJUN CORTES Automata Lecture Equivalence of Nfas and Dfas Part 1 1

    6/13

     

    Theory of Computation (With Automata Theory)

    Equivalence of NFAs and DFAs *Property o f STIPage 6 of 13

    The next states to be analyzed are states q1 and qdead since these are the statesthat can be reached from q0 as of this time.

    NFA and DFA Equivalence

    Page 9 of 21

    From state q1 (this is the DFA state that represents the situation where the NFA is

    at state q1 only).

    If input = 0, the NFA will stay at state q1. So the NFA will be at state q1 only. This is represented by state q1 of the equivalent DFA.

    If input = 1, the NFA will have two copies. One copy will stay at state q1 while the other copy will go to state q2. So the NFA will be at states q1 and q2. This is represented by state q12 of the equivalent DFA.

    From state qdead (this is the DFA state that represents the situation where all copiesof the NFA have stopped processing).

    The NFA will stay at its dead state regardless of the input symbolreceived. So the DFA will also stay at qdead  regardless of the input

    symbol arriving.

    Updating the transition function to reflect these gives

    The only state reachable from q0 that has not been analyzed so far is state q12 (it isreachable from state q0 through state q1). Therefore, it will be analyzed next.

  • 8/21/2019 MELJUN CORTES Automata Lecture Equivalence of Nfas and Dfas Part 1 1

    7/13

     

    Theory of Computation (With Automata Theory)

    Equivalence of NFAs and DFAs *Property o f STIPage 7 of 13

    NFA and DFA EquivalencePage 10 of 21

    From state q12 (this is the DFA state that represents the situation where the NFA isat states q1 and q2)

    If input = 0, the copy of the NFA which is at state q1 stays at q1. Theother copy which is at state q2 will stop processing. So the NFA will be at

    state q1 only. This is represented by state q1 of the equivalent DFA.

    If input = 1, the copy of the NFA which is at state q1 will go to states q1 and q2. While the other copy which is at q2 will stop processing. So theNFA will be at states q1 and q2. This is represented by state q12 of theequivalent DFA.

    Updating the transition function to reflect these gives

    NFA and DFA EquivalencePage 11 of 21

    So far the only states of the equivalent DFA that came out during the analysis areq1, q12, and qdead. All these three states have been analyzed already (they havecomplete entries in the transition function). It can therefore be said that thesestates are the only states that are reachable from the start state q0.

    The transition function will then be

    Observe that the DFA states q2, q01, q02, and q012 are not reachable from state q0.These are the useless states. For example, state q2  of the equivalent DFA isunreachable from state q0 because there is no situation wherein the NFA N 1 will bein state q2 only. Similarly, state q012 of the equivalent DFA is unreachable fromstate q0 because there is no situation wherein the NFA N 1 will be at states q0, q1,and q2 at the same time.

    These states can therefore be removed from the transition table.

  • 8/21/2019 MELJUN CORTES Automata Lecture Equivalence of Nfas and Dfas Part 1 1

    8/13

     

    Theory of Computation (With Automata Theory)

    Equivalence of NFAs and DFAs *Property o f STIPage 8 of 13

    NFA and DFA EquivalencePage 12 of 21

    The final transition function will then be

    The state diagram of the equivalent DFA is

    0 1q 0

    0 1

    q 12q 1

    0

    q dead

    0, 1

    1

     

    NFA and DFA EquivalencePage 13 of 21

     As another example, consider now the following NFA N 2.

    1q 0

    0, 1

    q 2q 1

    0, 1

    1

     

    Take note that N 2  accepts strings over the alphabet     = {0, 1} that contain the

    substring 11.

    Since this NFA has three states, the possible states N 2 can assume are q0 only, q1 only, q2 only, q0 and q1, q0 and q2, q1 and q2, q1 and q2 and q3, and the dead state.This is similar to the previous example.

  • 8/21/2019 MELJUN CORTES Automata Lecture Equivalence of Nfas and Dfas Part 1 1

    9/13

     

    Theory of Computation (With Automata Theory)

    Equivalence of NFAs and DFAs *Property o f STIPage 9 of 13

    NFA and DFA EquivalencePage 14 of 21

    So the states of the equivalent DFA will be:

    The next step now is to determine the start state and the final states of theequivalent DFA.

    The initial state of the NFA N 2  is q0 so the initial state of the equivalent DFA willalso be q0.

    NFA and DFA EquivalencePage 15 of 21

    The final state of NFA N 2 is q2. This means that the NFA is in the final state if onecopy of the NFA is at state q2. So the NFA is in the final state if it is in any of thefollowing states:

    1. q2 2. q0, q2 3. q1, q2 4. q0, q1, q2 

    In other words, the NFA will be in the final state if q2  is included in the subset.Therefore, the final states for the equivalent DFA are q2, q02, q12, and q012.

    The transition table for the equivalent DFA will be in the following form:

    For each state that is reachable from state q0, we next analyze to which state theequivalent DFA will go to given the arrival of each possible input symbol.

    Starting at state q0  (this is the DFA state that represents the situation where theNFA is at state q0 only):

    If input = 0, the NFA will go to state q0. So the NFA will be at state q0 only. This is represented by state q0 of the equivalent DFA.

    If input = 1, the NFA will have two copies. One copy will stay at state q0 

  • 8/21/2019 MELJUN CORTES Automata Lecture Equivalence of Nfas and Dfas Part 1 1

    10/13

     

    Theory of Computation (With Automata Theory)

    Equivalence of NFAs and DFAs *Property o f STIPage 10 of 13

    while the other copy will go to state q1. So the NFA will be at states q0 and q1. This is represented by state q01 of the equivalent DFA.

    Updating the transition function to reflect these gives

    The next state to be analyzed is state q01  since this is the only state that isreachable from q0 that has not been analyzed so far.

    NFA and DFA EquivalencePage 16 of 21

    From state q01 (this is the DFA state that represents the situation where the NFA isat states q0 and q1)

    If input = 0, the copy of the NFA which is at state q0 will stay at q0. Theother copy which is at q1 will stop processing. The NFA will then be atstate q0 only. This is represented by state q0 of the equivalent DFA.

    If input = 1, the copy of the NFA which is at state q0 will go to states q0 and q1. The other copy, which is at state q1, will go to state q2. The NFAwill then be at states q0, q1, and q2. This is represented by state q012 ofthe equivalent DFA.

    Updating the transition function to reflect these gives

    The only state reachable from q0 that has not been analyzed so far is state q012 (itis reachable from state q0 through state q01). Therefore, it will be analyzed next.

  • 8/21/2019 MELJUN CORTES Automata Lecture Equivalence of Nfas and Dfas Part 1 1

    11/13

     

    Theory of Computation (With Automata Theory)

    Equivalence of NFAs and DFAs *Property o f STIPage 11 of 13

    NFA and DFA EquivalencePage 17 of 26

    From state q012 (this is the DFA state that represents the situation where the NFA isat states q0,q1, and q2)

    If input = 0, the copy of the NFA which is at state q0 will stay at state q0.

    The second copy which is at state q1 will stop processing. The third copywhich is at state q2 will stay at state q2. The NFA will then be at states q0 and q2. This is represented by state q02 of the equivalent DFA.

    If input = 1, the copy of the NFA which is at state q0 will go to states q0 and q1. The second copy which is at state q1 will go to state q2. The thirdcopy of the NFA which is at state q2 will stay at state q2. The NFA willthen be at states q0, q1, and q2. This is represented by state q012 of theequivalent DFA.

    Updating the transition function to reflect these gives

    The only state reachable from q0 that has not been analyzed so far is state q02 (it is

    reachable from state q0 through state q01 and then through q012). Therefore, it willbe analyzed next.

    NFA and DFA EquivalencePage 18 of 21

    From state q02 (this is the DFA state that represents the situation where the NFA isat states q0 and q2)

    If input = 0, the copy of the NFA which is at state q0 will stay at state q0.The other copy which is at state q2 will stay at state q2. The NFA will thenbe at states q0 and q2. This is represented by state q02 of the equivalentDFA.

    If input = 1, the copy of the NFA which is at state q0 will go to states q0 and q1. The other copy which is at state q2 will stay state q2. The NFA

    will then be at states q0, q1, and q2. This is represented by state q012 ofthe equivalent DFA.

    Updating the transition function to reflect these gives

  • 8/21/2019 MELJUN CORTES Automata Lecture Equivalence of Nfas and Dfas Part 1 1

    12/13

     

    Theory of Computation (With Automata Theory)

    Equivalence of NFAs and DFAs *Property o f STIPage 12 of 13

    NFA and DFA EquivalencePage 19 of 21

    So far the only states of the equivalent DFA that came out during the analysis areq01, q02, and q012. All these three states had been analyzed already (they havecomplete entries in the transition function. It can therefore be said that these statesare the only states that are reachable from the start state q0.

    The transition function will be

    Observe that states q1, q2, q12, and qdead are not reachable from state q0. Thesecan therefore be removed from the transition table.

  • 8/21/2019 MELJUN CORTES Automata Lecture Equivalence of Nfas and Dfas Part 1 1

    13/13

     

    Theory of Computation (With Automata Theory)

    Equivalence of NFAs and DFAs *Property o f STIPage 13 of 13

    NFA and DFA EquivalencePage 20 of 21

    The final transition table will then be

    The state diagram of the equivalent DFA is

    1q 0

    0 1

    q 012q 01

    0

    1

    0q 02

    0 1

     

    NFA and DFA EquivalencePage 21 of 21

    For the given NFA, have the students construct the equivalent DFA.

    The given NFA accepts all strings that end with a 00.

    0q 0

    1

    q 012q 010

    0

    1

    1

     

    [NFA and DFA Equivalence, Pages 1 –

    21 of 21]