DCP 3122 Introduction to Formal Languageswgtzeng/courses... · DCP 3122 Introduction to Formal...
Transcript of DCP 3122 Introduction to Formal Languageswgtzeng/courses... · DCP 3122 Introduction to Formal...
Finite Automata
Wen-Guey Tzeng
Computer Science Department
National Chiao Tung University
Syllabus
• Deterministic finite acceptor
• Nondeterministic finite acceptor
• Equivalence of DFA and NFA
• Reduction of the number of states
22017 Fall
Deterministic Finite Acceptor
32017 Fall
• A very low-level machine
– It has only states (control unit).
– It has no memory of any type.
– It has no output.
4
Clock
2017 Fall
• A DFA M is defined by M=(Q, , , q0, F)
– Q: a finite set of states
– : input alphabet (set of symbols)
– : Q x Q: a total function called transition function
– q0Q: the initial state
– FQ: set of final states
• A dfa is an acceptor for strings.
52017 Fall
Example
• M=({q0, q1, q2}, {0,1}, , q0, {q1} ), where
(q0, 0)=q0, (q0, 1)=q1,(q1, 0)=q0, (q1, 1)=q2,(q2, 0)=q2, (q2, 1)=q1
• Transition table
62017 Fall
0 1
q0 q0 q1
q1 q0 q2
q2 q2 q1
• Graph representation
72017 Fall
• How does it work?
• What does it represent?
– accept strings
– reject strings
82017 Fall
From to *
• *: Qx* Q
– *(q,w) = the reached state after reading a string w by starting from state q.
– *(q, ) = q
– *(q, wa) = (*(q, w), a)=p
92017 Fall
Language L(M)
• w is accepted by M if *(q0, w) F
• w is not accepted by M if *(q0, w) F
• The language accepted by M isL(M)= {w * : *(q0, w) F }
102017 Fall
• What language is accepted by M
112017 Fall
• What language is accepted by M
• L(M) = {anb : n0}
122017 Fall
DFA design
• Find a dfa that accepts strings starting with prefix ab,where ={a, b}
132017 Fall
• Find a dfa that accepts strings ending with suffix ab.• Problem: we don’t know when the input ends until the
machine stops
• Technique: each state records the characteristic of “the input string up to the current position”
142017 Fall
• Design a dfa for L={w{0,1}* : w contains substring 001}
• Consider 100001
152017 Fall
• Design a dfa accepting all strings over {0, 1} except those containing substring 001
162017 Fall
• Design a dfa for
L = {w{a,b}* : na(w) is even and nb(w) is odd}
172017 Fall
• Can we design a dfa for L={anbn : n1} ?
182017 Fall
Regular languages
• Definition: a language L is regular if and only if some dfa M accepts L, that is, L=L(M).
• Show that L={awa : w{a,b}*} is regular.
192017 Fall
• Every finite language L is regular.
202017 Fall
• Questions?
– What languages are regular?
– How to determine whether a language L is regular or not?
• Yes design a DFA for L
• No show every DFA cannot accept it. – Feasible?
– Find other ways.
212017 Fall
Nondeterministic Finite Acceptor
22
2017 Fall
• An “imaginary” computing model
• As intermediate object to establish the relation between regular expressions and dfa
• Find important use in classifying complexity classes
232017 Fall
• An NFA M is defined by M=(Q, , , q0, F)– Q: finite set of states
– : input alphabet (set of symbols)
– : Qx({})2Q: nondeterministic transition function
– q0Q: the initial state
– FQ: set of final states
• Note– (q1, a)={q0, q2}
– -transition: (q1, )={q1, q2}
– Unspecified transition: (q1, b): not defined
242017 Fall
252017 Fall
0 1
q0 {q2} - {q1}
q1 - {q0, q2} {q2}
q2 - - -
F={q0}
Example: NFA with -transitions
26
• -transition (q0, )={q2}
• Unspecified (undefined) transition (q0, 0), …
– Going into a dead state
2017 Fall
27
Transition function *
• *(qi, w)=S : the states reachable from qi with input w– qj S if and only if there is a walk, labeled with w,
from qi to qj
– We can insert as many ’s as we want into w.
• Example– *(q0, a) = {q1, q2}– *(q1, b) = {q0}– *(q0, aa)={q1, q2}– *(q0, ab)= {q0}
– *(q0, wa)= ڂ𝒑∈𝜹∗(𝒒𝟎,𝒘)𝜹∗(𝒑, 𝒂)
2017 Fall
How to compute *
• Compute *(q, )
– The states are reachable from q by a sequence of -transitions. Note q*(q, )
– Note the difference between (q, ) and *(q, )
– (q, ) = {p, r}, *(q, ) = {q, p, r, t, s, u}
282017 Fall
q p
r
t
s
u
• *(q0, ) = {q0}, *(q1, ) = {q1, q2}, *(q2, ) = {q2},
• Compute *(q0, a) =
• Compute *(q1, b) =
• Compute *(q0, ab) = ڂ𝑝∈𝛿∗(𝑞,𝑎) 𝛿∗(𝑝, 𝑏)
292017 Fall
Language accepted by nfa
• A string w is accepted by nfa M
if there is a walk w from q0 to a final state qf, that is,
*(q0, w)F
• A string w is not accepted by M if there is not any walk w
from q0 to any final state qf
• The language L(M) accepted by nfa M is
L(M)={ w* : *(q0, w)F }
• Note: if *(q0, w) is undefined (dead), then w is not
accepted.
302017 Fall
Example,
• For the following nfa, L(M)={ (10)n : n0}
• *(q0, 10111) cannot reach a final state no matter what paths it takes. Thus, 10111 is not accepted by M
312017 Fall
Design nfa’s
• Design an nfa for L={w{a,b}* : w contains substring abb }
322017 Fall
• Design an nfa for L={w{a,b}* : w does not contain substring abb }
– Cannot exchange final and non-final states
332017 Fall
• Design an nfa for L={anb : n0}{bna : n1}
342017 Fall
Equivalence of nfa and dfa
• Two finite acceptors M1 and M2 are equivalentif and only if L(M1)=L(M2)
352017 Fall
• Cdfa : the class of dfa’s
• Cnfa : the class of nfa’s
• Is the class Cnfa more powerful than Cdfa?
– Is there a language accepted by an nfa, but not accepted by any dfa?
362017 Fall
Convert nfa to dfa:
• MN=(QN, , N, q0, FN)
• MD=(QD, , D, {q0}, FD)
• Key point: dfa simulates transitions of nfa
– MN: q0 … {qi, qj, …, qk} (state set) by N*
– MD: [q0]… [qi, qj, …, qk] (a state) by D*
• A set of states in MN is a state in MD
37
q0
qi
qj
qk
w
w
w
{q0}{qiqj …qk}
w
2017 Fall
• Recursion property– MN: {qi, qj, …, qk} {pr, ps, …, pt} by N
* in one step (on input symbol)
– MD: [qi, qj, …, qk] [pr, ps, …, pt] by D in one step
38
qi
qi
qi
pr
ps
pt
a
a
a
a
{prps …pt}{qiqj …qk}a
2017 Fall
39
Algorithm: nfadfa
1. Compute N*(q, a) for all qQN, a,
2. Start with {q0} in QD
3. Expand to {qi, qj,…, qk} from existent states– For any unspecified {qi, qj,…, qk} and a, compute
D({qi, qj,…, qk}, a)
= N*(qi, a)N*(qj, a) … N*(qk, a)
– Repeat until all states are expanded.
4. For FD , – a state {qi, qj,…, qk} is in FD if any of qi, qj, …, qk is in FN.
– {q0} is in FD if is accepted by MN
2017 Fall
40
Preparation
• *(q0, 0)• *(q0, 1)• *(q1, 0)• *(q1, 1)• *(q2, 0)• *(q2, 1)
2017 Fall
Example:
41
Expand from {q0}
2017 Fall
422017 Fall
43
Why correct?
• For any w+, D({q0}, w) = {qi, qj,…, qk} if and only if N*(q0, w) = {qi, qj, …, qk}
• Base: D({q0}, a) = N*(q0, a) for any a
• Hypothesis: true for |w|nD({q0}, w) = N*(q0, w) = {qi, qj,…, qk}
• Induction step: true for |wa|=n+1D*({q0}, wa) = D({qi, qj,…, qk}, a)= N*(qi, a) N*(qj, a) … N*(qk, a)= N*(q0, wa)
2017 Fall
Some thoughts
• Every dfa accepts a unique language.
• For a given language, there are many dfa’sthat accept it.
• Questions– How do we know two dfa’s are equivalent?
– How do find a minimum-state dfa for a given L, if existing?
– For any regular language, is the minimum-state dfa’s unique?
442017 Fall
45
Reduction of States
• Ideas: – Remove inaccessible states
– Find equivalent states – Mark()
– Merge equivalent states – Reduce()
2017 Fall
Indistinguishable states
• Two states p and q are indistinguishable if for every w*,
– *(p, w)F *(q, w)F
– *(p, w)F *(q, w)F
• Merge indistinguishable states as a new state.
• How to find indistinguishable states?
462017 Fall
47
p
q
w
w
q0
x
y
• States p and q are distinguishable.• They cannot be merged.
2017 Fall
Two states are distinguishable if there is w*,*(p, w) F and *(q, w) F, or*(p, w) F and *(q, w) F
• Observation (backward check)
– Start:Final and non-final states are distinguishable
– Recursion:if (p, a)=r, (q, a)=s, and (r, s) are distinguishable, then (p, q) are distinguishable.
482017 Fall
Find distinguishable states:
Procedure Mark():
1. Remove all inaccessible states.
2. For all pairs of states (p, q), if pF and qF, or vice versa, then mark (p, q) as distinguishable.
3. Repeat the following until no un-marked pairs are marked
– For all pairs (p, q) and a, compute (p, a)=pa and (q, a)=qa. If (pa, qa) is distinguishable, then mark (p, q) as distinguishable.
492017 Fall
50
From equivalence classes to the dfa:
Procedure: Reduce()
1. Use Mark() to find equivalence classes.
2. For each class {qi, qj,…, qk}, create a state [ij…k].
3. ’([ij…k], a) = [lm…n] if (qr, a)=qp, qr{qi, qj,…,qk}, qp{ql, qm,…, qn}
4. For each state [ij…k], if some r[ij…k] and qrF, then[ij…k]F’.
5. Output the reduced dfa, described above.
2017 Fall
51
q0
q1
q2
q3
q4
q0 q1 q2 q3 q4
2017 Fall
Example
522017 Fall
Exercise
53
p0 p1
p3
p4
p2
p5
a
ab
b
a
a
ba
a
b
p6 b
ba
2017 Fall
Remarks
• For every regular language L, the minimum-state dfais unique.
• The minimun-state dfa for L can be found from any dfa for L by the procedure mark() and reduce().
542017 Fall
Example
• Find a minimum-state dfa forL={anb : n0}{bna : n1}
552017 Fall