Algorithm Search⌘K

Proofs: Valiant's algorithm

These are the human proofs of Valiant's algorithm, at full length.

Valiant's algorithm fills every cell of the recognition table with the right set of nonterminals, and it stops full

This proof shows: correctness, termination.

We use the notation of the context-free grammar page and the pseudocode of the page: VALIANT (lines L1 to L4), COMPUTE (lines L5 to L7) and COMPLETE (lines L8 to L16). The grammar GG is in Chomsky normal form: every production is A→BCA \to BC with nonterminals BB and CC, or A→aA \to a with a terminal aa, and the only other production allowed is S→εS \to \varepsilon, when SS is on no right side. The input is w=a1⋯anw = a_1 \cdots a_n, and NN is the least power of 2 with N≥n+1N \ge n + 1.

The target values

For 0≤i<j≤N0 \le i < j \le N let T∗[i,j]T^*[i, j] be the set of nonterminals AA with A⇒∗ai+1⋯ajA \Rightarrow^* a_{i+1} \cdots a_j, where an+1,…,aNa_{n+1}, \ldots, a_N are a padding symbol that is not in GG. For a set KK of positions let

PK[i,j]={(B,C):there is k∈K with i<k<j, B∈T∗[i,k], C∈T∗[k,j]},P_K[i, j] = \{ (B, C) : \text{there is } k \in K \text{ with } i < k < j,\ B \in T^*[i, k],\ C \in T^*[k, j] \},

and write P∗[i,j]P^*[i, j] for PK[i,j]P_K[i, j] with KK the set of all positions: the pairs that can be split anywhere.

Lemma 1. (a) T∗[i,i+1]={A:A→ai+1 is in G}T^*[i, i+1] = \{ A : A \to a_{i+1} \text{ is in } G \}. (b) If j≥i+2j \ge i + 2, then T∗[i,j]={A:A→BC is in G, (B,C)∈P∗[i,j]}T^*[i, j] = \{ A : A \to BC \text{ is in } G,\ (B, C) \in P^*[i, j] \}. (c) T∗[i,j]=∅T^*[i, j] = \emptyset when j>nj > n.

Proof. In Chomsky normal form, a nonterminal other than a start symbol with S→εS \to \varepsilon derives only nonempty strings, and SS is on no right side. So in a derivation A⇒BC⇒∗xA \Rightarrow BC \Rightarrow^* x, both BB and CC derive nonempty parts of xx, and ∣x∣≥2|x| \ge 2. Hence a one-letter string comes only from a production A→aA \to a, which is (a). For ∣x∣≥2|x| \ge 2 the first step must be some A→BCA \to BC, and x=yzx = yz with B⇒∗yB \Rightarrow^* y and C⇒∗zC \Rightarrow^* z, both nonempty; with x=ai+1⋯ajx = a_{i+1} \cdots a_j this is a split point kk with i<k<ji < k < j, which is (b). For (c), no derived string contains the padding symbol. □\square

Line L1 sets the terminal cells T[i,i+1]T[i, i+1] to the value of Lemma 1(a) for i<ni < n and leaves them empty for i≥ni \ge n, which also agrees with Lemma 1(a), since no production has the padding symbol. All other cells of TT and all cells of PP start empty.

The block products

For blocks of cells, X⋅YX \cdot Y is the matrix whose cell [i,j][i, j] is the set of pairs (B,C)(B, C) with B∈X[i,k]B \in X[i, k] and C∈Y[k,j]C \in Y[k, j] for some kk in the shared index range. The implementation computes it with one Boolean product for each pair (B,C)(B, C) that is the right side of a production, which gives the same pairs.

A product T[r0:r1,k0:k1]⋅T[k0:k1,c0:c1]T[r_0{:}r_1, k_0{:}k_1] \cdot T[k_0{:}k_1, c_0{:}c_1] added into P[r0:r1,c0:c1]P[r_0{:}r_1, c_0{:}c_1] adds to each cell (i,j)(i, j) of the block exactly the pairs of the splits k∈[k0,k1)k \in [k_0, k_1), computed from the current TT. In every product of lines L13 to L15, r1≤k0r_1 \le k_0 and k1≤c0k_1 \le c_0, so i<k<ji < k < j holds for all ii, kk and jj of the blocks. If the cells T[i,k]T[i, k] and T[k,j]T[k, j] that it reads already hold their final values T∗T^*, the product adds exactly P[k0,k1)[i,j]P_{[k_0, k_1)}[i, j].

Lemma 2: COMPLETE

Consider a call COMPLETE(ℓ,m,ℓ′,m′)(\ell, m, \ell', m') with m−ℓ=m′−ℓ′m - \ell = m' - \ell' a power of 2 and m≤ℓ′m \le \ell'. Its rectangle is the set of cells (i,j)(i, j) with ℓ≤i<m\ell \le i < m and ℓ′≤j<m′\ell' \le j < m'. Suppose that, when the call starts,

  1. T[i,j]=T∗[i,j]T[i, j] = T^*[i, j] for all ℓ≤i<j<m\ell \le i < j < m (the triangle of rows) and for all ℓ′≤i<j<m′\ell' \le i < j < m' (the triangle of columns);
  2. P[i,j]=P[m,ℓ′)[i,j]P[i, j] = P_{[m, \ell')}[i, j] for every cell of the rectangle (the splits in the gap between the two ranges are in);
  3. every cell (i,j)(i, j) of the rectangle with j≥i+2j \ge i + 2 is still empty in TT.

Then, when the call returns, T[i,j]=T∗[i,j]T[i, j] = T^*[i, j] and P[i,j]=P∗[i,j]P[i, j] = P^*[i, j] for every cell of the rectangle, and no cell outside the rectangle has changed, in TT or in PP.

Proof. By induction on s=m−ℓs = m - \ell.

Base, s=1s = 1. The rectangle is the single cell (ℓ,ℓ′)(\ell, \ell'). Its splits kk with ℓ<k<ℓ′\ell < k < \ell' all lie in [m,ℓ′)=[ℓ+1,ℓ′)[m, \ell') = [\ell + 1, \ell'), so condition 2 says P[ℓ,ℓ′]=P∗[ℓ,ℓ′]P[\ell, \ell'] = P^*[\ell, \ell']. If ℓ′=ℓ+1\ell' = \ell + 1, the cell is a terminal cell, line L9 does nothing, and TT is right by line L1. Otherwise line L9 sets T[ℓ,ℓ′]T[\ell, \ell'], empty by condition 3, to {A:A→BC, (B,C)∈P∗[ℓ,ℓ′]}\{ A : A \to BC,\ (B, C) \in P^*[\ell, \ell'] \}, which is T∗[ℓ,ℓ′]T^*[\ell, \ell'] by Lemma 1(b). Nothing else changes.

Step, s≥2s \ge 2. Let a=(ℓ+m)/2a = (\ell + m)/2 and b=(ℓ′+m′)/2b = (\ell' + m')/2, as in line L11. The rectangle splits into four quadrants of side s/2s/2:

  • Q1Q_1: rows [a,m)[a, m), columns [ℓ′,b)[\ell', b), the quadrant nearest the diagonal;
  • Q2Q_2: rows [ℓ,a)[\ell, a), columns [ℓ′,b)[\ell', b);
  • Q3Q_3: rows [a,m)[a, m), columns [b,m′)[b, m');
  • Q4Q_4: rows [ℓ,a)[\ell, a), columns [b,m′)[b, m'), the farthest one.

We check the three conditions for each of the four recursive calls, in order. The triangles that the calls need lie inside the triangles of condition 1 or inside quadrants already finished, and condition 3 holds because only the call for a quadrant writes TT in that quadrant.

  • Line L12, COMPLETE(a,m,ℓ′,b)(a, m, \ell', b) for Q1Q_1. Its triangles [a,m)[a, m) and [ℓ′,b)[\ell', b) are parts of the triangles of condition 1. Its gap is [m,ℓ′)[m, \ell'), the same as ours, so condition 2 holds as it is. By induction, Q1Q_1 is final afterwards.
  • Line L13, then COMPLETE(ℓ,a,ℓ′,b)(\ell, a, \ell', b) for Q2Q_2. The gap of Q2Q_2 is [a,ℓ′)=[a,m)∪[m,ℓ′)[a, \ell') = [a, m) \cup [m, \ell'). The cells of Q2Q_2 have the splits [m,ℓ′)[m, \ell') by condition 2, and line L13 adds the splits k∈[a,m)k \in [a, m), with T[i,k]T[i, k] for ℓ≤i<a≤k<m\ell \le i < a \le k < m in the triangle of rows (final by condition 1) and T[k,j]T[k, j] in Q1Q_1 (final). So P=P[a,ℓ′)P = P_{[a, \ell')} on Q2Q_2. The triangles are [ℓ,a)[\ell, a), inside the triangle of rows, and [ℓ′,b)[\ell', b). By induction, Q2Q_2 is final.
  • Line L14, then COMPLETE(a,m,b,m′)(a, m, b, m') for Q3Q_3. The gap of Q3Q_3 is [m,b)=[m,ℓ′)∪[ℓ′,b)[m, b) = [m, \ell') \cup [\ell', b). Line L14 adds the splits k∈[ℓ′,b)k \in [\ell', b), with T[i,k]T[i, k] in Q1Q_1 and T[k,j]T[k, j] for ℓ′≤k<b≤j<m′\ell' \le k < b \le j < m' in the triangle of columns, all final. The triangles are [a,m)[a, m) and [b,m′)[b, m'). By induction, Q3Q_3 is final.
  • Line L15, then line L16, COMPLETE(ℓ,a,b,m′)(\ell, a, b, m') for Q4Q_4. The gap of Q4Q_4 is [a,b)=[a,m)∪[m,ℓ′)∪[ℓ′,b)[a, b) = [a, m) \cup [m, \ell') \cup [\ell', b). Line L15 adds k∈[a,m)k \in [a, m), reading T[i,k]T[i, k] in the triangle of rows and T[k,j]T[k, j] in Q3Q_3, and k∈[ℓ′,b)k \in [\ell', b), reading T[i,k]T[i, k] in Q2Q_2 and T[k,j]T[k, j] in the triangle of columns. All are final. The triangles are [ℓ,a)[\ell, a) and [b,m′)[b, m'). By induction, Q4Q_4 is final.

Each cell of the rectangle is in exactly one quadrant, so it is final at the end, and its PP cell equals P∗P^* by the induction hypothesis. The products write only into quadrants of our rectangle, and the recursive calls change nothing outside their own quadrants, so nothing outside the rectangle changes. □\square

Lemma 3: COMPUTE

Consider a call COMPUTE(ℓ,m)(\ell, m) with m−ℓm - \ell a power of 2, and suppose that, when it starts, every cell (i,j)(i, j) with ℓ≤i<j<m\ell \le i < j < m has P[i,j]=∅P[i, j] = \emptyset and is empty in TT unless it is a terminal cell. Then, when it returns, T[i,j]=T∗[i,j]T[i, j] = T^*[i, j] for all ℓ≤i<j<m\ell \le i < j < m, and no cell outside this triangle has changed.

Proof. By induction on m−ℓm - \ell. If m−ℓ=1m - \ell = 1, there is no cell and line L5 does nothing. Otherwise let h=(ℓ+m)/2h = (\ell + m)/2. Line L6 fills the triangles [ℓ,h)[\ell, h) and [h,m)[h, m), by induction, without touching the rectangle of rows [ℓ,h)[\ell, h) and columns [h,m)[h, m). On that rectangle, PP is still empty, which is P[h,h)P_{[h, h)} because the gap is empty, and the non-terminal cells are empty in TT. So line L7 meets the conditions of Lemma 2 and fills the rectangle. The two triangles and the rectangle make up the whole triangle. □\square

Theorem

VALIANT(G,w)(G, w) stops and returns true exactly when w∈L(G)w \in L(G). When it stops, T[i,j]=T∗[i,j]T[i, j] = T^*[i, j] for all 0≤i<j≤N0 \le i < j \le N, so the table holds every nonterminal that derives every substring of ww.

Proof. After line L1, the conditions of Lemma 3 hold for COMPUTE(0,N)(0, N), so after line L2 every cell is final. If n=0n = 0, then w=εw = \varepsilon, and in Chomsky normal form S⇒∗εS \Rightarrow^* \varepsilon only by the production S→εS \to \varepsilon, which line L3 tests. If n≥1n \ge 1, line L4 returns whether S∈T∗[0,n]S \in T^*[0, n], that is, whether S⇒∗wS \Rightarrow^* w.

Termination: every recursive call has half the side of its caller, so the depth of the recursion is at most log⁡2N\log_2 N for COMPUTE plus log⁡2N\log_2 N for COMPLETE, and every call makes at most four recursive calls and a bounded number of block products. □\square

Remark: the number of products

By induction on ss, a call COMPLETE of side ss makes s2/t2s^2 / t^2 block products of side tt for every power of 2 with t≤s/2t \le s/2: it makes 4 of side s/2s/2 itself, and its four calls of side s/2s/2 make 4(s/2)2/t2=s2/t24 (s/2)^2 / t^2 = s^2 / t^2 of each smaller side. COMPUTE(0,N)(0, N) makes 2d2^d calls of COMPLETE of side N/2d+1N / 2^{d+1} for d=0,1,…d = 0, 1, \ldots, so the number of block products of side tt is

∑d : N/2d+1≥2t2dN24d+1t2=Nt(N2t−1).\sum_{d \,:\, N / 2^{d+1} \ge 2t} 2^d \frac{N^2}{4^{d+1} t^2} = \frac{N}{t} \left( \frac{N}{2t} - 1 \right).

Each block product costs one Boolean matrix product of side tt per pair (B,C)(B, C), which gives the time bound of the page.

Back to Valiant's algorithm