Recitation 2 - Non Deterministic Finite Automata (NFA) and Regular Expressions Orit Moskovich Gal Rotem Tel Aviv University October 28, 2015 Recitation 2 - Non Deterministic Finite Automata (NFA) and Regular OctoberExpressions 28, 2015 1 / 22
Overview 1 Non Deterministic Finite Automata (NFA) 2 DFA and NFA Equivalence 3 Regular Expressions 4 Closure Under Operations Recitation 2 - Non Deterministic Finite Automata (NFA) and Regular OctoberExpressions 28, 2015 2 / 22
Reminder - Deterministic Finite Automata (DFA) Definition 1 A deterministic finite automaton is a 5-tuple M = (Q, Σ, δ, q 0, F) Q is a finite set called the states Σ is a finite set called the alphabet δ : Q Σ Q is the transition function q 0 Q is the initial state F Q is the set of accepting states Definition 2 A language L is regular there exists a DFA M that accepts it, i.e., L(M) = L Recitation 2 - Non Deterministic Finite Automata (NFA) and Regular OctoberExpressions 28, 2015 3 / 22
Non Deterministic Finite Automata (NFA) Definition 3 P(Q) is the power set of Q Definition 4 Σ ε = Σ {ε} Definition 5 A non deterministic finite automaton is a 5-tuple N = (Q, Σ ε, δ, q 0, F) Q is a finite set called the states Σ ε is a finite set called the alphabet δ : Q Σ ε P(Q) is the transition function q 0 Q is the initial state F Q is the set of accepting states Recitation 2 - Non Deterministic Finite Automata (NFA) and Regular OctoberExpressions 28, 2015 4 / 22
Non Deterministic Finite Automata (NFA) Definition 6 N accepts w Σ δ(q 0, w) F. I.e., if at least one of the computation routes ends in an accepting state. Recitation 2 - Non Deterministic Finite Automata (NFA) and Regular OctoberExpressions 28, 2015 5 / 22
DFA and NFA Equivalence Theorem 7 A language L is regular there exists an NFA N that accepts it Construction: NFA DFA Let N = (Q, Σ, δ, q 0, F ) be an NFA. We construct an equivalent DFA M = (Q, Σ, δ, q 0, F ). Q = P(Q) R Q, σ Σ. δ (R, σ) = q 0 = {q 0} q R δ(q, σ) F = {R Q R contains an accepting state} ( ) we ignore for now ε transitions Recitation 2 - Non Deterministic Finite Automata (NFA) and Regular OctoberExpressions 28, 2015 6 / 22
Example Example 8 Build an NFA for the following language: L 1 = {all words of length 2 that the letter before last is 0} Recitation 2 - Non Deterministic Finite Automata (NFA) and Regular OctoberExpressions 28, 2015 7 / 22
Example Example 9 Build the DFA from the NFA previously built for L 1 = {all words of length 2 that the letter before last is 0} Recitation 2 - Non Deterministic Finite Automata (NFA) and Regular OctoberExpressions 28, 2015 8 / 22
Regular expressions Definition 10 R is a regular expression over Σ if R is in the form of: σ for some σ Σ ε R 1 R 2 for regular expressions R 1 and R 2 R 1 R 2 for regular expressions R 1 and R 2 R1 for regular expression R 1 Definition 11 L(R) is the language defined by the regular expression R Recitation 2 - Non Deterministic Finite Automata (NFA) and Regular OctoberExpressions 28, 2015 9 / 22
Example Example 12 Build a regular expression for the language: L 1 = {all words of length 2 that the letter before last is 0} Regular expression: Σ 0(0 1) Recitation 2 - Non Deterministic Finite Automata (NFA) and October Regular 28, Expressions 2015 10 / 22
Example Example 13 Build an NFA and a regular expression for the following language: L 2 = {all words of that start with 0 or end with 1} Regular expression: 0Σ Σ 1 Recitation 2 - Non Deterministic Finite Automata (NFA) and October Regular 28, Expressions 2015 11 / 22
Example Example 14 Build the DFA from the NFA previously built for L 2 = {all words of that start with 0 or end with 1} Step 1: eliminate ε transitions Step 2: DFA... Recitation 2 - Non Deterministic Finite Automata (NFA) and October Regular 28, Expressions 2015 12 / 22
Closure Under Reverse Example 15 Define the operation: Reverse(L) = {w R w L} where w = x 1... x n, then w R = x n... x 1. Prove that regular languages are closed under Reverse. Proof. Let L be a regular language and let M = (Q, Σ, δ, q 0, F) be a DFA that accepts it. We build an NFA N Reverse s.t L(N Reverse ) = Reverse(L). Intuition? Recitation 2 - Non Deterministic Finite Automata (NFA) and October Regular 28, Expressions 2015 13 / 22
Closure Under Reverse (Cont.) Proof (Cont.) Intuition? Start by reversing all transitions: Note that this is no longer a DFA... Recitation 2 - Non Deterministic Finite Automata (NFA) and October Regular 28, Expressions 2015 14 / 22
Closure Under Reverse (Cont.) Proof (Cont.) Then add a new initial state, with an ε transition for all previously accepting states: Recitation 2 - Non Deterministic Finite Automata (NFA) and October Regular 28, Expressions 2015 15 / 22
Closure Under Reverse (Cont.) Proof (Cont.) Recall, M = (Q, Σ, δ, q 0, F) be a DFA that accepts L. Then, define Where, N Reverse = (Q {s}, Σ ε, δ, s, F ) s is the new initial state δ = (1) δ (s, ε) = F (2) q Q, σ Σ. δ (q, σ) = {r Q δ(r, σ) = q} F = {q 0 } Recitation 2 - Non Deterministic Finite Automata (NFA) and October Regular 28, Expressions 2015 16 / 22
Closure Under Reverse (Cont.) Proof (Cont.) Claim: L(N Reverse ) = Reverse(L) Proof: w R = x n... x 1 Reverse(L) w = x 1... x n L x q 0,..., q n Q. q 1 x 0 2 x q1... n qn where q n F (accepting route in M) x n s ε q }{{ n qn 1... x 1 q }}{{ 0 } (1) (2) where q 0 F w R = x n... x 1 L(N Reverse ) Recitation 2 - Non Deterministic Finite Automata (NFA) and October Regular 28, Expressions 2015 17 / 22
Closure Under DropChar Example 16 Define the operation: DropChar(L) = {w 1 w 2 w 1, w 2 Σ and σ Σ. w 1 σw 2 L} Prove that regular languages are closed under DropChar Recitation 2 - Non Deterministic Finite Automata (NFA) and October Regular 28, Expressions 2015 18 / 22
Closure Under DropChar (Cont.) Proof. Let L be a regular language and let M = (Q, Σ, δ, q 0, F) be a DFA that accepts it. We build an NFA N drop s.t L(N drop ) = DropChar(L): N drop = (Q {1, 2}, Σ ε, δ, (q 0, 1), F 2) Recitation 2 - Non Deterministic Finite Automata (NFA) and October Regular 28, Expressions 2015 19 / 22
Closure Under DropChar (Cont.) Proof (Cont.) Formally, ) ( ) q Q, σ Σ. δ ((q, 1), σ = δ(q, σ), 1 ) ( ) q Q, σ Σ. δ ((q, 2), σ = δ(q, σ), 2 ) { ( ) } q Q. δ ((q, 1), ε = δ(q, σ), 2 σ Σ Recitation 2 - Non Deterministic Finite Automata (NFA) and October Regular 28, Expressions 2015 20 / 22
Closure Under DropChar (Cont.) Proof (Cont.) Claim: L(N drop ) = DropChar(L) Proof: w DropChar(L) w 1, w 2 Σ. w = w 1 w 2 and σ Σ. w 1 σw 2 L w 1 σw 2 L(M) q 1, q 2 Q, q f F. (i) δ(q 0, w 1 ) = q 1 (ii) δ(q 1, σ) = q 2 (iii) δ(q 2, w 2 ) = q f (i) δ ((q 0, 1), w 1 ) (q 1, 1) (ii) δ ((q 1, 1), ε)) (q 2, 2) (iii) δ ((q 2, 2), w 2 ) = {(q f, 2)} w 1 w 2 L(N drop ) Recitation 2 - Non Deterministic Finite Automata (NFA) and October Regular 28, Expressions 2015 21 / 22
The End Recitation 2 - Non Deterministic Finite Automata (NFA) and October Regular 28, Expressions 2015 22 / 22