DCP 3122 Introduction to Formal Languageswgtzeng/courses... · DCP 3122 Introduction to Formal...

55
Finite Automata Wen-Guey Tzeng Computer Science Department National Chiao Tung University

Transcript of DCP 3122 Introduction to Formal Languageswgtzeng/courses... · DCP 3122 Introduction to Formal...

Page 1: DCP 3122 Introduction to Formal Languageswgtzeng/courses... · DCP 3122 Introduction to Formal Languages Author: wgtzeng Created Date: 2/13/2017 11:12:11 AM ...

Finite Automata

Wen-Guey Tzeng

Computer Science Department

National Chiao Tung University

Page 2: DCP 3122 Introduction to Formal Languageswgtzeng/courses... · DCP 3122 Introduction to Formal Languages Author: wgtzeng Created Date: 2/13/2017 11:12:11 AM ...

Syllabus

• Deterministic finite acceptor

• Nondeterministic finite acceptor

• Equivalence of DFA and NFA

• Reduction of the number of states

22017 Fall

Page 3: DCP 3122 Introduction to Formal Languageswgtzeng/courses... · DCP 3122 Introduction to Formal Languages Author: wgtzeng Created Date: 2/13/2017 11:12:11 AM ...

Deterministic Finite Acceptor

32017 Fall

Page 4: DCP 3122 Introduction to Formal Languageswgtzeng/courses... · DCP 3122 Introduction to Formal Languages Author: wgtzeng Created Date: 2/13/2017 11:12:11 AM ...

• 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

Page 5: DCP 3122 Introduction to Formal Languageswgtzeng/courses... · DCP 3122 Introduction to Formal Languages Author: wgtzeng Created Date: 2/13/2017 11:12:11 AM ...

• 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

Page 6: DCP 3122 Introduction to Formal Languageswgtzeng/courses... · DCP 3122 Introduction to Formal Languages Author: wgtzeng Created Date: 2/13/2017 11:12:11 AM ...

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

Page 7: DCP 3122 Introduction to Formal Languageswgtzeng/courses... · DCP 3122 Introduction to Formal Languages Author: wgtzeng Created Date: 2/13/2017 11:12:11 AM ...

• Graph representation

72017 Fall

Page 8: DCP 3122 Introduction to Formal Languageswgtzeng/courses... · DCP 3122 Introduction to Formal Languages Author: wgtzeng Created Date: 2/13/2017 11:12:11 AM ...

• How does it work?

• What does it represent?

– accept strings

– reject strings

82017 Fall

Page 9: DCP 3122 Introduction to Formal Languageswgtzeng/courses... · DCP 3122 Introduction to Formal Languages Author: wgtzeng Created Date: 2/13/2017 11:12:11 AM ...

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

Page 10: DCP 3122 Introduction to Formal Languageswgtzeng/courses... · DCP 3122 Introduction to Formal Languages Author: wgtzeng Created Date: 2/13/2017 11:12:11 AM ...

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

Page 11: DCP 3122 Introduction to Formal Languageswgtzeng/courses... · DCP 3122 Introduction to Formal Languages Author: wgtzeng Created Date: 2/13/2017 11:12:11 AM ...

• What language is accepted by M

112017 Fall

Page 12: DCP 3122 Introduction to Formal Languageswgtzeng/courses... · DCP 3122 Introduction to Formal Languages Author: wgtzeng Created Date: 2/13/2017 11:12:11 AM ...

• What language is accepted by M

• L(M) = {anb : n0}

122017 Fall

Page 13: DCP 3122 Introduction to Formal Languageswgtzeng/courses... · DCP 3122 Introduction to Formal Languages Author: wgtzeng Created Date: 2/13/2017 11:12:11 AM ...

DFA design

• Find a dfa that accepts strings starting with prefix ab,where ={a, b}

132017 Fall

Page 14: DCP 3122 Introduction to Formal Languageswgtzeng/courses... · DCP 3122 Introduction to Formal Languages Author: wgtzeng Created Date: 2/13/2017 11:12:11 AM ...

• 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

Page 15: DCP 3122 Introduction to Formal Languageswgtzeng/courses... · DCP 3122 Introduction to Formal Languages Author: wgtzeng Created Date: 2/13/2017 11:12:11 AM ...

• Design a dfa for L={w{0,1}* : w contains substring 001}

• Consider 100001

152017 Fall

Page 16: DCP 3122 Introduction to Formal Languageswgtzeng/courses... · DCP 3122 Introduction to Formal Languages Author: wgtzeng Created Date: 2/13/2017 11:12:11 AM ...

• Design a dfa accepting all strings over {0, 1} except those containing substring 001

162017 Fall

Page 17: DCP 3122 Introduction to Formal Languageswgtzeng/courses... · DCP 3122 Introduction to Formal Languages Author: wgtzeng Created Date: 2/13/2017 11:12:11 AM ...

• Design a dfa for

L = {w{a,b}* : na(w) is even and nb(w) is odd}

172017 Fall

Page 18: DCP 3122 Introduction to Formal Languageswgtzeng/courses... · DCP 3122 Introduction to Formal Languages Author: wgtzeng Created Date: 2/13/2017 11:12:11 AM ...

• Can we design a dfa for L={anbn : n1} ?

182017 Fall

Page 19: DCP 3122 Introduction to Formal Languageswgtzeng/courses... · DCP 3122 Introduction to Formal Languages Author: wgtzeng Created Date: 2/13/2017 11:12:11 AM ...

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

Page 20: DCP 3122 Introduction to Formal Languageswgtzeng/courses... · DCP 3122 Introduction to Formal Languages Author: wgtzeng Created Date: 2/13/2017 11:12:11 AM ...

• Every finite language L is regular.

202017 Fall

Page 21: DCP 3122 Introduction to Formal Languageswgtzeng/courses... · DCP 3122 Introduction to Formal Languages Author: wgtzeng Created Date: 2/13/2017 11:12:11 AM ...

• 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

Page 22: DCP 3122 Introduction to Formal Languageswgtzeng/courses... · DCP 3122 Introduction to Formal Languages Author: wgtzeng Created Date: 2/13/2017 11:12:11 AM ...

Nondeterministic Finite Acceptor

22

2017 Fall

Page 23: DCP 3122 Introduction to Formal Languageswgtzeng/courses... · DCP 3122 Introduction to Formal Languages Author: wgtzeng Created Date: 2/13/2017 11:12:11 AM ...

• 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

Page 24: DCP 3122 Introduction to Formal Languageswgtzeng/courses... · DCP 3122 Introduction to Formal Languages Author: wgtzeng Created Date: 2/13/2017 11:12:11 AM ...

• 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

Page 25: DCP 3122 Introduction to Formal Languageswgtzeng/courses... · DCP 3122 Introduction to Formal Languages Author: wgtzeng Created Date: 2/13/2017 11:12:11 AM ...

252017 Fall

0 1

q0 {q2} - {q1}

q1 - {q0, q2} {q2}

q2 - - -

F={q0}

Example: NFA with -transitions

Page 26: DCP 3122 Introduction to Formal Languageswgtzeng/courses... · DCP 3122 Introduction to Formal Languages Author: wgtzeng Created Date: 2/13/2017 11:12:11 AM ...

26

• -transition (q0, )={q2}

• Unspecified (undefined) transition (q0, 0), …

– Going into a dead state

2017 Fall

Page 27: DCP 3122 Introduction to Formal Languageswgtzeng/courses... · DCP 3122 Introduction to Formal Languages Author: wgtzeng Created Date: 2/13/2017 11:12:11 AM ...

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

Page 28: DCP 3122 Introduction to Formal Languageswgtzeng/courses... · DCP 3122 Introduction to Formal Languages Author: wgtzeng Created Date: 2/13/2017 11:12:11 AM ...

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

Page 29: DCP 3122 Introduction to Formal Languageswgtzeng/courses... · DCP 3122 Introduction to Formal Languages Author: wgtzeng Created Date: 2/13/2017 11:12:11 AM ...

• *(q0, ) = {q0}, *(q1, ) = {q1, q2}, *(q2, ) = {q2},

• Compute *(q0, a) =

• Compute *(q1, b) =

• Compute *(q0, ab) = ڂ𝑝∈𝛿∗(𝑞,𝑎) 𝛿∗(𝑝, 𝑏)

292017 Fall

Page 30: DCP 3122 Introduction to Formal Languageswgtzeng/courses... · DCP 3122 Introduction to Formal Languages Author: wgtzeng Created Date: 2/13/2017 11:12:11 AM ...

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

Page 31: DCP 3122 Introduction to Formal Languageswgtzeng/courses... · DCP 3122 Introduction to Formal Languages Author: wgtzeng Created Date: 2/13/2017 11:12:11 AM ...

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

Page 32: DCP 3122 Introduction to Formal Languageswgtzeng/courses... · DCP 3122 Introduction to Formal Languages Author: wgtzeng Created Date: 2/13/2017 11:12:11 AM ...

Design nfa’s

• Design an nfa for L={w{a,b}* : w contains substring abb }

322017 Fall

Page 33: DCP 3122 Introduction to Formal Languageswgtzeng/courses... · DCP 3122 Introduction to Formal Languages Author: wgtzeng Created Date: 2/13/2017 11:12:11 AM ...

• Design an nfa for L={w{a,b}* : w does not contain substring abb }

– Cannot exchange final and non-final states

332017 Fall

Page 34: DCP 3122 Introduction to Formal Languageswgtzeng/courses... · DCP 3122 Introduction to Formal Languages Author: wgtzeng Created Date: 2/13/2017 11:12:11 AM ...

• Design an nfa for L={anb : n0}{bna : n1}

342017 Fall

Page 35: DCP 3122 Introduction to Formal Languageswgtzeng/courses... · DCP 3122 Introduction to Formal Languages Author: wgtzeng Created Date: 2/13/2017 11:12:11 AM ...

Equivalence of nfa and dfa

• Two finite acceptors M1 and M2 are equivalentif and only if L(M1)=L(M2)

352017 Fall

Page 36: DCP 3122 Introduction to Formal Languageswgtzeng/courses... · DCP 3122 Introduction to Formal Languages Author: wgtzeng Created Date: 2/13/2017 11:12:11 AM ...

• 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

Page 37: DCP 3122 Introduction to Formal Languageswgtzeng/courses... · DCP 3122 Introduction to Formal Languages Author: wgtzeng Created Date: 2/13/2017 11:12:11 AM ...

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

Page 38: DCP 3122 Introduction to Formal Languageswgtzeng/courses... · DCP 3122 Introduction to Formal Languages Author: wgtzeng Created Date: 2/13/2017 11:12:11 AM ...

• 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

Page 39: DCP 3122 Introduction to Formal Languageswgtzeng/courses... · DCP 3122 Introduction to Formal Languages Author: wgtzeng Created Date: 2/13/2017 11:12:11 AM ...

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

Page 40: DCP 3122 Introduction to Formal Languageswgtzeng/courses... · DCP 3122 Introduction to Formal Languages Author: wgtzeng Created Date: 2/13/2017 11:12:11 AM ...

40

Preparation

• *(q0, 0)• *(q0, 1)• *(q1, 0)• *(q1, 1)• *(q2, 0)• *(q2, 1)

2017 Fall

Example:

Page 41: DCP 3122 Introduction to Formal Languageswgtzeng/courses... · DCP 3122 Introduction to Formal Languages Author: wgtzeng Created Date: 2/13/2017 11:12:11 AM ...

41

Expand from {q0}

2017 Fall

Page 42: DCP 3122 Introduction to Formal Languageswgtzeng/courses... · DCP 3122 Introduction to Formal Languages Author: wgtzeng Created Date: 2/13/2017 11:12:11 AM ...

422017 Fall

Page 43: DCP 3122 Introduction to Formal Languageswgtzeng/courses... · DCP 3122 Introduction to Formal Languages Author: wgtzeng Created Date: 2/13/2017 11:12:11 AM ...

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

Page 44: DCP 3122 Introduction to Formal Languageswgtzeng/courses... · DCP 3122 Introduction to Formal Languages Author: wgtzeng Created Date: 2/13/2017 11:12:11 AM ...

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

Page 45: DCP 3122 Introduction to Formal Languageswgtzeng/courses... · DCP 3122 Introduction to Formal Languages Author: wgtzeng Created Date: 2/13/2017 11:12:11 AM ...

45

Reduction of States

• Ideas: – Remove inaccessible states

– Find equivalent states – Mark()

– Merge equivalent states – Reduce()

2017 Fall

Page 46: DCP 3122 Introduction to Formal Languageswgtzeng/courses... · DCP 3122 Introduction to Formal Languages Author: wgtzeng Created Date: 2/13/2017 11:12:11 AM ...

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

Page 47: DCP 3122 Introduction to Formal Languageswgtzeng/courses... · DCP 3122 Introduction to Formal Languages Author: wgtzeng Created Date: 2/13/2017 11:12:11 AM ...

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

Page 48: DCP 3122 Introduction to Formal Languageswgtzeng/courses... · DCP 3122 Introduction to Formal Languages Author: wgtzeng Created Date: 2/13/2017 11:12:11 AM ...

• 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

Page 49: DCP 3122 Introduction to Formal Languageswgtzeng/courses... · DCP 3122 Introduction to Formal Languages Author: wgtzeng Created Date: 2/13/2017 11:12:11 AM ...

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

Page 50: DCP 3122 Introduction to Formal Languageswgtzeng/courses... · DCP 3122 Introduction to Formal Languages Author: wgtzeng Created Date: 2/13/2017 11:12:11 AM ...

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

Page 51: DCP 3122 Introduction to Formal Languageswgtzeng/courses... · DCP 3122 Introduction to Formal Languages Author: wgtzeng Created Date: 2/13/2017 11:12:11 AM ...

51

q0

q1

q2

q3

q4

q0 q1 q2 q3 q4

2017 Fall

Example

Page 52: DCP 3122 Introduction to Formal Languageswgtzeng/courses... · DCP 3122 Introduction to Formal Languages Author: wgtzeng Created Date: 2/13/2017 11:12:11 AM ...

522017 Fall

Page 53: DCP 3122 Introduction to Formal Languageswgtzeng/courses... · DCP 3122 Introduction to Formal Languages Author: wgtzeng Created Date: 2/13/2017 11:12:11 AM ...

Exercise

53

p0 p1

p3

p4

p2

p5

a

ab

b

a

a

ba

a

b

p6 b

ba

2017 Fall

Page 54: DCP 3122 Introduction to Formal Languageswgtzeng/courses... · DCP 3122 Introduction to Formal Languages Author: wgtzeng Created Date: 2/13/2017 11:12:11 AM ...

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

Page 55: DCP 3122 Introduction to Formal Languageswgtzeng/courses... · DCP 3122 Introduction to Formal Languages Author: wgtzeng Created Date: 2/13/2017 11:12:11 AM ...

Example

• Find a minimum-state dfa forL={anb : n0}{bna : n1}

552017 Fall