Finite Automata
Finite Automata
Contents
Deterministic Finite Automata
(DFA) Regular Languages
Regular Expressions
Nondeterministic Finite Automata
(NFA) Transformations
Non-Regular Languages
Deterministic Finite Automata (DFA)
Electric bulb
Problem
Design
the logic
behind
an
electric
bulb.
Electric bulb
Problem
Design the logic behind an electric
bulb.
Solution
Diagra
m.
Analysis.
States = {nolight, light}, Input = {off,
on} Finite Automaton.
on
of
f
Automatic doors
Problem
Design the logic behind automatic doors in
Walmart.
Automatic doors
Solution
Diagra
m.
Analysis.
States = {close, open}, Input = {left, right,
neither} Finite Automaton.
neither
left, right left,
right
close open
neither
Basic features of finite automata
Definition
A decision problem is a computational problem with
a ‘yes’ or ‘no’ answer.
A computer that solves a decision problem is a
decider. Input to a decider: A string w
Output of a decider: Accept (w is in the language) or
Reject (w is not in the language)
w Decid yes/
er no
What is a decision problem?
English Acce
word pt
Other Rejec
word t
a b
a
b
bbab q0 q1 q2 Acce
pt
a,
b
a b
a
b
aaba q0 q1 q2 Rejec
t
a,
b
How does a DFA work?
Problem
What language does the DFA
accept?
a b b
a
start q0 q2
q1 a,
b
How does a DFA work?
Problem
What language does the DFA
accept?
a a
b
start q0 q2
q1 b a, b
Examples
The DFA accepts the following strings:
b, ab, bb, aabbbb, ababababab, . . . D ends
with b baa, abaa, ababaaaaaa, . . . D ends with b followed
by even a’s The DFA rejects the following strings:
a, ba, babaaa, . . .
What language does the DFA accept?
Construct DFA for Σ = { a }
Problem
Construct a DFA that accepts all strings from the
language
L = {ϵ, a, aa, aaa, aaaa, . . .}
Construct DFA for Σ = { a }
Problem
Construct a DFA that accepts all strings from the
language
L = {ϵ, a, aa, aaa, aaaa, . . .}
Solution
Language L: Σ∗ = {ϵ, a, aa, aaa, aaaa, . . .}
Expression: a∗
Deterministic Finite Automaton (DFA) M :
a
start q0
Construct DFA for Σ = { a }
Problem
Construct a DFA that accepts all strings from the
language
L = {}
Construct DFA for Σ = { a }
Problem
Construct a DFA that accepts all strings from the
language
L = {}
Solution
Language L: $ = D Empty
{} language
Expression: $
DFA M : a
start
q0
Construct DFA for Σ = { a }
Problem
Construct a DFA that accepts all strings from the
language
L = {a, aa, aaa, aaaa, . . .}
Construct DFA for Σ = { a }
Problem
Construct a DFA that accepts all strings from the
language
L = {a, aa, aaa, aaaa, . . .}
Solution
Language L: Σ∗ − { ϵ} = {a, aa, aaa, aaaa, . . .}
Expression: a +
DFA M :
a
a
start q0 q1
Construct DFA for Σ = { a }
Problem
Construct a DFA that accepts all strings from the
language
L = {aaa}
Construct DFA for Σ = { a }
Problem
Construct a DFA that accepts all strings from the
language
L = {aaa}
Solution
Language L:
{aaa}
Expression: aaa
DFA M :
a
a
a
Construct DFA for Σ = { a }
Problem
Construct a DFA that accepts all strings from the
language
L = {strings with even size}
Construct DFA for Σ = { a }
Problem
Construct a DFA that accepts all strings from the
language
L = {strings with even size}
Solution
Language L: {ϵ, aa, aaaa, aaaaaa, . . .}
Expression: (aa)∗
DFA M :
a
start q0
q a
Construct DFA for Σ = { a }
Problem
Construct a DFA that accepts all strings from the
language
L = {strings with odd size}
Construct DFA for Σ = { a }
Problem
Construct a DFA that accepts all strings from the
language
L = {strings with odd size}
Solution
Language L: {a, aaa, aaaaa, . . .}
Expression: a(aa)∗
DFA M :
a
start q0
q a
Construct DFA for Σ = { a }
Problem
Construct a DFA that accepts all strings from the
language
L = {strings with size n where n mod 4 = 2}
Construct DFA for Σ = { a }
Problem
Construct a DFA that accepts all strings from the
language
L = {strings with size n where n mod 4 = 2}
Solution
Language L: {aaa, aaaaaaa, aaaaaaaaaaa, . . .}
Expression: aa(aaaa)∗
DFA M :
a a a
start rem 0 rem 1 rem 2 rem
3
a
What about strings with size n where n mod k = i?
Specifying a DFA
Definition
A deterministic finite automaton (DFA) M is
a 5-tuple
M = (Q , Σ, δ, q0, F ), where,
1. Q: A finite set (set of states). D Space
(computer memory)
2. Σ: A finite set (alphabet).
3. δ : Q × Σ → Q is the transition function.
D Time
(comput
ation)
Acceptance and rejection of strings
Definition
A DFA accepts a string w = w1w2 . . . wk iff there
exists a sequence of states r0, r 1 , . . . , r k such that
the current state starts from the start state and
ends at a final state when all the symbols of w have
been read.
A DFA rejects a string iff it does not accept it.
What is a regular language?
Definition
We say that a DFA M accepts a language L if
L = {w | M accepts w}.
A language is called a regular language if some DFA
accepts or recognizes it.
Construct DFA for Σ = {a, b}
Problem
Construct a DFA that accepts all strings from the
language
L = {strings with odd number of b’s}
Construct DFA for Σ = {a, b}
Problem
Construct a DFA that accepts all strings from the
language
L = {strings with odd number of b’s}
Solutio
n
States
qodd: DFA is in this state if it has read odd
b’s. qeven: DFA is in this state if it has read
even b’s.
Construct DFA for Σ = {a, b}
Problem
Construct a DFA that accepts all strings from the
language
L = {strings with odd number of b’s}
Solution
Language L: {strings with odd number
of b’s} Expression: a∗b(a ∪ ba∗b)∗ or
a∗ba∗(ba∗ba∗)∗ DFA M : a a
b
star
qeve qodd
t n b
Construct DFA for Σ = {a, b}
Problem
Construct a DFA that accepts all strings from the
language
L = {strings with odd number of b’s}
Solution (continued)
DFA M is specified
as
Set of states is Q = {qeven,
qodd} Set of symbols is Σ =
{a, b} Start state is qeven a b
Set of accept states qis F =qeven
even qodd
Problem
Construct a DFA that accepts all strings from the
language
L = {strings containing bab}
Construct DFA for Σ = {a, b}
Problem
Construct a DFA that accepts all strings from the
language
L = {strings containing bab}
Solutio
n
States
qb: DFA is in this state if the last symbol read was b,
but the substring bab has not been read.
qba: DFA is in this state if the last two symbols read
were ba, but the substring bab has not been read.
qbab: DFA is in this state if the substring bab has been
read in the input string.
q: In all other cases, the DFA is in this state.
Construct DFA for Σ = {a, b}
Problem
Construct a DFA that accepts all strings from the
language
L = {strings containing bab}
Solution (continued)
Language L: {strings containing
bab} Expression: (a∗b+aa)∗bab(a
∪ b)∗ DFA M : a,
a
b
b b a b
start q qb qba qbab
a
Construct DFA for Σ = {a, b}
Problem
Construct a DFA that accepts all strings from the
language
L = {strings containing bab}
Solution (continued)
DFA M is specified
as
Set of states is Q =
{q, qb, qba, qbab}
Transition function δ is:
Set of symbols is Σ δ a b
= {a, b} q q qb
Start state is q qb qba qb
Set of accept states qba q qbab
is F = {qbab} qbab qbab qbab
Equivalence of different computation models
DFA
NFA