Formal Definition of a Nondeterministic FiniteAutomaton
September 7, 2014
Formal Definition of a Nondeterministic Finite Automaton
A comment first
I The formal definition of an NFA is similar to that of a DFA.Both have states, an alphabet, transition function, one startstate, and a collection of accept states
I They differ in one essential way: the transition functions
I In a DFA transition function is δ : Q × Σ→ Q;In an NFA it is δ : Q × (Σ ∪ {ε})→ P(Q)where P(Q) is the collection of subsets of Q called the powerset of Q
Formal Definition of a Nondeterministic Finite Automaton
A comment first
I The formal definition of an NFA is similar to that of a DFA.Both have states, an alphabet, transition function, one startstate, and a collection of accept states
I They differ in one essential way: the transition functions
I In a DFA transition function is δ : Q × Σ→ Q;In an NFA it is δ : Q × (Σ ∪ {ε})→ P(Q)where P(Q) is the collection of subsets of Q called the powerset of Q
Formal Definition of a Nondeterministic Finite Automaton
A comment first
I The formal definition of an NFA is similar to that of a DFA.Both have states, an alphabet, transition function, one startstate, and a collection of accept states
I They differ in one essential way: the transition functions
I In a DFA transition function is δ : Q × Σ→ Q;In an NFA it is δ : Q × (Σ ∪ {ε})→ P(Q)where P(Q) is the collection of subsets of Q called the powerset of Q
Formal Definition of a Nondeterministic Finite Automaton
Notation
For any alphabet Σ, Σε = Σ ∪ {ε}
Formal Definition of a Nondeterministic Finite Automaton
NFA formal definition (Def. 1.37)
A nondeterministic finite automaton is a 5-tuple(Q,Σ, δ, q0,F ) where
1. Q is a finite set of states
2. Σ is a finite alphabet
3. δ : Q × Σε → P(Q) is the transition function
4. q0 ∈ Q is the start state
5. F ⊆ Q is the set of accept states
Formal Definition of a Nondeterministic Finite Automaton
Example NFA (Example 1.38)
Recall the NFA N1, Figure 1:
-����
q1
?
0,1
-1 ����
q2 -ε,0 ����
q3 -1 ��������
q4
?
0,1
Figure : Nondeterministic Finite Automaton N1
N1 = (Q,Σ, δ, q1,F ) where:1. Q = {q1, q2, q3, q4}2. Σ = {0, 1}3. δ is given in the table:
δ 0 1 εq1 {q1} {q1, q2} ∅q2 {q3} ∅ {q3}q3 ∅ {q4} ∅q4 {q4} {q4} ∅
4. q1 is the start set; 5. F = {q4}
Formal Definition of a Nondeterministic Finite Automaton
Example NFA (Example 1.38)
Recall the NFA N1, Figure 1:
-����
q1
?
0,1
-1 ����
q2 -ε,0 ����
q3 -1 ��������
q4
?
0,1
Figure : Nondeterministic Finite Automaton N1
N1 = (Q,Σ, δ, q1,F ) where:
1. Q = {q1, q2, q3, q4}2. Σ = {0, 1}3. δ is given in the table:
δ 0 1 εq1 {q1} {q1, q2} ∅q2 {q3} ∅ {q3}q3 ∅ {q4} ∅q4 {q4} {q4} ∅
4. q1 is the start set; 5. F = {q4}
Formal Definition of a Nondeterministic Finite Automaton
Example NFA (Example 1.38)
Recall the NFA N1, Figure 1:
-����
q1
?
0,1
-1 ����
q2 -ε,0 ����
q3 -1 ��������
q4
?
0,1
Figure : Nondeterministic Finite Automaton N1
N1 = (Q,Σ, δ, q1,F ) where:1. Q = {q1, q2, q3, q4}
2. Σ = {0, 1}3. δ is given in the table:
δ 0 1 εq1 {q1} {q1, q2} ∅q2 {q3} ∅ {q3}q3 ∅ {q4} ∅q4 {q4} {q4} ∅
4. q1 is the start set; 5. F = {q4}
Formal Definition of a Nondeterministic Finite Automaton
Example NFA (Example 1.38)
Recall the NFA N1, Figure 1:
-����
q1
?
0,1
-1 ����
q2 -ε,0 ����
q3 -1 ��������
q4
?
0,1
Figure : Nondeterministic Finite Automaton N1
N1 = (Q,Σ, δ, q1,F ) where:1. Q = {q1, q2, q3, q4}2. Σ = {0, 1}
3. δ is given in the table:δ 0 1 εq1 {q1} {q1, q2} ∅q2 {q3} ∅ {q3}q3 ∅ {q4} ∅q4 {q4} {q4} ∅
4. q1 is the start set; 5. F = {q4}
Formal Definition of a Nondeterministic Finite Automaton
Example NFA (Example 1.38)
Recall the NFA N1, Figure 1:
-����
q1
?
0,1
-1 ����
q2 -ε,0 ����
q3 -1 ��������
q4
?
0,1
Figure : Nondeterministic Finite Automaton N1
N1 = (Q,Σ, δ, q1,F ) where:1. Q = {q1, q2, q3, q4}2. Σ = {0, 1}3. δ is given in the table:
δ 0 1 εq1 {q1} {q1, q2} ∅q2 {q3} ∅ {q3}q3 ∅ {q4} ∅q4 {q4} {q4} ∅
4. q1 is the start set; 5. F = {q4}
Formal Definition of a Nondeterministic Finite Automaton
Example NFA (Example 1.38)
Recall the NFA N1, Figure 1:
-����
q1
?
0,1
-1 ����
q2 -ε,0 ����
q3 -1 ��������
q4
?
0,1
Figure : Nondeterministic Finite Automaton N1
N1 = (Q,Σ, δ, q1,F ) where:1. Q = {q1, q2, q3, q4}2. Σ = {0, 1}3. δ is given in the table:
δ 0 1 εq1 {q1} {q1, q2} ∅q2 {q3} ∅ {q3}q3 ∅ {q4} ∅q4 {q4} {q4} ∅
4. q1 is the start set; 5. F = {q4}
Formal Definition of a Nondeterministic Finite Automaton
Computation performed by an NFA
Let N = (Q,Σ, δ, q0,F ) be an NFA and w a string over Σ. We saythat N accepts w if we can write w as w = w1w2 . . .wm, yi ∈ Σε,1 ≤ i ≤ m, and a sequence of states r0, r1, . . . , rm exists in Q suchthat:
1. r0 = q0
2. ri+1 ∈ δ(ri ,wi+1), for i = 0, 1, . . . ,m − 1
3. rm ∈ F
I Condition 1 says that the machine starts its computation inthe start state
I Condition 2 says that state ri+1 is one of the allowable newstates when N is in state ri and reads wi+1. Note thatδ(ri ,wi+1) is a set
I Condition 3 says that the machine accepts the input if the laststate is in the accept state set.
Formal Definition of a Nondeterministic Finite Automaton
Computation performed by an NFA
Let N = (Q,Σ, δ, q0,F ) be an NFA and w a string over Σ. We saythat N accepts w if we can write w as w = w1w2 . . .wm, yi ∈ Σε,1 ≤ i ≤ m, and a sequence of states r0, r1, . . . , rm exists in Q suchthat:
1. r0 = q0
2. ri+1 ∈ δ(ri ,wi+1), for i = 0, 1, . . . ,m − 1
3. rm ∈ F
I Condition 1 says that the machine starts its computation inthe start state
I Condition 2 says that state ri+1 is one of the allowable newstates when N is in state ri and reads wi+1. Note thatδ(ri ,wi+1) is a set
I Condition 3 says that the machine accepts the input if the laststate is in the accept state set.
Formal Definition of a Nondeterministic Finite Automaton
Computation performed by an NFA
Let N = (Q,Σ, δ, q0,F ) be an NFA and w a string over Σ. We saythat N accepts w if we can write w as w = w1w2 . . .wm, yi ∈ Σε,1 ≤ i ≤ m, and a sequence of states r0, r1, . . . , rm exists in Q suchthat:
1. r0 = q0
2. ri+1 ∈ δ(ri ,wi+1), for i = 0, 1, . . . ,m − 1
3. rm ∈ F
I Condition 1 says that the machine starts its computation inthe start state
I Condition 2 says that state ri+1 is one of the allowable newstates when N is in state ri and reads wi+1. Note thatδ(ri ,wi+1) is a set
I Condition 3 says that the machine accepts the input if the laststate is in the accept state set.
Formal Definition of a Nondeterministic Finite Automaton
Computation performed by an NFA
Let N = (Q,Σ, δ, q0,F ) be an NFA and w a string over Σ. We saythat N accepts w if we can write w as w = w1w2 . . .wm, yi ∈ Σε,1 ≤ i ≤ m, and a sequence of states r0, r1, . . . , rm exists in Q suchthat:
1. r0 = q0
2. ri+1 ∈ δ(ri ,wi+1), for i = 0, 1, . . . ,m − 1
3. rm ∈ F
I Condition 1 says that the machine starts its computation inthe start state
I Condition 2 says that state ri+1 is one of the allowable newstates when N is in state ri and reads wi+1. Note thatδ(ri ,wi+1) is a set
I Condition 3 says that the machine accepts the input if the laststate is in the accept state set.
Formal Definition of a Nondeterministic Finite Automaton
Equivalence of NFA and DFA
I DFAs and NFAs recognize the same class of languages
I This equivalence is both surprising and useful
1. It is surprising because NFAs appears to have more power thanDFA, so we might expect that NFA recognizes more languages
2. It is useful because describing an NFA for a given languagesometimes is much easier than describing a DFA
Formal Definition of a Nondeterministic Finite Automaton
Equivalence of NFA and DFA
I DFAs and NFAs recognize the same class of languagesI This equivalence is both surprising and useful
1. It is surprising because NFAs appears to have more power thanDFA, so we might expect that NFA recognizes more languages
2. It is useful because describing an NFA for a given languagesometimes is much easier than describing a DFA
Formal Definition of a Nondeterministic Finite Automaton
Equivalence of NFA and DFA
I DFAs and NFAs recognize the same class of languagesI This equivalence is both surprising and useful
1. It is surprising because NFAs appears to have more power thanDFA, so we might expect that NFA recognizes more languages
2. It is useful because describing an NFA for a given languagesometimes is much easier than describing a DFA
Formal Definition of a Nondeterministic Finite Automaton
Equivalence of NFA and DFA
I DFAs and NFAs recognize the same class of languagesI This equivalence is both surprising and useful
1. It is surprising because NFAs appears to have more power thanDFA, so we might expect that NFA recognizes more languages
2. It is useful because describing an NFA for a given languagesometimes is much easier than describing a DFA
Formal Definition of a Nondeterministic Finite Automaton
Theorem 1.39
Every nondeterministic finite automation (NFA) has an equivalentdeterministic finite automaton (DFA).Proof idea: Convert the NFA into an equivalemt DFA thatsimulates the NFA
I The approach is to pretend to be an DFA simulating an NFA.I How would you simulate an NFA if you were pretending to be
a DFA?I What does one need to keep track of as the input is processed?
I In the examples of NFA we kept track of various branches ofthe computation by placing tokens on each state that couldbe activated at given points in the input
I Tokens were updated by moving, adding, and removing themaccording to the way NFA operates
Hence, all we needed to keep track of is the set of states withtokens on them
Formal Definition of a Nondeterministic Finite Automaton
Theorem 1.39
Every nondeterministic finite automation (NFA) has an equivalentdeterministic finite automaton (DFA).Proof idea: Convert the NFA into an equivalemt DFA thatsimulates the NFA
I The approach is to pretend to be an DFA simulating an NFA.I How would you simulate an NFA if you were pretending to be
a DFA?I What does one need to keep track of as the input is processed?
I In the examples of NFA we kept track of various branches ofthe computation by placing tokens on each state that couldbe activated at given points in the input
I Tokens were updated by moving, adding, and removing themaccording to the way NFA operates
Hence, all we needed to keep track of is the set of states withtokens on them
Formal Definition of a Nondeterministic Finite Automaton
Theorem 1.39
Every nondeterministic finite automation (NFA) has an equivalentdeterministic finite automaton (DFA).Proof idea: Convert the NFA into an equivalemt DFA thatsimulates the NFA
I The approach is to pretend to be an DFA simulating an NFA.I How would you simulate an NFA if you were pretending to be
a DFA?I What does one need to keep track of as the input is processed?
I In the examples of NFA we kept track of various branches ofthe computation by placing tokens on each state that couldbe activated at given points in the input
I Tokens were updated by moving, adding, and removing themaccording to the way NFA operates
Hence, all we needed to keep track of is the set of states withtokens on them
Formal Definition of a Nondeterministic Finite Automaton
Theorem 1.39
Every nondeterministic finite automation (NFA) has an equivalentdeterministic finite automaton (DFA).Proof idea: Convert the NFA into an equivalemt DFA thatsimulates the NFA
I The approach is to pretend to be an DFA simulating an NFA.I How would you simulate an NFA if you were pretending to be
a DFA?I What does one need to keep track of as the input is processed?
I In the examples of NFA we kept track of various branches ofthe computation by placing tokens on each state that couldbe activated at given points in the input
I Tokens were updated by moving, adding, and removing themaccording to the way NFA operates
Hence, all we needed to keep track of is the set of states withtokens on them
Formal Definition of a Nondeterministic Finite Automaton
Theorem 1.39
Every nondeterministic finite automation (NFA) has an equivalentdeterministic finite automaton (DFA).Proof idea: Convert the NFA into an equivalemt DFA thatsimulates the NFA
I The approach is to pretend to be an DFA simulating an NFA.I How would you simulate an NFA if you were pretending to be
a DFA?I What does one need to keep track of as the input is processed?
I In the examples of NFA we kept track of various branches ofthe computation by placing tokens on each state that couldbe activated at given points in the input
I Tokens were updated by moving, adding, and removing themaccording to the way NFA operates
Hence, all we needed to keep track of is the set of states withtokens on them
Formal Definition of a Nondeterministic Finite Automaton
Conclusion
I An NFA with k states has 2k subsets of states
I Each subset correspond to one of the possibilities that DFAmust remember. So, the DFA simulating an NFA with kstates has 2k states
I The only thing we need now is to figure out which are thestart state, the accept states, and transition function
Formal Definition of a Nondeterministic Finite Automaton
The formal proof
Let N = (Q,Σ, δ, q0,F ) be the NFA recognizing the language A.We construct the DFA M recognizing A. Before doing the fullconstruction, consider first the easier case when N has no εtransitions.Constructing M = (Q′,Σ, δ′, q′0,F
′)
1. Q′ = P(Q); every state of M is a set of states of N
2. For R ∈ Q′ and a ∈ Σ let δ′(R, a) = {q ∈ Q|q ∈ δ(r , a) for some r ∈ R}Note: δ′(R, a) = ∪r∈Rδ(r , a)
3. q′0 = {q0}; M starts computing in the same state as N
4. F ′ = {R ∈ Q′|R contains an accept state of N}that is, the machine M accepts if one of the possible states that N could be inat this point is an accept state
Formal Definition of a Nondeterministic Finite Automaton
The formal proof
Let N = (Q,Σ, δ, q0,F ) be the NFA recognizing the language A.We construct the DFA M recognizing A. Before doing the fullconstruction, consider first the easier case when N has no εtransitions.Constructing M = (Q′,Σ, δ′, q′0,F
′)
1. Q′ = P(Q); every state of M is a set of states of N
2. For R ∈ Q′ and a ∈ Σ let δ′(R, a) = {q ∈ Q|q ∈ δ(r , a) for some r ∈ R}Note: δ′(R, a) = ∪r∈Rδ(r , a)
3. q′0 = {q0}; M starts computing in the same state as N
4. F ′ = {R ∈ Q′|R contains an accept state of N}that is, the machine M accepts if one of the possible states that N could be inat this point is an accept state
Formal Definition of a Nondeterministic Finite Automaton
The formal proof
Let N = (Q,Σ, δ, q0,F ) be the NFA recognizing the language A.We construct the DFA M recognizing A. Before doing the fullconstruction, consider first the easier case when N has no εtransitions.Constructing M = (Q′,Σ, δ′, q′0,F
′)
1. Q′ = P(Q); every state of M is a set of states of N
2. For R ∈ Q′ and a ∈ Σ let δ′(R, a) = {q ∈ Q|q ∈ δ(r , a) for some r ∈ R}Note: δ′(R, a) = ∪r∈Rδ(r , a)
3. q′0 = {q0}; M starts computing in the same state as N
4. F ′ = {R ∈ Q′|R contains an accept state of N}that is, the machine M accepts if one of the possible states that N could be inat this point is an accept state
Formal Definition of a Nondeterministic Finite Automaton
The formal proof
Let N = (Q,Σ, δ, q0,F ) be the NFA recognizing the language A.We construct the DFA M recognizing A. Before doing the fullconstruction, consider first the easier case when N has no εtransitions.Constructing M = (Q′,Σ, δ′, q′0,F
′)
1. Q′ = P(Q); every state of M is a set of states of N
2. For R ∈ Q′ and a ∈ Σ let δ′(R, a) = {q ∈ Q|q ∈ δ(r , a) for some r ∈ R}Note: δ′(R, a) = ∪r∈Rδ(r , a)
3. q′0 = {q0}; M starts computing in the same state as N
4. F ′ = {R ∈ Q′|R contains an accept state of N}that is, the machine M accepts if one of the possible states that N could be inat this point is an accept state
Formal Definition of a Nondeterministic Finite Automaton
The formal proof
Let N = (Q,Σ, δ, q0,F ) be the NFA recognizing the language A.We construct the DFA M recognizing A. Before doing the fullconstruction, consider first the easier case when N has no εtransitions.Constructing M = (Q′,Σ, δ′, q′0,F
′)
1. Q′ = P(Q); every state of M is a set of states of N
2. For R ∈ Q′ and a ∈ Σ let δ′(R, a) = {q ∈ Q|q ∈ δ(r , a) for some r ∈ R}Note: δ′(R, a) = ∪r∈Rδ(r , a)
3. q′0 = {q0}; M starts computing in the same state as N
4. F ′ = {R ∈ Q′|R contains an accept state of N}that is, the machine M accepts if one of the possible states that N could be inat this point is an accept state
Formal Definition of a Nondeterministic Finite Automaton
Consider the ε transitions
Notation: for any R ∈ Q ′ define E (R) to be the collection ofstates that can be reached from R by going only along εtransitions, including the members of R themselves. Formally:E(R) = R ∪ {q ∈ Q|∃r ∈ R ∧ δ(r , ε) = q}Note: any q ∈ E (R) can be reached from R by traveling along 0or more ε arrows.
Formal Definition of a Nondeterministic Finite Automaton
Modifications
I Modify δ′ to place additional tokens on all states that can bereached by going along ε arrows after every step. This effectis achieved by replacing δ(r , a) by E (δ(r , a)) in definition ofδ′. That isδ′(R, a) = {q ∈ Q|q ∈ E(δ(r , a)) for some r ∈ R}
I Modify the start state of M to put tokens initially on all statesthat can be reached from q0 along ε transitions, i.e.,q′0 = E ({q0})
Formal Definition of a Nondeterministic Finite Automaton
Modifications
I Modify δ′ to place additional tokens on all states that can bereached by going along ε arrows after every step. This effectis achieved by replacing δ(r , a) by E (δ(r , a)) in definition ofδ′. That isδ′(R, a) = {q ∈ Q|q ∈ E(δ(r , a)) for some r ∈ R}
I Modify the start state of M to put tokens initially on all statesthat can be reached from q0 along ε transitions, i.e.,q′0 = E ({q0})
Formal Definition of a Nondeterministic Finite Automaton
Note
I M thus constructed obviously simulates N
I At every step in the computation of M it clearly enters a statethat corresponds to the subset of states that N could be in atthat point
Formal Definition of a Nondeterministic Finite Automaton
Corollary 1.40
A language is regular iff some NFA recognizes it
Proof:
I If a language A is recognized by an NFA then A is recognizedby the DFA equivalent, hence, A is regular
I If a language A is regular, it means that it is recognized by aDFA. But any DFA is also an NFA hence, the language isrecognized by an NFA
Formal Definition of a Nondeterministic Finite Automaton
Example conversion NFA to DFA
Consider the NFA N4 described in Figure 2:
?
��������
1
��
��=
b
R
εI
a
����
2-
a
-a,b ����
3
Figure : The NFA N4
Formal description of N4
N4 = ({1, 2, 3}, {a, b}, δ, 1, {1}) where δ is given in the table:
δ a b ε1 ∅ {2} {3}2 {2, 3} {3} ∅3 {1} ∅ ∅
Formal Definition of a Nondeterministic Finite Automaton
Example conversion NFA to DFA
Consider the NFA N4 described in Figure 2:
?
��������
1
��
��=
b
R
εI
a
����
2-
a
-a,b ����
3
Figure : The NFA N4
Formal description of N4
N4 = ({1, 2, 3}, {a, b}, δ, 1, {1}) where δ is given in the table:
δ a b ε1 ∅ {2} {3}2 {2, 3} {3} ∅3 {1} ∅ ∅
Formal Definition of a Nondeterministic Finite Automaton
Construction of D4 = (Q′, {a, b}, δ′, q′0,F ′)
I Since N4’s set of states is Q = {1, 2, 3}Q′ = {∅, {1}, {2}, {3}, {1, 2}, {1, 3}, {2, 3}, {1, 2, 3}}
I The start state of Q ′: the set of states reachable from 1 (thestart state of N4) traveling by ε: E ({1}) = {1, 3}
I The accept state of Q ′: are those states of Q ′ that contain theaccept states of Q: i.e., F ′ = {{1}, {1, 2}, {1, 3}, {1, 2, 3}}
Formal Definition of a Nondeterministic Finite Automaton
Transition function
δ′ : P(Q)× Σ→ P(Q), where for each q′ ∈ P(Q) and x ∈ Σ wehave δ′(q′, x) ∈ P(Q)
Formal Definition of a Nondeterministic Finite Automaton
Constructing D4’s transitions
I State ∅: goes to ∅ on both a, b, i.e., δ′(∅, a) = δ′(∅, b) = ∅I State {1}: goes to ∅ on a and to {2} on b
I State {2}: goes to {2, 3} on a and to {3} on b
I State {1, 2}: goes to {2, 3} on a and to {2, 3} on b
I And so on, for each state
Formal Definition of a Nondeterministic Finite Automaton
Putting all together
Figure 3 shows the resulting DFA D4 equivalent to NFA N4
�� ��{3}
�� ��∅
6b
-
a,b
-a�� ��� �{1, 3}
�� ��� �{1}�a -b
?� a�����
b �� ��{2, 3}
�� ��{2}
?
a
�����
���
b
Ib
Ra �� ��� �{1, 2, 3}
�� ��� �{1, 2}
Ib
?a
��
��
a,b
Figure : DFA D4 equivalent to NFA N4
Formal Definition of a Nondeterministic Finite Automaton
Simplifying D4
The automaton D4 may be simplified by observing that no arrowspoint at the states {1} and {1, 2}. Removing these states weobtain the automaton in Figure 4
-
�� ��{2}
�� ��� �{1, 3}?a
?b
�����*
b
-a�� ��{2, 3}
�� ��{3}
6b
�a -b
Ra �� ��� �{1, 2, 3}
�� ��∅?a,b
?a
Ib
Figure : The simplified version of D4
Formal Definition of a Nondeterministic Finite Automaton