A space efficient streaming algorithm for triangle counting using the birthday paradox

Madhav Jha, C. Seshadhri, Ali PinarKDD 2013

Abstract

Modern graph analysis often involves massive datasets where traditional in-memory algorithms fail. This paper presents a single-pass streaming algorithm for estimating triangle counts and transitivity. By leveraging the Birthday Paradox through reservoir sampling of edges and wedges, the proposed method achieves high accuracy while storing only a tiny fraction of the total edges.

Introduction

Triangle counting is a fundamental task in network analysis, providing insights into social cohesion, community structure, and spam detection. However, as modern graphs grow to hundreds of millions of edges, the memory requirements for exact counting become prohibitive. Existing streaming algorithms often struggle with accuracy on sparse graphs or require excessive memory. This work introduces STREAMING-TRIANGLES, an algorithm that balances extreme space efficiency with high precision.

Methodology: The Birthday Paradox for Wedges

The core innovation lies in applying the logic of the Birthday Paradox to "wedges" (two edges sharing a common vertex, such as a path $u-v-w$). Just as a small group of people is likely to share a birthday, a relatively small random sample of edges is likely to contain wedges that are eventually closed by a third edge to form a triangle.

The algorithm maintains two reservoirs in memory:

  1. Edge Reservoir: A uniform random sample of edges seen so far in the stream.
  2. Wedge Reservoir: A sample of wedges formed by the edges currently in the edge reservoir.

As new edges arrive in the stream, the algorithm checks if they "close" any wedges currently stored in the wedge reservoir. If a closure is detected, a triangle is counted. By focusing on "future-closed" wedges—where the closing edge appears last in the stream—the algorithm avoids double-counting and simplifies the statistical estimation of the total count.

Experimental Evaluation

The authors evaluated the algorithm on 14 diverse large-scale datasets from the Stanford Network Analysis Project (SNAP), including social networks like Orkut (223M edges), Flickr, and various web crawls.

Key Performance Metrics

  • Extreme Efficiency: For the Orkut graph, the algorithm produced accurate results by storing only 40,000 edges—a fraction of approximately 0.0001 of the total graph.
  • High Accuracy: Across nearly all datasets, the relative error for transitivity and triangle counts was less than 5%.
  • Robustness: The algorithm proved robust against different stream orderings (e.g., BFS vs. DFS order), a common weakness in other streaming approaches.

Real-time Tracking

A significant advantage of this single-pass streaming approach is the ability to maintain a running estimate of graph metrics as the network evolves. The authors demonstrated this by tracking the transitivity and triangle count of a patent citation network over a 40-year period.

Transitivity and Triangle count tracking over time in a patent citation network. The streaming estimates (Run1, Run2) closely follow the exact values.

As shown in the figure, the estimates for both transitivity (a) and triangle count (b) closely track the "exact" values throughout the network's growth, despite the exponential increase in the total number of triangles.

Conclusion

The STREAMING-TRIANGLES algorithm provides a scalable solution for motif analysis in massive graphs. By requiring sub-linear space—typically $O(m/\sqrt{T})$, which often translates to $O(\sqrt{n})$ for social networks—it enables real-time monitoring of global clustering properties on commodity hardware. Future work may extend this sampling logic to directed graphs and more complex motifs.

Summary updated

BibTeX
@inproceedings{jha2013space,
  author = {Madhav Jha and C. Seshadhri and Ali Pinar},
  title = {{A space efficient streaming algorithm for triangle counting using the birthday paradox}},
  booktitle = {ACM SIGKDD Conference on Knowledge Discovery and Data Mining},
  year = {2013},
  doi = {10.1145/2487575.2487678},
}
Figure