Algorithm Search⌘K

Proofs: Bit-parallel regex matching

These are the human proofs of Bit-parallel regex matching, at full length.

The bit-parallel simulation accepts exactly the language of the expression full

This proof shows: correctness, termination.

Let GG be the Glushkov automaton of rr, with states 0,…,k0, \ldots, k, moves (p,sym(q),q)(p, \mathrm{sym}(q), q) for q∈Follow(p)q \in \mathrm{Follow}(p), and accepting set FF. By the correctness of the Glushkov automaton, GG accepts exactly L(r)L(r). Identify a set D⊆{0,…,k}D \subseteq \{0, \ldots, k\} with the integer ∑p∈D2p\sum_{p \in D} 2^p, so that ∣\mid is union, &\& is intersection, and bit pp of the integer is set if and only if p∈Dp \in D. For a set DD write Φ(D)=⋃p∈DFollow(p)\Phi(D) = \bigcup_{p \in D} \mathrm{Follow}(p), and for a symbol cc write δ(D,c)={q:(p,c,q) is a move of G for some p∈D}\delta(D, c) = \{q : (p, c, q) \text{ is a move of } G \text{ for some } p \in D\}.

Lemma 1 (homogeneity). δ(D,c)=Φ(D)∩B[c]\delta(D, c) = \Phi(D) \cap B[c], where B[c]={q≥1:sym(q)=c}B[c] = \{q \ge 1 : \mathrm{sym}(q) = c\} is the set built at line L2.

Proof. A move (p,c,q)(p, c, q) exists if and only if q∈Follow(p)q \in \mathrm{Follow}(p) and sym(q)=c\mathrm{sym}(q) = c. The positions in a Follow set are at least 1, and sym(q)=c\mathrm{sym}(q) = c for q≥1q \ge 1 means q∈B[c]q \in B[c]. So q∈δ(D,c)q \in \delta(D, c) if and only if q∈Φ(D)q \in \Phi(D) and q∈B[c]q \in B[c]. □\square

Lemma 2 (tables). After line L5, for each chunk jj and each vv with 0≤v<2b0 \le v < 2^b, Tj[v]=Φ({jb+i:bit i of v is set})T_j[v] = \Phi(\{jb + i : \text{bit } i \text{ of } v \text{ is set}\}), with Follow(p)=∅\mathrm{Follow}(p) = \emptyset for p>kp > k.

Proof. By induction on vv. For v=0v = 0, Tj[0]=0=Φ(∅)T_j[0] = 0 = \Phi(\emptyset). For v>0v > 0, let ii be the index of the lowest set bit of vv and v′=v−2i<vv' = v - 2^i < v. Line L5 sets Tj[v]=Tj[v′]∣Follow(jb+i)T_j[v] = T_j[v'] \mid \mathrm{Follow}(jb + i), where Follow(p)=∅\mathrm{Follow}(p) = \emptyset for p>kp > k. By induction Tj[v′]=Φ(S′)T_j[v'] = \Phi(S') for the set S′S' of v′v', and the set of vv is S′∪{jb+i}S' \cup \{jb + i\}, so Tj[v]=Φ(S′)∪Follow(jb+i)=Φ(S′∪{jb+i})T_j[v] = \Phi(S') \cup \mathrm{Follow}(jb + i) = \Phi(S' \cup \{jb + i\}). □\square

Lemma 3 (one step). For every D⊆{0,…,k}D \subseteq \{0, \ldots, k\} and symbol cc, lines L8 and L9 compute δ(D,c)\delta(D, c).

Proof. For j=0,…,g−1j = 0, \ldots, g - 1, the value (D≫jb)&(2b−1)(D \gg jb) \mathbin{\&} (2^b - 1) is the integer vjv_j whose bit ii is set if and only if jb+i∈Djb + i \in D, so by Lemma 2, Tj[vj]=Φ(Dj)T_j[v_j] = \Phi(D_j) with Dj=D∩{jb,…,jb+b−1}D_j = D \cap \{jb, \ldots, jb + b - 1\}. Since gb≥k+1g b \ge k + 1, the sets D0,…,Dg−1D_0, \ldots, D_{g-1} cover DD, and Φ\Phi distributes over union, so E=Φ(D0)∪⋯∪Φ(Dg−1)=Φ(D)E = \Phi(D_0) \cup \cdots \cup \Phi(D_{g-1}) = \Phi(D) at line L8. Line L9 gives E∩B[c]=δ(D,c)E \cap B[c] = \delta(D, c) by Lemma 1. □\square

Invariant. After line L9 in round ii, D=δ∗({0},y1⋯yi)D = \delta^*(\{0\}, y_1 \cdots y_i), the set of states of GG that some run on y1⋯yiy_1 \cdots y_i from state 0 reaches.

Proof. Before the loop, D={0}=δ∗({0},ε)D = \{0\} = \delta^*(\{0\}, \varepsilon) by line L6. If D=δ∗({0},y1⋯yi−1)D = \delta^*(\{0\}, y_1 \cdots y_{i-1}) before round ii, Lemma 3 gives δ(D,yi)=δ∗({0},y1⋯yi)\delta(D, y_i) = \delta^*(\{0\}, y_1 \cdots y_i) after line L9. □\square

Termination. Line L5 runs g(2b−1)g(2^b - 1) rounds, the loop at line L7 at most nn rounds, and each round does gg lookups. So the algorithm stops.

Theorem. BITPARALLEL(r,y,b)(r, y, b) returns true if and only if y∈L(r)y \in L(r).

Proof. If line L10 returns false in round ii, then δ∗({0},y1⋯yi)=∅\delta^*(\{0\}, y_1 \cdots y_i) = \emptyset by the invariant; then δ∗({0},y)=δ∗(∅,yi+1⋯yn)=∅\delta^*(\{0\}, y) = \delta^*(\emptyset, y_{i+1} \cdots y_n) = \emptyset contains no accepting state, and y∉L(G)=L(r)y \notin L(G) = L(r). Otherwise the loop ends with D=δ∗({0},y)D = \delta^*(\{0\}, y), and line L11 returns true if and only if this set meets FF, that is, if and only if GG accepts yy, that is, if and only if y∈L(r)y \in L(r). ■\blacksquare

The search variant. With D←δ(D,yi)∪{0}D \leftarrow \delta(D, y_i) \cup \{0\} in round ii, and D={0}D = \{0\} at the start, the same induction shows that after round ii, D=⋃s=0iδ∗({0},ys+1⋯yi)D = \bigcup_{s=0}^{i} \delta^*(\{0\}, y_{s+1} \cdots y_i): the runs that started before round ii are advanced by Lemma 3, and the added state 0 is the run of the empty suffix, with s=is = i. So DD meets FF after round ii if and only if some ys+1⋯yiy_{s+1} \cdots y_i with 0≤s≤i0 \le s \le i is in L(r)L(r).

Back to Bit-parallel regex matching