All-Pairs reports exactly the pairs with dot product at least t full
This proof shows: correctness.
Let V=(x1,…,xn) be vectors in R≥0d, t>0, and m[i]=maxz∈Vz[i]. For a vector y, let y′ be its entries put into y′ at line L8 and y′′=y−y′ its entries put into the index. Write "y before x" when y comes earlier in the input order.
Lemma 1. For each y∈V and z∈V, z⋅y′<t.
Proof. Line L6 visits the entries of y in a fixed order, and b is the sum of m[i]y[i] over the entries visited so far. All terms are nonnegative, so b never decreases. So the entries with b<t after line L7 come first, and they are the entries of y′. If y′ is empty, z⋅y′=0<t. Otherwise, at its last entry, b=∑i∈y′m[i]y[i]<t. Since 0≤z[i]≤m[i] and y[i]≥0, z⋅y′=∑i∈y′z[i]y[i]≤∑i∈y′m[i]y[i]<t. □
Lemma 2. When FIND-MATCHES(x,I,t) runs, the lists hold exactly the pairs (y,y[i]) with y before x and y′′[i]>0, and after line L11, A[y]=x⋅y′′ for each y before x (with A[y]=0 when y is not in the map).
Proof. Line L8 appends (y,y[i]) to Ii exactly for the entries of y′′, after line L4 has run for y, and nothing is removed. For the second part, line L11 adds x[i]y[i] to A[y] for each i with x[i]>0 and y′′[i]>0. The sum of these terms is ∑ix[i]y′′[i]=x⋅y′′, since the other terms are 0. □
Theorem. ALL-PAIRS(V,t) reports exactly the pairs (y,x) with y before x and x⋅y≥t, each once, with s=x⋅y.
Proof. Soundness. A pair (y,x) is reported in FIND-MATCHES(x,⋅) at line L12, with y in the map, so y is before x by Lemma 2. Its score is s=A[y]+x⋅y′=x⋅y′′+x⋅y′=x⋅y, and it is reported only if s≥t.
Completeness. Let y be before x with x⋅y≥t. By Lemma 1 with z=x, x⋅y′<t, so x⋅y′′=x⋅y−x⋅y′>0. By Lemma 2, A[y]=x⋅y′′>0 after line L11 of FIND-MATCHES(x,⋅), so line L12 considers y, computes s=x⋅y≥t, and reports (y,x,s).
Uniqueness. The pair (y,x) can be reported only in FIND-MATCHES(x,⋅), which runs once, and line L12 considers each key y of A once. ■