0

How 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 e.g. 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.

bad_coder
  • 11,289
  • 20
  • 44
  • 72
AlenEviLL
  • 56
  • 1
  • 9

2 Answers2

0

To solve it I would go this way:

  1. Create a Map (String, String or integer), I will use the key to check what actual state>symbol is and the value to track which state the transaction goes to (null or -1 if the actual state do not have a transaction with that symbol);
  2. Populate the map keys and values, ex: map.put("1>1", null) means from state 1 with symbol 1 goes to null. For now, put null or -1 for every possibility;
  3. Iterate over your transaction possibilities, build the key string, retrieve fom the map the corresponding value. If it is null include the number of the state that goes to, else means there is another transaction coming from the same state with the same symbol that goes to a different state, so it is not deterministic;

If you can iterate over the whole array it is deterministic!

Patrick Sava
  • 126
  • 4
0

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 q1 and input a such that both

  • (q1, a) -> q2
  • (q1, a) -> q3

are in delta. You implement this (qfrom, input) -> qto 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 qfrom -> input -> qto, and it can be bracketed in two ways:

  • (qfrom -> input) -> qto: this is what you had, with a little different notation
  • qfrom -> (input -> qto)

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.

David Nemeskey
  • 640
  • 1
  • 5
  • 16