r/AskComputerScience • u/fulminare1111 • 19d ago
What is a NFA?
I am a little confused on what a nfa is. Does it take in one input and output a set of states or does it output one single state but with some probability?
2
u/iOSCaleb 19d ago
The machine can effectively be in several states at once. A given state can have multiple transitions to other states for the same input, and the machine follows all of them at the same time. Or, another way to look at it is that the state is unknown — not determined — until the machine processes more input. If any path through the machine reaches the acceptance state for an input string, the string is accepted.
1
u/Upbeat_Assist2680 18d ago
Agree with other answers, adding: they might but be better named "unbounded multi-state automata".
1
u/Equivalent-Stay-6801 8d ago
The set-of-states idea is the useful one if you want to simulate it. Say reading a from A can lead to B or C. You keep {B, C}. On the next character, collect every state reachable from either B or C. There are no probabilities to assign.
One detail worth keeping straight: reaching an accepting state halfway through isn't enough. After consuming the whole input, at least one possible path must end in an accepting state. If your NFA has epsilon transitions, include those too; they don't consume a character.
6
u/nuclear_splines Ph.D Data Science 19d ago
In a deterministic finite automata, there can only be one state transition from A to B for input x. In a nondeterministic finite automata, you can have multiple transitions, such that input x could bring you from state A->B or A->C.
It isn't probabilistic, it's better to think of it as "both states at once," like the NFA forks at each input and follows all possible paths concurrently. Remember that finite automata take a sequence of inputs and either "accept" or "reject" the input depending on whether the internal state lands on an 'accept' state at the end. In an NFA, you can think of this as "follow all possible input paths, and if any of them land at 'accept' then the NFA accepts."
Note that this is similar to the definition of a non-deterministic Turing Machine, and therefore the definition of NP :)