I'm trying to a develop a simulation that executes a non deterministic finite automaton in Java. The first command line argument is a text file that defines the machine. The second argument is an input string. If it accepts the string, it prints to standard output "accept" followed by a list of accept states it can end in. If it rejects, it outputs "reject" followed by a list of all possible end states.
For example, the text:
state 1 start
state 2
state 7 accept
transition 1 0 1
transition 1 1 1
transition 1 0 2
transition 2 0 2
transition 2 0 1
transition 2 1 1
transition 2 0 7
transition 7 0 7
transition 7 1 7
which looks like:
with an input string of 10 would output
reject 1 2
and with an input string of 000 would output
accept 7
I need advice on what data structures to use. I thought about using hashmaps and sets but I'm not sure.
NFA stands for non-deterministic finite automata. It is easy to construct an NFA when compared to DFA for a given regular language. The finite automata are called NFA when there exist many paths for specific input from the current state to the next state. Each NFA can be translated into DFA but every NFA is Non DFA.
An nfa can be implemented by means of a recursive search from the start state for a path (directed by the symbols of the input string) to a final state. One problem with this implementation is that it could get into an infinite loop if there is a cycle of l transitions.
It's important because NFAs may be wont to reduce the complexity of the mathematical work required to determine many important properties within the theory of computation. For instance, it's much easier to prove the closure properties of standard languages using NFAs than DFAs.
I think you should transform your automaton into a deterministic one and then do the obvious.
There are three common ways of implementing a finite state machine in software:
switch
statements for the current state and input character to explicitly define the next state in code.Since you'll need to build your automaton at run-time, the last two options don't really apply, so you should go with the lookup table. If your alphabet is small, you can write down all transitions. If the alphabet is huge, this might waste a lot of space and you can think about table compression which used to be an active field of research.
For the Downvoters: You cannot write a deterministic program that “executes” a non-deterministic automaton. However, by a rather fundamental theorem of theoretical computer science, you can transform any non-deterministic finite state automaton into a deterministic one by a fully automated procedure, that is, a deterministic algorithm that can easily be implemented in software. Every computer science student should have done this procedure at least once using pencil and paper. If you do so, you'll quickly see how to keep track of which states of the original automaton went into which states of the deterministic one. Then, simply simulate the latter one, see in what state it ends up and output the states of the original non-deterministic automaton that constitute it.
NFA means you can have set of states at a time. So to represent current state of NFA use Set interface.
Set interface guarantees that there won't be any duplicates of state in . This is important as NFA has more than one state at a time. If you use any other data set this gurrentee is not there. In case of NFA chance of having duplicate state in each transition is exponential. But set of state is always finite. Set interface guarantees that your current set will be filled with duplicates.
For space and performance you can use Enumset as Enumset use bit vectors to store state internally.
Algorithm:
initialise to start state
Process string from right to left starting from index 0;
for character at update the using the state transition;
If for any of this transition leads to final state which means that string is accepted.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With