FindCycle not working as expected for multigraph

We can find cycles which have repeated vertices (but not repeated edges) by using the line graph:

lg = LineGraph[g]

enter image description here

FindCycle[LineGraph[g], Infinity, All]

enter image description here


The difficulty with multigraphs

One big remaining problem is that Mathematica's graph API does not make it possible to distinguish between parallel edges. Edges are referred to by their endpoints. There are two edges a -> b in this graph, but both are referred to as a -> b.

We can translate the vertex names of the line graph back to the edges of the original graph, but the cycle specifications will be ambiguous:

asc = AssociationThread[VertexList[lg], EdgeList[g]];
Replace[
 FindCycle[LineGraph[g], Infinity, All] /. DirectedEdge -> Rule,
 asc,
 {3}
 ]

enter image description here

As a workaround, do not attempt to refer to edges by their name (endpoints). Simply refer to them by their index, which conveniently coincides with the vertex name in the line graph.


The typical limitation from not being able to distinguish edges is that one can't use graph properties with multigraphs (PropertyValue is ambiguous about which parallel edge is being referred to).

Cycles in multigraphs are another example of how this becomes a problem. I have personally run into this problem when working with cycle bases of multigraphs.

I have reported this serious limitation to Wolfram Support on multiple occasions, but I have not heard back so far anything concrete about whether this would ever be fixed. (It was confirmed that the message was passed on to the developers.) It would clearly require very large changes to the Graph framework.

Other packages handle this issue either by identifying edges through their index (igraph does this, and also Mathematica functions like EdgeCycleMatrix effectively do this) or by adding a unique tag to each edge (networkx does this).


Check the background section of FindCycle documentation:

FindCycle returns simple cycles, while FindHamiltonianCycle, FindEulerianCycle, and FindFundamentalCycles return specific types of cycles.

A simple cycle has no repeating vertices.