SlideShare a Scribd company logo
Deterministic Finite State Automata (DFA)   …… .. One-way, infinite tape, broken into cells One-way, read-only tape head. Finite control, I.e., a program, containing the position of the read head, current symbol being scanned, and the current “state.” A string is placed on the tape, read head is positioned at the left end, and the DFA will read the string one symbol at a time until all symbols have been read. The DFA will then either accept or reject. Finite Control 0 1 1 0 0
The finite control can be described by a  transition diagram : Example #1:   1   0   0   1   1   q 0 q 0 q 1 q 0 q 0 q 0 One state is final/accepting, all others are rejecting. The above DFA accepts those strings that contain an even number of 0’s q 0 q 1 0 0 1 1
Example #2:   a   c   c   c   b accepted   q 0 q 0 q 1 q 2 q 2 q 2   a   a   c rejected   q 0 q 0 q 0 q 1  Accepts those strings that contain  at least  two c’s q 1 q 0 q 2 a b a b c c a/b/c
Inductive Proof (sketch): Base:  x a string with |x|=0. state will be q0 => rejected. Inductive hypothesis:  |x|=k, rejected -in state q0 (x must have 0 c), rejected – in state q1 (x must have 1 c), accepted – in state q2 (x already with 2 c’s) Inductive step:  String xp, for p = a, b and c q0 and, xa or xb : q0->q0 rejected, as should be (no c) q0 and, xc : q0 -> q1 rejected, as should be (1 c) q1 and xa or xb:  q1 -> q1 rejected, … q1 and xc:  q1-> q2 accepted, as should be ( 2 c’s now) q2 and xa, or xb, or xc:  q2 -> q2 accepted, (no change)
Formal Definition of a DFA A DFA is a five-tuple: M = (Q, Σ, δ, q 0 , F) Q A  finite  set of states Σ A  finite  input alphabet q 0 The initial/starting state, q 0  is in Q F A set of final/accepting states, which is a subset of Q δ A transition function, which is a total function from Q x Σ to Q   δ: (Q x Σ) – >  Q   δ is defined for any q in Q and s in Σ, and    δ(q,s) = q’   is equal to some state q’ in Q, could be q’=q Intuitively, δ(q,s) is the state entered by M after reading symbol s while in state q.
For example #1: Q = {q 0 , q 1 } Σ = {0, 1} Start state is q 0 F = {q 0 } δ: 0 1   q 0   q 1    q 0   q 1    q 0   q 1 q 0 q 1 0 0 1 1
For example #2: Q = {q 0 , q 1 , q 2 } Σ = {a, b, c} Start state is q 0 F = {q 2 } δ: a b c   q 0   q 0    q 0   q 1   q 1    q 1   q 1   q 2   q 2   q 2   q 2   q 2 Since δ is a function, at each step M has exactly one option. It follows that for a given string, there is exactly one computation. q 1 q 0 q 2 a b a b c c a/b/c
Extension of  δ to Strings δ ^  : (Q x Σ * ) – >  Q δ ^ (q,w) – The state entered after reading string w having started in state q. Formally: 1) δ ^ (q, ε) = q, and 2) For all w in Σ *  and a in Σ   δ ^ (q,wa) = δ (δ ^ (q,w), a)
Recall Example #1: What is δ ^ (q 0 , 011)? Informally, it is the state entered by M after processing 011 having started in state q 0. Formally:   δ ^ (q 0 , 011)  = δ (δ ^ (q 0 ,01), 1) by rule #2 = δ (δ ( δ ^ (q 0 ,0), 1), 1) by rule #2 = δ (δ (δ (δ ^ (q 0 , λ), 0), 1), 1) by rule #2 = δ (δ (δ(q 0 ,0), 1), 1) by rule #1 = δ (δ (q 1 , 1), 1) by definition of δ = δ (q 1 , 1) by definition of δ = q 1 by definition of δ Is 011 accepted? No, since δ ^ (q 0 , 011) = q 1  is not a final state. q 0 q 1 0 0 1 1
Note that: δ ^  (q,a)  = δ(δ ^ (q,  ε ), a) by definition of δ ^ , rule #2 = δ(q, a) by definition of δ ^ , rule #1 Therefore:   δ ^  (q, a 1 a 2 …a n ) = δ(δ(…δ(δ(q, a1), a2)…), a n ) Hence, we can use δ in place of δ ^ : δ ^ (q, a 1 a 2 …a n ) = δ(q, a 1 a 2 …a n )
Recall Example #2: What is δ(q 0 , 011)? Informally, it is the state entered by M after processing 011 having started in state q 0. Formally:   δ(q 0 , 011)  = δ (δ(q 0 ,01), 1) by rule #2 = δ (δ (δ(q 0 ,0), 1), 1) by rule #2 = δ (δ (q 1 , 1), 1) by definition of δ = δ (q 1 , 1) by definition of δ = q 1 by definition of δ Is 011 accepted? No, since δ(q 0 , 011) = q 1  is not a final state. q 1 q 0 1 1 0 0 1 0 q 2
Recall Example #2: What is δ(q 1 , 10)?   δ(q 1 , 10)  = δ (δ(q 1 ,1), 0) by rule #2 = δ (q 1 , 0) by definition of δ = q 2 by definition of δ Is 10 accepted? No, since δ(q 0 , 10) = q 1  is not a final state. The fact that δ(q 1 , 10) = q 2  is irrelevant! 0 0 q 1 q 0 q 2 1 1 0 1
Definitions for DFAs Let M = (Q, Σ, δ,q 0 ,F) be a DFA and let w be in Σ * .  Then w is  accepted  by  M  iff δ(q 0 ,w) = p  for some state p in F.  Let  M = (Q, Σ, δ,q 0 ,F)  be a DFA. Then the  language accepted  by M is the set: L(M) = {w | w is in Σ *  and δ(q 0 ,w) is in F}  Another equivalent definition: L(M) = {w | w is in Σ *  and w is accepted by M} Let  L  be a language. Then  L  is a  regular language  iff there exists a DFA  M  such that L = L(M). Let  M 1  = (Q 1 , Σ 1 , δ 1 , q 0 , F 1 )  and M 2  = (Q 2 , Σ 2 , δ 2 , p 0 , F 2 )  be DFAs. Then M 1   and M 2  are  equivalent  iff  L(M 1 ) = L(M 2 ).
Notes: A DFA M = (Q, Σ, δ,q 0 ,F) partitions the set Σ *  into two sets: L(M) and Σ *  - L(M).  If L = L(M) then L is a subset of L(M) and L(M) is a subset of L. Similarly, if L(M 1 ) = L(M 2 ) then L(M 1 ) is a subset of L(M 2 ) and L(M 2 ) is a subset of L(M 1 ).  Some languages are regular, others are not. For example, if L 1  = {x | x is a string of 0's and 1's containing an even  number of 1's} and  L 2  = {x | x = 0 n 1 n  for some n >= 0}  then L 1  is regular but L 2  is not. Questions: How do we determine whether or not a given language is regular? How could a program “simulate” a DFA?
Give a DFA M such that: L(M) = {x | x is a string of 0’s and 1’s and |x| >= 2} Prove this by induction q 1 q 0 q 2 0/1 0/1 0/1
Give a DFA M such that: L(M) = {x | x is a string of (zero or more) a’s, b’s and c’s such  that x does not contain the substring  aa } q 2 q 0 a a/b/c a q 1 b/c b/c
Give a DFA M such that:   L(M) = {x | x is a string of a’s, b’s and c’s such that x  contains the substring  aba } q 2 q 0 a a/b/c b q 1 c b/c a b/c q 3 a
Give a DFA M such that: L(M) = {x | x is a string of a’s and b’s such that x  contains both  aa  and  bb } q 0 b q 7 q 5 q 4 q 6 b b b a q 2 q 1 q 3 a a a b a/b b a a a b
Let Σ = {0, 1}. Give DFAs for {}, {ε}, Σ * , and Σ + . For {}: For {ε}: For Σ * : For Σ + : 0/1 q 0 0/1 q 0 q 1 q 0 0/1 0/1 0/1 q 0 q 1 0/1
Nondeterministic Finite State Automata (NFA) An NFA is a five-tuple: M = (Q, Σ, δ, q 0 , F) Q A  finite  set of states Σ A  finite  input alphabet q 0 The initial/starting state, q 0  is in Q F A set of final/accepting states, which is a subset of Q δ A transition function, which is a total function from Q x Σ to 2 Q δ: (Q x Σ) – >   2 Q -2 Q  is the power set of Q, the set of all subsets of Q  δ(q,s) -The set of all states p such that there is a transition   labeled s from q to p δ(q,s) is a function from Q x S to 2 Q  (but not to Q)
Example #1: some 0’s followed by some 1’s Q = {q 0 , q 1 , q 2 } Σ = {0, 1} Start state is q 0 F = {q 2 } δ: 0 1   q 0   q 1   q 2 q 1 q 0 0 1 0 1 0/1 { q 0 , q 1 } {} {} { q 1 , q 2 } { q 2 } { q 2 } q 2
Example #2: pair os 0’s or pair of 1’s Q = {q 0 , q 1 , q 2  , q 3  , q 4 } Σ = {0, 1} Start state is q 0 F = {q 2 , q 4 } δ: 0 1 q 0 q 1 q 2 q 3 q 4 1 { q 0 , q 3 } { q 0 , q 1 } {} { q 2 } { q 2 } { q 2 } { q 4 } {} { q 4 } { q 4 } q 0 0/1 0 0 q 3 q 4 0/1 q 1 q 2 0/1 1
Notes: δ(q,s) may not be defined for some q and s (why?). Informally, a string is said to be accepted  if there exists  a path to some state in F. The language accepted by an NFA is the set of all accepted strings. Question: How does an NFA find the correct/accepting path for a given string? NFAs are a non-intuitive computing model. We are  primarily  interested in NFAs as language defining devices, i.e., do NFAs accept languages that DFAs do not? Other questions are secondary, including practical questions such as whether or not there is an algorithm for finding an accepting path through an NFA for a given string,
Determining if a given NFA (example #2) accepts a given string (001) can be done algorithmically: q 0 q 0 q 0 q 0 q 3 q 3 q 1 q 4 q 4 accepted Each level will have at most n states 0 0 1
Another example (010): q 0 q 0 q 0 q 0 q 3 q 1 q 3 not accepted All paths have been explored, and none lead to an accepting state. 0 1 0
Let Σ = {a, b, c}. Give an NFA M that accepts: L = {x | x is in Σ *  and x contains ab} Is L a subset of L(M)? Is L(M) a subset of L? Is an NFA necessary? Could a DFA accept L? Try and give an equivalent DFA as an exercise. Designing NFAs is not a typical task. q 1 q 0 q 2 a a/b/c b a/b/c
Let Σ = {a, b}. Give an NFA M that accepts: L = {x | x is in Σ *  and the third to the last symbol in x is b} Is L a subset of L(M)? Is L(M) a subset of L? Give an equivalent DFA as an exercise. q 1 q 0 b q 3 a/b a/b q 2 a/b
Extension of  δ to Strings and Sets of States What we currently have: δ : (Q x Σ) – >  2 Q What we want (why?): δ : (2 Q  x Σ * ) – >  2 Q We will do this in two steps, which will be slightly different from the book, and we will make use of the following NFA. q 0 0 1 q 1 q 4 q 3 0 1 q 2 0 0 1 0 0
Extension of  δ to Strings and Sets of States Step #1: Given δ: (Q x Σ) – >  2 Q  define δ # : (2 Q  x Σ) – >  2 Q  as follows: 1) δ # (R, a) =  δ(q, a) for all subsets R of Q, and symbols a in Σ Note that: δ # ({p},a) =  δ(q, a)  by definition of δ # , rule #1 above   = δ(p, a) Hence, we can use δ for δ # δ({q 0 , q 2 }, 0) These now make sense, but previously δ({q 0 , q 1 , q 2 }, 0) they did not.
Example: δ({q 0 , q 2 }, 0) = δ(q 0 , 0) U δ(q 2 , 0) = {q 1 , q 3 } U {q 3 , q 4 } = {q 1 , q 3 , q 4 } δ({q 0 , q 1 , q 2 }, 1) = δ(q 0 , 1) U δ(q 1 , 1) U δ(q 2 , 1)   = {} U {q 2 , q 3 } U {}   = {q 2 , q 3 }
Step #2: Given δ: (2 Q  x Σ) – >  2 Q  define δ ^ : (2 Q  x Σ*) – >  2 Q  as follows: δ ^ (R,w) – The set of states M could be in after processing string w, having starting from any state in R. Formally: 2) δ ^ (R, ε) = R for any subset R of Q 3) δ ^ (R,wa) = δ (δ ^ (R,w), a) for any w in Σ * , a in Σ, and subset R of Q Note that: δ ^ (R, a) = δ(δ ^ (R, ε), a)  by definition of δ ^ , rule #3 above = δ(R, a) by definition of δ ^ , rule #2 above Hence, we can use δ for δ ^ δ({q 0 , q 2 }, 0110) These now make sense, but previously δ({q 0 , q 1 , q 2 }, 101101) they did not.
Example: What is δ({q 0 }, 10)? Informally: The set of states the NFA could be in after processing 10, having started in state q 0 , i.e., {q 1 , q 2 , q 3 }. Formally:  δ({q 0 }, 10) = δ(δ({q 0 }, 1), 0)   = δ({q 0 }, 0)   = {q 1 , q 2 , q 3 } Is 10 accepted? Yes! q 0 0 1 q 1 q 3 0 1 q 2 1 1 0
Example: What is δ({q 0 , q 1 }, 1)? δ({q 0  , q 1 }, 1) = δ({q 0 }, 1) U δ({q 1 }, 1)  = {q 0 } U {q 2 , q 3 } = {q 0 , q 2 , q 3 } What is δ({q 0 , q 2 }, 10)? δ({q 0  , q 2 }, 10) = δ(δ({q 0  , q 2 }, 1), 0) = δ(δ({q 0 }, 1) U δ({q 2 }, 1), 0) = δ({q 0 } U {q 3 }, 0) = δ({q 0 ,q 3 }, 0) = δ({q 0 }, 0) U δ({q 3 }, 0) = {q 1 , q 2 , q 3 } U {} = {q 1 , q 2 , q 3 }
Example: δ({q 0 }, 101) = δ(δ({q 0 }, 10), 1) = δ(δ(δ({q 0 }, 1), 0), 1) = δ(δ({q 0 }, 0), 1) = δ({q 1  , q 2 , q 3 }, 1) = δ({q 1 }, 1) U δ({q 2 }, 1) U δ({q 3 }, 1)  = {q 2 , q 3 } U {q 3 } U {} = {q 2 , q 3 } Is 101 accepted? Yes!
Definitions for NFAs Let M = (Q, Σ, δ,q 0 ,F) be an NFA and let w be in Σ * .  Then w is  accepted  by M iff δ({q 0 }, w) contains at least one state in F.  Let  M = (Q, Σ, δ,q 0 ,F)  be an NFA. Then the  language accepted  by M is the set: L(M) = {w | w is in Σ *  and δ({q 0 },w) contains at least one state in F}  Another equivalent definition: L(M) = {w | w is in Σ *  and w is accepted by M}
Equivalence of DFAs and NFAs Do DFAs and NFAs accept the same  class  of languages? Is there a language L that is accepted by a DFA, but not by any NFA? Is there a language L that is accepted by an NFA, but not by any DFA? Observation: Every DFA is an NFA. Therefore, if L is a regular language then there exists an NFA M such that L = L(M). It follows that NFAs accept all regular languages. But do NFAs accept more?
Consider the following DFA: 2 or more c’s Q = {q 0 , q 1 , q 2 } Σ = {a, b, c} Start state is q 0 F = {q 2 } δ: a b c   q 0   q 0    q 0   q 1   q 1    q 1   q 1   q 2   q 2   q 2   q 2   q 2 q 1 q 0 q 2 a b a b c c a/b/c
An Equivalent NFA: Q = {q 0 , q 1 , q 2 } Σ = {a, b, c} Start state is q 0 F = {q 2 } δ: a b c   q 0   {q 0 }     {q 0 }   {q 1 }   q 1    {q 1 }   {q 1 }   {q 2 }   q 2   {q 2 }   {q 2 }   {q 2 } q 1 q 0 q 2 a b a b c c a/b/c
Lemma 1:   Let M be an DFA.  Then there exists a NFA M’ such that L(M) = L(M’). Proof:  Every DFA is an NFA. Hence, if we let M’ = M, then it follows that L(M’) = L(M). The above is just a formal statement of the observation from the previous slide.
Lemma 2:   Let M be an NFA.  Then there exists a DFA M’ such that L(M) = L(M’). Proof:  (sketch) Let M = (Q, Σ, δ,q 0 ,F). Define a DFA M’ = (Q’, Σ, δ’,q ’ 0 ,F’) as: Q’ = 2 Q Each state in M’ corresponds to a   = {Q 0 , Q 1 ,…,} subset of states from M where Q u  = [q i0 , q i1 ,…q ij ] F’ = {Q u  | Q u  contains at least one state in F} q ’ 0  = [q 0 ] δ’(Q u , a) = Q v  iff δ(Q u , a) = Q v
Example: empty string or start and end with 0 Q = {q 0 , q 1 } Σ = {0, 1} Start state is q 0 F = {q 1 } δ: 0 1   q 0   q 1 q 1 0 0/1 0 { q 1 } {} {q 0 ,  q 1 } { q 1 } q 0
Construct DFA M’ as follows: δ({q 0 }, 0) = {q 1 } => δ’([q 0 ], 0) = [q 1 ] δ({q 0 }, 1) = {} => δ’([q 0 ], 1) = [ ] δ({q 1 }, 0) = {q 0 , q 1 } => δ’([q 1 ], 0) = [q 0 , q 1 ] δ({q 1 }, 1) = {q 1 } => δ’([q 1 ], 1) = [q 1 ] δ({q 0 , q 1 }, 0) = {q 0 , q 1 } => δ’([q 0 , q 1 ], 0) = [q 0 , q 1 ] δ({q 0 , q 1 }, 1) = {q 1 } => δ’([q 0 , q 1 ], 1) = [q 1 ] δ({}, 0) = {} => δ’([ ], 0) = [ ] δ({}, 1) = {} => δ’([ ], 1) = [ ] [ ] 1 0 1 [q 1 ] 0 0/1 1 0 [q 0 , q 1 ] [q 0 ]
Theorem:   Let L be a language.  Then there exists an DFA M  such that L = L(M) iff there exists an NFA M’ such that L = L(M’). Proof: (if) Suppose there exists an NFA M’ such that L = L(M’).  Then by Lemma 2 there exists an DFA M such that L = L(M). (only if) Suppose there exists an DFA M such that L = L(M).  Then by Lemma 1 there exists an NFA M’ such that L = L(M’). Corollary:  The NFAs define the regular languages.
Note: Suppose R = {} δ(R, 0) = δ(δ(R, ε), 0) = δ(R, 0) =  δ(q, 0) = {} Since R = {} Exercise - Convert the following NFA to a DFA: Q = {q 0 , q 1 , q 2 } δ: 0 1 Σ = {0, 1}  Start state is q 0 q 0 F = {q 0 } q 1 q 2 { q 0 , q 1 } {   } { q 1 } { q 2 } { q 2 } { q 2 }
NFAs with  ε  Moves An NFA-ε is a five-tuple: M = (Q, Σ, δ, q 0 , F) Q A  finite  set of states Σ A  finite  input alphabet q 0 The initial/starting state, q 0  is in Q F A set of final/accepting states, which is a subset of Q δ A transition function, which is a total function from Q x Σ U {ε} to 2 Q δ: (Q x ( Σ U {ε} )) – >  2 Q δ(q,s) -The set of all states p such that there is a    transition labeled a from q to p, where a    is in Σ U {ε} Sometimes referred to as an  NFA- ε other times, simply as an NFA.
Example: δ: 0 1  ε q 0 - A string w = w 1 w 2 …w n  is processed   as w = ε * w 1 ε * w 2 ε *   … ε * w n ε *   q 1 - Example: all computations on 00:   0  ε  0 q 2 q 0   q 0   q 1   q 2   : q 3 q 0 ε 0/1 q 2 1 0 q 1 0 q 3 ε 0 1 { q 0 } {   } { q 1 } { q 1 , q 2 } { q 0 , q 3 } { q 2 } { q 2 } { q 2 } {   } { } { } {   }
Informal Definitions Let M = (Q, Σ, δ,q 0 ,F) be an NFA-ε . A String w in Σ *  is  accepted  by M iff there exists a path in M from q 0  to a state in F labeled by w and zero or more  ε  transitions. The language accepted by M is the set of all strings from Σ *  that are accepted by M.
ε -closure Define ε-closure(q) to denote the set of all states reachable from q by zero or more ε transitions. Examples: (for the previous NFA) ε-closure(q 0 ) = {q 0 , q 1 , q 2 } ε-closure(q 2 ) = {q 2 } ε-closure(q 1 ) = {q 1 , q 2 } ε-closure(q 3 ) = {q 3 } ε-closure(q) can be extended to sets of states by defining: ε-closure(P) =  ε-closure(q) Examples: ε-closure({q 1 , q 2 }) = {q 1 , q 2 } ε-closure({q 0 , q 3 }) = {q 0 , q 1 , q 2 , q 3 }
Extension of  δ to Strings and Sets of States What we currently have: δ : (Q x (Σ U {ε})) – >  2 Q What we want (why?): δ : (2 Q  x Σ * ) – >  2 Q As before, we will do this in two steps, which will be slightly different from the book, and we will make use of the following NFA. q 0 ε 0/1 q 2 1 0 q 1 0 q 3 ε 0 1
Step #1: Given δ: (Q x (Σ U {ε})) – >  2 Q  define δ # : (2 Q  x (Σ U {ε})) – >  2 Q  as follows: 1) δ # (R, a) =  δ(q, a) for all subsets R of Q, and symbols a in Σ U {ε} Note that: δ # ({p},a) =  δ(q, a) by definition of δ # , rule #1 above   = δ(p, a) Hence, we can use δ for δ # δ({q 0 , q 2 }, 0) These now make sense, but previously δ({q 0 , q 1 , q 2 }, 0) they did not.
Examples: What is δ({q 0  , q 1 , q 2 }, 1)? δ({q 0  , q 1 , q 2 }, 1) = δ(q 0 , 1) U δ(q 1 , 1) U δ(q 2 , 1)    = { } U {q 0 , q 3 } U {q 2 }   = {q 0 , q 2 , q 3 } What is δ({q 0 , q 1 }, 0)? δ({q 0  , q 1 }, 0) = δ(q 0 , 0) U δ(q 1 , 0)  = {q 0 } U {q 1 , q 2 } = {q 0 , q 1 , q 2 }
Step #2: Given δ: (2 Q  x (Σ U {ε})) – >  2 Q  define δ ^ : (2 Q  x Σ * ) – >  2 Q  as follows: δ ^ (R,w) – The set of states M could be in after processing string w, having starting from any state in R. Formally: 2) δ ^ (R, ε) = ε-closure(R) - for any subset R of Q 3) δ ^ (R,wa) = ε-closure(δ(δ ^ (R,w), a)) - for any w in Σ * , a in Σ, and subset R of Q Can we use δ for δ ^ ?
Consider the following example: δ({q 0 }, 0) = {q 0 } δ ^ ({q 0 }, 0) = ε-closure(δ(δ ^ ({q 0 }, ε), 0)) By rule #3   = ε-closure(δ(ε-closure({q 0 }), 0)) By rule #2   = ε-closure(δ({q 0,  q 1 , q 2 }, 0)) By ε-closure   = ε-closure(δ(q 0 , 0) U δ(q 1 , 0) U δ(q 2 , 0)) By rule #1   = ε-closure({q 0 } U {q 1 , q 2 } U {q 2 })   = ε-closure({q 0 , q 1 , q 2 })   = ε-closure({q 0 }) U ε-closure({q 1 }) U ε-closure({q 2 })   = {q 0 , q 1 , q 2 } U {q 1 , q 2 } U {q 2 }   = {q 0 , q 1 , q 2 } So what is the difference? δ(q 0 , 0) - Processes 0 as a single symbol, without ε transitions. δ ^ (q 0  , 0) - Processes 0 using as many ε transitions as are possible.
Example: δ ^ ({q 0 }, 01) = ε-closure(δ(δ ^ ({q 0 }, 0), 1)) By rule #3   = ε-closure(δ({q 0 , q 1 , q 2 }), 1) Previous slide   = ε-closure(δ(q 0 , 1) U δ(q 1 , 1) U δ(q 2 , 1)) By rule #1   = ε-closure({ } U {q 0 , q 3 } U {q 2 })   = ε-closure({q 0 , q 2 , q 3 })   = ε-closure({q 0 }) U ε-closure({q 2 }) U ε-closure({q 3 })   = {q 0 , q 1 , q 2 } U {q 2 } U {q 3 }   = {q 0 , q 1 , q 2 , q 3 }
Definitions for NFA- ε Machines Let M = (Q, Σ, δ,q 0 ,F) be an NFA - ε and let w be in Σ * .  Then w is  accepted  by M iff δ ^ ({q 0 }, w) contains at least one state in F.  Let  M = (Q, Σ, δ,q 0 ,F)  be an NFA - ε. Then the  language accepted  by M is the set: L(M) = {w | w is in Σ *  and δ ^ ({q 0 },w) contains at least one state in F}  Another equivalent definition: L(M) = {w | w is in Σ *  and w is accepted by M}
Equivalence of NFAs and NFA- εs Do NFAs and NFA - ε machines accept the same  class  of languages? Is there a language L that is accepted by a NFA, but not by any NFA - ε ? Is there a language L that is accepted by an NFA - ε , but not by any DFA? Observation: Every NFA is an NFA - ε. Therefore, if L is a regular language then there exists an NFA - ε M such that L = L(M). It follows that NFA - ε machines accept all regular languages. But do NFA - ε machines accept more?
Lemma 1:   Let M be an NFA.  Then there exists a NFA - ε M’ such that L(M) = L(M’). Proof:  Every NFA is an NFA - ε. Hence, if we let M’ = M, then it follows that L(M’) = L(M). The above is just a formal statement of the observation from the previous slide.
Lemma 2:   Let M be an NFA - ε.  Then there exists a NFA M’ such that L(M) = L(M’). Proof:  (sketch) Let M = (Q, Σ, δ,q 0 ,F) be an NFA - ε. Define an NFA M’ = (Q, Σ, δ’,q 0 ,F’) as: F’ = F U {q 0 } if ε-closure(q 0 ) contains at least one state from F F’ = F otherwise   δ’(q, a) = δ ^ (q, a) - for all q in Q and a in Σ Notes: δ’: (Q x Σ) – >  2 Q  is a function M’ has the same state set, the same alphabet, and the same start state as M M’ has no  ε  transitions
Example: Step #1: Same state set as M q 0  is the starting state q 0 ε 0/1 q 2 1 0 q 1 0 q 3 ε 0 1 q 2 q 1 q 3 q 0
Example: Step #2: q 0  becomes a final state q 1 q 3 q 0 ε 0/1 q 2 1 0 q 1 0 q 3 ε 0 1 q 2 q 0
Example: Step #3: q 0 ε 0/1 1 0 q 1 0 q 3 ε 0 1 q 1 q 3 0 0 0 q 2 q 2 q 0
Example: Step #4: q 0 ε 0/1 1 0 q 1 0 q 3 ε 0 1 q 1 q 3 0/1 0/1 0/1 1 q 2 q 2 q 0
Example: Step #5: q 0 ε 0/1 1 0 q 1 0 q 3 ε 0 1 q 1 q 3 0/1 0/1 0/1 1 0 0 q 2 q 2 q 0
Example: Step #6: q 0 ε 0/1 1 0 q 1 0 q 3 ε 0 1 q 1 q 3 0/1 0/1 0/1 1 0/1 0/1 1 1 q 2 q 2 q 0
Example: Step #7: q 0 ε 0/1 1 0 q 1 0 q 3 ε 0 1 q 1 q 3 0/1 0/1 0/1 1 0/1 0/1 1 1 0 q 2 q 2 q 0
Example: Step #8: Done! q 0 ε 0/1 1 0 q 1 0 q 3 ε 0 1 q 1 q 3 0/1 0/1 0/1 1 0/1 0/1 1 1 0/1 q 2 q 2 q 0
Theorem:   Let L be a language.  Then there exists an NFA M  such that L= L(M) iff there exists an NFA - ε M’ such that L = L(M’). Proof: (if) Suppose there exists an NFA - ε M’ such that L = L(M’).  Then by Lemma 2 there exists an NFA M such that L = L(M). (only if) Suppose there exists an NFA M such that L = L(M).  Then by Lemma 1 there exists an NFA - ε M’ such that L = L(M’). Corollary:  The NFA-ε machines define the regular languages.

More Related Content

PDF
Finite automata
PPT
Lec 3 ---- dfa
PPTX
simple problem to convert NFA with epsilon to without epsilon
PPT
NFA or Non deterministic finite automata
PPT
Nfa egs
PPT
Dfa h11
PDF
Formal Languages and Automata Theory Unit 1
PPT
Nondeterministic Finite Automata
Finite automata
Lec 3 ---- dfa
simple problem to convert NFA with epsilon to without epsilon
NFA or Non deterministic finite automata
Nfa egs
Dfa h11
Formal Languages and Automata Theory Unit 1
Nondeterministic Finite Automata

What's hot (20)

PPT
Minimization of DFA
PPTX
NFA Converted to DFA , Minimization of DFA , Transition Diagram
PDF
NFA DFA Equivalence theorem
PDF
Nondeterministic Finite Automata
PPT
Finite automata(For college Seminars)
PPT
Nondeterministic Finite Automat
PPT
Nfa vs dfa
PPTX
NFA Non Deterministic Finite Automata by Mudasir khushik
PPT
Finite Automata
PPTX
1.3.2 non deterministic finite automaton
PPT
DFA Minimization
PPT
Finite automata examples
PPTX
Theory of Automata and formal languages unit 1
PPTX
Automata theory - Push Down Automata (PDA)
PDF
Ch2 finite automaton
PPTX
Finite Automata: Deterministic And Non-deterministic Finite Automaton (DFA)
PPTX
Toc(df avs nfa)avishek130650107020
PDF
Automata theory
PDF
Nfa to-dfa
Minimization of DFA
NFA Converted to DFA , Minimization of DFA , Transition Diagram
NFA DFA Equivalence theorem
Nondeterministic Finite Automata
Finite automata(For college Seminars)
Nondeterministic Finite Automat
Nfa vs dfa
NFA Non Deterministic Finite Automata by Mudasir khushik
Finite Automata
1.3.2 non deterministic finite automaton
DFA Minimization
Finite automata examples
Theory of Automata and formal languages unit 1
Automata theory - Push Down Automata (PDA)
Ch2 finite automaton
Finite Automata: Deterministic And Non-deterministic Finite Automaton (DFA)
Toc(df avs nfa)avishek130650107020
Automata theory
Nfa to-dfa
Ad

Viewers also liked (7)

PDF
Formal language & automata theory
PPTX
Theory of automata and formal language
PPT
0227 regularlanguages
PPTX
Theory of computation Lec3 dfa
PDF
Lecture: Automata
PPT
Theory of Automata
PPT
Finite automata
Formal language & automata theory
Theory of automata and formal language
0227 regularlanguages
Theory of computation Lec3 dfa
Lecture: Automata
Theory of Automata
Finite automata
Ad

Similar to Finite automata (20)

PPT
FiniteAutomata (1).ppt
PPT
FiniteAutomata.ppt
PPT
FiniteAutomata-DFA and NFA from Theory of Computation.ppt
PPT
Finite automata examples
PDF
PDF
Theory of Computation FSM Conversions and Problems
DOCX
Deterministic finite automata
PPTX
Unit 2 Pumping lemma Unit 2 Pumping lemma
PPTX
Theory of Automata(Formal Language) Lecture 3.pptx
PDF
Teori automata lengkap
PPTX
INTRODUTION Formal Language and Automatic Theory.pptx
PPT
Description of Finite Automata and Languages Accepted by Push Down Automata
PPT
Push Down Automata (PDA) Definition, Problems and State Diagrams including tr...
PDF
Graph representation of DFA’s Da
PDF
Nfa to-dfa
PPTX
symbolic_automata or Advanced Programming Practice.pptx
PPT
Turing Machine push down automata-examples
PDF
Theory of Computation Basics of Finite Acceptors
PPTX
Resumen material MIT
FiniteAutomata (1).ppt
FiniteAutomata.ppt
FiniteAutomata-DFA and NFA from Theory of Computation.ppt
Finite automata examples
Theory of Computation FSM Conversions and Problems
Deterministic finite automata
Unit 2 Pumping lemma Unit 2 Pumping lemma
Theory of Automata(Formal Language) Lecture 3.pptx
Teori automata lengkap
INTRODUTION Formal Language and Automatic Theory.pptx
Description of Finite Automata and Languages Accepted by Push Down Automata
Push Down Automata (PDA) Definition, Problems and State Diagrams including tr...
Graph representation of DFA’s Da
Nfa to-dfa
symbolic_automata or Advanced Programming Practice.pptx
Turing Machine push down automata-examples
Theory of Computation Basics of Finite Acceptors
Resumen material MIT

More from Rajesh Yaramadi (7)

PDF
Flat [ www.uandi star.org]
PDF
Dbms[uandi star.org]
PDF
Compute1 dfa
PDF
Co [uandi star.org]
PDF
Chapter2
PDF
9 a05407 formal_languages_automata_theor_yfr_2029
PDF
07 a0123 sustainable development briefing note
Flat [ www.uandi star.org]
Dbms[uandi star.org]
Compute1 dfa
Co [uandi star.org]
Chapter2
9 a05407 formal_languages_automata_theor_yfr_2029
07 a0123 sustainable development briefing note

Recently uploaded (20)

PDF
Empathic Computing: Creating Shared Understanding
PDF
August Patch Tuesday
PDF
Approach and Philosophy of On baking technology
PPTX
Group 1 Presentation -Planning and Decision Making .pptx
PPTX
Machine Learning_overview_presentation.pptx
PDF
Network Security Unit 5.pdf for BCA BBA.
PDF
Reach Out and Touch Someone: Haptics and Empathic Computing
PPTX
OMC Textile Division Presentation 2021.pptx
PDF
Accuracy of neural networks in brain wave diagnosis of schizophrenia
PPTX
TLE Review Electricity (Electricity).pptx
PPTX
TechTalks-8-2019-Service-Management-ITIL-Refresh-ITIL-4-Framework-Supports-Ou...
PPTX
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
PDF
Per capita expenditure prediction using model stacking based on satellite ima...
PDF
Mobile App Security Testing_ A Comprehensive Guide.pdf
PPTX
Tartificialntelligence_presentation.pptx
PDF
Mushroom cultivation and it's methods.pdf
PDF
Machine learning based COVID-19 study performance prediction
PDF
MIND Revenue Release Quarter 2 2025 Press Release
PPTX
A Presentation on Artificial Intelligence
PDF
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
Empathic Computing: Creating Shared Understanding
August Patch Tuesday
Approach and Philosophy of On baking technology
Group 1 Presentation -Planning and Decision Making .pptx
Machine Learning_overview_presentation.pptx
Network Security Unit 5.pdf for BCA BBA.
Reach Out and Touch Someone: Haptics and Empathic Computing
OMC Textile Division Presentation 2021.pptx
Accuracy of neural networks in brain wave diagnosis of schizophrenia
TLE Review Electricity (Electricity).pptx
TechTalks-8-2019-Service-Management-ITIL-Refresh-ITIL-4-Framework-Supports-Ou...
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
Per capita expenditure prediction using model stacking based on satellite ima...
Mobile App Security Testing_ A Comprehensive Guide.pdf
Tartificialntelligence_presentation.pptx
Mushroom cultivation and it's methods.pdf
Machine learning based COVID-19 study performance prediction
MIND Revenue Release Quarter 2 2025 Press Release
A Presentation on Artificial Intelligence
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf

Finite automata

  • 1. Deterministic Finite State Automata (DFA) …… .. One-way, infinite tape, broken into cells One-way, read-only tape head. Finite control, I.e., a program, containing the position of the read head, current symbol being scanned, and the current “state.” A string is placed on the tape, read head is positioned at the left end, and the DFA will read the string one symbol at a time until all symbols have been read. The DFA will then either accept or reject. Finite Control 0 1 1 0 0
  • 2. The finite control can be described by a transition diagram : Example #1: 1 0 0 1 1 q 0 q 0 q 1 q 0 q 0 q 0 One state is final/accepting, all others are rejecting. The above DFA accepts those strings that contain an even number of 0’s q 0 q 1 0 0 1 1
  • 3. Example #2: a c c c b accepted q 0 q 0 q 1 q 2 q 2 q 2 a a c rejected q 0 q 0 q 0 q 1 Accepts those strings that contain at least two c’s q 1 q 0 q 2 a b a b c c a/b/c
  • 4. Inductive Proof (sketch): Base: x a string with |x|=0. state will be q0 => rejected. Inductive hypothesis: |x|=k, rejected -in state q0 (x must have 0 c), rejected – in state q1 (x must have 1 c), accepted – in state q2 (x already with 2 c’s) Inductive step: String xp, for p = a, b and c q0 and, xa or xb : q0->q0 rejected, as should be (no c) q0 and, xc : q0 -> q1 rejected, as should be (1 c) q1 and xa or xb: q1 -> q1 rejected, … q1 and xc: q1-> q2 accepted, as should be ( 2 c’s now) q2 and xa, or xb, or xc: q2 -> q2 accepted, (no change)
  • 5. Formal Definition of a DFA A DFA is a five-tuple: M = (Q, Σ, δ, q 0 , F) Q A finite set of states Σ A finite input alphabet q 0 The initial/starting state, q 0 is in Q F A set of final/accepting states, which is a subset of Q δ A transition function, which is a total function from Q x Σ to Q δ: (Q x Σ) – > Q δ is defined for any q in Q and s in Σ, and δ(q,s) = q’ is equal to some state q’ in Q, could be q’=q Intuitively, δ(q,s) is the state entered by M after reading symbol s while in state q.
  • 6. For example #1: Q = {q 0 , q 1 } Σ = {0, 1} Start state is q 0 F = {q 0 } δ: 0 1 q 0 q 1 q 0 q 1 q 0 q 1 q 0 q 1 0 0 1 1
  • 7. For example #2: Q = {q 0 , q 1 , q 2 } Σ = {a, b, c} Start state is q 0 F = {q 2 } δ: a b c q 0 q 0 q 0 q 1 q 1 q 1 q 1 q 2 q 2 q 2 q 2 q 2 Since δ is a function, at each step M has exactly one option. It follows that for a given string, there is exactly one computation. q 1 q 0 q 2 a b a b c c a/b/c
  • 8. Extension of δ to Strings δ ^ : (Q x Σ * ) – > Q δ ^ (q,w) – The state entered after reading string w having started in state q. Formally: 1) δ ^ (q, ε) = q, and 2) For all w in Σ * and a in Σ δ ^ (q,wa) = δ (δ ^ (q,w), a)
  • 9. Recall Example #1: What is δ ^ (q 0 , 011)? Informally, it is the state entered by M after processing 011 having started in state q 0. Formally: δ ^ (q 0 , 011) = δ (δ ^ (q 0 ,01), 1) by rule #2 = δ (δ ( δ ^ (q 0 ,0), 1), 1) by rule #2 = δ (δ (δ (δ ^ (q 0 , λ), 0), 1), 1) by rule #2 = δ (δ (δ(q 0 ,0), 1), 1) by rule #1 = δ (δ (q 1 , 1), 1) by definition of δ = δ (q 1 , 1) by definition of δ = q 1 by definition of δ Is 011 accepted? No, since δ ^ (q 0 , 011) = q 1 is not a final state. q 0 q 1 0 0 1 1
  • 10. Note that: δ ^ (q,a) = δ(δ ^ (q, ε ), a) by definition of δ ^ , rule #2 = δ(q, a) by definition of δ ^ , rule #1 Therefore: δ ^ (q, a 1 a 2 …a n ) = δ(δ(…δ(δ(q, a1), a2)…), a n ) Hence, we can use δ in place of δ ^ : δ ^ (q, a 1 a 2 …a n ) = δ(q, a 1 a 2 …a n )
  • 11. Recall Example #2: What is δ(q 0 , 011)? Informally, it is the state entered by M after processing 011 having started in state q 0. Formally: δ(q 0 , 011) = δ (δ(q 0 ,01), 1) by rule #2 = δ (δ (δ(q 0 ,0), 1), 1) by rule #2 = δ (δ (q 1 , 1), 1) by definition of δ = δ (q 1 , 1) by definition of δ = q 1 by definition of δ Is 011 accepted? No, since δ(q 0 , 011) = q 1 is not a final state. q 1 q 0 1 1 0 0 1 0 q 2
  • 12. Recall Example #2: What is δ(q 1 , 10)? δ(q 1 , 10) = δ (δ(q 1 ,1), 0) by rule #2 = δ (q 1 , 0) by definition of δ = q 2 by definition of δ Is 10 accepted? No, since δ(q 0 , 10) = q 1 is not a final state. The fact that δ(q 1 , 10) = q 2 is irrelevant! 0 0 q 1 q 0 q 2 1 1 0 1
  • 13. Definitions for DFAs Let M = (Q, Σ, δ,q 0 ,F) be a DFA and let w be in Σ * . Then w is accepted by M iff δ(q 0 ,w) = p for some state p in F. Let M = (Q, Σ, δ,q 0 ,F) be a DFA. Then the language accepted by M is the set: L(M) = {w | w is in Σ * and δ(q 0 ,w) is in F} Another equivalent definition: L(M) = {w | w is in Σ * and w is accepted by M} Let L be a language. Then L is a regular language iff there exists a DFA M such that L = L(M). Let M 1 = (Q 1 , Σ 1 , δ 1 , q 0 , F 1 ) and M 2 = (Q 2 , Σ 2 , δ 2 , p 0 , F 2 ) be DFAs. Then M 1 and M 2 are equivalent iff L(M 1 ) = L(M 2 ).
  • 14. Notes: A DFA M = (Q, Σ, δ,q 0 ,F) partitions the set Σ * into two sets: L(M) and Σ * - L(M). If L = L(M) then L is a subset of L(M) and L(M) is a subset of L. Similarly, if L(M 1 ) = L(M 2 ) then L(M 1 ) is a subset of L(M 2 ) and L(M 2 ) is a subset of L(M 1 ). Some languages are regular, others are not. For example, if L 1 = {x | x is a string of 0's and 1's containing an even number of 1's} and L 2 = {x | x = 0 n 1 n for some n >= 0} then L 1 is regular but L 2 is not. Questions: How do we determine whether or not a given language is regular? How could a program “simulate” a DFA?
  • 15. Give a DFA M such that: L(M) = {x | x is a string of 0’s and 1’s and |x| >= 2} Prove this by induction q 1 q 0 q 2 0/1 0/1 0/1
  • 16. Give a DFA M such that: L(M) = {x | x is a string of (zero or more) a’s, b’s and c’s such that x does not contain the substring aa } q 2 q 0 a a/b/c a q 1 b/c b/c
  • 17. Give a DFA M such that: L(M) = {x | x is a string of a’s, b’s and c’s such that x contains the substring aba } q 2 q 0 a a/b/c b q 1 c b/c a b/c q 3 a
  • 18. Give a DFA M such that: L(M) = {x | x is a string of a’s and b’s such that x contains both aa and bb } q 0 b q 7 q 5 q 4 q 6 b b b a q 2 q 1 q 3 a a a b a/b b a a a b
  • 19. Let Σ = {0, 1}. Give DFAs for {}, {ε}, Σ * , and Σ + . For {}: For {ε}: For Σ * : For Σ + : 0/1 q 0 0/1 q 0 q 1 q 0 0/1 0/1 0/1 q 0 q 1 0/1
  • 20. Nondeterministic Finite State Automata (NFA) An NFA is a five-tuple: M = (Q, Σ, δ, q 0 , F) Q A finite set of states Σ A finite input alphabet q 0 The initial/starting state, q 0 is in Q F A set of final/accepting states, which is a subset of Q δ A transition function, which is a total function from Q x Σ to 2 Q δ: (Q x Σ) – > 2 Q -2 Q is the power set of Q, the set of all subsets of Q δ(q,s) -The set of all states p such that there is a transition labeled s from q to p δ(q,s) is a function from Q x S to 2 Q (but not to Q)
  • 21. Example #1: some 0’s followed by some 1’s Q = {q 0 , q 1 , q 2 } Σ = {0, 1} Start state is q 0 F = {q 2 } δ: 0 1 q 0 q 1 q 2 q 1 q 0 0 1 0 1 0/1 { q 0 , q 1 } {} {} { q 1 , q 2 } { q 2 } { q 2 } q 2
  • 22. Example #2: pair os 0’s or pair of 1’s Q = {q 0 , q 1 , q 2 , q 3 , q 4 } Σ = {0, 1} Start state is q 0 F = {q 2 , q 4 } δ: 0 1 q 0 q 1 q 2 q 3 q 4 1 { q 0 , q 3 } { q 0 , q 1 } {} { q 2 } { q 2 } { q 2 } { q 4 } {} { q 4 } { q 4 } q 0 0/1 0 0 q 3 q 4 0/1 q 1 q 2 0/1 1
  • 23. Notes: δ(q,s) may not be defined for some q and s (why?). Informally, a string is said to be accepted if there exists a path to some state in F. The language accepted by an NFA is the set of all accepted strings. Question: How does an NFA find the correct/accepting path for a given string? NFAs are a non-intuitive computing model. We are primarily interested in NFAs as language defining devices, i.e., do NFAs accept languages that DFAs do not? Other questions are secondary, including practical questions such as whether or not there is an algorithm for finding an accepting path through an NFA for a given string,
  • 24. Determining if a given NFA (example #2) accepts a given string (001) can be done algorithmically: q 0 q 0 q 0 q 0 q 3 q 3 q 1 q 4 q 4 accepted Each level will have at most n states 0 0 1
  • 25. Another example (010): q 0 q 0 q 0 q 0 q 3 q 1 q 3 not accepted All paths have been explored, and none lead to an accepting state. 0 1 0
  • 26. Let Σ = {a, b, c}. Give an NFA M that accepts: L = {x | x is in Σ * and x contains ab} Is L a subset of L(M)? Is L(M) a subset of L? Is an NFA necessary? Could a DFA accept L? Try and give an equivalent DFA as an exercise. Designing NFAs is not a typical task. q 1 q 0 q 2 a a/b/c b a/b/c
  • 27. Let Σ = {a, b}. Give an NFA M that accepts: L = {x | x is in Σ * and the third to the last symbol in x is b} Is L a subset of L(M)? Is L(M) a subset of L? Give an equivalent DFA as an exercise. q 1 q 0 b q 3 a/b a/b q 2 a/b
  • 28. Extension of δ to Strings and Sets of States What we currently have: δ : (Q x Σ) – > 2 Q What we want (why?): δ : (2 Q x Σ * ) – > 2 Q We will do this in two steps, which will be slightly different from the book, and we will make use of the following NFA. q 0 0 1 q 1 q 4 q 3 0 1 q 2 0 0 1 0 0
  • 29. Extension of δ to Strings and Sets of States Step #1: Given δ: (Q x Σ) – > 2 Q define δ # : (2 Q x Σ) – > 2 Q as follows: 1) δ # (R, a) = δ(q, a) for all subsets R of Q, and symbols a in Σ Note that: δ # ({p},a) = δ(q, a) by definition of δ # , rule #1 above = δ(p, a) Hence, we can use δ for δ # δ({q 0 , q 2 }, 0) These now make sense, but previously δ({q 0 , q 1 , q 2 }, 0) they did not.
  • 30. Example: δ({q 0 , q 2 }, 0) = δ(q 0 , 0) U δ(q 2 , 0) = {q 1 , q 3 } U {q 3 , q 4 } = {q 1 , q 3 , q 4 } δ({q 0 , q 1 , q 2 }, 1) = δ(q 0 , 1) U δ(q 1 , 1) U δ(q 2 , 1) = {} U {q 2 , q 3 } U {} = {q 2 , q 3 }
  • 31. Step #2: Given δ: (2 Q x Σ) – > 2 Q define δ ^ : (2 Q x Σ*) – > 2 Q as follows: δ ^ (R,w) – The set of states M could be in after processing string w, having starting from any state in R. Formally: 2) δ ^ (R, ε) = R for any subset R of Q 3) δ ^ (R,wa) = δ (δ ^ (R,w), a) for any w in Σ * , a in Σ, and subset R of Q Note that: δ ^ (R, a) = δ(δ ^ (R, ε), a) by definition of δ ^ , rule #3 above = δ(R, a) by definition of δ ^ , rule #2 above Hence, we can use δ for δ ^ δ({q 0 , q 2 }, 0110) These now make sense, but previously δ({q 0 , q 1 , q 2 }, 101101) they did not.
  • 32. Example: What is δ({q 0 }, 10)? Informally: The set of states the NFA could be in after processing 10, having started in state q 0 , i.e., {q 1 , q 2 , q 3 }. Formally: δ({q 0 }, 10) = δ(δ({q 0 }, 1), 0) = δ({q 0 }, 0) = {q 1 , q 2 , q 3 } Is 10 accepted? Yes! q 0 0 1 q 1 q 3 0 1 q 2 1 1 0
  • 33. Example: What is δ({q 0 , q 1 }, 1)? δ({q 0 , q 1 }, 1) = δ({q 0 }, 1) U δ({q 1 }, 1) = {q 0 } U {q 2 , q 3 } = {q 0 , q 2 , q 3 } What is δ({q 0 , q 2 }, 10)? δ({q 0 , q 2 }, 10) = δ(δ({q 0 , q 2 }, 1), 0) = δ(δ({q 0 }, 1) U δ({q 2 }, 1), 0) = δ({q 0 } U {q 3 }, 0) = δ({q 0 ,q 3 }, 0) = δ({q 0 }, 0) U δ({q 3 }, 0) = {q 1 , q 2 , q 3 } U {} = {q 1 , q 2 , q 3 }
  • 34. Example: δ({q 0 }, 101) = δ(δ({q 0 }, 10), 1) = δ(δ(δ({q 0 }, 1), 0), 1) = δ(δ({q 0 }, 0), 1) = δ({q 1 , q 2 , q 3 }, 1) = δ({q 1 }, 1) U δ({q 2 }, 1) U δ({q 3 }, 1) = {q 2 , q 3 } U {q 3 } U {} = {q 2 , q 3 } Is 101 accepted? Yes!
  • 35. Definitions for NFAs Let M = (Q, Σ, δ,q 0 ,F) be an NFA and let w be in Σ * . Then w is accepted by M iff δ({q 0 }, w) contains at least one state in F. Let M = (Q, Σ, δ,q 0 ,F) be an NFA. Then the language accepted by M is the set: L(M) = {w | w is in Σ * and δ({q 0 },w) contains at least one state in F} Another equivalent definition: L(M) = {w | w is in Σ * and w is accepted by M}
  • 36. Equivalence of DFAs and NFAs Do DFAs and NFAs accept the same class of languages? Is there a language L that is accepted by a DFA, but not by any NFA? Is there a language L that is accepted by an NFA, but not by any DFA? Observation: Every DFA is an NFA. Therefore, if L is a regular language then there exists an NFA M such that L = L(M). It follows that NFAs accept all regular languages. But do NFAs accept more?
  • 37. Consider the following DFA: 2 or more c’s Q = {q 0 , q 1 , q 2 } Σ = {a, b, c} Start state is q 0 F = {q 2 } δ: a b c q 0 q 0 q 0 q 1 q 1 q 1 q 1 q 2 q 2 q 2 q 2 q 2 q 1 q 0 q 2 a b a b c c a/b/c
  • 38. An Equivalent NFA: Q = {q 0 , q 1 , q 2 } Σ = {a, b, c} Start state is q 0 F = {q 2 } δ: a b c q 0 {q 0 } {q 0 } {q 1 } q 1 {q 1 } {q 1 } {q 2 } q 2 {q 2 } {q 2 } {q 2 } q 1 q 0 q 2 a b a b c c a/b/c
  • 39. Lemma 1: Let M be an DFA. Then there exists a NFA M’ such that L(M) = L(M’). Proof: Every DFA is an NFA. Hence, if we let M’ = M, then it follows that L(M’) = L(M). The above is just a formal statement of the observation from the previous slide.
  • 40. Lemma 2: Let M be an NFA. Then there exists a DFA M’ such that L(M) = L(M’). Proof: (sketch) Let M = (Q, Σ, δ,q 0 ,F). Define a DFA M’ = (Q’, Σ, δ’,q ’ 0 ,F’) as: Q’ = 2 Q Each state in M’ corresponds to a = {Q 0 , Q 1 ,…,} subset of states from M where Q u = [q i0 , q i1 ,…q ij ] F’ = {Q u | Q u contains at least one state in F} q ’ 0 = [q 0 ] δ’(Q u , a) = Q v iff δ(Q u , a) = Q v
  • 41. Example: empty string or start and end with 0 Q = {q 0 , q 1 } Σ = {0, 1} Start state is q 0 F = {q 1 } δ: 0 1 q 0 q 1 q 1 0 0/1 0 { q 1 } {} {q 0 , q 1 } { q 1 } q 0
  • 42. Construct DFA M’ as follows: δ({q 0 }, 0) = {q 1 } => δ’([q 0 ], 0) = [q 1 ] δ({q 0 }, 1) = {} => δ’([q 0 ], 1) = [ ] δ({q 1 }, 0) = {q 0 , q 1 } => δ’([q 1 ], 0) = [q 0 , q 1 ] δ({q 1 }, 1) = {q 1 } => δ’([q 1 ], 1) = [q 1 ] δ({q 0 , q 1 }, 0) = {q 0 , q 1 } => δ’([q 0 , q 1 ], 0) = [q 0 , q 1 ] δ({q 0 , q 1 }, 1) = {q 1 } => δ’([q 0 , q 1 ], 1) = [q 1 ] δ({}, 0) = {} => δ’([ ], 0) = [ ] δ({}, 1) = {} => δ’([ ], 1) = [ ] [ ] 1 0 1 [q 1 ] 0 0/1 1 0 [q 0 , q 1 ] [q 0 ]
  • 43. Theorem: Let L be a language. Then there exists an DFA M such that L = L(M) iff there exists an NFA M’ such that L = L(M’). Proof: (if) Suppose there exists an NFA M’ such that L = L(M’). Then by Lemma 2 there exists an DFA M such that L = L(M). (only if) Suppose there exists an DFA M such that L = L(M). Then by Lemma 1 there exists an NFA M’ such that L = L(M’). Corollary: The NFAs define the regular languages.
  • 44. Note: Suppose R = {} δ(R, 0) = δ(δ(R, ε), 0) = δ(R, 0) = δ(q, 0) = {} Since R = {} Exercise - Convert the following NFA to a DFA: Q = {q 0 , q 1 , q 2 } δ: 0 1 Σ = {0, 1} Start state is q 0 q 0 F = {q 0 } q 1 q 2 { q 0 , q 1 } { } { q 1 } { q 2 } { q 2 } { q 2 }
  • 45. NFAs with ε Moves An NFA-ε is a five-tuple: M = (Q, Σ, δ, q 0 , F) Q A finite set of states Σ A finite input alphabet q 0 The initial/starting state, q 0 is in Q F A set of final/accepting states, which is a subset of Q δ A transition function, which is a total function from Q x Σ U {ε} to 2 Q δ: (Q x ( Σ U {ε} )) – > 2 Q δ(q,s) -The set of all states p such that there is a transition labeled a from q to p, where a is in Σ U {ε} Sometimes referred to as an NFA- ε other times, simply as an NFA.
  • 46. Example: δ: 0 1 ε q 0 - A string w = w 1 w 2 …w n is processed as w = ε * w 1 ε * w 2 ε * … ε * w n ε * q 1 - Example: all computations on 00: 0 ε 0 q 2 q 0 q 0 q 1 q 2 : q 3 q 0 ε 0/1 q 2 1 0 q 1 0 q 3 ε 0 1 { q 0 } { } { q 1 } { q 1 , q 2 } { q 0 , q 3 } { q 2 } { q 2 } { q 2 } { } { } { } { }
  • 47. Informal Definitions Let M = (Q, Σ, δ,q 0 ,F) be an NFA-ε . A String w in Σ * is accepted by M iff there exists a path in M from q 0 to a state in F labeled by w and zero or more ε transitions. The language accepted by M is the set of all strings from Σ * that are accepted by M.
  • 48. ε -closure Define ε-closure(q) to denote the set of all states reachable from q by zero or more ε transitions. Examples: (for the previous NFA) ε-closure(q 0 ) = {q 0 , q 1 , q 2 } ε-closure(q 2 ) = {q 2 } ε-closure(q 1 ) = {q 1 , q 2 } ε-closure(q 3 ) = {q 3 } ε-closure(q) can be extended to sets of states by defining: ε-closure(P) = ε-closure(q) Examples: ε-closure({q 1 , q 2 }) = {q 1 , q 2 } ε-closure({q 0 , q 3 }) = {q 0 , q 1 , q 2 , q 3 }
  • 49. Extension of δ to Strings and Sets of States What we currently have: δ : (Q x (Σ U {ε})) – > 2 Q What we want (why?): δ : (2 Q x Σ * ) – > 2 Q As before, we will do this in two steps, which will be slightly different from the book, and we will make use of the following NFA. q 0 ε 0/1 q 2 1 0 q 1 0 q 3 ε 0 1
  • 50. Step #1: Given δ: (Q x (Σ U {ε})) – > 2 Q define δ # : (2 Q x (Σ U {ε})) – > 2 Q as follows: 1) δ # (R, a) = δ(q, a) for all subsets R of Q, and symbols a in Σ U {ε} Note that: δ # ({p},a) = δ(q, a) by definition of δ # , rule #1 above = δ(p, a) Hence, we can use δ for δ # δ({q 0 , q 2 }, 0) These now make sense, but previously δ({q 0 , q 1 , q 2 }, 0) they did not.
  • 51. Examples: What is δ({q 0 , q 1 , q 2 }, 1)? δ({q 0 , q 1 , q 2 }, 1) = δ(q 0 , 1) U δ(q 1 , 1) U δ(q 2 , 1) = { } U {q 0 , q 3 } U {q 2 } = {q 0 , q 2 , q 3 } What is δ({q 0 , q 1 }, 0)? δ({q 0 , q 1 }, 0) = δ(q 0 , 0) U δ(q 1 , 0) = {q 0 } U {q 1 , q 2 } = {q 0 , q 1 , q 2 }
  • 52. Step #2: Given δ: (2 Q x (Σ U {ε})) – > 2 Q define δ ^ : (2 Q x Σ * ) – > 2 Q as follows: δ ^ (R,w) – The set of states M could be in after processing string w, having starting from any state in R. Formally: 2) δ ^ (R, ε) = ε-closure(R) - for any subset R of Q 3) δ ^ (R,wa) = ε-closure(δ(δ ^ (R,w), a)) - for any w in Σ * , a in Σ, and subset R of Q Can we use δ for δ ^ ?
  • 53. Consider the following example: δ({q 0 }, 0) = {q 0 } δ ^ ({q 0 }, 0) = ε-closure(δ(δ ^ ({q 0 }, ε), 0)) By rule #3 = ε-closure(δ(ε-closure({q 0 }), 0)) By rule #2 = ε-closure(δ({q 0, q 1 , q 2 }, 0)) By ε-closure = ε-closure(δ(q 0 , 0) U δ(q 1 , 0) U δ(q 2 , 0)) By rule #1 = ε-closure({q 0 } U {q 1 , q 2 } U {q 2 }) = ε-closure({q 0 , q 1 , q 2 }) = ε-closure({q 0 }) U ε-closure({q 1 }) U ε-closure({q 2 }) = {q 0 , q 1 , q 2 } U {q 1 , q 2 } U {q 2 } = {q 0 , q 1 , q 2 } So what is the difference? δ(q 0 , 0) - Processes 0 as a single symbol, without ε transitions. δ ^ (q 0 , 0) - Processes 0 using as many ε transitions as are possible.
  • 54. Example: δ ^ ({q 0 }, 01) = ε-closure(δ(δ ^ ({q 0 }, 0), 1)) By rule #3 = ε-closure(δ({q 0 , q 1 , q 2 }), 1) Previous slide = ε-closure(δ(q 0 , 1) U δ(q 1 , 1) U δ(q 2 , 1)) By rule #1 = ε-closure({ } U {q 0 , q 3 } U {q 2 }) = ε-closure({q 0 , q 2 , q 3 }) = ε-closure({q 0 }) U ε-closure({q 2 }) U ε-closure({q 3 }) = {q 0 , q 1 , q 2 } U {q 2 } U {q 3 } = {q 0 , q 1 , q 2 , q 3 }
  • 55. Definitions for NFA- ε Machines Let M = (Q, Σ, δ,q 0 ,F) be an NFA - ε and let w be in Σ * . Then w is accepted by M iff δ ^ ({q 0 }, w) contains at least one state in F. Let M = (Q, Σ, δ,q 0 ,F) be an NFA - ε. Then the language accepted by M is the set: L(M) = {w | w is in Σ * and δ ^ ({q 0 },w) contains at least one state in F} Another equivalent definition: L(M) = {w | w is in Σ * and w is accepted by M}
  • 56. Equivalence of NFAs and NFA- εs Do NFAs and NFA - ε machines accept the same class of languages? Is there a language L that is accepted by a NFA, but not by any NFA - ε ? Is there a language L that is accepted by an NFA - ε , but not by any DFA? Observation: Every NFA is an NFA - ε. Therefore, if L is a regular language then there exists an NFA - ε M such that L = L(M). It follows that NFA - ε machines accept all regular languages. But do NFA - ε machines accept more?
  • 57. Lemma 1: Let M be an NFA. Then there exists a NFA - ε M’ such that L(M) = L(M’). Proof: Every NFA is an NFA - ε. Hence, if we let M’ = M, then it follows that L(M’) = L(M). The above is just a formal statement of the observation from the previous slide.
  • 58. Lemma 2: Let M be an NFA - ε. Then there exists a NFA M’ such that L(M) = L(M’). Proof: (sketch) Let M = (Q, Σ, δ,q 0 ,F) be an NFA - ε. Define an NFA M’ = (Q, Σ, δ’,q 0 ,F’) as: F’ = F U {q 0 } if ε-closure(q 0 ) contains at least one state from F F’ = F otherwise δ’(q, a) = δ ^ (q, a) - for all q in Q and a in Σ Notes: δ’: (Q x Σ) – > 2 Q is a function M’ has the same state set, the same alphabet, and the same start state as M M’ has no ε transitions
  • 59. Example: Step #1: Same state set as M q 0 is the starting state q 0 ε 0/1 q 2 1 0 q 1 0 q 3 ε 0 1 q 2 q 1 q 3 q 0
  • 60. Example: Step #2: q 0 becomes a final state q 1 q 3 q 0 ε 0/1 q 2 1 0 q 1 0 q 3 ε 0 1 q 2 q 0
  • 61. Example: Step #3: q 0 ε 0/1 1 0 q 1 0 q 3 ε 0 1 q 1 q 3 0 0 0 q 2 q 2 q 0
  • 62. Example: Step #4: q 0 ε 0/1 1 0 q 1 0 q 3 ε 0 1 q 1 q 3 0/1 0/1 0/1 1 q 2 q 2 q 0
  • 63. Example: Step #5: q 0 ε 0/1 1 0 q 1 0 q 3 ε 0 1 q 1 q 3 0/1 0/1 0/1 1 0 0 q 2 q 2 q 0
  • 64. Example: Step #6: q 0 ε 0/1 1 0 q 1 0 q 3 ε 0 1 q 1 q 3 0/1 0/1 0/1 1 0/1 0/1 1 1 q 2 q 2 q 0
  • 65. Example: Step #7: q 0 ε 0/1 1 0 q 1 0 q 3 ε 0 1 q 1 q 3 0/1 0/1 0/1 1 0/1 0/1 1 1 0 q 2 q 2 q 0
  • 66. Example: Step #8: Done! q 0 ε 0/1 1 0 q 1 0 q 3 ε 0 1 q 1 q 3 0/1 0/1 0/1 1 0/1 0/1 1 1 0/1 q 2 q 2 q 0
  • 67. Theorem: Let L be a language. Then there exists an NFA M such that L= L(M) iff there exists an NFA - ε M’ such that L = L(M’). Proof: (if) Suppose there exists an NFA - ε M’ such that L = L(M’). Then by Lemma 2 there exists an NFA M such that L = L(M). (only if) Suppose there exists an NFA M such that L = L(M). Then by Lemma 1 there exists an NFA - ε M’ such that L = L(M’). Corollary: The NFA-ε machines define the regular languages.