Finite Automata

Finite Automata
Seungjin Choi
Department of Computer Science and Engineering
Pohang University of Science and Technology
77 Cheongam-ro, Nam-gu, Pohang 37673, Korea
[email protected]
1 / 28
Outline
I
Examples of finite automata (=finite state machine, good models
for computers with extremely limited amount of memory)
I
Deterministic finite automata (DFA)
I
Nondeterministic finite automata (NFA)
I
Equivalence between DFA and NFA
I
More details and applications
2 / 28
An Example of FA: Automatic Door
I
Two states: ”open” or ”closed”
I
Four inputs: front, rear, both, neither.
Rear
pad
Front
pad
Door
(a) Automatic Door
rear
both
neither
front
closed
front
rear
both
open
neither
(b) FA
3 / 28
An Example of FA: e-Commerce
Protocol for e-commerce using e-money
Allowed events:
1. The customer can pay the store (=send he money-file to the store).
2. The customer can cancel the money (like putting a stop on a check).
3. The store can ship the goods to the customer.
4. The store can redeem the money (=cash the check).
5. The bank can transfer the money to the store.
4 / 28
5 / 28
6 / 28
7 / 28
Deterministic Finite Automata (DFA)
Definition (DFA)
A deterministic finite automaton is a quintuple (5-tuple),
M = (Q, Σ, δ, q0 , F ), where
I Q: a finite set of states,
I
Σ: a finite set of input symbols (input alphabet),
I
δ : Q × Σ → Q: a transition function,
I
I
q0 ∈ Q: a start state (an initial state),
F ⊆ Q: a set of final or accepting states.
8 / 28
0
0
q0
q1
1
0
1
q2
1
The DFA associated with this transition diagram is given by
M = ({q0 , q1 , q2 }, {0, 1}, δ, q0 , {q1 }) ,
δ(q0 , 0)
= q0 ,
δ(q0 , 1) = q1 ,
δ(q1 , 0)
= q0 ,
δ(q1 , 1) = q2 ,
δ(q2 , 0)
= q2 ,
δ(q2 , 1) = q1 .
M accepts 01, 101, 0111, 11001 and does not accept 00,100, 1100.
9 / 28
Notations for DFA
I
Transition diagram
I
Transition table
I
See Sec. 2.2.3
10 / 28
Extended Transition Function
The extended transition function, δ ∗ : Q × Σ∗ → Q, describes what
happens when we start in any state and follow any sequence of inputs.
For example, given δ(q0 , a) = q1 and δ(q1 , b) = q2 , we have
δ ∗ (q0 , ab) = q2 .
We can define δ ∗ recursively by
δ ∗ (q, )
∗
δ (q, wa)
= q,
= δ(δ ∗ (q, w ), a),
q ∈ Q, w ∈ Σ∗ , a ∈ Σ.
For example, δ ∗ (q0 , a) = δ(δ ∗ (q0 , ), a) = δ(q0 , a) = q1 . Thus,
δ ∗ (q0 , ab) = δ(δ ∗ (q0 , a), b) = δ(q1 , b) = q2 .
11 / 28
The Language of DFA
Definition
The language accepted by a DFA, M = (Q, Σ, δ, q0 , F ), is the set of all
strings on Σ accepted by M,
L(M) = {w ∈ Σ∗ | δ ∗ (q0 , w ) ∈ F }.
Example: L = {0n 1 | n ≥ 0}, the language accepted by the DFA shown
below:
0
0, 1
q0
1
q1
0, 1
q2
12 / 28
Example: Find a DFA that recognizes the set of all strings on
Σ = {a, b} starting with the prefix ab.
Need 4 states: (1) a start state; (2) 2 states for recognizing ab ending in
a final trap state; (3) one non-final trap state.
a, b
q0
a
q1
b
q2
q3
a, b
13 / 28
Example: Find a DFA that accepts all the strings on Σ = {0, 1}, except
those containing the substring 001.
1
0
0
ǫ
0
0
00
0, 1
1
001
1
14 / 28
Regular Language
Definition (Regular)
A language L is called regular if and only if there exits a DFA M such
that
L = L(M).
Regular language ⇐⇒ DFA
What you need to do to show that L is regular, is to find a
DFA M such that L = L(M).
15 / 28
Example: Show that the language, L = {awa | w ∈ {a, b}∗ } is regular.
b
a
b
q0
b
a
q2
q3
a
q1
a,b
16 / 28
Example: Show that L2 is regular, where
L2 = {aw1 aaw2 a | w1 , w2 ∈ {a, b}∗ }.
b
q0
b
a
b
b
q2
q3
a
a
b
q4
a
q5
a
q1
a,b
17 / 28
Nondeterministic Finite Automata (NFA)
Definition (NFA)
A nondeterministic finite automaton is a quintuple (5-tuple),
M = (Q, Σ, δ, q0 , F ), where
I Q: a finite set of states,
I
Σ: a finite set of input symbols (input alphabet),
I
δ : Q × (Σ ∪ {}) → 2Q : a transition function,
I
I
q0 ∈ Q: a start state (an initial state),
F ⊆ Q: a set of final or accepting states.
18 / 28
Differences between DFA and NFA
I
In NFA, the range of the transition function δ is the power set 2Q ,
implying that several different movements are allowed. For example,
δ(q1 , a) = {q0 , q2 }.
I
NFA can make a transition without consuming an input symbol. For
example,
δ(q0 , ) = q1 .
I
The set δ(qi , a) may be empty. In other words, no transition may be
defined for a specific situation.
19 / 28
An Example of NFA
0
q0
0, 1
q1
q2
1
ǫ
I
Several edges with the same label originate from one vertex
I
-transition
I
δ(q2 , 0) = φ
20 / 28
NFA with Extended Transition Functions
ǫ
q0
0
q1
ǫ
q2
I
For an NFA, the extended transition function is defined so that
δ ∗ (qi , w ) contains qj if and only if there is a walk in the transition
graph from qi to qj labeled w .
I
δ ∗ (q1 , 0) = {q0 , q1 , q2 } and δ ∗ (q2 , ) = {q0 , q2 }.
21 / 28
The Language of NFA
Definition
The language L accepted by an NFA, M = (Q, Σ, δ, q0 , F ), is defined by
L(M) = {w ∈ Σ∗ | δ ∗ (q0 , w ) ∩ F 6= φ} .
Example: L = {(10)n | n ≥ 0} is accepted by the NFA shown below:
0
q0
0, 1
q1
q2
1
ǫ
22 / 28
Why Nondeterminism?
I
Nondeterministic machines can serve as models of
search-and-backtrack algorithms.
I
Sometimes helpful in solving problems easily.
I
Certain results are more easily established for NFA’s than for DFA’s.
23 / 28
Equivalence
Definition
Two finite automata M1 and M2 are said to be equivalent if
L(M1 ) = L(M2 ),
that is, they accept the same language.
0
q0
0, 1
0
0, 1
q1
1
q2
q0
q1
1
q2
0
ǫ
(a) NFA
1
(b) DFA
These two FAs are equivalent.
24 / 28
Equivalence of DFA and NFA
Theorem
For any NFA MN there exists a DFA MD such that
L(MD ) = L(MN )
and vice versa.
I
This involves the subset construction, an important example how an
automaton MB can be generically constructed from another
automaton MA .
I
Given an NFA MN = (QN , Σ, δN , q0 , FN ), we will construct a DFA
MD = (QD , Σ, δD , {q0 }, FD ) such that L(MD ) = L(MN ).
25 / 28
Subset Construction
I
QD = {S | S ⊆ QN }, i.e., QD = 2QN .
Note that |QD | = 2|QN | , although most states in QD are likely to be
garbage.
I
FD = {S ⊆ QN | S ∩ FN 6= φ}.
I
For every S ⊆ QN and a ∈ Σ,
δD (S, a) = ∪ δN (q, a).
q∈S
26 / 28
Example: Subset Construction
0, 1
q0
0
q1
1
q2
φ
→ {q0 }
{q1 }
∗{q2 }
{q0 , q1 }
∗{q0 , q2 }
∗{q1 , q2 }
∗{q0 , q1 , q2 }
0
φ
{q0 , q1 }
φ
φ
{q0 , q1 }
{q0 , q1 }
φ
{q0 , q1 }
1
φ
{q0 }
{q2 }
φ
{q0 , q2 }
{q0 }
{q2 }
{q0 , q2 }
27 / 28
Example: An Equivalent DFA
Determine states accessible from the start state and draw a transition
graph:
0
1
{q0}
0
{q0, q1}
1
{q0, q2}
0
1
28 / 28