Presentation is loading. Please wait.

Presentation is loading. Please wait.

FSA Lecture 1 Finite State Machines. Creating a Automaton  Given a language L over an alphabet , design a deterministic finite automaton (DFA) M such.

Similar presentations


Presentation on theme: "FSA Lecture 1 Finite State Machines. Creating a Automaton  Given a language L over an alphabet , design a deterministic finite automaton (DFA) M such."— Presentation transcript:

1 FSA Lecture 1 Finite State Machines

2 Creating a Automaton  Given a language L over an alphabet , design a deterministic finite automaton (DFA) M such that L(M) = L.

3 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 }  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

4 q oe q oo q ee 1 0 1 0 0 0 1 1 q eo Example 1

5

6 Example 2  L 2 = { w | w is a string over {0, 1} that does not contain an even number of 0s and an odd number of 1s } = L 1

7 q eo 1 0 1 0 0 0 1 1 Example 2 q ee q oe q oo

8 Example 3  L 3 = { 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}

9 Example 3 q0q0 q1q1 q2q2 0, 1 q3q3 q4q4

10 Example 4  L 4 = { 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}}

11 Example 4 q0q0 q1q1 0 1 0 1 q2q2 0, 1

12 Machine M accepts string w  If there exists a sequence of states r 0, r 1, …, r n in Q such that 1) r 0 = q 0 2)  (r i, w i+1 ) = r i+1, for i=0,…,n-1 3) r n in F Note: w = w 1 w 2 …w n

13 Regular Languages  Machine M recognizes language A if A = {w| M accepts w}  A language is called regular if some finite automaton recognizes it.

14 Regular Operations  Let A and B be languages. Union A  B = { x | x in A or x in B} Concatenation A  B = {xy | x in A and y in B} Star A* = {x 1 x 2 …x k | k  0 and each x j in A} Note:  is always a member of A*.

15 Regular languages are closed under union  Let A 1 and A 2 be regular languages. We want to show A 1  A 2 is a regular language. Since A 1 and A 2 are regular languages there exists a finite automaton M 1 and there exists a finite automaton M 2 such that M 1 recognizes A 1 and M 2 recognizes A 2. Assume M 1 = (Q 1, ,  1, q 1, F 1 ) and M 2 = (Q 2, ,  2, q 2, F 2 ) It suffices to create a finite automaton M that recognizes A 1  A 2.

16 Continue …  Let a be a symbol in  and states r 1 in Q 1 and r 2 in Q 2. Define M = (Q, , , q 0, F) where Q = Q 1 x Q 2 states  ((r 1, r 2 ), a) = (  1 (r 1, a),  2 (r 2, a)) transition function q 0 = (q 1, q 2 ) start state F = (F 1 x Q 2 )  (Q 1 x F 2 ) final states

17 Regular languages are closed under concatenation  Let A 1 and A 2 be regular languages. We want to show A 1  A 2 is a regular language. Since A 1 and A 2 are regular languages there exists a finite automaton M 1 and there exists a finite automaton M 2 such that M 1 recognizes A 1 and M 2 recognizes A 2. Assume M 1 = (Q 1, ,  1, q 1, F 1 ) and M 2 = (Q 2, ,  2, q 2, F 2 ) It suffices to create a finite automaton M that recognizes A 1  A 2. There is a problem since M doesn’t know where to subdivide the input string into the part accepted by M 1 and the remaining part that will be accepted by M 2. We will return to this later.

18 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 .

19 Example q1q1 q2q2 q3q3 0, 1 1 0,  1 0, 1 q4q4

20 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 .

21 Example q1q1 q2q2 q3q3 0, 1 1 0,  1 0, 1 q4q4

22 q1q1 q1q1 q1q1 q2q2 q1q1 q3q3 q2q2 q4q4 q3q3 q3q3 q1q1 q4q4 q4q4 q3q3 q2q2 q1q1 q3q3 q1q1 q4q4 q4q4 0 111 0 0 1 1 1 1 00 00 1 1 1 11 Input: 010110

23 Non-Deterministic Finite Automaton  N = (Q, , , q 0, 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 power set of Q = {X| X  Q}

24 Machine N accepts string w  If there exists a sequence of states r 0, r 1, …, r n in Q such that 1) r 0 = q 0 2) r i+1 in  (r i, w i+1 ) for i=0,…,n-1 3) r n in F Note: w = w 1 w 2 …w n  (r i, w i+1 ) is a set of states

25 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.

26 Non-deterministic? q0q0 q1q1 0 1 0 1 q2q2 0, 1 Non-deterministic interpretationDeterministic interpretation

27 Deterministic Equivalent? 1 2 3 b a, b a  a

28 DFA from NFA Construction  Assume no  edges. Let N = (Q, , , q 0, F) be an NFA that recognizes language A. We construct a DFA called M = (Q’, ,  ’, q 0 ’, 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}

29 Continued … 3) q 0 ’ = { q 0 } 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} q 0 ’ = E({q 0 }) start state

30 Deterministic Equivalent? 1 2 3 b a, b a  a Deterministic Equivalent Start state q 0 ’= E({1}) = {1,3} Final states F’ = {{1}, {1,2}, {1,3}, {1,2,3}}

31 Final Solution a {1,3} {2} b {3} a { } b b {2,3} a b a, b a b a {1,2,3} b {1,2} a, b {1} a

32 Regular languages are closed under union  Let A 1 and A 2 be regular languages. We want to show A 1  A 2 is a regular language. Since A 1 and A 2 are regular languages there exists an NFA N 1 and there exists an NFA N 2 such that N 1 recognizes A 1 and N 2 recognizes A 2. Assume N 1 = (Q 1, ,  1, q 1, F 1 ) and N 2 = (Q 2, ,  2, q 2, F 2 ) It suffices to create a NFA N that recognizes A 1  A 2.

33 Construction of NFA N1N1 N2N2 q0q0   N = (Q, , , q 0, F) Q = {q 0 }  Q 1  Q 2 F = F 1  F 2  1 (q,a) q in Q 1  (q,a) =  2 (q,a) q in Q 2 {q 1, q 2 } q = q 0 and a=  { } q = q 0 and a  N q1q1 q2q2

34 Regular languages are closed under concatenation  Let A 1 and A 2 be regular languages. We want to show A 1  A 2 is a regular language. Since A 1 and A 2 are regular languages there exists an NFA N 1 and there exists an NFA N 2 such that N 1 recognizes A 1 and N 2 recognizes A 2. Assume N 1 = (Q 1, ,  1, q 1, F 1 ) and N 2 = (Q 2, ,  2, q 2, F 2 ) It suffices to create a NFA N that recognizes A 1  A 2.

35 Construction of NFA N1N1 N2N2   N = (Q, , , q 1, F 2 ) Q = Q 1  Q 2 F = F 2  1 (q,a) q in Q 1 and q not in F 1  (q,a) =  2 (q,a) q in Q 2  1 (q,a)  {q 2 } q in F 1 and a=   1 (q,a) q in F 1 and a  q1q1 q2q2 q1q1 q2q2 N

36 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 N 1 such that N 1 recognizes A. Assume N 1 = (Q 1, ,  1, q 1, F 1 ) It suffices to create a NFA N that recognizes A*.

37 Regular languages are closed under union  Let A 1 and A 2 be regular languages. We want to show A 1  A 2 is a regular language. Since A 1 and A 2 are regular languages there exists an NFA N 1 and there exists an NFA N 2 such that N 1 recognizes A 1 and N 2 recognizes A 2. Assume N 1 = (Q 1, ,  1, q 1, F 1 ) and N 2 = (Q 2, ,  2, q 2, F 2 ) It suffices to create a NFA N that recognizes A 1  A 2.

38 Construction of NFA N1N1 N2N2 q0q0   N = (Q, , , q 0, F) Q = {q 0 }  Q 1  Q 2 F = F 1  F 2  1 (q,a) q in Q 1  (q,a) =  2 (q,a) q in Q 2 {q 1, q 2 } q = q 0 and a=  { } q = q 0 and a  N q1q1 q2q2

39 Regular languages are closed under concatenation  Let A 1 and A 2 be regular languages. We want to show A 1  A 2 is a regular language. Since A 1 and A 2 are regular languages there exists an NFA N 1 and there exists an NFA N 2 such that N 1 recognizes A 1 and N 2 recognizes A 2. Assume N 1 = (Q 1, ,  1, q 1, F 1 ) and N 2 = (Q 2, ,  2, q 2, F 2 ) It suffices to create a NFA N that recognizes A 1  A 2.

40 Construction of NFA N1N1 N2N2   N = (Q, , , q 1, F 2 ) Q = Q 1  Q 2 F = F 2  1 (q,a) q in Q 1 and q not in F 1  (q,a) =  2 (q,a) q in Q 2  1 (q,a)  {q 2 } q in F 1 and a=   1 (q,a) q in F 1 and a  q1q1 q2q2 q1q1 q2q2 N

41 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 N 1 such that N 1 recognizes A. Assume N 1 = (Q 1, ,  1, q 1, F 1 ) It suffices to create a NFA N that recognizes A*.

42 Construct NFA N1N1   N N = (Q, , , q 0, F) Q = {q 0 }  Q 1 F = F 1  {q 0 }  1 (q,a) q in Q 1 and q not in F 1  (q,a) =  1 (q,a) q in F 1 and a   1 (q,a)  {q 1 } q in F 1 and a=  {q 1 } q = q 0 and a=  { } q= q 0 and a  q0q0 q1q1  q1q1

43 Regular Expressions  R is a regular expression if 1) x for some x in  (note: regular expression x represents language {x}) 2)  (empty string) (note: regular expression  represents language {  }) 3)  (empty set) 4) (R 1  R 2 ) where R 1 and R 2 are regular expressions 5) (R 1  R 2 ) where R 1 and R 2 are regular expressions 6) (R 1 *) where R 1 is a regular expression If R is a regular expression then L(R) is the language of R.

44 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}

45 Using Regular Expressions

46 Beginning or End?

47 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.

48 x  NFA that recognizes {x} x

49   NFA that recognizes {  }

50   NFA that recognizes 

51 R 1  R 2, R 1  R 2, or R 1 *  Construct a machine the same way we did to show regular languages are closed under , , or *.

52 NFA to recognize (0  11)* 01 11  0 11   

53 0 11    0 11      

54 Part b) If a language is regular then a regular expression describes it.  Properties of GNFA 1) The start state has transition arrows going to every other state but no arrows coming in from any other state. 2) There is one accept state, and it has arrows coming in from every other state but no arrows going to any other state. The accept state is not the same as the final state. 3) Except for the start and accept states, one arrow goes from every state to every other state and also from each state to itself. 4) The labels on each edge is a regular expression.

55 Example GNFA start accept ab*  b ab  ba b* ab aa (aa)* a*

56 Generalize Non-deterministic Finite Automaton  GNFA is a 5-tuple (Q, , , q start, q accept )  : (Q – {q accept }) x (Q – {q start })   (all regular expressions over  ) (q s, q t )  R qsqs qtqt R

57

58 Example 1 a b a, b 2 b a ba b 2 1 a q start q accept   1 a q start q accept  b(a  b)* q start q accept a*b(a  b)*

59 Example 1.36 (b to c) new(s,2) = old(s,2)  old(s,1) old(1,1)* old(1,2) =     * a = a new(s,3) = old(s,3)  old(s,1) old(1,1)* old(1,3) =     * b = b new(2,2) = old(2,2)  old(2,1) old(1,1)* old(1,2) = b  a  * a = b  aa new(3,3) = old(3,3)  old(3,1) old(1,1)* old(1,3) =   b  * b = bb

60 Example 1.36 (b to c) new(2,3) = old(2,3)  old(2,1) old(1,1)* old(1,3) =   a  * b = ab new(3,2) = old(3,2)  old(3,1) old(1,1)* old(1,2) = a  b  * a = a  ba

61 Example 1.36 (c to d) new(s,a) = old(s,a)  old(s,2) old(2,2)* old(2,a) =   a (aa  b)*  = a(aa  b)* new(s,3) = old(s,3)  old(s,2) old(2,2)* old(2,3) = b  a (aa  b)* ab = b  a(aa  b)* ab new(3,a) = old(3,a)  old(3,2) old(2,2)* old(2,a) =   (ba  a) (aa  b)*  = (ba  a) (aa  b)*   new(3,3) = old(3,3)  old(3,2) old(2,2)* old(2,3) = bb  (ba  a) (aa  b)* ab

62 Example 1.36 (d to e) new(s,a) = old(s,a)  old(s,3) old(3,3)* old(3,a) = a(aa  b)*  (b  a(aa  b)* ab)(bb  (ba  a) (aa  b)* ab)* ((ba  a) (aa  b)*   )

63 Part b) If a language is regular then a regular expression describes it.  Properties of GNFA 1) The start state has transition arrows going to every other state but no arrows coming in from any other state. 2) There is one accept state, and it has arrows coming in from every other state but no arrows going to any other state. The accept state is not the same as the final state. 3) Except for the start and accept states, one arrow goes from every state to every other state and also from each state to itself. 4) The labels on each edge is a regular expression.

64 Example GNFA start accept ab*  b ab  ba b* ab aa (aa)* a*

65 Generalize Non-deterministic Finite Automaton  GNFA is a 5-tuple (Q, , , q start, q accept )  : (Q – {q accept }) x (Q – {q start })   (all regular expressions over  ) (q s, q t )  R qsqs qtqt R

66

67 Example 1 a b a, b 2 b a ba b 2 1 a q start q accept   1 a q start q accept  b(a  b)* q start q accept a*b(a  b)* new(1,q accept ) = old(1, q accept )  old(1,2) old(2,2)* old(2, q accept ) =   b (a  b)*  = b(a  b)* new(q start, q accept ) = old(q start, q accept )  old(q start,1) old(1,1)* old(1, q accept ) =    a* b(a  b)* = a*b(a  b)*

68 Example Remove vertex 2: new(1,q accept ) = old(1, q accept )  old(1,2) old(2,2)* old(2, q accept ) =   b (a  b)*  = b(a  b)* Remove vertx 1:| new(q start, q accept ) = old(q start, q accept )  old(q start,1) old(1,1)* old(1, q accept ) =    a* b(a  b)* = a* b(a  b)*

69 Example 1.36 s b 1 3 2 a a a b b 1 3 2 a a a b b a    b s 3 2 a ab b a   ba  a aa  b bb s 3 a (ba  a) (aa  b)*   b  a(aa  b)* ab a(aa  b)* bb  (ba  a) (aa  b)*ab

70 Example 1.36 (b to c) new(s,2) = old(s,2)  old(s,1) old(1,1)* old(1,2) =     * a = a new(s,3) = old(s,3)  old(s,1) old(1,1)* old(1,3) =     * b = b new(2,2) = old(2,2)  old(2,1) old(1,1)* old(1,2) = b  a  * a = b  aa new(3,3) = old(3,3)  old(3,1) old(1,1)* old(1,3) =   b  * b = bb Remove vertex 1

71 Example 1.36 (b to c) new(2,3) = old(2,3)  old(2,1) old(1,1)* old(1,3) =   a  * b = ab new(3,2) = old(3,2)  old(3,1) old(1,1)* old(1,2) = a  b  * a = a  ba Remove vertex 1

72 Example 1.36 (c to d) new(s,a) = old(s,a)  old(s,2) old(2,2)* old(2,a) =   a (aa  b)*  = a(aa  b)* new(s,3) = old(s,3)  old(s,2) old(2,2)* old(2,3) = b  a (aa  b)* ab = b  a(aa  b)* ab new(3,a) = old(3,a)  old(3,2) old(2,2)* old(2,a) =   (ba  a) (aa  b)*  = (ba  a) (aa  b)*   new(3,3) = old(3,3)  old(3,2) old(2,2)* old(2,3) = bb  (ba  a) (aa  b)* ab Remove vertex 2

73 Example 1.36 (d to e) new(s,a) = old(s,a)  old(s,3) old(3,3)* old(3,a) = a(aa  b)*  (b  a(aa  b)* ab)(bb  (ba  a) (aa  b)* ab)* ((ba  a) (aa  b)*   ) Remove vertex 3 s a a(aa  b)*  (b  a(aa  b)* ab)(bb  (ba  a) (aa  b)* ab)* ((ba  a) (aa  b)*   )

74 Pumping Lemma  Purpose: Used to prove a language is not regular.  What does it say? All strings in a regular language can be “pumped” if they are at least as long as the pumping length p. Suppose xyz represents a string in the language whose length is at least as long as p. There is a section of the string (say y) that can be repeated, i.e., xy k z, where k>=0 is also a member of the language.

75 Pumping Lemma  If A is a regular language, then there is a number p (the pumping length) where, if s is any string in A of length at least p, then s can be divided into three pieces, s = xyz satisfying the following conditions 1) for each k  0, the string xy k z is in A. 2) |y| > 0 3) |xy|  p. (Note form of this theorem is: R  S)

76 Sketch Proof  Since A is a regular language there exists a DFA M = (Q, , ,q 1,F) with p states that recognizes A.  Either A has strings of length at least p or it doesn’t. Case 1: Suppose no string in A has length at least p Then the Pumping Lemma (R  S) is vacuously true since the antecedent R is False. Case 2: See next page.

77 Sketch Proof  Let s be a string in A of length n, where n is at least p. Starting in q 1, M processes the string s by visiting n+1 states (namely, r 1 = q 1, r 2, … r n+1 ). By the Pigeonhole Principle (n+1 pigeons and p nests), some state must have been visited more than once (say q x ).

78 Sketch Proof  s = s 1 s 2 s 3 … s’ … s” … s n q 1 r 2 r 3 … q x … q x … r n+1 q1q1 qxqx r n+1 x y z Repetition first occurs when see the (p+1) state

79 Nonregular Languages  Consider the language L={0 n 1 n | n  0}. Assume L is a regular language. Because of this, there exists a DFA with p states that recognizes L. Consider the string s = 0 p 1 p from L. Since its length is at least as long as p, it follows from the Pumping Lemma that 1) s = xyz and xy k z in A for k  0 2) |y| > 0 3) |xy|  p Choose carefully

80 NonRegular Languages  By (3) xy consists of all 0s. Case 1: |xy| = p xy = 0 p-u 0 u where u>0 and z = 1 p Consider xz = 0 p-u 1 p. Pumping Lemma says xz in L. This is a contradiction. Case 2: |xy| 0 Consider xy 2 z = 0 t-v 0 v 0 v 0 p-t 1 p = 0 p+v 1 p. Pumping Lemma says xy 2 z in L. This is a contradiction.  Therefore L is not regular.

81 Another s  What if s = (01) p was chosen instead? s = (01) p = xyz Regardless how y is chosen, y can always be pumped. For example, if y = (01) k then it can be pumped.

82 Are Regular Languages Closed Under Other Operations?  Closed under union  Closed under intersection (see page 46)  Closed under complement (see exercise 1.10)  Closed under concatenation  Closed under star

83 Nonregular Languages  Consider the language L 2 ={w in  * | w has the same number of 0s as 1s}. Show L 2 is not regular. We know the language A = 0*1* is regular since it can be represented using regular expressions. Suppose L 2 is regular then A  L 2 = {0 n 1 n | n  0}is regular is a contradiction. Therefore L 2 is not regular.

84 Minimum Pumping Length  The minimum pumping length for a regular language A is the smallest p that is a pumping length of A.

85 Minimum Pumping Length  What is the minimum pumping length for 01*? s = 0 = xyz  x = , y = 0, z =  by Pumping Lemma Can’t pump y! Let p be greater than zero. s = 01 p = xyz  x = , y = 0, z = 1 p can’t pump y x = 0, y = 1, z = 1 p-1 can pump y, |xy|=2 x = , y = 01, z = 1 p-1 can’t pump y 2

86 Minimum Pumping Length  What is the minimum pumping length for 11? s = 11 = xyz  x = , y = 1, z = 1 can’t pump y x = 1, y = 1, z =  can’t pump y x = , y = 11, z =  can’t pump y minimum pumping length is 3 (vacuously true) 3


Download ppt "FSA Lecture 1 Finite State Machines. Creating a Automaton  Given a language L over an alphabet , design a deterministic finite automaton (DFA) M such."

Similar presentations


Ads by Google