Search code examples
loopswolfram-mathematicadeterministicautomaton

mathematica deterministic automaton


I want to make a module in mathematica that returns if an automaton is deterministic or not. I am considering that an automaton is not deterministic if there are 2 transitions that start at the same state and read the same simbol, or also if there exists an empty transition.

I want to debug this code but I cant:

isDeterministic[au_] := Module[{a, s},
  For[i = 1, i <= Length[au[[3]]],
   a = au[[3]][[i]][[1]];
   s = au[[3]][[i]][[2]];
   If[s == {}, Return[False]];
   For[j = i, j <= Length[au[[3]]],
    If[a == au[[3]][[j]][[1]] && s == au[[3]][[j]][[2]], 
     Return[False]];
    j++;
    ];
   i++;
   ];
  Return[True];
  ]
A = {{1, 2},
  {a, b},
  {{1, a, 2}, {2, b, 1}},
  1,
  {2}
  }
isDeterministic[A]

A is an automaton where the first element is a list of the states, second is the alphabet, third are the transitions, fourth is the initial state, fifth is the list of final states.

The main problem is that when I apply the function to A it never ends.

EDIT: SOLVED

this is the final code:

isDeterministic[au_] := 
 Module[{a, s, lambda}, 
  For[i = 1, i <= Length[au[[3]]], i++, a = au[[3]][[i]][[1]];
   s = au[[3]][[i]][[2]];
   If[s == lambda, Return[False]];
   For[j = i + 1, j <= Length[au[[3]]], j++, 
    If[a == au[[3]][[j]][[1]] && s == au[[3]][[j]][[2]], 
     Return[False]]]];
  True]

A = {{1, 2},
  {a, b},
  {{2, b, 1}, {1, a, 2}},
  1,
  {2}
  }

isDeterministic[A]

True

Solution

  • Try this

    isDeterministic[au_]:=Module[{a,s,len = Length[au[[3]]] },
    
      For[i = 1, i <= len, i++,
    
         a=au[[3]][[i]][[1]];
         s=au[[3]][[i]][[2]];
    
         If[s=={}, Return[False,Module] ];
    
         For[j = i, j <= len, j++,
    
            If[a==au[[3]][[j]][[1]]&&s==au[[3]][[j]][[2]],
               Return[False,Module]
            ]
         ]
      ];
    
      True
     ]