NeurIPS 2026Coming to NeurIPS?Explore papers, orals & directions

An Optimal Algorithm for Triangle Counting

Rajesh Jayaram, John KallaugherarXiv 2021 · OpenReview

Introduction

In the era of massive datasets, graphs representing social networks, web links, or biological interactions often grow too large to be stored in memory. Analyzing these structures requires processing them as "streams" where edges arrive sequentially. A fundamental problem in this domain is triangle counting—estimating the number of cycles of length three. This metric is essential for calculating clustering coefficients and identifying community structures or detecting spam.

The primary challenge is achieving high accuracy with sublinear space. Since storing the entire graph is impossible, researchers seek algorithms that use minimal memory relative to the graph's size. This paper resolves a long-standing quest for the most efficient way to estimate these counts in edge streams.

The Complexity of Graph Shapes

The difficulty of counting triangles in a stream is not just a function of the number of edges ($m$) or triangles ($T$). It depends heavily on how those triangles are distributed. Two critical parameters define this complexity:

  1. $\Delta_E$: The maximum number of triangles sharing a single edge (a "book" configuration).
  2. $\Delta_V$: The maximum number of triangles sharing a single vertex (a "fan" or "star" configuration).

Previous research established lower bounds for space complexity based on these parameters, but no single algorithm had successfully matched these bounds across all graph distributions—until now.

A Hybrid Sampling Approach

The researchers present a new algorithm that achieves optimality by combining two classic sampling strategies: Vertex Sampling and Edge Sampling.

The Mechanism

The algorithm employs a dual-sampling strategy using two hash functions:

  • Vertex Sampling ($p$): Each vertex is sampled with probability $p$.
  • Edge Activation ($q$): Each edge is "activated" with probability $q$.

An edge $(u, v)$ is stored in memory only if it is activated AND at least one of its endpoints is a sampled vertex. When a new edge $(w, v)$ arrives, the algorithm checks its stored edges for a vertex $u$ such that $(u, v)$ and $(u, w)$ are already present. If the "center" vertex $u$ was sampled, the algorithm increments a counter. This "wedge" approach allows the algorithm to detect a triangle by only storing two of its edges, effectively getting the third "closing" edge for free from the stream.

Comparison with Prior Work

The significance of this work lies in its theoretical optimality. By carefully balancing the sampling probabilities $p$ and $q$ based on $\Delta_E$ and $\Delta_V$, the algorithm achieves a space complexity that matches the known lower bounds.

Comparison of space complexity for triangle counting algorithms in different streaming models.

As shown in the table above, previous algorithms were optimal for specific scenarios but failed to cover the entire parameter space. For example, [PTTW13] focused on general insertion-only streams, while [KP17] provided lower bounds for vertex-heavy distributions. This new work provides an upper bound of $\widetilde{O}\left(\frac{m}{T} \cdot (\Delta_E + \sqrt{\Delta_V})\right)$, which perfectly aligns with the combined lower bounds of $\Omega(\frac{m \Delta_E}{T})$ and $\Omega(\frac{m \sqrt{\Delta_V}}{T})$.

Key Results and Implications

The algorithm provides a $(1 \pm \epsilon)$ approximation of the triangle count $T$ using: $$ \widetilde{O}\left(\frac{m}{T} \cdot (\Delta_E + \sqrt{\Delta_V})\right) \text{ bits of space.} $$

This result effectively "closes" the complexity question for triangle counting in insertion-only streams. While the algorithm assumes some prior knowledge of $T, \Delta_E,$ and $\Delta_V$ (which can often be estimated), it represents the theoretical limit of what is possible.

Future work remains in the domain of "turnstile" streams, where edges can be deleted as well as added. As the table indicates, linear sketching techniques used for turnstile streams currently require significantly more space, leaving a gap between insertion-only and fully dynamic graph streaming.

Summary updated

BibTeX
@misc{jayaram2021optimal,
  author = {Rajesh Jayaram and John Kallaugher},
  title = {{An Optimal Algorithm for Triangle Counting}},
  year = {2021},
  eprint = {2105.01785},
  archivePrefix = {arXiv},
  url = {https://arxiv.org/abs/2105.01785},
}
Figure