We consider the problem of counting 4-cycles ($C_4$) in an undirected graph $G$ of $n$ vertices and $m$ edges (in bipartite graphs, 4-cycles are also often referred to as $\textit{butterflies}$). Most recently, Wang et al. (2019, 2022) developed algorithms for this problem based on hash tables and sorting the graph by degree. Their algorithm takes $O(m\bar\delta)$ expected time and $O(m)$ space, where $\bar \delta \leq O(\sqrt{m})$ is the $\textit{average degeneracy}$ parameter introduced by Burkhardt, Faber \& Harris (2020). We develop a streamlined version of this algorithm requiring $O(m\bar\delta)$ time and precisely $n$ words of space. It has several practical improvements and optimizations; for example, it is fully deterministic, does not require any auxiliary storage or sorting of the input graph, and uses only addition and array access in its inner loops. Our algorithm is very simple and easily adapted to count 4-cycles incident to each vertex and edge. Empirical tests demonstrate that our array-based approach is $4\times$ -- $7\times$ faster on average compared to popular hash table implementations.
翻译:我们考虑在具有 $n$ 个顶点和 $m$ 条边的无向图 $G$ 中计数 4-环($C_4$)的问题(在二分图中,4-环也常被称为 $\textit{butterflies}$)。最近,Wang 等人(2019, 2022)基于哈希表及按度排序图的方法,为此问题开发了算法。其算法期望时间复杂度为 $O(m\bar\delta)$,空间复杂度为 $O(m)$,其中 $\bar \delta \leq O(\sqrt{m})$ 是由 Burkhardt、Faber 和 Harris(2020)引入的 $\textit{平均退化度}$ 参数。我们提出了该算法的一个精简版本,仅需 $O(m\bar\delta)$ 时间及精确的 $n$ 个字的存储空间。该版本包含多项实用改进与优化;例如,它是完全确定性的,无需任何辅助存储或对输入图进行排序,且其内层循环仅涉及加法与数组访问。我们的算法非常简洁,易于适配以计数关联到每个顶点和每条边的 4-环。实证测试表明,与常见的哈希表实现相比,我们基于数组的方法平均快 $4\times$ 至 $7\times$。