Nondeterministic Finite Automata - Research School … Finite Automata COMP2600 — Formal Methods...

39
Nondeterministic Finite Automata COMP2600 — Formal Methods for Software Engineering Katya Lebedeva Australian National University Semester 2, 2016 Slides by Katya Lebedeva. COMP 2600 — Nondeterministic Finite Automata 1

Transcript of Nondeterministic Finite Automata - Research School … Finite Automata COMP2600 — Formal Methods...

Page 1: Nondeterministic Finite Automata - Research School … Finite Automata COMP2600 — Formal Methods for Software Engineering Katya Lebedeva Australian National University Semester 2,

Nondeterministic Finite Automata

COMP2600 — Formal Methods for Software Engineering

Katya Lebedeva

Australian National University

Semester 2, 2016

Slides by Katya Lebedeva.

COMP 2600 — Nondeterministic Finite Automata 1

Page 2: Nondeterministic Finite Automata - Research School … Finite Automata COMP2600 — Formal Methods for Software Engineering Katya Lebedeva Australian National University Semester 2,

For a Deterministic Finite Automaton δ(s,a) is a unique state for all s ∈ S and

for all a ∈ Σ.

For a Nondeterministic Finite Automaton the transition function δ does not

define a unique state, but defines a set of states.

It may

• be the empty set

• be a singleton set (i.e. set with just one element)

• contain more than one element

An NFA has the ability to be in several states at once.

COMP 2600 — Nondeterministic Finite Automata 2

Page 3: Nondeterministic Finite Automata - Research School … Finite Automata COMP2600 — Formal Methods for Software Engineering Katya Lebedeva Australian National University Semester 2,

Nondeterministic Finite State Automata

s0start s1 s2

0,1

1 0,1

COMP 2600 — Nondeterministic Finite Automata 3

Page 4: Nondeterministic Finite Automata - Research School … Finite Automata COMP2600 — Formal Methods for Software Engineering Katya Lebedeva Australian National University Semester 2,

State diagram for 0111

s0start s1 s2

0,1

1 0,1

s0 s0

s0

s0

s0

1

s111

s1 s2 accept!111

s1 s2 stuck1

10

COMP 2600 — Nondeterministic Finite Automata 4

Page 5: Nondeterministic Finite Automata - Research School … Finite Automata COMP2600 — Formal Methods for Software Engineering Katya Lebedeva Australian National University Semester 2,

Informally, an NFA accepts a string if there exists at least one path in the state

diagram that

• starts in the start state

• does not get stuck before the entire string has been read

• ends in the accepting state

The fact that some choices of states using the input symbols of the string lead

to a nonaccepting state, or do not lead to any state at all does not prevent the

string from being accepted by the NFA.

The NFA from the previous slide accepts strings that have 1 as the second

symbol from the end of the string.

COMP 2600 — Nondeterministic Finite Automata 5

Page 6: Nondeterministic Finite Automata - Research School … Finite Automata COMP2600 — Formal Methods for Software Engineering Katya Lebedeva Australian National University Semester 2,

A Nondeterministic Finite State Automaton (DFA) is a 5-tuple

(Σ,S,s0,F,δ)

where

1. Σ is a finite set of input symbols (the alphabet)

2. S is a finite state of states

3. s0 is the start (or initial) state: s0 ∈ S

4. F is a set of final (or accepting) states: F ⊆ S

5. δ is a transition function such that δ : S×Σ→ 2S,

i.e. δ takes a state in S and an input symbol in Σ as arguments andreturns a subset of S.

Notice that the only difference between DFA and NFA is the type of value thatδ returns!

COMP 2600 — Nondeterministic Finite Automata 6

Page 7: Nondeterministic Finite Automata - Research School … Finite Automata COMP2600 — Formal Methods for Software Engineering Katya Lebedeva Australian National University Semester 2,

s0start s1 s2

0,1

1 0,1

The NFA can be specified formally as

({0,1},{s0,s1,s2,},s0,{s2},δ)

where the transition function δ is given by the transition table

0 1

→ s0 {s0} {s0,s1}s1 {s2} {s2}∗s2 /0 /0

COMP 2600 — Nondeterministic Finite Automata 7

Page 8: Nondeterministic Finite Automata - Research School … Finite Automata COMP2600 — Formal Methods for Software Engineering Katya Lebedeva Australian National University Semester 2,

Extended Transition Function

s0 s0

s0

s0

s0

1

s111

s1 s2 accept!111

s1 s2 stuck1

10

COMP 2600 — Nondeterministic Finite Automata 8

Page 9: Nondeterministic Finite Automata - Research School … Finite Automata COMP2600 — Formal Methods for Software Engineering Katya Lebedeva Australian National University Semester 2,

The extended transition function of a NFA is the function δ∗ : S×Σ∗ → 2S

defined as follows:

for all s ∈ S, for all a ∈ Σ, for all α ∈ Σ∗

• δ∗(s,ε) = {s}

• δ∗(s,αa) =⋃

p∈δ∗(s,α)δ(p,a)

I.e. if δ∗(s,α) = {p1, p2, . . . pk} then

δ∗(s,αa) = δ(p1,a)∪δ(p2,a)∪·· ·∪δ(pk,a)

Informally: we first compute δ∗(s,α) and then follow any transition that is

labeled with “a” from any of these states.

COMP 2600 — Nondeterministic Finite Automata 9

Page 10: Nondeterministic Finite Automata - Research School … Finite Automata COMP2600 — Formal Methods for Software Engineering Katya Lebedeva Australian National University Semester 2,

Input: 0111

s0start s1 s2

0,1

1 0,1

1. δ∗(s0,ε) = {s0} the basis rule!

2. δ∗(s0,0) = δ(s0,0) = {s0}

3. δ∗(s0,01) = δ(s0,1) = {s0,s1}

4. δ∗(s0,011) = δ(s0,1)∪δ(s1,1) = {s0,s1}∪{s2}= {s0,s1,s2}

5. δ∗(s0,0111)= δ(s0,1)∪δ(s1,1)∪δ(s2,1)= {s0,s1}∪{s2}∪ /0= {s0,s1,s2}

COMP 2600 — Nondeterministic Finite Automata 10

Page 11: Nondeterministic Finite Automata - Research School … Finite Automata COMP2600 — Formal Methods for Software Engineering Katya Lebedeva Australian National University Semester 2,

Different views of non-determinism

• The NFA always makes the right choice (of a successor state according

to δ) to insure reaching the final state (if possible at all)

• The NFA simultaneously explores multiple paths

• At each nondeterministic choice point the NFA spawns off mutiple copies

of itself

Note: The various path/computations evolve completely independently from

each other (this is different from parallel computations which may synchronise

at a certain point)

COMP 2600 — Nondeterministic Finite Automata 11

Page 12: Nondeterministic Finite Automata - Research School … Finite Automata COMP2600 — Formal Methods for Software Engineering Katya Lebedeva Australian National University Semester 2,

Language accepted by an NFA

If AN = (Σ,S,s0,F,δ) is an NFA, then

L(AN) = {w | δ∗(s0,w)∩F 6= /0}

That is, L(AN) is the set of strings w in Σ∗ such that δ∗(s0,w) contains at least

one accepting state.

COMP 2600 — Nondeterministic Finite Automata 12

Page 13: Nondeterministic Finite Automata - Research School … Finite Automata COMP2600 — Formal Methods for Software Engineering Katya Lebedeva Australian National University Semester 2,

Equivalence of DFAs and NFAs

It may be easier to construct an NFA than a DFA for a given language.

However, every language that can be described by some NFA can also be

described by some DFA (and vice versa)!!!

If an NFA has n states, the equivalent DFA will have at most 2n states.

In most cases, the DFA has about as many states as the equivalent NFA, but

more transitions.

COMP 2600 — Nondeterministic Finite Automata 13

Page 14: Nondeterministic Finite Automata - Research School … Finite Automata COMP2600 — Formal Methods for Software Engineering Katya Lebedeva Australian National University Semester 2,

Proof idea of the equivalence of DFAs and NFAs

We have to prove:

• L(DFA)⊆ L(NFA)

Whenever we have a DFA AD, we can construct a NFA AN such that

L(AD) = L(AN).

This is easy to show! Why?

• L(NFA)⊆ L(DFA)

For every NFA AN there is a DFA AD such that L(AD) = L(AN).

The proof of this direction involves Subset Construction.

COMP 2600 — Nondeterministic Finite Automata 14

Page 15: Nondeterministic Finite Automata - Research School … Finite Automata COMP2600 — Formal Methods for Software Engineering Katya Lebedeva Australian National University Semester 2,

Subset Construction

Intuition

The NFA AN can perform more than one computation on a given input string.

Construct a DFA AD that runs all these different computations simultaneously:

the state that AD is in after having read an initial part of the input string corre-

sponds exactly to the set of all states that AN can reach after having read the

same part of the input string

COMP 2600 — Nondeterministic Finite Automata 15

Page 16: Nondeterministic Finite Automata - Research School … Finite Automata COMP2600 — Formal Methods for Software Engineering Katya Lebedeva Australian National University Semester 2,

Idea for the construction of AD from AN

A subset of AN ’s states corresponds to a state in AD:

s0 s0

s0

s0

s0

1

s111

s1 s2 accept!111

s1 s2 stuck1

10

{s0} {s0} {s0 ,s1} {s0 ,s2} {s0 ,s1}1110

COMP 2600 — Nondeterministic Finite Automata 16

Page 17: Nondeterministic Finite Automata - Research School … Finite Automata COMP2600 — Formal Methods for Software Engineering Katya Lebedeva Australian National University Semester 2,

s0start s1 s2

0,1

1 0,1

The set of states of the DFA we have

to construct is associated with the set

of all subsets of the states of the given

NFA.

0 1

/0

{s0}{s1}{s2}

{s0,s1}{s0,s2}{s1,s2}

{s0,s1,s2}

COMP 2600 — Nondeterministic Finite Automata 17

Page 18: Nondeterministic Finite Automata - Research School … Finite Automata COMP2600 — Formal Methods for Software Engineering Katya Lebedeva Australian National University Semester 2,

s0start s1 s2

0,1

1 0,1

When we are in no state in the initial

NFA, regardless what the input symbol

is, we continue to be in no state.

0 1

/0 /0 /0

{s0}{s1}{s2}

{s0,s1}{s0,s2}{s1,s2}

{s0,s1,s2}

COMP 2600 — Nondeterministic Finite Automata 18

Page 19: Nondeterministic Finite Automata - Research School … Finite Automata COMP2600 — Formal Methods for Software Engineering Katya Lebedeva Australian National University Semester 2,

s0start s1 s2

0,1

1 0,1

When we in s0 in the NFA and the input

is “0”, the transition function of the NFA

takes us to {s0}.When we in s0 in the NFA and the input

is “1”, the transition function of the NFA

takes us to {s0,s1}.

0 1

/0 /0 /0

{s0} {s0} {s0,s1}{s1}{s2}

{s0,s1}{s0,s2}{s1,s2}

{s0,s1,s2}

COMP 2600 — Nondeterministic Finite Automata 19

Page 20: Nondeterministic Finite Automata - Research School … Finite Automata COMP2600 — Formal Methods for Software Engineering Katya Lebedeva Australian National University Semester 2,

s0start s1 s2

0,1

1 0,1

When we in s1 in the NFA and the input

is “0”, the transition function of the NFA

takes us to {s2}.When we in s1 in the NFA and the input

is “1”, the transition function of the NFA

takes us to {s2}.

0 1

/0 /0 /0

{s0} {s0} {s0,s1}{s1} {s2} {s2}{s2}

{s0,s1}{s0,s2}{s1,s2}

{s0,s1,s2}

COMP 2600 — Nondeterministic Finite Automata 20

Page 21: Nondeterministic Finite Automata - Research School … Finite Automata COMP2600 — Formal Methods for Software Engineering Katya Lebedeva Australian National University Semester 2,

s0start s1 s2

0,1

1 0,1

When we in s2 in the NFA and the input

is “0”, the transition function of the NFA

takes us to /0 (i.e. it takes us to none of

the states of the NFA).

When we in s1 in the NFA and the input

is “1”, the transition function of the NFA

takes us to /0.

0 1

/0 /0 /0

{s0} {s0} {s0,s1}{s1} {s2} {s2}{s2} /0 /0

{s0,s1}{s0,s2}{s1,s2}

{s0,s1,s2}

COMP 2600 — Nondeterministic Finite Automata 21

Page 22: Nondeterministic Finite Automata - Research School … Finite Automata COMP2600 — Formal Methods for Software Engineering Katya Lebedeva Australian National University Semester 2,

s0start s1 s2

0,1

1 0,1

Mark the start state.

Mark the accepting states.

Accepting states are those in which

appears at least one accepting state

of our NFA.

0 1

/0 /0 /0

→{s0} {s0} {s0,s1}{s1} {s2} {s2}∗{s2} /0 /0

{s0,s1} {s0,s2} {s0,s1,s2}∗{s0,s2} {s0} {s0,s1}∗{s1,s2} {s2} {s2}

∗{s0,s1,s2} {s0,s2} {s0,s1,s2}

COMP 2600 — Nondeterministic Finite Automata 22

Page 23: Nondeterministic Finite Automata - Research School … Finite Automata COMP2600 — Formal Methods for Software Engineering Katya Lebedeva Australian National University Semester 2,

s0start s1 s2

0,1

1 0,1

This transition table determines the

transition function of our DFA!!!

It belongs to a Deterministic Au-

tomaton!

Each of these sets is a state of

our DFA!

0 1

/0 /0 /0

→{s0} {s0} {s0,s1}{s1} {s2} {s2}∗{s2} /0 /0

{s0,s1} {s0,s2} {s0,s1,s2}∗{s0,s2} {s0} {s0,s1}∗{s1,s2} {s2} {s2}

∗{s0,s1,s2} {s0,s2} {s0,s1,s2}

COMP 2600 — Nondeterministic Finite Automata 23

Page 24: Nondeterministic Finite Automata - Research School … Finite Automata COMP2600 — Formal Methods for Software Engineering Katya Lebedeva Australian National University Semester 2,

s0start s1 s2

0,1

1 0,1

0 1

/0 /0 /0

→{s0} {s0} {s0,s1}{s1} {s2} {s2}∗{s2} /0 /0

{s0,s1} {s0,s2} {s0,s1,s2}∗{s0,s2} {s0} {s0,s1}∗{s1,s2} {s2} {s2}

∗{s0,s1,s2} {s0,s2} {s0,s1,s2}

COMP 2600 — Nondeterministic Finite Automata 24

Page 25: Nondeterministic Finite Automata - Research School … Finite Automata COMP2600 — Formal Methods for Software Engineering Katya Lebedeva Australian National University Semester 2,

s0start s1 s2

0,1

1 0,1

0 1

A A A

→ B B E

C D D

*D A A

E F H

*F B E

*G D D

*H F H

0 1

A /0 A /0 A /0

→ B→{s0} B {s0} E {s0,s1}C {s1} D {s2} D {s2}

*D ∗{s2} A /0 A /0

E {s0,s1} F {s0,s2} H {s0,s1,s2}*F ∗{s0,s2} B {s0} E {s0,s1}*G ∗{s1,s2} D {s2} D {s2}

*H ∗{s0,s1,s2} F {s0,s2} H {s0,s1,s2}

COMP 2600 — Nondeterministic Finite Automata 25

Page 26: Nondeterministic Finite Automata - Research School … Finite Automata COMP2600 — Formal Methods for Software Engineering Katya Lebedeva Australian National University Semester 2,

s0start s1 s2

0,1

1 0,1

0 1

A A A

→ B B E

C D D

*D A A

E F H

*F B E

*G D D

*H F H

COMP 2600 — Nondeterministic Finite Automata 26

Page 27: Nondeterministic Finite Automata - Research School … Finite Automata COMP2600 — Formal Methods for Software Engineering Katya Lebedeva Australian National University Semester 2,

COMP 2600 — Nondeterministic Finite Automata 27

Page 28: Nondeterministic Finite Automata - Research School … Finite Automata COMP2600 — Formal Methods for Software Engineering Katya Lebedeva Australian National University Semester 2,

0 1

A A A

→ B B E

C D D

*D A A

E F H

*F B E

*G D D

*H F H

Bstart E F

H

DC A

G

0

1 0

1

1 0

0

1

0,1

0,1

0,10,1

COMP 2600 — Nondeterministic Finite Automata 28

Page 29: Nondeterministic Finite Automata - Research School … Finite Automata COMP2600 — Formal Methods for Software Engineering Katya Lebedeva Australian National University Semester 2,

COMP 2600 — Nondeterministic Finite Automata 29

Page 30: Nondeterministic Finite Automata - Research School … Finite Automata COMP2600 — Formal Methods for Software Engineering Katya Lebedeva Australian National University Semester 2,

0 1

/0 /0 /0

→{s0} {s0} {s0,s1}{s1} {s2} {s2}∗{s2} /0 /0

{s0,s1} {s0,s2} {s0,s1,s2}∗{s0,s2} {s0} {s0,s1}∗{s1,s2} {s2} {s2}

∗{s0,s1,s2} {s0,s2} {s0,s1,s2}

{s0}start {s0,s1} {s0,s1}

{s0,s1,s2}

{s2}{s1} /0

{s1,s2}

0

1 0

1

1 0

0

1

0,1

0,1

0,10,1

COMP 2600 — Nondeterministic Finite Automata 30

Page 31: Nondeterministic Finite Automata - Research School … Finite Automata COMP2600 — Formal Methods for Software Engineering Katya Lebedeva Australian National University Semester 2,

Formally

Given AN = (Σ,SN ,s0,FN ,δN)

define AD = (Σ,SD,{s0},FD,δD) with

• SD = 2SN That is SD is the set of all subsets of SN

• FD = {Q⊆ SN | Q∩FN 6= /0}That is FD is all sets of AN ’s states that include at least one acceptingstate of AN

• For each set Q⊆ SN and for each input symbol a in Σ

δD(Q,a) =⋃p∈Q

δN(p,a)

That is δD(Q,a) is the set of states of AN reachable in AN from one of thestates in Q via a

COMP 2600 — Nondeterministic Finite Automata 31

Page 32: Nondeterministic Finite Automata - Research School … Finite Automata COMP2600 — Formal Methods for Software Engineering Katya Lebedeva Australian National University Semester 2,

COMP 2600 — Nondeterministic Finite Automata 32

Page 33: Nondeterministic Finite Automata - Research School … Finite Automata COMP2600 — Formal Methods for Software Engineering Katya Lebedeva Australian National University Semester 2,

{s0}start {s0,s1} {s0,s1}

{s0,s1,s2}

{s2}{s1} /0

{s1,s2}

0

1 0

1

1 0

0

1

0,1

0,1

0,10,1

COMP 2600 — Nondeterministic Finite Automata 33

Page 34: Nondeterministic Finite Automata - Research School … Finite Automata COMP2600 — Formal Methods for Software Engineering Katya Lebedeva Australian National University Semester 2,

“Lazy evaluation” on subsets for constructing AD

We eliminated states

that cannot be ac-

cessed from the start

state!

Can we avoid con-

structing the “inacces-

sible” part of the DFA?

Yes! As follows:

{s0}start {s0,s1} {s0,s1}

{s0,s1,s2}

0

1 0

1

1 0

0

1

Basis: The singleton set consisting only of the start state of AN is accessible.

Induction: Suppose we determined that the set Q of states is accessible.Then for each input symbol a, compute the set of states δD(Q,a). These setsof states will also be accessible.

COMP 2600 — Nondeterministic Finite Automata 34

Page 35: Nondeterministic Finite Automata - Research School … Finite Automata COMP2600 — Formal Methods for Software Engineering Katya Lebedeva Australian National University Semester 2,

Comment on the comment during the lecture whether it is δD or δN . The point

is that we are constructing δD via δN . Particularly, for each a∈ Σ, we compute

δD(Q,a) as⋃

p∈QδN(p,a).

COMP 2600 — Nondeterministic Finite Automata 35

Page 36: Nondeterministic Finite Automata - Research School … Finite Automata COMP2600 — Formal Methods for Software Engineering Katya Lebedeva Australian National University Semester 2,

s0start s1 s2

0,1

1 0,1

• s0 is the start state of AN . Therefore, {s0} is the start state of AD.

• Look at the transition diagram of AN .

Observe that on “0” there is only one arrow from s0, and it goes to s0.

Therefore, δD({s0},0) = {s0}.

Observe that on “1” there are two arrow from s0 that go to s0 and s1.

Therefore, δD({s0},1) = {s0,s1}.

• One of the sets we constructed - the set {s0} has just been analysed!

We only need to analyse {s0,s1} and compute its transitions!

COMP 2600 — Nondeterministic Finite Automata 36

Page 37: Nondeterministic Finite Automata - Research School … Finite Automata COMP2600 — Formal Methods for Software Engineering Katya Lebedeva Australian National University Semester 2,

s0start s1 s2

0,1

1 0,1

• What is δD({s0,s1},0)?

δD({s0,s1},0) = δN(s0,0)∪δN(s1,0) = {s0}∪{s2}= {s0,s2}

What is δD({s0,s1},1)?

δD({s0,s1},1) = δN(s0,1)∪δN(s1,1) = {s0,s1}∪{s2}= {s0,s1,s2}

COMP 2600 — Nondeterministic Finite Automata 37

Page 38: Nondeterministic Finite Automata - Research School … Finite Automata COMP2600 — Formal Methods for Software Engineering Katya Lebedeva Australian National University Semester 2,

s0start s1 s2

0,1

1 0,1

• What is δD({s0,s2},0)?

δD({s0,s2},0) = δN(s0,0)∪δN(s2,0) = {s0}∪ /0 = {s0}

What is δD({s0,s2},1)?

δD({s0,s2},1) = δN(s0,1)∪δN(s2,1) = {s0,s1}∪ /0 = {s0,s1}

• Both sets {s0} and {s0,s1} have already be analysed.

• But we still have set {s0,s1,s2} that we accessed on the previous slide

and that awaits analysis!

COMP 2600 — Nondeterministic Finite Automata 38

Page 39: Nondeterministic Finite Automata - Research School … Finite Automata COMP2600 — Formal Methods for Software Engineering Katya Lebedeva Australian National University Semester 2,

s0start s1 s2

0,1

1 0,1

• What is δD({s0,s1,s2},0)?

δD({s0,s1,s2},0)= δN(s0,0)∪δN(s1,0)∪δN(s1,0)= {s0}∪{s2}∪ /0= {s0,s2}

What is δD({s0,s1,s2},1)?

δD({s0,s1,s2},1)= δN(s0,1)∪δN(s1,1)∪δN(s2,1)= {s0,s1}∪{s2}∪ /0= {s0,s1,s2}

• All accessible sets have been analysed!

• We can draw AD now!

COMP 2600 — Nondeterministic Finite Automata 39