Finite Automata Examples
Finite Automata Examples
0 1 1 0 0 ……..
Finite
Control
• Example #1:
0
q0 q1 1
0
1 0 0 1 1
q0 q0 q1 q0 q0 q0
a a a/b/c
c c
q0 q1 q2
b b
a c c c b accepted
q0 q0 q1 q2 q2 q2
a a c rejected
q0 q0 q0 q1
4
Formal Definition of a DFA
• A DFA is a five-tuple:
M = (Q, Σ, δ, q0, F)
δ:
0 1
q0 q1 q0
q1 q0 q1
6
• For example #2:
a a a/b/c
Q = {q0, q1, q2}
c c
Σ = {a, b, c} q0 q1 q2
Start state is q0
F = {q2} b b
δ: a b c
q0 q0 q0 q1
q1 q1 q1 q2
q2 q2 q2 q2
δ^ : (Q x Σ*) –> Q
δ^(q,w) – The state entered after reading string w having started in state q.
Formally:
1) δ^(q, ε) = q, and
2) For all w in Σ* and a in Σ
δ^(q,wa) = δ (δ^(q,w), a)
8
• Recall Example #1: 1
0
q0 q1 1
0
• Therefore:
10
• Recall Example #2:
1 1 1
0
0
q0 q1 q2
0
11
• Is 011 accepted? No, since δ(q0, 011) = q1 is not a final state.
• Recall Example #2:
1 1 1
0
0
q0 q1 q2
0
• Is 10 accepted? No, since δ(q0, 10) = q1 is not a final state. The fact that
δ(q1, 10) = q2 is irrelevant!
12
Definitions for DFAs
• Let M = (Q, Σ, δ,q0,F) be a DFA and let w be in Σ*. Then w is accepted by M
iff δ(q0,w) = p for some state p in F.
• Let M = (Q, Σ, δ,q0,F) be a DFA. Then the language accepted by M is the set:
• Let M1 = (Q1, Σ1, δ1, q0, F1) and M2 = (Q2, Σ2, δ2, p0, F2) be DFAs. Then M1 and
M2 are equivalent iff L(M1) = L(M2).
13
• Notes:
– A DFA M = (Q, Σ, δ,q0,F) partitions the set Σ* into two sets: L(M) and
Σ* - L(M).
– Similarly, if L(M1) = L(M2) then L(M1) is a subset of L(M2) and L(M2) is a subset of
L(M1).
• Questions:
– How do we determine whether or not a given language is regular?
– How could a program “simulate” a DFA?
14
• Give a DFA M such that:
0/1
0/1 0/1
q0 q1 q2
15
• Give a DFA M such that:
b/c a/b/c
a
a
q0 q1 q2
b/c
16
• Give a DFA M such that:
b/c a a/b/c
a a
b
q0 q1 q2 q3
c
b/c
17
• Give a DFA M such that:
a
b
a
q1 q2 q3
b a/b
a a
q0 a b q7
b
b a
b a
q4 q5 q6
b
18
• Let Σ = {0, 1}. Give DFAs for {}, {ε}, Σ*, and Σ+.
0/1
q0 q0 q1
19
Nondeterministic Finite State
Automata (NFA)
• An NFA is a five-tuple:
M = (Q, Σ, δ, q0, F)
0 1 0/1
Q = {q0, q1, q2}
0 1
Σ = {0, 1} q0 q1 q2
Start state is q0
F = {q2}
δ: 0 1
q0 {q0, q1} {}
{} {q1, q2}
q1
{q2} {q2}
q2
21
• Example #2: pair of 0’s or pair of 1’s
0/1 0/1
Q = {q0, q1, q2 , q3 , q4}
0 0
Σ = {0, 1} q0 q3 q4
Start state is q0
1 0/1
F = {q2, q4}
1
q1 q2
δ: 0 1
q0 {q0, q3} {q0, q1}
{} {q2}
q1
{q2} {q2}
q2
{q4} {}
q3 {q4} {q4}
22
• Notes:
– δ(q,s) may not be defined for some q and s (why?).
– Informally, a string is said to be accepted if there exists a path to some
state in F.
– The language accepted by an NFA is the set of all accepted strings.
23
• Determining if a given NFA (example #2) accepts a given string (001)
can be done algorithmically:
0 0 1
q0 q0 q0 q0
q3 q3 q1
q4 q4 accepted
24
• Another example (010):
q0 0 q0 1 q0 0 q0
q3 q1 q3
not accepted
• All paths have been explored, and none lead to an accepting state.
25
• Question: Why non-determinism is useful?
–Non-determinism = Backtracking
–Non-determinism hides backtracking
–Programming languages, e.g., Prolog, hides backtracking => Easy to
program at a higher level: what we want to do, rather than how to do it
–Useful in complexity study
–Is NDA more “powerful” than DFA, i.e., accepts type of languages that any
DFA cannot?
26
• Let Σ = {a, b, c}. Give an NFA M that accepts:
a/b/c a/b/c
a b
q0 q1 q2
Is L a subset of L(M)?
Is L(M) a subset of L?
a/b
b a/b a/b q3
q0 q1 q2
Is L a subset of L(M)?
Is L(M) a subset of L?
28
Extension of δ to Strings and Sets of States
• What we currently have: δ : (Q x Σ) –> 2Q
• We will do this in two steps, which will be slightly different from the
book, and we will make use of the following NFA.
0
0 1
q0 q1 q2
0 1 0
0
q3 q4
1
0 29
Extension of δ to Strings and Sets of States
• Step #1:
• Note that:
δ#({p},a) = δ(q, a)
q∈{ p }
by definition of δ#, rule #1 above
= δ(p, a)
31
• Step #2:
Formally:
1 0
0 q1 1 q2
q0
0 1
1
q3
What is δ({q0}, 10)?
Informally: The set of states the NFA could be in after processing 10,
having started in state q0, i.e., {q1, q2, q3}.
35
Definitions for NFAs
36
Equivalence of DFAs and NFAs
• Do DFAs and NFAs accept the same class of languages?
– Is there a language L that is accepted by a DFA, but not by any NFA?
– Is there a language L that is accepted by an NFA, but not by any DFA?
37
• Consider the following DFA: 2 or more c’s
a a a/b/c
Q = {q0, q1, q2}
c c
Σ = {a, b, c} q0 q1 q2
Start state is q0
F = {q2} b b
δ: a b c
q0 q0 q0 q1
q1 q1 q1 q2
q2 q2 q2 q2
38
• An Equivalent NFA:
a a a/b/c
Q = {q0, q1, q2}
c c
Σ = {a, b, c} q0 q1 q2
Start state is q0
F = {q2} b b
δ: a b c
q0 {q0} {q0} {q1}
39
• Lemma 1: Let M be an DFA. Then there exists a NFA M’ such that
L(M) = L(M’).
40
• Lemma 2: Let M be an NFA. Then there exists a DFA M’ such that L(M) =
L(M’).
• Proof: (sketch)
q’0 = [q0]
δ: 0 1
q0 {q1} {}
42
• Construct DFA M’ as follows:
1
0/1
1 [q0]
0 [q1]
[]
1
[q0, q1]
0
δ({q0}, 0) = {q1} => δ’([q0], 0) = [q1]
δ({q0}, 1) = {} => δ’([q0], 1) = [ ]
δ({q1}, 0) = {q0, q1} => δ’([q1], 0) = [q0, q1]
δ({q1}, 1) = {q1} => δ’([q1], 1) = [q1]
δ({q0, q1}, 0) = {q0, q1} => δ’([q0, q1], 0) = [q0, q1]
δ({q0, q1}, 1) = {q1} => δ’([q0, q1], 1) = [q1]
δ({}, 0) = {} => δ’([ ], 0) = [ ]
δ({}, 1) = {} => δ’([ ], 1) = [ ] 43
• Theorem: Let L be a language. Then there exists an DFA M such
that L = L(M) iff there exists an NFA M’ such that L = L(M’).
• Proof:
(if) Suppose there exists an NFA M’ such that L = L(M’). Then by
Lemma 2 there exists an DFA M such that L = L(M).
(only if) Suppose there exists an DFA M such that L = L(M). Then by
Lemma 1 there exists an NFA M’ such that L = L(M’).
44
• Note: Suppose R = {}
= {} Since R = {}
{q2} {q2}
q2
45
NFAs with ε Moves
• An NFA-ε is a five-tuple:
M = (Q, Σ, δ, q0, F)
δ: (Q x (Σ U {ε})) –> 2Q
δ(q,s) -The set of all states p such that there is a
transition labeled a from q to p, where a
is in Σ U {ε}
• Sometimes referred to as an NFA-ε other times, simply as an NFA. 46
• Example: q3
1
0 0 0/1
ε ε
q0 q1 q2
1 0
δ: 0 1 ε
q0 {q0} {} {q1} - A string w = w1w2…wn is processed
as w = ε*w1ε*w2ε* … ε*wnε*
q1 {q1, q2} {q0, q3} {q2} - Example: all computations on 00:
0 ε 0
{q2} {q2} {}
q2 q 0 q 0 q1 q2
{} {} {} :
q3
47
Informal Definitions
• The language accepted by M is the set of all strings from Σ * that are accepted
by M.
48
ε-closure
• Define ε-closure(q) to denote the set of all states reachable from q by zero or
more ε transitions.
ε-closure(P) = ε-closure(q)
q∈P
• Examples:
q3
1
0 0 0/1
ε ε
q0 q1 q2
1 0
50
• Step #1:
• Note that:
= δ(p, a)
Given δ: (2Q x (Σ U {ε})) –> 2Q define δ^: (2Q x Σ*) –> 2Q as follows:
Formally:
δ({q0}, 0) = {q0}
55
Definitions for NFA-ε Machines
56
Equivalence of NFAs and NFA-εs
• Do NFAs and NFA-ε machines accept the same class of languages?
– Is there a language L that is accepted by a NFA, but not by any NFA -ε?
– Is there a language L that is accepted by an NFA-ε, but not by any DFA?
57
• Lemma 1: Let M be an NFA. Then there exists a NFA-ε M’ such
that L(M) = L(M’).
58
• Lemma 2: Let M be an NFA-ε. Then there exists a NFA M’ such
that L(M) = L(M’).
• Proof: (sketch)
• Notes:
– δ’: (Q x Σ) –> 2Q is a function
– M’ has the same state set, the same alphabet, and the same start state as M
– M’ has no ε transitions 59
• Example:
q3
1
0 0 0/1
ε ε
q0 q1 q2
1 0
• Step #1:
– Same state set as M
– q0 is the starting state q3
q0 q1 q2
60
• Example:
q3
1
0 0 0/1
ε ε
q0 q1 q2
1 0
• Step #2:
– q0 becomes a final state q3
q0 q1 q2
61
• Example:
q3
1
0 0 0/1
ε ε
q0 q1 q2
1 0
• Step #3:
q3
0
0
q0 q1 q2
62
• Example:
q3
1
0 0 0/1
ε ε
q0 q1 q2
1 0
• Step #4:
q3
1
0/1
0/1
q0 q1 q2
0/1
63
• Example:
q3
1
0 0 0/1
ε ε
q0 q1 q2
1 0
• Step #5:
q3
1
0/1
0
0/1 0
q0 q1 q2
0/1
64
• Example:
q3
1
0 0 0/1
ε ε
q0 q1 q2
1 0
• Step #6:
q3
1
0/1 1
0/1
0/1 0/1
q0 q1 q2
1
0/1
65
• Example:
q3
1
0 0 0/1
ε ε
q0 q1 q2
1 0
• Step #7:
q3
1
0/1 1 0
0/1
0/1 0/1
q0 q1 q2
1
0/1
66
• Example:
q3
1
0 0 0/1
ε ε
q0 q1 q2
1 0
• Step #8:
– Done! q3
1
0/1 1 0/1
0/1
0/1 0/1
q0 q1 q2
1
0/1
67
• Theorem: Let L be a language. Then there exists an NFA M such
that L= L(M) iff there exists an NFA-ε M’ such that L = L(M’).
• Proof:
(if) Suppose there exists an NFA-ε M’ such that L = L(M’). Then by
Lemma 2 there exists an NFA M such that L = L(M).
(only if) Suppose there exists an NFA M such that L = L(M). Then by
Lemma 1 there exists an NFA-ε M’ such that L = L(M’).
68