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]
FindCycle[LineGraph[g], Infinity, All]
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}
]
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, whileFindHamiltonianCycle
,FindEulerianCycle
, andFindFundamentalCycles
return specific types of cycles.
A simple cycle has no repeating vertices.