DFS find_cycle method is outputting a cycle where there is none

56 views
Skip to first unread message

Taylor Do

unread,
Dec 30, 2020, 1:42:46 PM12/30/20
to networkx-discuss
Hi,
_________________________________________
#test
import networkx as nx

G=nx.DiGraph()

for x in range(0, 11):
  G.add_node(x)

G.add_edges_from(
[ (0, 36), 
 (0, 38), 
 (1, 36), 
 (1, 39), 
 (2, 37), 
 (18, 0), 
 (19, 0), 
 (19, 39), 
 (20, 1), 
(20, 37), 
 (20, 39), 
 (21, 37), 
 (21, 40), 
 (22, 38), 
 (38, 1), 
 (39, 1), 
 (39, 40),
 (40, 2), 
 (40, 38) ])

print(nx.find_cycle(G, source=0))

**After the code is run: output => [(1, 39), (39, 1)]
______________________________________________

When the code above is run, it outputs a directed cycle of [(1, 39), (39, 1)] which obviously is not correct as the graph does not contain a cycle from 0 (as you can tell from the edges). 
Is there something that I am missing here or is there a problem with the find_cycle method that needs to be patched?

Thanks, 
Taylor Do

Dan Schult

unread,
Dec 30, 2020, 1:50:04 PM12/30/20
to networkx...@googlegroups.com
Take a look at the docs:
nx.find_cycle uses a depth first traversal starting at source.
If it finds a cycle during that traversal it reports the cycle.
This cycle might not include the source.

Taylor Do

unread,
Dec 30, 2020, 1:57:34 PM12/30/20
to networkx-discuss
Thank you for your response. 
Is there a method from a library that you know of that would return a cycle that contains the source node?

Dan Schult

unread,
Dec 30, 2020, 4:34:04 PM12/30/20
to networkx...@googlegroups.com
Reply all
Reply to author
Forward
0 new messages