Finite Automata Dr. Neil T. Dantam CSCI-561, Colorado School of Mines Fall 2017 Dantam (Mines CSCI-561) Finite Automata Fall 2017 1 / 35
Outline Dantam (Mines CSCI-561) Finite Automata Fall 2017 2 / 35
Languages Review Outline Dantam (Mines CSCI-561) Finite Automata Fall 2017 3 / 35
Languages Review Alphabets Symbol: An abstract, primitive, atomic thing Examples: a, x, 4, θ, ℵ,!, Set: An unordered collection, without repetition Alphabet: A non-empty, finite set of symbols Σ B = {0, 1} Σ E = {a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q, r, s, t, u, v, w, x, y, z} Σ C = {,,, } Dantam (Mines CSCI-561) Finite Automata Fall 2017 4 / 35
Languages Review String Sequence: An ordered list of objects Example: (1, 2, 3, 5, 8,...) Abbreviated Notation: 12358... Empty (zero-length) sequence: ɛ String: A sequence over some alphabet Γ B = 101010 Γ E = hello Γ C = Dantam (Mines CSCI-561) Finite Automata Fall 2017 5 / 35
Languages Review Languages Language: A set of strings Examples: Alternations of 0 and 1: {ɛ, 01, 10, 0101, 1010, 010101, 101010,...} 0s and 1s with no consecutive 1s: {ɛ, 0, 1, 00, 01, 10, 000, 001, 010, 100, 101, 0000, 0001, 0010, 0100, 0101,...} Dantam (Mines CSCI-561) Finite Automata Fall 2017 6 / 35
Traffic Light Example Outline Dantam (Mines CSCI-561) Finite Automata Fall 2017 7 / 35
Traffic Light Example Traffic Light Example State: NS = {red, yellow, green} EW = {red, yellow, green} timeout = {0, 1} Sensors: Q = {timeout, ns-car, ew-car} gr timeout grt ew-car yr timeout timeout rg timeout rgt ns-car ry Dantam (Mines CSCI-561) Finite Automata Fall 2017 8 / 35
Traffic Light Example Traffic Light w/ Pedestrian Example State: NS = {red, yellow, green}, EW = {red, yellow, green}, timeout = {0, 1}, pedestrian = {0, 1} Sensors: Q = {timeout, ns-car, ew-car, pedestrian} timeout ry ns-car ew-car gr timeout grt ew-car pedestrian yr timeout yrp rg timeout rgt timeout pedestrian ryp timeout rrp timeout rrpt ns-car More: Left-turn arrows Railroad crossings Synchronize with other lights Dantam (Mines CSCI-561) Finite Automata Fall 2017 9 / 35
Traffic Light Example Transition Table gr timeout grt ew-car yr timeout timeout rg timeout rgt ns-car state timeout ns-car ew-car gr grt grt yr yr rg rg rgt rgt ry ry gr ry / SWITCH CASE / e v e n t = n e x t e v e n t ( ) ; switch ( s t a t e ) { case GR: switch ( e v e n t ) { case TIMEOUT: s t a t e = GRT; break ; d e f a u l t : s t a t e = DEAD; } /... / } / LOOKUP TABLE / e v e n t = n e x t e v e n t ( ) ; s t a t e = t a b l e [ s t a t e ] [ e v e n t ] ; Dantam (Mines CSCI-561) Finite Automata Fall 2017 10 / 35
Deterministic Finite Automata Outline Dantam (Mines CSCI-561) Finite Automata Fall 2017 11 / 35
Deterministic Finite Automata Deterministic Finite Automata (DFA) A deterministic finite automaton is a 5-tuple: M = (Q, Σ, δ, q 0, F ), where Q is a finite set call the states Σ is a finite set call the alphabet δ : Q Σ Q is the transition function q0 Q is the start state F Q is the set of accept states Dantam (Mines CSCI-561) Finite Automata Fall 2017 12 / 35
Deterministic Finite Automata Conceptual DFA Operation tape M : Σ {accept, reject} σ = σ 0 } σ 1... {{ σ k 1 } history σ k σ k+1 }... {{ σ n } future Finite Control Q {accept, reject} accept Reach end of tape (string) with control in accept state q F reject Reach end of tape (string) with control not in accept state q F language The language of M is the set of strings accepted by M, L (M) = {x M(x) = accept} Dantam (Mines CSCI-561) Finite Automata Fall 2017 13 / 35
Deterministic Finite Automata Graphical DFA L (M) = {x {0, 1} x ends in 1} states Q Graph Nodes alphabet Σ Graph Edge Labels transition function δ Graph Edges start state q 0 Designed graph node accept states F Double-circled graph nodes start start 0 1 1 q 0 q 1 0 state 0 1 F q 0 q 0 q 1 0 q 1 q 0 q 1 1 Dantam (Mines CSCI-561) Finite Automata Fall 2017 14 / 35
Deterministic Finite Automata Example: FA Design q even q odd L (M) = {w {0, 1} the number of 1s is odd} What state do we need to track? What are the state transitions? What are the start and accept states? 0 0 1 q even 1 q odd 0 0 start 1 q even 1 q odd Dantam (Mines CSCI-561) Finite Automata Fall 2017 15 / 35
Deterministic Finite Automata Example: FA Design continued q even q odd 0 0 q even 1 1 q odd state 0 1 F q even q even q odd 0 q odd q odd q even 1 0 0 start 1 q even 1 q odd Dantam (Mines CSCI-561) Finite Automata Fall 2017 16 / 35
Deterministic Finite Automata Symbolic DFA Semantics Transition Function δ : Q Σ Q Extended Transition Function ˆδ : Q Σ Q base ˆδ(q, ɛ) = q inductive For ω Σ and a Σ, ˆδ(q, ωa) = δ(ˆδ(q, ω), a) Accept ˆδ(q 0, ω) F M accepts ω Reject ˆδ(q 0, ω) F M rejects ω { } Language L (M) = ω Σ ˆδ(q 0, ω) F M recognizes L (M) Dantam (Mines CSCI-561) Finite Automata Fall 2017 17 / 35
Deterministic Finite Automata Example: DFA Simulation start 0 0 1 q even 1 q odd state 0 1 F q even q even q odd 0 q odd q odd q even 1 Simulate the DFA on the following strings: ɛ 01 010 0100 Dantam (Mines CSCI-561) Finite Automata Fall 2017 18 / 35
Deterministic Finite Automata DFA Simulation Algorithm 1: DFA-Simulate(M) Input: M = (Q, Σ, δ, q 0, F ) ; // states,alphabet,transition,start,accept Output: y {accept, reject} 1 q q 0 ; /* Read next symbol on input tape */ 2 σ next() ; 3 while σ EOF do /* Follow state transition */ 4 q δ(q, σ) ; /* Read next symbol on input tape */ 5 σ next() ; 6 if q F then 7 y accept; 8 else 9 y reject; Dantam (Mines CSCI-561) Finite Automata Fall 2017 19 / 35
Deterministic Finite Automata Regular Languages The regular set is the set of languages that can be recognized by DFAs R = {L (M) M is a DFA} A language is a regular language if some DFA accepts it (L R) M, (L = L (M)) What languages are not regular? Dantam (Mines CSCI-561) Finite Automata Fall 2017 20 / 35
Nondeterministic Finite Automata Outline Dantam (Mines CSCI-561) Finite Automata Fall 2017 21 / 35
Nondeterministic Finite Automata Determinism vs. Nondeterminism tape σ 0 σ 1 σ 2 σ 3... Deterministic Transition to single state δ dfa (q i, σ) = q j δ(q 0, σ 0 ) δ(d 1, σ 1 ) δ(d 2, σ 2 ) δ(d 3, σ 3 ) q 0 d 1 d 2 d 3... Nondeterministic Transition to a multiple states δ nfa (q i, σ) = {q j, q k, q l,...} q 0 n 10. n 20.. n 11. Dantam (Mines CSCI-561) Finite Automata Fall 2017 22 / 35
Nondeterministic Finite Automata Example: NFA and Transition Table start a start 0 1 2 q ɛ 0 q ɛ 1 q 2 0 s, followed by 1 s, followed by 2 s b q 0 q 1 q 2 a, b ɛ a state 0 1 2 ɛ F q 0 {q 0 } {q 1 } 0 q 1 {q 1 } {q 2 } 0 q 2 {q 2 } 1 state a b ɛ F q 0 {q 1 } {q 2 } 1 q 1 {q 1, q 2 } {q 2 } 0 q 2 {q 0 } 0 Dantam (Mines CSCI-561) Finite Automata Fall 2017 23 / 35
Nondeterministic Finite Automata Nondeterministic Finite Automata (NFA) A nondeterministic finite automaton is a 5-tuple: N = (Q, Σ, δ, q 0, F ), where Q is a finite set call the states Σ is a finite set call the alphabet δ : Q Σ P (Q) is the transition function q0 Q is the start state F Q is the set of accept states Dantam (Mines CSCI-561) Finite Automata Fall 2017 24 / 35
Nondeterministic Finite Automata NFA vs. DFA Given: NFA N = (Q N, Σ, δ N, q 0,N, F N ) Find: DFA M = (Q M, Σ, δ M, q 0,N, F M ), such that L (M) = L (N) Solution: DFA states are sets of NFA states NFA DFA states Q N Q M = P (Q N ) alphabet Σ Σ transition δ N : Q N Σ P (Q N ) δ M (Q, σ) {q Q N σ-reachable from q i Q} start q 0,N Q N q 0,M = {q Q N ɛ-reachable from q 0,N } accept F N Q N F M = {q m Q M q m F N } Dantam (Mines CSCI-561) Finite Automata Fall 2017 25 / 35
Nondeterministic Finite Automata Practical usage of DFAs vs. NFAs Today s physical computers are deterministic Options: 1. Simulate NFA by tracking all possible current states 2. Convert NFA to DFA by constructing the subsets of NFA state set Which option is better? Dantam (Mines CSCI-561) Finite Automata Fall 2017 26 / 35
Nondeterministic Finite Automata NFA subset construction outines ɛ closure(q) States reachable from q on ɛ transitions move-ɛ closure(q, σ) States reachable from state q after reading symbol σ Construct the reachable subsets of NFA states Dantam (Mines CSCI-561) Finite Automata Fall 2017 27 / 35
Nondeterministic Finite Automata ɛ-closure examples 0 1 2 start q 0 ɛ q 1 ɛ q 2 ɛ-closure(q 0 ) = {q 0, q 1, q 2 } ɛ-closure(q 1 ) = {q 1, q 2 } ɛ-closure(q 2 ) = {q 2 } a start q 0 b a ɛ q 1 a, b q 2 ɛ-closure(q 0 ) = {q 0, q 2 } ɛ-closure(q 1 ) = {q 1 } ɛ-closure(q 2 ) = {q 2 } States reachable from initial state on only ɛ transitions Dantam (Mines CSCI-561) Finite Automata Fall 2017 28 / 35
Nondeterministic Finite Automata ɛ closure Algorithm 2: ɛ-closure(nfa,s,c) Input: NFA, S, C ; // NFA, unvisited states, initial closure Output: C ; // final closure /* visit: P (Q) Q P (Q) 1 Function visit(c,q) is 2 if q c then // base case 3 return c; 4 else // Recursive case 5 return ɛ closure NFA, p, {q} c q p NFA ɛ }{{} ; add q to closure }{{} ɛ-neighbors of q 6 C fold-left(visit, C, S) ; */ Dantam (Mines CSCI-561) Finite Automata Fall 2017 29 / 35
Nondeterministic Finite Automata move-ɛ-closure examples 0 1 2 start q 0 ɛ q 1 ɛ q 2 move-ɛ-closure(q 0, 0) = {q 0, q 1, q 2 } move-ɛ-closure(q 0, 1) = {q 1, q 2 } move-ɛ-closure(q 0, 2) = {q 2 } a start q 0 b a ɛ q 1 a, b q 2 move-ɛ-closure(q 0, a) = {q 0, q 2 } move-ɛ-closure(q 1, a) = {q 1, q 2 } move-ɛ-closure(q 2, a) = {q 0, q 2 } States reachable from initial state after reading one symbol Dantam (Mines CSCI-561) Finite Automata Fall 2017 30 / 35
Nondeterministic Finite Automata move-ɛ closure Algorithm 3: move-ɛ-closure(nfa,q,σ) Input: NFA, Q, σ ; // NFA, initial states, token Output: C ; // reachable states 1 Function visit(c,q) is 2 return ɛ closure NFA, σ reachable from q { }} { p q σ p NFA, c ; }{{} ɛ-reachable after move 3 C fold-left visit,, ɛ closure(nfa, Q, ) ; }{{} ɛ-reachable from Q Dantam (Mines CSCI-561) Finite Automata Fall 2017 31 / 35
Nondeterministic Finite Automata NFA Simulation Algorithm 4: NFA-Simulate(N) Input: N = (Q, Σ, δ, q 0, F ) ; // states,alphabet,transition,start,accept Output: y {accept, reject} 1 c ɛ-closure(q 0 ); /* Read next symbol on input tape */ 2 σ next() ; 3 while σ EOF do /* Follow state transition */ 4 c move-ɛ-closure(n, c, σ)) ; /* Read next symbol on input tape */ 5 σ next() ; 6 if c F then 7 y accept; 8 else 9 y reject; Dantam (Mines CSCI-561) Finite Automata Fall 2017 32 / 35
Nondeterministic Finite Automata NFA to DFA Subset Construction Algorithm 5: NFA-to-DFA Input: N = (Q, Σ, E, q 0, F ) ; // NFA states, alphabet, edges, start, accept Output: M = (Q, Σ, E, q 0, F ) ; // DFA states, alphabet, edges, start, accept 1 q 0 ɛ-closure(q 0) ; 2 Q {q 0 }, E ; /* Construct Subsets */ 3 i 0 ; 4 while i < Q do /* Finding outgoing edges of i th DFA state */ 5 forall σ Σ do /* Check for transition from Q i on symbol σ */ 6 u = move ɛ closure(n, Q i, σ) ; 7 if u then 8 Q Q u ; // Add new subset { } 9 E E Q i σ u ; // Add new edge/transition 10 i i + 1; /* Accept States */ 11 F = {q Q q F } Dantam (Mines CSCI-561) Finite Automata Fall 2017 33 / 35
Nondeterministic Finite Automata NFA to DFA Example NFA 0 1 2 start q ɛ 0 q ɛ 1 q 2 DFA 0 1 2 start 1 2 q 0, q1, q2 q 1, q2 q 2 Dantam (Mines CSCI-561) Finite Automata Fall 2017 34 / 35 2
Nondeterministic Finite Automata NFA to DFA Examples (cont.) a NFA start q 0 b a ɛ q 1 a, b q 2 DFA a start q 0, q 2 b a q 1 b q 2 a b a q 1, q 2 q 0, q 2, q 2 b a Dantam (Mines CSCI-561) Finite Automata Fall 2017 35 / 35