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.
With commq(x,y):=∣Qq(x)∩Qq(y)∣ the number of common q-grams:
Corollary 1 (ED).
commq(x,y)≥max{∣Qq(x)∣,∣Qq(y)∣}−q⋅ED(x,y)
Corollary 2 (PED).
commq(x,y)≥∣Qq(x)∣−q⋅PED(x,y)
Both also hold for consistently padded strings, with ∣Qq∣ counted on the padded strings (see Q-Gram > Padding).
ProofProof of the lemma
ED(x,y)=1: one edit operation changes at most the q q-grams that overlap the edited position, so ∣Qq(x)∖Qq(y)∣≤q.
ED(x,y)=k: take an optimal sequence x=x0→x1→⋯→xk=y with ED(xi−1,xi)=1. For any sets A0,…,Ak,
A0∖Ak⊆i=1⋃k(Ai−1∖Ai):
for a∈A0∖Ak let i≥1 be the smallest index with a∈/Ai; then a∈Ai−1∖Ai. With Ai:=Qq(xi):
∣Qq(x)∖Qq(y)∣≤i=1∑k∣Qq(xi−1)∖Qq(xi)∣≤k⋅q.ProofProof of Corollary 1
∣A∖B∣=∣A∣−∣A∩B∣, so the lemma gives ∣Qq(x)∣−commq(x,y)≤q⋅ED(x,y). Since ED is symmetric, the lemma for (y,x) gives ∣Qq(y)∣−commq(x,y)≤q⋅ED(x,y). Together: the max.
Intuition: similar strings share many q-grams; more precisely, each edit operation “affects” at most q 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.
ED filter: given x and δ, every y with ED(x,y)≤δ satisfies commq(x,y)≥max{∣Qq(x)∣,∣Qq(y)∣}−q⋅δ; if not, ED(x,y)>δ without computing it.
Corollary 2 is the filter for fuzzy prefix search (Fuzzy Search): given the query x and threshold δ,
commq(x,y)<∣Qq(x)∣−q⋅δ⟹PED(x,y)>δ,
so y can be pruned without computing PED(x,y). Only the survivors are verified with the Prefix Edit Distance. With left-only padding, ∣Qq(x′′)∣=∣x∣, so the threshold is ∣x∣−qδ. The counts commq(x,y) for all y at once come from the Q-Gram Index.
ProofProof of Corollary 2
PED(x,y)=ED(x,y′) for some prefix y′ of y. By the lemma, ∣Qq(x)∣−commq(x,y′)≤q⋅ED(x,y′)=q⋅PED(x,y). Every q-gram of y′ is a q-gram of y (Qq(y)⊇Qq(y′)), so commq(x,y)≥commq(x,y′).
Same strings with q=3: ∣Q3(x)∣=8, ∣Q3(y)∣=7, common {ree,urg,rge,ger}, so 4≥8−3⋅2=2.
Corollary 2: PED(bree,freeburger)=1 and comm2=∣{re,ee}∣=2≥3−2⋅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, so 3≥7−3⋅ED(x,y), i.e. ED(x,y)≥4/3, hence ED(x,y)≥2.