A fast algorithm for identifying friends-of-friends halos

Yu Feng, Chirag Modi

TL;DR

This paper introduces a novel, efficient algorithm for identifying "friends-of-friends" (FoF) halos in cosmological simulations. By leveraging advanced computer science techniques like dual KD-trees and splay trees, their method significantly speeds up halo detection while using less memory, making it a valuable tool for analyzing vast astronomical datasets.

What Problem Does This Paper Address?

Cosmologists often need to identify clusters of matter, known as halos, within massive simulations of the universe. These halos are the building blocks of cosmic structure, from individual galaxies to galaxy clusters. A common method for this is the "friends-of-friends" (FoF) algorithm, which groups particles that are close enough to each other. However, traditional FoF algorithms can be computationally expensive, especially in dense regions of simulations, requiring many costly "neighbor queries" to find nearby particles. This slowness hinders the analysis of increasingly large and detailed cosmological datasets. Existing methods often resort to approximations to gain speed, potentially sacrificing accuracy. This paper aims to provide a fast exact FoF algorithm that avoids these performance bottlenecks.

Methods

The core of the proposed algorithm is a dual KD-tree approach, inspired by fast 2-point correlation function codes. Instead of querying each particle individually, it simultaneously traverses two KD-trees (spatial data structures) to efficiently find pairs of particles within a given linking length. This avoids redundant neighbor searches.

To further optimize the process, two key techniques are introduced:

  • Splay Tree Operations: When merging two groups (proto-features), the algorithm uses a splay operation on a tree-like data structure representing these groups. This operation restructures the tree to bring recently accessed nodes closer to the root, reducing the average time to find the root of a group from O(log L) to O(1), where L is the group size. This significantly speeds up the merging process.
  • Pruning Self-Connected Nodes: In very dense regions, KD-tree nodes can become "self-connected" (all particles within the node are closer than the linking length). The algorithm identifies these nodes and skips redundant merge operations between them, as a single merge is sufficient to connect all particles within. This drastically reduces the number of operations in high-density peaks.

Datasets

The paper evaluates its algorithm on two cosmological simulation datasets:

  • Low A FastPM Simulation: A 100 Mpc/h box with 256³ particles, generated by the FastPM code.
  • High NyX Simulation: A 10 Mpc/h box with 256³ particles, generated by the AMR cosmological simulation code NyX.

These datasets are used to benchmark the performance of the new algorithm against existing methods.

Key Insights and Ideas

  • Bridging Computer Science and Cosmology: The paper effectively demonstrates how established computer science algorithms (dual KD-trees, splay trees) can be directly applied to solve computationally intensive problems in astrophysics, leading to significant performance gains.
  • The Power of Pair Enumeration: By framing FoF as a graph problem and using efficient pair enumeration, the algorithm avoids the iterative nature of some traditional methods, processing each potential connection only once.
  • Splay Trees for Dynamic Merging: The use of splay trees for managing proto-features is a clever way to optimize the hierarchical merging process. It dynamically restructures the data to keep frequently accessed elements readily available, reducing average access times.
  • Self-Connected Node Pruning as a Game-Changer: Identifying and intelligently handling self-connected nodes is crucial for performance in dense regions. This optimization directly tackles the bottleneck of over-density, a common challenge in cosmological simulations.

Key Results

The authors report substantial speedups and efficiency improvements:

  • Reduced Merge Operations: The algorithm eliminates more than half of merge operations for typical linking lengths (b ~ 0.2) compared to naive methods.
  • Significant Speedup: For cosmological datasets, the algorithm reduces the number of operations by almost two orders of magnitude compared to a naive implementation using linked lists.
  • Robustness: The splay tree optimization, especially with a "safe guard" operation, keeps the average depth of the hierarchical tree structure close to O(1), ensuring consistent performance.
  • Practical Performance: For linking lengths of 0.2, the algorithm processes 16 million points in under 20 seconds on a single core, comparable to the time taken for data loading and KD-tree construction.
  • Self-Connected Optimization Impact: This optimization reduces merge operations by up to 40% in high-resolution data and distance computations by about 10-70% for large linking lengths.

Limitations and Future Work

  • KD-Tree Implementation: The authors note that their reference KD-tree pair enumeration code is not highly optimized for performance, suggesting that porting the algorithm to more efficient correlation function codes (like chaining-mesh codes) could yield further speed improvements.
  • Parallelization: While the algorithm is designed to be easily integrated into parallel FOF finding routines, the paper focuses on the single-core performance. Future work could explore optimal distributed algorithms.
  • Closest Pair Algorithm: The paper mentions that a fast algorithm for finding the exact closest pair between two nodes exists but is left as future work due to its potentially large constant factor for small self-connected nodes.

Summary updated

Abstract

We describe a simple and fast algorithm for identifying friends-of-friends features and prove its correctness. The algorithm avoids unnecessary expensive neighbor queries, uses minimal memory overhead, and rejects slowdown in high over-density regions. We define our algorithm formally based on pair enumeration, a problem that has been heavily studied in fast 2-point correlation codes and our reference implementation employs a dual KD-tree correlation function code. We construct features in a hierarchical tree structure, and use a splay operation to reduce the average cost of identifying the root of a feature from $O[\log L]$ to $O[1]$ ($L$ is the size of a feature) without additional memory costs. This reduces the overall time complexity of merging trees from $O[L\log L]$ to $O[L]$, reducing the number of operations per splay by orders of magnitude. We next introduce a pruning operation that skips merge operations between two fully self-connected KD-tree nodes. This improves the robustness of the algorithm, reducing the number of merge operations in high density peaks from $O[\delta^2]$ to $O[\delta]$. We show that for cosmological data set the algorithm eliminates more than half of merge operations for typically used linking lengths $b \sim 0.2$ (relative to mean separation). Furthermore, our algorithm is extremely simple and easy to implement on top of an existing pair enumeration code, reusing the optimization effort that has been invested in fast correlation function codes.
BibTeX
@inproceedings{feng2017fast,
  author = {Yu Feng and Chirag Modi},
  title = {{A fast algorithm for identifying friends-of-friends halos}},
  booktitle = {Astron. Comput},
  year = {2017},
  url = {https://openreview.net/forum?id=ZdDJoDmikR_},
}
Figure