Proofs: NFA simulation
These are the human proofs of NFA simulation, at full length.
NFA simulation accepts exactly the strings of the NFA and stops in O(l(n + m)) steps full
This proof shows: correctness, termination.
Let be the input, with , and let . For , let be the set of states such that there is a path with , , and for .
A run of on is a sequence of moves that starts in and whose labels, in order, are with any number of labels before, between and after them. Let be the set of the last states of the runs on . By definition, if and only if .
Lemma 1 (CLOSE). On input , CLOSE stops after at most pops and returns .
Proof. A state is pushed on only at the moment it is added to , at line L9 or line L12, and states are never removed from . So each state is pushed at most once, there are at most pops, and the loop stops.
: the claim holds after line L9, since with paths of length 0. Line L12 adds only when for some ; a path from to followed by the move to is a path from to .
: each state that enters also enters , and the loop stops only when is empty, so each state of the final was popped at line L11, and line L12 then put all its -successors in . So the final contains and is closed under -moves. Take with a path . By induction on , : , and with gives .
Lemma 2 (one step). For each and , .
Proof. () Let be in the right side. Then there are , and a path of -moves from to . A run on that ends in , followed by the move on and the path to , is a run on that ends in .
() Let , with a run on that ends in . The run reads in exactly one move with a label in after it has read ; let this move go from to , so . The moves before it form a run on that ends in , so . The moves after it are -moves from to , so . Hence is in the right side.
Lemma 3 (invariant). If the algorithm reaches the test of line L2 after passes of the loop, then .
Proof. By induction on . For , line L1 sets by Lemma 1, and , since a run on uses only -moves. For the step, assume with before pass . Lines L3 to L5 make , and line L6 sets by Lemma 1. By Lemma 2 this is . If line L7 returns, the next test of line L2 is never reached, and the claim holds vacuously.
Theorem. NFA-SIMULATION stops and returns true if and only if .
Proof. The loop at line L2 makes at most passes, and each call of CLOSE stops by Lemma 1, so the algorithm stops.
If it returns false at line L7 in pass , then by Lemma 3 and the computation of pass . Each run on begins with a run on : the part of it up to and including the move that reads is a run on . That prefix would end in a state of , so there is no run on , and .
Otherwise, after all passes, by Lemma 3, and line L8 returns true if and only if , that is, if and only if .
Running time. In one pass, lines L4 and L5 read each -move of each state of once, at most moves, and has at most states. By Lemma 1, CLOSE pops at most states, and it reads the -moves of each popped state once, at most moves. With a boolean array for membership, each "add" and each test is . So a pass costs , and the passes cost . The space is the two sets, the stack and the array: .