CS 350 — Fall 2018 gilray.org/classes/fall2018/cs350/ Deterministic finite automata (DFA) & Non-deterministic finite automata (NFA) CS 350 — Fall 2018 gilray.org/classes/fall2018/cs350/
DFA transition diagram 𝐿={hello} h e l o q0 q1 q2 q3 l q4 q5 DFA transition diagram
Formally, each DFA is a 5-tuple: A set of final states Formally, each DFA is a 5-tuple: (𝑄,Σ,𝛿, 𝑞 0 ,𝐹) A starting state A finite set of states {q…} A transition function An alphabet
Formally, each DFA is a 5-tuple: (𝑄,Σ,𝛿, 𝑞 0 ,𝐹) 𝛿⊆(𝑄×Σ⇀𝑄) 𝐹⊆𝑄 𝑞 0 ∈𝑄
(𝑄,Σ,𝛿, 𝑞 0 ,𝐹),where 𝑄={ 𝑞 0 ,…, 𝑞 5 } Σ={a,b,…,z} 𝐹={ 𝑞 5 } l l o q0 q1 q2 q3 q4 q5 (𝑄,Σ,𝛿, 𝑞 0 ,𝐹),where 𝑄={ 𝑞 0 ,…, 𝑞 5 } Σ={a,b,…,z} 𝛿={( 𝑞 0 ,h, 𝑞 1 ),( 𝑞 1 ,e, 𝑞 2 ), ( 𝑞 2 ,l, 𝑞 3 ),( 𝑞 3 ,l, 𝑞 4 ),( 𝑞 4 ,o, 𝑞 5 )} 𝐹={ 𝑞 5 }
h e l o q0 q1 q2 q3 l q4 q5 𝛿 h l e o … … 𝑞 0 𝑞 1 DFA transition table 𝑞 1 𝑞 2 𝑞 2 𝑞 3 𝑞 3 𝑞 4 𝑞 4 𝑞 5 𝑞 5 𝛿={( 𝑞 0 ,h, 𝑞 1 ),( 𝑞 1 ,e, 𝑞 2 ), ( 𝑞 2 ,l, 𝑞 3 ),( 𝑞 3 ,l, 𝑞 4 ),( 𝑞 4 ,o, 𝑞 5 )}
Extended transition function h e l q3 l o q0 q1 q2 q4 q5 Extended transition function 𝛿 ̂ ={( 𝑞 0 ,h, 𝑞 1 ),( 𝑞 1 ,e, 𝑞 2 ), ( 𝑞 2 ,l, 𝑞 3 ),( 𝑞 3 ,l, 𝑞 4 ),( 𝑞 4 ,o, 𝑞 5 ), ( 𝑞 0 ,he, 𝑞 2 ),( 𝑞 1 ,el, 𝑞 3 ),( 𝑞 2 ,ll, 𝑞 4 ),( 𝑞 3 ,lo, 𝑞 5 ), ( 𝑞 0 ,hel, 𝑞 3 ),( 𝑞 1 ,ell, 𝑞 4 ),( 𝑞 2 ,llo, 𝑞 5 ), ( 𝑞 0 ,hell, 𝑞 4 ),( 𝑞 1 ,ello, 𝑞 5 ),( 𝑞 0 ,hello, 𝑞 5 )} 𝛿={( 𝑞 0 ,h, 𝑞 1 ),( 𝑞 1 ,e, 𝑞 2 ), ( 𝑞 2 ,l, 𝑞 3 ),( 𝑞 3 ,l, 𝑞 4 ),( 𝑞 4 ,o, 𝑞 5 )}
Computing the extended transition function as a least fixed point 𝛿 ̂ ,where 𝑓 𝛿,𝑄 ( 𝛿 ̂ )= 𝛿 ̂ 𝑓 𝛿,𝑄 ( 𝛿 ̂ )={( 𝑞 𝑖 , 𝑠 0 𝑠 1 , 𝑞 𝑗 )|( 𝑞 𝑖 , 𝑠 0 , 𝑞 𝑘 )∈𝛿∧( 𝑞 𝑘 , 𝑠 1 , 𝑞 𝑗 )∈ 𝛿 ̂ } ∪{( 𝑞 𝑖 ,𝜖, 𝑞 𝑖 )| 𝑞 𝑖 ∈𝑄}
Computing the extended transition function as a least fixed point 𝑓 𝛿,𝑄 ( 𝛿 ̂ )={( 𝑞 𝑖 , 𝑠 0 𝑠 1 , 𝑞 𝑗 )|( 𝑞 𝑖 , 𝑠 0 , 𝑞 𝑘 )∈𝛿∧( 𝑞 𝑘 , 𝑠 1 , 𝑞 𝑗 )∈ 𝛿 ̂ } ∪{( 𝑞 𝑖 ,𝜖, 𝑞 𝑖 )| 𝑞 𝑖 ∈𝑄} ℒ( a ∗ ) 𝛿 a 𝑞 0 𝑞 0 a q0
Computing the extended transition function as a least fixed point 𝑓 𝛿,𝑄 ( 𝛿 ̂ )={( 𝑞 𝑖 , 𝑠 0 𝑠 1 , 𝑞 𝑗 )|( 𝑞 𝑖 , 𝑠 0 , 𝑞 𝑘 )∈𝛿∧( 𝑞 𝑘 , 𝑠 1 , 𝑞 𝑗 )∈ 𝛿 ̂ } ∪{( 𝑞 𝑖 ,𝜖, 𝑞 𝑖 )| 𝑞 𝑖 ∈𝑄} ℒ( a ∗ ) 𝛿 ̂ 𝜖 a . . . 𝑞 0 𝑞 0 𝑞 0 a q0
Computing the extended transition function as a least fixed point 𝑓 𝛿,𝑄 ( 𝛿 ̂ )={( 𝑞 𝑖 , 𝑠 0 𝑠 1 , 𝑞 𝑗 )|( 𝑞 𝑖 , 𝑠 0 , 𝑞 𝑘 )∈𝛿∧( 𝑞 𝑘 , 𝑠 1 , 𝑞 𝑗 )∈ 𝛿 ̂ } ∪{( 𝑞 𝑖 ,𝜖, 𝑞 𝑖 )| 𝑞 𝑖 ∈𝑄} ℒ( a ∗ ) 𝛿 ̂ 𝜖 a aa aaa . . . 𝑞 0 𝑞 0 𝑞 0 𝑞 0 𝑞 0 a q0
ℒ(𝑄,Σ,𝛿, 𝑞 0 ,𝐹)={𝑠|( 𝑞 0 ,𝑠, 𝑞 𝑓 )∈ 𝛿 ̂ ∧ 𝑞 𝑓 ∈𝐹} The Language L of a DFA: and 𝛿 ̂ (𝑄,Σ,𝛿, 𝑞 0 ,𝐹) ℒ(𝑄,Σ,𝛿, 𝑞 0 ,𝐹)={𝑠|( 𝑞 0 ,𝑠, 𝑞 𝑓 )∈ 𝛿 ̂ ∧ 𝑞 𝑓 ∈𝐹} where 𝛿 ̂ is the least fixed point 𝛿 ̂ = 𝑓 𝛿,𝑄 ( 𝛿 ̂ )
L is the language L((a|b)*cc(a|b)*). Try an example: L is the language L((a|b)*cc(a|b)*).
L is the language L((a|b)*cc(a|b)*). Try an example: L is the language L((a|b)*cc(a|b)*). c c q0 q1 q2 a,b a,b
L is the language L((a|b)*bab). Try an example: L is the language L((a|b)*bab).
L is the language L((a|b)*bab). Try an example: L is the language L((a|b)*bab). b b b a b q0 q1 q2 q3 a a a
Try an example: L is the language, over {a,b}, of strings with an even number of a’s and of b’s.
Try an example: L is the language, over {a,b}, of strings with an even number of a’s and of b’s. a q0 q2 a b b b b q1 a q3 a
There are an even number of a’s There are an odd number of a’s q0 q2 a b b b b q1 a q3 a
a a b b b b a a There are an even number of b’s q0 q2 a b b b b q1 a q3 a There are an odd number of bs
So this is just the DFAs for (aa). and for (bb). composed So this is just the DFAs for (aa)* and for (bb)* composed. Can this idea be generalized? a (E,E) (O,E) a b b b b (E,O) a (O,O) a
𝐹 ∩ ={( 𝑞 0𝐹 , 𝑞 1𝐹 )| 𝑞 0𝐹 ∈ 𝐹 0 ∧ 𝑞 1𝐹 ∈ 𝐹 1 } ℒ( 𝑄 0 ,Σ, 𝛿 0 , 𝑞 00 , 𝐹 0 )∩ℒ( 𝑄 1 ,Σ, 𝛿 1 , 𝑞 10 , 𝐹 1 ) =ℒ( 𝑄 ∩ ,Σ, 𝛿 ∩ , 𝑞 ∩ , 𝐹 ∩ ) 𝑄 ∩ = 𝑄 0 × 𝑄 1 𝛿 ∩ ={(( 𝑞 0 , 𝑞 1 ),𝑠,( 𝑞 0 ′ , 𝑞 1 ′ )) |( 𝑞 0 ,𝑠, 𝑞 0 ′ )∈ 𝛿 0 𝑞 ∩ =( 𝑞 00 , 𝑞 10 ) ∧( 𝑞 1 ,𝑠, 𝑞 1 ′ )∈ 𝛿 1 } 𝐹 ∩ ={( 𝑞 0𝐹 , 𝑞 1𝐹 )| 𝑞 0𝐹 ∈ 𝐹 0 ∧ 𝑞 1𝐹 ∈ 𝐹 1 }
Try an example: L is the language, over {0,1}, of strings with an even number of 0s and two 1s.
Try an example: L is the language, over {0,1}, of strings with an even number of 0s and two 1s. 1 1 q0 q1 q2 q0 q1 1 1
Try an example: L is the language, over {0,1}, of strings with an even number of 0s and two 1s. 1 1 q0,q0 q0,q1 q0,q2 1 1 q1,q0 q1,q1 q1,q2
is the complement of language (a|b)*bab Try an example: is the complement of language (a|b)*bab 𝐿
(Recall that this is how we defined L as a DFA.) Try an example: is the complement of language (a|b)*bab 𝐿 b b b a b q0 q1 q2 q3 a a a (Recall that this is how we defined L as a DFA.)
is the complement of language (a|b)*bab Try an example: is the complement of language (a|b)*bab 𝐿 b b b a b q0 q1 q2 q3 a a a
(Recall that this is how we defined {a} as a DFA.) Try an example: is the complement of language a 𝐿 a q0 q1 (Recall that this is how we defined {a} as a DFA.)
is the complement of language a Try an example: is the complement of language a 𝐿 a a q0 q1 q2 a
𝑄 ′ =𝑄∪{ 𝑞 dead } 𝐹 ′ = 𝑄 ′ −𝐹 𝛿 ′ =𝛿∪{( 𝑞 dead ,𝑠, 𝑞 dead )|𝑠∈Σ} The complement ofℒ(𝑄,Σ,𝛿, 𝑞 0 ,𝐹)isℒ( 𝑄 ′ ,Σ, 𝛿 ′ , 𝑞 0 , 𝐹 ′ ) 𝑄 ′ =𝑄∪{ 𝑞 dead } 𝐹 ′ = 𝑄 ′ −𝐹 𝛿 ′ =𝛿∪{( 𝑞 dead ,𝑠, 𝑞 dead )|𝑠∈Σ} ∪{(𝑞,𝑠, 𝑞 dead )|𝑠∈Σ∧𝑞∈𝑄∧(𝑞,𝑠, 𝑞 ′ )∉𝛿}
Non-deterministic finite automata (NFA)
Models of regular languages “Converts to” GNFA “Converts to” RE DFA “Minimizes to” NFA “Converts to” “Converts to”
L is the language L((a|b)*bab). Try an example: L is the language L((a|b)*bab). b a b q0 q1 q2 q3 a,b
b a b a,b Let’s run this nondeterministically on the string babab and track what paths we can take: b a b q0 q1 q2 q3 a,b
/ Let’s run this nondeterministically on the string babab and track what paths we can take: b a b q0 q1 q2 q3 a,b
/ / Let’s run this nondeterministically on the string babab and track what paths we can take: b a b q0 q1 q2 q3 a,b
/ / / Let’s run this nondeterministically on the string babab and track what paths we can take: b a b q0 q1 q2 q3 a,b
/ / / / Let’s run this nondeterministically on the string babab and track what paths we can take: b a b q0 q1 q2 q3 a,b
If any possible path would accept, then the NFA accepts. / / / / / Let’s run this nondeterministically on the string babab and track what paths we can take: If any possible path would accept, then the NFA accepts. b a b q0 q1 q2 q3 a,b
Formally, each NFA is also a 5-tuple: A set of final states Formally, each NFA is also a 5-tuple: (𝑄,Σ,𝛿, 𝑞 0 ,𝐹) A starting state A finite set of states {q0,…} A transition relation An alphabet
𝛿:𝑄×Σ×𝑄 A transition relation
𝛿:𝑄×Σ×𝑄 𝛿:𝑄×Σ→𝒫(𝑄) A transition relation
b 𝛿 a 𝑞 0 𝑞 1 𝑞 2 𝑞 3 NFA transition table { 𝑞 0 } { 𝑞 0 , 𝑞 1 } q0 q1 q2 q3 a,b NFA transition table b 𝛿 a 𝑞 0 { 𝑞 0 } { 𝑞 0 , 𝑞 1 } 𝑞 1 { 𝑞 2 } 𝑞 2 { 𝑞 3 } 𝑞 3
NFA extended transition table q0 q1 q2 q3 NFA extended transition table a,b 𝛿 ̂ b ab ba bab a aa 𝑞 0 { 𝑞 0 } { 𝑞 0 , 𝑞 1 } { 𝑞 0 } { 𝑞 0 , 𝑞 1 } { 𝑞 0 , 𝑞 2 } { 𝑞 0 , 𝑞 3 } 𝑞 1 { 𝑞 2 } { 𝑞 3 } … 𝑞 2 { 𝑞 3 } 𝑞 3
This is exactly the same as for a DFA, except now a relation. Computing an NFA’s extended transition relation as a least fixed point This is exactly the same as for a DFA, except now a relation. 𝛿 ̂ ,where 𝑓 𝛿,𝑄 ( 𝛿 ̂ )= 𝛿 ̂ 𝑓 𝛿,𝑄 ( 𝛿 ̂ )={( 𝑞 𝑖 , 𝑠 0 𝑠 1 , 𝑞 𝑗 )|( 𝑞 𝑖 , 𝑠 0 , 𝑞 𝑘 )∈𝛿∧( 𝑞 𝑘 , 𝑠 1 , 𝑞 𝑗 )∈ 𝛿 ̂ } ∪{( 𝑞 𝑖 ,𝜖, 𝑞 𝑖 )| 𝑞 𝑖 ∈𝑄}
The Language L of an NFA: and 𝛿 ̂ (𝑄,Σ,𝛿, 𝑞 0 ,𝐹) 𝐿={𝑠|( 𝛿 ̂ ( 𝑞 0 ,𝑠)∩𝐹)⊃Ø}