Algorithm Search⌘K

Proofs: Galil-Park algorithm

These are the human proofs of Galil-Park algorithm, at full length.

The Galil-Park algorithm computes the furthest rows of the diagonals full

This proof shows: correctness.

Indices are 0-based, ∣P∣=m|P| = m, ∣T∣=n|T| = n, k≥0k \ge 0. Let CC be the Sellers table and L[d][e]L[d][e] the furthest row ii of diagonal dd with C[i][i+d]≤eC[i][i + d] \le e, or −∞-\infty. Write LCE(r,x)\mathrm{LCE}(r, x) for the length of the longest common prefix of P[r…m)P[r \ldots m) and T[x…n)T[x \ldots n).

Fact (Landau-Vishkin). For 0≤e≤k0 \le e \le k and max⁡(−e,−m)≤d≤n\max(-e, -m) \le d \le n, let r0r_0 be the value of line L5 computed from the true values L[d][e−1]L[d][e-1], L[d−1][e−1]L[d-1][e-1] and L[d+1][e−1]L[d+1][e-1]. Then L[d][e]=r0+LCE(r0,r0+d)L[d][e] = r_0 + \mathrm{LCE}(r_0, r_0 + d). For d<−ed < -e or d<−md < -m, L[d][e]=−∞L[d][e] = -\infty.

This is Lemma 4 of the Landau-Vishkin correctness proof, whose induction uses only these three values. The Galil-Park loop visits c=0,…,n+kc = 0, \ldots, n + k with c=d+ec = d + e. For c≥0c \ge 0 and 0≤e≤k0 \le e \le k, d=c−e≥−ed = c - e \ge -e, so the skipped entries are exactly those with d>nd > n (no cells) or d<−md < -m. Entries with c<0c < 0 have d<−ed < -e and are −∞-\infty. The inputs L[d][e−1]L[d][e-1], L[d−1][e−1]L[d-1][e-1] and L[d+1][e−1]L[d+1][e-1] lie on the anti-diagonals c−1c - 1, c−2c - 2 and cc, and the last one has the smaller error count e−1e - 1, so all three are computed before (d,e)(d, e).

A triple (q,y,f)(q, y, f) is maximal when T[q…q+f)=P[y…y+f)T[q \ldots q + f) = P[y \ldots y + f) and (q+f=nq + f = n or y+f=my + f = m or T[q+f]≠P[y+f]T[q + f] \ne P[y + f]). Equivalently, LCE(y,q)=f\mathrm{LCE}(y, q) = f.

Lemma 1. Assume that all triples in RR are maximal when an extension starts at line L6. Then the loop of lines L7 to L11 ends with r−r0=LCE(r0,x0)r - r_0 = \mathrm{LCE}(r_0, x_0), where x0=r0+dx_0 = r_0 + d, and the triple (x0,r0,r−r0)(x_0, r_0, r - r_0) is maximal.

Proof. The loop keeps the invariant x−r=dx - r = d and P[r0…r)=T[x0…x)P[r_0 \ldots r) = T[x_0 \ldots x). It holds at the start. We show that each step keeps it, and that each stop happens at a position where the extension cannot go on: r=mr = m, x=nx = n, or P[r]≠T[x]P[r] \ne T[x], after the step's increment.

  • Line L8 and line L11 compare P[r]P[r] with T[x]T[x]. On equality both indices grow by 1 and the invariant holds. On inequality the loop stops with P[r]≠T[x]P[r] \ne T[x].
  • Line L9 uses a triple (q,y,f)(q, y, f) of RR with q≤x<q+fq \le x < q + f. Let y′=y+x−qy' = y + x - q and h=q+f−x≥1h = q + f - x \ge 1. Maximality gives T[x…x+h)=P[y′…y′+h)T[x \ldots x + h) = P[y' \ldots y' + h). By definition, g=PP[r][y′]g = PP[r][y'] is the length of the longest common prefix of P[r…)P[r \ldots) and P[y′…)P[y' \ldots).
    • If g<hg < h: then P[r…r+g)=P[y′…y′+g)=T[x…x+g)P[r \ldots r + g) = P[y' \ldots y' + g) = T[x \ldots x + g). Since g<h≤m−y′g < h \le m - y', the position y′+gy' + g lies in PP. So either r+g=mr + g = m, or P[r+g]≠P[y′+g]=T[x+g]P[r + g] \ne P[y' + g] = T[x + g]. The loop stops with r+gr + g and x+gx + g, at the end of PP or at a difference.
    • If g>hg > h: then P[r…r+h)=P[y′…y′+h)=T[x…x+h)P[r \ldots r + h) = P[y' \ldots y' + h) = T[x \ldots x + h), and P[r+h]=P[y′+h]P[r + h] = P[y' + h] since h<gh < g. If y+f=my + f = m, then y′+h=my' + h = m and g≤m−y′=hg \le m - y' = h, a contradiction, so y+f<my + f < m. Maximality then gives x+h=q+f=nx + h = q + f = n, or T[x+h]≠P[y′+h]=P[r+h]T[x + h] \ne P[y' + h] = P[r + h]. The loop stops with r+hr + h and x+hx + h, at the end of TT or at a difference.
    • If g=hg = h: the equal prefixes give P[r…r+g)=T[x…x+g)P[r \ldots r + g) = T[x \ldots x + g), and both indices grow by gg. The invariant holds.
  • The condition of line L7 stops the loop when r=mr = m or x=nx = n.

Each step increases rr by at least 1 or stops, so the loop ends. At the end, the invariant and the stop condition give r−r0=LCE(r0,x0)r - r_0 = \mathrm{LCE}(r_0, x_0), and the same two facts say that (x0,r0,r−r0)(x_0, r_0, r - r_0) is maximal. □\square

Lemma 2. At every time, each triple in RR is maximal, and each stored value L[d][e]L[d][e] is the true one.

Proof. Induct over the steps of line L4. At the start RR is empty. Consider the entry (d,e)(d, e), with all earlier entries true and all triples maximal. By the order shown above, line L5 reads true inputs, so r0r_0 is the value of the Fact. By Lemma 1, line L12 stores r0+LCE(r0,r0+d)=L[d][e]r_0 + \mathrm{LCE}(r_0, r_0 + d) = L[d][e], and a new triple put into R[e]R[e] is maximal. The arrays of three anti-diagonals keep the entries of c−1c - 1 and c−2c - 2 until cc is done, and skipped entries hold −∞-\infty, so line L5 reads the right values. □\square

Theorem. GALIL-PARK(P,T,k)(P, T, k) reports, in increasing order, exactly the positions j∈{0,…,n}j \in \{0, \ldots, n\} such that d(P,T[h…j))≤kd(P, T[h \ldots j)) \le k for some h≤jh \le j.

Proof. As in the Landau-Vishkin proof, jj is a match exactly when L[j−m][k]=mL[j - m][k] = m. By Lemma 2 the values stored for e=ke = k are true, and line L13 tests each diagonal d=c−kd = c - k with −m≤d≤n-m \le d \le n once. The entries with d<−md < -m have no cells, and entries with d>n−md > n - m cannot reach row mm because r≤n−d<mr \le n - d < m. Line L13 runs in increasing order of cc, so in increasing order of dd and of j=m+dj = m + d. ■\blacksquare

Back to Galil-Park algorithm