nfa

51
Nondeterministic Finite Automata Dr. Iftikhar Ahmad ([email protected])

Upload: alisaqi

Post on 24-Dec-2015

6 views

Category:

Documents


2 download

DESCRIPTION

Automata Theory

TRANSCRIPT

Page 1: NFA

Nondeterministic Finite Automata

Dr. Iftikhar Ahmad ([email protected])

Page 2: NFA

Overview ¡ Non-Deterministic Finite Automata

¡ Converting NFA to DFA

¡  ε-NFA

1

Page 3: NFA

Non-Deterministic Finite Automata

2

Page 4: NFA

Recall DFA ¡  For every string S, there is a unique path from

initial state.

3

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

S

Page 5: NFA

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.

4

a a

a

Page 6: NFA

Nondeterminism ¡  Start in one start state.

¡ Accept if any sequence of choices leads to a final state.

¡  Intuitively: the NFA always “guesses right.”

5

Page 7: NFA

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.

6

1 2

5

7 9

3

4

8

6

Page 8: NFA

Example: Chessboard

7

1 2

5

7 9

3

4

8

6

1 r b b

4 2 1

5 3

7

5 1 3

9 7

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

Accept, since final state reached

Page 9: NFA

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)

8

Page 10: NFA

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

position from the right }

9

§  Remember, it always guesses correctly.

Page 11: NFA

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.

10

Page 12: NFA

Example: Language of an NFA

¡  For our chessboard NFA we saw that rbb is accepted.

¡  If the input consists of only bʼ’s, the set of accessible states alternates between {5} and {1,3,7,9}

¡ Only even-length, nonempty strings of bʼ’s are accepted.

¡ What about strings with at least one r?

11

1 2

5

7 9

3

4

8

6

Page 13: NFA

Converting NFA to DFA

12

Page 14: NFA

Equivalence of DFAʼ’s, NFAʼ’s ¡ 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.

13

Page 15: NFA

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, NFAʼ’s accept exactly the regular languages.

14

Page 16: NFA

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.

15

Page 17: NFA

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.

16

Page 18: NFA

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: Weʼ’ll construct the DFA equivalent of our “chessboard” NFA.

17

Page 19: NFA

Example: Subset Construction

18

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

r b {1} {2,4} {5}

{2,4} {5}

Alert: What we’re doing here is the lazy form of DFA construction, where we only construct a state if we are forced to.

Page 20: NFA

Example: Subset Construction

19

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

r b {1}

{2,4,6,8} {5}

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

{1,3,5,7}

{2,4} {5}

Page 21: NFA

Example: Subset Construction

20

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

r b {1}

* {1,3,7,9}

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

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

{1,3,5,7}

{2,4} {5}

Page 22: NFA

Example: Subset Construction

21

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

r b {1}

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

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

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

{1,3,5,7}

{2,4} {5}

Page 23: NFA

Example: Subset Construction

22

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

r b {1}

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

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

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

{1,3,5,7}

{2,4} {5}

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

Page 24: NFA

Example: Subset Construction

23

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

r b {1}

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

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

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

{1,3,5,7}

{2,4} {5}

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

Page 25: NFA

Example: Subset Construction

24

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

r b {1}

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

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

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

{1,3,5,7}

{2,4} {5}

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

Page 26: NFA

Another Example Fig1.  NFA  

 

               

 

1

3

2

4

5

a b

b

b

a,b

a,b

a a

a

25

Page 27: NFA

Subset Construction Method                

                                 

 

       Step1  Construct a transition table showing all reachable states for every state for every input signal.

Fig1.  NFA  

 

1

3

2

4

5

a b

b

b

a,b

a,b

a a

a

26

Page 28: NFA

Subset Construction Method Fig1.  NFA  

 

                           Fig2.  Transi3on  table    

 

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

2 {3} {5}

3 ∅ {2}

4 {5} {4}

5 ∅ ∅

1

3

2

4

5

a b

b

b

a,b

a,b

a a

a

27

Page 29: NFA

Subset Construction Method Fig1.  NFA  

 

                           Fig2.  Transi3on  table    

 

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

2 {3} {5}

3 ∅ {2}

4 {5} {4}

5 ∅ ∅

Transition from state q with input a

Transition from state q with input b

Starts here

1

3

2

4

5

a b

b

b

a,b

a,b

a a

a

28

Page 30: NFA

Fig2.  Transi3on  table  

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

2 {3} {5}

3 ∅ {2}

4 {5} {4}

5 ∅ ∅

Subset Construction Method

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.

29

Page 31: NFA

Fig2.  Transi3on  table  

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

2 {3} {5}

3 ∅ {2}

4 {5} {4}

5 ∅ ∅

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

Starts with Initial state

Fig3. Subset Construction table

30

Page 32: NFA

Fig2.  Transi3on  table  

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

2 {3} {5}

3 ∅ {2}

4 {5} {4}

5 ∅ ∅

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

{1,2,3,4,5}

{4,5}

Starts with Initial state

Fig3. Subset Construction table 31

Page 33: NFA

Fig2.  Transi3on  table  

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

2 {3} {5}

3 ∅ {2}

4 {5} {4}

5 ∅ ∅

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

{1,2,3,4,5}

{4,5}

Starts with Initial state

Fig3. Subset Construction table

Step3

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

32

Page 34: NFA

Fig2.  Transi3on  table  

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

2 {3} {5}

3 ∅ {2}

4 {5} {4}

5 ∅ ∅

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

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

{2,4,5}

Fig3. Subset Construction table 33

Page 35: NFA

Fig2.  Transi3on  table  

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

2 {3} {5}

3 ∅ {2}

4 {5} {4}

5 ∅ ∅

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

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

{2,4,5}

5

4

Fig3. Subset Construction table 34

Page 36: NFA

Fig2.  Transi3on  table  

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

2 {3} {5}

3 ∅ {2}

4 {5} {4}

5 ∅ ∅

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

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

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

5

4 {3,5}

Fig3. Subset Construction table 35

Page 37: NFA

Fig2.  Transi3on  table  

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

2 {3} {5}

3 ∅ {2}

4 {5} {4}

5 ∅ ∅

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

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

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

5 ∅ ∅ 4

{3,5}

Fig3. Subset Construction table 36

Page 38: NFA

Fig2.  Transi3on  table  

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

2 {3} {5}

3 ∅ {2}

4 {5} {4}

5 ∅ ∅

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

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

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

5 ∅ ∅ 4 5 4

{3,5}

We already got 4 and 5. So we don’t add them again.

Fig3. Subset Construction table 37

Page 39: NFA

Fig2.  Transi3on  table  

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

2 {3} {5}

3 ∅ {2}

4 {5} {4}

5 ∅ ∅

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

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

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

5 ∅ ∅ 4 5 4

{3,5} ∅ 2

∅ 2

Fig3. Subset Construction table 38

Page 40: NFA

Fig2.  Transi3on  table  

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

2 {3} {5}

3 ∅ {2}

4 {5} {4}

5 ∅ ∅

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

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

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

5 ∅ ∅ 4 5 4

{3,5} ∅ 2

∅ ∅ ∅ 2

Fig3. Subset Construction table 39

Page 41: NFA

Fig2.  Transi3on  table  

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

2 {3} {5}

3 ∅ {2}

4 {5} {4}

5 ∅ ∅

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

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

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

5 ∅ ∅ 4 5 4

{3,5} ∅ 2

∅ ∅ ∅ 2 3 5 3

Fig3. Subset Construction table 40

Page 42: NFA

Fig2.  Transi3on  table  

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

2 {3} {5}

3 ∅ {2}

4 {5} {4}

5 ∅ ∅

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

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

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

5 ∅ ∅ 4 5 4

{3,5} ∅ 2

∅ ∅ ∅ 2 3 5 3 ∅ 2

Fig3. Subset Construction table

Stops here as there are no more reachable states

41

Page 43: NFA

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

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

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

5 ∅ ∅ 4 5 4

{3,5} ∅ 2

∅ ∅ ∅ 2 3 5 3 ∅ 2

1

45

12345 245

35

5

4

∅ 3

2

a

a

a

a

a

a

a

a

b

b

b

b

b b

b

b

a,b

a,b

Fig3. Subset Construction table Fig4. Resulting FA after applying Subset Construction to fig1

42

Page 44: NFA

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.

43

Page 45: NFA

NFAʼ’s With ε-Transitions

44

Page 46: NFA

NFAʼ’s 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.

45

Page 47: NFA

Example: ε-NFA

46

C

E F

A

B D 1 1 1

0 0

0

ε

ε ε

0 1 ε A {E} {B} ∅ B ∅ {C} {D} C ∅ {D} ∅ D ∅ ∅ ∅ E {F} ∅ {B, C} F {D} ∅ ∅

*

Page 48: NFA

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

¡ Closure of a set of states = union of the closure of each state.

47

C

E F

A

B D 1 1 1

0 0

0

ε

ε ε

Page 49: NFA

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.

48

Page 50: NFA

Summary ¡ DFAʼ’s, NFAʼ’s, and ε–NFAʼ’s 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!

49

Page 51: NFA

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]