NFA — Non-deterministic Finite Automaton
Accepts a string only if it ENDS IN "ab".
What this machine checks
This machine is built to accept a string only if it ends in "ab". Nothing else.
Take "aab". It starts in q0. On the first 'a' the machine does something a DFA cannot: it goes to BOTH q0 and q1 at once — staying in q0 means "still scanning", moving to q1 means "guessing this 'a' starts the final ab". The second 'a' keeps both alive. Then 'b' takes q1 to q2, the accepting state, while q0 loops back to itself. We end with q2 among the active states, so the string is accepted.
Try "abb" and watch q2 light up in the middle and then go dark — the string contained "ab", but it did not END with it.
- 1.Type a string of a's and b's, e.g. a b b a b
- 2.Press Run
- 3.Watch every active state light up at once — an NFA explores all possibilities together
What the code is doing under the hood
The core is a TRANSITION TABLE, not a chain of if-statements. The loop does one lookup per character — current state plus current character gives the next state — and there is a single if at the very end asking whether the state we landed in is an accepting one. Swap the table and the same code recognises a different language.
What an NFA adds on top of that same core: the lookup returns a SET of possible next states instead of one, so the code carries a set forward instead of a single state. There is no backtracking and no re-running — all branches advance together, one step per character.
Run this demo to see the code that executed.