860 likes | 996 Views
FSA Lecture 1. Finite State Machines. Creating a Automaton. Given a language L over an alphabet , design a deterministic finite automaton (DFA) M such that L(M) = L. Example 1. L 1 = { w | w is a string over {0, 1} that contains an even number of 0s and an odd number of 1s }
E N D
FSA Lecture 1 Finite State Machines
Creating a Automaton • Given a language L over an alphabet , design a deterministic finite automaton (DFA) M such that L(M) = L.
Example 1 • L1 = { w | w is a string over {0, 1} that contains an even number of 0s and an odd number of 1s } • Method:Define nodes to represent when a) both an even number of 0s and 1s have been seen in the input b) both an odd number of 0s and 1s have been seen in the input c) an even number of 0s and an odd number of 1s have been seen in the input d) an even number of 1s and an odd number of 0s have been seen in the input
Example 1 1 qee qeo 1 0 0 0 0 1 qoe qoo 1
Example 2 • L2 = { w | w is a string over {0, 1} that does not contain an even number of 0s and an odd number of 1s } = L1
Example 2 1 qeo qee 1 0 0 0 0 1 qoe qoo 1
Example 3 • L3 = { w | w is a string over {0, 1} such that |w| 3} = {, 0, 1, 00, 01, 10, 11, 000, 001, 010, 011, 100, 101, 110, 111}
Example 3 0, 1 0, 1 0, 1 q2 q3 q0 q1 0, 1 q4 0, 1
Example 4 • L4 = { w | w is a string over {0, 1} such that w contains the substring 11} = { w | w = x11y, where x and y are strings over {0, 1}}
Example 4 0 0, 1 q0 q1 1 1 q2 0
Machine M accepts string w • If there exists a sequence of states r0, r1, …, rn in Q such that1) r0 = q0 2) (ri , wi+1) = ri+1, for i=0,…,n-1 3) rn in FNote: w = w1w2…wn
Regular Languages • Machine M recognizes language A if A = {w| M accepts w} • A language is called regular if some finite automaton recognizes it.
Regular Operations • Let A and B be languages. • UnionA B = { x | x in A or x in B} • ConcatenationA B = {xy | x in A and y in B} • StarA* = {x1x2…xk | k 0 and each xj in A}Note: is always a member of A*.
Regular languages are closed under union • Let A1 and A2 be regular languages. We want to show A1A2 is a regular language. Since A1 and A2 are regular languages there exists a finite automaton M1 and there exists a finite automaton M2 such that M1 recognizes A1 and M2 recognizes A2. Assume M1 = (Q1, , 1, q1, F1) and M2 = (Q2, , 2, q2, F2) It suffices to create a finite automaton M that recognizes A1A2.
Continue … • Let a be a symbol in and states r1 in Q1 and r2 in Q2. Define M = (Q, , , q0, F) where Q =Q1 x Q2 states ((r1, r2), a) = (1(r1, a), 2(r2, a)) transition function q0 = (q1, q2) start stateF = (F1 x Q2) (Q1 x F2) final states
Regular languages are closed under concatenation • Let A1 and A2 be regular languages. We want to show A1 A2 is a regular language. Since A1 and A2 are regular languages there exists a finite automaton M1 and there exists a finite automaton M2 such that M1 recognizes A1 and M2 recognizes A2. Assume M1 = (Q1, , 1, q1, F1) and M2 = (Q2, , 2, q2, F2) It suffices to create a finite automaton M that recognizes A1 A2. There is a problem since M doesn’t know where to subdivide the input string into the part accepted by M1 and the remaining part that will be accepted by M2. We will return to this later.
Non-Deterministic Automaton • NFAs generalize DFAs. • In a DFA, each state has exactly one transition for each symbol in the alphabet. • In an NFA, at any state there may be zero or more transitions for a symbol in the alphabet. • In a DFA, a label on a transition arrow is a symbol in the alphabet. • In an NFA, a label on a transition arrow is a symbol in the alphabet or .
Example 0, 1 0, 1 0, 1 1 q1 q2 q3 q4
Non-Deterministic Automaton • NFAs generalize DFAs. • In a DFA, each state has exactly one transition for each symbol in the alphabet. • In an NFA, at any state there may be zero or more transitions for a symbol in the alphabet. • In a DFA, a label on a transition arrow is a symbol in the alphabet. • In an NFA, a label on a transition arrow is a symbol in the alphabet or .
Example 0, 1 0, 1 0, 1 1 q1 q2 q3 q4
Input:010110 q1 0 q1 1 1 1 q1 q2 q3 0 0 q1 q3 1 1 1 1 q1 q2 q3 q4 1 1 1 1 1 q1 q2 q3 q4 q4 0 0 0 0 q1 q3 q4 q4
Non-Deterministic Finite Automaton • N = (Q, , , q0, F)Q is a finite set of states is a finite alphabet: Q x ( {}) (Q) F Q is a set of accept states (Q) is the powerset of Q = {X| X Q}
(ri , wi+1) is a set of states Machine N accepts string w • If there exists a sequence of states r0, r1, …, rn in Q such that1) r0 = q0 2) ri+1 in (ri , wi+1) for i=0,…,n-1 3) rn in FNote: w = w1w2…wn
Are NFAs more powerful than DFAs? • Every deterministic finite automaton has an equivalent non-deterministic finite automaton. (see next slide) • Every non-deterministic finite automaton has an equivalent deterministic finite automaton.
Non-deterministic? 0 0, 1 q0 q1 1 1 q2 0 Deterministic interpretation Non-deterministic interpretation
Deterministic Equivalent? 1 a b 3 2 a a, b
DFA from NFA Construction • Assume no edges.Let N = (Q, , , q0, F)be an NFA that recognizes language A. We construct a DFA called M = (Q’, , ’, q0’, F’)1) Q’ = (Q) 2) For R in Q’ and a in let ’(R,a) = {q in Q| q in (r,a) for some r in R} = (r,a) r in R Q’ = {{}, {1}, {2}, {3}, {1,2}, {1,3}, {2,3}, {1,2,3}} ’({1,2},b) = (1,b) (2,b) = {2} {3} = {2,3}
Continued … 3) q0’ = { q0} 4) F’ = {R in Q’| R contains an accept state of N}Assume edges, then we need these modifications.Let R be a state of M. Define E(R) = {q| q can be reached from R traveling along 0 or more edges}Modify ’(R,a) = {q in Q| q in E((r,a)) for some r in R} = E((r,a)) transition function r in R’({1,2},b) = E((1,b)) E((2,b)) = E({2}) E({3}) = {2,3}’({3},a) = E((3,a)) = E({1}) = {1,3} q0’ = E({q0}) start state
Deterministic Equivalent? 1 Deterministic Equivalent a b 3 2 a a, b Start state q0’= E({1}) = {1,3}Final states F’ = {{1}, {1,2}, {1,3}, {1,2,3}}
a {1} a, b {1,2} Final Solution a a, b {3} { } a b {1,3} b b b a {2} {2,3} a {1,2,3} a b b
Regular languages are closed under union • Let A1 and A2 be regular languages. We want to show A1A2 is a regular language. Since A1 and A2 are regular languages there exists an NFA N1 and there exists an NFA N2 such that N1 recognizes A1 and N2 recognizes A2. Assume N1 = (Q1, , 1, q1, F1) and N2 = (Q2, , 2, q2, F2) It suffices to create a NFA N that recognizes A1A2.
Construction of NFA q1 N1 N q2 q0 N2 N = (Q, , , q0, F)Q = {q0} Q1 Q2F = F1 F21(q,a) q in Q1(q,a) = 2(q,a) q in Q2 {q1, q2} q = q0 and a= { } q = q0 and a
Regular languages are closed under concatenation • Let A1 and A2 be regular languages. We want to show A1 A2 is a regular language. Since A1 and A2 are regular languages there exists an NFA N1 and there exists an NFA N2 such that N1 recognizes A1 and N2 recognizes A2. Assume N1 = (Q1, , 1, q1, F1) and N2 = (Q2, , 2, q2, F2) It suffices to create a NFA N that recognizes A1 A2.
Construction of NFA q1 N1 N q2 q1 N2 q2 N = (Q, , , q1, F2)Q = Q1 Q2F = F21(q,a) q in Q1 and q not in F1(q,a) = 2(q,a) q in Q21(q,a) {q2} q in F1 and a= 1(q,a) q in F1 and a
Regular languages are closed under the star operation • Let A be a regular language. We want to show A*is a regular language. Since A is regular language there exists an NFA N1 such that N1 recognizes A.Assume N1 = (Q1, , 1, q1, F1) It suffices to create a NFA N that recognizes A*.
Regular languages are closed under union • Let A1 and A2 be regular languages. We want to show A1A2 is a regular language. Since A1 and A2 are regular languages there exists an NFA N1 and there exists an NFA N2 such that N1 recognizes A1 and N2 recognizes A2. Assume N1 = (Q1, , 1, q1, F1) and N2 = (Q2, , 2, q2, F2) It suffices to create a NFA N that recognizes A1A2.
Construction of NFA q1 N1 N q2 q0 N2 N = (Q, , , q0, F)Q = {q0} Q1 Q2F = F1 F21(q,a) q in Q1(q,a) = 2(q,a) q in Q2 {q1, q2} q = q0 and a= { } q = q0 and a
Regular languages are closed under concatenation • Let A1 and A2 be regular languages. We want to show A1 A2 is a regular language. Since A1 and A2 are regular languages there exists an NFA N1 and there exists an NFA N2 such that N1 recognizes A1 and N2 recognizes A2. Assume N1 = (Q1, , 1, q1, F1) and N2 = (Q2, , 2, q2, F2) It suffices to create a NFA N that recognizes A1 A2.
Construction of NFA q1 N1 N q2 q1 N2 q2 N = (Q, , , q1, F2)Q = Q1 Q2F = F21(q,a) q in Q1 and q not in F1(q,a) = 2(q,a) q in Q21(q,a) {q2} q in F1 and a= 1(q,a) q in F1 and a
Regular languages are closed under the star operation • Let A be a regular language. We want to show A*is a regular language. Since A is regular language there exists an NFA N1 such that N1 recognizes A.Assume N1 = (Q1, , 1, q1, F1) It suffices to create a NFA N that recognizes A*.
Construct NFA N1 N q1 q1 q0 N = (Q, , , q0, F)Q = {q0} Q1F = F1 {q0}1(q,a) q in Q1and q not in F1(q,a) = 1(q,a) q in F1and a1(q,a) {q1} q in F1and a= {q1} q = q0 and a= { } q= q0 and a
Regular Expressions • R is a regular expression if1) x for some x in (note: regular expression x represents language {x})2) (empty string) (note: regular expression represents language {})3) (empty set)4) (R1 R2) where R1 and R2 are regular expressions5) (R1 R2) where R1 and R2 are regular expressions6) (R1*) where R1 is a regular expressionIf R is a regular expression then L(R) is the language of R.
Examples • 0*0 {w| w contains at least one zero} • * = {} • 11 00 = {11, 00} • 0 *1 = {w| w begins with a 0 and ends in a 1} • (01)* = {, 01, 0101, 010101, 01010101, …} • 1*0 = {w| w contains any number of 1s followed by exactly one 0}
Regular Expressions vs. Regular Languages • A language is regular if and only if some regular expression describes it.Part a) If a regular expression describes a language then it is regular.Part b) If a language is regular then a regular expression describes it.
x • NFA that recognizes {x} x
• NFA that recognizes {}
• NFA that recognizes