Lecture 4: Nondeterministic Finite Automata Dr Kieran T. Herley Department of Computer Science University College Cork 2017-2018 KH (26/09/17) Lecture 4: Nondeterministic Finite Automata 2017-2018 1 / 1
Nondeterministic Finite Automata NFA Notes Allows edges labelled ɛ Possibly several ɛ-edges per state (DFA disallows both) Subsumes DFA concept Seemingly more expressive/powerful (But actually not) KH (26/09/17) Lecture 4: Nondeterministic Finite Automata 2017-2018 2 / 1
NFA cont d A Nondeterministic Finite Automaton (NFA) consists of Σ, S, s 0, A As for DFA T = (S, E) a directed graph in which each edge in E is labelled with one or more elements of Σ {ɛ}. 1 1 Definition allows state to have multiple edges bearing same label, unlike DFA. We will not use this feature (except for ɛ-edges). KH (26/09/17) Lecture 4: Nondeterministic Finite Automata 2017-2018 3 / 1
Acceptance Criterion Intuitively, the automaton accepts string x 1 x 2 x n if there is a path in T from the start state to one of the accept states whose edge labels concatenated together yield x 1 x 2 x n. Note: ɛ labels disappear in concatenation, e.g. accepts ɛ ɛ a ɛ ɛ = a KH (26/09/17) Lecture 4: Nondeterministic Finite Automata 2017-2018 4 / 1
Acceptance Criterion Formal Version Definition More formally, a path e 1, e 2, e m yields y 1, y 2,, y m if for each i edge e i bears label y i. The automaton accepts string x 1 x 2 x n if there is a path e 1, e 2, e m in T from the start state to one of the accept states that yields some y 1, y 2,, y m such that All other strings are rejected. y 1 y 2 y m = x 1 x 2 x n KH (26/09/17) Lecture 4: Nondeterministic Finite Automata 2017-2018 5 / 1
Example Recall A ɛ B = A B. Accepted Strings a b abba ɛ Rejected Strings? KH (26/09/17) Lecture 4: Nondeterministic Finite Automata 2017-2018 6 / 1
NFA Example 1 Accepts a b aaa Rejects ab ba In other words... KH (26/09/17) Lecture 4: Nondeterministic Finite Automata 2017-2018 7 / 1
NFA Example 1 Accepts a b aaa Rejects ab ba In other words...all monotonous strings i.e. all as or all bs. (Proof?) KH (26/09/17) Lecture 4: Nondeterministic Finite Automata 2017-2018 7 / 1
NFA Example 2 Accepts abb ababababb Rejects abba abbaaaaa In other words... KH (26/09/17) Lecture 4: Nondeterministic Finite Automata 2017-2018 8 / 1
NFA Example 2 Accepts abb ababababb Rejects abba abbaaaaa In other words...strings that end with abb. (Proof?) KH (26/09/17) Lecture 4: Nondeterministic Finite Automata 2017-2018 8 / 1
Some Terminology ɛ-closure ɛ-closure(s) set of NFA states reachable from state s using ɛ edges only 0 {0, 1, 2, 4, 7} 2 {2} 6 {1, 2, 4, 6, 7} ɛ-closure(s) set of NFA states reachable from any state s S using ɛ edges only KH (26/09/17) Lecture 4: Nondeterministic Finite Automata 2017-2018 9 / 1
Operation of NFA Initially one token on each state in ɛ-closure(s 0 ) i.e. potentially multiple token-bearing states One step per input symbol (input symbol x): advance tokens along x-edge(s) emanating from current states (remove token if no such edge(s), also duplicates) replicate each token in each state t to states in ɛ-closure(t) Illustration (one step) KH (26/09/17) Lecture 4: Nondeterministic Finite Automata 2017-2018 10 / 1
Computing ɛ-closure Closure is essentially a reachability problem in a directed graph Stack-based depth-first search in transition graph beginning at states in X and using ɛ labelled edges only: Algorithm Closure(X): Push states in X onto stack S C X while S not empty do Pop t, the top element, off S for each u ɛadj(t) do if u C then C C {u} Push u unto S return (C) ɛadj(t) denotes the nodes to which there is an ɛ-edge leading from t. KH (26/09/17) Lecture 4: Nondeterministic Finite Automata 2017-2018 11 / 1
Computing ɛ-closure cont d Algorithm Closure(X): Push states in X onto stack S C X while S not empty do Pop t, the top element, off S for each u ɛadj(t) do if u C then C C {u} Push u unto S return (C) S t Adj C 0 0 0 0 1, 7 0, 1, 7 KH (26/09/17) Lecture 4: Nondeterministic Finite Automata 2017-2018 12 / 1
Computing ɛ-closure cont d Algorithm Closure(X): Push states in X onto stack S C X while S not empty do Pop t, the top element, off S for each u ɛadj(t) do if u C then C C {u} Push u unto S return (C) S t Adj C 0 0 0 0 1, 7 0, 1, 7 1 7 7 KH (26/09/17) Lecture 4: Nondeterministic Finite Automata 2017-2018 12 / 1
Computing ɛ-closure cont d Algorithm Closure(X): Push states in X onto stack S C X while S not empty do Pop t, the top element, off S for each u ɛadj(t) do if u C then C C {u} Push u unto S return (C) S t Adj C 0 0 0 0 1, 7 0, 1, 7 1 7 7 1 1 2, 4 0, 1, 7, 2, 4 KH (26/09/17) Lecture 4: Nondeterministic Finite Automata 2017-2018 12 / 1
Computing ɛ-closure cont d Algorithm Closure(X): Push states in X onto stack S C X while S not empty do Pop t, the top element, off S for each u ɛadj(t) do if u C then C C {u} Push u unto S return (C) S t Adj C 0 0 0 0 1, 7 0, 1, 7 1 7 7 1 1 2, 4 0, 1, 7, 2, 4 2, 4 4 KH (26/09/17) Lecture 4: Nondeterministic Finite Automata 2017-2018 12 / 1
Computing ɛ-closure cont d Algorithm Closure(X): Push states in X onto stack S C X while S not empty do Pop t, the top element, off S for each u ɛadj(t) do if u C then C C {u} Push u unto S return (C) S t Adj C 0 0 0 0 1, 7 0, 1, 7 1 7 7 1 1 2, 4 0, 1, 7, 2, 4 2, 4 4 2 2 KH (26/09/17) Lecture 4: Nondeterministic Finite Automata 2017-2018 12 / 1
Computing ɛ-closure cont d Algorithm Closure(X): Push states in X onto stack S C X while S not empty do Pop t, the top element, off S for each u ɛadj(t) do if u C then C C {u} Push u unto S return (C) S t Adj C 0 0 0 0 1, 7 0, 1, 7 1 7 7 1 1 2, 4 0, 1, 7, 2, 4 2, 4 4 2 2 Returns C = {0, 1, 2, 4, 7}. KH (26/09/17) Lecture 4: Nondeterministic Finite Automata 2017-2018 12 / 1
NFA Simulation Algorithm Algorithm NfaAccept(M, X): S Closure({M.start}) while ch eof do S Closure(M.moveTo(S, ch)) if S M.accept then return true else return false M.start M.accept start state accept states M.moveTo transition function for M, i.e. moveto(s, x) is set of states reachable from states in S using a single x-edge. Note: Use two stacks to represent S and C and alternate at each iteration efficient. KH (26/09/17) Lecture 4: Nondeterministic Finite Automata 2017-2018 13 / 1
Example of NFA Execution Automaton Input String N((a b) ) babbb Algorithm NfaAccept(M, X): S Closure({M.start}) while ch eof do S Closure( M.MoveTo(S, ch)) if S M.accept then return true else return false KH (26/09/17) Lecture 4: Nondeterministic Finite Automata 2017-2018 14 / 1
Example of NFA Execution Automaton Input String N((a b) ) babbb Algorithm NfaAccept(M, X): S Closure({M.start}) while ch eof do S Closure( M.MoveTo(S, ch)) if S M.accept then return true else return false KH (26/09/17) Lecture 4: Nondeterministic Finite Automata 2017-2018 14 / 1
Example of NFA Execution Automaton Input String N((a b) ) babbb Algorithm NfaAccept(M, X): S Closure({M.start}) while ch eof do S Closure( M.MoveTo(S, ch)) if S M.accept then return true else return false KH (26/09/17) Lecture 4: Nondeterministic Finite Automata 2017-2018 14 / 1
Example of NFA Execution Automaton Input String N((a b) ) babbb Algorithm NfaAccept(M, X): S Closure({M.start}) while ch eof do S Closure( M.MoveTo(S, ch)) if S M.accept then return true else return false KH (26/09/17) Lecture 4: Nondeterministic Finite Automata 2017-2018 14 / 1
Example of NFA Execution Automaton Input String N((a b) ) babbb Algorithm NfaAccept(M, X): S Closure({M.start}) while ch eof do S Closure( M.MoveTo(S, ch)) if S M.accept then return true else return false KH (26/09/17) Lecture 4: Nondeterministic Finite Automata 2017-2018 14 / 1
Example of NFA Execution Automaton Input String N((a b) ) babbb Algorithm NfaAccept(M, X): S Closure({M.start}) while ch eof do S Closure( M.MoveTo(S, ch)) if S M.accept then return true else return false KH (26/09/17) Lecture 4: Nondeterministic Finite Automata 2017-2018 14 / 1
Example of NFA Execution Automaton Input String N((a b) ) babbb Algorithm NfaAccept(M, X): S Closure({M.start}) while ch eof do S Closure( M.MoveTo(S, ch)) if S M.accept then return true else return false KH (26/09/17) Lecture 4: Nondeterministic Finite Automata 2017-2018 14 / 1
Example of NFA Execution Automaton Input String N((a b) ) babbb Algorithm NfaAccept(M, X): S Closure({M.start}) while ch eof do S Closure( M.MoveTo(S, ch)) if S M.accept then return true else return false KH (26/09/17) Lecture 4: Nondeterministic Finite Automata 2017-2018 14 / 1
Example of NFA Execution Automaton Input String N((a b) ) babbb Algorithm NfaAccept(M, X): S Closure({M.start}) while ch eof do S Closure( M.MoveTo(S, ch)) if S M.accept then return true else return false KH (26/09/17) Lecture 4: Nondeterministic Finite Automata 2017-2018 14 / 1
Example of NFA Execution Automaton Input String N((a b) ) babbb Algorithm NfaAccept(M, X): S Closure({M.start}) while ch eof do S Closure( M.MoveTo(S, ch)) if S M.accept then return true else return false KH (26/09/17) Lecture 4: Nondeterministic Finite Automata 2017-2018 14 / 1
Example of NFA Execution Automaton Input String N((a b) ) babbb Accept! Algorithm NfaAccept(M, X): S Closure({M.start}) while ch eof do S Closure( M.MoveTo(S, ch)) if S M.accept then return true else return false KH (26/09/17) Lecture 4: Nondeterministic Finite Automata 2017-2018 14 / 1
Example of NFA Execution Trace S ch moveto Closure(Move(...)) 0, 1, 2, 4, 7 b 5 1, 2, 4, 5, 6, 7 1, 2, 4, 5, 6, 7 a 3 1, 2, 3, 4, 6, 7 1, 2, 3, 4, 6, 7 b 5 1, 2, 4, 5, 6, 7 1, 2, 4, 5, 6, 7 b 5 1, 2, 4, 5, 6, 7 1, 2, 4, 5, 6, 7 b 5 1, 2, 4, 5, 6, 7 Outcome Accept string since S contains accept state 7 at conclusion. KH (26/09/17) Lecture 4: Nondeterministic Finite Automata 2017-2018 15 / 1
Correctness Algorithm NfaAccept(M, X): S Closure({M.start}) while ch eof do S Closure( M.MoveTo(S, ch)) if S M.accept then return true else return false Some Notation X S i S i = x 1 x 2 x 3 x n Set S at beginning of iteration i = 1, 2, Final state designated S n+1 ɛ-closure of S i KH (26/09/17) Lecture 4: Nondeterministic Finite Automata 2017-2018 16 / 1
Correctness cont d Algorithm NfaAccept(M, X): S Closure({M.start}) while ch eof do S Closure(M.MoveTo(S, ch)) if S M.accept then return true else return false Acceptance Criterion The automaton accepts string x 1 x 2 x n if there is a path in T from the start state to one of the accept states whose edge labels concatenated together yield x 1 x 2 x n. Path implies true returned Path has structure shown below Easy to see algorithm returns true KH (26/09/17) Lecture 4: Nondeterministic Finite Automata 2017-2018 17 / 1
Correctness cont d Algorithm NfaAccept(M, X): S Closure({M.start}) while ch eof do S Closure(M.MoveTo(S, ch)) if S M.accept then return true else return false Acceptance Criterion The automaton accepts string x 1 x 2 x n if there is a path in T from the start state to one of the accept states whose edge labels concatenated together yield x 1 x 2 x n. True implies path existance Consider relationships between sets S 1, S 2, S n+1 Implies existance of path KH (26/09/17) Lecture 4: Nondeterministic Finite Automata 2017-2018 18 / 1
Transformation not straightforward, but intuitively each DFA state KH (26/09/17) Lecture 4: Nondeterministic Finite Automata 2017-2018 19 / 1 DFA vs NFA Theorem For every NFA, there is an equivalent DFA, i.e. one that accepts precisely the same language.
DFA vs NFA cont d Algorithm DfaAccept(M, X): s M.start ch X.nextChar() while ch eof do s M.moveTo(s, ch) ch X.nextChar() if s in M.accept then return true else return false O( X ) running time Other Considerations NFA-to-DFA transformation takes time Algorithm NfaAccept(M, X): S Closure({M.start}) while ch eof do S Closure( M.MoveTo(S, ch)) if S M.accept then return true else return false O( X n) running time where n = # states/edges DFA can have upto 2 n states (n = # states of NFA equivalent); hence can be space-hungry (large tables!). KH (26/09/17) Lecture 4: Nondeterministic Finite Automata 2017-2018 20 / 1