MELJUN CORTES Automata Lecture Designing Finite Automata 2

download MELJUN CORTES Automata Lecture Designing Finite Automata 2

of 29

Transcript of MELJUN CORTES Automata Lecture Designing Finite Automata 2

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

    1/29

    Theory of Computation (With Automata Theory)

    * Property of STI 

    Page 1 of 29

    Designing Deterministic Finite Automata

    Designing Deterministic

    Finite AutomataDesigning DFAs

    Case Study 1: Design a DFA that

    accepts all strings over the alphabet  = {0, 1} with an odd number of 1s.

    • The states of a DFA represent

    something the machine must“remember” about the input

    string.

    • Assume that several inputsymbols have already arrived.

    Upon the arrival of the next

    symbol, the machine does not

    have to remember the exact

    number of 1s it has received.

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

    2/29

    Theory of Computation (With Automata Theory)

    * Property of STI 

    Page 2 of 29

    Designing Deterministic Finite Automata

    • Instead, it 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.

    • This implies that only two

    states are needed. One torepresent the fact that the

    current number of 1s is odd,

    and the other to represent

    even.

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

    3/29

    Theory of Computation (With Automata Theory)

    * Property of STI 

    Page 3 of 29

    Designing Deterministic Finite Automata

    • The state in which the number 

    of 1s is even will be

    designated as the start state.

    This is because while there is

    no input symbol yet, thenumber of 1s is zero (even).

    • Obviously, the   “odd state” will

    be the final state.

    • Let these two states be:

    q0 = current number of 1sis even.

    q1 = current number of 1s

    is odd.

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

    4/29

    Theory of Computation (With Automata Theory)

    * Property of STI 

    Page 4 of 29

    Designing Deterministic Finite Automata

    •  Assume that the DFA is in

    state q0 (even):

    If it receives a 0, it remains

    in that state since the

    number of 1s is still even.

    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 (odd):

    If it receives a 0, it remains

    in that state since thenumber of 1s is still odd.

    If it receives a 1, it moves

    to state q0 since the

    number of 1s is now even.

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

    5/29

    Theory of Computation (With Automata Theory)

    * Property of STI 

    Page 5 of 29

    Designing Deterministic Finite Automata

    • The state diagram for the

    required DFA:

    1

    1

    00

    q 1q 0

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

    6/29

    Theory of Computation (With Automata Theory)

    * Property of STI 

    Page 6 of 29

    Designing Deterministic Finite Automata

    Case Study 2: Design a DFA that

    accepts all strings over the alphabet

      = {0, 1} that contains the

    substring 011.

    •  As in case study 1, the DFA to

    be designed does not have to

    remember each and everyinput symbol that arrives.

    •  At any point in time, the DFA

    only has to remember whether the substring 011 has been

    encountered.

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

    7/29

    Theory of Computation (With Automata Theory)

    * Property of STI 

    Page 7 of 29

    Designing Deterministic Finite Automata

    • The DFA would have to

    remember the following things

    (states):

    1. it has not encountered the

    first symbol of the

    substring so far.

    2. it has encountered the firstsymbol of the substring (0).

    3. it has encountered the first

    two symbols of the

    substring (01).

    4. it has encountered all three

    symbols of the substring

    (011).

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

    8/29

    Theory of Computation (With Automata Theory)

    * Property of STI 

    Page 8 of 29

    Designing Deterministic Finite Automata

    • The state where the DFA hadnot yet encountered any of the

    symbols of the substring will

    be designated as the start

    state while the state where it

    had encountered all symbolswill be the final state.

    • The DFA will have four states:

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

    2. q1 = the first symbol had

    arrived.

    3. q2 = the first two symbols

    had arrived.

    4. q3 = all three symbols had

    arrived.

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

    9/29

    Theory of Computation (With Automata Theory)

    * Property of STI 

    Page 9 of 29

    Designing Deterministic Finite Automata

    •  Assume that the DFA is in

    state   q0 (had not encountered

    first symbol in the substring):

    If a 0 arrives, it moves to

    state q1 since it had

    encountered the first

    symbol of the substring.

    If a 1 arrives, it stays in

    that state because thesubstring must start with a

    0.

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

    10/29

    Theory of Computation (With Automata Theory)

    * Property of STI 

    Page 10 of 29

    Designing Deterministic Finite Automata

    •  Assume that the DFA is instate   q1 (only the first symbol,

    0, had been encountered):

    If a 0 arrives, it remains in

    that state. Remember thata 0 had already been

    encountered. The arrival

    of another 0 does not

    improve the search.

    However, this newlyarrived 0 can now be the

    new first symbol of the

    substring (the previous 0

    may be forgotten).

    If it receives a 1, it moves

    to state q2 since the two

    symbols of the substring

    have already been

    encountered.

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

    11/29

    Theory of Computation (With Automata Theory)

    * Property of STI 

    Page 11 of 29

    Designing Deterministic Finite Automata

    •  Assume that the DFA is in

    state   q2 (the first two symbols,

    01, had been encountered):

    If a 0 arrives, it goes to

    state q1. The arrival of this

    0 makes the substring 010.

    The previous 01 would

    have to be forgotten and

    this newly arrived 0 cannow be the new first

    symbol of the substring.

    If it receives a 1, it movesto state q3 since the three

    symbols of the substring

    (011) had already been

    encountered.

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

    12/29

    Theory of Computation (With Automata Theory)

    * Property of STI 

    Page 12 of 29

    Designing Deterministic Finite Automata

    •  Assume that the DFA is instate  q3 (the substring 011 had

    been encountered):

    The DFA will not change

    state upon the arrival ofsucceeding 0s and 1s

    since the desired substring

    had already been detected.

    • The state diagram for the

    required DFA:

    0

    01

    q 0   q 3q 1   q 21   1

    0

    0, 1

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

    13/29

    Theory of Computation (With Automata Theory)

    * Property of STI 

    Page 13 of 29

    Designing Deterministic Finite Automata

    Case Study 3: Design a DFA that

    accepts all strings over the alphabet  = {0, 1} that starts and ends with

    01.

    • In this example, the DFA to be

    designed must be able todetect if the input string has a

    prefix which is 01 and a suffix

    which is also 01.

    • The DFA to be designed

    should remember the following

    things:

    1. The state where it had not

    yet encountered the first

    symbol (0) of the prefix 01.

    2. The state where it had

    encountered the first

    symbol (0) of the prefix 01.

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

    14/29

    Theory of Computation (With Automata Theory)

    * Property of STI 

    Page 14 of 29

    Designing Deterministic Finite Automata

    3. The state where it hadencountered the two

    symbols (01) of the prefix

    01.

    4. The state where it had

    already encountered theprefix 01 and it had

    encountered the first

    symbol (0) of the suffix 01.

    5. The state where it had

    encountered the prefix 01and the two symbols (01)

    of the suffix 01.

    6. The state where the input

    string did not start with the

    prefix 01. If the string didnot start with a 01, then it

    would be useless for the

    DFA to determine if it

    would end with a 01.

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

    15/29

    Theory of Computation (With Automata Theory)

    * Property of STI 

    Page 15 of 29

    Designing Deterministic Finite Automata

    • 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.

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

    16/29

    Theory of Computation (With Automata Theory)

    * Property of STI 

    Page 16 of 29

    Designing Deterministic Finite Automata

    •  Assume that the DFA is in

    state  q0 (the first symbol of the

    prefix 01 had not arrived):

    If a 0 arrives, it moves to

    state q1 since it had

    encountered the first

    symbol of the prefix.

    If a 1 arrives, it goes to

    state q5 since it is nowimpossible for the input

    string to start with the

    prefix 01.

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

    17/29

    Theory of Computation (With Automata Theory)

    * Property of STI 

    Page 17 of 29

    Designing Deterministic Finite Automata

    •  Assume that the DFA is in

    state  q1 (the first symbol of the

    prefix 01 had arrived):

    If a 0 arrives, it moves to

    state q5 since it is now

    impossible for the input

    string to start with theprefix 01 since the first two

    symbols received are 00.

    If a 1 arrives, it goes tostate q2 since the two

    symbols of the prefix 01

    had been received.

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

    18/29

    Theory of Computation (With Automata Theory)

    * Property of STI 

    Page 18 of 29

    Designing Deterministic Finite Automata

    •  Assume that the DFA is in

    state   q2 (the two symbols of  

    the prefix 01 had arrived):

    If a 0 arrives, it moves to

    state q3 since this newly-

    arrived zero can potentiallybe the start of the suffix 01.

    If a 1 arrives, it stays in this

    state since the suffix 01 isstill to be detected.

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

    19/29

    Theory of Computation (With Automata Theory)

    * Property of STI 

    Page 19 of 29

    Designing Deterministic Finite Automata

    •  Assume that the DFA is in

    state   q3 (the two symbols of  

    the prefix 01 had been

    received and the first symbol

    of the suffix 01 had arrived):

    If a 0 arrives, it stays in q3.

    By this time, the last two

    symbols received are 00.

    The newly-arrived 0 cannow be the first symbol of

    the suffix 01 while the

    previous zero may be

    forgotten.

    If a 1 arrives, it moves to

    state q4 since the prefix 01

    had been received.

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

    20/29

    Theory of Computation (With Automata Theory)

    * Property of STI 

    Page 20 of 29

    Designing Deterministic Finite Automata

    •  Assume that the DFA is instate   q4 (the two symbols of  

    the prefix 01 had been

    received and the two symbols

    of the suffix 01 had arrived):

    If a 0 arrives, the previous

    two symbols (01) is not the

    true suffix of the input

    string. This newly arrived

    0 can become the firstsymbol of the potentially

    true suffix. The DFA

    therefore moves to state

    q3.

    If a 1 arrives, the previous

    two symbols (01) is not the

    true suffix of the input

    string. The DFA goes to

    state q2.

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

    21/29

    Theory of Computation (With Automata Theory)

    * Property of STI 

    Page 21 of 29

    Designing Deterministic Finite Automata

    •  Assume that the DFA is in

    state   q5 (the input string didnot start with the prefix 01):

    The DFA will not change

    state upon the arrival of  

    succeeding 0s and 1ssince the string did not start

    with the prefix 01. It is now

    useless for the DFA to track

    the arriving symbols

    because the string will notbe accepted anyway.

    • The state diagram for the

    required DFA:

    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 2

    22/29

    Theory of Computation (With Automata Theory)

    * Property of STI 

    Page 22 of 29

    Designing Deterministic Finite Automata

     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 allstrings 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.

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

    23/29

    Theory of Computation (With Automata Theory)

    * Property of STI 

    Page 23 of 29

    Designing Deterministic Finite Automata

    • For Exercise 1

    Design a DFA that accepts all

    strings over the alphabet      =

    {0, 1} with no more than 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 2

    24/29

    Theory of Computation (With Automata Theory)

    * Property of STI 

    Page 24 of 29

    Designing Deterministic Finite Automata

    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

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

    25/29

    Theory of Computation (With Automata Theory)

    * Property of STI 

    Page 25 of 29

    Designing Deterministic Finite Automata

    • For Exercise 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.

    The DFA will have 4 states:

    1. q0 = the number of 0s is

    even and the number of 1sis 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 2

    26/29

    Theory of Computation (With Automata Theory)

    * Property of STI 

    Page 26 of 29

    Designing Deterministic Finite Automata

    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

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

    27/29

    Theory of Computation (With Automata Theory)

    * Property of STI 

    Page 27 of 29

    Designing Deterministic Finite Automata

    • For Exercise 3

    Design a DFA that accepts all

    strings over the alphabet      =

    {0, 1} that starts or ends with a

    01.

    This is similar to the design

    example except that instead of 

    a string starting and ending

    with the substring 01, the DFA

    should accept strings startingOR ending with the substring

    01.

    One way of processing the

    strings is to determine first if  the prefix 01 is present. If it is,

    then the DFA stops at an

    accept state, regardless if  

    there are still incoming input

    strings.

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

    28/29

    Theory of Computation (With Automata Theory)

    * Property of STI 

    Page 28 of 29

    Designing Deterministic Finite Automata

    If the string did not start with

    the prefix 01, then the DFAproceeds to determine 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 the suffix 01 had

    arrived.

    6. q5 = the prefix 01 was not

    encountered and the two

    symbols of the suffix 01

    had arrived.

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

    29/29

    Theory of Computation (With Automata Theory)

    • 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