I have a large directional graph with ~250 nodes and ~750 weighted edges. I'm looking for a way to find all cycles of length n containing a specified node. For instance, if I want all cycles of length n
containing node A
in a directional graph DG
, I would do:
import networkx as nx
DG = nx.DiGraph() # assume this is a large graph
cycles = nx.simple_cycles(DG)
n = 4
node = 'A'
relevant_cycles = []
for cycle in cycles:
if len(cycle) == n and node in cycle:
relevant_cycles.append(cycle)
However, this is extremely slow for the large graph I'm dealing with. Is there a better solution to this problem?