Introduction
Introduction
Non-deterministic automata are a special type of automata where the exact state to which the machine moves cannot be determined. In practical terms, it means that the transition from state to state with a particular symbol, can take us to many different states, not only one. I talked about deterministic automata (DFA) on this post, here I focus on Non-deterministic automata. Formally we define a Non-Deterministic Finite Automata (NFA) as where:
N(Q,Σ,δ,,F)
q
0
◼
Q
◼
Σ
◼
δ:QΣ
Q
2
◼
A starting state ∈Q
q
0
◼
A set of final states
F⊆Q
Working with NFA in Mathematica
Working with NFA in Mathematica
Les use the following NFA as an example (taken from Hopcroft),
This automata accepts strings that end in 01. We use associations to build the transitions function. Keep in mind that we want to define them as , so:
δ:QΣ
Q
2
In[]:=
δ=Association[{0,"0"}{0,1},{0,"1"}{0},{1,"1"}{2}];
Notice how the keys map to a list of states, instead of a state.Now lets build a functions for processing a string with this transitions function, and lets do it step by step. Mathematically we want to create an extended function, the following way,
δ
◼
Base step: (q,ϵ){q}
δ
◼
Inductive step: let and (q,x){,,…,}, then we have that (q,w)δ(,a)
wxa
δ
p
1
p
2
p
k
δ
k
⋃
i1
p
i
You can check Hopcroft’s book for a more detailed explanation.
As a start, based on the mathematical definition, we have to evaluate recursively. For that, we can use FoldList. For example,
δ
FoldList[δ[{#1,#2}]&,0,{"1"}]
Out[]=
{0,{0}}
Now we have to deal with the fact that returns a list, and each one of those have to be evaluated. We have to modify the pure function δ[{#1,#2}]& that we are using in FoldList. So for example if we have to evaluate states {0,1} with symbol “1”, we can do,
δ
Table[δ[{#1[[i]],#2}],{i,Length[#1]}]&[{0,1},"0"]
Out[]=
{{0,1},Missing[KeyAbsent,{1,0}]}
Now lets deal with missing transitions. With the same example we can then write,
Table[δ[{#1[[i]],#2}]/._MissingNothing,{i,Length[#1]}]&[{0,1},"0"]
Out[]=
{{0,1}}
Basically we get rid of missinng keys. Now, we want the union of all resulting states,
Union@@Table[δ[{#1[[i]],#2}]/._MissingNothing,{i,Length[#1]}]&[{0,1},"0"]
Out[]=
{0,1}
Finally we are ready to create our function DeltaHat,
δ
In[]:=
DeltaHat[δ_,q0_List,string_]:=FoldList[Union@@Table[δ[{#1[[i]],#2}]/._MissingNothing,{i,Length[#1]}]&,q0,Characters[string]]
We can now use it to evaluate the string “00101” for example,
DeltaHat[δ,{0},"00101"]
Out[]=
{{0},{0,1},{0,1},{0,2},{0,1},{0,2}}
Now we can create a function that determines is a string is valid,
In[]:=
IsValid[δ_,q0_List,qf_List,string_]:=IntersectingQ[Last[DeltaHat[δ,q0,string]],qf]
IsValid[δ,{0},{2},"00101"]
Out[]=
True
Enjoy!
References
References
◼
Hopcroft and Ullman, Introduction to Automata Theory, Languages and Computation, Addison Wesley.