Algorithm Search⌘K

Proofs: AllPairs

These are the human proofs of AllPairs, at full length.

All-Pairs reports exactly the pairs with dot product at least t full

This proof shows: correctness.

Let V=(x1,…,xn)V = (x_1, \dots, x_n) be vectors in R≥0d\mathbb{R}_{\ge 0}^d, t>0t > 0, and m[i]=max⁡z∈Vz[i]m[i] = \max_{z \in V} z[i]. For a vector yy, let y′y' be its entries put into y′y' at line L8 and y′′=y−y′y'' = y - y' its entries put into the index. Write "yy before xx" when yy comes earlier in the input order.

Lemma 1. For each y∈Vy \in V and z∈Vz \in V, z⋅y′<tz \cdot y' < t.

Proof. Line L6 visits the entries of yy in a fixed order, and bb is the sum of m[i] y[i]m[i] \, y[i] over the entries visited so far. All terms are nonnegative, so bb never decreases. So the entries with b<tb < t after line L7 come first, and they are the entries of y′y'. If y′y' is empty, z⋅y′=0<tz \cdot y' = 0 < t. Otherwise, at its last entry, b=∑i∈y′m[i] y[i]<tb = \sum_{i \in y'} m[i] \, y[i] < t. Since 0≤z[i]≤m[i]0 \le z[i] \le m[i] and y[i]≥0y[i] \ge 0, z⋅y′=∑i∈y′z[i] y[i]≤∑i∈y′m[i] y[i]<tz \cdot y' = \sum_{i \in y'} z[i] \, y[i] \le \sum_{i \in y'} m[i] \, y[i] < t. □\square

Lemma 2. When FIND-MATCHES(x,I,t)(x, I, t) runs, the lists hold exactly the pairs (y,y[i])(y, y[i]) with yy before xx and y′′[i]>0y''[i] > 0, and after line L11, A[y]=x⋅y′′A[y] = x \cdot y'' for each yy before xx (with A[y]=0A[y] = 0 when yy is not in the map).

Proof. Line L8 appends (y,y[i])(y, y[i]) to IiI_i exactly for the entries of y′′y'', after line L4 has run for yy, and nothing is removed. For the second part, line L11 adds x[i] y[i]x[i] \, y[i] to A[y]A[y] for each ii with x[i]>0x[i] > 0 and y′′[i]>0y''[i] > 0. The sum of these terms is ∑ix[i] y′′[i]=x⋅y′′\sum_i x[i] \, y''[i] = x \cdot y'', since the other terms are 0. □\square

Theorem. ALL-PAIRS(V,t)(V, t) reports exactly the pairs (y,x)(y, x) with yy before xx and x⋅y≥tx \cdot y \ge t, each once, with s=x⋅ys = x \cdot y.

Proof. Soundness. A pair (y,x)(y, x) is reported in FIND-MATCHES(x,⋅)(x, \cdot) at line L12, with yy in the map, so yy is before xx by Lemma 2. Its score is s=A[y]+x⋅y′=x⋅y′′+x⋅y′=x⋅ys = A[y] + x \cdot y' = x \cdot y'' + x \cdot y' = x \cdot y, and it is reported only if s≥ts \ge t.

Completeness. Let yy be before xx with x⋅y≥tx \cdot y \ge t. By Lemma 1 with z=xz = x, x⋅y′<tx \cdot y' < t, so x⋅y′′=x⋅y−x⋅y′>0x \cdot y'' = x \cdot y - x \cdot y' > 0. By Lemma 2, A[y]=x⋅y′′>0A[y] = x \cdot y'' > 0 after line L11 of FIND-MATCHES(x,⋅)(x, \cdot), so line L12 considers yy, computes s=x⋅y≥ts = x \cdot y \ge t, and reports (y,x,s)(y, x, s).

Uniqueness. The pair (y,x)(y, x) can be reported only in FIND-MATCHES(x,⋅)(x, \cdot), which runs once, and line L12 considers each key yy of AA once. ■\blacksquare

Back to AllPairs