1 / 72

Nondeterministic Finite Automata

Nondeterministic Finite Automata. Nondeterminism Subset Construction e -transitions. Nondeterministic FA. A nondeterministic finite automaton has the ability to be in several states at once.

mauli
Download Presentation

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. Nondeterministic Finite Automata Nondeterminism Subset Construction e-transitions

  2. Nondeterministic FA • A nondeterministic finite automaton has the ability to be in several states at once. • Transition function defines a set of states (which may be empty and denoted by nil) to which NFA goes upon receipt of a specified input from S in a specified state.

  3. Nondeterministic FA • “go to the empty set” does not mean “stay in the states” that processed the character and were directed to the empty set. • “go to the empty set” means “terminate the thread” that led to the states that were directed to the empty set by processing the current character.

  4. 0 1 A B C 0 0 1 A A A A A A B B B C Examples of thread termination 1 0 Start 1 0 Every 0 creates a path that could lead to acceptance if followed by a 1 that terminates the string. If in B and process 0, terminate thread. If in C and process any character, terminate thread accepts C

  5. Nondeterministic FA • There is only one “start” state. • Accepts a string if the sequence of transitions dictated by the string leads to any one of a set of “final” states.

  6. Formal NFA={Q,S,d,q0,F} • A finite set of states, typically Q. • An input alphabet, typically Σ. • A transition function, typically δ. • Takes a state in Q and a symbol in S • Returns subset of Q (could be empty) • In DFA d is one state, in NFA d is set of states • A start state in Q, typically q0, is unique • A set of final states F ⊆ Q.

  7. Extended Transition Function of NFA • In δ(q, a), q is a state and a is a symbol • But δ(q, a) is a set of states. • Recursive definition • Basis: δ(q,ε) = {q}, δ(q,a) = δ(q,a) • Induction: δ(q,xa) = the union of all states to which states in δ(q,x) go upon receipt of input a. • Compute δ(q,w) by sequentially computing all prefixes of w ˄ ˄ ˄ ˄ ˄

  8. 0 1 A B C NFA accepting input strings ending in 01 Note differences from graphs of DFAs 1 0 Start S 0 1 Interpretation of nil as “terminate thread” lets us use set operations to test string acceptance by NFAs A A,B A nil C B C nil nil * Note differences from transition tables of DFAs

  9. 0 1 A B C 1 ˄ ˄ Test acceptance of string 00101 by NFA Use the method of “all prefixes” 0  ˄ ˄ ˄ Start S 0 1 ˄ ˄ A A,B A ˄ ˄ nil C B C nil nil *

  10. 0 1 A B C 0 0 1 A A A A A A B B B C ˄ Test acceptance of string 00101 by NFA d(A,e)={A} d(A,0)=d(A,0)={A,B} d(A,00)=d(A,0)d(B,0)={A,B}nil={A,B} d(A,001)=d(A,1)d(B,1)={A}{C}={A,C} d(A,0010)=d(A,0)d(C,0)={A,B}nil={A,B} d(A,00101)=d(A,1)d(B,1) )={A}{C}={A,C} accept 1 0  ˄ Start S 0 1 ˄ 1 0 A A,B A nil C B accepts C C nil nil *

  11. 0 1 A B C ˄ How does pattern change with repeated 1’s Test acceptance of string 001011 by NFA 1 ˄ d(A,e)={A} d(A,0)=d(A,0)={A,B} d(A,00)=d(A,0)d(B,0)={A,B}nil={A,B} d(A,001)=d(A,1)d(B,1)={A}{C}={A,C} d(A,0010)=d(A,0)d(C,0)={A,B}nil={A,B} d(A,00101)=d(A,1)d(B,1) )={A}{C}={A,C} d(A,001011)=d(A,1)d(C,1) )={A}nil={A} String rejected because set does not contains C 0  ˄ ˄ ˄ Start S 0 1 ˄ A A,B A ˄ ˄ nil C B ˄ C nil nil *

  12. Language of an NFA • A string w is accepted by an NFA if δ(q0,w) contains at least one accepting state. • The language of the NFA is the set of strings it accepts. • Formal definition • L(NFA) = {w|δ(q0,w)F is not nil} ˄ ˄

  13. Example of NFA: Moves on a Chessboard • States = squares. • Inputs = r (move to adjacent red squares) and b (move to adjacent black square). • Moves can be horizontal, vertical and diagonal • Start state and final state are in opposite corners.

  14. S r b • 2,4 5 • 4,6 1,3,5 • 2,6 5 • 2,8 1,5,7 • 2,4,6,8 1,3,7,9 • 2,8 3,5,9 • 4,8 5 • 4,6 5,7,9 • 6,8 5 1 2 3 4 5 6 7 8 9 r b b 1 2 1 5 * 4 3 1 5 3 7 7 Accept, since final state reached 9 Chessboard 2: rbb accepted

  15. S r b • 2,4 5 • 4,6 1,3,5 • 2,6 5 • 2,8 1,5,7 • 2,4,6,8 1,3,7,9 • 2,8 3,5,9 • 4,8 5 • 4,6 5,7,9 • 6,8 5 1 2 3 4 5 6 7 8 9 b b b 1 5 1 5 * 3 7 9 Chessboard 3: strings of b’s Even accepted, odd rejected

  16. Equivalence of DFA’s and NFA’s • Equivalence means defines the same language • A DFA can be easily turned into an equivalent NFA. If δD(q, a) = p, let the equivalent NFA have δN(q, a) = {p}. • Equivalent NFA is always in a set that containing exactly one state – the state the DFA is in after reading the same input.

  17. Equivalence 2 • For any NFA there is an equivalent DFA that defines the same language. • Proof is by subset construction. We build the states and transition function of the DFA out of subsets of the finite set of states QN of the NFA • If |QN|=n, then DFA will be constructed from the 2n possible subsets of QN • Many of these 2n subsets cannot be reached from the start state of the DFA, which is the same as the unique start state of the NFA.

  18. Formal definition of equivalent DFA • Given an NFA with states QN (|QN|=n), alphabet Σ, transition function δN, start state q0, and final states FN, construct equivalent DFA with the following rules: • States are among the 2n subsets of QN • Same alphabet Σ as NFA • Same start state {q0} • Final states = all subsets with a member in FN

  19. Transition function of equivalent DFA • 1st argument of δD is a state of DFA; hence, a subset of QN • 2nd argument of δD is a symbol from common alphabet S of NFA and DFA • δD({q1,…,qk}, a) is the union over all i=1,…,k of δN(qi, a), where δN(qi, a) is the set of states that NFA goes to if it is in state qi and processes input character a.

  20. “Lazy” subset construction of equivalent DFA: Consider only subsets of QN that can be reached from the start state of the DFA • The “start” state of NFA is unique. Make it the singleton “start” subset of DFA. • Since DFA and NFA have the same “start” state, the 1st rows of their respective transition tables must also be the same. • Every time δD({q1,…,qk}, a) is a new subset, add it as a row in the dD transition table.

  21. 0 1 A B C Example: find equivalent DFA by lazy subset construction 1 0 Start S 0 1 A A,B A nil C B C nil nil *

  22. Example of “lazy” construction • 1st row DFA transition table same as NFA transition tabledD({A,B},0)= dN(A,0) U dN(B,0)={A,B} U nil={A,B} dD({A,B},1)= dN(A,1) U dN(B,1)={A} U {C}={A,C} dD({A,C},0)= dN(A,0) U dN(C,0)={A,B} U nil={A,B} dD({A,C},1)= dN(A,1) U dN(C,1)={A} U nil={A} • We have closure A,B A,B A,C A,B A,C S 0 1 subsets 0 1 A A,B A A A A,B nil C B Transition table of NFA Transition table of DFAeq C nil nil * A Rename the states and graph DFAeq *

  23. 1 0 0 0 1 1 A B C q2 q1 0 1 You can rename states of equivalent DFA Equivalent DFA 1 0 q1 q1 q2 q0 q0 =A q1 ={A,B} q2 ={A,C} q1 q2 Start subsets 0 1 Start Note: graph obeys rules of DFA q0 q0 q1 NFA q0 * Test acceptance of string 101 by DFAeq

  24. 0 0 A B C CptS 317 Fall 2019 Assignment 3, Due 9-13-19 Construct the transition table of the NFA graphed above. Derive the transition table of the equivalent DFA by lazy subset construction. Show all algebra used in the derivation. Graph the equivalent DFA without renaming the subsets of QN that are its states. 1 0 Start

  25. 0 0 A B C What type of strings does this NFA accept? 1 0 Test the acceptance of string 0001 by the method of all prefixes Start

  26. 0 0 A B C L(NFA) accepts strings ending in 00 1 0 dN(A,e)=A dN(A,0)={A,B} dN(A,00)={A,B}U{C}={A,B,C} dN(A,000)={A,B}U{C}U{nil}={A,B,C} dN(A,0001)={A}U{nil}U{nil}={A} Start

  27. Prove: if w in L(DFA), then w in L(NFAeq)

  28. Prove: if w in L(DFA), then w in L(NFAeq) δN(q0,w) = {δD(q0,w)} for all w in L(DFA) by construction. NFAeq is a special case of NFAs with all transitions to sets of states that are singletons of states of the DFA Given w in L(DFA)-> δD(q0,w)F δN(q0,w) = {some final state of DFA}

  29. Prove: if w in L(NFA), then w in L(DFAeq) Given NFA={QN, S, dN, q0, FN} Show by induction on length of string that If w is in L(NFA) then w is in L(DFAep), which will be true if δD({q0},w)={δN(q0,w)} for all w in L(NFA)

  30. Prove: if w in L(NFA), then w in L(DFAeq) Basis: w=ε: δD({q0},ε) = {q0} δN(q0, ε) = q0 DFA and NFA have the same start state. {q0} = q0 w=a: δD({q0},a)={δN(q0,a)} first row of δD is the same the first row of δN Setup: Let w = xa

  31. Prove: if w in L(NFA), then w in L(DFAeq) • IH: Assumeif x is in L(NFA), then x is in L(DFAeq) Equivalent to δD({q0},x)={S} for any subset S of QN that contains an accepting state. • Application: w is in L(NFA), prove w in L(DFAeq) let T=subset of QN that is the union of all states dN(p,a) where pS. δN(q0,w) = δN(q0,xa) = dN(dN(q0,x),a) =dN(S,a) =T Given w is in L(NFA), so T must contain an accepting state δD({q0},w) = δD({q0},xa)=dD(δD({q0},x),a)=dD({S},a)=T Therefor w in L(DFAeq) I.H.

  32. NFA’s With ε-Transitions • ε-Transitions: Spontaneous transitions between states. • Occur on the empty string, e, at any point in the processing of the input string • Another way to let NFA be in more than one state

  33. Example: e-NFA accepts decimal numbers digits . e,+,- digits e q5 q0 q2 q1 q3 digits . digits start q4 • S = {+, - , ., {0,1,2,3,4,5,6,7,8,9}} • + sign and digits before . are optional; hence e-transition from q0 to q1 (simultaneously in q0 and q1) • Transition from q0on inputs digit and . are not needed because they are defined for q1 • What path do we follow with input .d?

  34. e-NFA accepts decimal numbers digits . e,+,- digits e q5 q0 q2 q1 q3 digits . digits start q4 • Digits after period also optional; hence e-transition from q3 to accepting state • What path do we follow with input d.?

  35. e-NFA accepts decimal numbers digits . e,+,- digits e q5 q0 q2 q1 q3 digits . digits start q4 • To reach “q3” must have at least one digit before or after a decimal point • Any string reaching “q3” may be complete or can get more digits (NFA in both states) • Is string .d. accepted?

  36. e-NFA accepts decimal numbers digits . e,+,- digits e q5 q0 q2 q1 q3 digits . digits start q4 String .d. rejectedbecause any thread that leads to q5 is terminated unless it is the end of the string.

  37. S ε +,- . digits q0 {q1} {q1} ∅ ∅ q1∅∅ {q2} {q1,q4} q2∅ ∅∅ {q3} q4∅∅ {q3} ∅ q3 {q5} ∅∅ {q3} q5∅∅ ∅ ∅ * Transition-function table for e-NFA digits . ε,+,- ε q5 q0 q2 q1 q3 digits digits . digits start q4 Second column shows e-transitions. Only q0 and q3 go to another state on the empty string

  38. e-Closure of states Both ECLOSE(q) (used in text) and CL(q) (used on slides) denote the set of states {q, all states reached from q by ε-transitions, both directly and indirectly} e-closure of a set of states = union of the e-closures of each state. e-closure of a the empty set = empty set CL(nil) = nil

  39. S ε +,- . digits q0 {q1} {q1} ∅ ∅ q1∅∅ {q2} {q1,q4} q2∅ ∅∅ {q3} q4∅∅ {q3} ∅ q3 {q5} ∅∅ {q3} q5∅∅ ∅ ∅ * e-Closure examples digits . ε,+,- ε q5 q0 q2 q1 q3 digits digits . digits start q4 ECLOSE(q0) = CL(q0) = {q0,q1} ECLOSE(q1) = CL(q1) = {q1} ECLOSE(q3) = CL(q3) = {q3,q5}

  40. 0 1 ε A {E} {B} ∅ B ∅ {C} {D} C ∅ {D} ∅ D ∅ ∅ ∅ E {F} ∅ {B, C} F {D} ∅ ∅ ε 1 1 1 B C D * ε A ε 0 0 E F 0 More examples of CL(q) • CL(B) = ? • CL(C) = ? • CL(D) = ? • CL(E) = ?

  41. 0 1 ε A {E} {B} ∅ B ∅ {C} {D} C ∅ {D} ∅ D ∅ ∅ ∅ E {F} ∅ {B, C} F {D} ∅ ∅ ε 1 1 1 B C D * ε A ε 0 0 E F 0 More examples of CL(q) • CL(B) = {B,D} • CL(C) = C • CL(D) = D • CL(E) = {E,B,C,D} note: includes D

  42. ˄ δ Using CL(q) to define (q,w) when e-transitions may be present. (slide uses d for delta-hat) ˄ δ • Basis: d(q, ε) = CL(q) Note! May be more than {q} • Induction: Let w=xa and d(q,x)={p1,…pk} be all and only the states reached from q in the processing of string x, including states reached by e-transitions. • d(q,w) = CL(d(q,xa)) = CL(d(d(q,x),a)) =CL(d({p1…pk},a)) CL({d(p1,a),…,d(pk,a)}) = CL(d(p1,a))UCL(d(p2,a))U…UCL(d(pk,a)) Each CL(d(pi,a)) is a set of states from the transition function that includes states reachable by e-transitions. L(ε-NFA) = set of strings w such that d(q0, w) contains an accepting state.

  43. 0 1 ε A {E} {B} ∅ B ∅ {C} {D} C ∅ {D} ∅ D ∅ ∅ ∅ E {F} ∅ {B, C} F {D} ∅ ∅ ε 1 1 1 B C D * ε A ε 0 0 E F 0 Is string 011 accepted by e-NFA below? Use the method of all prefixes with ECLOSS on all states

  44. 0 1 ε A {E} {B} ∅ B ∅ {C} {D} C ∅ {D} ∅ D ∅ ∅ ∅ E {F} ∅ {B, C} F {D} ∅ ∅ * Is string 011 accepted by e-NFA below? d(A,ε) = CL(A) = {A} d(A,0) = CL(E) = {E, B, C, D}. d(A,01) = CL(∅UC U D U ∅ U) = CL({C, D}) = CL(C) U CL(D)={C,D} d(A,011) = CL(D U ∅)=CL(D)={D} accepted Are strings 0110 and 0111 Accepted? No, d(D,0)=nil and d(D,1)=nil terminates thread that led tothe accepting state.

  45. S ε +,- . digits q0 {q1} {q1} ∅ ∅ q1∅∅ {q2} {q1,q4} q2∅ ∅∅ {q3} q4∅∅ {q3} ∅ q3 {q5} ∅∅ {q3} q5∅∅ ∅ ∅ * Show that 5.6 is accepted by method of all prefixes digits . e,+,- e q5 q0 q2 q1 q3 . digits start q4

  46. S ε +,- . digits q0 {q1} {q1} ∅ ∅ q1∅∅ {q2} {q1,q4} q2∅ ∅∅ {q3} q4∅∅ {q3} ∅ q3 {q5} ∅∅ {q3} q5∅∅ ∅ ∅ * Show that 5.6 is accepted by method of prefixes d(q0,ε)=CL(q0) = {q0,q1} d(q0,5)=CL(∅U{q1,q4})=CL({q1,q4})=CL(q1)UCL(q4)={q1,q4} d(q0,5.)=CL(q2Uq3}=CL({q2,q3})=CL(q2)UCL(q3)=q2U{q3,q5}={q2,q3,q5} d(q0,5.6)=CL(q3U∅)=CL(q3)={q3,q5} accepted

  47. Equivalence of NFA & ε-NFA • Every NFA is an ε-NFA. • It just has no ε-transitions. • Given NFA, e-NFAeq = NFA • Given ε-NFA, we must construct an NFAeq that accepts strings of L(ε-NFA)

  48. Given e-NFA find NFAeq • ε-NFA that has states QE, alphabet Σ, start state q0, final states FE, and transition function δE. • Construct an NFA with states QE, alphabet Σ, start state q0, final states FN, and transition function δN. • Note! QN=QE and q0N = q0E but δN and FN are different.

  49. Given e-NFA find NFAeq • For ever state q in QE, compute δN(q,a) from δE(q,a) as follows: • Let S = CL(q). • δN(q,a) is the union over all p in S of δE(p,a) • FN = the set of states q such that CL(q) contains a state of FE.

More Related