I have a automata coursework that is really bothering me by its difficulty. I have spent almost 5 hours today doing without actually knowing whether it's right or not. The task is to write a Java code that will determine whether the given automaton is deterministic. What I have is a class that represents automata and it has the following five variables: int alphabet_size; int n_states; int delta[][]; int initial_state; int accepting_states[];
Delta represents following eg in real automata q1, 1, q2 in program would look like {1, 1, 2}. I know that in a non-deterministic automaton there could be more than one possible state to go to, but I don't know how to compute that.
Thanks in advance.
To solve it I would go this way:
If you can iterate over the whole array it is deterministic!
With the member variables that you have now, you can only represent a deterministic FSA. Why? Because a NDFSA is one where there is a state q 1 and input a such that both
are in delta. You implement this (q from , input) -> q to mapping as int[][]
, which means that for each instate-input pair, you can only store one outstate.
One very quick fix would be to allow a list of outstates for all instate-input pair, like so:
ArrayList[][] delta;
However, this is ugly. We can arrive at a nicer solution* if you realize that delta is actually the mapping q from -> input -> q to , and it can be bracketed in two ways:
Another thing to realize is that int[]
is conceptually the same as Map<Integer, Integer>
. With that, the second mapping can implemented in Java as:
Map<Integer, Map<Integer, List<Integer>>> delta;
Whichever implementation you choose, the test that decides if the FSA is deterministic or not is very simple, and comes from the definition: A is a DFSA iff the lengths of all lists in delta
are <= 1; otherwise, it is a NDFSA .
*Note that real FSA libraries usually use a different approach. For instance, foma uses an array of transition object with fields instate, outstate and the input character. However, for starters, the ones above are probably easier to handle.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.