Abstract
Depth-first search (DFS) is the basis for many efficient graph algorithms. We introduce general techniques for the efficient implementation of DFS-based graph algorithms and exemplify them on three algorithms for computing strongly connected components. The techniques lead to speed-ups by a factor of two to three compared to the implementations provided by LEDA and BOOST. We have obtained similar speed-ups for biconnected components algorithms. We also compare the graph data types of LEDA and BOOST.
Introduction
While the asymptotic complexity of DFS-based algorithms for finding Strongly Connected Components (SCCs) has been known to be linear ($O(n+m)$) for decades, the actual running time on modern hardware is often dominated by memory latency rather than instruction count. Standard implementations in popular libraries like LEDA and the Boost Graph Library (BGL) often prioritize modularity and ease of use, leading to scattered data structures and frequent cache faults. This research aims to bridge the gap between theoretical efficiency and hardware-level performance through meticulous algorithm engineering.
Methodology
The authors apply several key engineering techniques to classic SCC algorithms, primarily Tarjan’s and the Cheriyan-Mehlhorn-Gabow (CMG) algorithm.
- Overlayed Node Data: Instead of using multiple arrays for different node properties (DFS number, component ID, etc.), data is packed into a single integer array. This reduces the memory footprint and ensures that loading a node's data into the cache brings all relevant state information with it.
- The "Edge Stack" Trick: Standard DFS accesses a node's adjacency list twice: once when it first reaches the node, and again when it returns from a recursive call. The authors propose pushing all edges onto a global stack upon first encounter, ensuring subsequent accesses are sequential and more likely to be cached.
- Adjacency Array Representation: A compact static graph representation is used to minimize overhead and improve spatial locality compared to linked-list structures.
- Low-point Optimization: For Tarjan’s algorithm, low-point values are passed via the recursion stack as local variables rather than stored in global arrays, further reducing memory traffic.
Experimental Setup
Implementations were evaluated using randomly generated directed graphs with up to 2 million nodes. Tests covered varying sizes ($n$) and densities ($m/n$ ratios) to observe scaling behavior. The "tuned" versions were benchmarked against built-in SCC implementations in LEDA 5.0 and BGL 1.34.1.
Results and Discussion
The optimized versions of Tarjan and CMG consistently outperformed BGL and LEDA by a factor of 2 to 3 across various graph configurations. One-pass algorithms (Tarjan, CMG) were significantly faster than two-pass algorithms like Kosaraju-Sharir, which suffer from the overhead of computing a reversed graph.
Key insights include:
- Memory Layout > Asymptotics: Constant factors determined by memory access patterns are the primary bottleneck for linear-time algorithms on modern CPUs.
- Backtracking Costs: The "return" part of a DFS is as expensive as the "forward" part due to cache misses; the "Edge Stack" effectively mitigates this.
- Algorithm Convergence: After aggressive optimization, CMG and Tarjan perform almost identically, suggesting they reach a similar hardware-level efficiency limit.
Conclusion
Algorithm engineering can reclaim significant performance lost to the "flexibility tax" of general-purpose libraries. While these techniques are highly effective for static graphs, future work is needed to adapt them for dynamic graphs where edges are frequently modified. The principles demonstrated here are broadly applicable to other DFS-based traversals, such as computing biconnected components.
Summary updated