Tutorial 03 CSC3130 : Formal Languages and Automata Theory - PowerPoint PPT Presentation

1 / 36
About This Presentation
Title:

Tutorial 03 CSC3130 : Formal Languages and Automata Theory

Description:

Tutorial 03 CSC3130 : Formal Languages and Automata Theory – PowerPoint PPT presentation

Number of Views:200
Avg rating:3.0/5.0
Slides: 37
Provided by: cseCu
Category:

less

Transcript and Presenter's Notes

Title: Tutorial 03 CSC3130 : Formal Languages and Automata Theory


1
Tutorial 03-- CSC3130 Formal Languages and
Automata Theory
  • Haifeng Wan (hfwan_at_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
    mgtn, 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 vi w is in L.
v
z


w
u
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 L3uu u in 0,1 is not regular.
  • Suppose L3 is regular,there exists n
  • Choose a string z0m10m1 with mgtn,
  • Although the decomposition of z into uvw is
    unknown, uv must consist entirely of 0s because
    uvn. Moreover, v1.
  • Simply choose i2. Thus uv2w will have more 0s
    before the first 1 than the second 1, which is
    not in L3.
  • Thus L3 is not regular due to the contradiction.

8
Example 2
  • Prove that Lx x has different numbers of 0s
    and 1s is not regular.
  • Trick
  • Instead of directly prove this, lets prove its
    dual stated language Dx x has the same number
    of 0s and 1s is not regular.
  • Steps
  • Remind that we have proven L0n1n n0 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..

9
  • Take x 0n1n! 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 uviw 0k((n!k)/k)
    (n-k)1n!n 0n! n1n! n

10
Example
  • Prove that L2 1m m is prime is not regular.
  • Suppose L2 is regular, and thus Pumping Lemma
    holds. Although n is unknown, we can still assume
    that there is one.
  • Choose a string z1m where m is a prime number
    and uvwmgtn1. Any prefix of z consists
    entirely of 1s.
  • Although the decomposition of z into uvw is
    unknown, it follows that wgt1 due to uvwgt n.
    Moreover, v1.
  • Choose iuw. (Remind wgt1 and uwgt1). We have
    u vi wuwvuw(1v)uw. Because both
    1v and uw are greater than 1, the product
    must be a composite number, i.e., u vi w is a
    composite not a prime number. It is not in L2 .
  • Thus, L2 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, d(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 d(q, w) and d(q,
w) are either both accepting or both rejecting

For some string w, one of the states d(q, w),
d(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
(d(q, a), d(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
q0
q1
1
0
0, 1
q1
0
0
q2
1
q0
q4
q2
q3
0
1
q4
1
q3
q4
q0
q1
q2
q3
16
Example 1 (cont.)
q1
1
0
0, 1
q1
0
0
q2
1
q0
q4
q2
q3
0
1
q4
x
x
x
x
1
q3
q0
q1
q2
q3
? q4 is distinguishable from all other states
17
Example 1 (cont.)
q1
1
0
0, 1
x
q1
0
0
x
q2
1
q0
q4
q2
x
q3
0
1
q4
x
x
x
x
1
q3
q0
q1
q2
q3
? q0 is distinguishable from q1, q2, q3, q4
18
Example 1 (cont.)
q1
1
0
0, 1
x
q1
0
0
x
B
q2
1
q0
q4
q2
x
B
B
q3
0
1
q4
x
x
x
x
1
q3
q0
q1
q2
q3
? Merge states not marked distinguishable q0
cannot be merged ? group A q1, q2, q3 are
equivalent ? group B q4 cannot be merged ?
group C
19
Example 1 (cont.)
B
q1
1
0
0, 1
x
q1
A
C
0
0
x
B
q2
1
q0
q4
q2
x
B
B
q3
0
1
q4
x
x
x
x
1
q3
q0
q1
q2
q3
0
0, 1
0, 1
1
qA
minimized DFA
qB
qC
20
Example 2
0
1
q1
q2
0
1
q0
q1
q2
q3
0
1
q4
0
1
q5
1
1
0
q3
q4
q5
q6
q6
1
0
q5
q0
q1
q2
q4
q3
0
21
Example 2 (cont.)
0
1
q1
q2
x
x
0
1
q0
q1
q2
q3
x
0
1
x
q4
0
1
x
q5
1
1
0
q3
q4
q5
q6
q6
x
1
0
q5
q0
q1
q2
q4
q3
0
? q2 is distinguishable from all other states
22
Example 2 (cont.)
0
1
q1
x
q2
x
x
0
1
q0
q1
q2
q3
x
0
1
x
x
q4
0
1
x
x
q5
1
1
0
q3
q4
q5
q6
q6
x
x
1
0
q5
q0
q1
q2
q4
q3
0
? q0 is distinguishable from q1, q2, q4, q5, q6
23
Example 2 (cont.)
0
1
q1
x
q2
x
x
0
1
q0
q1
q2
q3
x
x
0
1
x
x
x
q4
0
1
x
x
x
q5
1
1
0
q3
q4
q5
q6
q6
x
x
1
0
q5
q0
q1
q2
q4
q3
0
? q1 is distinguishable from q0, q2, q3, q4, q5
24
Example 2 (cont.)
0
1
q1
x
q2
x
x
0
1
q0
q1
q2
q3
x
x
0
1
x
x
x
x
q4
0
1
x
x
x
x
q5
1
1
0
q3
q4
q5
q6
q6
x
x
x
1
0
q5
q0
q1
q2
q4
q3
0
? q3 is distinguishable from q1, q2, q4, q5, q6
25
Example 2 (cont.)
0
1
q1
x
q2
x
x
0
1
q0
q1
q2
q3
x
x
0
1
x
x
x
x
q4
0
1
x
x
x
x
x
q5
1
1
0
q3
q4
q5
q6
q6
x
x
x
x
1
0
q5
q0
q1
q2
q4
q3
0
? q4 is distinguishable from q0, q1, q2, q3, q5,
q6
26
Example 2 (cont.)
0
1
q1
x
q2
x
x
0
1
q0
q1
q2
q3
x
x
0
1
x
x
x
x
q4
0
1
x
x
x
x
x
q5
1
1
0
q3
q4
q5
q6
q6
x
x
x
x
x
1
0
q5
q0
q1
q2
q4
q3
0
? q5 is distinguishable from q0, q1, q2, q3, q4,
q6
27
Example 2 (cont.)
0
1
q1
x
q2
x
x
0
1
q0
q1
q2
q3
x
x
0
1
x
x
x
x
q4
0
1
x
x
x
x
x
q5
1
1
0
q3
q4
q5
q6
q6
x
x
x
x
x
1
0
q5
q0
q1
q2
q4
q3
0
? Merge states not marked distinguishable q0,
q3 are equivalent ? group A q1,
q6 are equivalent ? group B q2 cannot be
merged ? group C q4 cannot be merged ?
group D q5 cannot be merged ? group E
28
Example 2 (cont.)
B
0
1
q1
x
A
q2
x
x
0
1
q0
q1
q2
q3
x
x
0
1
C
x
x
x
x
q4
0
1
x
x
x
x
x
q5
1
1
0
q3
q4
q5
q6
q6
x
x
x
x
x
1
0
D
E
q5
q0
q1
q2
q4
q3
0
1
0
1
qE
qD
1
qA
0
0
0
1
qB
qC
1
minimized DFA
0
29
Outline
  • Pumping Lemma
  • DFA Minimization
  • Context-free Languages

30
Relations
L L(G)
Context-free Languages L
Context-free Grammars G
L(G) L(M)
L L(M)
Push-down Automata 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) ?

Sa, 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?aa
bbbb
more as than bs
more bs than as
  • 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
L(S) aibj i?j
Lx aibj igtj
Lx aibj iltj
33
End of this tutorial!Thanks for coming!
34
Example (II)
  • Given the following language
  • (1) design a CFG for it

L 0i1j i j 2i, i0,1,, S 0, 1
35
Example (II) -- solution CFG
  • L 0i1j i j 2i, i0,1,, S 0, 1

Consider two extreme cases (a). if j i, then
L1 0i1j ij (b). if j 2i, then L2
0i1j 2ij .
S ? 0S1 S ? e
S ? 0S11 S ? e
blue-rule
red-rule
If i j 2i , then randomly choose red-rule
or blue-rule in the generation.
S ? 0S1 S ? 0S11 S ? e
36
Example (II) -- solution CFG
  • L 0i1j i j 2i, i0,1,, S 0, 1

?
Need to verify L L(G)
S ? 0S1 S ? 0S11 S ? e
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 0i1j, i
j 2i, we use red-rule (2i - j) times and then
blue-rule ( j - i ) times, i.e.,
S gt 02i-jS12i-j gt 02i-j0j-iS12(j-i)12i-j
gt 0i1j w
Write a Comment
User Comments (0)
About PowerShow.com