View
219
Download
0
Tags:
Embed Size (px)
Citation preview
1
CSCI 3130: Formal Languages and Automata Theory
Tutorial 4
Hung Chun Ho
Office: SHB 1026Department of Computer Science & Engineering
4
DFA Minimization
We will show how to turn any DFA for L into the minimal DFA for L
0
1
…
…
…
…q
q
q
q
q
q
q
q
q
q
q
0
1
0
1
0
1
1
1
1
1
0
1
q
q
q
q
1
1
1
0
0 00
5
DFA Minimization
• After we design a DFA, there may be some redundant states– They have the same “function”– They are indistinguishable– No matter what happens in the future (inputs),
they will have the same fate (accept/reject)!
• We can merge indistinguishable states into groups
6
Indistinguishable States
• DFA example from lecture note
• Indistinguishable states example: {q0, q00}
• Explain: next few slides
0
1
q
q
q
q
q
q
q
0
1
0
1
1
0
01
1
0
0
1
7
Indistinguishable States
• On continuation string (further input) “0”
• q0 Reject
• q00 Reject
0
1
q
q
q
q
q
q
q
0
1
0
1
1
0
01
1
0
0
1
8
Indistinguishable States
• On continuation string “11”
• q0 Accept
• q00 Accept
0
1
q
q
q
q
q
q
q
0
1
0
1
1
0
01
1
0
0
1
9
Indistinguishable States
• On continuation string “01011”
• q0 Accept
• q00 Accept
0
1
q
q
q
q
q
q
q
0
1
0
1
1
0
01
1
0
0
1
10
Indistinguishable States
• On all continuation strings, {q0 , q00} have the same outcome
• q0 , q00 are indistinguishable Can be merged
0
1
q
q
q
q
q
q
q
0
1
0
1
1
0
01
1
0
0
1
11
How to find?
• Infinitely many continuation states– A bit difficult to find indistinguishable states dir
ectly
• We do the reverse:– To recognize distinguishable states– Pair of states, s.t. there is a continuation strin
g, causing them have different “fates”– (De Morgan’s Law, still remember?)
12
Distinguishable States
• For continuation string = “1”
• q01 Accept
• q10 Reject
• Distinguishable0
1
q
q
q
q
q
q
q
0
1
0
1
1
0
01
1
0
0
1
13
Distinguishable States
• For continuation string = “”• q10 Reject
• q11 Accept
• Distinguishable0
1
q
q
q
q
q
q
q
0
1
0
1
1
0
01
1
0
0
1
14
Distinguishable States
• 2 states are distinguishable, if there is a continuation string, that bring them different fate!! (Accept / Reject)– Try all continuation strings? How?
• Idea:– For any pair of {Accept, Reject} states, they
are distinguishable– For these pairs, walk k transitions backward,
mark the obtained pairs as distinguishable
15
Repeat !! as long as you mark some pairs
Finding (in)distinguishable states
Rule 1:
Rule 2:
q q’If q is accepting and q’ is rejectingMark (q, q’) as distinguishable (x)
x
q1 q1’x
xq2 q2’
a a If (q1, q1’) are marked,Mark (q2, q2’) as distinguishable (x)
Rule 3: Unmarked pairs are indistinguishableMerge them into groups
16
Example 1
0
1
q
q
q
q
q
q
q
0
1
0
1
1
0
01
1
0
0
1
x x x x x x
q
q
q
q
q
q
qq q q qq
q11 is distinguishable from all other states
17
Example 1
0
1
q
q
q
q
q
q
q
0
1
0
1
1
0
01
1
0
0
1
x x x x x x
q
q
q
q
q
q
qq q q qq
Look at pair q, q0
0 1
Neither (q0, q00) nor (q1, q01) are distinguishable
18
Example 1
0
1
q
q
q
q
q
q
q
0
1
0
1
1
0
01
1
0
0
1
x x x x x x
q
q
q
q
q
q
qq q q qq
Look at pair q, q1
0 1
(q1, q11) is distinguishable
x
19
Example 1
x
x
x
x
x
xx x x x
q
q
q
q
q
q
qq q q qq
After going thru the whole table onceNow we make another pass
x x xx
0
1
q
q
q
q
q
q
q
0
1
0
1
1
0
01
1
0
0
1
20
Example 1
0
1
q
q
q
q
q
q
q
0
1
0
1
1
0
01
1
0
0
1
x
x
x
x
x
xx x x x
q
q
q
q
q
q
qq q q qq
In the second pass, nothing changesSo we are ready to apply Rule 3
x x xx
21
Example 1
q
q
q
q
q
q
q
x
x
x
x
x
x
x
xx
x
xxx x
q
q
q
q
q
q
qq q q qq
Merge unmarked pairs into groupsUnmarked Cells Pairs of Indistinguishable states
Blue line links indistinguishable states
22
A
BC
Example 1
q
q
q
q
q
q
q
x
x
x
x
x
x
x
xx
x
xxx x
q
q
q
q
q
q
qq q q qq
Merge unmarked pairs into groups
A
A
A
A
AB
A
23
Example 1
0
1
q
q
q
q
q
q
q
0
1
0
1
1
0
01
1
0
0
1
AxAxAx
xAxAx
xBxx
xAx
xx x
q
q
q
q
q
q
qq q q qq
1q q qC1
1
0
00
minimized DFA:
A
BC
24
Example 2
0
1
q
q
q
q
q
q
q
0
1
0
1
1
0
01
1
0
0
1
q
q
q
q
q
q
qq q q qq
x x x x x
q10 is distinguishable from all other states
25
Example 2
0
1
q
q
q
q
q
q
q
0
1
0
1
1
0
01
1
0
0
1
q
q
q
q
q
q
qq q q qq
x x x x xxx x x
x x xx
x x
After going thru the whole table several times(See white board for simulation)
26
Example 2
0
1
q
q
q
q
q
q
q
0
1
0
1
1
0
01
1
0
0
1
q
q
q
q
q
q
qq q q qq
x x x x xxx x x
x x xx
x x
Merge unmarked pairs into groups
27
Why it works?
• For a pair of states, there is a continuation string that bring them different fates– Distinguishable
• Idea: For any pair of {Accept, Reject} states, they are distinguishable, mark them– These pairs have a length-0 continuation string that
distinguishes them
• 1 step backward, mark them as distinguishable– These pairs have a length-1 continuation string that
distinguishes them
28
Why it works?
• 1 further step backward, mark– These pairs have a length-2 continuation
string that distinguishes them
• 1 further step backward, mark– These pairs have a length-3 continuation
string that distinguishes them
• So-on …
30
3) Production Rule
Context-Free Grammar (Recap)
• A context free grammar is consisted of
4) Start Variable
Another Production Rule
1) Variables 2) Terminal
Another Terminal
S AB | ba
A aA | a
B b
31
Context-Free Grammar (Recap)
• A string is said to belong to the language (of the CFG) if it can be derived from the start variable
Derivation
Therefore, aab belongs to the language
S AB | ba
A aA | a
B b
CFG Example
= Apply Production Rule
AB
aAB
aaB
aab
S
32
• L = {w = 0n1n : n is an positive integer}
• L is not a regular language– Proved by “Pumping Lemma”
• A Context-Free Grammar can describe it
• Thus, CFG is more general than regular expression– Recall: NFA Regular Expression DFA
Why CFG?
S 0S1
S 01
33
CFG Design
• Given a context-free language, design the CFG
• L = { ab-string, w : Number of a’s < Number of b’s }
• Some time for you to get into think… 1 min
S ?
…
34
CFG Design (Con’t)
• Idea: Bottom-up– Shortest string in L : “b”– Given a string in L, we can expand it, s.t. it is
still in L– i.e., Add terminals, while not violating the cons
traints
35
CFG Design (Con’t)
Good Try:
S b
S bS | Sb
S abS | baS | bSa | aSb
After adding 1 “b”, number of “b” is still greater than that of “a”
Adding 1 “a” and 1 “b”, the difference between the numbers of “a” and “b” keep constant
However, cannot parse strings like “aabbbaa”
Explaination makes sense.But there is a counter example.
36
CFG Design (Con’t)
Consider the answer:
S b
S SS
S SaS | aSS | SSa
Base Case
#b still > #a
: #b ≥ #a + 1
: #b ≥ #a + 1
: #a = 1
1st S
2nd S
‘a’
Explaination makes sense.But, is the grammar is correct?
SUM #b ≥ #a + 2 - 1
37
A set of strings
CFG Design (Con’t)
• After designing the grammar, G, you may have to prove (if required) that the language of this grammar is equivalent to the given language
• i.e., Prove that L(G) = L• Proof:
Part 1) L(G) L⊂Part 2) L L(G)⊂
• Due to time limit, I will not do this part
38
Parse Tree
• How to parse “aab” in this grammar? (Previous example)
S AB | ba
A aA | a
B b
CFG Example
S AB
aAB
aaB
aab
Derivation
39
AB
aAB
aaB
aab
Parse Tree (Con’t)
• Idea: Production Rule = Node + Children
• Should be very intuitive to understand
S
Derivation S
B
b
a
Aa
A
40Different parse trees!The grammar is ambiguous!
Parse Tree (Con’t)
• Ambiguity:S
S
2
1
AS
S
-
3
-
S - S
1 | 2 | 3
S
S
3 - 1 - 2String:
CFG:
3 – 1 – 2
S
S
3
2
SS
S
-
1
-
3 – (1 – 2)