automata - chap2+finiteautomata

Upload: scodrash

Post on 02-Jun-2018

218 views

Category:

Documents


0 download

TRANSCRIPT

  • 8/10/2019 Automata - chap2+finiteautomata

    1/47

    SSK5204

    CHAPTER2: FINITEAUTOMATA

    Dr. Nor Fazlida Mohd Sani

    Dept. of Computer Science

    Fac. of Computer Science and Information

    Technology, UPM.

    1

  • 8/10/2019 Automata - chap2+finiteautomata

    2/47

    Finite Automata

    2

  • 8/10/2019 Automata - chap2+finiteautomata

    3/47

    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 Automata - chap2+finiteautomata

    4/47

    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 Automata - chap2+finiteautomata

    5/47

    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 Automata - chap2+finiteautomata

    6/47

    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 Automata - chap2+finiteautomata

    7/47

    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 Automata - chap2+finiteautomata

    8/47

    EXAMPLES

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

    8

  • 8/10/2019 Automata - chap2+finiteautomata

    9/47

    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 Automata - chap2+finiteautomata

    10/47

    EXAMPLES

    Construct a DFA that accepts the language

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

    10

  • 8/10/2019 Automata - chap2+finiteautomata

    11/47

    EXAMPLES

    Construct a DFA that accepts the language

    Answer

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

    qe

    q0

    q1

    q01 q010

    qdie0, 1

    0

    1 0

    0, 11

    0 1

    0, 1

    11

  • 8/10/2019 Automata - chap2+finiteautomata

    12/47

    EXAMPLES

    Construct a DFA over alphabet {0, 1}that

    accepts all strings that end in 01

    12

  • 8/10/2019 Automata - chap2+finiteautomata

    13/47

    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

    13

  • 8/10/2019 Automata - chap2+finiteautomata

    14/47

    EXAMPLES

    Construct a DFA over alphabet {0, 1}that acceptsall strings that end in 01

    Answer:

    qe

    0

    1

    q0

    q1

    q00

    q10

    q01

    q11

    0

    1

    01

    0

    0

    1

    1

    1

    1

    0

    0

    14

  • 8/10/2019 Automata - chap2+finiteautomata

    15/47

    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

    15

  • 8/10/2019 Automata - chap2+finiteautomata

    16/47

    Nondeterminism

    16

  • 8/10/2019 Automata - chap2+finiteautomata

    17/47

    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:

    17

  • 8/10/2019 Automata - chap2+finiteautomata

    18/47

    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

    18

  • 8/10/2019 Automata - chap2+finiteautomata

    19/47

    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

    19

  • 8/10/2019 Automata - chap2+finiteautomata

    20/47

    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

    20

  • 8/10/2019 Automata - chap2+finiteautomata

    21/47

  • 8/10/2019 Automata - chap2+finiteautomata

    22/47

    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

    22

  • 8/10/2019 Automata - chap2+finiteautomata

    23/47

  • 8/10/2019 Automata - chap2+finiteautomata

    24/47

  • 8/10/2019 Automata - chap2+finiteautomata

    25/47

    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

    25

  • 8/10/2019 Automata - chap2+finiteautomata

    26/47

    EXAMPLE

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

    those strings that contain the pattern001

    26

  • 8/10/2019 Automata - chap2+finiteautomata

    27/47

    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

    27

  • 8/10/2019 Automata - chap2+finiteautomata

    28/47

    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

    28

  • 8/10/2019 Automata - chap2+finiteautomata

    29/47

    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

    29

  • 8/10/2019 Automata - chap2+finiteautomata

    30/47

    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}

    30

  • 8/10/2019 Automata - chap2+finiteautomata

    31/47

    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

    31

  • 8/10/2019 Automata - chap2+finiteautomata

    32/47

    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

    STOP32

  • 8/10/2019 Automata - chap2+finiteautomata

    33/47

    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)33

  • 8/10/2019 Automata - chap2+finiteautomata

    34/47

    NFA to DFA conversion

    34

  • 8/10/2019 Automata - chap2+finiteautomata

    35/47

    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

    35

  • 8/10/2019 Automata - chap2+finiteautomata

    36/47

    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

    36

  • 8/10/2019 Automata - chap2+finiteautomata

    37/47

    NFA TODFA CONVERSIONINTUITION

    1 0

    0, 1

    q0 q1 q2NFA:

    DFA: 1q0 q0or q1

    1

    q0or q2

    1

    0 0

    0

    37

  • 8/10/2019 Automata - chap2+finiteautomata

    38/47

    NFA TODFA CONVERSIONINTUITION

    1 0

    0, 1

    q0 q1 q2NFA:

    DFA: 1q0 {q0, q1}

    1

    {q0, q2}

    1

    0 0

    0

    38

  • 8/10/2019 Automata - chap2+finiteautomata

    39/47

    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}

    39

  • 8/10/2019 Automata - chap2+finiteautomata

    40/47

  • 8/10/2019 Automata - chap2+finiteautomata

    41/47

    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

    41

  • 8/10/2019 Automata - chap2+finiteautomata

    42/47

    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

    42

  • 8/10/2019 Automata - chap2+finiteautomata

    43/47

    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)

    43

  • 8/10/2019 Automata - chap2+finiteautomata

    44/47

  • 8/10/2019 Automata - chap2+finiteautomata

    45/47

    ELIMINATINGe-TRANSITIONS

    q0 q1 q2e, 1

    0

    0

    eNFA:

    NFA without es: q0 q1 q20, 1

    0

    0

    0

    0, 1

    0

    45

  • 8/10/2019 Automata - chap2+finiteautomata

    46/47

    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 qf46

  • 8/10/2019 Automata - chap2+finiteautomata

    47/47