Study Vault Search notes, aliases and full text
Graph
ConceptDBISAI-written · not yet proofread

Q-Gram Lemma

The q-gram lemma states |Q_q(x) \ Q_q(y)| ≤ q · ED(x, y), so strings with small edit distance (or prefix edit distance) must share many q-grams, which gives a lower bound used to filter fuzzy-search candidates.

Know these firstQ-GramEdit Distance

Formula#

With commq(x,y):=∣Qq(x)∩Qq(y)∣\mathrm{comm}_q(x, y) := |Q_q(x) \cap Q_q(y)| the number of common q-grams:

Corollary 1 (ED).

commq(x,y)≥max⁡{∣Qq(x)∣,∣Qq(y)∣}−q⋅ED(x,y)\mathrm{comm}_q(x, y) \ge \max\{|Q_q(x)|, |Q_q(y)|\} - q \cdot \mathrm{ED}(x, y)

Corollary 2 (PED).

commq(x,y)≥∣Qq(x)∣−q⋅PED(x,y)\mathrm{comm}_q(x, y) \ge |Q_q(x)| - q \cdot \mathrm{PED}(x, y)

Both also hold for consistently padded strings, with ∣Qq∣|Q_q| counted on the padded strings (see Q-Gram > Padding).

ProofProof of the lemma

ED(x,y)=1\mathrm{ED}(x, y) = 1: one edit operation changes at most the qq q-grams that overlap the edited position, so ∣Qq(x)∖Qq(y)∣≤q|Q_q(x) \setminus Q_q(y)| \le q.

ED(x,y)=k\mathrm{ED}(x, y) = k: take an optimal sequence x=x0→x1→⋯→xk=yx = x_0 \to x_1 \to \dots \to x_k = y with ED(xi−1,xi)=1\mathrm{ED}(x_{i-1}, x_i) = 1. For any sets A0,…,AkA_0, \dots, A_k,

A0∖Ak⊆⋃i=1k(Ai−1∖Ai):A_0 \setminus A_k \subseteq \bigcup_{i=1}^{k} (A_{i-1} \setminus A_i):

for a∈A0∖Aka \in A_0 \setminus A_k let i≥1i \ge 1 be the smallest index with a∉Aia \notin A_i; then a∈Ai−1∖Aia \in A_{i-1} \setminus A_i. With Ai:=Qq(xi)A_i := Q_q(x_i):

∣Qq(x)∖Qq(y)∣≤∑i=1k∣Qq(xi−1)∖Qq(xi)∣≤k⋅q.|Q_q(x) \setminus Q_q(y)| \le \sum_{i=1}^{k} |Q_q(x_{i-1}) \setminus Q_q(x_i)| \le k \cdot q.
ProofProof of Corollary 1

∣A∖B∣=∣A∣−∣A∩B∣|A \setminus B| = |A| - |A \cap B|, so the lemma gives ∣Qq(x)∣−commq(x,y)≤q⋅ED(x,y)|Q_q(x)| - \mathrm{comm}_q(x, y) \le q \cdot \mathrm{ED}(x, y). Since ED is symmetric, the lemma for (y,x)(y, x) gives ∣Qq(y)∣−commq(x,y)≤q⋅ED(x,y)|Q_q(y)| - \mathrm{comm}_q(x, y) \le q \cdot \mathrm{ED}(x, y). Together: the max.

Properties#

  • Intuition: similar strings share many q-grams; more precisely, each edit operation “affects” at most qq q-grams.
  • The bounds give necessary conditions only: a word that meets the bound may still be far away, so (P)ED must be verified; a word that misses it is certainly not a match (no false negatives).
  • Reading it backwards gives a lower bound on ED: ED(x,y)≥(max⁡{∣Qq(x)∣,∣Qq(y)∣}−commq(x,y))/q\mathrm{ED}(x, y) \ge \bigl(\max\{|Q_q(x)|, |Q_q(y)|\} - \mathrm{comm}_q(x, y)\bigr) / q.
  • ED filter: given xx and δ\delta, every yy with ED(x,y)≤δ\mathrm{ED}(x, y) \le \delta satisfies commq(x,y)≥max⁡{∣Qq(x)∣,∣Qq(y)∣}−q⋅δ\mathrm{comm}_q(x, y) \ge \max\{|Q_q(x)|, |Q_q(y)|\} - q \cdot \delta; if not, ED(x,y)>δ\mathrm{ED}(x, y) > \delta without computing it.

Application to PED Filtering#

Corollary 2 is the filter for fuzzy prefix search (Fuzzy Search): given the query xx and threshold δ\delta,

commq(x,y)<∣Qq(x)∣−q⋅δ  ⟹  PED(x,y)>δ,\mathrm{comm}_q(x, y) < |Q_q(x)| - q \cdot \delta \implies \mathrm{PED}(x, y) > \delta,

so yy can be pruned without computing PED(x,y)\mathrm{PED}(x, y). Only the survivors are verified with the Prefix Edit Distance. With left-only padding, ∣Qq(x′′)∣=∣x∣|Q_q(x'')| = |x|, so the threshold is ∣x∣−qδ|x| - q\delta. The counts commq(x,y)\mathrm{comm}_q(x, y) for all yy at once come from the Q-Gram Index.

ProofProof of Corollary 2

PED(x,y)=ED(x,y′)\mathrm{PED}(x, y) = \mathrm{ED}(x, y') for some prefix y′y' of yy. By the lemma, ∣Qq(x)∣−commq(x,y′)≤q⋅ED(x,y′)=q⋅PED(x,y)|Q_q(x)| - \mathrm{comm}_q(x, y') \le q \cdot \mathrm{ED}(x, y') = q \cdot \mathrm{PED}(x, y). Every q-gram of y′y' is a q-gram of yy (Qq(y)⊇Qq(y′)Q_q(y) \supseteq Q_q(y')), so commq(x,y)≥commq(x,y′)\mathrm{comm}_q(x, y) \ge \mathrm{comm}_q(x, y').

Example#

x=freeburgerx = \text{freeburger}, y=breeurgery = \text{breeurger}, q=2q = 2, ED(x,y)=2\mathrm{ED}(x, y) = 2 (replace f→b, delete b):

  • Q2(x)={fr,re,ee,eb,bu,ur,rg,ge,er}Q_2(x) = \{\text{fr}, \text{re}, \text{ee}, \text{eb}, \text{bu}, \text{ur}, \text{rg}, \text{ge}, \text{er}\} (9), Q2(y)={br,re,ee,eu,ur,rg,ge,er}Q_2(y) = \{\text{br}, \text{re}, \text{ee}, \text{eu}, \text{ur}, \text{rg}, \text{ge}, \text{er}\} (8).
  • Lemma: ∣Q2(x)∖Q2(y)∣=∣{fr,eb,bu}∣=3≤2⋅2=4|Q_2(x) \setminus Q_2(y)| = |\{\text{fr}, \text{eb}, \text{bu}\}| = 3 \le 2 \cdot 2 = 4.
  • Corollary 1: comm2(x,y)=6≥max⁡{9,8}−2⋅2=5\mathrm{comm}_2(x, y) = 6 \ge \max\{9, 8\} - 2 \cdot 2 = 5.
  • Same strings with q=3q = 3: ∣Q3(x)∣=8|Q_3(x)| = 8, ∣Q3(y)∣=7|Q_3(y)| = 7, common {ree,urg,rge,ger}\{\text{ree}, \text{urg}, \text{rge}, \text{ger}\}, so 4≥8−3⋅2=24 \ge 8 - 3 \cdot 2 = 2.

Corollary 2: PED(bree,freeburger)=1\mathrm{PED}(\text{bree}, \text{freeburger}) = 1 and comm2=∣{re,ee}∣=2≥3−2⋅1=1\mathrm{comm}_2 = |\{\text{re}, \text{ee}\}| = 2 \ge 3 - 2 \cdot 1 = 1.

Exam WS 22/23, Task 2.2: two strings of length 5 with 3 common 3-grams (padding on both sides) have ∣Q3(x′)∣=∣Q3(y′)∣=9−3+1=7|Q_3(x')| = |Q_3(y')| = 9 - 3 + 1 = 7, so 3≥7−3⋅ED(x,y)3 \ge 7 - 3 \cdot \mathrm{ED}(x, y), i.e. ED(x,y)≥4/3\mathrm{ED}(x, y) \ge 4/3, hence ED(x,y)≥2\mathrm{ED}(x, y) \ge 2.

Sources#

Search

Type In
↑ ↓ move ↵ open ⇥ next section Ctrl↵ results page Prefix # to search text only