Automata and Languages Prof. Mohamed Hamada Software Engineering Lab. The University of Aizu Japan
Nondeterministic Finite Automata with empty moves (-NFA) Definition A nondeterministic finite automaton with empty moves (-NFA) M is defined by a 5-tuple M=(Q,Σ,δ,q 0,F), with q Q: finite set of states q Σ: finite input alphabet q δ: transition function δ:q (Σ U {}) P(Q) q q 0 Q: start state q F Q: set of final states
Nondeterministic Finite Automata with empty moves (-NFA) Definition A string w is accepted by a -NFA M if and only if there exists a path starting at q 0 which is labeled by w and ends in a final state. The language accepted by a -NFA M is the set of all strings which are accepted by M and is denoted by L (M ). L(M)={w: δ(q 0,w) F Φ}
Notes Nondeterministic Finite Automata with empty moves (-NFA) δ : Q ( Σ { }) P( Q) A -transition causes the machine to change its state non-deterministically, without consuming any input. q q 2
Notes Nondeterministic Finite Automata with empty moves (-NFA) A -NFA has transition rules/possibilities like: q q 2 And q : q 2 q 3 : Empty string transition Nondeterministic transition
Nondeterministic Finite Automata with empty moves (-NFA) Nondeterminism ~ Parallelism For any string w, the nondeterministic automaton can be in a subset Q of several possible states. If the final set contains a final state, then the automaton accepts the string. The automaton processes the input in a parallel fashion; its computational path is no longer a line, but more like a tree.
Nondeterministic Finite Automata with empty moves (-NFA) We can write the NFA in two ways. State digraph a b q0 b q q2 2. Table δ : Q ( Σ { }) P( Q) δ a b q0 {q0} {q0,q} φ q φ φ {q2} q2 φ φ φ
Nondeterministic Finite Automata with empty moves (-NFA) Example 0, 0, 0, q q 2 q 3 q 4 This automaton accepts 00, because there is a possible path that leads to a final state, namely: q q q 2 q 3 q 4 q 4 (note that q q q q q is not accepting)
Nondeterministic Finite Automata with empty moves (-NFA) Example 0, 0, 0, q q 2 q 3 q 4 The string gets rejected: on the automaton can only reach: {q,q 2,q 3 }.
Example Difference between NFA and -NFA 0, 0, -NFA: 0, q q 2 q 3 0, q 4 0, NFA: 0 q q 2 q 3 q 4 The string is accepted by the above -NFA And rejected by the above NFA
Nondeterministic Finite Automata with empty moves (-NFA) Example a a start b b A -transition is taken without consuming any character from the input. What does the NFA above accepts? aa*+bb*
Quiz Nondeterministic Finite Automata with empty moves (-NFA) M : M 2 : M 3 : q 0 q 0 q q 0 q 2 M 4 : 0 q 0, q 0 q 3 q 2 What are δ(q 0,0), δ(q 0,), δ(q 0, ) in each of M, M 2, M 3 and in M 4.?
Nondeterministic Finite Automata with empty moves (-NFA) Answer M: M2: M3: q 0 q 0 q q 0 q 2 M4: 0 q 0, q 0 q 3 q 2 M: δ(q 0,0) = δ(q 0,) = δ(q 0, ) = M2: Same M3: δ(q 0,0) = δ(q 0,) =, δ(q 0, ) = {q,q 2 } M4: δ(q 0,0)={q,q 3 }, δ(q 0,)={q 2,q 3 }, δ(q 0, )=
Quiz Nondeterministic Finite Automata with empty moves (-NFA) 0 q 0, q 0 q 3 q 2 Which of the following strings is accepted?. 2. 0 3. 4. 0
Nondeterministic Finite Automata with empty moves (-NFA) Answer 0 q 0, q 0 q 3 q 2.. is rejected. No path labeled by empty string from start state to an accept state. 2. 0 is accepted. EG the path 3. is accepted. EG the path 4. 0 is accepted. There is only one accepted path: q q 0 0 q 0 q3 q 0 q3 q2 q3 q2 q3 q2 q3 0
Nondeterministic Finite Automata with empty moves (-NFA) Definition Given a -NFA state s, the -closure(s) is the set of states that are reachable through -transition from s. -closure(s)={q: there is a path from s to q labeled } Given a set of -NFA states T, the -closure(t) is the set of states that are reachable through -transition from any state s T. -closure(t)=u s T -closure(s)
Nondeterministic Finite Automata with empty moves (-NFA) Example : 0 2 q 0 q q 2 -closure(q 0 ) = {q 0,q, q 2 } -closure(q ) = {q, q 2 } -closure(q 2 ) = {q 2 }
Nondeterministic Finite Automata with empty moves (-NFA) Example 2: i f 2 3 any character IF 4 5 error a-z a-z 4 5 6 7 0-9 0-9 0-9 9 0 2 8 NUM 3 ID What states can be reached from state without consuming a character?
Nondeterministic Finite Automata with empty moves (-NFA) Example i f 2 3 any character IF 4 5 error a-z a-z 4 5 6 7 0-9 0-9 0-9 9 0 2 8 NUM 3 ID What states can be reached from state without consuming a character? {,4,9,4} form the -closure of state
Nondeterministic Finite Automata with empty moves (-NFA) Example i 2 3 f any character IF 4 5 a-z 4 5 6 7 error What are all the state closures in this NFA? a-z 0-9 0-9 0-9 9 0 2 8 NUM 3 ID closure() = {,4,9,4} closure(5) = {5,6,8} closure(8) = {6,8} closure(7) = {6,7,8} closure(0) = {0,,3} closure(3) = {,3} closure(2) = {,2,3}
Nondeterministic Finite Automata with empty moves (-NFA) Definition: Extension of δ δ : Q ( { }) P( Q) * δ : Q P( Q) δ is defined as follows:. (q,)=-closure(q) δ 2. (q,wa)=-closure(t) where δ T={p: p δ(r,a) and r δ (q,w)}, a Σ, w Σ*
Nondeterministic Finite Automata with empty moves (-NFA) Example: Extension of δ 0 2 q 0 q q 2 δ (q 0, 0) = {q, q 2 }
-NFA è NFA Theorem: For every language L that is accepted by a -NFA, there is an NFA that accepts L as well. -NFA and NFA are equivalent computational models.
-NFA è NFA Proof: Let M=(Q,Σ,δ,q 0,F) be a -NFA, an equivalent NFA, M =(Q,Σ,δ,q 0,F ) can be constructed as follows:. F ' = { F { q 0 } F If -closure(q 0 ) Otherwise F Φ ˆ( 2. δ '( q, a) = δ q, a)
-NFA è NFA Example: For the -NFA: 0 2 q 0 q q 2 Construct the equivalent NFA? Answer: Given -NFA Q={q 0,q,q 2 } and Σ={0,,2} -closure(q 0 )={q 0,q,q 2 } F Φ δ ( q,0) = ˆ( δ,0) = {q 0,q,q 2 } ' 0 q0 δ ( q,) = ˆ( δ,) = {q,q 2 } ' 0 q0 δ ( q,2) = ˆ( δ,2) = {q 2 } ' 0 q0 0 Constructed NFA Q={q 0,q,q 2 } and Σ={0,,2} 0, F = {q 0,q 2 } q 0 q q 2 0,,2
-NFA è NFA Example: For the -NFA: 0 2 q 0 q q 2 Construct the equivalent NFA? Answer: Given -NFA Q={q 0,q,q 2 } and Σ={0,,2} -closure(q 0 )={q 0,q,q 2 } F Φ δ ( q,0) = ˆ( δ,0) = Φ ' q δ ( q,) = ˆ( δ,) = {q,q 2 } ' q δ ( q,2) = ˆ( δ,2) = {q 2 } ' q Constructed NFA Q={q 0,q,q 2 } and Σ={0,,2} F = {q 0,q } 0 0,,2 q 0 q q 2 0,,2
-NFA è NFA Example: For the -NFA: 0 2 q 0 q q 2 Construct the equivalent NFA? Answer: Given -NFA Q={q 0,q,q 2 } and Σ={0,,2} -closure(q 0 )={q 0,q,q 2 } F Φ δ ( q,0) = ˆ( δ,0) = Φ ' 2 q2 δ ( q,) = ˆ( δ,) = Φ ' 2 q2 δ ( q,2) = ˆ( δ,2) = {q 2 } ' 2 q2 Constructed NFA Q={q 0,q,q 2 } and Σ={0,,2} F = {q 0,q } 0 0,,2 q 0 q q 2 0,,2 2
-NFA è NFA Example: For the -NFA: 0 2 q 0 q q 2 Construct the equivalent NFA? Answer: q 0 q q 2 0 2 0,,2 0,,2
RE è -NFA Theorem: Let r be RE, there exist a -NFA that accepts L(r).
RE è -NFA Proof: The proof works by induction, using the recursive definition of regular expressions. RE -NFA Φ q 0 q 0 a q 0 a q
RE è -NFA Proof: M M2 r+r2 è L(M) U L(M2) M r*è L(M)* M M2 r.r2 è L(M) L(M2)
RE è -NFA Example For the regular expression r=if we build the -NFA as follows: The -NFA for a symbol i is: start i 2 The -NFA for a symbol f is: start f 2 The -NFA for the regular expression if is: start i 2 f 3 IF
RE è -NFA Example 2 For the regular expression r=0+* build the equivalent -NFA? The -NFA for a symbol 0 is: 0 The -NFA for a symbol is: The -NFA for a symbol * is: The -NFA for the regular expression 0+* is:
RE è -NFA Example 2 For the regular expression r=0+* build the equivalent -NFA? The -NFA for the regular expression 0+* is: * 0+* 0 0
RE è -NFA Example 3 Q: Find an NFA for the regular expression (0 )*(0000000 (0 )*)(0 )*
RE è -NFA Example 3 (0 )*(0000000 (0 )*)(0 )* Note that: in this example ε =
RE è -NFA Exercise Construct a -NFA for the regular expression: 00*+(+0)*+0*