The focus of this article is recovering a sparse vector x∈Rn in the streaming setting. Initially, x=0. Then we see a sequence of updates (i,Δ) each causing the change xi←xi+Δ. There are three popular studied models
insertion only:Δ=1 for each update.
strict turnstile:Δ can be negative, but it is guaranteed that x≥0 all the time.
general turnstile:Δ can be negative, and entries of x can be negative.
Let supp(x)⊂[n] be the set of nonzero indices of x. The vector x is called k sparse if supp(x)≤k.
Goal. Given an integer k, design a data structure such that, if x is k sparse, the structure returns x exactly; otherwise, the return can be arbitrary.
As we can't actually store real numbers, we use the following assumption:
The updates are integers and the entries of x are bounded by ±M.
With this restriction, it suffices to solve this problem in a finite field Fp where p≥max{n,M}. As opposed to the naive solution that keeps all entries of x and uses O(nlogM) bits of memory, we show that the memory can be reduced to O(klog(nM)) bits.
Algorithm
The problem is easy to solve in the insertion only model. We can use a hash set that keeps the first k occurrence of the indices. If the we detect the k+1 new index, we can just report error.
The scenario is a little more complicated for the other two models. E.g., let n=4 and k=1. Consider the following stream
(1,1),(2,1),(3,−1),(4,1),(1,−1),(2,−1),(3,1).
The vector x is finally 1-sparse and the only non-zero index is revealed at the end of the stream.
Solution 1
Encode
In the first approach, we use a matrix H∈Fpm×n (m to be determined) and encode x as Enc(x)=Hx. If the following property holds, we are guaranteed to recover x if it is k sparse.
For every k sparse x,x′∈Fpn, if x=x′, then Hx=Hx′.
If Hx=Hx′, then H(x−x′)=0. Observe that x−x′ is a 2k sparse vector. This is equivalent to that Hz=0 for every nonzero 2k sparse vector z∈Fpn.
Such H does exists, with m=2k. Let a1,a2,...,an∈Fp be different integers. Then H is given as
Let S=supp(z)⊂[n] be a set of size 2k. Denote zS be the vector by keeping only the nonzero dimensions of z and HS be an 2k×2k matrix by keeping only columns indexed by S. Then Hz=HSzS. We show that the rows of HS are linearly independently, hence its columns. It follows that if zS=0⟹HSzS=0.
It is left to show that ∀c∈Fp2k, c=0→HSTc=0. Define the polynomial in Fp
f(x)=c2kx2k−1+c2k−1x2k−1+...+c1.
Then HSTc=(f(ai))i∈[S]. As f(x) has at most 2k−1 distinct roots, and the (ai)i∈[S] are different, there exists some i∈[S] such that f(ai)=0.
Decode
Given y=Enc(x)=Hx, how can we recover x? We can loop over every subset S⊂[n] of size 2k and compute x′=HS−1y. If x′ is k sparse, by the property of H, it is the unique k-sparse vector such that HSx′=y.
Remark: The proposed method is exponential respect to k. However, there is an algorithm (called syndrome decoding) to perform the recovery in O(k2poly(logp)) time.
Space and Time
Space. The algorithm uses O(klog(nM)) bits.
This is because we don't store H. For the finite field Fp, there exists at least one primitive element whose powers give all nonzero elements. We can pick such a primitive element a, and set a1=a, a2=a2, ..., an=an. Storing y=Hx requires only O(klog(nM)) bits.
Time. Processing each update (i,Δ) in the stream takes O(k) time.
Solution 2
We first show how to recover an 1 sparse vector when negative updates are allowed, then use it as a black box to recover k sparse vector.
1 sparse recovery
We show an algorithm that successes with probability n/p.
First, we pick a non-zero integer z uniformly at random from Fp. Let
H=11z12z213z3.........1nzn.
The algorithm consists of 3 parts.
Initialize: w←{0}3 Sample z uniformly from 1,...,p−1.
Updates: (i,Δ) w←w+Δ⋅Hi, where Hi is the ith column of H.
Output: If w1=0 or w3=w1⋅zw2/w1: Returnx is not 1 sparse; Else: Returnj≐w2/w1
Failure Analysis
Theorem. The algorithm fails with probability n/p.
If w1=0, either x=0 or there is cancellation. Conditioned on w1=0, if x is 1 sparse, then
w1=xj,w2=j⋅xj,w3=zj⋅xj.
Hence, j=w2/w1 is exactly the non-zero index, and w3=w1⋅zj.
If x is not 1 sparse, then
w3−w1⋅zj
is a polynomial of degree at most n. It has at most n distinct roots with one of them being 0. The probability that z being a root is at most (n−1)/(p−1)≤n/p.
Space and Time
Space. The algorithm uses O(log(nM)) bits.
Time. Processing each update (i,Δ) takes O(1) time.
k sparse recovery
To apply the 1 sparse recovery data structure, we partition x into 2k subsets randomly. In particular, we use a pairwise hash function that maps each i∈[n] to [2k] uniformly at random. For ℓ∈[2k], let Sℓ be the set of indices mapped to ℓ:
Sℓ≐{i∈[n]:h(i)=ℓ}.
Then we run 1 sparse recovery on each partition. If some partition contains only one nonzero index, then we recover it successfully.
Theorem. If x is k sparse, an nonzero dimension i is recovered with probability 1/2.
Proof. It suffices to prove that i is the only nonzero index in Sh(i). For j∈supp(x), define the indicator variable Xj which equals 1 if h(j)=h(i) and equals 0 otherwise. By Markov's inequality and linearity of expectation,
Remark. The Markov's inequality is rather tight here. Even if we have a fully independent hash function h, the probability that none of the nonzero indices collides with i is given by
(1−2k1)k−1⪅exp(−2kk−1)≈exp(−21)≈0.60653.
□
Corollary. Running log(k/δ) independent copies of the above structure reduce the failure probability to δ.
Space and Time
Space. The algorithm uses O(k(log(k/δ))log(nM)) bits.
Time. Processing each update (i,Δ) takes O(log(k/δ)) time.