Finite Automata
Finite Automata
– p. 1/30
Some Examples
1. ON-OFF switch (push button).
Two states, on and off. Each input toggles the state.
2. Recognition of words (lexical analyzers).
3. Many home devices such as VCRs, . . .
– p. 2/30
Deterministic Finite Automata (DFA)
A deterministic Finite Automaton consists of:
A = (Q, Σ, δ, q0 , F ).
– p. 3/30
DFA: Example
DFA accepting strings which contain odd number of bs
(here we are taking the alphabet as {a, b}).
– p. 4/30
a
a
b
q0 q1
b
– p. 5/30
DFA: Another Example
DFA accepting strings which contain 00 as a substring.
This is same as accepting the set: {w | w is of the form x00y
for some strings x and y} (here we are taking the alphabet
as {0, 1}).
– p. 6/30
Transition Diagrams
Using circles to denote states, arrows to denote
transition.
Starting state is denoted by using an arrow labeled
start.
Final/accepting states are denoted by using double
circles.
– p. 7/30
Transition Tables
0 1
q0 q1 q0
q1 q2 q0
q2 q2 q2
– p. 8/30
Extending transition function to strings
Basis:
δ̂(q, ǫ) = q .
Induction:
δ̂(q, xa) = δ(δ̂(q, x), a).
– p. 9/30
Language Accepted by a DFA
L(A) = {w | δ̂(q0 , w) ∈ F }
L(A) is also denoted by Lang(A).
– p. 10/30
Dead and Unreachable States in a DFA
Dead State is a state from which one cannot reach a final
state, whatever the sequence of inputs.
Formally, q is a dead state if, for all w ∈ Σ∗ , δ̂(q, w) 6∈ F .
Unreachable states are states which cannot be reached
from starting state, whatever the sequence of inputs.
Formally, q is an unreachable state if, for all w ∈ Σ∗ ,
δ̂(q0 , w) 6= q .
– p. 11/30
Nondeterministic Finite State Automata
Nondeterministic Finite State Automata (NFA)
A = (Q, Σ, δ, q0 , F ).
The transition function maps the input (state, symbol) to
a set of states (a subset of Q)
– p. 12/30
Extending transition function to strings
Basis:
δ̂(q, ǫ) = {q}
Induction:
[
δ̂(q, xa) = δ(p, a)
p∈δ̂(q,x)
– p. 13/30
Language Accepted by an NFA
L(A) = {w | δ̂(q0 , w) ∩ F 6= ∅}
– p. 14/30
Equivalence of DFA and NFA
Clearly, a DFA is also an NFA.
So, we only need to show that a language accepted by NFA
is also accepted by some DFA.
Suppose NFA A = (Q, Σ, δ, q0 , F ) is given.
Define DFA AD = (QD , Σ, δD , {q0 }, FD ) as follows.
QD = {S | S ⊆ Q}.
FD = {S | S ⊆ Q and S ∩ F 6= ∅}.
[
δD (S, a) = δ(q, a)
q∈S
– p. 15/30
Equivalence of DFA and NFA
Claim: For any string w , δˆD ({q0 }, w) = δ̂(q0 , w).
Proof: By induction on length of w .
Base Case: w = ǫ.
In this case clearly, δˆD ({q0 }, ǫ) = {q0 } = δ̂(q0 , ǫ).
Induction Step:
= δ̂(q0 , wa)
– p. 17/30
NFA with ǫ transitions
A = (Q, Σ, δ, q0 , F ).
δ maps Q × (Σ ∪ {ǫ}) to subsets of Q.
– p. 18/30
ǫ Closures
Definition of Eclose(q)
1. q ∈ Eclose(q).
2. If state p ∈ Eclose(q), then each state in δ(p, ǫ), is in
Eclose(q).
3. We iterate step 2, until no more changes are done to
Eclose(q).
– p. 19/30
Extended transition function δ̂
δ̂(q, ǫ) = Eclose(q).
δ̂(q, wa) = S , where S is defined as follows:
Let [
R= δ(p, a)
p∈δ̂(q,w)
Then,
[
S= Eclose(p)
p∈R
– p. 20/30
Equivalence of ǫ-NFA and DFA
Tutorial problem.
Idea:
Suppose A = (Q, Σ, δ, q0 , F ).
Form AD = (QD , Σ, δD , qD , FD ) as follows:
Essentially let QD = {S | S ⊆ Q}.
qD = Eclose(q0 ).
Transition: [
δD (S, a) = δ̂(p, a)
p∈S
FD = {S | S ∩ F 6= ∅}.
– p. 21/30
Regular Expressions
Basis: The constant ǫ and ∅ are regular expressions, and
L(ǫ) = {ǫ} and L(∅) = ∅.
If a is any symbol in Σ, then a is a regular expression, and
L(a) = {a}.
Induction: If r1 and r2 are regular expressions, then so are:
(a) r1 + r2
L(r1 + r2 ) = L(r1 ) ∪ L(r2 ).
(b) r1 · r2
L(r1 · r2 ) = {xy | x ∈ L(r1 ) and y ∈ L(r2 )}
(c) r1∗
L(r1∗ ) = {x1 x2 . . . xk | for 1 ≤ i ≤ k , xi ∈ L(r1 )}.
Note: in above k can be 0, and thus, ǫ ∈ L(r1∗ ).
(d) (r1 )
L((r1 )) = L(r1 ).
– p. 22/30
Precedence
∗ has highest precedence
· has next highest precedence
+ has least precedence
Association
– p. 23/30
DFA to Regular Expressions
Let the DFA A = (Q, Σ, δ, qstart , F ).
We assume Q = {1, 2, . . . , n} for some n, and qstart = 1.
k denote the regular expression for the set of strings
Ri,j
which can be formed by going from state i to state j using
intermediate states numbered ≤ k .
Base Case: Definition of Ri,j 0 .
0 = a + a . . . + a , where a , a , . . . , a are all
If i 6= j : Ri,j 1 2 m 1 2 m
the symbols such that δ(i, ar ) = j (If no such symbols, then
0 = ∅).
Ri,j
0 = ǫ + a + a . . . + a , where a , a , . . . , a are all
If i = j : Ri,i 1 2 m 1 2 m
the symbols such that δ(i, ar ) = i
– p. 24/30
DFA to Regular Expressions
Induction Case:
k+1 k + Rk k ∗ Rk
Ri,j = Ri,j i,k+1 (R k+1,k+1 ) k+1,j .
Regular Expression for language L(A) is given by:
X
n
R1,j
j∈F
– p. 25/30
Regular Expressions to ǫ-NFA
We will show how to inductively construct a ǫ-NFA for every
regular expression which additionally satisfies:
a) It has only one final state.
b) There is no transition into the starting state.
c) There is no transition out of the final state.
d) The starting and final states are different.
Base Cases:
(i) ∅:
A = ({q0 , qf }, Σ, δ, q0 , {qf }), where δ is empty function (that
is, there are no tansitions).
(ii) ǫ:
A = ({q0 , qf }, Σ, δ, q0 , {qf }), where δ(q0 , ǫ) = qf .
(iii) a:
A = ({q0 , qf }, Σ, δ, q0 , {qf }), where δ(q0 , a) = qf .
– p. 26/30
Induction Case:
(iv) r1 + r2 :
Suppose A1 = (Q1 , Σ, q01 , δ1 , F1 ), and
A2 = (Q2 , Σ, q02 , δ2 , F2 )
are the automata for r1 and r2 respectively, where
Q1 ∩ Q2 = ∅.
Let A = ({q0 , qf } ∪ Q1 ∪ Q2 , Σ, δ, q0 , {qf }), where
δ(q0 , ǫ) = {q01 , q02 }
δ(qf1 , ǫ) = {qf }, for qf1 ∈ F1
δ(qf2 , ǫ) = {qf }, for qf2 ∈ F2
Additionally, δ contains all transitions of δ1 and δ2
– p. 27/30
(v) r1 · r2 :
Suppose A1 = (Q1 , Σ, q01 , δ1 , F1 ), and
A2 = (Q2 , Σ, q02 , δ2 , F2 )
are the automata for r1 and r2 respectively, where
Q1 ∩ Q2 = ∅.
Let A = ({q0 , qf } ∪ Q1 ∪ Q2 , Σ, δ, q0 , {qf }), where
δ(q0 , ǫ) = {q01 }
δ(qf1 , ǫ) = {q02 }, for qf1 ∈ F1
δ(qf2 , ǫ) = {qf }, for qf2 ∈ F2
Additionally, δ contains all transitions of δ1 and δ2
– p. 28/30
(vi) r1∗ :
Suppose A1 = (Q1 , Σ, q01 , δ1 , F1 )
is the automata for r1 .
Let A = ({q0 , qf } ∪ Q1 , Σ, δ, q0 , {qf }), where
δ(q0 , ǫ) = {q01 , qf }
δ(qf1 , ǫ) = {q01 , qf }, for qf1 ∈ F1
Additionally, δ contains all transitions of δ1
– p. 29/30
Some Properties of Regular Expressions
M +N =N +M
L(M + N ) = LM + LN
L+L=L
(L∗ )∗ = L∗ .
∅∗ = ǫ
ǫ∗ = ǫ
L+ = LL∗ = L∗ L
L∗ = ǫ + L+
(L + M )∗ = (L∗ M ∗ )∗
– p. 30/30