Better Size Estimation for Sparse Matrix Products

Rasmus Resen Amossen, Andrea Campagna, Rasmus Pagh

This paper, presented by researchers from the IT University of Copenhagen, tackles a fundamental problem in large-scale data processing: how to quickly estimate the size of a sparse matrix product without actually performing the expensive multiplication.

TL;DR

The authors introduce the first linear-time algorithm ($O(n)$) to estimate the number of non-zero entries in a sparse boolean matrix product with a $1 \pm \epsilon$ relative error. By using a clever structured hash function, they bypass the need to generate all intermediate pairs, making size estimation significantly faster than the best known algorithms for actually computing the product.

What Problem Does This Paper Address?

In fields like database systems and computer algebra, we often need to know the "size" (number of non-zero entries, $z$) of the product of two sparse matrices. In database terms, this is known as the join-project or join-distinct problem: if you join two tables and then project away the join attribute, how many unique rows are left?

The challenge is that while the input matrices might be small (totaling $n$ non-zero entries), the number of intermediate "candidate" pairs can be massive—much larger than both the input and the final output. Traditional exact algorithms for sparse matrix multiplication are slow (super-linear time), and previous estimation techniques like Cohen’s (1997) required $O(n/\epsilon^2)$ time. The authors sought a way to reach a reliable estimate in truly linear time, $O(n)$.

Methods

The core of the paper is Algorithm 1, which builds on the "k-th smallest value" sketching technique. The standard approach to estimate the size of a set is to hash all its elements and look at the $k$-th smallest hash value; the smaller that value is, the larger the set likely is.

The "aha!" moment in this paper is the choice of the hash function. To estimate the size of a Cartesian product $A \times C$ (which represents the potential non-zero entries in a matrix product), you would normally have to hash every single pair $(x, y)$, taking $|A| \cdot |C|$ time.

Instead, the authors use a structured hash function: $$h(x, y) = (h_1(x) - h_2(y)) \pmod 1$$ Because of this structure, if you sort the values of $h_1(x)$ and $h_2(y)$, you can find all pairs $(x, y)$ where $h(x, y)$ is below a certain threshold $p$ in roughly $O(|A| + |C|)$ time—essentially a linear scan similar to merging two sorted lists.

They also introduce a buffering trick that allows them to maintain the $k$ smallest hash values in $O(1)$ amortized time per insertion, avoiding the logarithmic overhead of a standard priority queue (heap).

Datasets

The authors evaluated their methods on several real-world datasets:

  • FIMI Repository: Datasets commonly used for frequent itemset mining, such as accidents, chess, connect, kosarak, mushroom, and retail.
  • IMDB (Internet Movie Database): Extracted relations like directoractor and movieactor.
  • Self-Joins: They performed size estimation on the self-joins of these relations, which mimics the behavior of the Apriori data mining algorithm.

Key Insights and Ideas

  • Structured Hashing is Key: By using a hash function that respects the order of its components, the authors turned a quadratic search problem into a linear one. This is the primary reason they achieved $O(n)$ complexity.
  • Cache-Obliviousness: The paper doesn't just focus on RAM; it provides a variant that works efficiently in the cache-oblivious model, requiring $O(\text{sort}(n))$ I/Os. This is crucial for datasets that don't fit in main memory.
  • Sampling for Scale: They show that if the result size $z$ is very large, you can combine their algorithm with "distinct sampling" to get an estimate in $o(n)$ time (sub-linear), which matches theoretical lower bounds for space usage.
  • Theory vs. Practice: A recurring theme is that the theoretical "worst-case" error bounds (derived from Chebyshev’s inequality) are quite conservative. In practice, the algorithm is much more accurate than the theory suggests.

Key Results

  • Complexity: The algorithm achieves a $1 \pm \epsilon$ approximation in expected $O(n)$ time for any $\epsilon > 4/\sqrt[4]{z}$.
  • Accuracy: In experiments with $k=1024$, the estimates were remarkably stable. For most datasets, 2/3 of the estimates were within 4% of the exact size, whereas the theoretical bound for that $k$ was roughly 9.4%.
  • Efficiency: The algorithm is robust enough to handle millions of non-zero entries on a standard machine, providing a "linear time" alternative where previous methods would have scaled quadratically with the number of join-attribute matches.

Limitations and Future Work

  • Beyond Two Matrices: The current work focuses on the product of two matrices. The authors note that extending this to a chain of three or more matrices (or the transitive closure of a graph) remains an interesting open problem.
  • Streaming Constraints: While the algorithm is fast, it isn't a "pure" streaming algorithm in the strictest sense because it requires the ability to group or sort tuples by their join attribute (the $b$ in $R_1(a,b)$ and $R_2(b,c)$).
  • Sampling Thresholds: As shown in Figure 2, if the sampling probability is set too low on small datasets, the sketch might end up empty, making it impossible to return an estimate. Choosing the optimal sampling rate automatically is a potential area for refinement.

Summary updated

BibTeX
@article{amossen2014better,
  author = {Rasmus Resen Amossen and Andrea Campagna and Rasmus Pagh},
  title = {{Better Size Estimation for Sparse Matrix Products}},
  journal = {Algorithmica},
  year = {2014},
  doi = {10.1007/s00453-012-9692-9},
}
Figure