chap2+finiteautomataStud

download chap2+finiteautomataStud

of 45

Transcript of chap2+finiteautomataStud

  • 8/10/2019 chap2+finiteautomataStud

    1/45

    SSK5204

    CHAPTER2: FINITEAUTOMATA

    Dr. Nor Fazlida Mohd Sani

    Dept. of Computer Science

    Fac. of Computer Science and Information

    Technology, UPM.

    1

  • 8/10/2019 chap2+finiteautomataStud

    2/45

    Finite Automata

    2

  • 8/10/2019 chap2+finiteautomataStud

    3/45

    EXAMPLEOFAFINITEAUTOMATON

    There are statesevenand odd, evenis the startstate

    The automaton takes inputsfrom alphabet {L, R}

    The state evenis an accepting stateThere are transitionssaying what to do for every

    state and every alphabet symbol

    even odd

    L, R

    L, R

    3

  • 8/10/2019 chap2+finiteautomataStud

    4/45

    DETERMINISTICFINITEAUTOMATA

    A finite automaton(DFA) is a 5-tuple (Q, S, d, q0,

    F)where

    Qis a finite set of states

    Sis an alphabet d:QSQis a transition function

    q0Qis the initial state

    F Q is a set of accepting states(or final states).

    In diagrams, the accepting states will be

    denoted by double loops 4

  • 8/10/2019 chap2+finiteautomataStud

    5/45

    EXAMPLE

    5

    q0 q1 q21 0

    0 0,11

    alphabet S= {0, 1}

    statesQ= {q0, q1, q2}

    initial state q0

    accepting states F= {q0, q1}

    states

    inputs

    0 1q0q1q2

    q0 q1q2

    q2q2

    q1

    table of

    transition function d:

  • 8/10/2019 chap2+finiteautomataStud

    6/45

    LANGUAGEOFADFA

    Language ofMis {x {L, R}*: xhas even length} 6

    The language of a DFA (Q, S, d, q0, F)is the set of

    all strings over Sthat, starting from q0and

    following the transitions as the string is read left

    to right, will reach some accepting state.

    M: even odd

    L, R

    L, R

  • 8/10/2019 chap2+finiteautomataStud

    7/45

    q0 q1

    b a

    a

    b

    What are the languages of these automata?

    EXAMPLES

    q0

    q1

    q2

    q3

    q4

    a

    a

    a a

    a

    b

    b

    bb

    b

    q0 q1

    0 1

    1 0 q2

    0, 1

    S= {a, b} S= {a, b}

    S= {0, 1}

    7

  • 8/10/2019 chap2+finiteautomataStud

    8/45

    EXAMPLES

    Construct a DFA over alphabet {0, 1} that acceptsall strings with at most three 1s

    8

  • 8/10/2019 chap2+finiteautomataStud

    9/45

    EXAMPLES

    Construct a DFA over alphabet {0, 1} that acceptsall strings with at most three 1s

    Answer

    q0 q1

    0

    1 1 q2

    0

    q31 q4+

    0, 10

    1

    0

    9

  • 8/10/2019 chap2+finiteautomataStud

    10/45

    EXAMPLES

    Construct a DFA that accepts the language

    L= {010, 1} ( S= {0, 1} )

    10

  • 8/10/2019 chap2+finiteautomataStud

    11/45

  • 8/10/2019 chap2+finiteautomataStud

    12/45

    EXAMPLES

    Construct a DFA over alphabet {0, 1}that

    accepts all strings that end in 01

    Hint: The DFA must remember the last 2bits of the string it is reading

    12

  • 8/10/2019 chap2+finiteautomataStud

    13/45

    EXAMPLES

    Construct a DFA over alphabet {0, 1}that accepts

    all strings that end in 101

    Sketch of answer:

    0

    1

    qe

    q0

    q1

    q00

    q10

    q01

    q11

    q000

    q001

    q101

    q111

    0

    1

    0

    1

    0

    1

    1

    1

    1

    1

    0

    13

  • 8/10/2019 chap2+finiteautomataStud

    14/45

    Nondeterminism

    14

  • 8/10/2019 chap2+finiteautomataStud

    15/45

    EXAMPLE

    0

    1

    qe

    q0

    q1

    q00

    q10

    q01

    q11

    q000

    q001

    q101

    q111

    0

    1

    0

    1

    0

    1

    1

    1

    1

    1

    0

    Construct a DFA over alphabet {0, 1}thataccepts those strings that end in 101

    Sketch of answer:

    15

  • 8/10/2019 chap2+finiteautomataStud

    16/45

    WOULDBEEASIERIF

    Suppose we could guesswhen the string we are

    reading has only 3 symbols left

    Then we could simply look for the sequence 101

    and accept if we see it

    qdie

    1

    0 13 symbols left

    This is nota DFA!

    0

    1

    0

    16

  • 8/10/2019 chap2+finiteautomataStud

    17/45

    NONDETERMINISM

    Nondeterminismis the ability to make guesses,

    which we can later verify

    How a nondeterministicautomaton for strings that

    end in 101works:

    1. Guessif you are approaching end of input

    2. If guess is yes, look for 101and accept if you see it

    3. If guess is no, read one more symbol and go to step1

    17

  • 8/10/2019 chap2+finiteautomataStud

    18/45

    NONDETERMINISTICFINITEAUTOMATON

    This is a kind of automaton that allows you to

    make guesses

    Each state can have zero, one, or moreoutgoingtransitions labeled by the same symbol

    1 0 1

    0, 1

    q0 q1 q2 q3

    18

  • 8/10/2019 chap2+finiteautomataStud

    19/45

    THEABILITYTOMAKECHOICES

    1 0 1

    0, 1

    q0 q1 q2 q3

    State q0has two transitions labeled 1

    Upon reading 1, we have the choiceof staying in

    q0or moving to q1

    19

  • 8/10/2019 chap2+finiteautomataStud

    20/45

    THEABILITYTOMAKECHOICES

    1 0 1

    0, 1

    q0 q1 q2 q3

    State q1has no transition labeled 1

    Upon reading 1 in q1, we die; upon reading 0, we

    continue to q2

    20

  • 8/10/2019 chap2+finiteautomataStud

    21/45

    THEABILITYTOMAKECHOICES

    1 0 1

    0, 1

    q0 q1 q2 q3

    State q3has no transition going out

    Upon reading 0or 1in q3, we die

    21

  • 8/10/2019 chap2+finiteautomataStud

    22/45

    MEANINGOFNFA

    1 0 1

    0, 1

    q0 q1 q2 q3

    Guess if you are 3

    symbols away from end of

    input

    If so, guess you

    will see the pattern101

    Check that you are at

    the end of input

    22

  • 8/10/2019 chap2+finiteautomataStud

    23/45

    HOWTORUNANNFA

    1 0 1

    0

    q0 q1 q2 q3

    input: 0 1 1 0 1

    ,1

    The NFA can have several active statesat the same time

    23

  • 8/10/2019 chap2+finiteautomataStud

    24/45

    EXAMPLE

    Construct an NFA over alphabet {0, 1} that accepts

    those strings that contain the pattern001

    24

  • 8/10/2019 chap2+finiteautomataStud

    25/45

    EXAMPLE

    Construct an NFA over alphabet {0, 1} that accepts thosestrings that contain the pattern 001 somewhere

    Answer

    0 0 1

    0, 1

    q0 q1 q2 q3

    0, 1

    25

  • 8/10/2019 chap2+finiteautomataStud

    26/45

    DEFINITION

    A nondeterministic finite automaton(NFA) is a5-tuple (Q, S, d, q0, F)where

    Qis a finite set of states

    Sis an alphabet

    d:Q(S {e}) subsets of Qis a transition function q0Qis the initial state

    F Q is a set of accepting states (or final states).

    Differences from DFA: transition functiondcan go into several states

    It allows e-transitions

    26

  • 8/10/2019 chap2+finiteautomataStud

    27/45

    LANGUAGEOFANNFA

    The language of an NFA is the set of allstrings for which there is somepath that,starting from q0, leads to an accepting state as

    the string is read left to right (and e-transitions

    are taken for free). Example

    e, 00, 001, 101are accepted, but 11, 0110are not

    q0e, 1 0

    0

    eq1 q2

    27

  • 8/10/2019 chap2+finiteautomataStud

    28/45

    EXAMPLE

    alphabet S = {0, 1}

    states Q= {q0, q1, q2}

    initial state q0

    accepting states F= {q2}

    states

    inputs0 1

    q0

    q1

    q2

    {q1}

    table of

    transition function d :

    {q0, q1}

    q0e, 1 0

    0

    eq1 q2

    e

    {q1}

    {q2}

    28

  • 8/10/2019 chap2+finiteautomataStud

    29/45

    EXAMPLES

    q0e, 1 e

    0

    q1 q2

    q0e, 1

    e

    0

    q1 q2

    e1 2

    00

    0

    01

    2

    3

    4

    5

    q0

    e, 1e

    0q1 q2

    01

    2 3

    4

    q0e, 1

    e

    0

    q1 q2

    01

    2

    3

    54

    or

    or

    29

  • 8/10/2019 chap2+finiteautomataStud

    30/45

    EXAMPLES

    q0e , 1 e

    0

    q1 q21 5

    001

    0

    q0

    e, 1e

    0q1 q2

    01

    4

    q0e, 1

    e

    0

    q1 q2

    01

    2

    3

    4

    1012

    3

    11

    q0e, 1

    e

    0

    q1 q2

    01

    2

    STOP

    STOP30

  • 8/10/2019 chap2+finiteautomataStud

    31/45

    EXAMPLEOFe-TRANSITIONS

    Construct an NFA that accepts all strings with an evennumber of 0s oran odd number of 1s

    r0 r1

    1 1

    0

    0

    even number of 0s

    s0 s1

    0 0

    1

    1

    odd number of 1s

    q0

    e

    e

    e-transitions can be taken for free (without reading input)31

  • 8/10/2019 chap2+finiteautomataStud

    32/45

    NFA to DFA conversion

    32

  • 8/10/2019 chap2+finiteautomataStud

    33/45

    NFASAREASPOWERFULASDFAS

    Obviously, an NFA can do everything a DFA can do

    How about the other way?

    If Lis the language of some NFA, thenit is also the language of some DFA.

    YES

    33

  • 8/10/2019 chap2+finiteautomataStud

    34/45

    CONVERTINGNFASINTODFAS

    We will show a general way to convert every NFA into an

    equivalent DFA

    Step 1:Simplify NFA by eliminating e-transitions

    Step 2: Convert simplified NFA (without es)

    We do this first

    34

  • 8/10/2019 chap2+finiteautomataStud

    35/45

    NFA TODFA CONVERSIONINTUITION

    1 0

    0, 1

    q0 q1 q2NFA:

    DFA: 1q0 q0or q1

    1

    q0or q2

    1

    0 0

    0

    35

  • 8/10/2019 chap2+finiteautomataStud

    36/45

    NFA TODFA CONVERSIONINTUITION

    1 0

    0, 1

    q0 q1 q2NFA:

    DFA: 1q0 {q0, q1}

    1

    {q0, q2}

    1

    0 0

    0

    36

  • 8/10/2019 chap2+finiteautomataStud

    37/45

    GENERALMETHOD

    NFA DFAstates q0, q1, , qn {q0}, {q1}, {q0,q1}, , {q0,,qn}

    one for each subset of statesin the NFA

    initial state q0 {q0}

    transitions d d({qi1,,qik}, a) =

    d(qi1

    , a) d(qik

    , a)

    accepting

    states

    F Q F = {S: Scontains some statein F}

    37

  • 8/10/2019 chap2+finiteautomataStud

    38/45

    CONVERTINGVIATHEGENERALMETHOD

    1 0

    0, 1

    q0 q1 q2NFA:

    DFA:

    {q0, q1}

    {q0, q2} {q0, q1, q2}

    {q1, q2}

    {q0}

    {q1}

    {q2}

    0, 1

    0

    1

    0

    1

    0

    1

    0 1

    0

    1

    0

    1

    38

  • 8/10/2019 chap2+finiteautomataStud

    39/45

    CONVERTINGVIATHEGENERALMETHOD

    {q0, q1}

    {q0, q2} {q0, q1, q2}

    {q1, q2}

    {q0}

    {q1}

    {q2}

    0, 1

    0

    1

    0

    1

    0

    1

    0 1

    0

    1

    0

    1

    After eliminating the dead states andtransitions, we end up with the same picture

    39

  • 8/10/2019 chap2+finiteautomataStud

    40/45

    WHYTHEMETHODWORKS

    At the end, the DFA accepts when it is in a state thatcontains some accepting state of NFA

    So the DFA accepts only when the NFA accepts too

    After reading nsymbols, the DFA is in state{qi1,,qik}if and only if the NFA is in one of the

    states qi1,,qik

    40

  • 8/10/2019 chap2+finiteautomataStud

    41/45

    CONVERTINGNFASINTODFAS

    We will show a general way to convert every NFA into an

    equivalent DFA

    Step 1:Simplify NFA by eliminating e-transitions

    Step 2: Convert simplified NFA (without es)

    41

  • 8/10/2019 chap2+finiteautomataStud

    42/45

    ELIMINATINGe-TRANSITIONS

    q0 q1 q2e, 1 0

    0

    eeNFA:

    Transition table of corresponding NFA:

    states

    inputs0 1

    q0

    q1

    q2

    {q1, q2}{q0, q1, q2}

    {q0, q1, q2}

    Accepting states of NFA: q0, q1, q242

  • 8/10/2019 chap2+finiteautomataStud

    43/45

    ELIMINATINGe-TRANSITIONS

    q0 q1 q2e, 1

    0

    0

    eNFA:

    NFA without es: q0 q1 q20, 1

    0

    0

    0

    0, 1

    0

    43

  • 8/10/2019 chap2+finiteautomataStud

    44/45

    ELIMINATINGe-TRANSITIONS: GENERAL

    METHOD

    For every pair of states qi, qj and every symbol a S,replace every pathlike

    For every accept state qf, make accepting all states

    connected to it via es:

    qi q5 q2 q0 qje e ea

    q4

    qi

    q5

    a

    e

    e

    qi qja qi

    a

    e e

    e

    q9 q7 q3 qf44

  • 8/10/2019 chap2+finiteautomataStud

    45/45

    ELIMINATINGe-TRANSITIONS

    q0 q1 q2e, 1

    0

    0

    eNFA:

    NFA without es: q0 q1 q20

    0

    0

    00

    , 1

    0, 1

    45