Sunteți pe pagina 1din 19

CSci 311, Models of Computation

Chapter 2
Finite Automata

H. Conrad Cunningham

29 December 2015

Contents
Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2
2.1 Deterministic Finite Accepters . . . . . . . . . . . . . . . . . . . 2
2.1.1 Accepters . . . . . . . . . . . . . . . . . . . . . . . . . . . 2
2.1.2 Transition Graphs . . . . . . . . . . . . . . . . . . . . . . 3
2.1.3 Linz Example 2.1 . . . . . . . . . . . . . . . . . . . . . . . 3
2.1.4 Extended Transition Function for a DFA . . . . . . . . . 4
2.1.5 Language Accepted by a DFA . . . . . . . . . . . . . . . . 4
2.1.6 Linz Example 2.2 . . . . . . . . . . . . . . . . . . . . . . . 4
2.1.7 Linz Example 2.3 . . . . . . . . . . . . . . . . . . . . . . . 5
2.1.8 Linz Example 2.4 . . . . . . . . . . . . . . . . . . . . . . . 5
2.1.9 Regular Languages . . . . . . . . . . . . . . . . . . . . . . 7
2.1.10 Linz Example 2.5 . . . . . . . . . . . . . . . . . . . . . . . 7
2.1.11 Linz Example 2.6 . . . . . . . . . . . . . . . . . . . . . . . 8
2.2 Nondeterministic Finite Accepters . . . . . . . . . . . . . . . . . 8
2.2.1 Nondeterministic Accepters . . . . . . . . . . . . . . . . . 8
2.2.2 Linz Example 2.7 . . . . . . . . . . . . . . . . . . . . . . . 9
2.2.3 Linz Example 2.8 . . . . . . . . . . . . . . . . . . . . . . . 9
2.2.4 Extended Transition Function for an NFA . . . . . . . . . 11

1
2.2.5 Language Accepted by an NFA . . . . . . . . . . . . . . . 11
2.2.6 Linz Example 2.10 (Example 2.8 Revisited) . . . . . . . . 11
2.2.7 Why Nondeterminism . . . . . . . . . . . . . . . . . . . . 12
2.3 Equivalence of DFAs and NFAs . . . . . . . . . . . . . . . . . . . 12
2.3.1 Meaning of Equivalence . . . . . . . . . . . . . . . . . . . 12
2.3.2 Linz Example 2.11 . . . . . . . . . . . . . . . . . . . . . . 13
2.3.3 Power of NFA versus DFA . . . . . . . . . . . . . . . . . . 13
2.3.4 Linz Example 2.12 . . . . . . . . . . . . . . . . . . . . . . 15
2.3.5 Linz Example 2.13 . . . . . . . . . . . . . . . . . . . . . . 18
2.4 Reduction in the Number of States in Finite Automata . . . . . . 19
Copyright (C) 2015, H. Conrad Cunningham
Acknowledgements: These lecture notes are for use with Chapter 2 of the
textbook: Peter Linz. Introduction to Formal Languages and Automata, Fifth
Edition, Jones and Bartlett Learning, 2012. The terminology and notation used
in these notes are similar to those used in the Linz textbook. This document
uses several figures from the Linz textbook.
Advisory: The HTML version of this document requires use of a browser that
supports the display of MathML. A good choice as of December 2015 seems to
be a recent version of Firefox from Mozilla.

Introduction

In chapter 2, we approach automata and languages more precisely and formally


than in chapter 1.
A finite automaton is an automaton that has no temporary storage (and, like
all automata we consider in this course, a finite number of states and input
alphabet).

2.1 Deterministic Finite Accepters

2.1.1 Accepters

Linz Definition (DFA): A deterministic finite accepter, or dfa, is defined by


the tuple M = (Q, Σ, δ, q0 , F ) where

• Q is a finite set of internal states


• Σ is a finite set of symbols called the input alphabet

2
• δ : Q × Σ → Q is a total function called the transition function
• q0 ∈ Q is the initial state.
• F ⊆ Q is a set of final states.

A dfa operates as described in the following pseudocode: currentState


:= q0
position input at left end of string

while more input exists currentInput := next_input_symbol


advance input to right
currentState := δ(currentState,currentInput)

if currentState ∈ F then ACCEPT else REJECT

2.1.2 Transition Graphs

To visualize a dfa, we use a transition graph constructed as follows:

• Vertices represent states


– labels are state names
– exactly one vertex for every qi ∈ Q
• Directed edges represent transitions
– label on edge is current input symbol
– directed edge (q, r) with label a if and only if δ(q, a) = r

2.1.3 Linz Example 2.1

The graph pictured in Linz Figure 2.1 represents the dfa M = ({q0 , q1 , q2 }, {0, 1}, δ, q0 , {q1 }),
where δ is represented by

δ(q0 , 0) = q0 , δ(q0 , 1) = q1
δ(q1 , 0) = q0 , δ(q1 , 1) = q2
δ(q2 , 0) = q2 , δ(q2 , 1) = q1

Note that q0 is the initial state and q1 is the only final state in this dfa.
The dfa in Linz Figure 2.1:

• Accepts 01, 101


• Rejects 00, 100

What about 0111? 1100?

3
Linz Fig. 2.1: DFA Transition Graph

2.1.4 Extended Transition Function for a DFA

Linz Definition: The extended transition function δ ∗ : Q × Σ∗ → Q is defined


recursively:

δ ∗ (q, λ) = q
δ ∗ (q, wa) = δ(δ ∗ (q, w), a)

The extended transition function gives the state of the automaton after reading
a string.

2.1.5 Language Accepted by a DFA

Linz Definition 2.2 (Language Accepted by DFA): The language accepted


by a dfa M = (Q, Σ, δ, q0 , F ) is L(M ) = {w ∈ Σ∗ : δ ∗ (q0 , w) ∈ F }.
That is, L(M ) is the set of all strings on the input alphabet accepted by
automaton M.
Note that above δ and δ ∗ are total functions (i.e., defined for all strings).

2.1.6 Linz Example 2.2

The automaton in Linz Figure 2.2 accepts all strings consisting of arbitrary
numbers of a’s followed by a single b.
In set notation, the language accepted by the automaton is L = {an b : n ≥ 0}.
Note that q2 has two self-loop edges, each with a different label. We write this
compactly with multiple labels.
A trap state is a state from which the automaton can never “escape”.
Note that q2 is a trap state in the dfa transition graph shown in Linz Figure 2.2.
Transition graphs are quite convenient for understanding finite automata.

4
Linz Fig. 2.2: DFA Transition Graph with Trap State

For other purposes–such as representing finite automata in programs–a tabular


representation of transition function δ may also be convenient (as shown in Linz
Fig. 2.3).

Linz Fig. 2.3: DFA Transition Table

2.1.7 Linz Example 2.3

Find a deterministic finite accepter that recognizes the set of all string on
Σ = {a, b} starting with the prefix ab.
Linz Figure 2.4 shows a transition graph for a dfa for this example.
The dfa must accept ab and then continue until the string ends.
This dfa has a final trap state q2 (accepts) and a non-final trap state q3 (rejects).

2.1.8 Linz Example 2.4

Find a dfa that accepts all strings on {0, 1}, except those containing the substring
001.

• need to “remember” whether last two inputs were 00


• use state changes for “memory”

5
Linz Fig. 2.4

Linz Fig. 2.5

6
Linz Figure 2.5 shows a dfa for this example.
Accepts: λ, 0, 00, 01, 010000
Rejects: 001, 000001, 0010101010101

2.1.9 Regular Languages

Linz Definition 2.3 (Regular Language): A language L is called regular if


and only if there exists a dfa M such that L = L(M ).
Thus dfas define the family of languages called regular.

2.1.10 Linz Example 2.5

Show that the language L = {awa : w ∈ {a, b}∗ } is regular.

• Construct a dfa.
• Check whether begin/end with “a”.
• Am in final state when second a input.

Linz Figure 2.6 shows a dfa for this example.

Linz Fig. 2.6

Question: How would we prove that a languages is not regular?


We will come back to this question in chapter 4.

7
2.1.11 Linz Example 2.6

Let L be the language in the previous example (Linz Example 2.5).


Show that L2 is regular.
L2 = {aw1 aaw2 a : w1 , w2 ∈ {a, b}∗ }.

• Construct a dfa.

• Use Example 2.5 dfa as starting point.


• Accept two consecutive strings of form awa.
• Note that any two consecutive a’s could start a second string.

Linz Figure 2.7 shows a dfa for this example.

Linz Fig. 2.7

The last example suggests the conjecture that if a language L then so is L2 , L3 ,


etc.
We will come back to this issue in chapter 4.

2.2 Nondeterministic Finite Accepters

2.2.1 Nondeterministic Accepters

Linz Definition 2.4 (NFA): A nondeterministic finite accepter or nfa is


defined by the tuple M = (Q, Σ, δ, q0 , F ) where Q, Σ, q0 , and F are defined as
for deterministic finite accepters, but

8
δ : Q × (Σ ∪ {λ}) → 2Q .

Remember for dfas:

• Q is a finite set of internal states.

• Σ is a finite set of symbols called the input alphabet.


• q0 ∈ Q is the initial state.
• F ⊆ Q is a set of final states.

The key differences between dfas and nfas are

1. dfa: δ yields a single state


nfa: δ yields a set of states
2. dfa: consumes input on each move
nfa: can move without input (λ)
3. dfa: moves for all inputs in all states
nfa: some situations have no defined moves

An nfa accepts a string if some possible sequence of moves ends in a final state.
An nfa rejects a string if no possible sequence of moves ends in a final state.

2.2.2 Linz Example 2.7

Consider the transition graph shown in Linz Figure 2.8.


Note the nondeterminism in state q0 with two possible transitions for input a.
Also state q3 has no transition for any input.

2.2.3 Linz Example 2.8

Consider the transition graph for an nfa shown in Linz Figure 2.9.
Note the nondeterminism and the λ-transition.
Note: Here λ means the move takes place without consuming any input symbol.
This is different from accepting an empty string.
Transitions:

• for (q0 , 0)?

9
Linz Fig. 2.8

Linz Fig. 2.9

10
• for (q1 , 0)?
• for (q2 , 0)?
• for (q2 , 1)?

Accepts: λ, 10, 1010, 101010


Rejects: 0, 11

2.2.4 Extended Transition Function for an NFA

As with dfas, the transition function can be extended so its second argument is
a string.
Requirement: δ ∗ (qi , w) = Qj where Qj is the set of all possible states the
automaton may be in, having started in state qi and read string w.
Linz Definition (Extended Transition Function): For an nfa, the extended
transition function is defined so that δ ∗ (qi , w) contains qj if there is a walk in
the transition graph from qi to qj labelled w.

2.2.5 Language Accepted by an NFA

Linz Definition 2.6 (Language Accepted by NFA): The language L ac-


cepted by the nfa M = (Q, Σ, δ, q0 , F ) is defined

L(M ) = {w ∈ Σ∗ : δ ∗ (q0 , w) ∈ F 6= ∅}.

That is, L(M ) is the set of all strings w for which there is a walk labeled w from
the initial vertex of the transition graph to some final vertex.

2.2.6 Linz Example 2.10 (Example 2.8 Revisited)

Let’s again examine the automaton given in Linz Figure 2.9 (Example 2.8).
This nfa, call it M :

• must end in q0

• L(M ) = {(10)n : n ≥ 0}

Note that q2 is a dead configuration because δ ∗ (q0 , 110) = ∅.

11
Linz Fig. 2.9 (Repeated)

2.2.7 Why Nondeterminism

When computers are deterministic?

• an nfa can model a search or backtracking algorithm

• nfa solutions may be simpler than dfa solutions (can convert from nfa to
dfa)
• nondeterminism may model externally influenced interactions (or abstract
more detailed computations)

2.3 Equivalence of DFAs and NFAs

2.3.1 Meaning of Equivalence

When are two mechanisms (e.g., programs) equivalent?

• When they have exactly the same descriptions?


• When they always go through the exact same sequence of steps?

• When the same input generates the same output for both?

The last seems to be the best approach.


Linz Definition 2.7 (DFA-NFA Equivalence): Two finite accepters M1 and
M2 are said to be equivalent if L(M1 ) = L(M2 ). That is, if they both accept
the same language.
Here “same language” refers to the input and “both accept” refers to the output.
Often there are many accepters for a language.
Thus there are many equivalent dfa and nfas.

12
2.3.2 Linz Example 2.11

Again consider the nfa represented by the graph in Linz Fig. 2.9. Call this M1 .

Linz Fig. 2.9 (Repeated): An NFA

As we saw, L(M1 ) = {(10)n : n ≥ 0}.


Now consider the dfa represented by the graph in Linz Figure 2.11. Call this
M2 .

Linz Fig. 2.11: DFA Equivalent to Fig. 9 NFA

L(M2 ) = {(10)n : n ≥ 0}.


Thus, M1 is equivalent to M2 .

2.3.3 Power of NFA versus DFA

Which is more powerful, dfa or nfa?


Clearly, any dfa D can be made into a nfa N .

• Keep the same states.

13
• Define δN (q, a) = {δD (q, a)}.

Can any nfa N be made into a dfa D?

• Yes, but it is less obvious. (See theorem below.)

Thus, dfas and nfas are “equally powerful”.


Linz Theorem 2.2 (Existence of DFA Equivalent to NFA): Let L be the
language accepted by the nfa MN = (QN , Σ, δN , q0 , FN ). Then there exists a
dfa MD = (QD , Σ, δD , {q0 }, FD ) such that L = L(MD ).
A pure mathematician would be content with an existence proof.
But, as computing scientists, we want an algorithm for construction of MD from
MN . The proof of the theorem follows from the correctness of the following
algorithm.
Key ideas:

• After reading w, MN will be in the some state from {qi , qj , . . . qk }. That


is, δ ∗ (q0 , w) = {qi , qj , . . . qk }.
• Label the dfa state that has accepted w with the set of nfa states
{qi , qj , . . . qk }. This is an interesting “trick”!

Remember from the earlier definitions in these notes and from discrete mathe-
matics:

• Σ is finite (and the same for the nfa and dfa).


• QN is finite.
• δD is a total function. That is, every vertex of the dfa graph has |Σ|
outgoing edges.
• The maximum number of dfa states with the above labeling is |2QN | = 2|QN | .
Hence, finite.
• The maximum number of dfa edges is 2|QD | |Σ|. Hence, finite.

Procedure nfa_to_dfa
Given a transition graph GN for nfa MN = (QN , Σ, δN , q0 , FN ), construct a
transition graph GD for a dfa MD = (QD , Σ, δD , q0 , FD ). Label each vertex in
GD with a subset of the vertices in GN .

1. Initialize graph GD to have an initial vertex {q0 } where q0 is the initial


state of GN .

14
2. Repeat the following steps until no more edges are missing from GD :

a. Take any vertex from GD labeled {qi , qj , . . . qk } that has no outgoing


edge for some a ∈ Σ.
b. For this vertex and input, compute δN ∗
(qi , a), δN

(qj , a), . . . δN

(qk , a).
(Each of these is a set of states from QN .)
c. Let {ql , qm , . . . qn } be the union of all δN

sets formed in the previous
step.
d. If vertex {ql , qm , . . . qn } constructed in the previous step (step 2c) is
not already in GD , then add it to GD .
e. Add an edge to GD from vertex {qi , qj , . . . qk } (vertex selected in
step 2b) to vertex {ql , qm , . . . qn } (vertex possibly created in step 2d)
and label the new edge with a (input selected in step 2b).

3. Make every vertex of GD whose label contains any vertex qf ∈ FN a final


vertex of GD .

4. If MN accepts λ, then vertex {q0 } in GD is also a final vertex.

This, if the loop terminates, it constructs the dfa corresponding to the nfa.
Does the loop terminate?

• Each iteration of the loop adds one edge to GD .


• There are a finite number of edges possible in GD .

• Thus the loop must terminate.

What is the loop invariant? (This ia a property always that must hold at the
loop test.)

• If there is a walk ({q0 }, . . . {. . . qi , . . .}) in GD labeled w, then there is a


walk (q0 , . . . qi ) in GN labeled w.

2.3.4 Linz Example 2.12

Convert the nfa in Linz Figure 2.12 to an equivalent dfa.


Intermediate steps are shown in Figures 2.12-1 and 2.12-2, with the final results
in Linz Figure 2.13.

15
Linz Fig. 2.12: An NFA

Intermediate Fig. 2.12-1

Intermediate Fig. 2.12-2

16
Linz Fig. 2.13: Corresponding DFA

17
Linz Fig. 2.14: An NFA

2.3.5 Linz Example 2.13

Convert the nfa shown in Linz Figure 2.14 into an equivalent dfa.
δD ({q0 }, 0) = δN

(q0 , 0) = {q0 , q1 }
δD ({q0 }, 1) = δN

(q0 , 1) = {q1 }
δD ({q0 , q1 }, 0) = δN

(q0 , 0) ∪ δN

(q1 , 0) = {q0 , q1 , q2 }
δD ({q0 , q1 , q2 }, 1) = δN

(q0 , 1) ∪ δN

(q1 , 1) ∪ δN

(q2 , 1) = {q1 , q2 }
The above gives us the partially constructed dfa shown in Linz Figure 2.15.

Linz Fig. 2.15

δD ({q1 }, 0) = δN

(q1 , 0) = {q2 }
δD ({q1 }, 1) = δN

(q1 , 1) = {q2 }

18
δD ({q2 }, 0) = δN

(q1 , 0) = ∅
δD ({q2 }, 1) = δN

(q2 , 1) = {q2 }
δD ({q0 , q1 }, 1) = δN

(q0 , 1) ∪ δN

(q1 , 1) = {q1 , q2 }
δD ({q0 , q1 , q2 }, 0) = δN

(q0 , 0) ∪ δN

(q1 , 0) ∪ δN

(q2 , 0) = {q0 , q1 , q2 }
δD ({q1 , q2 }, 0) = δN

(q1 , 0) ∪ δN

(q2 , 0) = {q2 }
δD ({q1 , q2 }, 1) = δN

(q1 , 1) ∪ δN

(q2 , 1) = {q2 }
Now, the above gives us the dfa shown in Linz Figure 2.16.

Linz Fig. 2.16: Corresponding DFA for NFA

2.4 Reduction in the Number of States in Finite Au-


tomata

This section is not covered in this course.

19

S-ar putea să vă placă și