Chapter 2: Finite Automata - Nanjing University · PDF file · 2015-07-13Chapter 2:...

34
Chapter 2: Finite Automata 2.1 States, State Diagrams, and Transitions Finite automaton is the simplest acceptor or recognizer for language specification. It is also the simplest model of a computer. A state is “the condition with respect to structure, form, constitution, and phase”. 1

Transcript of Chapter 2: Finite Automata - Nanjing University · PDF file · 2015-07-13Chapter 2:...

Page 1: Chapter 2: Finite Automata - Nanjing University · PDF file · 2015-07-13Chapter 2: Finite Automata 2.1 States, State Diagrams, ... to continue M’s computation. ... Similarly q

'

&

$

%

Chapter 2: Finite Automata

2.1 States, State Diagrams, and Transitions

Finite automaton is the simplest acceptor or recognizer for languagespecification. It is also the simplest model of a computer.

A state is “the condition with respect to structure, form,constitution, and phase”.

1

Page 2: Chapter 2: Finite Automata - Nanjing University · PDF file · 2015-07-13Chapter 2: Finite Automata 2.1 States, State Diagrams, ... to continue M’s computation. ... Similarly q

'

&

$

%

2.2 Deterministic Finite Automata

Definition A deterministic finite automaton (DFA) M is specifiedby a quintuple M = (Q, Σ, δ, s, F ) where

• Q is an alphabet of state symbols;

• Σ is an alphabet of input symbols;

• δ : Q× Σ → Q is a transition function;

• s in Q is the start state; and

• F ⊆ Q is a set of final states.

2

Page 3: Chapter 2: Finite Automata - Nanjing University · PDF file · 2015-07-13Chapter 2: Finite Automata 2.1 States, State Diagrams, ... to continue M’s computation. ... Similarly q

'

&

$

%

DFAs are intended to read input words and accept or reject them,that is they are recognizers. To see how DFAs do this we treat themfrom a machine-oriented viewpoint.

6@

@@I¡

¡¡µ

Cells

Input tape

6Read head

Finitecontrol

3

Page 4: Chapter 2: Finite Automata - Nanjing University · PDF file · 2015-07-13Chapter 2: Finite Automata 2.1 States, State Diagrams, ... to continue M’s computation. ... Similarly q

'

&

$

%

Let M = (Q, Σ, δ, s, F ) be a DFA, then we view it as a machine (aprimitive computer) which has an input tape (or file) of cells, areading head, and a finite control:

• The finite control knows the transition function and the currentstate.

• The transition function and current state are analogous to aprogram and the current statement being executed (instructioncounter).

• The input tape contains the input word, one symbol to each cellof the tape.

4

Page 5: Chapter 2: Finite Automata - Nanjing University · PDF file · 2015-07-13Chapter 2: Finite Automata 2.1 States, State Diagrams, ... to continue M’s computation. ... Similarly q

'

&

$

%

The DFA is initialized with an input word x as follows:

1. x is placed on the input tape one symbol to a cell.

2. The reading head is positioned over the leftmost cell.

3. The current state is set to s.

4. The DFA is started.

5

Page 6: Chapter 2: Finite Automata - Nanjing University · PDF file · 2015-07-13Chapter 2: Finite Automata 2.1 States, State Diagrams, ... to continue M’s computation. ... Similarly q

'

&

$

%

The DFA, once started, begins its computation on the input word.As with any computer it has a basic execute cycle:

1. The symbol under the reading head is read, that is, the currentsymbol. If there is no symbol under the reading head the DFAterminates. This occurs when the whole input word has beenread.

2. The next state is computed from the current state and symbolusing the transition function, that is δ(current state, currentsymbol)=next state. If the current state is undefined the DFAaborts.

3. The reading head moves one cell to the right.

4. The next state becomes the current state and the execute cyclehas been completed.

6

Page 7: Chapter 2: Finite Automata - Nanjing University · PDF file · 2015-07-13Chapter 2: Finite Automata 2.1 States, State Diagrams, ... to continue M’s computation. ... Similarly q

'

&

$

%

Definition Let M = (Q, Σ, δ, s, F ) be a DFA. We say that a word inQΣ∗ is a configuration of M . It represents the current state of M andthe remaining unread input of M .

A configuration of a DFA, M , contains all the information necessaryto continue M ’s computation. In programming parlance it isequivalent to a dump of the current value of all variables of aprogram and the current position in the program.

7

Page 8: Chapter 2: Finite Automata - Nanjing University · PDF file · 2015-07-13Chapter 2: Finite Automata 2.1 States, State Diagrams, ... to continue M’s computation. ... Similarly q

'

&

$

%

Definition Let M = (Q, Σ, δ, s, F ) be a DFA. If px and qy are twoconfigurations of M , then we write px ` qy, if x = ay for some a inΣ, and δ(p, a) = q. We say px yields qy. Observe that ` is a binaryrelation over QΣ∗

For k ≥ 1, we write px `k qy if

• k = 1 and px ` qy, or

• k > 1 and there exists a configuration rz such that px ` rz andrz `k−1 qy.

8

Page 9: Chapter 2: Finite Automata - Nanjing University · PDF file · 2015-07-13Chapter 2: Finite Automata 2.1 States, State Diagrams, ... to continue M’s computation. ... Similarly q

'

&

$

%

We write px `+ qy, where `+ is the trsnsitive closure of `, ifpx `k qy, for some k ≥ 1.

We write px `∗ qy, where `∗ is the reflexive trsnsitive closure of `, ifeither px = qy or px `+ qy.

We say that the sequence of configurations given by px `∗ qy is aconfiguration sequence.

9

Page 10: Chapter 2: Finite Automata - Nanjing University · PDF file · 2015-07-13Chapter 2: Finite Automata 2.1 States, State Diagrams, ... to continue M’s computation. ... Similarly q

'

&

$

%

We can formally define acceptance by a DFA and the language of aDFA as follows.

Definition Let M = (Q, Σ, δ, s, F ) be a DFA. We say that a word x

in Σ∗ is accepted by M if sx `∗ f , for some f in F . We say thatsx `∗ f is an accepting configuration sequence. A word that is notaccepted is said to be rejected. The set of words accepted by M ,called the language accepted, defined, or recognized by M is denotedby L(M) and is defined as

L(M) = {x : x is in Σ∗ and sx `∗ f , for some f in F} .

10

Page 11: Chapter 2: Finite Automata - Nanjing University · PDF file · 2015-07-13Chapter 2: Finite Automata 2.1 States, State Diagrams, ... to continue M’s computation. ... Similarly q

'

&

$

%

The notion of acceptance has caused DFA to stand for deterministicfinite acceptor in some circles.

We say that L ⊆ Σ∗ is a DFA language if there is a DFA, M , withL = L(M).

The collection of all DFA languages is denoted by LDFA and is calledthe family of DFA languages.

11

Page 12: Chapter 2: Finite Automata - Nanjing University · PDF file · 2015-07-13Chapter 2: Finite Automata 2.1 States, State Diagrams, ... to continue M’s computation. ... Similarly q

'

&

$

%

DFA MEMBERSHIPINSTANCE: A DFA, M = (Q, Σ, δ, s, F ), and a word x in Σ∗.QUESTION: Is x in L(M)?

Definition If the transition function of a DFA is total we say thatthe DFA is complete and otherwise we say it is incomplete.

Every incomplete DFA can be transformed into a complete one whilepreserving its language.

12

Page 13: Chapter 2: Finite Automata - Nanjing University · PDF file · 2015-07-13Chapter 2: Finite Automata 2.1 States, State Diagrams, ... to continue M’s computation. ... Similarly q

'

&

$

%

Example Let Li = {aibi}, i ≥ 1. Construct DFAs, Mi, accepting Li,for each i ≥ 1.

Let K =⋃∞

j=1 Lj

K = {aibi, : i ≥ 1}.Claim: K is not a DFA language.

There is a language that is not a DFA language.

The family of all DFA language over Σ is enumerable.

13

Page 14: Chapter 2: Finite Automata - Nanjing University · PDF file · 2015-07-13Chapter 2: Finite Automata 2.1 States, State Diagrams, ... to continue M’s computation. ... Similarly q

'

&

$

%

Definition Let M1 and M2 be two DFAs. If L(M1) = L(M2) we saythat M1 and M2 are equivalent.

Theorem 2.2.1 Let M = (Q, Σ, δ, s, F ) be an incomplete DFA, thatis, there is a pair (q, a) in Q× Σ such that δ(q, a) = undef . Then aDFA M ′ can be constructed such that M ′ is complete andL(M ′) = L(M).

Consider a computer implementation of DFAs.

14

Page 15: Chapter 2: Finite Automata - Nanjing University · PDF file · 2015-07-13Chapter 2: Finite Automata 2.1 States, State Diagrams, ... to continue M’s computation. ... Similarly q

'

&

$

%

2.3 Nondeterministic Finite Automata

Definition A nondeterministic finite automaton (NFA) M isspecified by a quintuple M = (Q, Σ, δ, s, F ) where

• Q is an alphabet of state symbols;

• Σ is an alphabet of input symbols;

• δ ⊆ Q× Σ×Q is a transition relation;

• s in Q is the start state; and

• F ⊆ Q is a set of final states.

15

Page 16: Chapter 2: Finite Automata - Nanjing University · PDF file · 2015-07-13Chapter 2: Finite Automata 2.1 States, State Diagrams, ... to continue M’s computation. ... Similarly q

'

&

$

%

For a triple (p, a, q) in δ, p corresponds to the current state, a to thecurrent input symbol, and q to a next state.

Clearly an NFA, (Q, Σ, δ, s, F ), is a DFA if for all p in Q and all a inΣ, there is at most one triple (p, a, q) in δ, for some q.

When we are unconcerned about a finite automaton beingdeterministic or nondeterministic we refer to it simply as an FA.

Remark: It is sometimes convenient to consider the transitionrelation δ as a function, which, in general, gives state sets, that isδ : Q× Σ → 2Q is defined by

• For all p in Q and for all a in Σ: δ(p, a) = {q : (p, a, q) is in δ}.

16

Page 17: Chapter 2: Finite Automata - Nanjing University · PDF file · 2015-07-13Chapter 2: Finite Automata 2.1 States, State Diagrams, ... to continue M’s computation. ... Similarly q

'

&

$

%

Definition Let M = (Q, Σ, δ, s, F ) be an NFA. A configuration of M

is a word in QΣ∗, as for DFAs. Given two configurations px and qy

of M , we write px ` qy in M if x = ay for some a in Σ and (p, a, q) isin δ. Similarly, we write px `k qy in M if either k = 1, when `1 isidentical to ` or x = az, for some a in Σ and some z in Σ+, and thereis an r such that

px ` rz and rz `k−1 qy .

We write px `+ qy if px `k qy for some k ≥ 1, and, finally, px `∗ qy ifeither px = qy or px `+ qy.

17

Page 18: Chapter 2: Finite Automata - Nanjing University · PDF file · 2015-07-13Chapter 2: Finite Automata 2.1 States, State Diagrams, ... to continue M’s computation. ... Similarly q

'

&

$

%

Definition Given M = (Q, Σ, δ, s, F ), an NFA, we say a word x inΣ∗ is accepted if there is a configuration sequence sx `∗ q, for some q

in F , and is rejected otherwise. The language of M , denoted by L(M)as before, is defined by

L(M) = {x : x is in Σ∗ and sx `∗ q, for some q in F} .

A language L ⊆ Σ∗ is said to be an NFA language if there exists anNFA, M , with L = L(M) and we say two NFAs, M1 and M2, areequivalent if L(M1) = L(M2) and inequivalent otherwise. Finally, thefamily of NFA languages is denoted by LNFA.

NFAs are simpler to define in many cases and, moreover, we canalways construct a DFA from a given NFA which accepts exactly thesame language.

18

Page 19: Chapter 2: Finite Automata - Nanjing University · PDF file · 2015-07-13Chapter 2: Finite Automata 2.1 States, State Diagrams, ... to continue M’s computation. ... Similarly q

'

&

$

%

Definition Let M = (Q, Σ, δ, s, F ) be an NFA. A super-configurationof M has the form Kx where K ⊆ Q and x is in Σ∗. It means that K

is the current set of states and x is the unread portion of the inputword. The starting super-configuration for a word x in Σ∗ is {s}x.

We extend ` to super-configurations as follows. We say Kx ` Ny, forK,N ⊆ Q and x, y in Σ∗, if x = ay, for some a in Σ, y in Σ∗, andN = {q : (p, a, q) is in δ, for some p in K}. We obtain `+ and `∗ asbefore and if Kx `∗ Ny we call it super-configuration sequence.

Note that K `∗ K for all K ⊆ Q and ∅x `∗ ∅, for all x in Σ∗.

19

Page 20: Chapter 2: Finite Automata - Nanjing University · PDF file · 2015-07-13Chapter 2: Finite Automata 2.1 States, State Diagrams, ... to continue M’s computation. ... Similarly q

'

&

$

%

Lemma 2.3.1 Let M = (Q, Σ, δ, s, F ) be an NFA. Then for all wordsx in Σ∗ and for all K ⊆ Q

Kx `∗ N and Kx `∗ P implies P = N .

Hence, M is deterministic with respect to super-configurationsequences.

The above lemma tells us that there is a unique super-configurationsequence, begining with K, for each input word that is readcompletely.

20

Page 21: Chapter 2: Finite Automata - Nanjing University · PDF file · 2015-07-13Chapter 2: Finite Automata 2.1 States, State Diagrams, ... to continue M’s computation. ... Similarly q

'

&

$

%

Lemma 2.3.2 Let M = (Q, Σ, δ, s, F ) be an NFA. Then for all wordsx in Σ∗, and for all q in Q, qx `∗ p if and only if {q}x `∗ P , for someP with p in P .

Definition Let M = (Q, Σ, δ, s, F ) be an NFA and x a word in Σ∗.We say x is accepted by M if {s}x `∗ P , for some P with P ∩ F 6= ∅,otherwise x is rejected.

21

Page 22: Chapter 2: Finite Automata - Nanjing University · PDF file · 2015-07-13Chapter 2: Finite Automata 2.1 States, State Diagrams, ... to continue M’s computation. ... Similarly q

'

&

$

%

Algorithm NFA to DFA - The Subset Construction.On entry: An NFA, M = (Q, Σ, δ, s, F ).On exit: A DFA, M ′ = (Q′, Σ, δ′, s′, F ′) satisfying L(M) = L(M ′).begin Let Q′ = 2Q, s′ = {s},

and F ′ = {K : K ⊆ Q and K ∩ F 6= ∅}.We define δ′ : Q′ × Σ → Q′ by

For all K ⊆ Q and for all a in Σ,δ′(K, a) = N if Ka ` N in M

end of Algorithm.

22

Page 23: Chapter 2: Finite Automata - Nanjing University · PDF file · 2015-07-13Chapter 2: Finite Automata 2.1 States, State Diagrams, ... to continue M’s computation. ... Similarly q

'

&

$

%

Theorem 2.3.3 Given an NFA, M = (Q, Σ, δ, s, F ), then the DFA,M ′ = (Q′,Σ, δ′, s′, F ′), obtained by either subset constructionsatisfies L(M) = L(M ′).

Theorem 2.3.4 LDFA = LNFA. Thus, NFAs have the someexpressive power as DFAs.

23

Page 24: Chapter 2: Finite Automata - Nanjing University · PDF file · 2015-07-13Chapter 2: Finite Automata 2.1 States, State Diagrams, ... to continue M’s computation. ... Similarly q

'

&

$

%

2.4 Minimization and Simplification

Definition Let M = (Q, Σ, δ, s, F ) be an NFA. A state q in Q isreachable or accessible if there exists a word x in Σ∗ such thatsx `∗ q. If no such word exists q is said to be unreachable orinaccessible. A state q in Q is useful if there exists a word x in Σ∗

such that qx `∗ r, for some state r in F . Similarly q is useless if nosuch word exists. A symbol a in Σ is (symbol) reachable if thereexists a reachable state q in Q for which δ(q, a) 6= ∅.

24

Page 25: Chapter 2: Finite Automata - Nanjing University · PDF file · 2015-07-13Chapter 2: Finite Automata 2.1 States, State Diagrams, ... to continue M’s computation. ... Similarly q

'

&

$

%

Definition Let M = (Q, Σ, δ, s, F ) be a DFA. For two distinct statesp and q in Q we say p and q are distinguishable if there exists an x inΣ∗ such that px `∗ f , qx `∗ g and exactly one of f and g is in F .The word x distinguishes p from q. If no such word exists then p andq are indistingushable and we write p ≡ q, since ther are equivalent intheir action as far as acceptance is concerned. Indeed, they can bemerged without affecting the language accepted by M . This formsthe basis of the minimization for which we need to refine the notionof indistingushable. Given an integer k ≥ 0, we say two distinctstates p and q in Q are k-distindushable if there is a word x in Σ∗,|x| ≤ k, which distindushes p from q. If there is no such word, then

we say that p and q are k-indistindushable and we write pk≡ q. These

notations are appropriate since both ≡ andk≡ are equivalence

relations over Q.

25

Page 26: Chapter 2: Finite Automata - Nanjing University · PDF file · 2015-07-13Chapter 2: Finite Automata 2.1 States, State Diagrams, ... to continue M’s computation. ... Similarly q

'

&

$

%

If we cannot distingush p and q with words of length at most k + 1,for some k ≥ 0, then we cannot distingush them with the words of at

most k. In other words, pk+1≡ q implies p

k≡ q, for all k ≥ 0, that isk+1≡ ⊆ k≡.

We sayk+1≡ is a refinement of

k≡. Similarly, we obtain ≡⊆ k≡, for allk ≥ 0.

≡⊆ . . . ⊆ k≡⊆ . . . ⊆ 1≡⊆ 0≡

There are at most two equivalence classes formed by0≡, F and Q−F .

We can construct ≡ by refining0≡ to give

1≡,1≡ to give

2≡, and so on.

To refinei≡ to give

i+1≡ we use the observation

• For two distinct states p and q with pi≡ q, p

i+1≡ q if and only if,

for all a in Σ, δ(p, a)i≡ δ(q, a)

26

Page 27: Chapter 2: Finite Automata - Nanjing University · PDF file · 2015-07-13Chapter 2: Finite Automata 2.1 States, State Diagrams, ... to continue M’s computation. ... Similarly q

'

&

$

%

Lemma 2.4.1 Let M = (Q, Σ, δ, s, F ) be a DFA with #Q = m.Then two distinct states p and q are indistingushable if and only ifthey are (m− 2)-indistingushable.

Let M = (Q, Σ, δ, s, F ), ≡ bek≡ and construct a DFA,

M ′ = (Q′,Σ, δ′, s′, F ′) fromk≡ as follows:

• Let

Q′ =

[p]

∣∣∣∣∣∣p is in Q and [p] is the

equivalence class of p in ≡

,

s′ = [s], F ′ = {[p] : p is in F}, and define δ′ by

– For all [p] in Q′, for all a in Σ, δ′([p], a) = [δ(p, a)]

27

Page 28: Chapter 2: Finite Automata - Nanjing University · PDF file · 2015-07-13Chapter 2: Finite Automata 2.1 States, State Diagrams, ... to continue M’s computation. ... Similarly q

'

&

$

%

Theorem 2.4.2 Let M = (Q, Σ, δ, s, F ) be a complete DFA havingonly reachable states and M ′ = (Q′,Σ, δ′, s′, F ′) be the correspondingDFA constructed from ≡ for M . Then M ′ is a minimal completeDFA equivalent to M .

Corollary 2.4.3 Let M = (Q, Σ, δ, s, F ) be a complete DFA and M ′

and M ′′ be two minimal complete DFAs equivalent to M . Then M ′

and M ′′ are equal apart from a renaming states, that is, there is anisomorphism between M ′ and M .

This corollary implies that the following question is decidable:

DFA EQUIVALENCEINSTANCE: Two DFAs M1 and M2.QUESTION: Is L(M1) = L(M2)?

28

Page 29: Chapter 2: Finite Automata - Nanjing University · PDF file · 2015-07-13Chapter 2: Finite Automata 2.1 States, State Diagrams, ... to continue M’s computation. ... Similarly q

'

&

$

%

2.5 DFAs and Tries

Although the definition of DFA cannot be further restricted withoutrestricting the languages accepted by them, there are two restrictionsthat are of interest.

We say a DFA, M = (Q, Σ, δ, s, F ), has a loop if there is a state q inQ and a nonempty word x in Σ+ such that qx `∗ q; q is said to be alooping state.

M is said to be loop-free if it has no looping states, in other words,the state diagram has no cycles.

29

Page 30: Chapter 2: Finite Automata - Nanjing University · PDF file · 2015-07-13Chapter 2: Finite Automata 2.1 States, State Diagrams, ... to continue M’s computation. ... Similarly q

'

&

$

%

Furthermore, we say M is a tree if it is loop-free and for all states q

in Q, q 6= s, there is one and only one state p with δ(p, a) = q, forsome a in Σ.

Loop-free DFAs accept only finite languages, and this raises thequestion: Is every finite language accepted by some loop-free DFA?

Even if a DFA has loops it may still accept a finite language;therefore, we have the decision problem

DFA FINITENESSINSTANCE: A DFA, M = (Q, Σ, δ, s, F ).QUESTION: Is L(M) finite?

30

Page 31: Chapter 2: Finite Automata - Nanjing University · PDF file · 2015-07-13Chapter 2: Finite Automata 2.1 States, State Diagrams, ... to continue M’s computation. ... Similarly q

'

&

$

%

2.6 λ-FAs and Lazy FAs

2.6.1 λ-FAs and λ-Transitions

In the FAs we have discussed so far we require that the reading headmove at each step. We now relax the condition by allowing thereading head to remain at a cell during a transition. We call such atransition a λ-transition; the input symbol is ignored.

31

Page 32: Chapter 2: Finite Automata - Nanjing University · PDF file · 2015-07-13Chapter 2: Finite Automata 2.1 States, State Diagrams, ... to continue M’s computation. ... Similarly q

'

&

$

%

Definition M = (Q, Σ, δ, s, F ) is a λ-NFA, or λ-transition NFA, ifδ ⊆ Q× (Σ ∪ {λ})×Q. We require, on a λ-transition, that thereading head doesn’t move and the input symbol is ignored.

Given a configuration px in QΣ∗ we write

px ` qy

if x = Ay, for some A in Σ ∪ {λ}, and (p,A, q) is in δ; ` is extendedto `+ and `∗ as before.

Thus, at each configuration there is not only the choice of transitionusual in NFAs, but also the additional possibility of takingλ-transitions.

32

Page 33: Chapter 2: Finite Automata - Nanjing University · PDF file · 2015-07-13Chapter 2: Finite Automata 2.1 States, State Diagrams, ... to continue M’s computation. ... Similarly q

'

&

$

%

There are two situation in which λ-transitions are useful:

• The Plus of an NFA

• The Union of Two NFAs

Clearly, every NFA is a λ-NFA, but surprisingly the expressive powerof λ-NFAs is no more than that of NFAs.

Theorem 2.6.1 Let M = (Q, Σ, δ, s, F ) be a λ-NFA; then anequivalent NFA, M ′, can be constructed from M .

33

Page 34: Chapter 2: Finite Automata - Nanjing University · PDF file · 2015-07-13Chapter 2: Finite Automata 2.1 States, State Diagrams, ... to continue M’s computation. ... Similarly q

'

&

$

%

2.6.2 Lazy Finite Automata

Definition A lazy finite automaton, M , is a quintuple (Q, Σ, δ, s, F )where Q,, Σ, s, and F are as for FAs, but δ ⊆ Q× Σ∗ ×Q, a finitetransition relation. In other words, transitions are allowed withwords rather than just symbols and the empty word. A single moveis defined by

px ` qy

if x = wy, for some w in Σ∗, and (p, w, q) is in δ

Theorem 2.6.2 Let M be a lazy FA. Then there is a λ-NFA, M ′,with L(M ′) = L(M).

34