Why doesn't the NetworkX find_cycle function yield outputs that involve the specified source vertex?

Viewed 229

Take the following undirected graph object in NetworkX.

G_3 = nx.Graph()
G_3.add_nodes_from([1,2,3,4,5])
G_3.add_weighted_edges_from([(1,2,1), (2,1,1), (2,4,1), (4,2,1), (2,3,1), (3,2,1), (3,4,1), (4,3,1), (1,4,1), (4,1,1),(5,2,1),(5,1,1)])

The graph looks like this. There are three basic cycles, and I want to use the functions nx.cycle_basis and nx.find_cycle to obtain lists of nodes and edges that comprise each cycle.

enter image description here

When I use nx.cycle_basis, I get the following result:

cycles_3 = [c for c in nx.cycle_basis(G_3)]
print(cycles_3)
>>> [[2, 5, 1], [2, 4, 1], [2, 3, 4]] 

This is the correct output. However, when I use the nx.find_cycle function, I end up with the follwoing output:

num_nodes = len(G_3.nodes())
edges_in_cycle = {}

for i in range(1,num_nodes+1):
  edges_in_cycle[i] = nx.find_cycle(G_3,i)`

edges_in_cycle
>>>
{1: [(2, 4), (4, 3), (3, 2)],
 2: [(2, 1), (1, 4), (4, 2)],
 3: [(2, 1), (1, 4), (4, 2)],
 4: [(4, 2), (2, 1), (1, 4)],
 5: [(2, 1), (1, 4), (4, 2)]}

How does nx.find_cycle(G_3,source=5) result in an output that does not involve the vertex 5? Please let me know how I can extract the edge information for that particular cycle with this function.

0 Answers
Related