Algorithm Search⌘K

Proofs: State elimination

These are the human proofs of State elimination, at full length.

State elimination returns an expression for the language of the automaton full

This proof shows: correctness.

Statement. Let N=(Q,Σ,δ,q0,F)N = (Q, \Sigma, \delta, q_0, F) be a finite automaton with nn states and tt moves, some of which may be ε\varepsilon-moves. For every order of removal, STATE-ELIMINATION(N)(N) returns a regular expression rr with L(r)=L(N)L(r) = L(N). With shared labels it runs in O(n3+t)O(n^3 + t) time.

Setup. Let N′N' be NN with the two new states ss and ff and the moves s→εq0s \xrightarrow{\varepsilon} q_0 and q→εfq \xrightarrow{\varepsilon} f for q∈Fq \in F of line L1. A word ww is in L(N)L(N) if and only if some path of N′N' from ss to ff reads ww: such a path is s→q0s \to q_0, then a path of NN from q0q_0 to some q∈Fq \in F, then q→fq \to f, and the two new moves read nothing. Let EE be the set of states eliminated so far, a subset of QQ. For remaining states p,qp, q (in Q∖EQ \setminus E or in {s,f}\{s, f\}), let PE(p,q)P_E(p, q) be the set of words read by paths of N′N' from pp to qq that have at least one move and whose inner states, all states visited strictly between the first and the last, lie in EE.

Simplifications. The constructors used in lines L2, L4 and L7 apply only the laws ∅∣r=r\emptyset \mid r = r, r∣r=rr \mid r = r (when both sides are the same stored expression), ∅r=r∅=∅\emptyset r = r \emptyset = \emptyset, εr=rε=r\varepsilon r = r \varepsilon = r, ∅∗=ε∗=ε\emptyset^* = \varepsilon^* = \varepsilon and (r∗)∗=r∗(r^*)^* = r^*. Each holds as an equality of languages, so we can reason about the languages of the labels and ignore the simplifications.

Invariant. Before each pass of line L3 and after the last one, L(R[p][q])=PE(p,q)L(R[p][q]) = P_E(p, q) for all remaining p,qp, q, where a missing label stands for ∅\emptyset.

Base. Before the first pass, E=∅E = \emptyset, so a path counted by P∅(p,q)P_\emptyset(p, q) has no inner state and is a single move from pp to qq. Lines L1 and L2 set R[p][q]R[p][q] to the union of the labels of these moves, which is exactly P∅(p,q)P_\emptyset(p, q).

Step. Suppose the invariant holds for EE, and the pass removes kk; let E′=E∪{k}E' = E \cup \{k\}. Take remaining states p,q≠kp, q \neq k; they may be equal. Consider a path π\pi from pp to qq with at least one move and inner states in E′E'.

  • If kk is not an inner state of π\pi, then π\pi is counted by PE(p,q)P_E(p, q).
  • Otherwise, cut π\pi at each visit to kk. The pieces are: a path from pp to kk, then j≥0j \ge 0 paths from kk to kk, then a path from kk to qq. Each piece has at least one move, since it joins two distinct visits or a visit to an end point, and its inner states lie in EE, since all visits to kk are cut points. So the word of π\pi is in PE(p,k) PE(k,k)j PE(k,q)P_E(p, k) \, P_E(k, k)^j \, P_E(k, q).

Conversely, every word of PE(p,q)P_E(p, q) is read by a path with inner states in E⊆E′E \subseteq E', and every word in PE(p,k) PE(k,k)j PE(k,q)P_E(p, k) \, P_E(k, k)^j \, P_E(k, q) is read by the concatenation of the corresponding paths, which goes from pp to qq, has at least one move, and has inner states in E∪{k}=E′E \cup \{k\} = E'. Hence

PE′(p,q)=PE(p,q)∪PE(p,k) PE(k,k)∗ PE(k,q).P_{E'}(p, q) = P_E(p, q) \cup P_E(p, k)\, P_E(k, k)^*\, P_E(k, q).

By the invariant for EE, the right side is L(R[p][q]∣R[p][k]⋅(R[k][k])∗⋅R[k][q])L(R[p][q] \mid R[p][k] \cdot (R[k][k])^* \cdot R[k][q]). Line L4 sets LL to (R[k][k])∗(R[k][k])^*, and line L7 assigns this expression to R[p][q]R[p][q] for every pp with R[p][k]≠∅R[p][k] \neq \emptyset and every qq with R[k][q]≠∅R[k][q] \neq \emptyset. For the other pairs the second term is ∅\emptyset, and R[p][q]R[p][q] correctly stays as it is. All updates in the pass read R[p][k]R[p][k], R[k][k]R[k][k] and R[k][q]R[k][q], which the pass does not change, because p,q≠kp, q \neq k; so the order of the updates inside the pass does not matter. Line L8 removes kk, which is no longer a remaining state. The invariant holds for E′E'.

Conclusion. After the last pass, E=QE = Q and the remaining states are ss and ff. Every path of N′N' from ss to ff has at least one move, since s≠fs \neq f, and its inner states are in QQ, since ss has no incoming moves and ff has no outgoing moves. So L(R[s][f])=PQ(s,f)=L(N)L(R[s][f]) = P_Q(s, f) = L(N), and line L9 returns a correct expression. □\square

Running time. Lines L1 and L2 cost O(n+t)O(n + t), building at most ∣F∣+t+1|F| + t + 1 labels with O(1)O(1) work each. The pass that removes kk runs the inner loop for at most in(k)⋅out(k)≤(n+1)2\mathrm{in}(k) \cdot \mathrm{out}(k) \le (n + 1)^2 pairs, and each update creates O(1)O(1) new expression nodes that point to existing labels. Line L8 costs O(in(k)+out(k))O(\mathrm{in}(k) + \mathrm{out}(k)). With the labels kept in hash tables per state, every operation is O(1)O(1) expected, or O(1)O(1) worst case with arrays of size n+2n + 2 per state. So nn passes cost O(n3)O(n^3), and the total is O(n3+t)O(n^3 + t). □\square

Back to State elimination