Download presentation
Presentation is loading. Please wait.
1
Principles of Computing – UFCFA3-30-1
Week-5 Instructor : Mazhar H Malik Global College of Engineering and Technology
2
NON-DETERMINISTIC FINITE AUTOMATA (NFA)
1
3
Kleene Closure/Plus
4
Kleene Closure
5
Kleene Star – Example 2
6
Kleene Plus
7
Kleene Plus…
8
NON-DETERMINISTIC FINITE AUTOMATA (NFA)
Automata that are non-deterministic (NFA) can be in several states at once from a state q on input a it can go to several different states this is "non-determinism" Deterministic Finite Automata (DFA) can go only to one state in such situations it also has one start state and several final states so when there are choices where to go, a NFA tries all 8
9
NFA Transition Function
Unlike in DFAs, δ(q,a) returns a set of states δ(q,a)={p1,...,pm} and it can return an empty set ∅ if there's nothing to go next 9
10
Definition: NON-DETERMINISTIC FINITE AUTOMATA (NFA)
10
11
NON-DETERMINISTIC FINITE AUTOMATA (NFA)
In a nondeterministic finite automaton (NFA), for each state there can be zero, one, two, or more transitions corresponding to a particular symbol. If NFA gets to state with more than one possible transition corresponding to the input symbol, we say it branches. If NFA gets to a state where there is no valid transition, then that branch dies. 11
12
11
13
Example: An NFA for a++(ab)+
Input : - 12
14
Example: An NFA for a++(ab)+
Input : a 13
15
Example: An NFA for a++(ab)+
Input : a 14
16
Example: An NFA for a++(ab)+
Input : a b 15
17
Example: An NFA for a++(ab)+
Input : a b (Accepted) 16
18
Example: An NFA for a++(ab)+
Input : a b a (Rejected) 17
19
Example: An NFA for a++(ab)+
Input : a a (Rejected) 18
20
19
21
Nondeterministic Finite Accepter (NFA)
Alphabet = {a} q1 q2 q0 q3
22
q1 q2 q0 q3 Nondeterministic Finite Accepter (NFA) Alphabet = {a}
Two choices q1 q2 q0 q3
23
q3 No transition q1 q2 q0 Nondeterministic Finite Accepter (NFA)
Alphabet = {a} Two choices q1 q2 No transition q0 q3 No transition
24
First Choice q1 q2 q0 q3
25
First Choice q1 q2 q0 q3 24
26
First Choice q1 q2 q0 q3 25
27
First Choice q1 q2 “accept” q0 q3
28
Second Choice q1 q2 q0 q3
29
Second Choice q1 q2 q0 q3
30
Second Choice q1 q2 q0 No transition: the automaton hangs q3
31
Second Choice q1 q2 q0 q3 “reject”
32
Observation An NFA accepts a string if
there is a computation of the NFA that accepts the string
33
aa is accepted by the NFA:
Example aa is accepted by the NFA: q1 q2 q0 q3
34
Design NFA by observing the Output Language Given in side window
33
35
34
36
Example: Moves on a Chessboard
States = squares. Inputs = r (move to an adjacent red square) and b (move to an adjacent black square). Start state, final state are in opposite corners. 1 2 3 4 5 6 7 8 9 35
37
Example: Chessboard – (2)
1 2,4 5 2 4,6 1,3,5 3 2,6 4 2,8 1,5,7 2,4,6,8 1,3,7,9 6 3,5,9 7 4,8 8 5,7,9 9 6,8 1 2 3 4 5 6 7 8 9 r b b 2 4 1 3 5 7 5 1 3 7 9 1 * Accept, since final state reached 36
38
Example 37
39
Language of an NFA A string w is accepted by an NFA if δ(q0, w)
contains at least one final state. The language of the NFA is the set of strings it accepts. 39
40
Example: Language of an NFA
1 2 3 4 5 6 7 8 9 Example: Language of an NFA For our chessboard NFA we saw that rbb is accepted. If the input consists of only b’s, the set of accessible states alternates between {5} and {1,3,7,9}, so only even-length, nonempty strings of b’s are accepted. What about strings with at least one r? 40
41
40
42
Lambda Transitions q0 q1 q1 q3
43
q0 q1 q2 q3
44
q0 q1 q2 q3
45
(read head doesn’t move)
q0 q1 q2 q3
46
q0 q1 q2 q3
47
“accept” q3 q0 q1 q2 String aa is accepted
48
Language accepted: L {aa}
q0 q1 q2 q3
49
Another NFA Example a b q1 q2 q3 q0
50
a b a b q1 q2 q3 q0
51
a b a b q1 q2 q3 q0
52
a b a b q1 q2 q3 q0
53
a b “accept” q2 a b q1 q3 q0
54
Another String a b a b q0 q1 q2 q3
55
a b a b q0 q1 q2 q3
56
a b a b q0 q1 q2 q3
57
a b a b q0 q1 q2 q3
58
a b a b q0 q1 q2 q3
59
a b a b q0 q1 q2 q3
60
a b a b q0 q1 q2 q3
61
a b “accept” q2 a b q0 q1 q3
62
Language accepted L ab, abab, ababab, ... ab a b q1 q2 q3 q0
63
Another NFA Example 0, 1 q1 q2 q0 1
64
Language accepted L , 10, 1010, , ... 10* 0, 1 q1 q2 q0 1
65
q0 : Initial state : Q : M Q, , , q0 , F : F :
Formal Definition of NFAs M Q, , , q0 , F Q : : : Set of states, i.e. q0 , q1, q2 Input aplhabet, i.e. a, b Transition function q0 : Initial state F : Final states
66
65
67
Transition Function q0 , 1 q1 0, 1 q0 q1 q2 1
68
(q1,0) {q0 , q2} 0, 1 q0 q1 q2 1
69
(q0 ,) {q0 , q2} 0, 1 q0 q1 q2 1
70
(q2 ,1) 0, 1 q0 q1 q2 1
71
70
72
Extended Transition Function *
* q0 , a q1 q4 q5 a a q1 a b q0 q2 q3
73
* q0 , aa q4 , q5 q4 q5 a a q1 a b q0 q2 q3
74
* q0 , ab q2 , q3, q0 q4 q5 a a q1 a b q0 q2 q3
75
q j *qi , w Formally It holds if and only if
there is a walk from qi with label w to q j
76
Your Turn 75
77
Example NFA.12 Design NFA for the language L = all string over {0,1} that have at least two consecutive 0’s or 1’s Solution: By the analysis, it is clear that NFA will accept the string of the patern 00, 11, , 76
78
Example NFA.13 Example: Design NFA for language L = (ab ∪ aba)*
Solution: Let us first analyze the given language i.e. L = (ab ∪ aba)* means either ab, aba or any combination of should be accepted by the NFA. Null string (€) is also accepted. 77
79
Example NFA.13 Example: Design NFA for language L = (ab ∪ aba)*
Solution: Let us first analyze the given language i.e. L = (ab ∪ aba)* means either ab, aba or any combination of should be accepted by the NFA. Null string (€) is also accepted. 78
80
Example NFA.13 Example: Design NFA for language L = (ab ∪ aba)*
Solution: Let us first analyze the given language i.e. L = (ab ∪ aba)* means either ab, aba or any combination of should be accepted by the NFA. Null string (€) is also accepted. 79
81
Example NFA.13 Example: Design NFA for language L = (ab ∪ aba)*
Solution: Let us first analyze the given language i.e. L = (ab ∪ aba)* means either ab, aba or any combination of should be accepted by the NFA. Null string (€) is also accepted. 80
82
Example NFA.13 Example: Design NFA for language L = (ab ∪ aba)*
Solution: Let us first analyze the given language i.e. L = (ab ∪ aba)* means either ab, aba or any combination of should be accepted by the NFA. Null string (€) is also accepted. 81
83
Example NFA.14 Example: Draw NFA for language L = (ab)* (ba) ∪ aa*
84
Example NFA.14 Example: Draw NFA for language L = (ab)* (ba) ∪ aa*
Solution: We can construct NFA for the language L in two paths i.e. L = L1 ∪ L2 L1 = (ab)* (ba) L2 = aa* First we can construct NFA L1
85
Example NFA.14 Secondly we construct NFA for L2
86
Example NFA.14 Now combine L2 and L2 85
87
Example NFA.14 Input: Empty 86
88
Example NFA.14 Input: Empty 87
89
Example NFA.14 Input: Empty 88
90
Example NFA.14 Input: aa 89
91
Example NFA.14 Input: aa 90
92
NFA TO DFA
93
92
94
Non Deterministic Features of NFA
There are three main cases of non- determinism in NFAs: Transition to a state without consuming any input. Multiple transitions on the same input symbol. No transition on an input symbol. To convert NFAs to DFAs we need to get rid of non-determinism from NFAs.
95
NFA to DFA 94
96
Example: Subset Construction
1 2,4 5 2 4,6 1,3,5 3 2,6 4 2,8 1,5,7 2,4,6,8 1,3,7,9 6 3,5,9 7 4,8 8 5,7,9 9 6,8 r b {1} {2,4} {5} {2,4} {5} Alert: What we’re doing here is the lazy form of DFA construction, where we only construct a state if we are forced to. * 96
97
Example: Subset Construction
1 2,4 5 2 4,6 1,3,5 3 2,6 4 2,8 1,5,7 2,4,6,8 1,3,7,9 6 3,5,9 7 4,8 8 5,7,9 9 6,8 r b {1} {2,4} {5} {2,4,6,8} {1,3,5,7} {2,4} {5} {2,4,6,8} {1,3,5,7} * 97
98
Example: Subset Construction
1 2,4 5 2 4,6 1,3,5 3 2,6 4 2,8 1,5,7 2,4,6,8 1,3,7,9 6 3,5,9 7 4,8 8 5,7,9 9 6,8 r b {1} {2,4} {5} {2,4,6,8} {1,3,5,7} * {1,3,7,9} {2,4} {5} {2,4,6,8} {1,3,5,7} {2,4,6,8} {1,3,7,9} * 98
99
Example: Subset Construction
1 2,4 5 2 4,6 1,3,5 3 2,6 4 2,8 1,5,7 2,4,6,8 1,3,7,9 6 3,5,9 7 4,8 8 5,7,9 9 6,8 r b {1} {2,4} {5} {2,4,6,8} {1,3,5,7} * {1,3,7,9} * {1,3,5,7,9} {2,4} {5} {2,4,6,8} {1,3,5,7} {2,4,6,8} {1,3,7,9} {2,4,6,8} {1,3,5,7,9} * 99
100
Example: Subset Construction
1 2,4 5 2 4,6 1,3,5 3 2,6 4 2,8 1,5,7 2,4,6,8 1,3,7,9 6 3,5,9 7 4,8 8 5,7,9 9 6,8 r b {1} {2,4} {5} {2,4,6,8} {1,3,5,7} * {1,3,7,9} * {1,3,5,7,9} {2,4} {5} {2,4,6,8} {1,3,5,7} {2,4,6,8} {1,3,7,9} {2,4,6,8} {1,3,5,7,9} * 100
101
Example: Subset Construction
1 2,4 5 2 4,6 1,3,5 3 2,6 4 2,8 1,5,7 2,4,6,8 1,3,7,9 6 3,5,9 7 4,8 8 5,7,9 9 6,8 r b {1} {2,4} {5} {2,4,6,8} {1,3,5,7} * {1,3,7,9} * {1,3,5,7,9} {2,4} {5} {2,4,6,8} {1,3,5,7} {2,4,6,8} {1,3,7,9} {2,4,6,8} {1,3,5,7,9} {2,4,6,8} {5} * 101
102
Example: Subset Construction
1 2,4 5 2 4,6 1,3,5 3 2,6 4 2,8 1,5,7 2,4,6,8 1,3,7,9 6 3,5,9 7 4,8 8 5,7,9 9 6,8 r b {1} {2,4} {5} {2,4,6,8} {1,3,5,7} * {1,3,7,9} * {1,3,5,7,9} {2,4} {5} {2,4,6,8} {1,3,5,7} {2,4,6,8} {1,3,7,9} {2,4,6,8} {1,3,5,7,9} {2,4,6,8} {5} * 102
103
Example: Subset Construction
1 2,4 5 2 4,6 1,3,5 3 2,6 4 2,8 1,5,7 2,4,6,8 1,3,7,9 6 3,5,9 7 4,8 8 5,7,9 9 6,8 r b {1} {2,4} {5} {2,4} {5} {2,4,6,8} {1,3,5,7} {2,4,6,8} {1,3,7,9} A B C {2,4,6,8} D {2,4,6,8} {1,3,5,7,9} {1,3,5,7} E {2,4,6,8} {1,3,5,7,9} F {2,4,6,8} {5} * {1,3,7,9} * {1,3,5,7,9} G {2,4,6,8} {1,3,5,7,9} * 103
104
Example: Subset Construction
1 2,4 5 2 4,6 1,3,5 3 2,6 4 2,8 1,5,7 2,4,6,8 1,3,7,9 6 3,5,9 7 4,8 8 5,7,9 9 6,8 r b B C D E D F D G F D C G D G * 104
105
Example: Subset Construction
r b B D D D D D D C E F G G C G A B C D E * F * G 105
106
105
107
Subset Construction Method
Fig1. NFA without λ-transitions 3 a b a a a 2 a,b b 1 5 a a,b 4 b
108
Subset Construction Method
Fig1. NFA without λ-transitions 3 a b Step1 Construct a transition table showing all reachable states for every state for every input signal. a a a 2 a,b b 1 5 a a,b 4 b
109
Subset Construction Method
Fig1. NFA without λ-transitions 3 Fig2. Transition table a b a a a 2 a,b b 1 5 a a,b 4 b
110
Subset Construction Method
Fig1. NFA without λ-transitions 3 Fig2. Transition table q δ(q,a) δ(q,b) 1 {1,2,3,4,5} {4,5} 2 {3} {5} 3 ∅ {2} 4 {4} 5 a b a a a 2 a,b b 1 5 a a,b 4 b
111
Subset Construction Method
Transition from stateFqig2. TraTnrasnitsiiotionn tfarobmlestate q with input a with input b Fig1. NFA without λ-transitions q δ(q,a) δ(q,b) 1 {1,2,3,4,5} {4,5} 2 {3} {5} 3 ∅ {2} 4 {4} 5 3 a b Starts here a a a 2 a,b b 1 5 a a,b 4 b
112
Subset Construction Method
Fig2. Transition table q δ(q,a) δ(q,b) 1 {1,2,3,4,5} {4,5} 2 {3} {5} 3 ∅ {2} 4 {4} 5 Step2 The set of states resulting from every transition function constitutes a new state. Calculate all reachable states for every such state for every input signal.
113
Fig3. Subset Construction table
Fig2. Transition table Fig3. Subset Construction table q δ(q,a) δ(q,b) 1 {1,2,3,4,5} {4,5} 2 {3} {5} 3 ∅ {2} 4 {4} 5 q δ(q,a) δ(q,b) 1 {1,2,3,4,5} {4,5} Starts with Initial state
114
∅ q δ(q,a) δ(q,b) q δ(q,a) δ(q,b) 4 5 Fig2. Transition table 1 {4,5} 2
Fig3. Subset Construction table q δ(q,a) δ(q,b) 1 {1,2,3,4,5} {4,5} 2 {3} {5} 3 ∅ {2} 4 {4} 5 q δ(q,a) δ(q,b) 1 {1,2,3,4,5} {4,5} Starts with Initial state
115
∅ q δ(q,a) δ(q,b) q δ(q,a) δ(q,b) Step3 4 5 Fig2. Transition table 1
Fig3. Subset Construction table q δ(q,a) δ(q,b) 1 {1,2,3,4,5} {4,5} 2 {3} {5} 3 ∅ {2} 4 {4} 5 q δ(q,a) δ(q,b) 1 {1,2,3,4,5} {4,5} Starts with Initial state Step3 Repeat this process(step2) until no more new states are reachable.
116
∅ q δ(q,a) δ(q,b) q δ(q,a) δ(q,b) 4 5 Fig2. Transition table 1 {4,5} 2
Fig3. Subset Construction table q δ(q,a) δ(q,b) 1 {1,2,3,4,5} {4,5} 2 {3} {5} 3 ∅ {2} 4 {4} 5 q δ(q,a) δ(q,b) 1 {1,2,3,4,5} {4,5} {2,4,5}
117
∅ q δ(q,a) δ(q,b) q δ(q,a) δ(q,b) 4 5 5 4 Fig2. Transition table 1
Fig3. Subset Construction table q δ(q,a) δ(q,b) 1 {1,2,3,4,5} {4,5} 2 {3} {5} 3 ∅ {2} 4 {4} 5 q δ(q,a) δ(q,b) 1 {1,2,3,4,5} {4,5} {2,4,5} 5 4
118
∅ q δ(q,a) δ(q,b) q δ(q,a) δ(q,b) 4 5 5 4 Fig2. Transition table 1
Fig3. Subset Construction table q δ(q,a) δ(q,b) 1 {1,2,3,4,5} {4,5} 2 {3} {5} 3 ∅ {2} 4 {4} 5 q δ(q,a) δ(q,b) 1 {1,2,3,4,5} {4,5} {2,4,5} 5 4 {3,5}
119
Fig3. Subset Construction table
Fig2. Transition table Fig3. Subset Construction table q δ(q,a) δ(q,b) 1 {1,2,3,4,5} {4,5} 2 {3} {5} 3 ∅ {2} 4 {4} 5 q δ(q,a) δ(q,b) 1 {1,2,3,4,5} {4,5} {2,4,5} 5 4 {3,5} ∅
120
∅ ∅ q δ(q,a) δ(q,b) q δ(q,a) δ(q,b) 4 5 5 4 Fig2. Transition table 1
Fig3. Subset Construction table q δ(q,a) δ(q,b) 1 {1,2,3,4,5} {4,5} 2 {3} {5} 3 ∅ {2} 4 {4} 5 q δ(q,a) δ(q,b) 1 {1,2,3,4,5} {4,5} {2,4,5} 5 4 {3,5} ∅ We already got 4 and 5. So we don’t add them again.
121
∅ ∅ q δ(q,a) δ(q,b) q δ(q,a) δ(q,b) 4 5 5 4 Fig2. Transition table 1
Fig3. Subset Construction table q δ(q,a) δ(q,b) 1 {1,2,3,4,5} {4,5} 2 {3} {5} 3 ∅ {2} 4 {4} 5 q δ(q,a) δ(q,b) 1 {1,2,3,4,5} {4,5} {2,4,5} 5 4 {3,5} ∅ 2
122
∅ ∅ q δ(q,a) δ(q,b) q δ(q,a) δ(q,b) 4 5 5 4 Fig2. Transition table 1
Fig3. Subset Construction table q δ(q,a) δ(q,b) 1 {1,2,3,4,5} {4,5} 2 {3} {5} 3 ∅ {2} 4 {4} 5 q δ(q,a) δ(q,b) 1 {1,2,3,4,5} {4,5} {2,4,5} 5 4 {3,5} ∅ 2
123
∅ ∅ q δ(q,a) δ(q,b) q δ(q,a) δ(q,b) 4 5 5 4 Fig2. Transition table 1
Fig3. Subset Construction table q δ(q,a) δ(q,b) 1 {1,2,3,4,5} {4,5} 2 {3} {5} 3 ∅ {2} 4 {4} 5 q δ(q,a) δ(q,b) 1 {1,2,3,4,5} {4,5} {2,4,5} 5 4 {3,5} ∅ 2 3
124
∅ ∅ q δ(q,a) δ(q,b) q δ(q,a) δ(q,b) 4 5 5 4 Fig2. Transition table 1
Fig3. Subset Construction table q δ(q,a) δ(q,b) 1 {1,2,3,4,5} {4,5} 2 {3} {5} 3 ∅ {2} 4 {4} 5 q δ(q,a) δ(q,b) 1 {1,2,3,4,5} {4,5} {2,4,5} 5 4 {3,5} ∅ 2 3 Stops here as there are no more reachable states
125
Fig4. Resulting FA after applying Subset Construction to fig1
Fig3. Subset Construction table a q δ(q,a) δ(q,b) 1 {1,2,3,4,5} {4,5} {2,4,5} 5 4 {3,5} ∅ 2 3 b a 12345 245 35 a a,b a b b ∅ a,b a 1 3 b a b 2 a 45 5 b b a 4 b
126
Fig4. Resulting FA after applying Subset Construction to fig1
Fig3. Subset Construction table a q δ(q,a) δ(q,b) I 1 {1,2,3,4,5} {4,5} II {2,4,5} III 5 4 IV {3,5} V ∅ VI VII 2 VIII IX 3 X b a II IV VII a a,b a b b a I b VIII X b a,b a IX a III V b b a VI b
127
Summary DFA’s, NFA’s, and ε–NFA’s all accept exactly the same set of languages: the regular languages. The NFA types are easier to design and may have exponentially fewer states than a DFA. But only a DFA can be implemented! 126
128
AI: Chapter 2: Intelligent 127
Agents
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.