## Abstract

We describe a general purpose algorithm for counting simple cycles and simple paths of any length \(\ell \) on a (weighted di)graph on *N* vertices and *M* edges, achieving an asymptotic running time of \(O\left( N+M+\big (\ell ^\omega +\ell \Delta \big ) |S_\ell |\right) \). In this expression, \(|S_\ell |\) is the number of (weakly) connected induced subgraphs of *G* on at most \(\ell \) vertices, \(\Delta \) is the maximum degree of any vertex and \(\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 \((\ell ^{\omega -1}\Delta ^{-1}+1) |S_\ell |\le |\text {Cycle}_\ell |\), with \(|\text {Cycle}_\ell |\) the total number of simple cycles of length at most \(\ell \), 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 an asymptotic running time of \(O\left( N+M+\big (n^\ell \ell ^\omega +\ell \Delta \big ) |S_\ell |\right) \). A Matlab implementation of the algorithm proposed here is available for download.

### Similar content being viewed by others

## Notes

There is one exception to this observation: by extending an approach of Merris to count Hamiltonian cycles [32], we show in Sect. 4 that all simple cycles can be counted with an asymptotic running time scaling as \(\ell \,t_{\text {imm}}(\ell )|S_\ell |\), where \(t_{\text {imm}}(\ell )\) is

*exponential*in \(\ell \). Hence, this extension is still not competitive with the algorithm presented here.Note in this context, backtracks, that is bidirected edges, count as simple cycles. Furthermore the orientation of the cycles counts as well but not their starting point. Thus, for example, the complete graph on three vertices with no self-loops, \(K_3\), has two simple cycles of length 3 and three of length 2.

In addition, we have empirically observed that the running time of the algorithm proposed here scales with an effective parameter \(\Delta _\text {eff}\ll \Delta \). What determines \(\Delta _\text {eff}\) remains unclear.

Even if a reduction to connected induced subgraphs can be devised for this method, which would yield a \(O(t(S_\ell ) + t_{\text {imm}}(\ell )|S_\ell |)\) asymptotic running time, it would only marginally improve upon Merris’ approach and would still be worse than that of the algorithm presented here.

That is, beyond the fact that AYZ is limited to \(\ell =7\) on undirected graphs.

Variants for counting simple paths and for counting simple paths and simple cycles simultaneously are also available at https://uk.mathworks.com/matlabcentral/fileexchange/63850 and https://uk.mathworks.com/matlabcentral/fileexchange/63849, respectively.

## References

Alon, N., Yuster, R., Zwick, U.: Finding and counting given length cycles. Algorithmica

**17**, 209–223 (1997)Amini, Omid, Fomin, Fedor V., Saurabh, Saket: Counting subgraphs via homomorphisms. SIAM J. Discrete Math.

**26**(2), 695–717 (2012)Avis, D., Fukuda, K.: Reverse search for enumeration. Discrete Appl. Math.

**65**, 21–46 (1996)Barabási, Albert-László, Albert, Réka: Emergence of scaling in random networks. Science

**286**(5439), 509–512 (1999)Bax, E., Franklin, J.: A finite-difference sieve to count paths and cycles by length. Inf. Process. Lett.

**60**(4), 171–176 (1996)Bax, Eric T.: Algorithms to count paths and cycles. Inf. Process. Lett.

**52**(5), 249–252 (1994)Birmelé, E., Ferreira, R., Grossi, R., Marino, A., Pisanti, N., Rizzi, R., Sacomoto, G.: Optimal listing of cycles and st-paths in undirected paths. In: Proceedings of the Twenty-Fourth Annual ACM-SIAM Symposium on Discrete Algorithms, pp. 1884–1896 (2013)

Bürgisser, P., Clausen, M., Amin Shokrollahi, M.: Algebraic Complexity Theory, volume 315 of Grundlehren der mathematischen Wissenschaften. Springer, Berlin, New York (1997)

Cash, Gordon G.: The number of \(n\)-cycles in a graph. Appl. Math. Comput.

**184**(2), 1080–1083 (2007)Chang, Y.C., Fu, H.L.: The number of 6-cycles in a graph. Bull. Inst. Combin. Appl.

**39**, 27–30 (2003)Demaine, E.D., Reidl, F., Rossmanith, P., Villaamil, Fernando, S., Sikdar, S., Sullivan, B.D.: Structural sparsity of complex networks: random graph models and linear algorithms. CoRR (2014). arXiv:1406.2587

Dorn, F. (2010) Planar subgraph isomorphism revisited. In: Marion, J.-Y., Schwentick, T. (eds.) 27th International Symposium on Theoretical Aspects of Computer Science (STACS 2010), volume 5 of Leibniz International Proceedings in Informatics (LIPIcs), pp. 263–274. Schloss Dagstuhl–Leibniz-Zentrum fuer Informatik, Dagstuhl, Germany

Downey, R.G., Fellows, M.R.: Parameterized Complexity. Monographs in Computer Science. Springer, New York (1999)

Elbassioni, K.: A polynomial delay algorithm for generating connected induced subgraphs of a given cardinality. J. Graph Algorithms Appl.

**19**, 273–280 (2015)Eppstein, David: Subgraph isomorphism in planar graphs and related problems. J. Graph Algorithms Appl.

**3**(3), 1–27 (1999)Flum, Jörg, Grohe, Martin: The parameterized complexity of counting problems. SIAM J. Comput.

**33**, 892–922 (2004)Giscard, P.-L., Rochet, P.: Enumerating simple paths from connected induced subgraphs (2016). arXiv:1606.00289

Giscard, P.-L., Rochet, P., Wilson, R.C.: An Hopf algebra for counting simple cycles (2016). arXiv:1607.00902

Giscard, P.-L., Rochet, P., Wilson, R.C.: Evaluating balance on social networks from their simple cycles (2016). arXiv:1606.03347

Grohe, M.: Descriptive and parameterized complexity. In: Flum, J., Rodriguez-Artalejo, M. (eds) Computer Science Logic, volume 1683 of Lecture Notes in Computer Science chapter 3, pp. 14–31. Springer-Verlag, Berlin, Heidelberg (1999)

Harary, F., Manvel, B.: On the number of cycles in a graph. Matematický časopis

**21**, 55–63 (1971)Howbert, J.: Count all cycles in simple undirected graph. File Exchange—MATLAB Central—MathWorks (2011). https://uk.mathworks.com/matlabcentral/fileexchange/29438-count-all-cycles-in-simple-undirected-graph

Impagliazzo, R., Paturi, R.: On the complexity of \(k\)-SAT. J. Comput. Syst. Sci.

**62**, 367–375 (2001)Isella, Lorenzo, Stehlé, Juliette, Barrat, Alain, Cattuto, Ciro, Pinton, Jean-François, Van den Broeck, Wouter: What’s in a crowd? Analysis of face-to-face behavioral networks. J. Theor. Biol.

**271**(1), 166–180 (2011)Johnson, D.B.: Finding all the elementary circuits of a directed graph. SIAM J. Comput.

**4**, 77–84 (1975)Karakashian, S., Choueiry, B.Y., Hartke, S.G.: An Algorithm for Generating All Connected Subgraphs with \(k\) Vertices of a Graph. University of Nebraska, student report, UNL-CSE-2013-0005, pp. 1–38 (2013)

Karp, Richard M.: Dynamic programming meets the principle of inclusion and exclusion. Oper. Res. Lett.

**1**(2), 49–51 (1982)Khomenko, N.P., Golovko, L.D.: Identifying certain types of parts of a graph and computing their number. Ukrainskii Matematicheskii Zhurnal

**24**, 385–396 (1972)Khomenko, N.P., Shevchenko, E.N.: The problem of isolating and counting. Ukr. Math. J.

**30**(2), 152–160 (1978)Kunegis, Jérôme: KONECT: The Koblenz network collection. In: Proceedings of the 22nd International Conference on World Wide Web, WWW ’13 Companion, pp. 1343–1350. ACM, New York, NY, USA (2013). Available at: http://konect.uni-koblenz.de/

Mateti, P., Deo, N.: On algorithms for enumerating all circuits of a graph. SIAM J. Comput.

**5**, 90–99 (1976)Merris, Russell: Single-hook characters and Hamiltonian circuits. Linear Multilinear Algebra

**14**, 21–35 (1983)Merris, Russell: Immanantal invariants of graphs. Linear Algebra Appl.

**401**, 67–75 (2005)Movarraei, N., Boxwala, S.A.: On the number of cycles in a graph. Open J. Discrete Math.

**6**, 41–69 (2016)Nesetril, J., de Mendez, P.O.: Sparsity—Graphs, Structures, and Algorithms, volume 28 of Algorithms and combinatorics. Springer, Berlin (2012)

Perepechko, S.N., Voropaev, A.N.: The number of fixed length cycles in an undirected graph. Explicit formulae in case of small lengths. Mathematical Modeling and Computational Physics (MMCP2009), pp. 148–149 (2009)

Perepechko, S.N., Voropaev, A.N.: Количество простых циклов фиксированноЙ длины в неориентированном графе. Явные формулы в случае малых длин. (The Number of Fixed Length Cycles in Undirected Graph. Explicit Formulae in Case of Small Lengths). Вестник РУДН. Серија Математика. Информатика. Физика

**2**5–11 (2012)Schott, R., Staples, G.S.: Complexity of counting cycles using Zeons. Comput. Math. Appl.

**62**(4), 1828–1837 (2011)Uehara, R.: The number of connected components in graphs and its applications. IEICE Technical Report, COMP99-10 (1999)

Valiant, Leslie G.: The complexity of computing the permanent. Theor. Comput. Sci.

**8**, 189–201 (1979)Valiant, Leslie G.: The complexity of enumeration and reliability problems. SIAM J. Comput.

**8**, 410–421 (1979)Wikipedia Adminship Election Data (2016). http://snap.stanford.edu/data/wiki-Elec.html

## Funding

P.-L. Giscard acknowledges financial support from the Royal Commission for the Exhibition of 1851. N. Kriege is supported by the German Science Foundation (DFG) within the Collaborative Research Center SFB 876 “Providing Information by Resource-Constrained Data Analysis”, project A6 “Resource-efficient Graph Mining” .

## Author information

### Authors and Affiliations

### Corresponding author

## Additional information

### Publisher's Note

Springer Nature remains neutral with regard to jurisdictional claims in published maps and institutional affiliations.

## Rights and permissions

## About this article

### Cite this article

Giscard, PL., Kriege, N. & Wilson, R.C. A General Purpose Algorithm for Counting Simple Cycles and Simple Paths of Any Length.
*Algorithmica* **81**, 2716–2737 (2019). https://doi.org/10.1007/s00453-019-00552-1

Received:

Accepted:

Published:

Issue Date:

DOI: https://doi.org/10.1007/s00453-019-00552-1

### Keywords

- Simple cycles
- Simple paths
- Self-avoiding walks
- Self-avoiding polygons
- Elementary circuits
- Connected induced subgraphs
- Networks
- Graphs
- Digraphs
- Labeled graphs