MELJUN CORTES Automata Lecture Nondeterministic Finite Automata 2

Embed Size (px)

Citation preview

  • 8/21/2019 MELJUN CORTES Automata Lecture Nondeterministic Finite Automata 2

    1/33

    Theory of Computation (With Automata Theory)

    * Property of STI 

    Page 1 of 33

    Nondeterministic Finite Automata

    Nondeterministic Finite

    Automata

    Nondeterminism

    Introduction

    • The main characteristic of a

    DFA is that for each input

    symbol, the automaton can

    move to only one state from

    any given current state.

    • Consider a DFA that accepts

    strings over the alphabet     =

    {0, 1} that starts with a 0 and

    ends with a 1.

    0 1q 0

    0 1

    q 2q 1

    0

    q 3

    0, 1

    1

  • 8/21/2019 MELJUN CORTES Automata Lecture Nondeterministic Finite Automata 2

    2/33

    Theory of Computation (With Automata Theory)

    * Property of STI 

    Page 2 of 33

    Nondeterministic Finite Automata

    • Consider now the following

    finite automaton:

    This machine also accepts

    strings that start with a 0 and

    end with 1.

    Observe that if the machine is

    at state q1 and a 1 arrives, the

    machine goes to state  q2 and

    stays at q1 at the same time.

    This machine is not a DFA

    because there is a situation in

    which an input symbol causes

    the automaton to move to

    more than one state.

    0 1q 0

    0, 1

    q 2q 1

  • 8/21/2019 MELJUN CORTES Automata Lecture Nondeterministic Finite Automata 2

    3/33

    Theory of Computation (With Automata Theory)

    * Property of STI 

    Page 3 of 33

    Nondeterministic Finite Automata

    • The given graph is the state

    diagram for a

    nondeterm in is t ic f in i te  

    automaton (NFA).

    • Because an NFA can move to

    more than one state given a

    single input symbol, an NFA issaid to have “choices.”

    Take the given NFA as an

    example. If the NFA iscurrently in state   q1 and an

    input symbol 1 arrives, the

    NFA has the option of staying

    in state q1 or going to state q2.

  • 8/21/2019 MELJUN CORTES Automata Lecture Nondeterministic Finite Automata 2

    4/33

    Theory of Computation (With Automata Theory)

    * Property of STI 

    Page 4 of 33

    Nondeterministic Finite Automata

    In this case, the NFA will

    consider both possibilities.

    The NFA will make a   “copy” of 

    itself. One copy continues the

    computation at state   q1 while

    the other continues at state q2.

    Effectively, the NFA is said to

    be in two states, q1 and q2.

    This is in contrast with a DFA

    since DFAs can only be in one

    state at any given time.

  • 8/21/2019 MELJUN CORTES Automata Lecture Nondeterministic Finite Automata 2

    5/33

    Theory of Computation (With Automata Theory)

    * Property of STI 

    Page 5 of 33

    Nondeterministic Finite Automata

    • Notice also that in an NFA, it ispossible for a state not to have

    a transition edge for one or  

    more input symbols.

    In the given example, takenote that for state q0, there is a

    transition edge for input

    symbol 0 but none for 1.

    If a copy of the NFA is at stateq0 and an input symbol 1

    arrives, this copy stops

    processing because it has

    nowhere to go. This copy of  

    the NFA simply “dies.”

    Similarly, if a copy of the NFA

    is at q2 and a 1 or a 0 arrives,

    this copy also stops

    processing and dies.

  • 8/21/2019 MELJUN CORTES Automata Lecture Nondeterministic Finite Automata 2

    6/33

    Theory of Computation (With Automata Theory)

    * Property of STI 

    Page 6 of 33

    Nondeterministic Finite Automata

    • Here is an example to illustrate

    how an NFA processesstrings:

    Given the following NFA:

    Determine if the string 01011

    will be accepted.

    1. Initially, the NFA will be at

    state q0. When the first

    input symbol (0) arrives,

    the NFA goes to state q1as shown below:

    0 1q 0

    0, 1

    q 2q 1

    q 0

    q 1

    0

    input state

    start

  • 8/21/2019 MELJUN CORTES Automata Lecture Nondeterministic Finite Automata 2

    7/33

    Theory of Computation (With Automata Theory)

    * Property of STI 

    Page 7 of 33

    Nondeterministic Finite Automata

    2. Assume now that the

    second input symbol (1)arrives. The NFA has two

    options, either it stays at q1or goes to q2. The

    machine will try both

    possibilities at the sametime. This is shown below:

    The machine made

    another copy of itself. One

    copy to continue the

    processing at q1 and the

    other at q2.

    q 0

    q 1

    0

    input state

    (start)

    q 1

    1

    q 2

  • 8/21/2019 MELJUN CORTES Automata Lecture Nondeterministic Finite Automata 2

    8/33

    Theory of Computation (With Automata Theory)

    * Property of STI 

    Page 8 of 33

    Nondeterministic Finite Automata

    3. Assume now that the third

    input symbol (0) arrives.

    One copy of the NFA (the

    one at q1) stays at q1. The

    other copy (the one at q2)

    dies because it has

    nowhere to go.

     At this point in time, there

    will only be one remaining

    copy of the NFA (the one

    that is still at q1).

    q 0

    q 1

    0

    input state

    (start)

    q 1

    1

    q 2

    q 1

    (stopped)

    0

  • 8/21/2019 MELJUN CORTES Automata Lecture Nondeterministic Finite Automata 2

    9/33

    Theory of Computation (With Automata Theory)

    * Property of STI 

    Page 9 of 33

    Nondeterministic Finite Automata

    4. Assume now that the

    fourth input symbol (1)arrives. There are again

    two options, either stay at

    q1 or go to q2. The NFA

    will again consider both

    possibilities.

    q 0

    q 1

    0

    input state

    (start)

    q 1

    1

    q 2

    q 1

    (stopped)

    0

    q 1   q 2

    1

  • 8/21/2019 MELJUN CORTES Automata Lecture Nondeterministic Finite Automata 2

    10/33

    Theory of Computation (With Automata Theory)

    * Property of STI 

    Page 10 of 33

    Nondeterministic Finite Automata

    5. Assume now that the fifth

    and last input symbol (1)arrives. One copy of the

    NFA (the one at q1) has

    the option of staying at q1or going to q2. As usual,

    the NFA will consider both.The other copy (the one at

    q2) stops processing.

    q 0

    q 1

    0

    input state

    (start)

    q 1

    1

    q 2

    q 1

    (stopped)

    0

    q 1   q 2

    1

    q 1   q 2

    (stopped)

    1

  • 8/21/2019 MELJUN CORTES Automata Lecture Nondeterministic Finite Automata 2

    11/33

    Theory of Computation (With Automata Theory)

    * Property of STI 

    Page 11 of 33

    Nondeterministic Finite Automata

    There are two copies of theNFA. One copy is currently at

    state q1 and the other at state

    q2. Since one of the copies is

    at a final state, the NFA

    accepts the string 01011.

    If there is no copy of the NFA

    that is in a final state, then the

    string will be rejected.

    •  Another example:

    Given the following NFA:

    Determine if the string 110110

    will be accepted.

    1 0, 1q 0

    0, 1

    q 1   q 3q 2

    0, 1

  • 8/21/2019 MELJUN CORTES Automata Lecture Nondeterministic Finite Automata 2

    12/33

    Theory of Computation (With Automata Theory)

    * Property of STI 

    Page 12 of 33

    Nondeterministic Finite Automata

    1. Initially, the NFA will be at

    state q0. When the first

    input symbol (1) arrives,

    the NFA has two options,

    either to stay at q0

    or go to

    q1. The machine will try

    both possibilities.

    There will now be two

    copies of the NFA. One

    copy is at state q0 while the

    other is at state q1.

    q 0

    1

    input   state

    (start)

    q 1q 0

  • 8/21/2019 MELJUN CORTES Automata Lecture Nondeterministic Finite Automata 2

    13/33

    Theory of Computation (With Automata Theory)

    * Property of STI 

    Page 13 of 33

    Nondeterministic Finite Automata

    2. When the second input

    symbol (1) arrives, thecopy of the NFA which is at

    state q0 has two options—

    stay at q0 or go to q1. The

    machine will try both

    possibilities. The copywhich is at state q1 will go

    to q2.

    There will now be three

    copies of the NFA. One is

    at state q0, the second one

    is at state q1, and the third

    is at state q2.

    q 0

    1

    input   state

    (start)

    1

    q 0   q 1   q 2

    q 1q 0

  • 8/21/2019 MELJUN CORTES Automata Lecture Nondeterministic Finite Automata 2

    14/33

    Theory of Computation (With Automata Theory)

    * Property of STI 

    Page 14 of 33

    Nondeterministic Finite Automata

    3. When the third input

    symbol (0) arrives, the

    copy of the NFA which is at

    state q0 stays at q0, the

    copy which is at state q1goes to q2, and the copy

    which is at q2 goes to state

    q3.

    q 0

    1

    input   state

    (start)

    1

    0

    q 2q 0   q 3

    q 0   q 1   q 2

    q 1q 0

  • 8/21/2019 MELJUN CORTES Automata Lecture Nondeterministic Finite Automata 2

    15/33

    Theory of Computation (With Automata Theory)

    * Property of STI 

    Page 15 of 33

    Nondeterministic Finite Automata

    4. If the fourth input symbol

    (1) arrives, the copy of theNFA which is at state q0has two options. Stay at q0or go to q1. The machine

    will try both possibilities.

    The copy which is at stateq2 will go to q3. The copy

    which is at state q3 stops

    computing because it has

    nowhere to go.

    q 0

    1

    input   state

    (start)

    1

    0

    1

    q 1q 0   q 3

    q 2q 0   q 3  (stopped)

    q 0   q 1   q 2

    q 1q 0

  • 8/21/2019 MELJUN CORTES Automata Lecture Nondeterministic Finite Automata 2

    16/33

    Theory of Computation (With Automata Theory)

    * Property of STI 

    Page 16 of 33

    Nondeterministic Finite Automata

    5. When the fifth input symbol

    (1) arrives, the copy of theNFA which is at state q0has two options—stay at q0or go to q1. The machine

    will try both possibilities.

    The copy which is at stateq1 will go to q2. The copy

    which is at q3 dies.

    q 0

    1

    input   state

    (start)

    1

    0

    1

    (stopped)

    1

    q 0   q 1   q 2

    q 1q 0   q 3

    q 2q 0   q 3  (stopped)

    q 0   q 1   q 2

    q 1q 0

  • 8/21/2019 MELJUN CORTES Automata Lecture Nondeterministic Finite Automata 2

    17/33

    Theory of Computation (With Automata Theory)

    * Property of STI 

    Page 17 of 33

    Nondeterministic Finite Automata

    6. When the sixth and final

    input symbol (0) arrives,the copy of the NFA which

    is at state q0 stays at q0.

    The copy at state q1 goes

    to q2. The copy at state q2

    goes to q3.

    q 0

    1

    input   state

    (start)

    1

    0

    1

    q 0   q 2

    (stopped)

    1

    q 3

    q 0   q 1   q 2

    q 1q 0   q 3

    q 2q 0   q 3  (stopped)

    q 0   q 1   q 2

    q 1q 0

    0

  • 8/21/2019 MELJUN CORTES Automata Lecture Nondeterministic Finite Automata 2

    18/33

    Theory of Computation (With Automata Theory)

    * Property of STI 

    Page 18 of 33

    Nondeterministic Finite Automata

    The NFA is now at threestates,  q0,  q2, and  q3. Since

    one of these states is a final

    state (q3), then the NFA

    accepts the input string

    110110.

    •   ε -Transitions

    One other difference between

    an NFA and a DFA is that an

    NFA may have   ε -transitions.

     An  ε -transition is shown below:

    e

    q  jq i

  • 8/21/2019 MELJUN CORTES Automata Lecture Nondeterministic Finite Automata 2

    19/33

    Theory of Computation (With Automata Theory)

    * Property of STI 

    Page 19 of 33

    Nondeterministic Finite Automata

    Whenever there is an   ε -

    transition from state qi to stateq j, this means that once an

    NFA goes to state qi, it has the

    option of staying at qi or it can

    go right away to state q j even

    without any additional inputarriving.

    Example:

    Given the following NFA:

    Determine if the string 010010

    will be accepted.

    0e

    q 0

    0

    q 2q 1

    1

  • 8/21/2019 MELJUN CORTES Automata Lecture Nondeterministic Finite Automata 2

    20/33

    Theory of Computation (With Automata Theory)

    * Property of STI 

    Page 20 of 33

    Nondeterministic Finite Automata

    1. Initially, the NFA will be at

    state q0. When the first

    input symbol (0) arrives,

    the NFA goes to q1. At q1, it

    has the option ofimmediately going to q2without any new input

    symbol arriving (because

    of the ε -transition from q1to q2) . The NFA willconsider both and will be in

    two states, q1 and q2.

    q 0

    q 1

    0

    input state

    (start)

    q 2

  • 8/21/2019 MELJUN CORTES Automata Lecture Nondeterministic Finite Automata 2

    21/33

    Theory of Computation (With Automata Theory)

    * Property of STI 

    Page 21 of 33

    Nondeterministic Finite Automata

    2. When the second input

    symbol (1) arrives, the

    copy of the NFA which is at

    state q1 stops computing

    since it has nowhere to go.The copy at q2 will go to q0.

    The NFA will only be in

    one state which is q0.

    q 0

    q 1

    0

    input state

    (start)

    q 0

    1

    q 2(stopped)

  • 8/21/2019 MELJUN CORTES Automata Lecture Nondeterministic Finite Automata 2

    22/33

    Theory of Computation (With Automata Theory)

    * Property of STI 

    Page 22 of 33

    Nondeterministic Finite Automata

    3. When the third inputsymbol (0) arrives, the

    NFA goes to q1. Once at

    q1, it has the option of

    immediately going to q2

    without any new inputsymbol arriving. The NFA

    will consider both and will

    be in two states, q1 and q2.

    q 0

    q 1

    0

    input state

    (start)

    q 0

    1

    q 2(stopped)

    q 1   q 2

    0

  • 8/21/2019 MELJUN CORTES Automata Lecture Nondeterministic Finite Automata 2

    23/33

    Theory of Computation (With Automata Theory)

    * Property of STI 

    Page 23 of 33

    Nondeterministic Finite Automata

    4. When the fourth input

    symbol (0) arrives, thecopy of the NFA which is at

    state q1 stays at q1. Since

    there is an ε -transition from

    q1 to q2, it will also

    immediately go to q2. Theother copy of the NFA (the

    one at state q2) stops

    because it has nowhere to

    go.

    q 0

    q 1

    0

    input state

    (start)

    q 0

    1

    q 2(stopped)

    q 1   q 2

    0

    (stopped)

    q 1   q 2

    0

  • 8/21/2019 MELJUN CORTES Automata Lecture Nondeterministic Finite Automata 2

    24/33

    Theory of Computation (With Automata Theory)

    * Property of STI 

    Page 24 of 33

    Nondeterministic Finite Automata

    5. When the fifth input symbol(1) arrives, the copy of the

    NFA which is at state q1stops since it has nowhere

    to go. The remaining copy

    of the NFA (the one atstate q2) goes to q0.

    q 0

    q 1

    0

    input state

    (start)

    q 0

    1

    q 2(stopped)

    q 1   q 2

    0

    (stopped)

    q 1   q 2

    q 0

    0

    1

    (stopped)

  • 8/21/2019 MELJUN CORTES Automata Lecture Nondeterministic Finite Automata 2

    25/33

    Theory of Computation (With Automata Theory)

    * Property of STI 

    Page 25 of 33

    Nondeterministic Finite Automata

    6. When the sixth and last

    input symbol (0) arrives,

    the NFA goes to q1.  At q1,

    it has the option ofimmediately going to q2without any new input

    symbol arriving (because

    of the ε -transition from q1

    to q2).

    The NFA will then be at

    state q1

    and state q2

    .

    Since one of these states

    is a final state (q1), the

    string 010010 is accepted.

  • 8/21/2019 MELJUN CORTES Automata Lecture Nondeterministic Finite Automata 2

    26/33

    Theory of Computation (With Automata Theory)

    * Property of STI 

    Page 26 of 33

    Nondeterministic Finite Automata

    q 0

    q 1

    0

    input state

    (start)

    q 0

    1

    q 2(stopped)

    q 1   q 2

    0

    (stopped)

    q 1   q 2

    q 0

    0

    1

    q 1   q 2

    0

    (stopped)

  • 8/21/2019 MELJUN CORTES Automata Lecture Nondeterministic Finite Automata 2

    27/33

    Theory of Computation (With Automata Theory)

    * Property of STI 

    Page 27 of 33

    Nondeterministic Finite Automata

    • To summarize the differences

    between an NFA and a DFA:

    1. NFAs can move to more

    than one state from anygiven current state. They

    can therefore be in several

    states at the same time.

    2. In a given state, NFAs may

    not necessarily have a

    transition edge for each

    symbol in the alphabet.

    3. NFAs can move to a state

    even without any input

    symbol arriving (ε-

    transitions).

  • 8/21/2019 MELJUN CORTES Automata Lecture Nondeterministic Finite Automata 2

    28/33

    Theory of Computation (With Automata Theory)

    * Property of STI 

    Page 28 of 33

    Nondeterministic Finite Automata

    Nondeterministic Finite Automata

    Formal Definition:

    • The formal definition of a NFA

    is similar to that of a DFA.

    • The main difference lies in the

    transition function.

    • The transition function of anNFA must consider the

    possibility of state transitions

    even if there is no input

    symbol (ε -transitions).

    • The transition function must

    also consider the possibility

    that the NFA may go to

    several states given the same

    input symbol.

  • 8/21/2019 MELJUN CORTES Automata Lecture Nondeterministic Finite Automata 2

    29/33

    Theory of Computation (With Automata Theory)

    * Property of STI 

    Page 29 of 33

    Nondeterministic Finite Automata

    • For example:

    The formal definition of this

    NFA is a 5-tuple N 1 = {Q,   ,    ,

    q0, F } where:

    1. Q = {q0, q1, q2}2.    = {0, 1}

    3.    :

    4. Start State = q0.

    5. F = {q2}

    0 1   ε

    q0   q1   -- --

    q1   q1   q1, q2  --

    q2   -- -- --

    0 1q 0

    0, 1

    q 2q 1

  • 8/21/2019 MELJUN CORTES Automata Lecture Nondeterministic Finite Automata 2

    30/33

    Theory of Computation (With Automata Theory)

    * Property of STI 

    Page 30 of 33

    Nondeterministic Finite Automata

    •  Another example:

    The formal definition of this

    NFA is a 5-tuple N 2 = {Q,   ,    ,

    q0, F } where:

    1. Q = {q0, q1, q2}

    2.    = {0, 1}

    3.    :

    4. Start State = q0

    5. F = {q1}

    0 1   ε

    q0   q1   -- --

    q1   q1   -- q2

    q2   -- q0 --

    0e

    q 0

    0

    q 2q 1

    1

  • 8/21/2019 MELJUN CORTES Automata Lecture Nondeterministic Finite Automata 2

    31/33

    Theory of Computation (With Automata Theory)

    * Property of STI 

    Page 31 of 33

    Nondeterministic Finite Automata

    More exercises:

    • NFA N 1:

    • NFA N 2:

    10 1

    0

    q 2q 1q 0

    e

    1q 0

    0, 1

    q 2q 1

    0, 1

    1

  • 8/21/2019 MELJUN CORTES Automata Lecture Nondeterministic Finite Automata 2

    32/33

    Theory of Computation (With Automata Theory)

    * Property of STI 

    Page 32 of 33

    Nondeterministic Finite Automata

    More exercises:

    • NFA N 3:

    • NFA N 4:

    1

    q 1

    e

    q 3

    q 0

    q 4   q 50 1

    0

    q 2

    1   0

    10 e

    q 0

    0, 1

    q 2   q 3q 1

    0, 1

    1

  • 8/21/2019 MELJUN CORTES Automata Lecture Nondeterministic Finite Automata 2

    33/33

    Theory of Computation (With Automata Theory)

    • In an NFA, a state may have

    zero, one, or many outgoing

    edges for each input symbol.

    DFAs can only have one

    outgoing edge for each inputsymbol.

    This means that a DFA is a

    restricted type of NFA. It can

    therefore be said that all DFAs

    are NFAs.

    However, not all NFAs are

    DFAs.

    In other words,

    nondeterminism is a

    generalization of determinism.