Finite Automata
Seungjin Choi
Department of Computer Science and EngineeringPohang University of Science and Technology
77 Cheongam-ro, Nam-gu, Pohang 37673, [email protected]
1 / 28
Outline
I Examples of finite automata (=finite state machine, good modelsfor 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.
Door
Frontpad
Rearpad
(a) Automatic Door
open
front
neither
neitherbothrear front
rearboth
closed
(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 q0 ∈ Q: a start state (an initial state),
I F ⊆ Q: a set of final or accepting states.
8 / 28
q0 q1 q2
0 00
11
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 whathappens 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,
δ∗(q,wa) = δ(δ∗(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
DefinitionThe language accepted by a DFA, M = (Q,Σ, δ, q0,F ), is the set of allstrings on Σ accepted by M,
L(M) = {w ∈ Σ∗ | δ∗(q0,w) ∈ F}.
Example: L = {0n1 | n ≥ 0}, the language accepted by the DFA shownbelow:
q0 q1 q2
0
1
0, 1
0, 1
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 ina final trap state; (3) one non-final trap state.
q0 q1 q2
q3
a ba, b
a, b
13 / 28
Example: Find a DFA that accepts all the strings on Σ = {0, 1}, exceptthose containing the substring 001.
00100ǫ
0
0
0
01
1
1
0, 1
14 / 28
Regular Language
Definition (Regular)A language L is called regular if and only if there exits a DFA M suchthat
L = L(M).
Regular language ⇐⇒ DFA
What you need to do to show that L is regular, is to find aDFA M such that L = L(M).
15 / 28
Example: Show that the language, L = {awa |w ∈ {a, b}∗} is regular.
a,b
q0
q1
q2 q3
a
a
a
bb
b
16 / 28
Example: Show that L2 is regular, whereL2 = {aw1aaw2a |w1,w2 ∈ {a, b}∗}.
a,b
q0
q1
q2 q3 q4 q5
a
aa
a a
bbb
b
b
If L is regular, so are L2, L3, . . .? (the answer is ”correct”)
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 q0 ∈ Q: a start state (an initial state),
I 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. Forexample,
δ(q0, ε) = q1.
I The set δ(qi , a) may be empty. In other words, no transition may bedefined for a specific situation.
19 / 28
An Example of NFA
ǫ
q0 q1 q2
0
1
0, 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 q1 q20
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 transitiongraph from qi to qj labeled w .
I δ∗(q1, 0) = {q0, q1, q2} and δ∗(q2, ε) = {q0, q2}.
21 / 28
The Language of NFADefinitionThe 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:
ǫ
q0 q1 q2
0
1
0, 1
22 / 28
Why Nondeterminism?
I Nondeterministic machines can serve as models ofsearch-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
DefinitionTwo finite automata M1 and M2 are said to be equivalent if
L(M1) = L(M2),
that is, they accept the same language.
ǫ
q0 q1 q2
0
1
0, 1 q0 q1 q2
0
0
1
1
0, 1
(a) NFA (b) DFA
These two FAs are equivalent.
24 / 28
Equivalence of DFA and NFA
TheoremFor 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 anautomaton MB can be generically constructed from anotherautomaton MA.
I Given an NFA MN = (QN ,Σ, δN , q0,FN), we will construct a DFAMD = (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 begarbage.
I FD = {S ⊆ QN |S ∩ FN 6= φ}.I For every S ⊆ QN and a ∈ Σ,
δD(S , a) = ∪q∈S
δN(q, a).
26 / 28
Example: Subset Construction
q0 q1 q20 1
0, 10 1
φ φ φ→ {q0} {q0, q1} {q0}{q1} φ {q2}∗{q2} φ φ{q0, q1} {q0, q1} {q0, q2}∗{q0, q2} {q0, q1} {q0}∗{q1, q2} φ {q2}
∗{q0, q1, q2} {q0, q1} {q0, q2}
27 / 28
Example: An Equivalent DFA
Determine states accessible from the start state and draw a transitiongraph:
{q0} {q0, q1} {q0, q2}0
0
0
1
1
1
28 / 28