1 / 66

Lecture Four: Nondeterministic Finite Automata

Lecture Four: Nondeterministic Finite Automata. Amjad Ali. A DFA has exactly one transition from every state on every symbol in the alphabet. By relaxing this requirement we get a related but more flexible kind of automaton: the nondeterministic finite automaton or NFA.

adamma
Télécharger la présentation

Lecture Four: Nondeterministic Finite Automata

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Lecture Four:Nondeterministic Finite Automata Amjad Ali

  2. A DFA has exactly one transition from every state on every symbol in the alphabet. By relaxing this requirement we get a related but more flexible kind of automaton: the nondeterministic finite automatonor NFA. NFAs are a bit harder to think about than DFAs, because they do not appear to define simple computational processes. They may seem at first to be unnatural, like puzzles invented by professors for the torment of students. But have patience! NFAs and other kinds of nondeterministic automata arise naturally in many ways, and they too have a variety of practical applications.

  3. Not A DFA • Does not have exactly one transition from every state on every symbol: • Two transitions from q0 on a • No transition from q0 (on either a or b) • Though not a DFA, this can be taken as defining a language, in a slightly different way

  4. Possible Sequences of Moves • We'll consider all possible sequences of moves the machine might make for a given string • For example, on the string aa there are three: • From q0 to q0 to q0, rejecting • From q0 to q0 to q1, accepting • From q0 to q1, getting stuck on the last a • Our convention for this new kind of machine: a string is in L(M) if there is at least one accepting sequence

  5. Nondeterministic Finite Automaton (NFA) • L(M) = the set of strings that have at least one accepting sequence • In the example above, L(M) = {xa | x {a,b}*} • A DFA is a special case of an NFA: • An NFA that happens to be deterministic: there is exactly one transition from every state on every symbol • So there is exactly one possible sequence for every string • NFA is not necessarily deterministic

  6. Definitions for NFAs • Let N = (Q, Σ, δ, q0, F) be an NFA and let w be in Σ*. Then w is accepted by N iff δ({q0}, w) contains at least one state in F. • Let N = (Q, Σ, δ, q0, F) be an NFA. Then the language accepted by N is the set: L(N) = {w | w is in Σ* and δ({q0},w) contains at least one state in F} • Another equivalent definition: L(N) = {w | w is in Σ* and w is accepted by N} NFA, Lecture 4, slide 6

  7. NFA Advantage • An NFA for a language can be smaller and easier to construct than a DFA • Strings whose next-to-last symbol is 1: DFA: NFA:

  8. Spontaneous Transitions • An NFA can make a state transition spontaneously, without consuming an input symbol • Shown as an arrow labeled with  • For example, {a}*  {b}*:

  9. -Transitions To Accepting States • An -transition can be made at any time • For example, there are three sequences on the empty string • No moves, ending in q0, rejecting • From q0 to q1, accepting • From q0 to q2, accepting • Any state with an -transition to an accepting state ends up working like an accepting state too

  10. -transitions For NFA Combining • -transitions are useful for combining smaller automata into larger ones • This machine combines a machine for {a}* and a machine for {b}* • It uses an -transition at the start to achieve the union of the two languages

  11. Incorrect Union A = {an| n is odd} B = {bn| n is odd} A B ? No: this NFA accepts aab

  12. Correct Union A = {an| n is odd} B = {bn| n is odd} A B

  13. Incorrect Concatenation A = {an| n is odd} B = {bn| n is odd} {xy | x A and y B}? No: this NFA accepts abbaab

  14. Correct Concatenation A = {an| n is odd} B = {bn| n is odd} {xy | x A and y B}

  15. Nondeterminism • The essence of nondeterminism: • For a given input there can be more than one legal sequence of steps • The input is in the language if at least one of the legal sequences says so • We can achieve the same result by deterministically searching the legal sequences, but… • ...this nondeterminism does not directly correspond to anything in physical computer systems • In spite of that, NFAs have many practical applications

  16. Formal Definition of Nondeterministic Finite Automata • An NFA is a five-tuple: N = (Q, Σ, δ, q0, F) Q A finite set of states Σ A finite input alphabet q0 The initial/starting state, q0 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 2Q δ: (Q x Σ) → P(Q) -P(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 P(Q) (but not to Q)

  17. 0/1 0/1 0 0 q0 q3 1 0/1 q4 q2 q1 • Example : Formal Definition of NFA given below: (pair of 0’s or pair of 1’s) Q = {q0, q1, q2 , q3 , q4} Σ = {0, 1} Start state is q0 F = {q2, q4} δ: 0 1 q0 q1 q2 q3 q4 1

  18. 0/1 0/1 0 0 q0 q3 0 1 0 1 0/1 q4 q2 q1 1 Example: Possible Sequences of 001 • Determining if a given NFA accepts a given string (001) can be done algorithmically: q0 q0q0q0 q3 q3(stuck) q1 q4q4accepted • Each level will have at most n states

  19. Formal Definition of Computation (NFA) The * Function: • What we currently have: δ : (Q x Σ) → P(Q) • What we want (why?): δ : (P(Q) x Σ*) → P(Q) δ*(R, ε) = R δ*(R, xa) = δ (δ*(R, x), a) - for any subset R of Q, for any w=xa in Σ*, a in Σ, and subset R of Q. and δ(R, a) = δ(q, a) for all subsets R of Q, and symbols a in Σ

  20. 1 0 0 1 q0 q1 q2 0 1 q3 1 • Example: What is δ*(q0, 011) ? Is 011 Accepted? δ*({q0}, 011) = δ(δ*({q0 }, 01), 1) = δ(δ(δ*({q0 }, 0), 1), 1) = δ(δ(δ(δ*({q0 }, ε), 0), 1), 1) = δ(δ(δ({q0 }, 0), 1), 1) = δ(δ({q1, q2, q3 }, 1), 1) = δ(δ({q1}, 1) U δ({q2}, 1) U δ({q3}, 1), 1) = δ(({q2 ,q3} U {q3} U {}), 1) = δ({q2 ,q3}, 1) = δ({q2}, 1) U δ({q3} , 1) = {q3} U {} = {q3} Is 011 accepted? Yes, since δ*({q0}, 011) = {q3 } is a final state.

  21. 1 0 0 1 1 1 0 q0 q1 q2 0 1 q3 1 • Example: Possible Sequences of 011 q0 q1 q2 q3 (Accepted) q2 q3 (Stuck) q3(Stuck) q3 (Stuck)

  22. Definitions for NFAs • Let N = (Q, Σ, δ,q0,F) be an NFA and let w be in Σ*. Then w is accepted by N iffδ*({q0}, w) contains at least one state in F. • Let N = (Q, Σ, δ,q0,F) be an NFA. Then the language accepted by N is the set: L(N) = {w | w is in Σ* and δ*({q0},w) contains at least one state in F} • Another equivalent definition: L(N) = {w | w is in Σ* and w is accepted by N}

  23. The Language An NFA Defines For any NFA N= (Q, , , q0, F), L(N) denotes the language accepted by Nwhich is L(N) = {x* | *(q0, x)  F  {}}.

  24. ε-Non-deterministic Finite Automate(NFA-ε)

  25. Formal Definition of NFAs with ε Moves • An NFA-ε is a five-tuple: N = (Q, Σ, δ, q0, F) Q A finite set of states Σ A finite input alphabet q0 The initial/starting state, q0 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 P(Q) δ: (Q x (Σ U {ε})) → P(Q) • A String w in Σ* is accepted by NFA iff there exists a path in NFA from q0 to a state in F labeled by w and zero or more ε transitions. • Sometimes referred to as an NFA-ε other times, simply as an NFA.

  26. q3 1 q2 0 0 0/1 ε ε q0 q1 0 1 Example: Formally: Q = {q0, q1, q2 , q3 } Σ = {0, 1} Start state is q0 F = {q2, q4} δ: δ: (Q x (Σ U {ε})) → P(Q) 0 1 ε q0 q1 q2 q3

  27. Formal Definition of Computation (NFA- ε) The * Function: • What we currently have: δ : (Q x (Σ U {ε})) → P(Q) • What we want (why?): δ : (P(Q) x Σ*) → P(Q) δ*(R, ε) = ε-closure(R) - for any subset R of Q δ*(R, xa) = ε-closure(δ(δ*(R,x), a)) - for any w=xa in Σ*, a in Σ, and subset R of Q and δ(R, a) = δ(q, a) for all subsets R of Q, and symbols a in Σ

  28. q3 1 q2 0 0 0/1 ε ε q0 q1 0 1 Example: Computation of ε-closure • Define ε-closure(q) to denote the set of all states reachable from q by zero or more ε transitions. ε-closure(q0) = {q0, q1, q2} ε-closure(q1) = {q1, q2} ε-closure(q2) = {q2} ε-closure(q3) = {q3} • ε-closure(q) can be extended to sets of states by defining: ε-closure(P) = ε-closure(q) ε-closure({q1, q2}) = ε-closure(q1) U ε-closure(q2) = {q1, q2} U {q2}= {q1, q2} ε-closure({q0, q3}) = ε-closure(q0) U ε-closure(q3) = {q0, q1, q2} U {q3}={q0, q1, q2, q3}

  29. q3 1 q2 0 0 0/1 ε ε q0 q1 0 1 Example: What is δ*(q0, 010) ? Is 010 Accepted? δ*({q0}, 010) = E(δ(δ*({q0 }, 01), 0)) = E(δ(E(δ(δ*({q0 }, 0), 1)), 0)) = E(δ(E(δ(E(δ(δ*({q0 }, ε), 0)), 1)), 0)) = E(δ(E(δ(E(δ(E({q0 }), 0)), 1)), 0)) = E(δ(E(δ(E(δ({q0, q1, q2}, 0)), 1)), 0)) = E(δ(E(δ(E(δ({q0}, 0) U δ({q1}, 0) U δ({q2}, 0)), 1)), 0)) = E(δ(E(δ(E({q0} U {q2} U {q2}), 1)), 0)) = E(δ(E(δ(E({q0, q2})), 1)), 0)) = E(δ(E(δ( (E({q0 }) U E({q2})) , 1)), 0)) = E(δ(E(δ( ({q0, q1, q2 } U {q2} ) , 1)), 0)) = E(δ(E(δ( {q0, q1, q2 } , 1)), 0))

  30. q3 1 q2 0 0 0/1 ε ε q0 q1 0 1 = E(δ(E(δ( {q0, q1, q2 } , 1)), 0)) = E(δ(E((δ({q0}, 1) U δ({q1}, 1) U δ({q2},1)), 0)) = E(δ(E( {} U {q0, q3} U {q2} ), 0)) = E(δ(E( {q0, q2 , q3} ), 0)) = E(δ( (E({q0}) U E({q2}) U E({q3})) , 0)) = E(δ( ({q0 , q1 , q2} U {q2} U {q3} ) , 0)) = E(δ( {q0 , q1 , q2 , q3} , 0)) = E( ( δ({q0}, 0) U δ({q1}, 0) U δ({q2}, 0) U δ({q3}, 0)) ) = E( ( {q0} U {q1, q2} U {q2} U {} ) ) = E( {q0 , q1, q2 } ) = E({q0}) U E({q1}) U E({q2}) = {q0 , q1 , q2} U {q1 , q2}) U {q2} = {q0 , q1 , q2} (Is 010 accepted? Yes, since {q2 } is a final state )

  31. Definitions for NFA-ε Machines • Let N = (Q, Σ, δ,q0,F) be an NFA-ε and let w be in Σ*. Then w is accepted by N iff δ*({q0}, w) contains at least one state in F. • Let N = (Q, Σ, δ,q0,F) be an NFA-ε. Then the language accepted by N is the set: L(N) = {w | w is in Σ* and δ*({q0},w) contains at least one state in F} • Another equivalent definition: L(N) = {w | w is in Σ* and w is accepted by N}

  32. 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-ε N such that L = L(N). • It follows that NFA-ε machines accept all regular languages. • But do NFA-ε machines accept more?

  33. 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).

  34. Lemma 2: Let M be an NFA-ε. Then there exists a NFA M’ such that L(M) = L(M’). Proof: (sketch) Let M = (Q, Σ, δ,q0,F) be an NFA-ε. Define an NFA M’ = (Q, Σ, δ’,q0,F’) as: F’ = F U {q0} if ε-closure(q0) 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 Σ) –> 2Q is a function • M’ has the same state set, the same alphabet, and the same start state as M • M’ has no ε transitions

  35. q3 1 q2 q2 0 0 0/1 ε ε q0 q1 q3 0 1 q0 q1 • Example: Equivalence of NFAs and NFA-ε s • Step #1: • Same state set as M • q0 is the starting state

  36. q3 1 q0 q2 q2 0 0 0/1 ε ε q0 q1 0 1 Example: Equivalence of NFAs and NFA-ε s Step #2: F’ = F U {q0} if ε-closure(q0) contains at least one state from F F’ = F otherwise • q0becomes a final state q3 q1

  37. q3 1 q2 q0 q2 0 0 0/1 ε ε q0 q1 0 1 Example: Equivalence of NFAs and NFA-ε s Step #3: Q = {q0, q1, q2, q3} δ’(q, a) = δ^(q, a) - for all q in Q and a in Σ δ’(q0, 0) = δ^ (q0, 0) q3 0 0 q1 0

  38. q3 1 q2 q0 q2 0 0 0/1 ε ε q0 q1 0 1 Example: Equivalence of NFAs and NFA-ε s Step #3 Q = {q0, q1, q2, q3} δ’(q, a) = δ^(q, a) - for all q in Q and a in Σ δ’(q0, 1) = δ^ (q0, 1) q3 1 0,1 0 q1 0,1

  39. q3 1 q2 q0 q2 0 0 0/1 ε ε q0 q1 0 1 Example: Equivalence of NFAs and NFA-ε s Step #4: Q = {q0, q1, q2, q3} δ’(q, a) = δ^(q, a) - for all q in Q and a in Σ δ’(q1, 0) = δ^ (q1, 0) q3 1 0,1 0 0 0,1 q1 0,1

  40. q3 1 q2 q0 q2 0 0 0/1 ε ε q0 q1 0 1 Example: Equivalence of NFAs and NFA-ε s Step #4: Q = {q0, q1, q2, q3} δ’(q, a) = δ^(q, a) - for all q in Q and a in Σ δ’(q1, 1) = δ^ (q1, 1) q3 1 0,1 1 0,1 0,1 0,1 q1 1 0,1

  41. q3 1 q2 q0 q2 0 0 0/1 ε ε q0 q1 0 1 Example: Equivalence of NFAs and NFA-ε s Step #5: Q = {q0, q1, q2, q3} δ’(q, a) = δ^(q, a) - for all q in Q and a in Σ δ’(q2, 0) = δ^ (q2, 0) q3 1 0,1 1 0 0,1 0,1 0,1 q1 1 0,1

  42. q3 1 q2 q0 q2 0 0 0/1 ε ε q0 q1 0 1 Example: Equivalence of NFAs and NFA-ε s Step #5: Q = {q0, q1, q2, q3} δ’(q, a) = δ^(q, a) - for all q in Q and a in Σ δ’(q2, 1) = δ^ (q2, 1) q3 1 0,1 1 0,1 0,1 0,1 0,1 q1 1 0,1

  43. q3 1 q2 q0 q2 0 0 0/1 ε ε q0 q1 0 1 Example: Equivalence of NFAs and NFA-ε s Step #6: Q = {q0, q1, q2, q3} δ’(q, a) = δ^(q, a) - for all q in Q and a in Σ δ’(q3, 0) = δ^ (q3, 0) q3 1 0,1 1 0,1 0,1 0,1 0,1 q1 1 0,1

  44. q3 1 q2 q0 q2 0 0 0/1 ε ε q0 q1 0 1 Example: Equivalence of NFAs and NFA-ε s Step #6: Q = {q0, q1, q2, q3} δ’(q, a) = δ^(q, a) - for all q in Q and a in Σ δ’(q3, 1) = δ^ (q3, 1) q3 1 0,1 1 0,1 0,1 0,1 0,1 q1 1 0,1

  45. q3 1 q2 q0 q2 0 0 0/1 ε ε q0 q1 0 1 Example: Equivalence of NFAs and NFA-ε s Step #7: • Done q3 1 0,1 1 0,1 0,1 0,1 0,1 q1 1 0,1

  46. 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?

  47. 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).

  48. Lemma 2: Let M be an NFA. Then there exists a DFA M’ such that L(M) = L(M’). Proof: (sketch) Let M = (Q, Σ, δ,q0,F) be an NFA, and define a DFA M’ = (Q’, Σ, δ’,q’0,F’) as: 1. Q’ = P(Q) - Every state of M’ is a set of states of M 2. q’0 = E({q0}) 3. δ’ (R, a) = {q є Q | q є E(δ (r, a)) for some r є R} or δ’ (R, a) = E ( δ (r, a) ) - R є Q’ and a є ∑. If R is a state of M’, it is also a set of states of M. When M’ reads symbol a in state R, it shows where a takes each state in R. 4. F’ = {R є Q’ | R contains an accept state of M}

  49. 1 Example: Equivalence of DFAs and NFAs b a є 3 2 Here Q = {1, 2, 3} - Q is set of NFA States As Q’ = P(Q) 1. Q’ = { Ф,{1}, {2}, {3}, {1,2}, {1,3}, {2,3}, {1,2,3} } 2. q’0 = E({q0}) –q0 is the start state of NFA and q0’ is start state of DFA q’0 = E({1}) q0’= { 1, 3 } - start state of DFA 3. δ’ (R, a) = E ( δ (r, a) ) a, b a

  50. 1 Example: Equivalence of DFAs and NFAs b a є 3 2 δ’ (R, a) = E ( δ (r, a) ) (i) δ’ ({}, a) = E ( δ({}, a) ) = E ( {} ) = { } (ii) δ’ ({}, b) = E ( δ({}, b) ) = E ( {} ) = {} (i) δ’ ({1}, a) = E ( δ({1}, a) ) = E ( {} ) = { } (ii) δ’ ({1}, b) = E ( δ({1}, b) ) = E ( {2} ) = {2} a, b a

More Related