nondeterministic finite automata
TRANSCRIPT
![Page 1: Nondeterministic Finite Automata](https://reader030.vdocument.in/reader030/viewer/2022012811/61c2505d58994204f94fcf25/html5/thumbnails/1.jpg)
Chapter 1, Part 2
Nondeterministic Finite Automata
CSC527, Chapter 1, Part 2 c© 2012 Mitsunori Ogihara 1
![Page 2: Nondeterministic Finite Automata](https://reader030.vdocument.in/reader030/viewer/2022012811/61c2505d58994204f94fcf25/html5/thumbnails/2.jpg)
Fundamental set operations
Let A and B be two languages. In addition to union andintersection, we consider the following set operations:
• Concatenation of A and B, A ◦ B, is {xy | x ∈ A andy ∈ B},
• Star of A, A∗, is {x1x2 · · ·xk | k ≥ 0 and x1, . . . , xk ∈ A}.
• Complement of A, A, is Σ∗−A, i.e., {w | w ∈ Σ∗∧w 6∈ A}.
CSC527, Chapter 1, Part 2 c© 2012 Mitsunori Ogihara 2
![Page 3: Nondeterministic Finite Automata](https://reader030.vdocument.in/reader030/viewer/2022012811/61c2505d58994204f94fcf25/html5/thumbnails/3.jpg)
Fundamental set operations
Let A and B be two languages. In addition to union andintersection, we consider the following set operations:
• Concatenation of A and B, A ◦ B, is {xy | x ∈ A andy ∈ B},
• Star of A, A∗, is {x1x2 · · ·xk | k ≥ 0 and x1, . . . , xk ∈ A}.
• Complement of A, A, is Σ∗−A, i.e., {w | w ∈ Σ∗∧w 6∈ A}.
We will prove that the regular languages are closed under
union, intersection, concatenation, star, and complement;,
i.e., that if A and B are regular, then A ∪ B, A ∩ B, A ◦ B, A∗,
and A are each regular.
CSC527, Chapter 1, Part 2 c© 2012 Mitsunori Ogihara 3
![Page 4: Nondeterministic Finite Automata](https://reader030.vdocument.in/reader030/viewer/2022012811/61c2505d58994204f94fcf25/html5/thumbnails/4.jpg)
Nondeterministic Finite Automata
The nondeterministic finite automaton is a variant of finiteautomaton with two characteristics:
• ǫ-transition: state transition can be made without readinga symbol;
• nondeterminism: zero or more than one possible valuemay exist for state transition.
CSC527, Chapter 1, Part 2 c© 2012 Mitsunori Ogihara 4
![Page 5: Nondeterministic Finite Automata](https://reader030.vdocument.in/reader030/viewer/2022012811/61c2505d58994204f94fcf25/html5/thumbnails/5.jpg)
An Example Nondeterministic Finite Automaton
An NFA that accepts all strings over {0, 1} that contain a 1 either
at the third position from the end or at the second position from
the end.
0,1
1 0,1,ε 0,1q1
q2
q4
q3
• There are two edges labeled 1 coming out of q1.
• There are no edges coming out of q4.
• The edge from q2 is labeled with ǫ, in addition to 0 and 1.
CSC527, Chapter 1, Part 2 c© 2012 Mitsunori Ogihara 5
![Page 6: Nondeterministic Finite Automata](https://reader030.vdocument.in/reader030/viewer/2022012811/61c2505d58994204f94fcf25/html5/thumbnails/6.jpg)
What the Diagram Says
• If the node you are in has an outgoing edge labeled ǫ, youmay choose to follow it.
• After receiving a symbol,• if the node you are in has multiple outgoing edges labeledwith the symbol, you nondeterministically choose one ofthem and cross it;
• if there are no choices, stop there.
0,1
1 0,1,ε 0,1q1
q2
q4
q3
CSC527, Chapter 1, Part 2 c© 2012 Mitsunori Ogihara 6
![Page 7: Nondeterministic Finite Automata](https://reader030.vdocument.in/reader030/viewer/2022012811/61c2505d58994204f94fcf25/html5/thumbnails/7.jpg)
Nondeterministic Finite Automata, Formally
A nondeterministic finite automaton is a 5-tuple N =
(Q,Σ, δ, q0, F ), where δ is a mapping of Q×Σǫ to 2Q (alternatively,
written P(Q)), and Σǫ = Σ ∪ {ǫ}.
For each p ∈ Q and each a ∈ Σǫ, δ(s, a) = R means:
“upon reading an a the automaton N may transition from
state s to any state in R.”
For ǫ in particular, δ(s, ǫ) = R means:
“without reading the symbol the automaton N may
transition from state s to any state in R.”
CSC527, Chapter 1, Part 2 c© 2012 Mitsunori Ogihara 7
![Page 8: Nondeterministic Finite Automata](https://reader030.vdocument.in/reader030/viewer/2022012811/61c2505d58994204f94fcf25/html5/thumbnails/8.jpg)
Acceptance by Nondeterministic Finite Automata
N accepts a word w = w1 · · ·wn, w1, . . . , wn ∈ Σ if: there exist(p0, . . . , pm), p0, . . . , pm ∈ Q and y1, . . . , ym ∈ Σǫ such that
• w = y1 · · · ym,
• p0 = q0,
• pm ∈ F , and
• for every i, 1 ≤ i ≤ m, pi ∈ δ(pi−1, wi).
CSC527, Chapter 1, Part 2 c© 2012 Mitsunori Ogihara 8
![Page 9: Nondeterministic Finite Automata](https://reader030.vdocument.in/reader030/viewer/2022012811/61c2505d58994204f94fcf25/html5/thumbnails/9.jpg)
Transition Function for the Previous Example
0,1
1 0,1,ε 0,1q1
q2
q4
q3
state symbol
0 1 ǫ
q1 {q1} {q1, q2} ∅
q2 {q3} {q3} { q3 }
q3 {q4} {q4} ∅
q4 ∅ ∅ ∅
CSC527, Chapter 1, Part 2 c© 2012 Mitsunori Ogihara 9
![Page 10: Nondeterministic Finite Automata](https://reader030.vdocument.in/reader030/viewer/2022012811/61c2505d58994204f94fcf25/html5/thumbnails/10.jpg)
Transition Function for the Previous Example
0,1
1 0,1,ε 0,1q1
q2
q4
q3
state symbol
0 1 ǫ
q1 {q1} {q1, q2} ∅
q2 {q3} {q3} { q3 }
q3 {q4} {q4} ∅
q4 ∅ ∅ ∅
This NFA accepts y = 11 with respect to state sequence
(q1, q2, q3, q4) and decomposition y = 1ǫ1.
CSC527, Chapter 1, Part 2 c© 2012 Mitsunori Ogihara 10
![Page 11: Nondeterministic Finite Automata](https://reader030.vdocument.in/reader030/viewer/2022012811/61c2505d58994204f94fcf25/html5/thumbnails/11.jpg)
Nondeterministic Choices
Nondeterministic computation can be thought of as a self-
reproducing agent traveling in the state space.
1. At the start of computation the agent is in the initial state.
CSC527, Chapter 1, Part 2 c© 2012 Mitsunori Ogihara 11
![Page 12: Nondeterministic Finite Automata](https://reader030.vdocument.in/reader030/viewer/2022012811/61c2505d58994204f94fcf25/html5/thumbnails/12.jpg)
Nondeterministic Choices
Nondeterministic computation can be thought of as a self-
reproducing agent traveling in the state space.
1. At the start of computation the agent is in the initial state.
CSC527, Chapter 1, Part 2 c© 2012 Mitsunori Ogihara 12
![Page 13: Nondeterministic Finite Automata](https://reader030.vdocument.in/reader030/viewer/2022012811/61c2505d58994204f94fcf25/html5/thumbnails/13.jpg)
Nondeterministic Choices
2. Both before and after receiving an input symbol, the agentfollows each ǫ-labeled outgoing edge by producing its ownclone and sending it along the edge. Thus, the originalremains in the current location.
CSC527, Chapter 1, Part 2 c© 2012 Mitsunori Ogihara 13
![Page 14: Nondeterministic Finite Automata](https://reader030.vdocument.in/reader030/viewer/2022012811/61c2505d58994204f94fcf25/html5/thumbnails/14.jpg)
Nondeterministic Choices
2. Both before and after receiving an input symbol, the agentfollows each ǫ-labeled outgoing edge by producing its ownclone and sending it along the edge. Thus, the originalremains in the current location.
I'll go to p2.
I'll stay
here.
I'll go to p5.
I'll go to p7.
CSC527, Chapter 1, Part 2 c© 2012 Mitsunori Ogihara 14
![Page 15: Nondeterministic Finite Automata](https://reader030.vdocument.in/reader030/viewer/2022012811/61c2505d58994204f94fcf25/html5/thumbnails/15.jpg)
Nondeterministic Choices
3. On receiving an input symbol, say a,(a) If there is only one a-labeled outgoing edge, the agent
follows the edge.
(b) If there is no a-labeled outgoing edge, the agentevaporates.
CSC527, Chapter 1, Part 2 c© 2012 Mitsunori Ogihara 15
![Page 16: Nondeterministic Finite Automata](https://reader030.vdocument.in/reader030/viewer/2022012811/61c2505d58994204f94fcf25/html5/thumbnails/16.jpg)
Nondeterministic Choices
3. On receiving an input symbol, say a,(a) If there is only one a-labeled outgoing edge, the agent
follows the edge.
(b) If there is no a-labeled outgoing edge, the agentevaporates.
I am
gone.
CSC527, Chapter 1, Part 2 c© 2012 Mitsunori Ogihara 16
![Page 17: Nondeterministic Finite Automata](https://reader030.vdocument.in/reader030/viewer/2022012811/61c2505d58994204f94fcf25/html5/thumbnails/17.jpg)
Nondeterministic Choices
3. On receiving an input symbol, say a,(c) If there are k ≥ 2 a-labeled outgoing edges, the agent
produces k − 1 clones, make them cross k − 1 of theedges, and cross the remaining one by himself.
CSC527, Chapter 1, Part 2 c© 2012 Mitsunori Ogihara 17
![Page 18: Nondeterministic Finite Automata](https://reader030.vdocument.in/reader030/viewer/2022012811/61c2505d58994204f94fcf25/html5/thumbnails/18.jpg)
Nondeterministic Choices
3. On receiving an input symbol, say a,(c) If there are k ≥ 2 a-labeled outgoing edges, the agent
produces k − 1 clones, make them cross k − 1 of theedges, and cross the remaining one by himself.
I'll go to p2.
I'll go to
p3.
I'll go to p5.
I'll go to p7.
CSC527, Chapter 1, Part 2 c© 2012 Mitsunori Ogihara 18
![Page 19: Nondeterministic Finite Automata](https://reader030.vdocument.in/reader030/viewer/2022012811/61c2505d58994204f94fcf25/html5/thumbnails/19.jpg)
Nondeterministic Choices
4. When two agents collide in a state, they merge themselvesinto one.
CSC527, Chapter 1, Part 2 c© 2012 Mitsunori Ogihara 19
![Page 20: Nondeterministic Finite Automata](https://reader030.vdocument.in/reader030/viewer/2022012811/61c2505d58994204f94fcf25/html5/thumbnails/20.jpg)
Nondeterministic Choices
4. When two agents collide in a state, they merge themselvesinto one.
CSC527, Chapter 1, Part 2 c© 2012 Mitsunori Ogihara 20
![Page 21: Nondeterministic Finite Automata](https://reader030.vdocument.in/reader030/viewer/2022012811/61c2505d58994204f94fcf25/html5/thumbnails/21.jpg)
Why Use NFA?
For some languages construction is much easier.
Below is a DFA that accepts the same language by remembering
the last three symbols.
0
0
0
0q000
q001
q101
q110
q100
q010
q011
q111
0
1
1
0
01
10
1
1 1
1
CSC527, Chapter 1, Part 2 c© 2012 Mitsunori Ogihara 21
![Page 22: Nondeterministic Finite Automata](https://reader030.vdocument.in/reader030/viewer/2022012811/61c2505d58994204f94fcf25/html5/thumbnails/22.jpg)
Comparison
0
0
0
0q000
q001
q101
q110
q100
q010
q011
q111
0
1
1
0
01
10
1
1 1
1
DFA
0,1
1 0,1,ε 0,1q1
q2
q4
q3
NFA
CSC527, Chapter 1, Part 2 c© 2012 Mitsunori Ogihara 22
![Page 23: Nondeterministic Finite Automata](https://reader030.vdocument.in/reader030/viewer/2022012811/61c2505d58994204f94fcf25/html5/thumbnails/23.jpg)
Example 1
An NFA for the language of all strings over {a, b, c} that end with
one of ab, bc, and ca.
CSC527, Chapter 1, Part 2 c© 2012 Mitsunori Ogihara 23
![Page 24: Nondeterministic Finite Automata](https://reader030.vdocument.in/reader030/viewer/2022012811/61c2505d58994204f94fcf25/html5/thumbnails/24.jpg)
Example 1
An NFA for the language of all strings over {a, b, c} that end with
one of ab, bc, and ca.
q0
p1 p2
p3 p4
p5 p6
c
b
a
b
a
c
a,b,c
CSC527, Chapter 1, Part 2 c© 2012 Mitsunori Ogihara 24
![Page 25: Nondeterministic Finite Automata](https://reader030.vdocument.in/reader030/viewer/2022012811/61c2505d58994204f94fcf25/html5/thumbnails/25.jpg)
A DFA Version of the Same Language
q0
p1 p2
p3 p4
p5 p6
c
b
a
b
a
c
a
c
b
a
c
b
a
c
b
b
a
c
b
a
c
CSC527, Chapter 1, Part 2 c© 2012 Mitsunori Ogihara 25
![Page 26: Nondeterministic Finite Automata](https://reader030.vdocument.in/reader030/viewer/2022012811/61c2505d58994204f94fcf25/html5/thumbnails/26.jpg)
Example 2
An NFA for the language of all strings over {0, 1} that end with
one of 0110, 010, and 00.
CSC527, Chapter 1, Part 2 c© 2012 Mitsunori Ogihara 26
![Page 27: Nondeterministic Finite Automata](https://reader030.vdocument.in/reader030/viewer/2022012811/61c2505d58994204f94fcf25/html5/thumbnails/27.jpg)
Example 2
An NFA for the language of all strings over {0, 1} that end with
one of 0110, 010, and 00.
0,1
1,e 1,e 0q1
q2
q4
q3
q0
0
CSC527, Chapter 1, Part 2 c© 2012 Mitsunori Ogihara 27
![Page 28: Nondeterministic Finite Automata](https://reader030.vdocument.in/reader030/viewer/2022012811/61c2505d58994204f94fcf25/html5/thumbnails/28.jpg)
Example 3
An NFA for the language of all strings over {a, b, c} for which one
of (the number of occurrences of a), (the number of occurrences
of b), and (the number of occurrences of c) is a multiple of 3.
CSC527, Chapter 1, Part 2 c© 2012 Mitsunori Ogihara 28
![Page 29: Nondeterministic Finite Automata](https://reader030.vdocument.in/reader030/viewer/2022012811/61c2505d58994204f94fcf25/html5/thumbnails/29.jpg)
Example 3
An NFA for the language of all strings over {a, b, c} for which one
of (the number of occurrences of a), (the number of occurrences
of b), and (the number of occurrences of c) is a multiple of 3.
a a
b,c b,c
b,c
b b
c,a c,a
c,a
c c
a,b a,b
a,b
a
b
c
CSC527, Chapter 1, Part 2 c© 2012 Mitsunori Ogihara 29
![Page 30: Nondeterministic Finite Automata](https://reader030.vdocument.in/reader030/viewer/2022012811/61c2505d58994204f94fcf25/html5/thumbnails/30.jpg)
Example 4
An NFA for the language of all strings over {a, b} that contain
ababb.
CSC527, Chapter 1, Part 2 c© 2012 Mitsunori Ogihara 30
![Page 31: Nondeterministic Finite Automata](https://reader030.vdocument.in/reader030/viewer/2022012811/61c2505d58994204f94fcf25/html5/thumbnails/31.jpg)
Example 4
An NFA for the language of all strings over {a, b} that contain
ababb.
a,b
aa
b
b
b
a,b
CSC527, Chapter 1, Part 2 c© 2012 Mitsunori Ogihara 31
![Page 32: Nondeterministic Finite Automata](https://reader030.vdocument.in/reader030/viewer/2022012811/61c2505d58994204f94fcf25/html5/thumbnails/32.jpg)
The FA Model Versus NFA Model
The NFA simplifies computational design, but the use of
nondeterministic selections and ǫ-transitions makes it look very
different from FA.
Is that really so?
CSC527, Chapter 1, Part 2 c© 2012 Mitsunori Ogihara 32
![Page 33: Nondeterministic Finite Automata](https://reader030.vdocument.in/reader030/viewer/2022012811/61c2505d58994204f94fcf25/html5/thumbnails/33.jpg)
The FA Model Versus NFA Model
The NFA simplifies computational design, but the use of
nondeterministic selections and ǫ-transitions makes it look very
different from FA.
Is that really so?
No, the FA model is equivalent to the NFA model. That is,
every language accepted by an DFA is accepted by an NFA, vice
versa.
Obviously, we have:
Theorem. Every FA is already an NFA.
We will show:
Theorem. Every NFA can be converted to an equivalent FA.
CSC527, Chapter 1, Part 2 c© 2012 Mitsunori Ogihara 33
![Page 34: Nondeterministic Finite Automata](https://reader030.vdocument.in/reader030/viewer/2022012811/61c2505d58994204f94fcf25/html5/thumbnails/34.jpg)
Proof
The principle is that just before the first symbol is received
and after each symbol is read, there will be at most one
agent in any state.
CSC527, Chapter 1, Part 2 c© 2012 Mitsunori Ogihara 34
![Page 35: Nondeterministic Finite Automata](https://reader030.vdocument.in/reader030/viewer/2022012811/61c2505d58994204f94fcf25/html5/thumbnails/35.jpg)
Proof
The principle is that just before the first symbol is received
and after each symbol is read, there will be at most one
agent in any state.
We will thus consider the “set of all states an agent can be
in.”
CSC527, Chapter 1, Part 2 c© 2012 Mitsunori Ogihara 35
![Page 36: Nondeterministic Finite Automata](https://reader030.vdocument.in/reader030/viewer/2022012811/61c2505d58994204f94fcf25/html5/thumbnails/36.jpg)
Proof (cont’d)
Let N = (Q,Σ, δ, q0, F ) be an NFA.
We will construct a DFA M = (S,Σ, γ, s0, G).
• The state set S is P(Q).
• The initial state s0 is the set consisting of q0 and all the statesreachable from q0 by following only ǫ transitions.
• The final state set G is {A ∈ S | A ∩ F 6= ∅}; i.e., the set ofall subsets of Q containing an element of F .
CSC527, Chapter 1, Part 2 c© 2012 Mitsunori Ogihara 36
![Page 37: Nondeterministic Finite Automata](https://reader030.vdocument.in/reader030/viewer/2022012811/61c2505d58994204f94fcf25/html5/thumbnails/37.jpg)
Transition Function
The transition function γ is defined as follows:
For each A ∈ S and for each b ∈ Σ, γ(A, b) =⋃
p∈A δ(p, ǫ∗bǫ∗),the collection of all states r that can be reached from a state p inA by following
• any number of ǫ-arrows,
• a b-arrow, and then
• any number of ǫ-arrows.
For all w over Σ, w is accepted by N if and only if the new DFA
transitions from s0 to a state in G on input w.
CSC527, Chapter 1, Part 2 c© 2012 Mitsunori Ogihara 37
![Page 38: Nondeterministic Finite Automata](https://reader030.vdocument.in/reader030/viewer/2022012811/61c2505d58994204f94fcf25/html5/thumbnails/38.jpg)
Example
An NFA that recognizes the language consisting of all strings over
{0, 1} that contain a 1 at either the third to last position or the
second to last position.
0,1
1 0,1,ε 0,1q1
q2
q4
q3
CSC527, Chapter 1, Part 2 c© 2012 Mitsunori Ogihara 38
![Page 39: Nondeterministic Finite Automata](https://reader030.vdocument.in/reader030/viewer/2022012811/61c2505d58994204f94fcf25/html5/thumbnails/39.jpg)
Conversion to DFA
The state set consists of: ∅, {q1}, {q2}, {q3}, {q4},
{q1, q2}, {q1, q3}, {q1, q4}, {q2, q3}, {q2, q4}, {q3, q4},
{q1, q2, q3}, {q1, q2, q4}, {q1, q3, q4}, {q2, q3, q4},
{q1, q2, q3, q4}.
F consists of: {q4}, {q1, q4}, {q2, q4}, {q3, q4},
{q1, q2, q4}, {q1, q3, q4}, {q2, q3, q4}, {q1, q2, q3, q4}.
The initial state is {q1}.
CSC527, Chapter 1, Part 2 c© 2012 Mitsunori Ogihara 39
![Page 40: Nondeterministic Finite Automata](https://reader030.vdocument.in/reader030/viewer/2022012811/61c2505d58994204f94fcf25/html5/thumbnails/40.jpg)
Transition
State 0 1
{q1} {q1} {q1, q2, q3}
{q1, q2, q3} {q1, q3, q4} {q1, q2, q3, q4}
{q1, q3, q4} {q1, q4} {q1, q2, q3, q4}
{q1, q4} {q1} {q1, q2, q3}
{q1, q2, q3, q4} {q1, q3, q4} {q1, q2, q3, q4}
The other states are unreachable from the initial state.
CSC527, Chapter 1, Part 2 c© 2012 Mitsunori Ogihara 40
![Page 41: Nondeterministic Finite Automata](https://reader030.vdocument.in/reader030/viewer/2022012811/61c2505d58994204f94fcf25/html5/thumbnails/41.jpg)
0
123
1234
14
134101
1
0
1
0
1
0
1
CSC527, Chapter 1, Part 2 c© 2012 Mitsunori Ogihara 41
![Page 42: Nondeterministic Finite Automata](https://reader030.vdocument.in/reader030/viewer/2022012811/61c2505d58994204f94fcf25/html5/thumbnails/42.jpg)
A Greedy Conversion Algorithm
Step 1 For each state p ∈ Q and for each symbol a ∈ Σ, computethe set R(p, a) of all states that can be reached from state p by:(a) any number of ǫ transitions,
(b) one transition labeled by a, and then
(c) any number of ǫ transitions.
Step 2 Initialize the state collection S as {p0}, where p0 is theset of all states that can be reached from q0 by following anynumber of ǫ transitions.
Step 3 While S contains a state with no outgoing edges, selectan arbitrary member, say r, of S, and do the following:
• For each symbol a, compute the state ra as ∪q∈rR(q, a),add ra to S if ra is not already in it, and then draw an arcfrom r to ra.
CSC527, Chapter 1, Part 2 c© 2012 Mitsunori Ogihara 42
![Page 43: Nondeterministic Finite Automata](https://reader030.vdocument.in/reader030/viewer/2022012811/61c2505d58994204f94fcf25/html5/thumbnails/43.jpg)
Algorithm Execution Example
Use the previous NFA.
0,1
1 0,1,ε 0,1q1
q2
q4
q3
CSC527, Chapter 1, Part 2 c© 2012 Mitsunori Ogihara 43
![Page 44: Nondeterministic Finite Automata](https://reader030.vdocument.in/reader030/viewer/2022012811/61c2505d58994204f94fcf25/html5/thumbnails/44.jpg)
Step 1
state 0 1
q1 q1 q1, q2, q3
q2 q3, q4 q3, q4
q3 q4 q4
q4 ∅ ∅
Step 2
Initially S = {{q1}}.
CSC527, Chapter 1, Part 2 c© 2012 Mitsunori Ogihara 44
![Page 45: Nondeterministic Finite Automata](https://reader030.vdocument.in/reader030/viewer/2022012811/61c2505d58994204f94fcf25/html5/thumbnails/45.jpg)
Step 3
{q1} on 0 changes the state to {q1}
{q1} on 1 changes the state to {q1, q2, q3}. New!
{q1, q2, q3} on 0 changes the state to {q1, q3, q4}. New!
{q1, q2, q3} on 1 changes the state to {q1, q2, q3, q4}. New!
{q1, q3, q4} on 0 changes the state to {q1, q4}. New!
{q1, q2, q3} on 1 changes the state to {q1, q2, q3, q4}.
{q1, q2, q3, q4} on 0 changes the state to {q1, q3, q4}.
{q1, q2, q3, q4} on 1 changes the state to {q1, q2, q3, q4}.
{q1, q4} on 0 changes the state to {q13}.
{q1, q4} on 1 changes the state to {q1, q2, q3}.
CSC527, Chapter 1, Part 2 c© 2012 Mitsunori Ogihara 45