Topic

Automata and regular expressions

A finite automaton reads a word one symbol at a time, moving between finitely many states, and accepts or rejects at the end. The languages it can recognise are exactly those a regular expression can describe, and almost every question about them can be decided.

Automata and runs

A DFA has one move for each state and symbol. An NFA may have several, or none, or moves on the empty word ε; it accepts when some way of running ends in an accepting state. Give the moves as a table:

import { automaton } from "@ratmath/logic";

// words with an even number of a's
const even = automaton({ states: ["e", "o"], alphabet: "ab", start: "e", accept: ["e"],
  delta: { e: { a: "o", b: "e" }, o: { a: "e", b: "o" } } });
even.accepts("abba");     // true
even.run("aba").trace;    // [{ read: null, states: ["e"] }, { read: "a", states: ["o"] },
                          //  { read: "b", states: ["o"] }, { read: "a", states: ["e"] }]
even.words(3);            // ["", "b", "aa", "bb", "aab", "aba", "baa", "bbb"]

From NFA to DFA, and the smallest DFA

An NFA is in a set of states at each step, so a DFA whose states are those sets does the same job (the subset construction). It can need 2n2^n states for an nn-state NFA, but usually far fewer are reachable. Then the DFA can be shrunk: two states that accept exactly the same continuations can be merged, and refining "accepting or not" until nothing splits gives the smallest DFA for the language, which is unique up to the names of its states (Myhill and Nerode).

import { regexToNFA, toDFA, minimizeAutomaton } from "@ratmath/logic";

const n = regexToNFA("(a|b)*abb");     // 20 states, with ε moves
toDFA(n).states.length;                // 5
const m = minimizeAutomaton(n);
m.toString();
// start q0; accept q3
// q0 --a--> q1      q0 --b--> q0
// q1 --a--> q1      q1 --b--> q2
// q2 --a--> q1      q2 --b--> q3
// q3 --a--> q1      q3 --b--> q0

State qkq_k remembers how much of abb the word has just read.

Equivalence, with a reason

Two automata accept the same language exactly when, running both side by side, no reachable pair of states has one accepting and the other not. A breadth-first search over the pairs finds a shortest word that tells them apart, and the word is checked on both.

import { equivalentAutomata } from "@ratmath/logic";

equivalentAutomata("(a|b)*", "(a*b*)*");   // { equivalent: true }
equivalentAutomata("a*b*", "(a|b)*");
// { equivalent: false, word: "ba", acceptedBy: "second" }

Regular expressions

A regular expression is built from symbols, ε, union |, concatenation and star * (and +, ? as shorthand). Thompson's construction turns each piece into a small NFA with one start and one end and joins them with ε moves. Going back, state elimination removes the states of an automaton one at a time, labelling the edges with ever larger expressions, until one edge from start to end is left (Kleene's theorem, both directions).

import { toRegex } from "@ratmath/logic";

toRegex(even);    // "b*|b*a((b|ab*a)*)ab*"

The expressions that come back are correct but rarely the shortest; the tests check each against JavaScript's own regular expressions on every word up to length 7.

Complements and products

Complete a DFA with a dead state and swap accepting and rejecting states, and it accepts the complement. Run two DFAs side by side, and accepting when both do (or either, or the first and not the second) gives intersection, union and difference. Regular languages are closed under all of these.

import { complement, product } from "@ratmath/logic";

product(even, n, "and").words(5);   // ["aabb", "ababb", "baabb"]
complement(even).words(3);          // ["a", "ab", "ba", "aaa", "abb", "bab", "bba"]

Reference

ExportWhat it does
automaton({ states, alphabet, start, accept, delta })An Automaton: run(word) with a trace, accepts, words(maxLength), isDFA, isComplete, toString().
toDFA(a), complete(a, alphabet)The subset construction; every move defined, with a dead state ∅.
minimizeAutomaton(a)The smallest DFA, with states named q0, q1, …
equivalentAutomata(a, b)Automata or regular expressions. { equivalent }, with a shortest word when not.
complement(a, alphabet), product(a, b, op)op: "and", "or" or "minus".
parseRegex(text), regexToNFA(regex), toRegex(a)Regular expressions to automata (Thompson) and back (state elimination).
Try itEdit the code and run it with @ratmath/logic and Core's Rational in scope. The last line's value is shown; PRINT(…) adds lines, TEX(…) typesets math.
Ctrl/⌘ + Enter runs