# A General Purpose Algorithm for Counting Simple Cycles and Simple Paths of Any Length

### Abstract

We describe a general purpose algorithm for counting simple cycles and simple paths of any length ℓ on a (weighted di)graph on N vertices and M edges, achieving a time complexity of O(N+M+(ℓω+ℓΔ)|Sℓ|). In this expression, |Sℓ| is the number of (weakly) connected induced subgraphs of G on at most ℓ vertices, Δ is the maximum degree of any vertex and ω is the exponent of matrix multiplication. We compare the algorithm complexity both theoretically and experimentally with most of the existing algorithms for the same task. These comparisons show that the algorithm described here is the best general purpose algorithm for the class of graphs where (ℓω−1Δ−1+1)|Sℓ|≤|Cycleℓ|, with |Cycleℓ| the total number of simple cycles of length at most ℓ, including backtracks and self-loops. On Erdős-Rényi random graphs, we find empirically that this happens when the edge probability is larger than circa 4/N. In addition, we show that some real-world networks also belong to this class. Finally, the algorithm permits the enumeration of simple cycles and simple paths on networks where vertices are labeled from an alphabet on n letters with a time complexity of O(N+M+(nℓℓω+ℓΔ)|Sℓ|). A Matlab implementation of the algorithm proposed here is available for download.

Publication
Algorithmica
Date

## Bibtex

@Article{Giscard2019,
author="Giscard, Pierre-Louis
and Kriege, Nils
and Wilson, Richard C.",
title="A General Purpose Algorithm for Counting Simple Cycles and Simple Paths of Any Length",
journal="Algorithmica",
year="2019",
month="Jul",
day="01",
volume="81",
number="7",
pages="2716--2737",
abstract="We describe a general purpose algorithm for counting simple cycles and simple paths of any length {\$}{\$}{\backslash}ell {\$}{\$}ℓon a (weighted di)graph on N vertices and M edges, achieving an asymptotic running time of {\$}{\$}O{\backslash}left( N+M+{\backslash}big ({\backslash}ell ^{\backslash}omega +{\backslash}ell {\backslash}Delta {\backslash}big ) |S{\_}{\backslash}ell |{\backslash}right) {\$}{\$}ON+M+(ℓ$\omega$+ℓ$\Delta$)|Sℓ|. In this expression, {\$}{\$}|S{\_}{\backslash}ell |{\$}{\$}|Sℓ|is the number of (weakly) connected induced subgraphs of G on at most {\$}{\$}{\backslash}ell {\$}{\$}ℓvertices, {\$}{\$}{\backslash}Delta {\$}{\$}$\Delta$is the maximum degree of any vertex and {\$}{\$}{\backslash}omega {\$}{\$}$\omega$is the exponent of matrix multiplication. We compare the algorithm complexity both theoretically and experimentally with most of the existing algorithms for the same task. These comparisons show that the algorithm described here is the best general purpose algorithm for the class of graphs where {\$}{\$}({\backslash}ell ^{\{}{\backslash}omega -1{\}}{\backslash}Delta ^{\{}-1{\}}+1) |S{\_}{\backslash}ell |{\backslash}le |{\backslash}text {\{}Cycle{\}}{\_}{\backslash}ell |{\$}{\$}(ℓ$\omega$-1$\Delta$-1+1)|Sℓ|≤|Cycleℓ|, with {\$}{\$}|{\backslash}text {\{}Cycle{\}}{\_}{\backslash}ell |{\$}{\$}|Cycleℓ|the total number of simple cycles of length at most {\$}{\$}{\backslash}ell {\$}{\$}ℓ, including backtracks and self-loops. On Erd{\H{o}}s--R{\'e}nyi random graphs, we find empirically that this happens when the edge probability is larger than circa 4 / N. In addition, we show that some real-world networks also belong to this class. Finally, the algorithm permits the enumeration of simple cycles and simple paths on networks where vertices are labeled from an alphabet on n letters with an asymptotic running time of {\$}{\$}O{\backslash}left( N+M+{\backslash}big (n^{\backslash}ell {\backslash}ell ^{\backslash}omega +{\backslash}ell {\backslash}Delta {\backslash}big ) |S{\_}{\backslash}ell |{\backslash}right) {\$}{\$}ON+M+(nℓℓ$\omega$+ℓ$\Delta$)|Sℓ|. A Matlab implementation of the algorithm proposed here is available for download.",
issn="1432-0541",
doi="10.1007/s00453-019-00552-1",
url="https://doi.org/10.1007/s00453-019-00552-1"
}