antifold

Computation

NFAs from First Principles: Interactive

What if a machine could be in many states at once? Nondeterministic finite automata explore every possible path simultaneously, and yet they are no more powerful than their deterministic cousins. Build one, run one, and watch the subset construction prove it.

Hover here to see links to related content.

From Words to Machines

Here is an idea that should stop you in your tracks: what if a machine could be in multiple states at the same time? Not broken, not confused, but genuinely exploring every possible path through a computation simultaneously. That is exactly what a nondeterministic finite automaton does. Think of it like navigating a maze by cloning yourself at every fork. Each clone walks a different corridor. If any single clone reaches the exit, you win.

The formal setup starts simply. Pick an alphabet $\Sigma$ (e.g., $\{a,b\}$). Words are finite strings over $\Sigma$; the set of all words is $\Sigma^*$, which includes the empty word $\varepsilon$. An NFA is a five-tuple $(Q,\Sigma,\delta,q_0,F)$, just like a DFA, except for one crucial difference: $\delta(q,a)$ returns a set of possible next states rather than a single one. We may also allow $\varepsilon$-moves, which let the machine jump between states without consuming any input at all.

How does the machine process an entire word? The following formula says: first, collect all the states reachable via free $\varepsilon$-moves. Then, for each symbol, follow every possible transition from every current state and close under $\varepsilon$ again. $$\hat{\delta}(S,\varepsilon)=\varepsilon\text{-closure}(S),\qquad \hat{\delta}(S,aw)=\hat{\delta}(\,\varepsilon\text{-closure}(\bigcup_{q\in S}\delta(q,a)),\,w\,).$$

A word $w$ is accepted when $\hat{\delta}(\{q_0\},w)$ intersects $F$. In plain words: if even one of your clones ends up in an accepting state, the whole string is accepted. The machine is an optimist; it only needs one successful path.

Try It Yourself: Run Preset NFAs

Pick a pattern from the dropdown, type an input string, and step through it. Watch how the set of active states splits and merges as the machine explores every possibility in parallel. Remember: the string is accepted if even one path ends in an accepting state. Try different inputs and see what gets through.

Build Your Own NFA

Now it is your turn. Place states on the canvas, choose a start, toggle which states accept, and wire them up with transitions. Type 'e' for an $\varepsilon$-move and watch the machine teleport between states for free. There are no wrong answers here; experiment freely. Try building an NFA with redundant paths, or one that uses $\varepsilon$-moves to branch. Enter an input and step through to see all the places the machine might be at once. When you are ready, click “Determinize” to see how the subset construction collapses your nondeterministic design into a clean, equivalent DFA.

Here is the beautiful thing about all of this. Every NFA, no matter how wildly nondeterministic, can be converted into a plain old DFA that accepts exactly the same strings. The trick is called subset construction, and the intuition is lovely: each "state" of the new DFA is an entire set of NFA states. The $\varepsilon$-closure gathers all the states you can reach for free. Each input step follows transitions from the whole set, then re-closes under $\varepsilon$. The starting state of the DFA is $\varepsilon$-closure($\{q_0\}$), and a DFA state is accepting if it contains any accepting NFA state. Nondeterminism, it turns out, is a convenience, not extra power.

Connections