Nondeterministic Finite Automata COMP2600 Formal Methods for Software Engineering Katya Lebedeva Australian National University Semester 2, 206 Slides by Katya Lebedeva. COMP 2600 Nondeterministic Finite Automata
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
Nondeterministic Finite State Automata s 0 0, start s s 2 0, COMP 2600 Nondeterministic Finite Automata 3
State diagram for 0 s 0 0, start s s 2 0, s 0 0 s 0 s s 2 stuck s s 2 accept! s 0 s s 0 s 0 COMP 2600 Nondeterministic Finite Automata 4
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 as the second symbol from the end of the string. COMP 2600 Nondeterministic Finite Automata 5
A Nondeterministic Finite State Automaton (DFA) is a 5-tuple where (Σ,S,s 0,F,δ). Σ is a finite set of input symbols (the alphabet) 2. S is a finite state of states 3. s 0 is the start (or initial) state: s 0 S 4. F is a set of final (or accepting) states: F S 5. δ is a transition function such that δ : S Σ 2 S, i.e. δ takes a state in S and an input symbol in Σ as arguments and returns 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
s 0 0, start s s 2 0, The NFA can be specified formally as ({0,},{s 0,s,s 2,},s 0,{s 2 },δ) where the transition function δ is given by the transition table 0 s 0 {s 0 } {s 0,s } s {s 2 } {s 2 } s 2 /0 /0 COMP 2600 Nondeterministic Finite Automata 7
Extended Transition Function s 0 0 s 0 s s 2 stuck s s 2 accept! s 0 s s 0 s 0 COMP 2600 Nondeterministic Finite Automata 8
The extended transition function of a NFA is the function δ : S Σ 2 S defined as follows: for all s S, for all a Σ, for all α Σ δ (s,ε) = {s} δ (s,αa) = p δ (s,α) δ(p, a) I.e. if δ (s,α) = {p, p 2,... p k } then δ (s,αa) = δ(p,a) δ(p 2,a) δ(p k,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
Input: 0 s 0 0, start s s 2 0,. δ (s 0,ε) = {s 0 } the basis rule! 2. δ (s 0,0) = δ(s 0,0) = {s 0 } 3. δ (s 0,0) = δ(s 0,) = {s 0,s } 4. δ (s 0,0) = δ(s 0,) δ(s,) = {s 0,s } {s 2 } = {s 0,s,s 2 } 5. δ (s 0,0) = δ(s 0,) δ(s,) δ(s 2,) = {s 0,s } {s 2 } /0 = {s 0,s,s 2 } COMP 2600 Nondeterministic Finite Automata 0
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
Language accepted by an NFA If A N = (Σ,S,s 0,F,δ) is an NFA, then L(A N ) = {w δ (s 0,w) F /0} That is, L(A N ) is the set of strings w in Σ such that δ (s 0,w) contains at least one accepting state. COMP 2600 Nondeterministic Finite Automata 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 2 n states. In most cases, the DFA has about as many states as the equivalent NFA, but more transitions. COMP 2600 Nondeterministic Finite Automata 3
Proof idea of the equivalence of DFAs and NFAs We have to prove: L(DFA) L(NFA) Whenever we have a DFA A D, we can construct a NFA A N such that L(A D ) = L(A N ). This is easy to show! Why? L(NFA) L(DFA) For every NFA A N there is a DFA A D such that L(A D ) = L(A N ). The proof of this direction involves Subset Construction. COMP 2600 Nondeterministic Finite Automata 4
Subset Construction Intuition The NFA A N can perform more than one computation on a given input string. Construct a DFA A D that runs all these different computations simultaneously: the state that A D is in after having read an initial part of the input string corresponds exactly to the set of all states that A N can reach after having read the same part of the input string COMP 2600 Nondeterministic Finite Automata 5
Idea for the construction of A D from A N A subset of A N s states corresponds to a state in A D : s 0 0 s 0 s s 2 stuck s s 2 accept! s 0 s s 0 s 0 0 {s 0 } {s 0 } {s 0,s } {s 0,s 2 } {s 0,s } COMP 2600 Nondeterministic Finite Automata 6
s 0 0, start s s 2 0, 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 {s 0 } {s } {s 2 } {s 0,s } {s 0,s 2 } {s,s 2 } {s 0,s,s 2 } 0 COMP 2600 Nondeterministic Finite Automata 7
s 0 0, start s s 2 0, When we are in no state in the initial NFA, regardless what the input symbol is, we continue to be in no state. 0 /0 /0 /0 {s 0 } {s } {s 2 } {s 0,s } {s 0,s 2 } {s,s 2 } {s 0,s,s 2 } COMP 2600 Nondeterministic Finite Automata 8
s 0 0, start s s 2 0, When we in s 0 in the NFA and the input is 0, the transition function of the NFA takes us to {s 0 }. When we in s 0 in the NFA and the input is, the transition function of the NFA takes us to {s 0,s }. 0 /0 /0 /0 {s 0 } {s 0 } {s 0,s } {s } {s 2 } {s 0,s } {s 0,s 2 } {s,s 2 } {s 0,s,s 2 } COMP 2600 Nondeterministic Finite Automata 9
s 0 0, start s s 2 0, When we in s in the NFA and the input is 0, the transition function of the NFA takes us to {s 2 }. When we in s in the NFA and the input is, the transition function of the NFA takes us to {s 2 }. 0 /0 /0 /0 {s 0 } {s 0 } {s 0,s } {s } {s 2 } {s 2 } {s 2 } {s 0,s } {s 0,s 2 } {s,s 2 } {s 0,s,s 2 } COMP 2600 Nondeterministic Finite Automata 20
s 0 0, start s s 2 0, When we in s 2 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 s in the NFA and the input is, the transition function of the NFA takes us to /0. 0 /0 /0 /0 {s 0 } {s 0 } {s 0,s } {s } {s 2 } {s 2 } {s 2 } /0 /0 {s 0,s } {s 0,s 2 } {s,s 2 } {s 0,s,s 2 } COMP 2600 Nondeterministic Finite Automata 2
s 0 0, start s s 2 0, Mark the start state. Mark the accepting states. Accepting states are those in which appears at least one accepting state of our NFA. 0 /0 /0 /0 {s 0 } {s 0 } {s 0,s } {s } {s 2 } {s 2 } {s 2 } /0 /0 {s 0,s } {s 0,s 2 } {s 0,s,s 2 } {s 0,s 2 } {s 0 } {s 0,s } {s,s 2 } {s 2 } {s 2 } {s 0,s,s 2 } {s 0,s 2 } {s 0,s,s 2 } COMP 2600 Nondeterministic Finite Automata 22
s 0 0, start s s 2 0, This transition table determines the transition function of our DFA!!! It belongs to a Deterministic Automaton! Each of these sets is a state of our DFA! 0 /0 /0 /0 {s 0 } {s 0 } {s 0,s } {s } {s 2 } {s 2 } {s 2 } /0 /0 {s 0,s } {s 0,s 2 } {s 0,s,s 2 } {s 0,s 2 } {s 0 } {s 0,s } {s,s 2 } {s 2 } {s 2 } {s 0,s,s 2 } {s 0,s 2 } {s 0,s,s 2 } COMP 2600 Nondeterministic Finite Automata 23
s 0 0, start s s 2 0, 0 /0 /0 /0 {s 0 } {s 0 } {s 0,s } {s } {s 2 } {s 2 } {s 2 } /0 /0 {s 0,s } {s 0,s 2 } {s 0,s,s 2 } {s 0,s 2 } {s 0 } {s 0,s } {s,s 2 } {s 2 } {s 2 } {s 0,s,s 2 } {s 0,s 2 } {s 0,s,s 2 } COMP 2600 Nondeterministic Finite Automata 24
s 0 0, start s s 2 0 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, 0 A /0 A /0 A /0 B {s 0 } B {s 0 } E {s 0,s } C {s } D {s 2 } D {s 2 } *D {s 2 } A /0 A /0 E {s 0,s } F {s 0,s 2 } H {s 0,s,s 2 } *F {s 0,s 2 } B {s 0 } E {s 0,s } *G {s,s 2 } D {s 2 } D {s 2 } *H {s 0,s,s 2 } F {s 0,s 2 } H {s 0,s,s 2 } COMP 2600 Nondeterministic Finite Automata 25
s 0 0, start s s 2 0 0, 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
COMP 2600 Nondeterministic Finite Automata 27
0 0 A A A B B E 0 start B E F 0 0 C D D H *D A A E F H *F B E *G D D C 0, D 0, A 0, *H F H 0, G COMP 2600 Nondeterministic Finite Automata 28
COMP 2600 Nondeterministic Finite Automata 29
0 0 /0 /0 /0 {s 0 } {s 0 } {s 0,s } {s } {s 2 } {s 2 } {s 2 } /0 /0 {s 0,s } {s 0,s 2 } {s 0,s,s 2 } {s 0,s 2 } {s 0 } {s 0,s } {s,s 2 } {s 2 } {s 2 } {s 0,s,s 2 } {s 0,s 2 } {s 0,s,s 2 } start {s 0 } {s 0,s } {s 0,s } 0 0, {s 0,s,s 2 } {s } {s 2 } /0 0, 0 0 0, 0, {s,s 2 } COMP 2600 Nondeterministic Finite Automata 30
Formally Given A N = (Σ,S N,s 0,F N,δ N ) define A D = (Σ,S D,{s 0 },F D,δ D ) with S D = 2 S N That is S D is the set of all subsets of S N F D = {Q S N Q F N /0} That is F D is all sets of A N s states that include at least one accepting state of A N For each set Q S N 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 A N reachable in A N from one of the states in Q via a COMP 2600 Nondeterministic Finite Automata 3
COMP 2600 Nondeterministic Finite Automata 32
0 start {s 0 } {s 0,s } {s 0,s } 0 0 0 {s 0,s,s 2 } 0, 0, {s } {s 2 } /0 0, 0, {s,s 2 } COMP 2600 Nondeterministic Finite Automata 33
Lazy evaluation on subsets for constructing A D We eliminated states that cannot be accessed from the start state! Can we avoid constructing the inaccessible part of the DFA? Yes! As follows: start {s 0 } {s 0,s } {s 0,s } 0 0 {s 0,s,s 2 } 0 0 Basis: The singleton set consisting only of the start state of A N 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 sets of states will also be accessible. COMP 2600 Nondeterministic Finite Automata 34
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 δ N (p,a). p Q COMP 2600 Nondeterministic Finite Automata 35
s 0 0, start s s 2 0, s 0 is the start state of A N. Therefore, {s 0 } is the start state of A D. Look at the transition diagram of A N. Observe that on 0 there is only one arrow from s 0, and it goes to s 0. Therefore, δ D ({s 0 },0) = {s 0 }. Observe that on there are two arrow from s 0 that go to s 0 and s. Therefore, δ D ({s 0 },) = {s 0,s }. One of the sets we constructed - the set {s 0 } has just been analysed! We only need to analyse {s 0,s } and compute its transitions! COMP 2600 Nondeterministic Finite Automata 36
s 0 0, start s s 2 0, What is δ D ({s 0,s },0)? δ D ({s 0,s },0) = δ N (s 0,0) δ N (s,0) = {s 0 } {s 2 } = {s 0,s 2 } What is δ D ({s 0,s },)? δ D ({s 0,s },) = δ N (s 0,) δ N (s,) = {s 0,s } {s 2 } = {s 0,s,s 2 } COMP 2600 Nondeterministic Finite Automata 37
s 0 0, start s s 2 0, What is δ D ({s 0,s 2 },0)? δ D ({s 0,s 2 },0) = δ N (s 0,0) δ N (s 2,0) = {s 0 } /0 = {s 0 } What is δ D ({s 0,s 2 },)? δ D ({s 0,s 2 },) = δ N (s 0,) δ N (s 2,) = {s 0,s } /0 = {s 0,s } Both sets {s 0 } and {s 0,s } have already be analysed. But we still have set {s 0,s,s 2 } that we accessed on the previous slide and that awaits analysis! COMP 2600 Nondeterministic Finite Automata 38
s 0 0, start s s 2 0, What is δ D ({s 0,s,s 2 },0)? δ D ({s 0,s,s 2 },0) = δ N (s 0,0) δ N (s,0) δ N (s,0) = {s 0 } {s 2 } /0 = {s 0,s 2 } What is δ D ({s 0,s,s 2 },)? δ D ({s 0,s,s 2 },) = δ N (s 0,) δ N (s,) δ N (s 2,) = {s 0,s } {s 2 } /0 = {s 0,s,s 2 } All accessible sets have been analysed! We can draw A D now! COMP 2600 Nondeterministic Finite Automata 39