Principles of Computing – UFCFA3-30-1 Week-5 Instructor : Mazhar H Malik Email : mazhar@gcet.edu.om Global College of Engineering and Technology
NON-DETERMINISTIC FINITE AUTOMATA (NFA) 1
Kleene Closure/Plus
Kleene Closure
Kleene Star – Example 2
Kleene Plus
Kleene Plus…
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
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
Definition: NON-DETERMINISTIC FINITE AUTOMATA (NFA) 10
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
11
Example: An NFA for a++(ab)+ Input : - 12
Example: An NFA for a++(ab)+ Input : a 13
Example: An NFA for a++(ab)+ Input : a 14
Example: An NFA for a++(ab)+ Input : a b 15
Example: An NFA for a++(ab)+ Input : a b (Accepted) 16
Example: An NFA for a++(ab)+ Input : a b a (Rejected) 17
Example: An NFA for a++(ab)+ Input : a a (Rejected) 18
19
Nondeterministic Finite Accepter (NFA) Alphabet = {a} q1 q2 q0 q3
q1 q2 q0 q3 Nondeterministic Finite Accepter (NFA) Alphabet = {a} Two choices q1 q2 q0 q3
q3 No transition q1 q2 q0 Nondeterministic Finite Accepter (NFA) Alphabet = {a} Two choices q1 q2 No transition q0 q3 No transition
First Choice q1 q2 q0 q3
First Choice q1 q2 q0 q3 24
First Choice q1 q2 q0 q3 25
First Choice q1 q2 “accept” q0 q3
Second Choice q1 q2 q0 q3
Second Choice q1 q2 q0 q3
Second Choice q1 q2 q0 No transition: the automaton hangs q3
Second Choice q1 q2 q0 q3 “reject”
Observation An NFA accepts a string if there is a computation of the NFA that accepts the string
aa is accepted by the NFA: Example aa is accepted by the NFA: q1 q2 q0 q3
Design NFA by observing the Output Language Given in side window 33
34
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
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
Example 37
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
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
40
Lambda Transitions q0 q1 q1 q3
q0 q1 q2 q3
q0 q1 q2 q3
(read head doesn’t move) q0 q1 q2 q3
q0 q1 q2 q3
“accept” q3 q0 q1 q2 String aa is accepted
Language accepted: L {aa} q0 q1 q2 q3
Another NFA Example a b q1 q2 q3 q0
a b a b q1 q2 q3 q0
a b a b q1 q2 q3 q0
a b a b q1 q2 q3 q0
a b “accept” q2 a b q1 q3 q0
Another String a b a b q0 q1 q2 q3
a b a b q0 q1 q2 q3
a b a b q0 q1 q2 q3
a b a b q0 q1 q2 q3
a b a b q0 q1 q2 q3
a b a b q0 q1 q2 q3
a b a b q0 q1 q2 q3
a b “accept” q2 a b q0 q1 q3
Language accepted L ab, abab, ababab, ... ab a b q1 q2 q3 q0
Another NFA Example 0, 1 q1 q2 q0 1
Language accepted L , 10, 1010, 101010, ... 10* 0, 1 q1 q2 q0 1
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
65
Transition Function q0 , 1 q1 0, 1 q0 q1 q2 1
(q1,0) {q0 , q2} 0, 1 q0 q1 q2 1
(q0 ,) {q0 , q2} 0, 1 q0 q1 q2 1
(q2 ,1) 0, 1 q0 q1 q2 1
70
Extended Transition Function * * q0 , a q1 q4 q5 a a q1 a b q0 q2 q3
* q0 , aa q4 , q5 q4 q5 a a q1 a b q0 q2 q3
* q0 , ab q2 , q3, q0 q4 q5 a a q1 a b q0 q2 q3
q j *qi , w Formally It holds if and only if there is a walk from qi with label w to q j
Your Turn 75
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, 101000, 101100 76
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
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
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
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
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
Example NFA.14 Example: Draw NFA for language L = (ab)* (ba) ∪ aa*
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
Example NFA.14 Secondly we construct NFA for L2
Example NFA.14 Now combine L2 and L2 85
Example NFA.14 Input: Empty 86
Example NFA.14 Input: Empty 87
Example NFA.14 Input: Empty 88
Example NFA.14 Input: aa 89
Example NFA.14 Input: aa 90
NFA TO DFA
92
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.
NFA to DFA 94
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
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
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
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
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
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
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
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
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
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
105
Subset Construction Method Fig1. NFA without λ-transitions 3 a b a a a 2 a,b b 1 5 a a,b 4 b
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
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
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
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
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.
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
∅ 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
∅ 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.
∅ 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}
∅ 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
∅ 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}
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} ∅
∅ ∅ 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.
∅ ∅ 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
∅ ∅ 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
∅ ∅ 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
∅ ∅ 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
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
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
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
AI: Chapter 2: Intelligent 127 Agents