Presentation is loading. Please wait.

Presentation is loading. Please wait.

Tutorial 03 -- CSC3130 : Formal Languages and Automata Theory Haifeng Wan ( ) 2009-09-27.

Similar presentations


Presentation on theme: "Tutorial 03 -- CSC3130 : Formal Languages and Automata Theory Haifeng Wan ( ) 2009-09-27."— Presentation transcript:

1 Tutorial 03 -- CSC3130 : Formal Languages and Automata Theory Haifeng Wan ( hfwan@cse.cuhk.edu.hk ) 2009-09-27

2 Outline Pumping Lemma DFA Minimization Context-free Languages

3 Pigeonhole Principle Pigeonhole principle If m objects are put into n containers, where m>n, then at least one container must hold more than one object. The pigeonhole can be used to prove that certain infinite languages are not regular. Remind: any finite language is regular.

4 Pumping Lemma for Regular Languages Theorem: For every regular language L There exists a number n such that for every string z in L, we can write z = uvw where  |uv| ≤ n  |v| ≥ 1  For every i ≥ 0, the string u v i w is in L. z …… u v w

5 Pumping Lemma What does the Pumping Lemma say? If an infinite language is regular, it can be defined by a DFA. The DFA has a finite number m of states. Since the language is infinite, some strings of the language must have length greater than m. For a string of length greater than m accepted by the DFA, the walk through the DFA must contain a cycle. Repeating the cycle an arbitrary number of times must yield another string accepted by the DFA. Remind: the Pumping Lemma is not sufficient. It is one way to prove that a given infinite language is not regular, while it cannot be used to prove that a given infinite language is regular.

6 Outline to Prove by Pumping Lemma Main idea: prove by contradiction. Brief outline: Assume the language L is regular (and thus the Pumping Lemma holds). Show that repeating the cycle some number of times (“pumping” the cycle) yields a string that is not in L. Conclude that L is not regular by contradiction. What can we think about during using Pumping Lemma? On choosing the particular string z in L. On choosing the number of times to “pump” the cycle.

7 Example 1 Prove that L 3 ={uu: u in {0,1}*} is not regular. Suppose L 3 is regular,there exists n Choose a string z=0 m 10 m 1 with m>n, Although the decomposition of z into uvw is unknown, uv must consist entirely of 0 s because |uv| ≤ n. Moreover, |v| ≥1. Simply choose i=2. Thus uv 2 w will have more 0 s before the first 1 than the second 1, which is not in L 3. Thus L 3 is not regular due to the contradiction.

8 Prove that L={x: x has different numbers of 0s and 1s} is not regular. Trick: Instead of directly prove this, let’s prove its dual stated language D={x: x has the same number of 0s and 1s} is not regular. Steps: Remind that we have proven L={0 n 1 n : n≥0} is not regular. And L = D If L is regular, then D should also be regular. Thus D is not regular according to the contradiction. Neither is L.. Example 2

9 Take x = 0^n1^{n! + n}. Then the adversary splits it as uvw. Let k be the length of the v part. Now pump it (n!+k)/k times. Then you get uv^iw = 0^{k((n!+k)/k) + (n- k)}1^{n!+n} = 0^{n! + n}1^{n! + n}

10 Example Prove that L 2 ={ 1 m : m is prime} is not regular. Suppose L 2 is regular, and thus Pumping Lemma holds. Although n is unknown, we can still assume that there is one. Choose a string z=1 m where m is a prime number and |uvw|=m>n+1. Any prefix of z consists entirely of 1 s. Although the decomposition of z into uvw is unknown, it follows that |w|>1 due to |uvw|> n. Moreover, |v| ≥1. Choose i=|uw|. (Remind |w|>1 and |uw|>1 ). We have |u v i w|=|uw|+|v||uw|=(1+|v|)|uw|. Because both 1+|v| and |uw| are greater than 1, the product must be a composite number, i.e., |u v i w| is a composite not a prime number. It is not in L 2. Thus, L 2 is not regular due to the contradiction. Q.E.D.

11 Outline Pumping Lemma DFA Minimization Context-free grammars (CFG)

12 DFA Minimization There is an algorithm to start with any DFA and reduce it to the smallest possible DFA The algorithm attempts to identify classes of equivalent states These are states that can be merged together without affecting the answer of the computation

13 Equivalent and Distinguishable States Two states q, q’ are equivalent if Here,  (q, w) is the state that the machine is in if it starts at q and reads the string w q, q’ are distinguishable if they are not equivalent: ^ For every string w, the states  (q, w) and  (q’, w) are either both accepting or both rejecting ^ ^ For some string w, one of the states  (q, w),  (q’, w) is accepting and the other is rejecting

14 DFA Minimization Algorithm Find all pairs of distinguishable states as follows: For any pair of states q, q’ : If q is accepting and q’ is rejecting Mark (q, q’) as distinguishable Repeat until nothing is marked: For any pair of states (q, q’) : For every alphabet symbol a : If (  (q, a),  (q’, a)) are marked as distinguishable Mark (q, q’) as distinguishable For any pair of states (q, q’) : If (q, q’) is not marked as distinguishable Merge q and q’ into a single state   

15 Example 1 qq qq qq qq 0 1 0 qq qq qq qq qq qq qq qq qq 00 0, 1 1 1 1 qq qq

16 Example 1 (cont.) qq qq qq qq 0 1 0 qq qq qq qq qq qq qq qq qq 00 0, 1 1 1 1 xxxx  q 4 is distinguishable from all other states

17 Example 1 (cont.) qq qq qq qq 0 1 0 qq qq qq qq qq qq qq qq qq 00 0, 1 1 1 1 xxxx  q 0 is distinguishable from q 1, q 2, q 3, q 4 x x x

18 Example 1 (cont.) qq qq qq qq 0 1 0 qq qq qq qq qq qq qq qq qq 00 0, 1 1 1 1 xxxx x x x  Merge states not marked distinguishable q 0 cannot be merged → group A q , q 2, q 3 are equivalent → group B q 4 cannot be merged → group C B BB

19 Example 1 (cont.) qq qq qq qq 0 1 0 qq qq qq qq qq qq qq qq qq 00 0, 1 1 1 1 xxxx x x x B BB qq qq qCqC 1 minimized DFA: 0 0, 1 A B C

20 Example 2 qq qq qq qq qq qq qq 0 01 1 1 0 0 110 1 1 0 0 qq qq qq qq qq qq qq qq qq qq qq qq

21 Example 2 (cont.) qq qq qq qq qq qq qq 0 01 1 1 0 0 110 1 1 0 0 qq qq qq qq qq qq qq qq qq qq qq qq xx x x x x  q 2 is distinguishable from all other states

22 Example 2 (cont.) qq qq qq qq qq qq qq 0 01 1 1 0 0 110 1 1 0 0  q 0 is distinguishable from q 1, q 2, q 4, q 5, q 6 qq qq qq qq qq qq qq qq qq qq qq qq xx x x x x x x x x

23 Example 2 (cont.) qq qq qq qq qq qq qq 0 01 1 1 0 0 110 1 1 0 0 qq qq qq qq qq qq qq qq qq qq qq qq xx x x x x  q 1 is distinguishable from q 0, q 2, q 3, q 4, q 5 x x x x x x x

24 Example 2 (cont.) qq qq qq qq qq qq qq 0 01 1 1 0 0 110 1 1 0 0 qq qq qq qq qq qq qq qq qq qq qq qq xx x x x x  q 3 is distinguishable from q 1, q 2, q 4, q 5, q 6 x x x x x x x x xx

25 Example 2 (cont.) qq qq qq qq qq qq qq 0 01 1 1 0 0 110 1 1 0 0 qq qq qq qq qq qq qq qq qq qq qq qq xx x x x x  q 4 is distinguishable from q 0, q 1, q 2, q 3, q 5, q 6 x x x x x x x x xxx x

26 Example 2 (cont.) qq qq qq qq qq qq qq 0 01 1 1 0 0 110 1 1 0 0 qq qq qq qq qq qq qq qq qq qq qq qq xx x x x x  q 5 is distinguishable from q 0, q 1, q 2, q 3, q 4, q 6 x x x x x x x x xxx xx

27 Example 2 (cont.) qq qq qq qq qq qq qq 0 01 1 1 0 0 110 1 1 0 0 qq qq qq qq qq qq qq qq qq qq qq qq xx x x x x x x x x x x x x xxx xx  Merge states not marked distinguishable q 0, q 3 are equivalent → group A q , q 6 are equivalent → group B q 2 cannot be merged → group C q 4 cannot be merged → group D q 5 cannot be merged → group E

28 Example 2 (cont.) qq qq qq qq qq qq qq 0 01 1 1 0 0 110 1 1 0 0 qq qq qq qq qq qq qq qq qq qq qq qq xx x x x x x x x x x x x x xxx xx qAqA qEqE qCqC 1 minimized DFA: A B C DE qBqB qDqD 0 0 1 0 1 0 1 1 0

29 Outline Pumping Lemma DFA Minimization Context-free Languages

30 Relations Context-free Languages L Context-free Grammars G Push-down Automata M L = L(G) L = L(M) L(G) = L(M) PDA = NFA + a stack (infinite memory)

31 Example (I) Given the following CFG S  X | Y X  aXb | aX | a Y  aYb | Yb | b (1) L(G) = ? Σ={a, b}

32 Example (I) --- solution: L(S) S  X | Y X  aXb | aX | a Y  aYb | Yb | b Try to write some strings generated by it: S  X  aXb  aaXbb  aaaXbb  aaaabb S  Y  aYb  aYbb  aaYbbb  aabbbb more a’s than b’s more b’s than a’s Observations: Start from S, we can enter two States X & Y, and X, Y are “independent”; In X state, always more a are generated; In Y state, always more b are generated. Ls = Lx U Ly Lx = { a i b j ; i>j } Lx = { a i b j ; i<j } L(S) = { a i b j ; i≠j }

33 End of this tutorial! Thanks for coming!

34 Example (II) Given the following language: (1) design a CFG for it; L = {0 i 1 j : i ≤ j ≤ 2i, i=0,1,…},  = {0, 1}

35 Example (II) -- solution: CFG L = {0 i 1 j : i ≤ j ≤ 2i, i=0,1,…},  = {0, 1} Consider two extreme cases: (a). if j = i, then L 1 = { 0 i 1 j : i=j }; (b). if j = 2i, then L 2 = { 0 i 1 j : 2i=j }. S  0S1 S  ε S  0S11 S  ε If i ≤ j ≤ 2i, then randomly choose “red- rule” or “blue-rule” in the generation. “red-rule” “blue-rule” S  0S1 S  0S11 S  ε

36 Example (II) -- solution: CFG L = {0 i 1 j : i ≤ j ≤ 2i, i=0,1,…},  = {0, 1} S  0S1 S  0S11 S  ε Need to verify L = L(G) G = 1). L(G) is a subset of L: The “red-rule” and “blue-rule” guarantee that in each derivation, the number of 1s generated is one or two times larger than that of 0s. So, L(G) is a subset of L. 2). L is a subset of L(G): For any w = 0 i 1 j, i ≤ j ≤ 2i, we use “red-rule” ( 2i - j ) times and then “blue-rule” ( j - i ) times, i.e., S =*=> 0 2i-j S1 2i-j =*=> 0 2i-j 0 j-i S1 2(j-i) 1 2i-j ==> 0 i 1 j = w


Download ppt "Tutorial 03 -- CSC3130 : Formal Languages and Automata Theory Haifeng Wan ( ) 2009-09-27."

Similar presentations


Ads by Google