slides

Finite Automata
Wen-Guey Tzeng
Department of Computer Science
National Chiao Tung University
1
Deterministic Finite Acceptor
• A DFA M is defined by M=(Q, , , q0, F)
– Q: a finite set of states
– : input alphabet (set of symbols)
– : Qx Q: a total function called transition
function
– q0Q: the initial state
– FQ: set of final states
• A dfa is an acceptor for strings.
2
DFA 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
• Graph representation
3
From  to *
• *: Qx*Q
– *(q, ) = q
– *(q, wa) = (*(q, w), a)=q
4
Language L(M) accepted by 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 is
L(M)= {w * : *(q0, w)  F }
• Example: L={anb : n0}
5
Simple DFA design
• Find a dfa that accepts strings starting with
prefix ab. ={a, b}
6
• Design a dfa for L={w{0,1}* : w contains substring
001}
7
• Design a dfa accepting all strings over {0, 1} except
those containing substring 001
8
• Design a dfa for L = {w{a,b}* : na(w) mod 3=0}
9
• Design a dfa for L = {w{a,b}* : na(w) is even
and nb(w) is odd}
10
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.
11
• L2 is regular. L2=LL={aw1aaw2a : w1, w2{a,b}*}
12
Nondeterministic Finite Acceptor
• An NFA M is define by M=(Q, , , q0, F)
– Q: set of finite states
– : input alphabet (set of symbols)
– : Qx({})2Q: nondeterministic transition
function
– q0Q: the initial state
– FQ: set of final states
• Ex. (q1, a)={q0, q2}
13
Example: NFA
14
Example: NFA with -transitions
• Unspecified transition (q2, 0)=.
• Accepted strings: , 10, 1010, 101010, …
• Not accepted strings: 110, 10100
15
Transition function *
• *(qi, w)=Q 
qj  Q if and only if there is a walk from qi to qj
• Example
– *(q1, a)={q0, q1, q2}
– *(q2, )={q0, q2}, *(q0, )={q0},
– *(q2, aa)={q0, q1, q2}
16
Language accepted by nfa
• A string w is accepted by nfa M if there is a walk
from q0 to a final state qf, that is, *(q0, w)F  
– A string is not accepted by M if there are no walks
from q0 to qf
• The language L(M) accepted by nfa M is
L(M)={ w* : *(q0, w)F  }
17
• Example, L(M)={ (10)n : n0}
• Dead configuration: *(q0, 10111)= 
18
Design of nfa’s
• Design an nfa for L={w{a,b}* : w contains
substring abb }
• Design an nfa for L={w{a,b}* : w does not
contain substring abb }
19
• Design an nfa for L={anb : n0}{bna : n1}
20
Why Nondeterminism?
• Nondeterministic machines can be as models of
search-and-backtrack algorithms.
– Nondeterministic steps are branches of searching.
– Deterministic machines can simulate nondeterministic
machines
• Sometimes, helpful in solving problems
– Consider L={a3}{a2n : n1}
• Good way to describe complicated languages
– S  aSb | 
• Technical reason: some theoretical results are
more easily established for nfa’s than dfa’s.
21
Equivalence of finite automata
• Two finite acceptors M1 and M2 are equivalent
if L(M1)=L(M2)
22
• 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 a nfa, but not
accepted by any dfa?
23
Convert nfa to dfa
• MN=(QN, , N, q0, FN)
• MD=(QD, , D, {q0}, FD)
• Key point: label a state of the dfa with a set of
states in the nfa such that, for any w *
– N*(q0, w) = {qi, qj, …, qk}
– D*({q0}, w) = {qi, qj, …, qk}
• {qi, qj, …, qk} is a final state in FD if QFN
• QD = 2QN
24
Example
•
•
•
•
•
•
D({q0}, a) = {q1, q2}
D({q0}, b) = 
D({q1, q2}, a) = {q1, q2}
D({q1, q2}, b) = {q0}
D(, a) = 
D(, b) = 
25
How to convert
• For any w* , N*(q0, w) = {qi, qj, …, qk} if
and only if D({q0}, w) = {qi, qj,…, qk}
• For any unspecified {qi, qj,…, qk} and a,
compute
D({qi, qj,…, qk}, a)
= N*(qi, a) N*(qj, a) N*(qk, a)
26
Procedures
•
•
•
•
•
•
*(q0, 0)
*(q0, 1)
*(q1, 0)
*(q1, 1)
*(q2, 0)
*(q2, 1)
27
28
29
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|n
D({q0}, w) = N*(q0, w) = {qi, qj,…, qk}
• Induction step: true for |wa|=n+1
D({q0}, wa)
= N*(qi, a) N*(qj, a) … N*(qk, a)
= N*(q0, wa)
30
Some thoughts
• Every dfa accepts a unique language.
• For a given language, there are many dfa’s
that 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?
31
Reduction of States
• Ideas:
– Remove inaccessible states
– Merge equivalent states
32
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
• Two states are distinguishable if there is w*,
– *(p, w)  F and *(q, w)  F, or
– *(p, w)  F and *(q, w)  F
• Observation: if (p, a)=r, (q, a)=s, and (r, s) are
distinguishable, then (p, q) are distinguishable.
33
Find distinguishable states
Procedure Mark():
1. Remove all inaccessible states.
2. For all pairs of states (p, q), if pF and qF, 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.
34
Example,
Find equivalence classes of indistinguishable states
by Mark().
35
From equivalence classes to the dfa
• Procedure: Reduce(M=(Q, , ’, q0, F))
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 qrF,
then[ij…k]F’.
5. Output the reduced dfa, described above.
36
37
Remarks
• For every regular language L, the minimumstate dfa is unique.
• The minimun-state dfa for L can be found from
any dfa for L by the procedure mark().
38
Sum up
• Find a minimum-state dfa for
L={anb : n0}{bna : n1}
39