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 be a finite automaton with states and moves, some of which may be -moves. For every order of removal, STATE-ELIMINATION returns a regular expression with . With shared labels it runs in time.
Setup. Let be with the two new states and and the moves and for of line L1. A word is in if and only if some path of from to reads : such a path is , then a path of from to some , then , and the two new moves read nothing. Let be the set of states eliminated so far, a subset of . For remaining states (in or in ), let be the set of words read by paths of from to that have at least one move and whose inner states, all states visited strictly between the first and the last, lie in .
Simplifications. The constructors used in lines L2, L4 and L7 apply only the laws , (when both sides are the same stored expression), , , and . 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, for all remaining , where a missing label stands for .
Base. Before the first pass, , so a path counted by has no inner state and is a single move from to . Lines L1 and L2 set to the union of the labels of these moves, which is exactly .
Step. Suppose the invariant holds for , and the pass removes ; let . Take remaining states ; they may be equal. Consider a path from to with at least one move and inner states in .
- If is not an inner state of , then is counted by .
- Otherwise, cut at each visit to . The pieces are: a path from to , then paths from to , then a path from to . 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 , since all visits to are cut points. So the word of is in .
Conversely, every word of is read by a path with inner states in , and every word in is read by the concatenation of the corresponding paths, which goes from to , has at least one move, and has inner states in . Hence
By the invariant for , the right side is . Line L4 sets to , and line L7 assigns this expression to for every with and every with . For the other pairs the second term is , and correctly stays as it is. All updates in the pass read , and , which the pass does not change, because ; so the order of the updates inside the pass does not matter. Line L8 removes , which is no longer a remaining state. The invariant holds for .
Conclusion. After the last pass, and the remaining states are and . Every path of from to has at least one move, since , and its inner states are in , since has no incoming moves and has no outgoing moves. So , and line L9 returns a correct expression.
Running time. Lines L1 and L2 cost , building at most labels with work each. The pass that removes runs the inner loop for at most pairs, and each update creates new expression nodes that point to existing labels. Line L8 costs . With the labels kept in hash tables per state, every operation is expected, or worst case with arrays of size per state. So passes cost , and the total is .