Anda di halaman 1dari 47

SSK5204

CHAPTER2: FINITE AUTOMATA


1

Dr. Nor Fazlida Mohd Sani


Dept. of Computer Science
Fac. of Computer Science and Information
Technology, UPM.

Finite Automata

EXAMPLE OF A FINITE AUTOMATON


L, R
odd

even
L, R

There

are states even and odd, even is the start

state
The automaton takes inputs from alphabet {L, R}
The state even is an accepting state
There are transitions saying what to do for every
state and every alphabet symbol
3

DETERMINISTIC FINITE AUTOMATA


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

Q is a finite set of states


S is an alphabet
d: Q S Q is a transition function
q0 Q is 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

EXAMPLE
q0

1
1

q1

alphabet S = {0, 1}
states Q = {q0, q1, q2}
initial state q0
accepting states F = {q0, q1}

0,1
0

q2

table of
transition function d:
inputs

states

q0
q1
q2

0
q0
q2
q2

1
q1
q1
q2
5

LANGUAGE OF A DFA
The language of a DFA (Q, S, d, q0, F) is the set of
all strings over S that, starting from q0 and
following the transitions as the string is read left
to right, will reach some accepting state.
L, R

M:

odd

even
L, R

Language of M is {x {L, R}*: x has even length}

EXAMPLES
S = {a, b}

b
q0

b
b

0
q0

q1

q1

a
a

q3
a

q1

q2

b
b

q4

1
1

S = {a, b}

q0

0, 1
0

q2

S = {0, 1}

What are the languages of these automata?

EXAMPLES
a DFA over alphabet {0, 1} that accepts
all strings with at most three 1s

Construct

EXAMPLES
a DFA over alphabet {0, 1} that accepts
all strings with at most three 1s

Construct

Answer
0

0
q0

q1

0
1

q2

0, 1

0
1

q3

q4+

EXAMPLES
Construct

a DFA that accepts the language


L = {010, 1}

( S = {0, 1} )

10

EXAMPLES
Construct

a DFA that accepts the language


( S = {0, 1} )

L = {010, 1}
Answer

q0

qe

q01

q010

1
0, 1

q1

0, 1

qdie

0, 1
11

EXAMPLES
a DFA over alphabet {0, 1} that
accepts all strings that end in 01

Construct

12

EXAMPLES
a DFA over alphabet {0, 1} that
accepts all strings that end in 01

Construct

The DFA must remember the last 2


bits of the string it is reading

Hint:

13

EXAMPLES
a DFA over alphabet {0, 1} that accepts
all strings that end in 01
0
Answer:
0
q00
Construct

q0
0

q01

qe

0
1

0
q1

0
1

q10
0
q11
1

14

EXAMPLES
a DFA over alphabet {0, 1} that accepts
all strings that end in 101
Sketch of answer:
0
Construct

q0

qe
1

0
q1

q00

q01

q10
q11

1
q001

q000

q101

q111

1
15

Nondeterminism

16

EXAMPLE
a DFA over alphabet {0, 1} that
accepts those strings that end in 101
Sketch of answer:
0
Construct

q0

qe
1

0
q1

q00

q01

q10
q11

1
q001

q000

q101

q111

1
17

WOULD BE EASIER IF
Suppose

we could guess when 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
3 symbols left

1
0

0
1
qdie

This is not a DFA!


18

NONDETERMINISM
Nondeterminism

is the ability to make guesses,


which we can later verify
How a nondeterministic automaton for strings that
end in 101 works:
1. Guess if you are approaching end of input
2. If guess is yes, look for 101 and accept if you see it
3. If guess is no, read one more symbol and go to step
1
19

NONDETERMINISTIC FINITE AUTOMATON


This

is a kind of automaton that allows you to


make guesses
0, 1
q0

q1

q2

q3

Each

state can have zero, one, or more outgoing


transitions labeled by the same symbol
20

THE ABILITY TO MAKE CHOICES


0, 1
q0

q1

q2

q3

q0 has two transitions labeled 1


Upon reading 1, we have the choice of staying in
q0 or moving to q1
State

21

THE ABILITY TO MAKE CHOICES


0, 1
q0

q1

q2

q3

q1 has no transition labeled 1


Upon reading 1 in q1, we die; upon reading 0, we
continue to q2
State

22

THE ABILITY TO MAKE CHOICES


0, 1
q0

q1

q2

q3

State q3 has no transition going out


Upon reading 0 or 1 in q3, we die

23

MEANING OF NFA
Guess if you are 3
symbols away from end of
input

0, 1
q0

q1

If so, guess you


will see the pattern
101

q2

q3

Check that you are at


the end of input
24

HOW TO RUN AN NFA


0,1
q0

q1

q2

q3

input: 0 1 1 0 1

The NFA can have several active states


at the same time
25

EXAMPLE

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


those strings that contain the pattern 001

26

EXAMPLE

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


strings that contain the pattern 001 somewhere

Answer

0, 1
q0

0, 1
0

q1

q2

q3

27

DEFINITION

A nondeterministic finite automaton (NFA) is a


5-tuple (Q, S, d, q0, F) where

Q is a finite set of states


S is an alphabet
d: Q (S{e}) subsets of Q is a transition function
q0 Q is the initial state
F Q is a set of accepting states (or final states).

Differences from DFA:


transition functiond can go into several states
It allows e-transitions

28

LANGUAGE OF AN NFA
The language of an NFA is the set of all
strings for which there is some path 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
q0

e, 1
0

q1

q2

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

EXAMPLE
q0

e, 1
0

q1

q2

table of
transition function d:
alphabet S = {0, 1}
states Q = {q0, q1, q2}
initial state q0
accepting states F = {q2}
states

q0
q1
q2

inputs
1

{q1}

{q1}

{q2}

{q0, q1}

30

EXAMPLES
e

q0

e, 1

q1

q2

0
2

00

q0

e, 1

q1

q2

q2

q2

0
2

or

q0

e, 1

q1
0

or

q0

e, 1
2

q1
4

31

EXAMPLES
001

q0

,1

101

q0

11

q0

e, 1

q0

q2

0
1

q1

q2

0
1

q1
0

e, 1

q1
0

e, 1

q2

STOP

0
1

q1
0

q2
STOP

32

EXAMPLE OF e-TRANSITIONS

Construct an NFA that accepts all strings with an even


number of 0s or an odd number of 1s
1

e
q0

r1

0
s0

e-transitions

r0

1
even number of 0s

0
1
1

s1

odd number of 1s

can be taken for free (without reading input)

33

NFA to DFA conversion

34

NFAS ARE AS POWERFUL AS DFAS


Obviously, an NFA can do everything a DFA can do
How about the other way?

YES
If L is the language of some NFA, then
it is also the language of some DFA.
35

CONVERTING NFAS INTO DFAS

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

NFA TO DFA CONVERSION INTUITION


0, 1
NFA:

q0

q0

q2

0
DFA:

q1

q0 or q1

q0 or q2

37

NFA TO DFA CONVERSION INTUITION


0, 1
NFA:

q0

q0

q2

0
DFA:

q1

{q0, q1}

{q0, q2}

38

GENERAL METHOD
states

NFA

DFA

q0, q1, , qn

{q0}, {q1}, {q0,q1}, , {q0,,qn}


one for each subset of states in the NFA

initial state q0
transitions

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

accepting
states

FQ

F = {S: S contains some state in F}


39

CONVERTING VIA THE GENERAL METHOD


0, 1

NFA:

q0

q1

q2

0
DFA:
{q0}
0, 1

{q0, q1}
0

{q0, q2}

{q1}

1
0

{q0, q1, q2}

0
1

{q2}

{q1, q2}
40

CONVERTING VIA THE GENERAL METHOD


After eliminating the dead states and
transitions, we end up with the same picture
0
{q0}
0, 1

{q0, q1}
0

{q0, q2}

{q1}

1
0

{q0, q1, q2}

0
1

{q2}

{q1, q2}
41

WHY THE METHOD WORKS


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

At the end, the DFA accepts when it is in a state that


contains some accepting state of NFA

So the DFA accepts only when the NFA accepts too

42

CONVERTING NFAS INTO DFAS

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

ELIMINATING e-TRANSITIONS
eNFA:

e, 1

q0

q1

q2

0
Transition table of corresponding NFA:
inputs

states

q0

{q0, q1, q2}

{q1, q2}

q1

{q0, q1, q2}

q2

Accepting states of NFA:

q0, q1, q2
44

ELIMINATING e-TRANSITIONS
NFA:

e, 1

q0

q1

q2

q2

NFA without es:

q0

0, 1

q1

0
0, 1
45

ELIMINATING e-TRANSITIONS: GENERAL


METHOD

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


q
replace every path like
e 4
qi

q5

q2

q0

qj

qi
e

qi

q5

qj

qi

For every accept state qf, make accepting all states


connected to it via es:
q9

q7

e
e

q3

qf

46

ELIMINATING e-TRANSITIONS
NFA:

e, 1

q0

q1

q2

q2

NFA without es:

q0

0, 1

q1

0
0, 1
47

Anda mungkin juga menyukai