Sie sind auf Seite 1von 51

Nondeterministic Finite

Automata
Dr. Iftikhar Ahmad
(ia@giki.edu.pk)

Overview
Non-Deterministic Finite Automata
Converting NFA to DFA
-NFA

Non-Deterministic
Finite Automata

Recall DFA
For every string S, there is a unique path from
initial state.

S is accepted if and only if this path ends at a final state.

Nondeterminism
A nondeterministic finite automaton has the ability
to be in several states at once.
Transitions from a state on an input symbol can be
to any set of states.

Nondeterminism
Start in one start state.
Accept if any sequence of choices leads to a
final state.
Intuitively: the NFA always guesses right.

Example: Moves on a
Chessboard
States = squares.
Inputs = {r , b}
r : move to an adjacent red square.
b: move to an adjacent black square.

Start state, final state are in opposite corners.

Example: Chessboard
1

r
1

b
2
4

b
1
3
5
7

5
1
3
7
9

1
2
3
4
5
6
7
8
* 9

r
2,4
4,6
2,6
2,8
2,4,6,8
2,8
4,8
4,6
6,8

b
5
1,3,5
5
1,5,7
1,3,7,9
3,5,9
5
5,7,9
5

Accept, since final state reached

Formal NFA
An NFA can be represented by 5 tuples (Q, , ,
q0, F);
A finite set of states, typically Q.
An input alphabet, typically .
A transition function, typically .
A start state in Q, typically q0.
A set of final states F Q.

: Q x { U {} } P(Q)

Example : NFA
A = { w | w \in {0,1}* and w has a 1 from third
position from the right }

Remember, it always guesses correctly.

10

Language of an NFA
A string w is accepted by an NFA if (q0, w)
contains at least one final state.
The language of the NFA is the set of strings it
accepts.

11

Example: Language of
an NFA
For our chessboard NFA we saw that rbb is
accepted.
If the input consists of only bs, the set of
accessible states alternates between {5} and
{1,3,7,9}
Only even-length, nonempty strings of bs are
accepted.
What about strings with at least one r?

12

Converting NFA to DFA

13

Equivalence of DFAs, NFAs


A DFA can be turned into an NFA that accepts the same
language.
If D(q, a) = p, let the NFA have

N(q, a) = {p}.

Then the NFA is always in a set containing exactly one state


the state the DFA is in after reading the same input.

14

Equivalence
Surprisingly, for any NFA there is a DFA that accepts the
same language.
Proof is the subset construction.
The number of states of the DFA can be exponential in the
number of states of the NFA.
Thus, NFAs accept exactly the regular languages.

15

Subset Construction
Given an NFA with states Q, inputs , transition function N,
start state q0, and final states F, construct equivalent DFA
with:
States 2Q (Set of subsets of Q).
Inputs .
Start state {q0}.
Final states = all those with a member of F.

16

Critical Point
The DFA states have names that are sets of NFA states.
But as a DFA state, an expression like {p,q} must be read as
a single symbol, not as a set.

17

Subset Construction
The transition function D is defined by:
D({q1,,qk}, a) is the union over all i = 1,,k of
N(qi, a).
Example: Well construct the DFA equivalent of
our chessboard NFA.

18

Example: Subset Construction


1
2
3
4
5
6
7
8
* 9

r
2,4
4,6
2,6
2,8
2,4,6,8
2,8
4,8
4,6
6,8

b
5
1,3,5
5
1,5,7
1,3,7,9
3,5,9
5
5,7,9
5

{1}
{2,4}
{5}

r
{2,4}

b
{5}

Alert: What were doing here is


the lazy form of DFA construction,
where we only construct a state
if we are forced to.

19

Example: Subset Construction


1
2
3
4
5
6
7
8
* 9

r
2,4
4,6
2,6
2,8
2,4,6,8
2,8
4,8
4,6
6,8

b
5
1,3,5
5
1,5,7
1,3,7,9
3,5,9
5
5,7,9
5

{1}
{2,4}
{5}
{2,4,6,8}
{1,3,5,7}

r
b
{2,4}
{5}
{2,4,6,8} {1,3,5,7}

20

Example: Subset Construction


1
2
3
4
5
6
7
8
* 9

r
2,4
4,6
2,6
2,8
2,4,6,8
2,8
4,8
4,6
6,8

b
5
1,3,5
5
1,5,7
1,3,7,9
3,5,9
5
5,7,9
5

{1}
{2,4}
{5}
{2,4,6,8}
{1,3,5,7}
* {1,3,7,9}

r
b
{2,4}
{5}
{2,4,6,8} {1,3,5,7}
{2,4,6,8} {1,3,7,9}

21

Example: Subset Construction


1
2
3
4
5
6
7
8
* 9

r
2,4
4,6
2,6
2,8
2,4,6,8
2,8
4,8
4,6
6,8

b
5
1,3,5
5
1,5,7
1,3,7,9
3,5,9
5
5,7,9
5

r
b
{1}
{2,4}
{5}
{2,4}
{2,4,6,8} {1,3,5,7}
{5}
{2,4,6,8} {1,3,7,9}
{2,4,6,8} {2,4,6,8} {1,3,5,7,9}
{1,3,5,7}
* {1,3,7,9}
* {1,3,5,7,9}

22

Example: Subset Construction


1
2
3
4
5
6
7
8
* 9

r
2,4
4,6
2,6
2,8
2,4,6,8
2,8
4,8
4,6
6,8

b
5
1,3,5
5
1,5,7
1,3,7,9
3,5,9
5
5,7,9
5

r
{1}
{2,4}
{2,4}
{2,4,6,8}
{5}
{2,4,6,8}
{2,4,6,8} {2,4,6,8}
{1,3,5,7} {2,4,6,8}
* {1,3,7,9}
* {1,3,5,7,9}

b
{5}
{1,3,5,7}
{1,3,7,9}
{1,3,5,7,9}
{1,3,5,7,9}

23

Example: Subset Construction


1
2
3
4
5
6
7
8
* 9

r
2,4
4,6
2,6
2,8
2,4,6,8
2,8
4,8
4,6
6,8

b
5
1,3,5
5
1,5,7
1,3,7,9
3,5,9
5
5,7,9
5

r
{1}
{2,4}
{2,4}
{2,4,6,8}
{5}
{2,4,6,8}
{2,4,6,8} {2,4,6,8}
{1,3,5,7} {2,4,6,8}
* {1,3,7,9} {2,4,6,8}
* {1,3,5,7,9}

b
{5}
{1,3,5,7}
{1,3,7,9}
{1,3,5,7,9}
{1,3,5,7,9}
{5}

24

Example: Subset Construction


1
2
3
4
5
6
7
8
* 9

r
2,4
4,6
2,6
2,8
2,4,6,8
2,8
4,8
4,6
6,8

b
5
1,3,5
5
1,5,7
1,3,7,9
3,5,9
5
5,7,9
5

{1}
{2,4}
{5}
{2,4,6,8}
{1,3,5,7}
* {1,3,7,9}
* {1,3,5,7,9}

r
{2,4}
{2,4,6,8}
{2,4,6,8}
{2,4,6,8}
{2,4,6,8}
{2,4,6,8}
{2,4,6,8}

b
{5}
{1,3,5,7}
{1,3,7,9}
{1,3,5,7,9}
{1,3,5,7,9}
{5}
{1,3,5,7,9}

25

Another Example

Fig1. NFA

3
a

b
a

1
a,b

2
a,b
4
b

b
5
a

26

Subset Construction Method


Fig1. NFA

3
a

b
a

1
a,b

2
a,b
4
b

Step1

b
5
a

Construct a transition table


showing all reachable
states for every state for
every input signal.

27

Subset Construction Method


Fig2. Transi3on table

Fig1. NFA

3
a

b
a

1
a,b

2
a,b
4
b

b
5
a

(q,a)

(q,b)

{1,2,3,4,5}

{4,5}

{3}

{5}

{2}

{5}

{4}

28

Subset Construction Method


Transition
q Transi3on
Transition
from
state
Fig2.
table from
state q
with input a
with input b

Fig1. NFA

3
a

b
a

1
a,b

2
a,b
4
b

Starts
here
5

(q,a)

(q,b)

{1,2,3,4,5}

{4,5}

{3}

{5}

{2}

{5}

{4}

Subset Construction Method

29

Fig2. Transi3on table

(q,a)

(q,b)

{1,2,3,4,5}

{4,5}

{3}

{5}

{2}

{5}

{4}

Step2
The set of states resulting
from every transition
function constitutes a new
state. Calculate all
reachable states for every
such state for every input
signal.

30

Fig3. Subset Construction table

Fig2. Transi3on table

(q,a)

(q,b)

{1,2,3,4,5}

{4,5}

{3}

{5}

{2}

{5}

{4}

Starts with
Initial state

(q,a)

{1,2,3,4,5}

(q,b)
{4,5}

31

Fig2. Transi3on table

(q,a)

(q,b)

{1,2,3,4,5}

{4,5}

{3}

{5}

{2}

{5}

{4}

Starts with
Initial state

Fig3. Subset Construction table


q
(q,a) (q,b)
1
{1,2,3,4,5}

{4,5}

{1,2,3,4,5}

{4,5}

32

Fig2. Transi3on table

(q,a)

(q,b)

{1,2,3,4,5}

{4,5}

{3}

{5}

{2}

{5}

{4}

Starts with
Initial state

Step3
Repeat this process(step2)
until no more new states
are reachable.

Fig3. Subset Construction table


q
(q,a) (q,b)
1
{1,2,3,4,5}

{4,5}

{1,2,3,4,5}

{4,5}

33

Fig2. Transi3on table

(q,a)

{1,2,3,4,5}

(q,b)
{4,5}

{3}

{5}

{2}

{5}

{4}

Fig3. Subset Construction table


q
(q,a) (q,b)
1

{1,2,3,4,5}

{1,2,3,4,5} {1,2,3,4,5}

{4,5}
{2,4,5}

{4,5}
{2,4,5}

34

Fig2. Transi3on table

(q,a)

{1,2,3,4,5}

(q,b)
{4,5}

{3}

{5}

{2}

{5}

{4}

Fig3. Subset Construction table


q
(q,a) (q,b)
1

{1,2,3,4,5}

{1,2,3,4,5} {1,2,3,4,5}

{4,5}
{2,4,5}

5
4

{4,5}
{2,4,5}

35

Fig2. Transi3on table

(q,a)

{1,2,3,4,5}

(q,b)
{4,5}

{3}

{5}

{2}

{5}

{4}

Fig3. Subset Construction table


q
(q,a) (q,b)
1

{1,2,3,4,5}

{1,2,3,4,5} {1,2,3,4,5}

{4,5}
{2,4,5}

{4,5}

{2,4,5}

{3,5}

{4,5}

5
4
{3,5}

36

Fig2. Transi3on table

(q,a)

{1,2,3,4,5}

(q,b)
{4,5}

{3}

{5}

{2}

{5}

Fig3. Subset Construction table


q
(q,a) (q,b)
1

{1,2,3,4,5}

{1,2,3,4,5} {1,2,3,4,5}

{4,5}
{2,4,5}

{4,5}

{2,4,5}

{3,5}

{4,5}

{4}

4
{3,5}

37

Fig2. Transi3on table

(q,a)

{1,2,3,4,5}

(q,b)
{4,5}

{3}

{5}

{2}

{5}

{4}

Fig3. Subset Construction table


q
(q,a) (q,b)
1

{1,2,3,4,5} {1,2,3,4,5}

{4,5}
{2,4,5}

{4,5}

{2,4,5}

{3,5}

{4,5}

{3,5}

We already got 4 and 5.


So we dont add them
again.

{1,2,3,4,5}

38

Fig2. Transi3on table

(q,a)

{1,2,3,4,5}

(q,b)
{4,5}

{3}

{5}

{2}

{5}

{4}

Fig3. Subset Construction table


q
(q,a) (q,b)
1

{1,2,3,4,5}

{1,2,3,4,5} {1,2,3,4,5}

{4,5}
{2,4,5}

{4,5}

{2,4,5}

{3,5}

{4,5}

{3,5}

39

Fig3. Subset Construction table

Fig2. Transi3on table

(q,a)

{1,2,3,4,5}

(q,b)

(q,a)

{4,5}

{1,2,3,4,5}

{3}

{5}

{2}

{5}

{4}

{1,2,3,4,5} {1,2,3,4,5}

(q,b)
{4,5}
{2,4,5}

{4,5}

{2,4,5}

{3,5}

{4,5}

{3,5}

40

Fig3. Subset Construction table

Fig2. Transi3on table

(q,a)

{1,2,3,4,5}

(q,b)

(q,a)

{4,5}

{1,2,3,4,5}

{3}

{5}

{2}

{5}

{4}

{1,2,3,4,5} {1,2,3,4,5}

(q,b)
{4,5}
{2,4,5}

{4,5}

{2,4,5}

{3,5}

{4,5}

{3,5}

41

Fig3. Subset Construction table

Fig2. Transi3on table

(q,a)

{1,2,3,4,5}

(q,b)

(q,a)

{4,5}

{1,2,3,4,5}

{3}

{5}

{2}

{5}

{4}

Stops here as
there are no more
reachable states

{1,2,3,4,5} {1,2,3,4,5}

(q,b)
{4,5}
{2,4,5}

{4,5}

{2,4,5}

{3,5}

{4,5}

{3,5}

Fig4. Resulting FA after


42
applying Subset
Construction to fig1

Fig3. Subset Construction table


q

(q,a)

{1,2,3,4,5}

{1,2,3,4,5} {1,2,3,4,5}

(q,b)
{4,5}

{2,4,5}

{3,5}

{4,5}

4
{3,5}

12345

245

{2,4,5}

{4,5}

4
2

35

a,b

a
a

a,b

b
2

45

43

Recap : Subset Construction


Method
Using Subset construction method to
convert NFA to DFA involves the
following steps:
For every state in the NFA, determine all
reachable states for every input symbol.
The set of reachable states constitute a single
state in the converted DFA (Each state in the DFA
corresponds to a subset of states in the NFA).
Find reachable states for each new DFA state,
until no more new states can be found.

44

NFAs With -Transitions

45

NFAs With -Transitions


We can allow state-to-state transitions on input.
These transitions are done spontaneously, without
looking at the input string.
A convenience at times, but still only regular
languages are accepted.

46

Example: -NFA

A
0

1
0

A
B
C
* D
E
F

0
1

{E} {B}
{C} {D}
{D}

{F} {B, C}
{D}

47

Closure of States
CL(q) = set of states we can reach from state q following
only arcs labeled .

Example: CL(A) = {A};


CL(E) = {B, C, D, E}.

1
A
0

B 1 C 1

Closure of a set of states = union of the closure of each


state.

48

Equivalence of NFA, -NFA


Every NFA is an -NFA.

It just has no transitions on .

Converse requires us to take an -NFA and construct an


NFA that accepts the same language.
We do so by combining transitions with the next
transition on a real input.

49

Summary
DFAs, NFAs, and NFAs all accept exactly the
same set of languages: the regular languages.
The NFA types are easier to design and may have
exponentially fewer states than a DFA.
But only a DFA can be implemented!

Acknowledgement
Material presented in these slides is taken from
www.infolab.stanford.edu/~ullman/
www.cs.gsu.edu/yli/teaching/Spring12/4510/ [Example
Updated and Corrected]

Das könnte Ihnen auch gefallen